@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/src/autoformat.js CHANGED
@@ -1,189 +1,189 @@
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 { Plugin } from 'ckeditor5/src/core';
6
- import { Delete } from 'ckeditor5/src/typing';
7
- import blockAutoformatEditing from './blockautoformatediting';
8
- import inlineAutoformatEditing from './inlineautoformatediting';
9
- /**
10
- * Enables a set of predefined autoformatting actions.
11
- *
12
- * For a detailed overview, check the {@glink features/autoformat Autoformatting} feature guide
13
- * and the {@glink api/autoformat package page}.
14
- */
15
- export default class Autoformat extends Plugin {
16
- /**
17
- * @inheritDoc
18
- */
19
- static get requires() {
20
- return [Delete];
21
- }
22
- /**
23
- * @inheritDoc
24
- */
25
- static get pluginName() {
26
- return 'Autoformat';
27
- }
28
- /**
29
- * @inheritDoc
30
- */
31
- afterInit() {
32
- this._addListAutoformats();
33
- this._addBasicStylesAutoformats();
34
- this._addHeadingAutoformats();
35
- this._addBlockQuoteAutoformats();
36
- this._addCodeBlockAutoformats();
37
- this._addHorizontalLineAutoformats();
38
- }
39
- /**
40
- * Adds autoformatting related to the {@link module:list/list~List}.
41
- *
42
- * When typed:
43
- * - `* ` or `- ` – A paragraph will be changed into a bulleted list.
44
- * - `1. ` or `1) ` – A paragraph will be changed into a numbered list ("1" can be any digit or a list of digits).
45
- * - `[] ` or `[ ] ` – A paragraph will be changed into a to-do list.
46
- * - `[x] ` or `[ x ] ` – A paragraph will be changed into a checked to-do list.
47
- */
48
- _addListAutoformats() {
49
- const commands = this.editor.commands;
50
- if (commands.get('bulletedList')) {
51
- blockAutoformatEditing(this.editor, this, /^[*-]\s$/, 'bulletedList');
52
- }
53
- if (commands.get('numberedList')) {
54
- blockAutoformatEditing(this.editor, this, /^1[.|)]\s$/, 'numberedList');
55
- }
56
- if (commands.get('todoList')) {
57
- blockAutoformatEditing(this.editor, this, /^\[\s?\]\s$/, 'todoList');
58
- }
59
- if (commands.get('checkTodoList')) {
60
- blockAutoformatEditing(this.editor, this, /^\[\s?x\s?\]\s$/, () => {
61
- this.editor.execute('todoList');
62
- this.editor.execute('checkTodoList');
63
- });
64
- }
65
- }
66
- /**
67
- * Adds autoformatting related to the {@link module:basic-styles/bold~Bold},
68
- * {@link module:basic-styles/italic~Italic}, {@link module:basic-styles/code~Code}
69
- * and {@link module:basic-styles/strikethrough~Strikethrough}
70
- *
71
- * When typed:
72
- * - `**foobar**` – `**` characters are removed and `foobar` is set to bold,
73
- * - `__foobar__` – `__` characters are removed and `foobar` is set to bold,
74
- * - `*foobar*` – `*` characters are removed and `foobar` is set to italic,
75
- * - `_foobar_` – `_` characters are removed and `foobar` is set to italic,
76
- * - ``` `foobar` – ``` ` ``` characters are removed and `foobar` is set to code,
77
- * - `~~foobar~~` – `~~` characters are removed and `foobar` is set to strikethrough.
78
- */
79
- _addBasicStylesAutoformats() {
80
- const commands = this.editor.commands;
81
- if (commands.get('bold')) {
82
- const boldCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'bold');
83
- inlineAutoformatEditing(this.editor, this, /(?:^|\s)(\*\*)([^*]+)(\*\*)$/g, boldCallback);
84
- inlineAutoformatEditing(this.editor, this, /(?:^|\s)(__)([^_]+)(__)$/g, boldCallback);
85
- }
86
- if (commands.get('italic')) {
87
- const italicCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'italic');
88
- // The italic autoformatter cannot be triggered by the bold markers, so we need to check the
89
- // text before the pattern (e.g. `(?:^|[^\*])`).
90
- inlineAutoformatEditing(this.editor, this, /(?:^|\s)(\*)([^*_]+)(\*)$/g, italicCallback);
91
- inlineAutoformatEditing(this.editor, this, /(?:^|\s)(_)([^_]+)(_)$/g, italicCallback);
92
- }
93
- if (commands.get('code')) {
94
- const codeCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'code');
95
- inlineAutoformatEditing(this.editor, this, /(`)([^`]+)(`)$/g, codeCallback);
96
- }
97
- if (commands.get('strikethrough')) {
98
- const strikethroughCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'strikethrough');
99
- inlineAutoformatEditing(this.editor, this, /(~~)([^~]+)(~~)$/g, strikethroughCallback);
100
- }
101
- }
102
- /**
103
- * Adds autoformatting related to {@link module:heading/heading~Heading}.
104
- *
105
- * It is using a number at the end of the command name to associate it with the proper trigger:
106
- *
107
- * * `heading` with a `heading1` value will be executed when typing `#`,
108
- * * `heading` with a `heading2` value will be executed when typing `##`,
109
- * * ... up to `heading6` for `######`.
110
- */
111
- _addHeadingAutoformats() {
112
- const command = this.editor.commands.get('heading');
113
- if (command) {
114
- command.modelElements
115
- .filter(name => name.match(/^heading[1-6]$/))
116
- .forEach(modelName => {
117
- const level = modelName[7];
118
- const pattern = new RegExp(`^(#{${level}})\\s$`);
119
- blockAutoformatEditing(this.editor, this, pattern, () => {
120
- // Should only be active if command is enabled and heading style associated with pattern is inactive.
121
- if (!command.isEnabled || command.value === modelName) {
122
- return false;
123
- }
124
- this.editor.execute('heading', { value: modelName });
125
- });
126
- });
127
- }
128
- }
129
- /**
130
- * Adds autoformatting related to {@link module:block-quote/blockquote~BlockQuote}.
131
- *
132
- * When typed:
133
- * * `> ` – A paragraph will be changed to a block quote.
134
- */
135
- _addBlockQuoteAutoformats() {
136
- if (this.editor.commands.get('blockQuote')) {
137
- blockAutoformatEditing(this.editor, this, /^>\s$/, 'blockQuote');
138
- }
139
- }
140
- /**
141
- * Adds autoformatting related to {@link module:code-block/codeblock~CodeBlock}.
142
- *
143
- * When typed:
144
- * - `` ``` `` – A paragraph will be changed to a code block.
145
- */
146
- _addCodeBlockAutoformats() {
147
- const editor = this.editor;
148
- const selection = editor.model.document.selection;
149
- if (editor.commands.get('codeBlock')) {
150
- blockAutoformatEditing(editor, this, /^```$/, () => {
151
- if (selection.getFirstPosition().parent.is('element', 'listItem')) {
152
- return false;
153
- }
154
- this.editor.execute('codeBlock', {
155
- usePreviousLanguageChoice: true
156
- });
157
- });
158
- }
159
- }
160
- /**
161
- * Adds autoformatting related to {@link module:horizontal-line/horizontalline~HorizontalLine}.
162
- *
163
- * When typed:
164
- * - `` --- `` – Will be replaced with a horizontal line.
165
- */
166
- _addHorizontalLineAutoformats() {
167
- if (this.editor.commands.get('horizontalLine')) {
168
- blockAutoformatEditing(this.editor, this, /^---$/, 'horizontalLine');
169
- }
170
- }
171
- }
172
- /**
173
- * Helper function for getting `inlineAutoformatEditing` callbacks that checks if command is enabled.
174
- */
175
- function getCallbackFunctionForInlineAutoformat(editor, attributeKey) {
176
- return (writer, rangesToFormat) => {
177
- const command = editor.commands.get(attributeKey);
178
- if (!command.isEnabled) {
179
- return false;
180
- }
181
- const validRanges = editor.model.schema.getValidRanges(rangesToFormat, attributeKey);
182
- for (const range of validRanges) {
183
- writer.setAttribute(attributeKey, true, range);
184
- }
185
- // After applying attribute to the text, remove given attribute from the selection.
186
- // This way user is able to type a text without attribute used by auto formatter.
187
- writer.removeSelectionAttribute(attributeKey);
188
- };
189
- }
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 { Plugin } from 'ckeditor5/src/core';
6
+ import { Delete } from 'ckeditor5/src/typing';
7
+ import blockAutoformatEditing from './blockautoformatediting';
8
+ import inlineAutoformatEditing from './inlineautoformatediting';
9
+ /**
10
+ * Enables a set of predefined autoformatting actions.
11
+ *
12
+ * For a detailed overview, check the {@glink features/autoformat Autoformatting} feature guide
13
+ * and the {@glink api/autoformat package page}.
14
+ */
15
+ export default class Autoformat extends Plugin {
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get requires() {
20
+ return [Delete];
21
+ }
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ static get pluginName() {
26
+ return 'Autoformat';
27
+ }
28
+ /**
29
+ * @inheritDoc
30
+ */
31
+ afterInit() {
32
+ this._addListAutoformats();
33
+ this._addBasicStylesAutoformats();
34
+ this._addHeadingAutoformats();
35
+ this._addBlockQuoteAutoformats();
36
+ this._addCodeBlockAutoformats();
37
+ this._addHorizontalLineAutoformats();
38
+ }
39
+ /**
40
+ * Adds autoformatting related to the {@link module:list/list~List}.
41
+ *
42
+ * When typed:
43
+ * - `* ` or `- ` – A paragraph will be changed into a bulleted list.
44
+ * - `1. ` or `1) ` – A paragraph will be changed into a numbered list ("1" can be any digit or a list of digits).
45
+ * - `[] ` or `[ ] ` – A paragraph will be changed into a to-do list.
46
+ * - `[x] ` or `[ x ] ` – A paragraph will be changed into a checked to-do list.
47
+ */
48
+ _addListAutoformats() {
49
+ const commands = this.editor.commands;
50
+ if (commands.get('bulletedList')) {
51
+ blockAutoformatEditing(this.editor, this, /^[*-]\s$/, 'bulletedList');
52
+ }
53
+ if (commands.get('numberedList')) {
54
+ blockAutoformatEditing(this.editor, this, /^1[.|)]\s$/, 'numberedList');
55
+ }
56
+ if (commands.get('todoList')) {
57
+ blockAutoformatEditing(this.editor, this, /^\[\s?\]\s$/, 'todoList');
58
+ }
59
+ if (commands.get('checkTodoList')) {
60
+ blockAutoformatEditing(this.editor, this, /^\[\s?x\s?\]\s$/, () => {
61
+ this.editor.execute('todoList');
62
+ this.editor.execute('checkTodoList');
63
+ });
64
+ }
65
+ }
66
+ /**
67
+ * Adds autoformatting related to the {@link module:basic-styles/bold~Bold},
68
+ * {@link module:basic-styles/italic~Italic}, {@link module:basic-styles/code~Code}
69
+ * and {@link module:basic-styles/strikethrough~Strikethrough}
70
+ *
71
+ * When typed:
72
+ * - `**foobar**` – `**` characters are removed and `foobar` is set to bold,
73
+ * - `__foobar__` – `__` characters are removed and `foobar` is set to bold,
74
+ * - `*foobar*` – `*` characters are removed and `foobar` is set to italic,
75
+ * - `_foobar_` – `_` characters are removed and `foobar` is set to italic,
76
+ * - ``` `foobar` – ``` ` ``` characters are removed and `foobar` is set to code,
77
+ * - `~~foobar~~` – `~~` characters are removed and `foobar` is set to strikethrough.
78
+ */
79
+ _addBasicStylesAutoformats() {
80
+ const commands = this.editor.commands;
81
+ if (commands.get('bold')) {
82
+ const boldCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'bold');
83
+ inlineAutoformatEditing(this.editor, this, /(?:^|\s)(\*\*)([^*]+)(\*\*)$/g, boldCallback);
84
+ inlineAutoformatEditing(this.editor, this, /(?:^|\s)(__)([^_]+)(__)$/g, boldCallback);
85
+ }
86
+ if (commands.get('italic')) {
87
+ const italicCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'italic');
88
+ // The italic autoformatter cannot be triggered by the bold markers, so we need to check the
89
+ // text before the pattern (e.g. `(?:^|[^\*])`).
90
+ inlineAutoformatEditing(this.editor, this, /(?:^|\s)(\*)([^*_]+)(\*)$/g, italicCallback);
91
+ inlineAutoformatEditing(this.editor, this, /(?:^|\s)(_)([^_]+)(_)$/g, italicCallback);
92
+ }
93
+ if (commands.get('code')) {
94
+ const codeCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'code');
95
+ inlineAutoformatEditing(this.editor, this, /(`)([^`]+)(`)$/g, codeCallback);
96
+ }
97
+ if (commands.get('strikethrough')) {
98
+ const strikethroughCallback = getCallbackFunctionForInlineAutoformat(this.editor, 'strikethrough');
99
+ inlineAutoformatEditing(this.editor, this, /(~~)([^~]+)(~~)$/g, strikethroughCallback);
100
+ }
101
+ }
102
+ /**
103
+ * Adds autoformatting related to {@link module:heading/heading~Heading}.
104
+ *
105
+ * It is using a number at the end of the command name to associate it with the proper trigger:
106
+ *
107
+ * * `heading` with a `heading1` value will be executed when typing `#`,
108
+ * * `heading` with a `heading2` value will be executed when typing `##`,
109
+ * * ... up to `heading6` for `######`.
110
+ */
111
+ _addHeadingAutoformats() {
112
+ const command = this.editor.commands.get('heading');
113
+ if (command) {
114
+ command.modelElements
115
+ .filter(name => name.match(/^heading[1-6]$/))
116
+ .forEach(modelName => {
117
+ const level = modelName[7];
118
+ const pattern = new RegExp(`^(#{${level}})\\s$`);
119
+ blockAutoformatEditing(this.editor, this, pattern, () => {
120
+ // Should only be active if command is enabled and heading style associated with pattern is inactive.
121
+ if (!command.isEnabled || command.value === modelName) {
122
+ return false;
123
+ }
124
+ this.editor.execute('heading', { value: modelName });
125
+ });
126
+ });
127
+ }
128
+ }
129
+ /**
130
+ * Adds autoformatting related to {@link module:block-quote/blockquote~BlockQuote}.
131
+ *
132
+ * When typed:
133
+ * * `> ` – A paragraph will be changed to a block quote.
134
+ */
135
+ _addBlockQuoteAutoformats() {
136
+ if (this.editor.commands.get('blockQuote')) {
137
+ blockAutoformatEditing(this.editor, this, /^>\s$/, 'blockQuote');
138
+ }
139
+ }
140
+ /**
141
+ * Adds autoformatting related to {@link module:code-block/codeblock~CodeBlock}.
142
+ *
143
+ * When typed:
144
+ * - `` ``` `` – A paragraph will be changed to a code block.
145
+ */
146
+ _addCodeBlockAutoformats() {
147
+ const editor = this.editor;
148
+ const selection = editor.model.document.selection;
149
+ if (editor.commands.get('codeBlock')) {
150
+ blockAutoformatEditing(editor, this, /^```$/, () => {
151
+ if (selection.getFirstPosition().parent.is('element', 'listItem')) {
152
+ return false;
153
+ }
154
+ this.editor.execute('codeBlock', {
155
+ usePreviousLanguageChoice: true
156
+ });
157
+ });
158
+ }
159
+ }
160
+ /**
161
+ * Adds autoformatting related to {@link module:horizontal-line/horizontalline~HorizontalLine}.
162
+ *
163
+ * When typed:
164
+ * - `` --- `` – Will be replaced with a horizontal line.
165
+ */
166
+ _addHorizontalLineAutoformats() {
167
+ if (this.editor.commands.get('horizontalLine')) {
168
+ blockAutoformatEditing(this.editor, this, /^---$/, 'horizontalLine');
169
+ }
170
+ }
171
+ }
172
+ /**
173
+ * Helper function for getting `inlineAutoformatEditing` callbacks that checks if command is enabled.
174
+ */
175
+ function getCallbackFunctionForInlineAutoformat(editor, attributeKey) {
176
+ return (writer, rangesToFormat) => {
177
+ const command = editor.commands.get(attributeKey);
178
+ if (!command.isEnabled) {
179
+ return false;
180
+ }
181
+ const validRanges = editor.model.schema.getValidRanges(rangesToFormat, attributeKey);
182
+ for (const range of validRanges) {
183
+ writer.setAttribute(attributeKey, true, range);
184
+ }
185
+ // After applying attribute to the text, remove given attribute from the selection.
186
+ // This way user is able to type a text without attribute used by auto formatter.
187
+ writer.removeSelectionAttribute(attributeKey);
188
+ };
189
+ }
@@ -1,57 +1,57 @@
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 type { Editor } from 'ckeditor5/src/core';
6
- import type Autoformat from './autoformat';
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: Editor, plugin: Autoformat, pattern: RegExp, callbackOrCommand: string | ((context: {
56
- match: RegExpExecArray;
57
- }) => unknown)): 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
+ import type { Editor } from 'ckeditor5/src/core';
6
+ import type Autoformat from './autoformat';
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: Editor, plugin: Autoformat, pattern: RegExp, callbackOrCommand: string | ((context: {
56
+ match: RegExpExecArray;
57
+ }) => unknown)): void;