-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
ckEditorApi.js
89 lines (76 loc) · 3.38 KB
/
ckEditorApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import debounce from 'lodash.debounce';
import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
import {actions} from '@neos-project/neos-ui-redux-store';
// We remove opening and closing span tags that are produced by the inlineMode plugin
const cleanupContentBeforeCommit = content => {
// TODO: remove when this is fixed: https://github.com/ckeditor/ckeditor5/issues/401
if (content.match(/^<([a-z][a-z0-9]*)\b[^>]*> <\/\1>$/)) {
return '';
}
if (content.match(/^<span>/) && content.match(/<\/span>$/)) {
return content
.replace(/^<span>/, '')
.replace(/<\/span>$/, '');
}
return content;
};
let currentEditor = null;
let editorConfig = {};
// We cache the "formattingUnderCursor"; to only emit events when it really changed.
// As there is only a single cursor active at any given time, it is safe to do this caching here inside the singleton object.
let lastFormattingUnderCursorSerialized = '';
// We get the state of all commands from CKE5 and serialize it into "formattingUnderCursor"
const handleUserInteractionCallback = () => {
if (!currentEditor) {
return;
}
const formattingUnderCursor = {};
[...currentEditor.commands].forEach(commandTuple => {
const [commandName, command] = commandTuple;
if (command.value !== undefined) {
formattingUnderCursor[commandName] = command.value;
}
});
const formattingUnderCursorSerialized = JSON.stringify(formattingUnderCursor);
if (formattingUnderCursorSerialized !== lastFormattingUnderCursorSerialized) {
editorConfig.setFormattingUnderCursor(formattingUnderCursor);
lastFormattingUnderCursorSerialized = formattingUnderCursorSerialized;
}
};
export const bootstrap = _editorConfig => {
editorConfig = _editorConfig;
};
export const createEditor = store => options => {
const {propertyDomNode, propertyName, editorOptions, globalRegistry, userPreferences, onChange} = options;
const ckEditorConfig = editorConfig.configRegistry.getCkeditorConfig({
editorOptions,
userPreferences,
globalRegistry,
propertyDomNode
});
DecoupledEditor
.create(propertyDomNode, ckEditorConfig)
.then(editor => {
editor.ui.focusTracker.on('change:isFocused', event => {
if (event.source.isFocused) {
currentEditor = editor;
editorConfig.setCurrentlyEditedPropertyName(propertyName);
handleUserInteractionCallback();
}
});
editor.keystrokes.set('Ctrl+K', (_, cancel) => {
store.dispatch(actions.UI.ContentCanvas.toggleLinkEditor());
cancel();
});
// We attach all options for this editor to the editor DOM node, so it would be easier to access them from CKE plugins
editor.neos = options;
editor.model.document.on('change', () => handleUserInteractionCallback());
editor.model.document.on('change:data', debounce(() => onChange(cleanupContentBeforeCommit(editor.getData())), 500, {maxWait: 5000}));
}).catch(e => console.error(e));
};
export const executeCommand = (command, argument, reFocusEditor = true) => {
currentEditor.execute(command, argument);
if (reFocusEditor) {
currentEditor.editing.view.focus();
}
};