Skip to content

Commit 110f75a

Browse files
authored
IBX-10565: Fallback configuration when failing init (#273)
1 parent a754775 commit 110f75a

File tree

1 file changed

+96
-79
lines changed

1 file changed

+96
-79
lines changed

src/bundle/Resources/public/js/CKEditor/core/base-ckeditor.js

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -249,106 +249,123 @@ const VIEWPORT_TOP_OFFSET_DISTRACTION_FREE_MODE = 0;
249249
const customEvent = new CustomEvent('ibexa-ckeditor:configure', {
250250
detail: { container, config },
251251
});
252+
const createCKEditor = () => {
253+
CKEditor.create(container, config).then((editor) => {
254+
this.editor = editor;
252255

253-
doc.body.dispatchEvent(customEvent);
256+
const editableUIView = editor.ui.view.editable;
257+
const editableElement = this.editor.editing.view.getDomRoot();
258+
const editorToolbarPanelInstance = this.editor.ui.view.panel;
259+
const initialData = this.getData();
260+
const updateInput = (data, shouldFireInputEvent = true) => {
261+
const textarea = container.closest('.ibexa-data-source').querySelector('textarea');
254262

255-
CKEditor.create(container, config).then((editor) => {
256-
this.editor = editor;
263+
textarea.value = this.xhtmlify(data).replace(this.xhtmlNamespace, this.ezNamespace);
257264

258-
const editableUIView = editor.ui.view.editable;
259-
const editableElement = this.editor.editing.view.getDomRoot();
260-
const editorToolbarPanelInstance = this.editor.ui.view.panel;
261-
const initialData = this.getData();
262-
const updateInput = (data, shouldFireInputEvent = true) => {
263-
const textarea = container.closest('.ibexa-data-source').querySelector('textarea');
265+
if (shouldFireInputEvent) {
266+
textarea.dispatchEvent(new Event('input'));
267+
}
268+
};
269+
const setDataSourceHeight = (toolbarNode, fieldEditNode) => {
270+
const dataSourceNode = fieldEditNode.querySelector('.ibexa-data-source');
271+
const { height: toolbarHeight } = toolbarNode.getBoundingClientRect();
272+
const { top: dataSourceTop } = dataSourceNode.getBoundingClientRect();
264273

265-
textarea.value = this.xhtmlify(data).replace(this.xhtmlNamespace, this.ezNamespace);
274+
if (toolbarHeight > dataSourceTop) {
275+
const positionDiff = toolbarHeight - dataSourceTop;
266276

267-
if (shouldFireInputEvent) {
268-
textarea.dispatchEvent(new Event('input'));
269-
}
270-
};
271-
const setDataSourceHeight = (toolbarNode, fieldEditNode) => {
272-
const dataSourceNode = fieldEditNode.querySelector('.ibexa-data-source');
273-
const { height: toolbarHeight } = toolbarNode.getBoundingClientRect();
274-
const { top: dataSourceTop } = dataSourceNode.getBoundingClientRect();
277+
dataSourceNode.style.height = `calc(100% - ${positionDiff}px)`;
278+
dataSourceNode.style.marginTop = `${positionDiff}px`;
279+
}
280+
};
281+
const clearDataSourceHeight = () => {
282+
const fieldEditNode = editableElement.closest('.ibexa-field-edit');
283+
const dataSourceNode = fieldEditNode.querySelector('.ibexa-data-source');
275284

276-
if (toolbarHeight > dataSourceTop) {
277-
const positionDiff = toolbarHeight - dataSourceTop;
285+
dataSourceNode.style.removeProperty('height');
286+
dataSourceNode.style.removeProperty('margin-top');
287+
};
288+
const setToolbarMaxWidth = (toolbarNode, fieldEditNode) => {
289+
const distractionFreeModeControlNodeBtn = fieldEditNode.querySelector(
290+
'.ibexa-field-edit__distraction-free-mode-control-container .ibexa-field-edit__distraction-free-mode-btns',
291+
);
278292

279-
dataSourceNode.style.height = `calc(100% - ${positionDiff}px)`;
280-
dataSourceNode.style.marginTop = `${positionDiff}px`;
281-
}
282-
};
283-
const clearDataSourceHeight = () => {
284-
const fieldEditNode = editableElement.closest('.ibexa-field-edit');
285-
const dataSourceNode = fieldEditNode.querySelector('.ibexa-data-source');
286-
287-
dataSourceNode.style.removeProperty('height');
288-
dataSourceNode.style.removeProperty('margin-top');
289-
};
290-
const setToolbarMaxWidth = (toolbarNode, fieldEditNode) => {
291-
const distractionFreeModeControlNodeBtn = fieldEditNode.querySelector(
292-
'.ibexa-field-edit__distraction-free-mode-control-container .ibexa-field-edit__distraction-free-mode-btns',
293-
);
294-
295-
const dataSourceNode = fieldEditNode.querySelector('.ibexa-data-source');
296-
const { offsetWidth: dataSourceNodeWidth } = dataSourceNode;
297-
let toolbarNodeMaxWidth = dataSourceNodeWidth;
298-
299-
if (distractionFreeModeControlNodeBtn !== null) {
300-
const { offsetWidth: distractionFreeModeControlNodeBtnWidth } = distractionFreeModeControlNodeBtn;
301-
302-
toolbarNodeMaxWidth = dataSourceNodeWidth - distractionFreeModeControlNodeBtnWidth;
303-
}
293+
const dataSourceNode = fieldEditNode.querySelector('.ibexa-data-source');
294+
const { offsetWidth: dataSourceNodeWidth } = dataSourceNode;
295+
let toolbarNodeMaxWidth = dataSourceNodeWidth;
304296

305-
toolbarNode.style.maxWidth = `${toolbarNodeMaxWidth}px`;
306-
};
297+
if (distractionFreeModeControlNodeBtn !== null) {
298+
const { offsetWidth: distractionFreeModeControlNodeBtnWidth } = distractionFreeModeControlNodeBtn;
307299

308-
updateInput(initialData, false);
300+
toolbarNodeMaxWidth = dataSourceNodeWidth - distractionFreeModeControlNodeBtnWidth;
301+
}
309302

310-
this.editor.model.document.on('change:data', () => {
311-
const data = this.getData();
303+
toolbarNode.style.maxWidth = `${toolbarNodeMaxWidth}px`;
304+
};
312305

313-
updateInput(data);
314-
});
306+
updateInput(initialData, false);
315307

316-
this.editor.on('set:distractionFreeModeActive', ({ source: eventEditorInstance }, name, value) => {
317-
const { ui: eventEditorUiInstance } = eventEditorInstance;
318-
const { panel: eventEditorToolbarPanelInstance } = eventEditorUiInstance.view;
319-
const toolbarPanelNode = eventEditorToolbarPanelInstance.element;
320-
const toolbarPanelsContainer = toolbarPanelNode.closest('.ck-body');
308+
this.editor.model.document.on('change:data', () => {
309+
const data = this.getData();
321310

322-
eventEditorUiInstance.viewportOffset = {
323-
top: value ? VIEWPORT_TOP_OFFSET_DISTRACTION_FREE_MODE : this.viewportTopOffset,
324-
};
311+
updateInput(data);
312+
});
325313

326-
toolbarPanelsContainer.classList.toggle('ck-body--distraction-free-mode-active');
314+
this.editor.on('set:distractionFreeModeActive', ({ source: eventEditorInstance }, name, value) => {
315+
const { ui: eventEditorUiInstance } = eventEditorInstance;
316+
const { panel: eventEditorToolbarPanelInstance } = eventEditorUiInstance.view;
317+
const toolbarPanelNode = eventEditorToolbarPanelInstance.element;
318+
const toolbarPanelsContainer = toolbarPanelNode.closest('.ck-body');
327319

328-
if (!value) {
329-
eventEditorToolbarPanelInstance.hide();
330-
clearDataSourceHeight();
331-
}
332-
});
320+
eventEditorUiInstance.viewportOffset = {
321+
top: value ? VIEWPORT_TOP_OFFSET_DISTRACTION_FREE_MODE : this.viewportTopOffset,
322+
};
333323

334-
editableUIView.on('change:isFocused', (event) => {
335-
const containerDataSource =
336-
container.closest('.ibexa-field-edit--ezrichtext .ibexa-data-source') ??
337-
container.closest('.ibexa-data-source--richtext');
324+
toolbarPanelsContainer.classList.toggle('ck-body--distraction-free-mode-active');
338325

339-
containerDataSource.classList.toggle('ibexa-data-source--focused', event.source.isFocused);
340-
});
326+
if (!value) {
327+
eventEditorToolbarPanelInstance.hide();
328+
clearDataSourceHeight();
329+
}
330+
});
341331

342-
editorToolbarPanelInstance.on('change:isVisible', ({ source: eventBalloonPanelViewInstance }) => {
343-
const fieldEditNode = editableElement.closest('.ibexa-field-edit');
332+
global.setTimeout(() => {
333+
if (this.editor.isReadOnly) {
334+
this.editor.destroy();
344335

345-
setToolbarMaxWidth(eventBalloonPanelViewInstance.element, fieldEditNode);
336+
const customEventFallback = new CustomEvent('ibexa-ckeditor:fallback-configuration', {
337+
detail: { container, config },
338+
});
346339

347-
if (editor?.distractionFreeModeActive) {
348-
setDataSourceHeight(eventBalloonPanelViewInstance.element, fieldEditNode);
349-
}
340+
doc.body.dispatchEvent(customEventFallback);
341+
342+
createCKEditor();
343+
}
344+
}, 2000);
345+
346+
editableUIView.on('change:isFocused', (event) => {
347+
const containerDataSource =
348+
container.closest('.ibexa-field-edit--ezrichtext .ibexa-data-source') ??
349+
container.closest('.ibexa-data-source--richtext');
350+
351+
containerDataSource.classList.toggle('ibexa-data-source--focused', event.source.isFocused);
352+
});
353+
354+
editorToolbarPanelInstance.on('change:isVisible', ({ source: eventBalloonPanelViewInstance }) => {
355+
const fieldEditNode = editableElement.closest('.ibexa-field-edit');
356+
357+
setToolbarMaxWidth(eventBalloonPanelViewInstance.element, fieldEditNode);
358+
359+
if (editor?.distractionFreeModeActive) {
360+
setDataSourceHeight(eventBalloonPanelViewInstance.element, fieldEditNode);
361+
}
362+
});
350363
});
351-
});
364+
};
365+
366+
doc.body.dispatchEvent(customEvent);
367+
368+
createCKEditor();
352369
}
353370
}
354371

0 commit comments

Comments
 (0)