@ckeditor/ckeditor5-editor-classic 0.0.0-internal-20241017.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,467 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import { EditorUI, normalizeToolbarConfig, DialogView, BoxedEditorUIView, StickyPanelView, ToolbarView, MenuBarView, InlineEditableUIView } from '@ckeditor/ckeditor5-ui/dist/index.js';
6
+ import { enablePlaceholder } from '@ckeditor/ckeditor5-engine/dist/index.js';
7
+ import { ElementReplacer, Rect, CKEditorError, getDataFromElement } from '@ckeditor/ckeditor5-utils/dist/index.js';
8
+ import { ElementApiMixin, Editor, attachToForm } from '@ckeditor/ckeditor5-core/dist/index.js';
9
+ import { isElement as isElement$1 } from 'lodash-es';
10
+
11
+ /**
12
+ * The classic editor UI class.
13
+ */ class ClassicEditorUI extends EditorUI {
14
+ /**
15
+ * The main (top–most) view of the editor UI.
16
+ */ view;
17
+ /**
18
+ * A normalized `config.toolbar` object.
19
+ */ _toolbarConfig;
20
+ /**
21
+ * The element replacer instance used to hide the editor's source element.
22
+ */ _elementReplacer;
23
+ /**
24
+ * Creates an instance of the classic editor UI class.
25
+ *
26
+ * @param editor The editor instance.
27
+ * @param view The view of the UI.
28
+ */ constructor(editor, view){
29
+ super(editor);
30
+ this.view = view;
31
+ this._toolbarConfig = normalizeToolbarConfig(editor.config.get('toolbar'));
32
+ this._elementReplacer = new ElementReplacer();
33
+ this.listenTo(editor.editing.view, 'scrollToTheSelection', this._handleScrollToTheSelectionWithStickyPanel.bind(this));
34
+ }
35
+ /**
36
+ * @inheritDoc
37
+ */ get element() {
38
+ return this.view.element;
39
+ }
40
+ /**
41
+ * Initializes the UI.
42
+ *
43
+ * @param replacementElement The DOM element that will be the source for the created editor.
44
+ */ init(replacementElement) {
45
+ const editor = this.editor;
46
+ const view = this.view;
47
+ const editingView = editor.editing.view;
48
+ const editable = view.editable;
49
+ const editingRoot = editingView.document.getRoot();
50
+ // The editable UI and editing root should share the same name. Then name is used
51
+ // to recognize the particular editable, for instance in ARIA attributes.
52
+ editable.name = editingRoot.rootName;
53
+ view.render();
54
+ // The editable UI element in DOM is available for sure only after the editor UI view has been rendered.
55
+ // But it can be available earlier if a DOM element has been passed to BalloonEditor.create().
56
+ const editableElement = editable.element;
57
+ // Register the editable UI view in the editor. A single editor instance can aggregate multiple
58
+ // editable areas (roots) but the classic editor has only one.
59
+ this.setEditableElement(editable.name, editableElement);
60
+ // Let the editable UI element respond to the changes in the global editor focus
61
+ // tracker. It has been added to the same tracker a few lines above but, in reality, there are
62
+ // many focusable areas in the editor, like balloons, toolbars or dropdowns and as long
63
+ // as they have focus, the editable should act like it is focused too (although technically
64
+ // it isn't), e.g. by setting the proper CSS class, visually announcing focus to the user.
65
+ // Doing otherwise will result in editable focus styles disappearing, once e.g. the
66
+ // toolbar gets focused.
67
+ view.editable.bind('isFocused').to(this.focusTracker);
68
+ // Bind the editable UI element to the editing view, making it an end– and entry–point
69
+ // of the editor's engine. This is where the engine meets the UI.
70
+ editingView.attachDomRoot(editableElement);
71
+ // If an element containing the initial data of the editor was provided, replace it with
72
+ // an editor instance's UI in DOM until the editor is destroyed. For instance, a <textarea>
73
+ // can be such element.
74
+ if (replacementElement) {
75
+ this._elementReplacer.replace(replacementElement, this.element);
76
+ }
77
+ this._initPlaceholder();
78
+ this._initToolbar();
79
+ if (view.menuBarView) {
80
+ this._initMenuBar(view.menuBarView);
81
+ }
82
+ this._initDialogPluginIntegration();
83
+ this._initContextualBalloonIntegration();
84
+ this.fire('ready');
85
+ }
86
+ /**
87
+ * @inheritDoc
88
+ */ destroy() {
89
+ super.destroy();
90
+ const view = this.view;
91
+ const editingView = this.editor.editing.view;
92
+ this._elementReplacer.restore();
93
+ if (editingView.getDomRoot(view.editable.name)) {
94
+ editingView.detachDomRoot(view.editable.name);
95
+ }
96
+ view.destroy();
97
+ }
98
+ /**
99
+ * Initializes the editor toolbar.
100
+ */ _initToolbar() {
101
+ const view = this.view;
102
+ // Set–up the sticky panel with toolbar.
103
+ view.stickyPanel.bind('isActive').to(this.focusTracker, 'isFocused');
104
+ view.stickyPanel.limiterElement = view.element;
105
+ view.stickyPanel.bind('viewportTopOffset').to(this, 'viewportOffset', ({ top })=>top || 0);
106
+ view.toolbar.fillFromConfig(this._toolbarConfig, this.componentFactory);
107
+ // Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
108
+ this.addToolbar(view.toolbar);
109
+ }
110
+ /**
111
+ * Enable the placeholder text on the editing root.
112
+ */ _initPlaceholder() {
113
+ const editor = this.editor;
114
+ const editingView = editor.editing.view;
115
+ const editingRoot = editingView.document.getRoot();
116
+ const sourceElement = editor.sourceElement;
117
+ let placeholderText;
118
+ const placeholder = editor.config.get('placeholder');
119
+ if (placeholder) {
120
+ placeholderText = typeof placeholder === 'string' ? placeholder : placeholder[this.view.editable.name];
121
+ }
122
+ if (!placeholderText && sourceElement && sourceElement.tagName.toLowerCase() === 'textarea') {
123
+ placeholderText = sourceElement.getAttribute('placeholder');
124
+ }
125
+ if (placeholderText) {
126
+ editingRoot.placeholder = placeholderText;
127
+ }
128
+ enablePlaceholder({
129
+ view: editingView,
130
+ element: editingRoot,
131
+ isDirectHost: false,
132
+ keepOnFocus: true
133
+ });
134
+ }
135
+ /**
136
+ * Provides an integration between the sticky toolbar and {@link module:ui/panel/balloon/contextualballoon contextual balloon plugin}.
137
+ * It allows the contextual balloon to consider the height of the
138
+ * {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#stickyPanel}. It prevents the balloon from overlapping
139
+ * the sticky toolbar by adjusting the balloon's position using viewport offset configuration.
140
+ */ _initContextualBalloonIntegration() {
141
+ if (!this.editor.plugins.has('ContextualBalloon')) {
142
+ return;
143
+ }
144
+ const { stickyPanel } = this.view;
145
+ const contextualBalloon = this.editor.plugins.get('ContextualBalloon');
146
+ contextualBalloon.on('getPositionOptions', (evt)=>{
147
+ const position = evt.return;
148
+ if (!position || !stickyPanel.isSticky || !stickyPanel.element) {
149
+ return;
150
+ }
151
+ // Measure toolbar (and menu bar) height.
152
+ const stickyPanelHeight = new Rect(stickyPanel.element).height;
153
+ // Handle edge case when the target element is larger than the limiter.
154
+ // It's an issue because the contextual balloon can overlap top table cells when the table is larger than the viewport
155
+ // and it's placed at the top of the editor. It's better to overlap toolbar in that situation.
156
+ // Check this issue: https://github.com/ckeditor/ckeditor5/issues/15744
157
+ const target = typeof position.target === 'function' ? position.target() : position.target;
158
+ const limiter = typeof position.limiter === 'function' ? position.limiter() : position.limiter;
159
+ if (target && limiter && new Rect(target).height >= new Rect(limiter).height - stickyPanelHeight) {
160
+ return;
161
+ }
162
+ // Ensure that viewport offset is present, it can be undefined according to the typing.
163
+ const viewportOffsetConfig = {
164
+ ...position.viewportOffsetConfig
165
+ };
166
+ const newTopViewportOffset = (viewportOffsetConfig.top || 0) + stickyPanelHeight;
167
+ evt.return = {
168
+ ...position,
169
+ viewportOffsetConfig: {
170
+ ...viewportOffsetConfig,
171
+ top: newTopViewportOffset
172
+ }
173
+ };
174
+ }, {
175
+ priority: 'low'
176
+ });
177
+ // Update balloon position when the toolbar becomes sticky or when ui viewportOffset changes.
178
+ const updateBalloonPosition = ()=>{
179
+ if (contextualBalloon.visibleView) {
180
+ contextualBalloon.updatePosition();
181
+ }
182
+ };
183
+ this.listenTo(stickyPanel, 'change:isSticky', updateBalloonPosition);
184
+ this.listenTo(this.editor.ui, 'change:viewportOffset', updateBalloonPosition);
185
+ }
186
+ /**
187
+ * Provides an integration between the sticky toolbar and {@link module:utils/dom/scroll~scrollViewportToShowTarget}.
188
+ * It allows the UI-agnostic engine method to consider the geometry of the
189
+ * {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#stickyPanel} that pins to the
190
+ * edge of the viewport and can obscure the user caret after scrolling the window.
191
+ *
192
+ * @param evt The `scrollToTheSelection` event info.
193
+ * @param data The payload carried by the `scrollToTheSelection` event.
194
+ * @param originalArgs The original arguments passed to `scrollViewportToShowTarget()` method (see implementation to learn more).
195
+ */ _handleScrollToTheSelectionWithStickyPanel(evt, data, originalArgs) {
196
+ const stickyPanel = this.view.stickyPanel;
197
+ if (stickyPanel.isSticky) {
198
+ const stickyPanelHeight = new Rect(stickyPanel.element).height;
199
+ data.viewportOffset.top += stickyPanelHeight;
200
+ } else {
201
+ const scrollViewportOnPanelGettingSticky = ()=>{
202
+ this.editor.editing.view.scrollToTheSelection(originalArgs);
203
+ };
204
+ this.listenTo(stickyPanel, 'change:isSticky', scrollViewportOnPanelGettingSticky);
205
+ // This works as a post-scroll-fixer because it's impossible predict whether the panel will be sticky after scrolling or not.
206
+ // Listen for a short period of time only and if the toolbar does not become sticky very soon, cancel the listener.
207
+ setTimeout(()=>{
208
+ this.stopListening(stickyPanel, 'change:isSticky', scrollViewportOnPanelGettingSticky);
209
+ }, 20);
210
+ }
211
+ }
212
+ /**
213
+ * Provides an integration between the sticky toolbar and {@link module:ui/dialog/dialog the Dialog plugin}.
214
+ *
215
+ * It moves the dialog down to ensure that the
216
+ * {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#stickyPanel sticky panel}
217
+ * used by the editor UI will not get obscured by the dialog when the dialog uses one of its automatic positions.
218
+ */ _initDialogPluginIntegration() {
219
+ if (!this.editor.plugins.has('Dialog')) {
220
+ return;
221
+ }
222
+ const stickyPanel = this.view.stickyPanel;
223
+ const dialogPlugin = this.editor.plugins.get('Dialog');
224
+ dialogPlugin.on('show', ()=>{
225
+ const dialogView = dialogPlugin.view;
226
+ dialogView.on('moveTo', (evt, data)=>{
227
+ // Engage only when the panel is sticky, and the dialog is using one of default positions.
228
+ if (!stickyPanel.isSticky || dialogView.wasMoved) {
229
+ return;
230
+ }
231
+ const stickyPanelContentRect = new Rect(stickyPanel.contentPanelElement);
232
+ if (data[1] < stickyPanelContentRect.bottom + DialogView.defaultOffset) {
233
+ data[1] = stickyPanelContentRect.bottom + DialogView.defaultOffset;
234
+ }
235
+ }, {
236
+ priority: 'high'
237
+ });
238
+ }, {
239
+ priority: 'low'
240
+ });
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Classic editor UI view. Uses an inline editable and a sticky toolbar, all
246
+ * enclosed in a boxed UI view.
247
+ */ class ClassicEditorUIView extends BoxedEditorUIView {
248
+ /**
249
+ * Sticky panel view instance. This is a parent view of a {@link #toolbar}
250
+ * that makes toolbar sticky.
251
+ */ stickyPanel;
252
+ /**
253
+ * Toolbar view instance.
254
+ */ toolbar;
255
+ /**
256
+ * Editable UI view.
257
+ */ editable;
258
+ /**
259
+ * Creates an instance of the classic editor UI view.
260
+ *
261
+ * @param locale The {@link module:core/editor/editor~Editor#locale} instance.
262
+ * @param editingView The editing view instance this view is related to.
263
+ * @param options Configuration options for the view instance.
264
+ * @param options.shouldToolbarGroupWhenFull When set `true` enables automatic items grouping
265
+ * in the main {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#toolbar toolbar}.
266
+ * See {@link module:ui/toolbar/toolbarview~ToolbarOptions#shouldGroupWhenFull} to learn more.
267
+ * @param options.label When set, this value will be used as an accessible `aria-label` of the
268
+ * {@link module:ui/editableui/editableuiview~EditableUIView editable view}.
269
+ */ constructor(locale, editingView, options = {}){
270
+ super(locale);
271
+ this.stickyPanel = new StickyPanelView(locale);
272
+ this.toolbar = new ToolbarView(locale, {
273
+ shouldGroupWhenFull: options.shouldToolbarGroupWhenFull
274
+ });
275
+ if (options.useMenuBar) {
276
+ this.menuBarView = new MenuBarView(locale);
277
+ }
278
+ this.editable = new InlineEditableUIView(locale, editingView, undefined, {
279
+ label: options.label
280
+ });
281
+ }
282
+ /**
283
+ * @inheritDoc
284
+ */ render() {
285
+ super.render();
286
+ if (this.menuBarView) {
287
+ // Set toolbar as a child of a stickyPanel and makes toolbar sticky.
288
+ this.stickyPanel.content.addMany([
289
+ this.menuBarView,
290
+ this.toolbar
291
+ ]);
292
+ } else {
293
+ this.stickyPanel.content.add(this.toolbar);
294
+ }
295
+ this.top.add(this.stickyPanel);
296
+ this.main.add(this.editable);
297
+ }
298
+ }
299
+
300
+ /**
301
+ * The classic editor implementation. It uses an inline editable and a sticky toolbar, all enclosed in a boxed UI.
302
+ * See the {@glink examples/builds/classic-editor demo}.
303
+ *
304
+ * In order to create a classic editor instance, use the static
305
+ * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method.
306
+ */ class ClassicEditor extends /* #__PURE__ */ ElementApiMixin(Editor) {
307
+ /**
308
+ * @inheritdoc
309
+ */ static get editorName() {
310
+ return 'ClassicEditor';
311
+ }
312
+ /**
313
+ * @inheritDoc
314
+ */ ui;
315
+ /**
316
+ * Creates an instance of the classic editor.
317
+ *
318
+ * **Note:** do not use the constructor to create editor instances. Use the static
319
+ * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method instead.
320
+ *
321
+ * @param sourceElementOrData The DOM element that will be the source for the created editor
322
+ * or the editor's initial data. For more information see
323
+ * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
324
+ * @param config The editor configuration.
325
+ */ constructor(sourceElementOrData, config = {}){
326
+ // If both `config.initialData` is set and initial data is passed as the constructor parameter, then throw.
327
+ if (!isElement(sourceElementOrData) && config.initialData !== undefined) {
328
+ // Documented in core/editor/editorconfig.jsdoc.
329
+ // eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
330
+ throw new CKEditorError('editor-create-initial-data', null);
331
+ }
332
+ super(config);
333
+ this.config.define('menuBar.isVisible', false);
334
+ if (this.config.get('initialData') === undefined) {
335
+ this.config.set('initialData', getInitialData(sourceElementOrData));
336
+ }
337
+ if (isElement(sourceElementOrData)) {
338
+ this.sourceElement = sourceElementOrData;
339
+ }
340
+ this.model.document.createRoot();
341
+ const shouldToolbarGroupWhenFull = !this.config.get('toolbar.shouldNotGroupWhenFull');
342
+ const menuBarConfig = this.config.get('menuBar');
343
+ const view = new ClassicEditorUIView(this.locale, this.editing.view, {
344
+ shouldToolbarGroupWhenFull,
345
+ useMenuBar: menuBarConfig.isVisible,
346
+ label: this.config.get('label')
347
+ });
348
+ this.ui = new ClassicEditorUI(this, view);
349
+ attachToForm(this);
350
+ }
351
+ /**
352
+ * Destroys the editor instance, releasing all resources used by it.
353
+ *
354
+ * Updates the original editor element with the data if the
355
+ * {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy `updateSourceElementOnDestroy`}
356
+ * configuration option is set to `true`.
357
+ */ destroy() {
358
+ if (this.sourceElement) {
359
+ this.updateSourceElement();
360
+ }
361
+ this.ui.destroy();
362
+ return super.destroy();
363
+ }
364
+ /**
365
+ * Creates a new classic editor instance.
366
+ *
367
+ * There are three ways how the editor can be initialized.
368
+ *
369
+ * # Replacing a DOM element (and loading data from it)
370
+ *
371
+ * You can initialize the editor using an existing DOM element:
372
+ *
373
+ * ```ts
374
+ * ClassicEditor
375
+ * .create( document.querySelector( '#editor' ) )
376
+ * .then( editor => {
377
+ * console.log( 'Editor was initialized', editor );
378
+ * } )
379
+ * .catch( err => {
380
+ * console.error( err.stack );
381
+ * } );
382
+ * ```
383
+ *
384
+ * The element's content will be used as the editor data and the element will be replaced by the editor UI.
385
+ *
386
+ * # Creating a detached editor
387
+ *
388
+ * Alternatively, you can initialize the editor by passing the initial data directly as a string.
389
+ * In this case, the editor will render an element that must be inserted into the DOM:
390
+ *
391
+ * ```ts
392
+ * ClassicEditor
393
+ * .create( '<p>Hello world!</p>' )
394
+ * .then( editor => {
395
+ * console.log( 'Editor was initialized', editor );
396
+ *
397
+ * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
398
+ * document.body.appendChild( editor.ui.element );
399
+ * } )
400
+ * .catch( err => {
401
+ * console.error( err.stack );
402
+ * } );
403
+ * ```
404
+ *
405
+ * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
406
+ * web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
407
+ *
408
+ * # Replacing a DOM element (and data provided in `config.initialData`)
409
+ *
410
+ * You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
411
+ *
412
+ * ```ts
413
+ * ClassicEditor
414
+ * .create( document.querySelector( '#editor' ), {
415
+ * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
416
+ * } )
417
+ * .then( editor => {
418
+ * console.log( 'Editor was initialized', editor );
419
+ * } )
420
+ * .catch( err => {
421
+ * console.error( err.stack );
422
+ * } );
423
+ * ```
424
+ *
425
+ * This method can be used to initialize the editor on an existing element with the specified content in case if your integration
426
+ * makes it difficult to set the content of the source element.
427
+ *
428
+ * Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
429
+ *
430
+ * # Configuring the editor
431
+ *
432
+ * See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
433
+ * customizing plugins, toolbar and more.
434
+ *
435
+ * @param sourceElementOrData The DOM element that will be the source for the created editor
436
+ * or the editor's initial data.
437
+ *
438
+ * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization
439
+ * and the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element editor element} will replace the passed element
440
+ * in the DOM (the original one will be hidden and the editor will be injected next to it).
441
+ *
442
+ * If the {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy updateSourceElementOnDestroy}
443
+ * option is set to `true`, the editor data will be set back to the original element once the editor is destroyed and when a form,
444
+ * in which this element is contained, is submitted (if the original element is a `<textarea>`). This ensures seamless integration
445
+ * with native web forms.
446
+ *
447
+ * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
448
+ * It is available under the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element `editor.ui.element`} property.
449
+ *
450
+ * @param config The editor configuration.
451
+ * @returns A promise resolved once the editor is ready. The promise resolves with the created editor instance.
452
+ */ static create(sourceElementOrData, config = {}) {
453
+ return new Promise((resolve)=>{
454
+ const editor = new this(sourceElementOrData, config);
455
+ resolve(editor.initPlugins().then(()=>editor.ui.init(isElement(sourceElementOrData) ? sourceElementOrData : null)).then(()=>editor.data.init(editor.config.get('initialData'))).then(()=>editor.fire('ready')).then(()=>editor));
456
+ });
457
+ }
458
+ }
459
+ function getInitialData(sourceElementOrData) {
460
+ return isElement(sourceElementOrData) ? getDataFromElement(sourceElementOrData) : sourceElementOrData;
461
+ }
462
+ function isElement(value) {
463
+ return isElement$1(value);
464
+ }
465
+
466
+ export { ClassicEditor };
467
+ //# sourceMappingURL=index.js.map