@ckeditor/ckeditor5-autoformat 40.0.0 → 40.2.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/CHANGELOG.md +28 -28
- package/LICENSE.md +2 -2
- package/package.json +2 -2
- package/src/augmentation.d.ts +10 -10
- package/src/augmentation.js +5 -5
- package/src/autoformat.d.ts +81 -81
- package/src/autoformat.js +189 -189
- package/src/blockautoformatediting.d.ts +57 -57
- package/src/blockautoformatediting.js +137 -137
- package/src/index.d.ts +9 -9
- package/src/index.js +9 -9
- package/src/inlineautoformatediting.d.ts +83 -83
- package/src/inlineautoformatediting.js +174 -174
- package/build/autoformat.js.map +0 -1
|
@@ -1,137 +1,137 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, 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 { LiveRange } from 'ckeditor5/src/engine';
|
|
6
|
-
import { first } from 'ckeditor5/src/utils';
|
|
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~Document#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 default 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
|
-
// Matched range.
|
|
115
|
-
const start = writer.createPositionAt(blockToFormat, 0);
|
|
116
|
-
const end = writer.createPositionAt(blockToFormat, match[0].length);
|
|
117
|
-
const range = new LiveRange(start, end);
|
|
118
|
-
const wasChanged = callback({ match });
|
|
119
|
-
// Remove matched text.
|
|
120
|
-
if (wasChanged !== false) {
|
|
121
|
-
writer.remove(range);
|
|
122
|
-
const selectionRange = editor.model.document.selection.getFirstRange();
|
|
123
|
-
const blockRange = writer.createRangeIn(blockToFormat);
|
|
124
|
-
// If the block is empty and the document selection has been moved when
|
|
125
|
-
// applying formatting (e.g. is now in newly created block).
|
|
126
|
-
if (blockToFormat.isEmpty && !blockRange.isEqual(selectionRange) && !blockRange.containsRange(selectionRange, true)) {
|
|
127
|
-
writer.remove(blockToFormat);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
range.detach();
|
|
131
|
-
editor.model.enqueueChange(() => {
|
|
132
|
-
const deletePlugin = editor.plugins.get('Delete');
|
|
133
|
-
deletePlugin.requestUndoOnBackspace();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, 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 { LiveRange } from 'ckeditor5/src/engine';
|
|
6
|
+
import { first } from 'ckeditor5/src/utils';
|
|
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~Document#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 default 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
|
+
// Matched range.
|
|
115
|
+
const start = writer.createPositionAt(blockToFormat, 0);
|
|
116
|
+
const end = writer.createPositionAt(blockToFormat, match[0].length);
|
|
117
|
+
const range = new LiveRange(start, end);
|
|
118
|
+
const wasChanged = callback({ match });
|
|
119
|
+
// Remove matched text.
|
|
120
|
+
if (wasChanged !== false) {
|
|
121
|
+
writer.remove(range);
|
|
122
|
+
const selectionRange = editor.model.document.selection.getFirstRange();
|
|
123
|
+
const blockRange = writer.createRangeIn(blockToFormat);
|
|
124
|
+
// If the block is empty and the document selection has been moved when
|
|
125
|
+
// applying formatting (e.g. is now in newly created block).
|
|
126
|
+
if (blockToFormat.isEmpty && !blockRange.isEqual(selectionRange) && !blockRange.containsRange(selectionRange, true)) {
|
|
127
|
+
writer.remove(blockToFormat);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
range.detach();
|
|
131
|
+
editor.model.enqueueChange(() => {
|
|
132
|
+
const deletePlugin = editor.plugins.get('Delete');
|
|
133
|
+
deletePlugin.requestUndoOnBackspace();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, 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
|
-
/**
|
|
6
|
-
* @module autoformat
|
|
7
|
-
*/
|
|
8
|
-
export { default as Autoformat } from './autoformat';
|
|
9
|
-
import './augmentation';
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
|
6
|
+
* @module autoformat
|
|
7
|
+
*/
|
|
8
|
+
export { default as Autoformat } from './autoformat';
|
|
9
|
+
import './augmentation';
|
package/src/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, 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
|
-
/**
|
|
6
|
-
* @module autoformat
|
|
7
|
-
*/
|
|
8
|
-
export { default as Autoformat } from './autoformat';
|
|
9
|
-
import './augmentation';
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
|
6
|
+
* @module autoformat
|
|
7
|
+
*/
|
|
8
|
+
export { default as Autoformat } from './autoformat';
|
|
9
|
+
import './augmentation';
|
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, 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
|
-
/**
|
|
6
|
-
* The inline autoformatting engine. It allows to format various inline patterns. For example,
|
|
7
|
-
* it can be configured to make "foo" bold when typed `**foo**` (the `**` markers will be removed).
|
|
8
|
-
*
|
|
9
|
-
* The autoformatting operation is integrated with the undo manager,
|
|
10
|
-
* so the autoformatting step can be undone if the user's intention was not to format the text.
|
|
11
|
-
*
|
|
12
|
-
* See the {@link module:autoformat/inlineautoformatediting~inlineAutoformatEditing `inlineAutoformatEditing`} documentation
|
|
13
|
-
* to learn how to create custom inline autoformatters. You can also use
|
|
14
|
-
* the {@link module:autoformat/autoformat~Autoformat} feature which enables a set of default autoformatters
|
|
15
|
-
* (lists, headings, bold and italic).
|
|
16
|
-
*
|
|
17
|
-
* @module autoformat/inlineautoformatediting
|
|
18
|
-
*/
|
|
19
|
-
import type { Editor } from 'ckeditor5/src/core';
|
|
20
|
-
import type { Range, Writer } from 'ckeditor5/src/engine';
|
|
21
|
-
import type Autoformat from './autoformat';
|
|
22
|
-
export type TestCallback = (text: string) => {
|
|
23
|
-
remove: Array<Array<number>>;
|
|
24
|
-
format: Array<Array<number>>;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Enables autoformatting mechanism for a given {@link module:core/editor/editor~Editor}.
|
|
28
|
-
*
|
|
29
|
-
* It formats the matched text by applying the given model attribute or by running the provided formatting callback.
|
|
30
|
-
* On every {@link module:engine/model/document~Document#event:change:data data change} in the model document
|
|
31
|
-
* the autoformatting engine checks the text on the left of the selection
|
|
32
|
-
* and executes the provided action if the text matches given criteria (regular expression or callback).
|
|
33
|
-
*
|
|
34
|
-
* @param editor The editor instance.
|
|
35
|
-
* @param plugin The autoformat plugin instance.
|
|
36
|
-
* @param testRegexpOrCallback The regular expression or callback to execute on text.
|
|
37
|
-
* Provided regular expression *must* have three capture groups. The first and the third capture group
|
|
38
|
-
* should match opening and closing delimiters. The second capture group should match the text to format.
|
|
39
|
-
*
|
|
40
|
-
* ```ts
|
|
41
|
-
* // Matches the `**bold text**` pattern.
|
|
42
|
-
* // There are three capturing groups:
|
|
43
|
-
* // - The first to match the starting `**` delimiter.
|
|
44
|
-
* // - The second to match the text to format.
|
|
45
|
-
* // - The third to match the ending `**` delimiter.
|
|
46
|
-
* inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, formatCallback );
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* When a function is provided instead of the regular expression, it will be executed with the text to match as a parameter.
|
|
50
|
-
* The function should return proper "ranges" to delete and format.
|
|
51
|
-
*
|
|
52
|
-
* ```ts
|
|
53
|
-
* {
|
|
54
|
-
* remove: [
|
|
55
|
-
* [ 0, 1 ], // Remove the first letter from the given text.
|
|
56
|
-
* [ 5, 6 ] // Remove the 6th letter from the given text.
|
|
57
|
-
* ],
|
|
58
|
-
* format: [
|
|
59
|
-
* [ 1, 5 ] // Format all letters from 2nd to 5th.
|
|
60
|
-
* ]
|
|
61
|
-
* }
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* @param formatCallback A callback to apply actual formatting.
|
|
65
|
-
* It should return `false` if changes should not be applied (e.g. if a command is disabled).
|
|
66
|
-
*
|
|
67
|
-
* ```ts
|
|
68
|
-
* inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
|
|
69
|
-
* const command = editor.commands.get( 'bold' );
|
|
70
|
-
*
|
|
71
|
-
* if ( !command.isEnabled ) {
|
|
72
|
-
* return false;
|
|
73
|
-
* }
|
|
74
|
-
*
|
|
75
|
-
* const validRanges = editor.model.schema.getValidRanges( rangesToFormat, 'bold' );
|
|
76
|
-
*
|
|
77
|
-
* for ( let range of validRanges ) {
|
|
78
|
-
* writer.setAttribute( 'bold', true, range );
|
|
79
|
-
* }
|
|
80
|
-
* } );
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export default function inlineAutoformatEditing(editor: Editor, plugin: Autoformat, testRegexpOrCallback: RegExp | TestCallback, formatCallback: (writer: Writer, rangesToFormat: Array<Range>) => boolean | undefined): void;
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
|
6
|
+
* The inline autoformatting engine. It allows to format various inline patterns. For example,
|
|
7
|
+
* it can be configured to make "foo" bold when typed `**foo**` (the `**` markers will be removed).
|
|
8
|
+
*
|
|
9
|
+
* The autoformatting operation is integrated with the undo manager,
|
|
10
|
+
* so the autoformatting step can be undone if the user's intention was not to format the text.
|
|
11
|
+
*
|
|
12
|
+
* See the {@link module:autoformat/inlineautoformatediting~inlineAutoformatEditing `inlineAutoformatEditing`} documentation
|
|
13
|
+
* to learn how to create custom inline autoformatters. You can also use
|
|
14
|
+
* the {@link module:autoformat/autoformat~Autoformat} feature which enables a set of default autoformatters
|
|
15
|
+
* (lists, headings, bold and italic).
|
|
16
|
+
*
|
|
17
|
+
* @module autoformat/inlineautoformatediting
|
|
18
|
+
*/
|
|
19
|
+
import type { Editor } from 'ckeditor5/src/core';
|
|
20
|
+
import type { Range, Writer } from 'ckeditor5/src/engine';
|
|
21
|
+
import type Autoformat from './autoformat';
|
|
22
|
+
export type TestCallback = (text: string) => {
|
|
23
|
+
remove: Array<Array<number>>;
|
|
24
|
+
format: Array<Array<number>>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Enables autoformatting mechanism for a given {@link module:core/editor/editor~Editor}.
|
|
28
|
+
*
|
|
29
|
+
* It formats the matched text by applying the given model attribute or by running the provided formatting callback.
|
|
30
|
+
* On every {@link module:engine/model/document~Document#event:change:data data change} in the model document
|
|
31
|
+
* the autoformatting engine checks the text on the left of the selection
|
|
32
|
+
* and executes the provided action if the text matches given criteria (regular expression or callback).
|
|
33
|
+
*
|
|
34
|
+
* @param editor The editor instance.
|
|
35
|
+
* @param plugin The autoformat plugin instance.
|
|
36
|
+
* @param testRegexpOrCallback The regular expression or callback to execute on text.
|
|
37
|
+
* Provided regular expression *must* have three capture groups. The first and the third capture group
|
|
38
|
+
* should match opening and closing delimiters. The second capture group should match the text to format.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* // Matches the `**bold text**` pattern.
|
|
42
|
+
* // There are three capturing groups:
|
|
43
|
+
* // - The first to match the starting `**` delimiter.
|
|
44
|
+
* // - The second to match the text to format.
|
|
45
|
+
* // - The third to match the ending `**` delimiter.
|
|
46
|
+
* inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, formatCallback );
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* When a function is provided instead of the regular expression, it will be executed with the text to match as a parameter.
|
|
50
|
+
* The function should return proper "ranges" to delete and format.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* {
|
|
54
|
+
* remove: [
|
|
55
|
+
* [ 0, 1 ], // Remove the first letter from the given text.
|
|
56
|
+
* [ 5, 6 ] // Remove the 6th letter from the given text.
|
|
57
|
+
* ],
|
|
58
|
+
* format: [
|
|
59
|
+
* [ 1, 5 ] // Format all letters from 2nd to 5th.
|
|
60
|
+
* ]
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @param formatCallback A callback to apply actual formatting.
|
|
65
|
+
* It should return `false` if changes should not be applied (e.g. if a command is disabled).
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
|
|
69
|
+
* const command = editor.commands.get( 'bold' );
|
|
70
|
+
*
|
|
71
|
+
* if ( !command.isEnabled ) {
|
|
72
|
+
* return false;
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* const validRanges = editor.model.schema.getValidRanges( rangesToFormat, 'bold' );
|
|
76
|
+
*
|
|
77
|
+
* for ( let range of validRanges ) {
|
|
78
|
+
* writer.setAttribute( 'bold', true, range );
|
|
79
|
+
* }
|
|
80
|
+
* } );
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export default function inlineAutoformatEditing(editor: Editor, plugin: Autoformat, testRegexpOrCallback: RegExp | TestCallback, formatCallback: (writer: Writer, rangesToFormat: Array<Range>) => boolean | undefined): void;
|