@@ -6,7 +6,8 @@ import {cleanupContentBeforeCommit} from './cleanupContentBeforeCommit'
66// FIXME import from @ckeditor /ckeditor5-engine/theme/placeholder.css instead! (Needs build setup configuration)
77import './cke-overwrites.vanilla-css' ;
88import './placeholder.vanilla-css' ;
9- import { createElement } from "@ckeditor/ckeditor5-utils" ;
9+ import { createElement } from '@ckeditor/ckeditor5-utils' ;
10+ import { getGuestFrame , getGuestFrameDocument } from '@neos-project/neos-ui-guest-frame/src/dom' ;
1011
1112let currentEditor = null ;
1213let editorConfig = { } ;
@@ -41,7 +42,8 @@ export const bootstrap = _editorConfig => {
4142
4243/**
4344 * A custom BodyCollection implementation that attaches to the DOM of the guest frame.
44- * This is necessary because the editor runs in a separate iframe and needs to manage its own body
45+ * This is necessary because the editor runs in a separate iframe and needs to manage its own body.
46+ * The editor doesn't allow a custom position for the collection currently. See https://github.com/ckeditor/ckeditor5/issues/5319
4547 */
4648class GuestFrameBodyCollection extends BodyCollection {
4749 attachToDom ( ) {
@@ -60,25 +62,28 @@ class GuestFrameBodyCollection extends BodyCollection {
6062 children : this
6163 } ) . render ( ) ;
6264
63- // Get the current document instance each time
64- const iframe = document . querySelector ( '[name="neos-content-main"]' ) ;
65- const documentForWrapper = iframe ?. contentDocument || iframe ?. contentWindow ?. document ;
65+ const guestFrame = getGuestFrame ( ) ;
66+ const guestFrameDocument = getGuestFrameDocument ( ) ;
6667
67- // Ensure we have a valid document and it's loaded
68- if ( ! documentForWrapper || documentForWrapper . readyState === 'loading' ) {
69- // Wait for document to be ready if it's still loading
70- iframe . addEventListener ( 'load' , ( ) => this . attachToDom ( ) , { once : true } ) ;
68+ if ( ! guestFrameDocument || guestFrameDocument . readyState === 'loading' ) {
69+ // When we navigate to other documents we need to reattach the body collection after the guest frame is loaded.
70+ guestFrame . addEventListener ( 'load' , ( ) => this . attachToDom ( ) , { once : true } ) ;
7171 return ;
7272 }
7373
7474 // Create a shared wrapper if there were none or the previous one got disconnected from DOM
75- if ( ! BodyCollection . _bodyWrapper || ! BodyCollection . _bodyWrapper . isConnected ||
76- BodyCollection . _bodyWrapper . ownerDocument !== documentForWrapper ) {
77- BodyCollection . _bodyWrapper = createElement ( documentForWrapper , 'div' , { class : 'ck-body-wrapper' } ) ;
78- documentForWrapper . body . appendChild ( BodyCollection . _bodyWrapper ) ;
75+ // This wrapper is stored as a static property to ensure it is reused across instances.
76+ if ( ! GuestFrameBodyCollection . _bodyWrapper || ! GuestFrameBodyCollection . _bodyWrapper . isConnected ||
77+ GuestFrameBodyCollection . _bodyWrapper . ownerDocument !== guestFrameDocument ) {
78+ GuestFrameBodyCollection . _bodyWrapper = createElement (
79+ guestFrameDocument ,
80+ 'div' ,
81+ { class : 'ck-body-wrapper' }
82+ ) ;
83+ guestFrameDocument . body . appendChild ( GuestFrameBodyCollection . _bodyWrapper ) ;
7984 }
8085
81- BodyCollection . _bodyWrapper . appendChild ( this . _bodyCollectionContainer ) ;
86+ GuestFrameBodyCollection . _bodyWrapper . appendChild ( this . _bodyCollectionContainer ) ;
8287 }
8388}
8489
0 commit comments