@ckeditor/ckeditor5-source-editing 48.2.0-alpha.7 → 48.3.0-alpha.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 CHANGED
@@ -2,400 +2,345 @@
2
2
  * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
4
  */
5
- import { Plugin, PendingActions } from '@ckeditor/ckeditor5-core/dist/index.js';
6
- import { IconSource } from '@ckeditor/ckeditor5-icons/dist/index.js';
7
- import { ButtonView, MenuBarMenuListItemButtonView } from '@ckeditor/ckeditor5-ui/dist/index.js';
8
- import { ElementReplacer, CKEditorError, createElement, env, formatHtml } from '@ckeditor/ckeditor5-utils/dist/index.js';
5
+ import { PendingActions, Plugin } from "@ckeditor/ckeditor5-core";
6
+ import { IconSource } from "@ckeditor/ckeditor5-icons";
7
+ import { ButtonView, MenuBarMenuListItemButtonView } from "@ckeditor/ckeditor5-ui";
8
+ import { CKEditorError, ElementReplacer, createElement, env, formatHtml } from "@ckeditor/ckeditor5-utils";
9
9
 
10
- const COMMAND_FORCE_DISABLE_ID = 'SourceEditingMode';
11
10
  /**
12
- * The source editing feature.
13
- *
14
- * It provides the possibility to view and edit the source of the document.
15
- *
16
- * For a detailed overview, check the {@glink features/source-editing/source-editing source editing feature documentation} and the
17
- * {@glink api/source-editing package page}.
18
- */ class SourceEditing extends Plugin {
19
- /**
20
- * @inheritDoc
21
- */ static get pluginName() {
22
- return 'SourceEditing';
23
- }
24
- /**
25
- * @inheritDoc
26
- */ static get isOfficialPlugin() {
27
- return true;
28
- }
29
- /**
30
- * @inheritDoc
31
- */ static get requires() {
32
- return [
33
- PendingActions
34
- ];
35
- }
36
- /**
37
- * The element replacer instance used to replace the editing roots with the wrapper elements containing the document source.
38
- */ _elementReplacer;
39
- /**
40
- * Maps all root names to wrapper elements containing the document source.
41
- */ _replacedRoots;
42
- /**
43
- * Maps all root names to their document data.
44
- */ _dataFromRoots;
45
- /**
46
- * @inheritDoc
47
- */ constructor(editor){
48
- super(editor);
49
- this.set('isSourceEditingMode', false);
50
- this._elementReplacer = new ElementReplacer();
51
- this._replacedRoots = new Map();
52
- this._dataFromRoots = new Map();
53
- editor.config.define('sourceEditing.allowCollaborationFeatures', false);
54
- }
55
- /**
56
- * @inheritDoc
57
- */ init() {
58
- this._checkCompatibility();
59
- const editor = this.editor;
60
- const t = editor.locale.t;
61
- editor.ui.componentFactory.add('sourceEditing', ()=>{
62
- const buttonView = this._createButton(ButtonView);
63
- buttonView.set({
64
- label: t('Source'),
65
- icon: IconSource,
66
- tooltip: true,
67
- class: 'ck-source-editing-button'
68
- });
69
- return buttonView;
70
- });
71
- editor.ui.componentFactory.add('menuBar:sourceEditing', ()=>{
72
- const buttonView = this._createButton(MenuBarMenuListItemButtonView);
73
- buttonView.set({
74
- label: t('Show source'),
75
- role: 'menuitemcheckbox'
76
- });
77
- return buttonView;
78
- });
79
- // Currently, the plugin handles the source editing mode by itself only for the classic editor. To use this plugin with other
80
- // integrations, listen to the `change:isSourceEditingMode` event and act accordingly.
81
- if (this._isAllowedToHandleSourceEditingMode()) {
82
- this.on('change:isSourceEditingMode', (evt, name, isSourceEditingMode)=>{
83
- if (isSourceEditingMode) {
84
- this._hideVisibleDialog();
85
- this._showSourceEditing();
86
- this._disableCommands();
87
- } else {
88
- this._hideSourceEditing();
89
- this._enableCommands();
90
- }
91
- });
92
- this.on('change:isEnabled', (evt, name, isEnabled)=>this._handleReadOnlyMode(!isEnabled));
93
- this.listenTo(editor, 'change:isReadOnly', (evt, name, isReadOnly)=>this._handleReadOnlyMode(isReadOnly));
94
- }
95
- // Update the editor data while calling editor.getData() in the source editing mode.
96
- editor.data.on('get', ()=>{
97
- if (this.isSourceEditingMode) {
98
- this.updateEditorData();
99
- }
100
- }, {
101
- priority: 'high'
102
- });
103
- }
104
- /**
105
- * Updates the source data in all hidden editing roots.
106
- */ updateEditorData() {
107
- const editor = this.editor;
108
- const data = {};
109
- for (const [rootName, domSourceEditingElementWrapper] of this._replacedRoots){
110
- const oldData = this._dataFromRoots.get(rootName);
111
- const newData = domSourceEditingElementWrapper.dataset.value;
112
- // Do not set the data unless some changes have been made in the meantime.
113
- // This prevents empty undo steps after switching to the normal editor.
114
- if (oldData !== newData) {
115
- data[rootName] = newData;
116
- this._dataFromRoots.set(rootName, newData);
117
- }
118
- }
119
- if (Object.keys(data).length) {
120
- editor.data.set(data, {
121
- batchType: {
122
- isUndoable: true
123
- },
124
- suppressErrorInCollaboration: true
125
- });
126
- }
127
- }
128
- _checkCompatibility() {
129
- const editor = this.editor;
130
- const allowCollaboration = editor.config.get('sourceEditing.allowCollaborationFeatures');
131
- if (!allowCollaboration && editor.plugins.has('RealTimeCollaborativeEditing')) {
132
- /**
133
- * Source editing feature is not fully compatible with real-time collaboration,
134
- * and using it may lead to data loss. Please read
135
- * {@glink features/source-editing/source-editing#limitations-and-incompatibilities source editing feature guide} to learn more.
136
- *
137
- * If you understand the possible risk of data loss, you can enable the source editing
138
- * by setting the
139
- * {@link module:source-editing/sourceeditingconfig~SourceEditingConfig#allowCollaborationFeatures}
140
- * configuration flag to `true`.
141
- *
142
- * @error source-editing-incompatible-with-real-time-collaboration
143
- */ throw new CKEditorError('source-editing-incompatible-with-real-time-collaboration', null);
144
- }
145
- const collaborationPluginNamesToWarn = [
146
- 'CommentsEditing',
147
- 'TrackChangesEditing',
148
- 'RevisionHistory'
149
- ];
150
- // Currently, the basic integration with Collaboration Features is to display a warning in the console.
151
- //
152
- // If `allowCollaboration` flag is set, do not show these warnings. If the flag is set, we assume that the integrator read
153
- // appropriate section of the guide so there's no use to spam the console with warnings.
154
- //
155
- if (!allowCollaboration && collaborationPluginNamesToWarn.some((pluginName)=>editor.plugins.has(pluginName))) {
156
- console.warn('You initialized the editor with the source editing feature and at least one of the collaboration features. ' + 'Please be advised that the source editing feature may not work, and be careful when editing document source ' + 'that contains markers created by the collaboration features.');
157
- }
158
- // Restricted Editing integration can also lead to problems. Warn the user accordingly.
159
- if (editor.plugins.has('RestrictedEditingModeEditing')) {
160
- console.warn('You initialized the editor with the source editing feature and restricted editing feature. ' + 'Please be advised that the source editing feature may not work, and be careful when editing document source ' + 'that contains markers created by the restricted editing feature.');
161
- }
162
- }
163
- /**
164
- * Creates source editing wrappers that replace each editing root. Each wrapper contains the document source from the corresponding
165
- * root.
166
- *
167
- * The wrapper element contains a textarea and it solves the problem, that the textarea element cannot auto expand its height based on
168
- * the content it contains. The solution is to make the textarea more like a plain div element, which expands in height as much as it
169
- * needs to, in order to display the whole document source without scrolling. The wrapper element is a parent for the textarea and for
170
- * the pseudo-element `::after`, that replicates the look, content, and position of the textarea. The pseudo-element replica is hidden,
171
- * but it is styled to be an identical visual copy of the textarea with the same content. Then, the wrapper is a grid container and both
172
- * of its children (the textarea and the `::after` pseudo-element) are positioned within a CSS grid to occupy the same grid cell. The
173
- * content in the pseudo-element `::after` is set in CSS and it stretches the grid to the appropriate size based on the textarea value.
174
- * Since both children occupy the same grid cell, both have always the same height.
175
- */ _showSourceEditing() {
176
- const editor = this.editor;
177
- const editingView = editor.editing.view;
178
- const model = editor.model;
179
- model.change((writer)=>{
180
- writer.setSelection(null);
181
- writer.removeSelectionAttribute(model.document.selection.getAttributeKeys());
182
- });
183
- // It is not needed to iterate through all editing roots, as currently the plugin supports only the Classic Editor with a single
184
- // main root, but this code may help understand and use this feature in external integrations.
185
- for (const [rootName, domRootElement] of editingView.domRoots){
186
- const data = formatSource(editor.data.get({
187
- rootName
188
- }));
189
- const domSourceEditingElementTextarea = createElement(domRootElement.ownerDocument, 'textarea', {
190
- rows: '1',
191
- 'aria-label': 'Source code editing area'
192
- });
193
- const domSourceEditingElementWrapper = createElement(domRootElement.ownerDocument, 'div', {
194
- class: 'ck-source-editing-area',
195
- 'data-value': data
196
- }, [
197
- domSourceEditingElementTextarea
198
- ]);
199
- domSourceEditingElementTextarea.value = data;
200
- // Setting a value to textarea moves the input cursor to the end. We want the selection at the beginning.
201
- domSourceEditingElementTextarea.setSelectionRange(0, 0);
202
- // Bind the textarea's value to the wrapper's `data-value` property. Each change of the textarea's value updates the
203
- // wrapper's `data-value` property.
204
- domSourceEditingElementTextarea.addEventListener('input', ()=>{
205
- domSourceEditingElementWrapper.dataset.value = domSourceEditingElementTextarea.value;
206
- editor.ui.update();
207
- });
208
- // Allow native undo/redo in the textarea. The editor's keystroke handler (attached via
209
- // setEditableElement()) intercepts Ctrl+Z/Y and calls preventDefault(), blocking browser
210
- // undo/redo while editor commands are force-disabled in source editing mode.
211
- // See: https://github.com/ckeditor/ckeditor5/issues/13700
212
- domSourceEditingElementTextarea.addEventListener('keydown', (evt)=>{
213
- // Normalize the key to lowercase because `evt.key` reflects the Caps Lock state
214
- // (e.g. returns 'Z' instead of 'z' when Caps Lock is on), which would otherwise
215
- // bypass the comparison below and let the editor's keystroke handler block undo/redo.
216
- const key = evt.key.toLowerCase();
217
- if ((evt.ctrlKey || evt.metaKey) && !evt.altKey && (key === 'z' || key === 'y')) {
218
- evt.stopImmediatePropagation();
219
- // macOS does not natively map Cmd+Y to redo in textareas (the platform convention is
220
- // Cmd+Shift+Z). CKEditor accepts both keystrokes for redo, so users expect Cmd+Y to
221
- // work here too. Manually invoke the browser's redo on the focused textarea.
222
- if (env.isMac && key === 'y') {
223
- evt.preventDefault();
224
- domRootElement.ownerDocument.execCommand('redo');
225
- }
226
- }
227
- });
228
- editingView.change((writer)=>{
229
- const viewRoot = editingView.document.getRoot(rootName);
230
- writer.addClass('ck-hidden', viewRoot);
231
- });
232
- // Register the element so it becomes available for Alt+F10 and Esc navigation.
233
- editor.ui.setEditableElement('sourceEditing:' + rootName, domSourceEditingElementTextarea);
234
- this._replacedRoots.set(rootName, domSourceEditingElementWrapper);
235
- this._elementReplacer.replace(domRootElement, domSourceEditingElementWrapper);
236
- this._dataFromRoots.set(rootName, data);
237
- }
238
- this._hideDocumentOutline();
239
- this._refreshAnnotationsVisibility();
240
- this._focusSourceEditing();
241
- }
242
- /**
243
- * Restores all hidden editing roots and sets the source data in them.
244
- */ _hideSourceEditing() {
245
- const editor = this.editor;
246
- const editingView = editor.editing.view;
247
- this.updateEditorData();
248
- editingView.change((writer)=>{
249
- for (const [rootName] of this._replacedRoots){
250
- writer.removeClass('ck-hidden', editingView.document.getRoot(rootName));
251
- }
252
- });
253
- this._elementReplacer.restore();
254
- this._replacedRoots.clear();
255
- this._dataFromRoots.clear();
256
- this._showDocumentOutline();
257
- this._refreshAnnotationsVisibility();
258
- editingView.focus();
259
- }
260
- /**
261
- * Hides the document outline if it is configured.
262
- */ _hideDocumentOutline() {
263
- if (this.editor.plugins.has('DocumentOutlineUI')) {
264
- this.editor.plugins.get('DocumentOutlineUI').view.element.style.display = 'none';
265
- }
266
- }
267
- /**
268
- * Shows the document outline if it was hidden when entering the source editing.
269
- */ _showDocumentOutline() {
270
- if (this.editor.plugins.has('DocumentOutlineUI')) {
271
- this.editor.plugins.get('DocumentOutlineUI').view.element.style.display = '';
272
- }
273
- }
274
- /**
275
- * Hides the annotations when entering the source editing mode and shows back them after leaving it.
276
- */ _refreshAnnotationsVisibility() {
277
- if (this.editor.plugins.has('Annotations')) {
278
- this.editor.plugins.get('Annotations').refreshVisibility();
279
- }
280
- }
281
- /**
282
- * Focuses the textarea containing document source from the first editing root.
283
- */ _focusSourceEditing() {
284
- const editor = this.editor;
285
- const [domSourceEditingElementWrapper] = this._replacedRoots.values();
286
- const textarea = domSourceEditingElementWrapper.querySelector('textarea');
287
- // The FocusObserver was disabled by View.render() while the DOM root was getting hidden and the replacer
288
- // revealed the textarea. So it couldn't notice that the DOM root got blurred in the process.
289
- // Let's sync this state manually here because otherwise Renderer will attempt to render selection
290
- // in an invisible DOM root.
291
- editor.editing.view.document.isFocused = false;
292
- textarea.focus();
293
- }
294
- /**
295
- * Disables all commands.
296
- */ _disableCommands() {
297
- const editor = this.editor;
298
- for (const command of editor.commands.commands()){
299
- command.forceDisabled(COMMAND_FORCE_DISABLE_ID);
300
- }
301
- // Comments archive UI plugin will be disabled manually too.
302
- if (editor.plugins.has('CommentsArchiveUI')) {
303
- editor.plugins.get('CommentsArchiveUI').forceDisabled(COMMAND_FORCE_DISABLE_ID);
304
- }
305
- }
306
- /**
307
- * Clears forced disable for all commands, that was previously set through {@link #_disableCommands}.
308
- */ _enableCommands() {
309
- const editor = this.editor;
310
- for (const command of editor.commands.commands()){
311
- command.clearForceDisabled(COMMAND_FORCE_DISABLE_ID);
312
- }
313
- // Comments archive UI plugin will be enabled manually too.
314
- if (editor.plugins.has('CommentsArchiveUI')) {
315
- editor.plugins.get('CommentsArchiveUI').clearForceDisabled(COMMAND_FORCE_DISABLE_ID);
316
- }
317
- }
318
- /**
319
- * Adds or removes the `readonly` attribute from the textarea from all roots, if document source mode is active.
320
- *
321
- * @param isReadOnly Indicates whether all textarea elements should be read-only.
322
- */ _handleReadOnlyMode(isReadOnly) {
323
- if (!this.isSourceEditingMode) {
324
- return;
325
- }
326
- for (const [, domSourceEditingElementWrapper] of this._replacedRoots){
327
- domSourceEditingElementWrapper.querySelector('textarea').readOnly = isReadOnly;
328
- }
329
- }
330
- /**
331
- * Checks, if the plugin is allowed to handle the source editing mode by itself. Currently, the source editing mode is supported only
332
- * for the {@link module:editor-classic/classiceditor~ClassicEditor classic editor}.
333
- */ _isAllowedToHandleSourceEditingMode() {
334
- const editor = this.editor;
335
- const editable = editor.ui.view.editable;
336
- // Checks, if the editor's editable belongs to the editor's DOM tree.
337
- return editable && !editable.hasExternalElement;
338
- }
339
- /**
340
- * If any {@link module:ui/dialog/dialogview~DialogView editor dialog} is currently visible, hide it.
341
- */ _hideVisibleDialog() {
342
- if (this.editor.plugins.has('Dialog')) {
343
- const dialogPlugin = this.editor.plugins.get('Dialog');
344
- if (dialogPlugin.isOpen) {
345
- dialogPlugin.hide();
346
- }
347
- }
348
- }
349
- _createButton(ButtonClass) {
350
- const editor = this.editor;
351
- const buttonView = new ButtonClass(editor.locale);
352
- buttonView.set({
353
- withText: true,
354
- isToggleable: true
355
- });
356
- buttonView.bind('isOn').to(this, 'isSourceEditingMode');
357
- // The button should be disabled if one of the following conditions is met:
358
- buttonView.bind('isEnabled').to(this, 'isEnabled', editor, 'isReadOnly', editor.plugins.get(PendingActions), 'hasAny', (isEnabled, isEditorReadOnly, hasAnyPendingActions)=>{
359
- // (1) The plugin itself is disabled.
360
- if (!isEnabled) {
361
- return false;
362
- }
363
- // (2) The editor is in read-only mode.
364
- if (isEditorReadOnly) {
365
- return false;
366
- }
367
- // (3) Any pending action is scheduled. It may change the model, so modifying the document source should be prevented
368
- // until the model is finally set.
369
- if (hasAnyPendingActions) {
370
- return false;
371
- }
372
- return true;
373
- });
374
- this.listenTo(buttonView, 'execute', ()=>{
375
- this.isSourceEditingMode = !this.isSourceEditingMode;
376
- });
377
- return buttonView;
378
- }
379
- }
11
+ * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
12
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
13
+ */
14
+ /**
15
+ * @module source-editing/sourceediting
16
+ */
17
+ const COMMAND_FORCE_DISABLE_ID = "SourceEditingMode";
18
+ /**
19
+ * The source editing feature.
20
+ *
21
+ * It provides the possibility to view and edit the source of the document.
22
+ *
23
+ * For a detailed overview, check the {@glink features/source-editing/source-editing source editing feature documentation} and the
24
+ * {@glink api/source-editing package page}.
25
+ */
26
+ var SourceEditing = class extends Plugin {
27
+ /**
28
+ * @inheritDoc
29
+ */
30
+ static get pluginName() {
31
+ return "SourceEditing";
32
+ }
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ static get isOfficialPlugin() {
37
+ return true;
38
+ }
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ static get requires() {
43
+ return [PendingActions];
44
+ }
45
+ /**
46
+ * The element replacer instance used to replace the editing roots with the wrapper elements containing the document source.
47
+ */
48
+ _elementReplacer;
49
+ /**
50
+ * Maps all root names to wrapper elements containing the document source.
51
+ */
52
+ _replacedRoots;
53
+ /**
54
+ * Maps all root names to their document data.
55
+ */
56
+ _dataFromRoots;
57
+ /**
58
+ * @inheritDoc
59
+ */
60
+ constructor(editor) {
61
+ super(editor);
62
+ this.set("isSourceEditingMode", false);
63
+ this._elementReplacer = new ElementReplacer();
64
+ this._replacedRoots = /* @__PURE__ */ new Map();
65
+ this._dataFromRoots = /* @__PURE__ */ new Map();
66
+ editor.config.define("sourceEditing.allowCollaborationFeatures", false);
67
+ }
68
+ /**
69
+ * @inheritDoc
70
+ */
71
+ init() {
72
+ this._checkCompatibility();
73
+ const editor = this.editor;
74
+ const t = editor.locale.t;
75
+ editor.ui.componentFactory.add("sourceEditing", () => {
76
+ const buttonView = this._createButton(ButtonView);
77
+ buttonView.set({
78
+ label: t("Source"),
79
+ icon: IconSource,
80
+ tooltip: true,
81
+ class: "ck-source-editing-button"
82
+ });
83
+ return buttonView;
84
+ });
85
+ editor.ui.componentFactory.add("menuBar:sourceEditing", () => {
86
+ const buttonView = this._createButton(MenuBarMenuListItemButtonView);
87
+ buttonView.set({
88
+ label: t("Show source"),
89
+ role: "menuitemcheckbox"
90
+ });
91
+ return buttonView;
92
+ });
93
+ if (this._isAllowedToHandleSourceEditingMode()) {
94
+ this.on("change:isSourceEditingMode", (evt, name, isSourceEditingMode) => {
95
+ if (isSourceEditingMode) {
96
+ this._hideVisibleDialog();
97
+ this._showSourceEditing();
98
+ this._disableCommands();
99
+ } else {
100
+ this._hideSourceEditing();
101
+ this._enableCommands();
102
+ }
103
+ });
104
+ this.on("change:isEnabled", (evt, name, isEnabled) => this._handleReadOnlyMode(!isEnabled));
105
+ this.listenTo(editor, "change:isReadOnly", (evt, name, isReadOnly) => this._handleReadOnlyMode(isReadOnly));
106
+ }
107
+ editor.data.on("get", () => {
108
+ if (this.isSourceEditingMode) this.updateEditorData();
109
+ }, { priority: "high" });
110
+ }
111
+ /**
112
+ * Updates the source data in all hidden editing roots.
113
+ */
114
+ updateEditorData() {
115
+ const editor = this.editor;
116
+ const data = {};
117
+ for (const [rootName, domSourceEditingElementWrapper] of this._replacedRoots) {
118
+ const oldData = this._dataFromRoots.get(rootName);
119
+ const newData = domSourceEditingElementWrapper.dataset.value;
120
+ if (oldData !== newData) {
121
+ data[rootName] = newData;
122
+ this._dataFromRoots.set(rootName, newData);
123
+ }
124
+ }
125
+ if (Object.keys(data).length) editor.data.set(data, {
126
+ batchType: { isUndoable: true },
127
+ suppressErrorInCollaboration: true
128
+ });
129
+ }
130
+ _checkCompatibility() {
131
+ const editor = this.editor;
132
+ const allowCollaboration = editor.config.get("sourceEditing.allowCollaborationFeatures");
133
+ if (!allowCollaboration && editor.plugins.has("RealTimeCollaborativeEditing"))
134
+ /**
135
+ * Source editing feature is not fully compatible with real-time collaboration,
136
+ * and using it may lead to data loss. Please read
137
+ * {@glink features/source-editing/source-editing#limitations-and-incompatibilities source editing feature guide} to learn more.
138
+ *
139
+ * If you understand the possible risk of data loss, you can enable the source editing
140
+ * by setting the
141
+ * {@link module:source-editing/sourceeditingconfig~SourceEditingConfig#allowCollaborationFeatures}
142
+ * configuration flag to `true`.
143
+ *
144
+ * @error source-editing-incompatible-with-real-time-collaboration
145
+ */
146
+ throw new CKEditorError("source-editing-incompatible-with-real-time-collaboration", null);
147
+ if (!allowCollaboration && [
148
+ "CommentsEditing",
149
+ "TrackChangesEditing",
150
+ "RevisionHistory"
151
+ ].some((pluginName) => editor.plugins.has(pluginName))) console.warn("You initialized the editor with the source editing feature and at least one of the collaboration features. Please be advised that the source editing feature may not work, and be careful when editing document source that contains markers created by the collaboration features.");
152
+ if (editor.plugins.has("RestrictedEditingModeEditing")) console.warn("You initialized the editor with the source editing feature and restricted editing feature. Please be advised that the source editing feature may not work, and be careful when editing document source that contains markers created by the restricted editing feature.");
153
+ }
154
+ /**
155
+ * Creates source editing wrappers that replace each editing root. Each wrapper contains the document source from the corresponding
156
+ * root.
157
+ *
158
+ * The wrapper element contains a textarea and it solves the problem, that the textarea element cannot auto expand its height based on
159
+ * the content it contains. The solution is to make the textarea more like a plain div element, which expands in height as much as it
160
+ * needs to, in order to display the whole document source without scrolling. The wrapper element is a parent for the textarea and for
161
+ * the pseudo-element `::after`, that replicates the look, content, and position of the textarea. The pseudo-element replica is hidden,
162
+ * but it is styled to be an identical visual copy of the textarea with the same content. Then, the wrapper is a grid container and both
163
+ * of its children (the textarea and the `::after` pseudo-element) are positioned within a CSS grid to occupy the same grid cell. The
164
+ * content in the pseudo-element `::after` is set in CSS and it stretches the grid to the appropriate size based on the textarea value.
165
+ * Since both children occupy the same grid cell, both have always the same height.
166
+ */
167
+ _showSourceEditing() {
168
+ const editor = this.editor;
169
+ const editingView = editor.editing.view;
170
+ const model = editor.model;
171
+ model.change((writer) => {
172
+ writer.setSelection(null);
173
+ writer.removeSelectionAttribute(model.document.selection.getAttributeKeys());
174
+ });
175
+ for (const [rootName, domRootElement] of editingView.domRoots) {
176
+ const data = formatSource(editor.data.get({ rootName }));
177
+ const domSourceEditingElementTextarea = createElement(domRootElement.ownerDocument, "textarea", {
178
+ rows: "1",
179
+ "aria-label": "Source code editing area"
180
+ });
181
+ const domSourceEditingElementWrapper = createElement(domRootElement.ownerDocument, "div", {
182
+ class: "ck-source-editing-area",
183
+ "data-value": data
184
+ }, [domSourceEditingElementTextarea]);
185
+ domSourceEditingElementTextarea.value = data;
186
+ domSourceEditingElementTextarea.setSelectionRange(0, 0);
187
+ domSourceEditingElementTextarea.addEventListener("input", () => {
188
+ domSourceEditingElementWrapper.dataset.value = domSourceEditingElementTextarea.value;
189
+ editor.ui.update();
190
+ });
191
+ domSourceEditingElementTextarea.addEventListener("keydown", (evt) => {
192
+ const key = evt.key.toLowerCase();
193
+ if ((evt.ctrlKey || evt.metaKey) && !evt.altKey && (key === "z" || key === "y")) {
194
+ evt.stopImmediatePropagation();
195
+ if (env.isMac && key === "y") {
196
+ evt.preventDefault();
197
+ domRootElement.ownerDocument.execCommand("redo");
198
+ }
199
+ }
200
+ });
201
+ editingView.change((writer) => {
202
+ const viewRoot = editingView.document.getRoot(rootName);
203
+ writer.addClass("ck-hidden", viewRoot);
204
+ });
205
+ editor.ui.setEditableElement("sourceEditing:" + rootName, domSourceEditingElementTextarea);
206
+ this._replacedRoots.set(rootName, domSourceEditingElementWrapper);
207
+ this._elementReplacer.replace(domRootElement, domSourceEditingElementWrapper);
208
+ this._dataFromRoots.set(rootName, data);
209
+ }
210
+ this._hideDocumentOutline();
211
+ this._refreshAnnotationsVisibility();
212
+ this._focusSourceEditing();
213
+ }
214
+ /**
215
+ * Restores all hidden editing roots and sets the source data in them.
216
+ */
217
+ _hideSourceEditing() {
218
+ const editingView = this.editor.editing.view;
219
+ this.updateEditorData();
220
+ editingView.change((writer) => {
221
+ for (const [rootName] of this._replacedRoots) writer.removeClass("ck-hidden", editingView.document.getRoot(rootName));
222
+ });
223
+ this._elementReplacer.restore();
224
+ this._replacedRoots.clear();
225
+ this._dataFromRoots.clear();
226
+ this._showDocumentOutline();
227
+ this._refreshAnnotationsVisibility();
228
+ editingView.focus();
229
+ }
230
+ /**
231
+ * Hides the document outline if it is configured.
232
+ */
233
+ _hideDocumentOutline() {
234
+ if (this.editor.plugins.has("DocumentOutlineUI")) this.editor.plugins.get("DocumentOutlineUI").view.element.style.display = "none";
235
+ }
236
+ /**
237
+ * Shows the document outline if it was hidden when entering the source editing.
238
+ */
239
+ _showDocumentOutline() {
240
+ if (this.editor.plugins.has("DocumentOutlineUI")) this.editor.plugins.get("DocumentOutlineUI").view.element.style.display = "";
241
+ }
242
+ /**
243
+ * Hides the annotations when entering the source editing mode and shows back them after leaving it.
244
+ */
245
+ _refreshAnnotationsVisibility() {
246
+ if (this.editor.plugins.has("Annotations")) this.editor.plugins.get("Annotations").refreshVisibility();
247
+ }
248
+ /**
249
+ * Focuses the textarea containing document source from the first editing root.
250
+ */
251
+ _focusSourceEditing() {
252
+ const editor = this.editor;
253
+ const [domSourceEditingElementWrapper] = this._replacedRoots.values();
254
+ const textarea = domSourceEditingElementWrapper.querySelector("textarea");
255
+ editor.editing.view.document.isFocused = false;
256
+ textarea.focus();
257
+ }
258
+ /**
259
+ * Disables all commands.
260
+ */
261
+ _disableCommands() {
262
+ const editor = this.editor;
263
+ for (const command of editor.commands.commands()) command.forceDisabled(COMMAND_FORCE_DISABLE_ID);
264
+ if (editor.plugins.has("CommentsArchiveUI")) editor.plugins.get("CommentsArchiveUI").forceDisabled(COMMAND_FORCE_DISABLE_ID);
265
+ }
266
+ /**
267
+ * Clears forced disable for all commands, that was previously set through {@link #_disableCommands}.
268
+ */
269
+ _enableCommands() {
270
+ const editor = this.editor;
271
+ for (const command of editor.commands.commands()) command.clearForceDisabled(COMMAND_FORCE_DISABLE_ID);
272
+ if (editor.plugins.has("CommentsArchiveUI")) editor.plugins.get("CommentsArchiveUI").clearForceDisabled(COMMAND_FORCE_DISABLE_ID);
273
+ }
274
+ /**
275
+ * Adds or removes the `readonly` attribute from the textarea from all roots, if document source mode is active.
276
+ *
277
+ * @param isReadOnly Indicates whether all textarea elements should be read-only.
278
+ */
279
+ _handleReadOnlyMode(isReadOnly) {
280
+ if (!this.isSourceEditingMode) return;
281
+ for (const [, domSourceEditingElementWrapper] of this._replacedRoots) domSourceEditingElementWrapper.querySelector("textarea").readOnly = isReadOnly;
282
+ }
283
+ /**
284
+ * Checks, if the plugin is allowed to handle the source editing mode by itself. Currently, the source editing mode is supported only
285
+ * for the {@link module:editor-classic/classiceditor~ClassicEditor classic editor}.
286
+ */
287
+ _isAllowedToHandleSourceEditingMode() {
288
+ const editable = this.editor.ui.view.editable;
289
+ return editable && !editable.hasExternalElement;
290
+ }
291
+ /**
292
+ * If any {@link module:ui/dialog/dialogview~DialogView editor dialog} is currently visible, hide it.
293
+ */
294
+ _hideVisibleDialog() {
295
+ if (this.editor.plugins.has("Dialog")) {
296
+ const dialogPlugin = this.editor.plugins.get("Dialog");
297
+ if (dialogPlugin.isOpen) dialogPlugin.hide();
298
+ }
299
+ }
300
+ _createButton(ButtonClass) {
301
+ const editor = this.editor;
302
+ const buttonView = new ButtonClass(editor.locale);
303
+ buttonView.set({
304
+ withText: true,
305
+ isToggleable: true
306
+ });
307
+ buttonView.bind("isOn").to(this, "isSourceEditingMode");
308
+ buttonView.bind("isEnabled").to(this, "isEnabled", editor, "isReadOnly", editor.plugins.get(PendingActions), "hasAny", (isEnabled, isEditorReadOnly, hasAnyPendingActions) => {
309
+ if (!isEnabled) return false;
310
+ if (isEditorReadOnly) return false;
311
+ if (hasAnyPendingActions) return false;
312
+ return true;
313
+ });
314
+ this.listenTo(buttonView, "execute", () => {
315
+ this.isSourceEditingMode = !this.isSourceEditingMode;
316
+ });
317
+ return buttonView;
318
+ }
319
+ };
380
320
  /**
381
- * Formats the content for a better readability.
382
- *
383
- * For a non-HTML source the unchanged input string is returned.
384
- *
385
- * @param input Input string to check.
386
- */ function formatSource(input) {
387
- if (!isHtml(input)) {
388
- return input;
389
- }
390
- return formatHtml(input);
321
+ * Formats the content for a better readability.
322
+ *
323
+ * For a non-HTML source the unchanged input string is returned.
324
+ *
325
+ * @param input Input string to check.
326
+ */
327
+ function formatSource(input) {
328
+ if (!isHtml(input)) return input;
329
+ return formatHtml(input);
391
330
  }
392
331
  /**
393
- * Checks, if the document source is HTML. It is sufficient to just check the first character from the document data.
394
- *
395
- * @param input Input string to check.
396
- */ function isHtml(input) {
397
- return input.startsWith('<');
332
+ * Checks, if the document source is HTML. It is sufficient to just check the first character from the document data.
333
+ *
334
+ * @param input Input string to check.
335
+ */
336
+ function isHtml(input) {
337
+ return input.startsWith("<");
398
338
  }
399
339
 
340
+ /**
341
+ * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
342
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
343
+ */
344
+
400
345
  export { SourceEditing };
401
- //# sourceMappingURL=index.js.map
346
+ //# sourceMappingURL=index.js.map