@ckeditor/ckeditor5-code-block 0.0.0-nightly-20260123.0 → 0.0.0-nightly-next-20260123.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.
Files changed (173) hide show
  1. package/ckeditor5-metadata.json +1 -1
  2. package/{src → dist}/codeblock.d.ts +1 -1
  3. package/{src → dist}/codeblockcommand.d.ts +1 -1
  4. package/{src → dist}/codeblockediting.d.ts +2 -2
  5. package/{src → dist}/codeblockui.d.ts +1 -1
  6. package/{src → dist}/converters.d.ts +2 -2
  7. package/{src → dist}/indentcodeblockcommand.d.ts +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/{src → dist}/outdentcodeblockcommand.d.ts +1 -1
  10. package/{src → dist}/utils.d.ts +3 -3
  11. package/package.json +25 -49
  12. package/build/code-block.js +0 -5
  13. package/build/translations/af.js +0 -1
  14. package/build/translations/ar.js +0 -1
  15. package/build/translations/ast.js +0 -1
  16. package/build/translations/az.js +0 -1
  17. package/build/translations/be.js +0 -1
  18. package/build/translations/bg.js +0 -1
  19. package/build/translations/bn.js +0 -1
  20. package/build/translations/bs.js +0 -1
  21. package/build/translations/ca.js +0 -1
  22. package/build/translations/cs.js +0 -1
  23. package/build/translations/da.js +0 -1
  24. package/build/translations/de-ch.js +0 -1
  25. package/build/translations/de.js +0 -1
  26. package/build/translations/el.js +0 -1
  27. package/build/translations/en-au.js +0 -1
  28. package/build/translations/en-gb.js +0 -1
  29. package/build/translations/eo.js +0 -1
  30. package/build/translations/es-co.js +0 -1
  31. package/build/translations/es.js +0 -1
  32. package/build/translations/et.js +0 -1
  33. package/build/translations/eu.js +0 -1
  34. package/build/translations/fa.js +0 -1
  35. package/build/translations/fi.js +0 -1
  36. package/build/translations/fr.js +0 -1
  37. package/build/translations/gl.js +0 -1
  38. package/build/translations/gu.js +0 -1
  39. package/build/translations/he.js +0 -1
  40. package/build/translations/hi.js +0 -1
  41. package/build/translations/hr.js +0 -1
  42. package/build/translations/hu.js +0 -1
  43. package/build/translations/hy.js +0 -1
  44. package/build/translations/id.js +0 -1
  45. package/build/translations/it.js +0 -1
  46. package/build/translations/ja.js +0 -1
  47. package/build/translations/jv.js +0 -1
  48. package/build/translations/kk.js +0 -1
  49. package/build/translations/km.js +0 -1
  50. package/build/translations/kn.js +0 -1
  51. package/build/translations/ko.js +0 -1
  52. package/build/translations/ku.js +0 -1
  53. package/build/translations/lt.js +0 -1
  54. package/build/translations/lv.js +0 -1
  55. package/build/translations/ms.js +0 -1
  56. package/build/translations/nb.js +0 -1
  57. package/build/translations/ne.js +0 -1
  58. package/build/translations/nl.js +0 -1
  59. package/build/translations/no.js +0 -1
  60. package/build/translations/oc.js +0 -1
  61. package/build/translations/pl.js +0 -1
  62. package/build/translations/pt-br.js +0 -1
  63. package/build/translations/pt.js +0 -1
  64. package/build/translations/ro.js +0 -1
  65. package/build/translations/ru.js +0 -1
  66. package/build/translations/si.js +0 -1
  67. package/build/translations/sk.js +0 -1
  68. package/build/translations/sl.js +0 -1
  69. package/build/translations/sq.js +0 -1
  70. package/build/translations/sr-latn.js +0 -1
  71. package/build/translations/sr.js +0 -1
  72. package/build/translations/sv.js +0 -1
  73. package/build/translations/th.js +0 -1
  74. package/build/translations/ti.js +0 -1
  75. package/build/translations/tk.js +0 -1
  76. package/build/translations/tr.js +0 -1
  77. package/build/translations/tt.js +0 -1
  78. package/build/translations/ug.js +0 -1
  79. package/build/translations/uk.js +0 -1
  80. package/build/translations/ur.js +0 -1
  81. package/build/translations/uz.js +0 -1
  82. package/build/translations/vi.js +0 -1
  83. package/build/translations/zh-cn.js +0 -1
  84. package/build/translations/zh.js +0 -1
  85. package/lang/contexts.json +0 -9
  86. package/lang/translations/af.po +0 -40
  87. package/lang/translations/ar.po +0 -40
  88. package/lang/translations/ast.po +0 -40
  89. package/lang/translations/az.po +0 -40
  90. package/lang/translations/be.po +0 -40
  91. package/lang/translations/bg.po +0 -40
  92. package/lang/translations/bn.po +0 -40
  93. package/lang/translations/bs.po +0 -40
  94. package/lang/translations/ca.po +0 -40
  95. package/lang/translations/cs.po +0 -40
  96. package/lang/translations/da.po +0 -40
  97. package/lang/translations/de-ch.po +0 -40
  98. package/lang/translations/de.po +0 -40
  99. package/lang/translations/el.po +0 -40
  100. package/lang/translations/en-au.po +0 -40
  101. package/lang/translations/en-gb.po +0 -40
  102. package/lang/translations/en.po +0 -40
  103. package/lang/translations/eo.po +0 -40
  104. package/lang/translations/es-co.po +0 -40
  105. package/lang/translations/es.po +0 -40
  106. package/lang/translations/et.po +0 -40
  107. package/lang/translations/eu.po +0 -40
  108. package/lang/translations/fa.po +0 -40
  109. package/lang/translations/fi.po +0 -40
  110. package/lang/translations/fr.po +0 -40
  111. package/lang/translations/gl.po +0 -40
  112. package/lang/translations/gu.po +0 -40
  113. package/lang/translations/he.po +0 -40
  114. package/lang/translations/hi.po +0 -40
  115. package/lang/translations/hr.po +0 -40
  116. package/lang/translations/hu.po +0 -40
  117. package/lang/translations/hy.po +0 -40
  118. package/lang/translations/id.po +0 -40
  119. package/lang/translations/it.po +0 -40
  120. package/lang/translations/ja.po +0 -40
  121. package/lang/translations/jv.po +0 -40
  122. package/lang/translations/kk.po +0 -40
  123. package/lang/translations/km.po +0 -40
  124. package/lang/translations/kn.po +0 -40
  125. package/lang/translations/ko.po +0 -40
  126. package/lang/translations/ku.po +0 -40
  127. package/lang/translations/lt.po +0 -40
  128. package/lang/translations/lv.po +0 -40
  129. package/lang/translations/ms.po +0 -40
  130. package/lang/translations/nb.po +0 -40
  131. package/lang/translations/ne.po +0 -40
  132. package/lang/translations/nl.po +0 -40
  133. package/lang/translations/no.po +0 -40
  134. package/lang/translations/oc.po +0 -40
  135. package/lang/translations/pl.po +0 -40
  136. package/lang/translations/pt-br.po +0 -40
  137. package/lang/translations/pt.po +0 -40
  138. package/lang/translations/ro.po +0 -40
  139. package/lang/translations/ru.po +0 -40
  140. package/lang/translations/si.po +0 -40
  141. package/lang/translations/sk.po +0 -40
  142. package/lang/translations/sl.po +0 -40
  143. package/lang/translations/sq.po +0 -40
  144. package/lang/translations/sr-latn.po +0 -40
  145. package/lang/translations/sr.po +0 -40
  146. package/lang/translations/sv.po +0 -40
  147. package/lang/translations/th.po +0 -40
  148. package/lang/translations/ti.po +0 -40
  149. package/lang/translations/tk.po +0 -40
  150. package/lang/translations/tr.po +0 -40
  151. package/lang/translations/tt.po +0 -40
  152. package/lang/translations/ug.po +0 -40
  153. package/lang/translations/uk.po +0 -40
  154. package/lang/translations/ur.po +0 -40
  155. package/lang/translations/uz.po +0 -40
  156. package/lang/translations/vi.po +0 -40
  157. package/lang/translations/zh-cn.po +0 -40
  158. package/lang/translations/zh.po +0 -40
  159. package/src/augmentation.js +0 -5
  160. package/src/codeblock.js +0 -39
  161. package/src/codeblockcommand.js +0 -142
  162. package/src/codeblockconfig.js +0 -5
  163. package/src/codeblockediting.js +0 -430
  164. package/src/codeblockui.js +0 -134
  165. package/src/converters.js +0 -285
  166. package/src/indentcodeblockcommand.js +0 -82
  167. package/src/index.js +0 -16
  168. package/src/outdentcodeblockcommand.js +0 -137
  169. package/src/utils.js +0 -296
  170. package/theme/codeblock.css +0 -40
  171. /package/{src → dist}/augmentation.d.ts +0 -0
  172. /package/{src → dist}/codeblockconfig.d.ts +0 -0
  173. /package/{src → dist}/index.d.ts +0 -0
package/src/converters.js DELETED
@@ -1,285 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- import { getPropertyAssociation } from './utils.js';
6
- /**
7
- * A model-to-view (both editing and data) converter for the `codeBlock` element.
8
- *
9
- * Sample input:
10
- *
11
- * ```html
12
- * <codeBlock language="javascript">foo();<softBreak></softBreak>bar();</codeBlock>
13
- * ```
14
- *
15
- * Sample output (editing):
16
- *
17
- * ```html
18
- * <pre data-language="JavaScript"><code class="language-javascript">foo();<br />bar();</code></pre>
19
- * ```
20
- *
21
- * Sample output (data, see {@link module:code-block/converters~modelToDataViewSoftBreakInsertion}):
22
- *
23
- * ```html
24
- * <pre><code class="language-javascript">foo();\nbar();</code></pre>
25
- * ```
26
- *
27
- * @param languageDefs The normalized language configuration passed to the feature.
28
- * @param useLabels When `true`, the `<pre>` element will get a `data-language` attribute with a
29
- * human–readable label of the language. Used only in the editing.
30
- * @returns Returns a conversion callback.
31
- * @internal
32
- */
33
- export function modelToViewCodeBlockInsertion(model, languageDefs, useLabels = false) {
34
- // Language CSS classes:
35
- //
36
- // {
37
- // php: 'language-php',
38
- // python: 'language-python',
39
- // javascript: 'js',
40
- // ...
41
- // }
42
- const languagesToClasses = getPropertyAssociation(languageDefs, 'language', 'class');
43
- // Language labels:
44
- //
45
- // {
46
- // php: 'PHP',
47
- // python: 'Python',
48
- // javascript: 'JavaScript',
49
- // ...
50
- // }
51
- const languagesToLabels = getPropertyAssociation(languageDefs, 'language', 'label');
52
- return (evt, data, conversionApi) => {
53
- const { writer, mapper, consumable } = conversionApi;
54
- if (!consumable.consume(data.item, 'insert')) {
55
- return;
56
- }
57
- const codeBlockLanguage = data.item.getAttribute('language');
58
- const targetViewPosition = mapper.toViewPosition(model.createPositionBefore(data.item));
59
- const preAttributes = {};
60
- // Attributes added only in the editing view.
61
- if (useLabels) {
62
- preAttributes['data-language'] = languagesToLabels[codeBlockLanguage];
63
- preAttributes.spellcheck = 'false';
64
- }
65
- const codeAttributes = languagesToClasses[codeBlockLanguage] ? {
66
- class: languagesToClasses[codeBlockLanguage]
67
- } : undefined;
68
- const code = writer.createContainerElement('code', codeAttributes);
69
- const pre = writer.createContainerElement('pre', preAttributes, code);
70
- writer.insert(targetViewPosition, pre);
71
- mapper.bindElements(data.item, code);
72
- };
73
- }
74
- /**
75
- * A model-to-data view converter for the new line (`softBreak`) separator.
76
- *
77
- * Sample input:
78
- *
79
- * ```html
80
- * <codeBlock ...>foo();<softBreak></softBreak>bar();</codeBlock>
81
- * ```
82
- *
83
- * Sample output:
84
- *
85
- * ```html
86
- * <pre><code ...>foo();\nbar();</code></pre>
87
- * ```
88
- *
89
- * @returns Returns a conversion callback.
90
- * @internal
91
- */
92
- export function modelToDataViewSoftBreakInsertion(model) {
93
- return (evt, data, conversionApi) => {
94
- if (data.item.parent.name !== 'codeBlock') {
95
- return;
96
- }
97
- const { writer, mapper, consumable } = conversionApi;
98
- if (!consumable.consume(data.item, 'insert')) {
99
- return;
100
- }
101
- const position = mapper.toViewPosition(model.createPositionBefore(data.item));
102
- writer.insert(position, writer.createText('\n'));
103
- };
104
- }
105
- /**
106
- * A view-to-model converter for `<pre>` with the `<code>` HTML.
107
- *
108
- * Sample input:
109
- *
110
- * ```html
111
- * <pre><code class="language-javascript">foo();bar();</code></pre>
112
- * ```
113
- *
114
- * Sample output:
115
- *
116
- * ```html
117
- * <codeBlock language="javascript">foo();bar();</codeBlock>
118
- * ```
119
- *
120
- * @param languageDefs The normalized language configuration passed to the feature.
121
- * @returns Returns a conversion callback.
122
- * @internal
123
- */
124
- export function dataViewToModelCodeBlockInsertion(editingView, languageDefs) {
125
- // Language names associated with CSS classes:
126
- //
127
- // {
128
- // 'language-php': 'php',
129
- // 'language-python': 'python',
130
- // js: 'javascript',
131
- // ...
132
- // }
133
- const classesToLanguages = getPropertyAssociation(languageDefs, 'class', 'language');
134
- const defaultLanguageName = languageDefs[0].language;
135
- return (evt, data, conversionApi) => {
136
- const viewCodeElement = data.viewItem;
137
- const viewPreElement = viewCodeElement.parent;
138
- if (!viewPreElement || !viewPreElement.is('element', 'pre')) {
139
- return;
140
- }
141
- // In case of nested code blocks we don't want to convert to another code block.
142
- if (data.modelCursor.findAncestor('codeBlock')) {
143
- return;
144
- }
145
- const { consumable, writer } = conversionApi;
146
- if (!consumable.test(viewCodeElement, { name: true })) {
147
- return;
148
- }
149
- const codeBlock = writer.createElement('codeBlock');
150
- const viewChildClasses = [...viewCodeElement.getClassNames()];
151
- // As we're to associate each class with a model language, a lack of class (empty class) can be
152
- // also associated with a language if the language definition was configured so. Pushing an empty
153
- // string to make sure the association will work.
154
- if (!viewChildClasses.length) {
155
- viewChildClasses.push('');
156
- }
157
- // Figure out if any of the <code> element's class names is a valid programming
158
- // language class. If so, use it on the model element (becomes the language of the entire block).
159
- for (const className of viewChildClasses) {
160
- const language = classesToLanguages[className];
161
- if (language) {
162
- consumable.consume(viewCodeElement, { classes: [className] });
163
- writer.setAttribute('language', language, codeBlock);
164
- break;
165
- }
166
- }
167
- // If no language value was set, use the default language from the config.
168
- if (!codeBlock.hasAttribute('language')) {
169
- writer.setAttribute('language', defaultLanguageName, codeBlock);
170
- }
171
- // Convert children before inserting the code block element
172
- // to make sure that code block won't be splitted by any block.
173
- conversionApi.convertChildren(viewCodeElement, codeBlock);
174
- // Let's try to insert code block.
175
- if (!conversionApi.safeInsert(codeBlock, data.modelCursor)) {
176
- return;
177
- }
178
- consumable.consume(viewCodeElement, { name: true });
179
- conversionApi.updateConversionResult(codeBlock, data);
180
- };
181
- }
182
- /**
183
- * A view-to-model converter for new line characters in `<pre>`.
184
- *
185
- * Sample input:
186
- *
187
- * ```html
188
- * <pre><code class="language-javascript">foo();\nbar();</code></pre>
189
- * ```
190
- *
191
- * Sample output:
192
- *
193
- * ```html
194
- * <codeBlock language="javascript">foo();<softBreak></softBreak>bar();</codeBlock>
195
- * ```
196
- *
197
- * @returns {Function} Returns a conversion callback.
198
- * @internal
199
- */
200
- export function dataViewToModelTextNewlinesInsertion() {
201
- return (evt, data, { consumable, writer }) => {
202
- let position = data.modelCursor;
203
- // When node is already converted then do nothing.
204
- if (!consumable.test(data.viewItem)) {
205
- return;
206
- }
207
- // When not inside `codeBlock` then do nothing.
208
- if (!position.findAncestor('codeBlock')) {
209
- return;
210
- }
211
- consumable.consume(data.viewItem);
212
- const text = data.viewItem.data;
213
- const textLines = text.split('\n').map(data => writer.createText(data));
214
- const lastLine = textLines[textLines.length - 1];
215
- for (const node of textLines) {
216
- writer.insert(node, position);
217
- position = position.getShiftedBy(node.offsetSize);
218
- if (node !== lastLine) {
219
- const softBreak = writer.createElement('softBreak');
220
- writer.insert(softBreak, position);
221
- position = writer.createPositionAfter(softBreak);
222
- }
223
- }
224
- data.modelRange = writer.createRange(data.modelCursor, position);
225
- data.modelCursor = position;
226
- };
227
- }
228
- /**
229
- * A view-to-model converter that handles orphan text nodes (white spaces, new lines, etc.)
230
- * that surround `<code>` inside `<pre>`.
231
- *
232
- * Sample input:
233
- *
234
- * ```html
235
- * // White spaces
236
- * <pre> <code>foo()</code> </pre>
237
- *
238
- * // White spaces
239
- * <pre> <code>foo()</code> </pre>
240
- *
241
- * // White spaces
242
- * <pre> <code>foo()</code> </pre>
243
- *
244
- * // New lines
245
- * <pre>
246
- * <code>foo()</code>
247
- * </pre>
248
- *
249
- * // Redundant text
250
- * <pre>ABC<code>foo()</code>DEF</pre>
251
- * ```
252
- *
253
- * Unified output for each case:
254
- *
255
- * ```html
256
- * <codeBlock language="plaintext">foo()</codeBlock>
257
- * ```
258
- *
259
- * @returns Returns a conversion callback.
260
- * @internal
261
- */
262
- export function dataViewToModelOrphanNodeConsumer() {
263
- return (evt, data, { consumable }) => {
264
- const preElement = data.viewItem;
265
- // Don't clean up nested pre elements. Their content should stay as it is, they are not upcasted
266
- // to code blocks.
267
- if (preElement.findAncestor('pre')) {
268
- return;
269
- }
270
- const preChildren = Array.from(preElement.getChildren());
271
- const childCodeElement = preChildren.find(node => node.is('element', 'code'));
272
- // <code>-less <pre>. It will not upcast to code block in the model, skipping.
273
- if (!childCodeElement) {
274
- return;
275
- }
276
- for (const child of preChildren) {
277
- if (child === childCodeElement || !child.is('$text')) {
278
- continue;
279
- }
280
- // Consuming the orphan to remove it from the input data.
281
- // Second argument in `consumable.consume` is discarded for text nodes.
282
- consumable.consume(child, { name: true });
283
- }
284
- };
285
- }
@@ -1,82 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module code-block/indentcodeblockcommand
7
- */
8
- import { Command } from 'ckeditor5/src/core.js';
9
- import { getIndentOutdentPositions, isModelSelectionInCodeBlock } from './utils.js';
10
- /**
11
- * The code block indentation increase command plugin.
12
- */
13
- export class IndentCodeBlockCommand extends Command {
14
- /**
15
- * A sequence of characters added to the line when the command is executed.
16
- */
17
- _indentSequence;
18
- constructor(editor) {
19
- super(editor);
20
- this._indentSequence = editor.config.get('codeBlock.indentSequence');
21
- }
22
- /**
23
- * @inheritDoc
24
- */
25
- refresh() {
26
- this.isEnabled = this._checkEnabled();
27
- }
28
- /**
29
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
30
- * code lines in the selection will be increased.
31
- *
32
- * @fires execute
33
- */
34
- execute() {
35
- const editor = this.editor;
36
- const model = editor.model;
37
- model.change(writer => {
38
- const positions = getIndentOutdentPositions(model);
39
- // Indent all positions, for instance assuming the indent sequence is 4x space (" "):
40
- //
41
- // <codeBlock>^foo</codeBlock> -> <codeBlock> foo</codeBlock>
42
- //
43
- // <codeBlock>foo^bar</codeBlock> -> <codeBlock>foo bar</codeBlock>
44
- //
45
- // Also, when there is more than one position:
46
- //
47
- // <codeBlock>
48
- // ^foobar
49
- // <softBreak></softBreak>
50
- // ^bazqux
51
- // </codeBlock>
52
- //
53
- // ->
54
- //
55
- // <codeBlock>
56
- // foobar
57
- // <softBreak></softBreak>
58
- // bazqux
59
- // </codeBlock>
60
- //
61
- for (const position of positions) {
62
- const indentSequenceTextElement = writer.createText(this._indentSequence);
63
- // Previously insertion was done by writer.insertText(). It was changed to insertContent() to enable
64
- // integration of code block with track changes. It's the easiest way of integration because insertContent()
65
- // is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
66
- // some additional work will be required in track changes integration of code block.
67
- model.insertContent(indentSequenceTextElement, position);
68
- }
69
- });
70
- }
71
- /**
72
- * Checks whether the command can be enabled in the current context.
73
- */
74
- _checkEnabled() {
75
- if (!this._indentSequence) {
76
- return false;
77
- }
78
- // Indent (forward) command is always enabled when there's any code block in the selection
79
- // because you can always indent code lines.
80
- return isModelSelectionInCodeBlock(this.editor.model.document.selection);
81
- }
82
- }
package/src/index.js DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module code-block
7
- */
8
- export { CodeBlock } from './codeblock.js';
9
- export { CodeBlockEditing } from './codeblockediting.js';
10
- export { CodeBlockUI } from './codeblockui.js';
11
- export { CodeBlockCommand } from './codeblockcommand.js';
12
- export { IndentCodeBlockCommand } from './indentcodeblockcommand.js';
13
- export { OutdentCodeBlockCommand } from './outdentcodeblockcommand.js';
14
- export { modelToViewCodeBlockInsertion as _modelToViewCodeBlockInsertion, modelToDataViewSoftBreakInsertion as _modelToDataViewCodeBlockSoftBreakInsertion, dataViewToModelCodeBlockInsertion as _dataViewToModelCodeBlockInsertion, dataViewToModelTextNewlinesInsertion as _dataViewToModelCodeBlockTextNewlinesInsertion, dataViewToModelOrphanNodeConsumer as _dataViewToModelCodeBlockOrphanNodeConsumer } from './converters.js';
15
- export { getNormalizedAndLocalizedLanguageDefinitions as _getNormalizedAndLocalizedCodeBlockLanguageDefinitions, getPropertyAssociation as _getCodeBlockPropertyAssociation, getLeadingWhiteSpaces as _getCodeBlockLeadingWhiteSpaces, rawSnippetTextToViewDocumentFragment as _rawCodeBlockSnippetTextToViewDocumentFragment, getIndentOutdentPositions as _getCodeBlockIndentOutdentPositions, isModelSelectionInCodeBlock as _isModelSelectionInCodeBlock, canBeCodeBlock as _canBeCodeBlock, getCodeBlockAriaAnnouncement as _getCodeBlockAriaAnnouncement, getTextNodeAtLineStart as _getCodeBlockTextNodeAtLineStart } from './utils.js';
16
- import './augmentation.js';
@@ -1,137 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- import { Command } from 'ckeditor5/src/core.js';
6
- import { getLeadingWhiteSpaces, getIndentOutdentPositions, isModelSelectionInCodeBlock, getTextNodeAtLineStart } from './utils.js';
7
- /**
8
- * The code block indentation decrease command plugin.
9
- */
10
- export class OutdentCodeBlockCommand extends Command {
11
- /**
12
- * A sequence of characters removed from the line when the command is executed.
13
- */
14
- _indentSequence;
15
- constructor(editor) {
16
- super(editor);
17
- this._indentSequence = editor.config.get('codeBlock.indentSequence');
18
- }
19
- /**
20
- * @inheritDoc
21
- */
22
- refresh() {
23
- this.isEnabled = this._checkEnabled();
24
- }
25
- /**
26
- * Executes the command. When the command {@link #isEnabled is enabled}, the indentation of the
27
- * code lines in the selection will be decreased.
28
- *
29
- * @fires execute
30
- */
31
- execute() {
32
- const editor = this.editor;
33
- const model = editor.model;
34
- model.change(() => {
35
- const positions = getIndentOutdentPositions(model);
36
- // Outdent all positions, for instance assuming the indent sequence is 4x space (" "):
37
- //
38
- // <codeBlock>^foo</codeBlock> -> <codeBlock>foo</codeBlock>
39
- //
40
- // <codeBlock> ^bar</codeBlock> -> <codeBlock>bar</codeBlock>
41
- //
42
- // Also, when there is more than one position:
43
- //
44
- // <codeBlock>
45
- // ^foobar
46
- // <softBreak></softBreak>
47
- // ^bazqux
48
- // </codeBlock>
49
- //
50
- // ->
51
- //
52
- // <codeBlock>
53
- // foobar
54
- // <softBreak></softBreak>
55
- // bazqux
56
- // </codeBlock>
57
- for (const position of positions) {
58
- const range = getLastOutdentableSequenceRange(model, position, this._indentSequence);
59
- if (range) {
60
- // Previously deletion was done by writer.remove(). It was changed to deleteContent() to enable
61
- // integration of code block with track changes. It's the easiest way of integration because deleteContent()
62
- // is already integrated with track changes, but if it ever cause any troubles it can be reverted, however
63
- // some additional work will be required in track changes integration of code block.
64
- model.deleteContent(model.createSelection(range));
65
- }
66
- }
67
- });
68
- }
69
- /**
70
- * Checks whether the command can be enabled in the current context.
71
- *
72
- * @private
73
- * @returns {Boolean} Whether the command should be enabled.
74
- */
75
- _checkEnabled() {
76
- if (!this._indentSequence) {
77
- return false;
78
- }
79
- const model = this.editor.model;
80
- if (!isModelSelectionInCodeBlock(model.document.selection)) {
81
- return false;
82
- }
83
- // Outdent command can execute only when there is an indent character sequence
84
- // in some of the lines.
85
- return getIndentOutdentPositions(model).some(position => {
86
- return getLastOutdentableSequenceRange(model, position, this._indentSequence);
87
- });
88
- }
89
- }
90
- // For a position coming from `getIndentOutdentPositions()`, it returns the range representing
91
- // the last occurrence of the indent sequence among the leading whitespaces of the code line the
92
- // position represents.
93
- //
94
- // For instance, assuming the indent sequence is 4x space (" "):
95
- //
96
- // <codeBlock>foo^</codeBlock> -> null
97
- // <codeBlock>foo^<softBreak></softBreak>bar</codeBlock> -> null
98
- // <codeBlock> ^foo</codeBlock> -> null
99
- // <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
100
- // <codeBlock> ^foo bar</codeBlock> -> <codeBlock>[ ]foo bar</codeBlock>
101
- //
102
- // @param {<module:engine/model/model~Model>} model
103
- // @param {<module:engine/model/position~ModelPosition>} position
104
- // @param {String} sequence
105
- // @returns {<module:engine/model/range~ModelRange>|null}
106
- function getLastOutdentableSequenceRange(model, position, sequence) {
107
- // Positions start before each text node (code line). Get the node corresponding to the position.
108
- const nodeAtPosition = getTextNodeAtLineStart(position, model);
109
- if (!nodeAtPosition) {
110
- return null;
111
- }
112
- const leadingWhiteSpaces = getLeadingWhiteSpaces(nodeAtPosition);
113
- const lastIndexOfSequence = leadingWhiteSpaces.lastIndexOf(sequence);
114
- // For instance, assuming the indent sequence is 4x space (" "):
115
- //
116
- // <codeBlock> ^foo</codeBlock> -> null
117
- //
118
- if (lastIndexOfSequence + sequence.length !== leadingWhiteSpaces.length) {
119
- return null;
120
- }
121
- // For instance, assuming the indent sequence is 4x space (" "):
122
- //
123
- // <codeBlock> ^foo</codeBlock> -> null
124
- //
125
- if (lastIndexOfSequence === -1) {
126
- return null;
127
- }
128
- const { parent, startOffset } = nodeAtPosition;
129
- // Create a range that contains the **last** indent sequence among the leading whitespaces
130
- // of the line.
131
- //
132
- // For instance, assuming the indent sequence is 4x space (" "):
133
- //
134
- // <codeBlock> ^foo</codeBlock> -> <codeBlock> [ ]foo</codeBlock>
135
- //
136
- return model.createRange(model.createPositionAt(parent, startOffset + lastIndexOfSequence), model.createPositionAt(parent, startOffset + lastIndexOfSequence + sequence.length));
137
- }