Skip to content

Commit 61d048a

Browse files
committed
TASK: Cleanup custom bodycollection code
1 parent 1351ed6 commit 61d048a

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

packages/neos-ui-ckeditor5-bindings/src/ckEditorApi.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {cleanupContentBeforeCommit} from './cleanupContentBeforeCommit'
66
// FIXME import from @ckeditor/ckeditor5-engine/theme/placeholder.css instead! (Needs build setup configuration)
77
import './cke-overwrites.vanilla-css';
88
import './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

1112
let currentEditor = null;
1213
let 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
*/
4648
class 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

packages/neos-ui-guest-frame/src/dom.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@ import style from './style.module.css';
1010
// Get the guest frame's document object
1111
//
1212
export const getGuestFrameDocument = () => {
13-
const guestFrame = document.getElementsByName('neos-content-main')[0];
13+
const guestFrame = getGuestFrame();
1414
return guestFrame && guestFrame.contentDocument;
1515
};
1616

1717
//
1818
// Get the guest frame's window object
1919
//
2020
export const getGuestFrameWindow = () => {
21-
const guestFrame = document.getElementsByName('neos-content-main')[0];
21+
const guestFrame = getGuestFrame();
2222
return guestFrame && guestFrame.contentWindow;
2323
};
2424

25+
//
26+
// Get the guest frame
27+
//
28+
export const getGuestFrame = () => {
29+
return document.getElementsByName('neos-content-main')[0];
30+
};
31+
2532
//
2633
// Get the guest frame's body DOM node
2734
//

0 commit comments

Comments
 (0)