@@ -37,7 +37,6 @@ ns.Html.prototype.inButtons = function (button) {
37
37
38
38
ns . Html . prototype . getCKEditorConfig = function ( ) {
39
39
const config = {
40
- updateSourceElementOnDestroy : true ,
41
40
plugins : [ 'Essentials' , 'Paragraph' ] ,
42
41
alignment : { options : [ "left" , "center" , "right" ] } ,
43
42
toolbar : [ ] ,
@@ -415,9 +414,6 @@ ns.Html.prototype.appendTo = function ($wrapper) {
415
414
this . ckEditorConfig = this . getCKEditorConfig ( ) ;
416
415
417
416
this . $input . focus ( function ( ) {
418
- // Blur is not fired on destroy. Therefore we need to keep track of it!
419
- var blurFired = false ;
420
-
421
417
// Remove placeholder
422
418
that . $placeholder = that . $item . find ( '.h5peditor-ckeditor-placeholder' ) . detach ( ) ;
423
419
@@ -484,7 +480,7 @@ ns.Html.prototype.appendTo = function ($wrapper) {
484
480
485
481
// Remove overflow protection on startup
486
482
let initialData = editor . getData ( ) ;
487
- if ( initialData . includes ( 'table-overflow-protection' ) ) {
483
+ if ( initialData . includes ( '<div class=" table-overflow-protection" ' ) ) {
488
484
initialData = initialData . replace ( / < d i v c l a s s = \" t a b l e - o v e r f l o w - p r o t e c t i o n \" > .* < \/ d i v > / , '' ) ;
489
485
editor . setData ( initialData ) ;
490
486
}
@@ -506,45 +502,24 @@ ns.Html.prototype.appendTo = function ($wrapper) {
506
502
editor . editing . view . focus ( ) ;
507
503
508
504
editor . on ( 'focus' , function ( ) {
509
- blurFired = false ;
510
505
editorElement . style . maxHeight = getEditorHeight ( ) ;
511
506
} ) ;
512
507
513
508
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
519
514
520
515
// Display placeholder if:
521
516
// -- The value held by the field is empty AND
522
517
// -- The value shown in the UI is empty AND
523
518
// -- 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 ) ) {
526
520
that . $placeholder . appendTo ( that . $item . find ( '.ckeditor' ) ) ;
527
521
}
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
- }
536
522
} ) ;
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 ) ;
548
523
} )
549
524
. catch ( error => {
550
525
throw new Error ( 'Error loading CKEditor: ' + error ) ;
@@ -577,7 +552,7 @@ ns.Html.prototype.createHtml = function () {
577
552
input += '<span class="h5peditor-ckeditor-placeholder">' + this . field . placeholder + '</span>' ;
578
553
}
579
554
// 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" ' ) ) {
581
556
input += '<div class="table-overflow-protection"></div>' ;
582
557
}
583
558
input += '</div>' ;
@@ -596,11 +571,8 @@ ns.Html.prototype.validate = function () {
596
571
this . $input . addClass ( 'error' ) ;
597
572
}
598
573
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 ( ) ;
604
576
605
577
value = value
606
578
// Remove placeholder text if any:
@@ -628,12 +600,6 @@ ns.Html.prototype.validate = function () {
628
600
}
629
601
} ) ;
630
602
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
-
637
603
value = $value . html ( ) ;
638
604
639
605
// Display errors and bail if set.
@@ -644,9 +610,13 @@ ns.Html.prototype.validate = function () {
644
610
this . $input . removeClass ( 'error' ) ;
645
611
}
646
612
613
+ if ( value . includes ( '<table' ) && ! value . includes ( '<div class="table-overflow-protection"' ) ) {
614
+ value = value + '<div class="table-overflow-protection"></div>' ;
615
+ }
616
+
647
617
this . value = value ;
648
618
this . setValue ( this . field , value ) ;
649
- this . $input . change ( ) ; // Trigger change event.
619
+ this . $input . html ( value ) . change ( ) ; // Trigger change event.
650
620
651
621
return value ;
652
622
} ;
0 commit comments