@ckeditor/ckeditor5-autoformat 47.6.1 → 48.0.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.
Files changed (162) hide show
  1. package/ckeditor5-metadata.json +1 -1
  2. package/{src → dist}/autoformat.d.ts +2 -2
  3. package/{src → dist}/blockautoformatediting.d.ts +1 -1
  4. package/dist/index.css +3 -0
  5. package/dist/index.css.map +1 -0
  6. package/dist/index.js.map +1 -1
  7. package/{src → dist}/inlineautoformatediting.d.ts +2 -2
  8. package/package.json +23 -46
  9. package/build/autoformat.js +0 -5
  10. package/build/translations/af.js +0 -1
  11. package/build/translations/ar.js +0 -1
  12. package/build/translations/ast.js +0 -1
  13. package/build/translations/az.js +0 -1
  14. package/build/translations/be.js +0 -1
  15. package/build/translations/bg.js +0 -1
  16. package/build/translations/bn.js +0 -1
  17. package/build/translations/bs.js +0 -1
  18. package/build/translations/ca.js +0 -1
  19. package/build/translations/cs.js +0 -1
  20. package/build/translations/da.js +0 -1
  21. package/build/translations/de-ch.js +0 -1
  22. package/build/translations/de.js +0 -1
  23. package/build/translations/el.js +0 -1
  24. package/build/translations/en-au.js +0 -1
  25. package/build/translations/en-gb.js +0 -1
  26. package/build/translations/eo.js +0 -1
  27. package/build/translations/es-co.js +0 -1
  28. package/build/translations/es.js +0 -1
  29. package/build/translations/et.js +0 -1
  30. package/build/translations/eu.js +0 -1
  31. package/build/translations/fa.js +0 -1
  32. package/build/translations/fi.js +0 -1
  33. package/build/translations/fr.js +0 -1
  34. package/build/translations/gl.js +0 -1
  35. package/build/translations/gu.js +0 -1
  36. package/build/translations/he.js +0 -1
  37. package/build/translations/hi.js +0 -1
  38. package/build/translations/hr.js +0 -1
  39. package/build/translations/hu.js +0 -1
  40. package/build/translations/hy.js +0 -1
  41. package/build/translations/id.js +0 -1
  42. package/build/translations/it.js +0 -1
  43. package/build/translations/ja.js +0 -1
  44. package/build/translations/jv.js +0 -1
  45. package/build/translations/kk.js +0 -1
  46. package/build/translations/km.js +0 -1
  47. package/build/translations/kn.js +0 -1
  48. package/build/translations/ko.js +0 -1
  49. package/build/translations/ku.js +0 -1
  50. package/build/translations/lt.js +0 -1
  51. package/build/translations/lv.js +0 -1
  52. package/build/translations/ms.js +0 -1
  53. package/build/translations/nb.js +0 -1
  54. package/build/translations/ne.js +0 -1
  55. package/build/translations/nl.js +0 -1
  56. package/build/translations/no.js +0 -1
  57. package/build/translations/oc.js +0 -1
  58. package/build/translations/pl.js +0 -1
  59. package/build/translations/pt-br.js +0 -1
  60. package/build/translations/pt.js +0 -1
  61. package/build/translations/ro.js +0 -1
  62. package/build/translations/ru.js +0 -1
  63. package/build/translations/si.js +0 -1
  64. package/build/translations/sk.js +0 -1
  65. package/build/translations/sl.js +0 -1
  66. package/build/translations/sq.js +0 -1
  67. package/build/translations/sr-latn.js +0 -1
  68. package/build/translations/sr.js +0 -1
  69. package/build/translations/sv.js +0 -1
  70. package/build/translations/th.js +0 -1
  71. package/build/translations/ti.js +0 -1
  72. package/build/translations/tk.js +0 -1
  73. package/build/translations/tr.js +0 -1
  74. package/build/translations/tt.js +0 -1
  75. package/build/translations/ug.js +0 -1
  76. package/build/translations/uk.js +0 -1
  77. package/build/translations/ur.js +0 -1
  78. package/build/translations/uz.js +0 -1
  79. package/build/translations/vi.js +0 -1
  80. package/build/translations/zh-cn.js +0 -1
  81. package/build/translations/zh.js +0 -1
  82. package/lang/contexts.json +0 -3
  83. package/lang/translations/af.po +0 -16
  84. package/lang/translations/ar.po +0 -16
  85. package/lang/translations/ast.po +0 -16
  86. package/lang/translations/az.po +0 -16
  87. package/lang/translations/be.po +0 -16
  88. package/lang/translations/bg.po +0 -16
  89. package/lang/translations/bn.po +0 -16
  90. package/lang/translations/bs.po +0 -16
  91. package/lang/translations/ca.po +0 -16
  92. package/lang/translations/cs.po +0 -16
  93. package/lang/translations/da.po +0 -16
  94. package/lang/translations/de-ch.po +0 -16
  95. package/lang/translations/de.po +0 -16
  96. package/lang/translations/el.po +0 -16
  97. package/lang/translations/en-au.po +0 -16
  98. package/lang/translations/en-gb.po +0 -16
  99. package/lang/translations/en.po +0 -16
  100. package/lang/translations/eo.po +0 -16
  101. package/lang/translations/es-co.po +0 -16
  102. package/lang/translations/es.po +0 -16
  103. package/lang/translations/et.po +0 -16
  104. package/lang/translations/eu.po +0 -16
  105. package/lang/translations/fa.po +0 -16
  106. package/lang/translations/fi.po +0 -16
  107. package/lang/translations/fr.po +0 -16
  108. package/lang/translations/gl.po +0 -16
  109. package/lang/translations/gu.po +0 -16
  110. package/lang/translations/he.po +0 -16
  111. package/lang/translations/hi.po +0 -16
  112. package/lang/translations/hr.po +0 -16
  113. package/lang/translations/hu.po +0 -16
  114. package/lang/translations/hy.po +0 -16
  115. package/lang/translations/id.po +0 -16
  116. package/lang/translations/it.po +0 -16
  117. package/lang/translations/ja.po +0 -16
  118. package/lang/translations/jv.po +0 -16
  119. package/lang/translations/kk.po +0 -16
  120. package/lang/translations/km.po +0 -16
  121. package/lang/translations/kn.po +0 -16
  122. package/lang/translations/ko.po +0 -16
  123. package/lang/translations/ku.po +0 -16
  124. package/lang/translations/lt.po +0 -16
  125. package/lang/translations/lv.po +0 -16
  126. package/lang/translations/ms.po +0 -16
  127. package/lang/translations/nb.po +0 -16
  128. package/lang/translations/ne.po +0 -16
  129. package/lang/translations/nl.po +0 -16
  130. package/lang/translations/no.po +0 -16
  131. package/lang/translations/oc.po +0 -16
  132. package/lang/translations/pl.po +0 -16
  133. package/lang/translations/pt-br.po +0 -16
  134. package/lang/translations/pt.po +0 -16
  135. package/lang/translations/ro.po +0 -16
  136. package/lang/translations/ru.po +0 -16
  137. package/lang/translations/si.po +0 -16
  138. package/lang/translations/sk.po +0 -16
  139. package/lang/translations/sl.po +0 -16
  140. package/lang/translations/sq.po +0 -16
  141. package/lang/translations/sr-latn.po +0 -16
  142. package/lang/translations/sr.po +0 -16
  143. package/lang/translations/sv.po +0 -16
  144. package/lang/translations/th.po +0 -16
  145. package/lang/translations/ti.po +0 -16
  146. package/lang/translations/tk.po +0 -16
  147. package/lang/translations/tr.po +0 -16
  148. package/lang/translations/tt.po +0 -16
  149. package/lang/translations/ug.po +0 -16
  150. package/lang/translations/uk.po +0 -16
  151. package/lang/translations/ur.po +0 -16
  152. package/lang/translations/uz.po +0 -16
  153. package/lang/translations/vi.po +0 -16
  154. package/lang/translations/zh-cn.po +0 -16
  155. package/lang/translations/zh.po +0 -16
  156. package/src/augmentation.js +0 -5
  157. package/src/autoformat.js +0 -206
  158. package/src/blockautoformatediting.js +0 -158
  159. package/src/index.js +0 -11
  160. package/src/inlineautoformatediting.js +0 -174
  161. /package/{src → dist}/augmentation.d.ts +0 -0
  162. /package/{src → dist}/index.d.ts +0 -0
@@ -1,158 +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 { ModelLiveRange, ModelSchemaContext } from 'ckeditor5/src/engine.js';
6
- import { first } from 'ckeditor5/src/utils.js';
7
- /**
8
- * The block autoformatting engine. It allows to format various block patterns. For example,
9
- * it can be configured to turn a paragraph starting with `*` and followed by a space into a list item.
10
- *
11
- * The autoformatting operation is integrated with the undo manager,
12
- * so the autoformatting step can be undone if the user's intention was not to format the text.
13
- *
14
- * See the {@link module:autoformat/blockautoformatediting~blockAutoformatEditing `blockAutoformatEditing`} documentation
15
- * to learn how to create custom block autoformatters. You can also use
16
- * the {@link module:autoformat/autoformat~Autoformat} feature which enables a set of default autoformatters
17
- * (lists, headings, bold and italic).
18
- *
19
- * @module autoformat/blockautoformatediting
20
- */
21
- /**
22
- * Creates a listener triggered on {@link module:engine/model/document~ModelDocument#event:change:data `change:data`} event in the document.
23
- * Calls the callback when inserted text matches the regular expression or the command name
24
- * if provided instead of the callback.
25
- *
26
- * Examples of usage:
27
- *
28
- * To convert a paragraph into heading 1 when `- ` is typed, using just the command name:
29
- *
30
- * ```ts
31
- * blockAutoformatEditing( editor, plugin, /^\- $/, 'heading1' );
32
- * ```
33
- *
34
- * To convert a paragraph into heading 1 when `- ` is typed, using just the callback:
35
- *
36
- * ```ts
37
- * blockAutoformatEditing( editor, plugin, /^\- $/, ( context ) => {
38
- * const { match } = context;
39
- * const headingLevel = match[ 1 ].length;
40
- *
41
- * editor.execute( 'heading', {
42
- * formatId: `heading${ headingLevel }`
43
- * } );
44
- * } );
45
- * ```
46
- *
47
- * @param editor The editor instance.
48
- * @param plugin The autoformat plugin instance.
49
- * @param pattern The regular expression to execute on just inserted text. The regular expression is tested against the text
50
- * from the beginning until the caret position.
51
- * @param callbackOrCommand The callback to execute or the command to run when the text is matched.
52
- * In case of providing the callback, it receives the following parameter:
53
- * * match RegExp.exec() result of matching the pattern to inserted text.
54
- */
55
- export function blockAutoformatEditing(editor, plugin, pattern, callbackOrCommand) {
56
- let callback;
57
- let command = null;
58
- if (typeof callbackOrCommand == 'function') {
59
- callback = callbackOrCommand;
60
- }
61
- else {
62
- // We assume that the actual command name was provided.
63
- command = editor.commands.get(callbackOrCommand);
64
- callback = () => {
65
- editor.execute(callbackOrCommand);
66
- };
67
- }
68
- editor.model.document.on('change:data', (evt, batch) => {
69
- if (command && !command.isEnabled || !plugin.isEnabled) {
70
- return;
71
- }
72
- const range = first(editor.model.document.selection.getRanges());
73
- if (!range.isCollapsed) {
74
- return;
75
- }
76
- if (batch.isUndo || !batch.isLocal) {
77
- return;
78
- }
79
- const changes = Array.from(editor.model.document.differ.getChanges());
80
- const entry = changes[0];
81
- // Typing is represented by only a single change.
82
- if (changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1) {
83
- return;
84
- }
85
- const blockToFormat = entry.position.parent;
86
- // Block formatting should be disabled in codeBlocks (#5800).
87
- if (blockToFormat.is('element', 'codeBlock')) {
88
- return;
89
- }
90
- // Only list commands and custom callbacks can be applied inside a list.
91
- if (blockToFormat.is('element', 'listItem') &&
92
- typeof callbackOrCommand !== 'function' &&
93
- !['numberedList', 'bulletedList', 'todoList'].includes(callbackOrCommand)) {
94
- return;
95
- }
96
- // In case a command is bound, do not re-execute it over an existing block style which would result in a style removal.
97
- // Instead, just drop processing so that autoformat trigger text is not lost. E.g. writing "# " in a level 1 heading.
98
- if (command && command.value === true) {
99
- return;
100
- }
101
- const firstNode = blockToFormat.getChild(0);
102
- const firstNodeRange = editor.model.createRangeOn(firstNode);
103
- // Range is only expected to be within or at the very end of the first text node.
104
- if (!firstNodeRange.containsRange(range) && !range.end.isEqual(firstNodeRange.end)) {
105
- return;
106
- }
107
- const match = pattern.exec(firstNode.data.substr(0, range.end.offset));
108
- // ...and this text node's data match the pattern.
109
- if (!match) {
110
- return;
111
- }
112
- // Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
113
- editor.model.enqueueChange(writer => {
114
- const selection = editor.model.document.selection;
115
- // Matched range.
116
- const start = writer.createPositionAt(blockToFormat, 0);
117
- const end = writer.createPositionAt(blockToFormat, match[0].length);
118
- const range = new ModelLiveRange(start, end);
119
- const wasChanged = callback({ match });
120
- // Remove matched text.
121
- if (wasChanged !== false) {
122
- // Store selection attributes to restore them after matched text removed.
123
- const selectionAttributes = Array.from(selection.getAttributes());
124
- writer.remove(range);
125
- const selectionRange = selection.getFirstRange();
126
- const blockRange = writer.createRangeIn(blockToFormat);
127
- // If the block is empty and the document selection has been moved when
128
- // applying formatting (e.g. is now in newly created block).
129
- if (blockToFormat.isEmpty && !blockRange.isEqual(selectionRange) && !blockRange.containsRange(selectionRange, true)) {
130
- writer.remove(blockToFormat);
131
- }
132
- // Restore selection attributes.
133
- restoreSelectionAttributes(writer, selection, selectionAttributes);
134
- }
135
- range.detach();
136
- editor.model.enqueueChange(() => {
137
- const deletePlugin = editor.plugins.get('Delete');
138
- deletePlugin.requestUndoOnBackspace();
139
- });
140
- });
141
- });
142
- }
143
- /**
144
- * Restore allowed selection attributes.
145
- */
146
- function restoreSelectionAttributes(writer, selection, selectionAttributes) {
147
- const schema = writer.model.schema;
148
- const selectionPosition = selection.getFirstPosition();
149
- let selectionSchemaContext = new ModelSchemaContext(selectionPosition);
150
- if (schema.checkChild(selectionSchemaContext, '$text')) {
151
- selectionSchemaContext = selectionSchemaContext.push('$text');
152
- }
153
- for (const [attributeName, attributeValue] of selectionAttributes) {
154
- if (schema.checkAttribute(selectionSchemaContext, attributeName)) {
155
- writer.setSelectionAttribute(attributeName, attributeValue);
156
- }
157
- }
158
- }
package/src/index.js DELETED
@@ -1,11 +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 autoformat
7
- */
8
- export { Autoformat } from './autoformat.js';
9
- export { blockAutoformatEditing } from './blockautoformatediting.js';
10
- export { inlineAutoformatEditing } from './inlineautoformatediting.js';
11
- import './augmentation.js';
@@ -1,174 +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
- * Enables autoformatting mechanism for a given {@link module:core/editor/editor~Editor}.
7
- *
8
- * It formats the matched text by applying the given model attribute or by running the provided formatting callback.
9
- * On every {@link module:engine/model/document~ModelDocument#event:change:data data change} in the model document
10
- * the autoformatting engine checks the text on the left of the selection
11
- * and executes the provided action if the text matches given criteria (regular expression or callback).
12
- *
13
- * @param editor The editor instance.
14
- * @param plugin The autoformat plugin instance.
15
- * @param testRegexpOrCallback The regular expression or callback to execute on text.
16
- * Provided regular expression *must* have three capture groups. The first and the third capture group
17
- * should match opening and closing delimiters. The second capture group should match the text to format.
18
- *
19
- * ```ts
20
- * // Matches the `**bold text**` pattern.
21
- * // There are three capturing groups:
22
- * // - The first to match the starting `**` delimiter.
23
- * // - The second to match the text to format.
24
- * // - The third to match the ending `**` delimiter.
25
- * inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, formatCallback );
26
- * ```
27
- *
28
- * When a function is provided instead of the regular expression, it will be executed with the text to match as a parameter.
29
- * The function should return proper "ranges" to delete and format.
30
- *
31
- * ```ts
32
- * {
33
- * remove: [
34
- * [ 0, 1 ], // Remove the first letter from the given text.
35
- * [ 5, 6 ] // Remove the 6th letter from the given text.
36
- * ],
37
- * format: [
38
- * [ 1, 5 ] // Format all letters from 2nd to 5th.
39
- * ]
40
- * }
41
- * ```
42
- *
43
- * @param formatCallback A callback to apply actual formatting.
44
- * It should return `false` if changes should not be applied (e.g. if a command is disabled).
45
- *
46
- * ```ts
47
- * inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
48
- * const command = editor.commands.get( 'bold' );
49
- *
50
- * if ( !command.isEnabled ) {
51
- * return false;
52
- * }
53
- *
54
- * const validRanges = editor.model.schema.getValidRanges( rangesToFormat, 'bold' );
55
- *
56
- * for ( let range of validRanges ) {
57
- * writer.setAttribute( 'bold', true, range );
58
- * }
59
- * } );
60
- * ```
61
- */
62
- export function inlineAutoformatEditing(editor, plugin, testRegexpOrCallback, formatCallback) {
63
- let regExp;
64
- let testCallback;
65
- if (testRegexpOrCallback instanceof RegExp) {
66
- regExp = testRegexpOrCallback;
67
- }
68
- else {
69
- testCallback = testRegexpOrCallback;
70
- }
71
- // A test callback run on changed text.
72
- testCallback = testCallback || (text => {
73
- let result;
74
- const remove = [];
75
- const format = [];
76
- while ((result = regExp.exec(text)) !== null) {
77
- // There should be full match and 3 capture groups.
78
- if (result && result.length < 4) {
79
- break;
80
- }
81
- let { index, '1': leftDel, '2': content, '3': rightDel } = result;
82
- // Real matched string - there might be some non-capturing groups so we need to recalculate starting index.
83
- const found = leftDel + content + rightDel;
84
- index += result[0].length - found.length;
85
- // Start and End offsets of delimiters to remove.
86
- const delStart = [
87
- index,
88
- index + leftDel.length
89
- ];
90
- const delEnd = [
91
- index + leftDel.length + content.length,
92
- index + leftDel.length + content.length + rightDel.length
93
- ];
94
- remove.push(delStart);
95
- remove.push(delEnd);
96
- format.push([index + leftDel.length, index + leftDel.length + content.length]);
97
- }
98
- return {
99
- remove,
100
- format
101
- };
102
- });
103
- editor.model.document.on('change:data', (evt, batch) => {
104
- if (batch.isUndo || !batch.isLocal || !plugin.isEnabled) {
105
- return;
106
- }
107
- const model = editor.model;
108
- const selection = model.document.selection;
109
- // Do nothing if selection is not collapsed.
110
- if (!selection.isCollapsed) {
111
- return;
112
- }
113
- const changes = Array.from(model.document.differ.getChanges());
114
- const entry = changes[0];
115
- // Typing is represented by only a single change.
116
- if (changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1) {
117
- return;
118
- }
119
- const focus = selection.focus;
120
- const block = focus.parent;
121
- const { text, range } = getTextAfterCode(model.createRange(model.createPositionAt(block, 0), focus), model);
122
- const testOutput = testCallback(text);
123
- const rangesToFormat = testOutputToRanges(range.start, testOutput.format, model);
124
- const rangesToRemove = testOutputToRanges(range.start, testOutput.remove, model);
125
- if (!(rangesToFormat.length && rangesToRemove.length)) {
126
- return;
127
- }
128
- // Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
129
- model.enqueueChange(writer => {
130
- // Apply format.
131
- const hasChanged = formatCallback(writer, rangesToFormat);
132
- // Strict check on `false` to have backward compatibility (when callbacks were returning `undefined`).
133
- if (hasChanged === false) {
134
- return;
135
- }
136
- // Remove delimiters - use reversed order to not mix the offsets while removing.
137
- for (const range of rangesToRemove.reverse()) {
138
- writer.remove(range);
139
- }
140
- model.enqueueChange(() => {
141
- const deletePlugin = editor.plugins.get('Delete');
142
- deletePlugin.requestUndoOnBackspace();
143
- });
144
- });
145
- });
146
- }
147
- /**
148
- * Converts output of the test function provided to the inlineAutoformatEditing and converts it to the model ranges
149
- * inside provided block.
150
- */
151
- function testOutputToRanges(start, arrays, model) {
152
- return arrays
153
- .filter(array => (array[0] !== undefined && array[1] !== undefined))
154
- .map(array => {
155
- return model.createRange(start.getShiftedBy(array[0]), start.getShiftedBy(array[1]));
156
- });
157
- }
158
- /**
159
- * Returns the last text line after the last code element from the given range.
160
- * It is similar to {@link module:typing/utils/getlasttextline.getLastTextLine `getLastTextLine()`},
161
- * but it ignores any text before the last `code`.
162
- */
163
- function getTextAfterCode(range, model) {
164
- let start = range.start;
165
- const text = Array.from(range.getItems()).reduce((rangeText, node) => {
166
- // Trim text to a last occurrence of an inline element and update range start.
167
- if (!(node.is('$text') || node.is('$textProxy')) || node.getAttribute('code')) {
168
- start = model.createPositionAfter(node);
169
- return '';
170
- }
171
- return rangeText + node.data;
172
- }, '');
173
- return { text, range: model.createRange(start, range.end) };
174
- }
File without changes
File without changes