@@ -57,7 +57,11 @@ export default class Preview extends BasePreview {
57
57
this . element = element ;
58
58
element . id = this . parent . id + "-editor" ;
59
59
60
- // This function is called whenever a render happens, meaning our WYSIWYG instance is destroyed
60
+ /**
61
+ * afterRenderWysiwyg is called whenever Knockout causes a DOM re-render. This occurs frequently within Slider
62
+ * due to Slick's inability to perform a refresh with Knockout managing the DOM. Due to this the original
63
+ * WYSIWYG instance will be detached from this slide and we need to re-initialize on click.
64
+ */
61
65
this . wysiwyg = null ;
62
66
}
63
67
@@ -355,6 +359,8 @@ export default class Preview extends BasePreview {
355
359
const selection = window . getSelection ( ) ;
356
360
if ( selection . getRangeAt && selection . rangeCount ) {
357
361
const range = selection . getRangeAt ( 0 ) . cloneRange ( ) ;
362
+ $ ( range . startContainer . parentNode ) . attr ( "data-startContainer" , "true" ) ;
363
+ $ ( range . endContainer . parentNode ) . attr ( "data-endContainer" , "true" ) ;
358
364
return {
359
365
startContainer : range . startContainer ,
360
366
startOffset : range . startOffset ,
@@ -375,10 +381,20 @@ export default class Preview extends BasePreview {
375
381
private restoreSelection ( element : HTMLElement , selection : Selection ) {
376
382
if ( window . getSelection ) {
377
383
// Find the original container that had the selection
378
- const startContainer : HTMLElement = this . findTextNode ( element , selection . startContainer . nodeValue ) ;
384
+ const startContainerParent = $ ( element ) . find ( "[data-startContainer]" ) ;
385
+ startContainerParent . removeAttr ( "data-startContainer" ) ;
386
+ const startContainer : HTMLElement = this . findTextNode (
387
+ startContainerParent ,
388
+ selection . startContainer . nodeValue ,
389
+ ) ;
390
+ const endContainerParent = $ ( element ) . find ( "[data-endContainer]" ) ;
391
+ endContainerParent . removeAttr ( "data-endContainer" ) ;
379
392
let endContainer : HTMLElement = startContainer ;
380
393
if ( selection . endContainer . nodeValue !== selection . startContainer . nodeValue ) {
381
- endContainer = this . findTextNode ( element , selection . endContainer . nodeValue ) ;
394
+ endContainer = this . findTextNode (
395
+ endContainerParent ,
396
+ selection . endContainer . nodeValue ,
397
+ ) ;
382
398
}
383
399
384
400
if ( startContainer && endContainer ) {
@@ -400,15 +416,11 @@ export default class Preview extends BasePreview {
400
416
* @param {string } text
401
417
* @returns {HTMLElement }
402
418
*/
403
- private findTextNode ( element : HTMLElement , text : string ) : HTMLElement {
419
+ private findTextNode ( element : JQuery , text : string ) : HTMLElement {
404
420
if ( text && text . trim ( ) . length > 0 ) {
405
- const textSearch = $ ( element ) . find ( ":contains(\"" + text . trim ( ) + "\")" ) ;
406
- if ( textSearch . length > 0 ) {
407
- // Search for the #text node within the element for the new range
408
- return textSearch . last ( ) . contents ( ) . filter ( function ( ) {
409
- return this . nodeType === Node . TEXT_NODE && text === this . nodeValue ;
410
- } ) [ 0 ] ;
411
- }
421
+ return element . contents ( ) . filter ( function ( ) {
422
+ return this . nodeType === Node . TEXT_NODE && text === this . nodeValue ;
423
+ } ) [ 0 ] ;
412
424
}
413
425
}
414
426
}
0 commit comments