Skip to content

Commit ce5aec3

Browse files
committed
Merge branch 'master' of https://github.com/h5p/h5p-editor-php-library into fix/AU-6854-rename-reuseContentTabLabel
2 parents 70f88c2 + e0dbeeb commit ce5aec3

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

scripts/h5peditor-html.js

+15-45
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ ns.Html.prototype.inButtons = function (button) {
3737

3838
ns.Html.prototype.getCKEditorConfig = function () {
3939
const config = {
40-
updateSourceElementOnDestroy: true,
4140
plugins: ['Essentials', 'Paragraph'],
4241
alignment: { options: ["left", "center", "right"] },
4342
toolbar: [],
@@ -415,9 +414,6 @@ ns.Html.prototype.appendTo = function ($wrapper) {
415414
this.ckEditorConfig = this.getCKEditorConfig();
416415

417416
this.$input.focus(function () {
418-
// Blur is not fired on destroy. Therefore we need to keep track of it!
419-
var blurFired = false;
420-
421417
// Remove placeholder
422418
that.$placeholder = that.$item.find('.h5peditor-ckeditor-placeholder').detach();
423419

@@ -484,7 +480,7 @@ ns.Html.prototype.appendTo = function ($wrapper) {
484480

485481
// Remove overflow protection on startup
486482
let initialData = editor.getData();
487-
if (initialData.includes('table-overflow-protection')) {
483+
if (initialData.includes('<div class="table-overflow-protection"')) {
488484
initialData = initialData.replace(/<div class=\"table-overflow-protection\">.*<\/div>/, '');
489485
editor.setData(initialData);
490486
}
@@ -506,45 +502,24 @@ ns.Html.prototype.appendTo = function ($wrapper) {
506502
editor.editing.view.focus();
507503

508504
editor.on('focus', function () {
509-
blurFired = false;
510505
editorElement.style.maxHeight = getEditorHeight();
511506
});
512507

513508
editor.once('destroy', function () {
514-
// In some cases, the blur event is not fired. Need to be sure it is, so that
515-
// validation and saving is done
516-
if (!blurFired) {
517-
blur();
518-
}
509+
510+
// We always need to run validate when removing CKE5 to have the .$input properly populated.
511+
// CKE5 cannot do this as the data has to be massaged/filtered before updating the .$input.
512+
const value = that.validate();
513+
delete that.ckeditor; // Prevent usage of destroyed CK beyond this point
519514

520515
// Display placeholder if:
521516
// -- The value held by the field is empty AND
522517
// -- The value shown in the UI is empty AND
523518
// -- A placeholder is defined
524-
const value = editor.getData();
525-
if (that.$placeholder.length !== 0 && (value === undefined || value.length === 0) && (that.value === undefined || that.value.length === 0)) {
519+
if (that.$placeholder.length !== 0 && (value === undefined || value.length === 0)) {
526520
that.$placeholder.appendTo(that.$item.find('.ckeditor'));
527521
}
528-
529-
// Since validate() is not always run,
530-
// make sure tabe overflow protection is added always when editor is destroyed
531-
if (value.includes('table') && !value.includes('table-overflow-protection')) {
532-
that.value = value + '<div class="table-overflow-protection"></div>';
533-
that.setValue(that.field, that.value);
534-
that.$input.html(that.value).change();
535-
}
536522
});
537-
538-
var blur = function () {
539-
blurFired = true;
540-
541-
// Do not validate if the field has been hidden.
542-
if (that.$item.is(':visible')) {
543-
that.validate();
544-
}
545-
};
546-
547-
editor.on('blur', blur);
548523
})
549524
.catch(error => {
550525
throw new Error('Error loading CKEditor: ' + error);
@@ -577,7 +552,7 @@ ns.Html.prototype.createHtml = function () {
577552
input += '<span class="h5peditor-ckeditor-placeholder">' + this.field.placeholder + '</span>';
578553
}
579554
// Add overflow protection if table
580-
if (this.field.tags.includes('table') && !input.includes('table-overflow-protection')) {
555+
if (this.field.tags.includes('table') && !input.includes('<div class="table-overflow-protection"')) {
581556
input += '<div class="table-overflow-protection"></div>';
582557
}
583558
input += '</div>';
@@ -596,11 +571,8 @@ ns.Html.prototype.validate = function () {
596571
this.$input.addClass('error');
597572
}
598573

599-
// Get contents from editor
600-
// If there are more than one ckeditor, getData() might be undefined when ckeditor is not
601-
let value = ((this.ckeditor !== undefined && this.ckeditor.getData() !== undefined)
602-
? this.ckeditor.getData()
603-
: this.$input.html());
574+
// Get contents from CKEditor5
575+
let value = this.ckeditor ? this.ckeditor.getData() : this.$input.html();
604576

605577
value = value
606578
// Remove placeholder text if any:
@@ -628,12 +600,6 @@ ns.Html.prototype.validate = function () {
628600
}
629601
});
630602

631-
// Add overflow protection if chance of aligned tables
632-
if(that.inTags('table') && !value.includes('table-overflow-protection')) {
633-
this.$input.append('<div class="table-overflow-protection"></div>');
634-
$value.append('<div class="table-overflow-protection"></div>');
635-
}
636-
637603
value = $value.html();
638604

639605
// Display errors and bail if set.
@@ -644,9 +610,13 @@ ns.Html.prototype.validate = function () {
644610
this.$input.removeClass('error');
645611
}
646612

613+
if (value.includes('<table') && !value.includes('<div class="table-overflow-protection"')) {
614+
value = value + '<div class="table-overflow-protection"></div>';
615+
}
616+
647617
this.value = value;
648618
this.setValue(this.field, value);
649-
this.$input.change(); // Trigger change event.
619+
this.$input.html(value).change(); // Trigger change event.
650620

651621
return value;
652622
};

0 commit comments

Comments
 (0)