@ckeditor/ckeditor5-autoformat 40.0.0 → 40.1.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/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
package/LICENSE.md
CHANGED
|
@@ -2,7 +2,7 @@ Software License Agreement
|
|
|
2
2
|
==========================
|
|
3
3
|
|
|
4
4
|
**CKEditor 5 autoformat feature** – https://github.com/ckeditor/ckeditor5-autoformat <br>
|
|
5
|
-
Copyright (c) 2003
|
|
5
|
+
Copyright (c) 2003–2023, [CKSource Holding sp. z o.o.](https://cksource.com) All rights reserved.
|
|
6
6
|
|
|
7
7
|
Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html).
|
|
8
8
|
|
|
@@ -14,4 +14,4 @@ Where not otherwise indicated, all CKEditor content is authored by CKSource engi
|
|
|
14
14
|
Trademarks
|
|
15
15
|
----------
|
|
16
16
|
|
|
17
|
-
**CKEditor** is a trademark of [CKSource Holding sp. z o.o.](https://cksource.com) All other brand and product names are trademarks, registered trademarks or service marks of their respective holders.
|
|
17
|
+
**CKEditor** is a trademark of [CKSource Holding sp. z o.o.](https://cksource.com) All other brand and product names are trademarks, registered trademarks, or service marks of their respective holders.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckeditor/ckeditor5-autoformat",
|
|
3
|
-
"version": "40.
|
|
3
|
+
"version": "40.1.0",
|
|
4
4
|
"description": "Autoformatting feature for CKEditor 5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckeditor",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
],
|
|
13
13
|
"main": "src/index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"ckeditor5": "40.
|
|
15
|
+
"ckeditor5": "40.1.0"
|
|
16
16
|
},
|
|
17
17
|
"author": "CKSource (http://cksource.com/)",
|
|
18
18
|
"license": "GPL-2.0-or-later",
|
package/src/augmentation.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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 { Autoformat } from './index';
|
|
6
|
-
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
-
interface PluginsMap {
|
|
8
|
-
[Autoformat.pluginName]: Autoformat;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
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 { Autoformat } from './index';
|
|
6
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
+
interface PluginsMap {
|
|
8
|
+
[Autoformat.pluginName]: Autoformat;
|
|
9
|
+
}
|
|
10
|
+
}
|
package/src/augmentation.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
-
export {};
|
|
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
|
+
export {};
|
package/src/autoformat.d.ts
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
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
|
-
/**
|
|
8
|
-
* Enables a set of predefined autoformatting actions.
|
|
9
|
-
*
|
|
10
|
-
* For a detailed overview, check the {@glink features/autoformat Autoformatting} feature guide
|
|
11
|
-
* and the {@glink api/autoformat package page}.
|
|
12
|
-
*/
|
|
13
|
-
export default class Autoformat extends Plugin {
|
|
14
|
-
/**
|
|
15
|
-
* @inheritDoc
|
|
16
|
-
*/
|
|
17
|
-
static get requires(): readonly [typeof Delete];
|
|
18
|
-
/**
|
|
19
|
-
* @inheritDoc
|
|
20
|
-
*/
|
|
21
|
-
static get pluginName(): "Autoformat";
|
|
22
|
-
/**
|
|
23
|
-
* @inheritDoc
|
|
24
|
-
*/
|
|
25
|
-
afterInit(): void;
|
|
26
|
-
/**
|
|
27
|
-
* Adds autoformatting related to the {@link module:list/list~List}.
|
|
28
|
-
*
|
|
29
|
-
* When typed:
|
|
30
|
-
* - `* ` or `- ` – A paragraph will be changed into a bulleted list.
|
|
31
|
-
* - `1. ` or `1) ` – A paragraph will be changed into a numbered list ("1" can be any digit or a list of digits).
|
|
32
|
-
* - `[] ` or `[ ] ` – A paragraph will be changed into a to-do list.
|
|
33
|
-
* - `[x] ` or `[ x ] ` – A paragraph will be changed into a checked to-do list.
|
|
34
|
-
*/
|
|
35
|
-
private _addListAutoformats;
|
|
36
|
-
/**
|
|
37
|
-
* Adds autoformatting related to the {@link module:basic-styles/bold~Bold},
|
|
38
|
-
* {@link module:basic-styles/italic~Italic}, {@link module:basic-styles/code~Code}
|
|
39
|
-
* and {@link module:basic-styles/strikethrough~Strikethrough}
|
|
40
|
-
*
|
|
41
|
-
* When typed:
|
|
42
|
-
* - `**foobar**` – `**` characters are removed and `foobar` is set to bold,
|
|
43
|
-
* - `__foobar__` – `__` characters are removed and `foobar` is set to bold,
|
|
44
|
-
* - `*foobar*` – `*` characters are removed and `foobar` is set to italic,
|
|
45
|
-
* - `_foobar_` – `_` characters are removed and `foobar` is set to italic,
|
|
46
|
-
* - ``` `foobar` – ``` ` ``` characters are removed and `foobar` is set to code,
|
|
47
|
-
* - `~~foobar~~` – `~~` characters are removed and `foobar` is set to strikethrough.
|
|
48
|
-
*/
|
|
49
|
-
private _addBasicStylesAutoformats;
|
|
50
|
-
/**
|
|
51
|
-
* Adds autoformatting related to {@link module:heading/heading~Heading}.
|
|
52
|
-
*
|
|
53
|
-
* It is using a number at the end of the command name to associate it with the proper trigger:
|
|
54
|
-
*
|
|
55
|
-
* * `heading` with a `heading1` value will be executed when typing `#`,
|
|
56
|
-
* * `heading` with a `heading2` value will be executed when typing `##`,
|
|
57
|
-
* * ... up to `heading6` for `######`.
|
|
58
|
-
*/
|
|
59
|
-
private _addHeadingAutoformats;
|
|
60
|
-
/**
|
|
61
|
-
* Adds autoformatting related to {@link module:block-quote/blockquote~BlockQuote}.
|
|
62
|
-
*
|
|
63
|
-
* When typed:
|
|
64
|
-
* * `> ` – A paragraph will be changed to a block quote.
|
|
65
|
-
*/
|
|
66
|
-
private _addBlockQuoteAutoformats;
|
|
67
|
-
/**
|
|
68
|
-
* Adds autoformatting related to {@link module:code-block/codeblock~CodeBlock}.
|
|
69
|
-
*
|
|
70
|
-
* When typed:
|
|
71
|
-
* - `` ``` `` – A paragraph will be changed to a code block.
|
|
72
|
-
*/
|
|
73
|
-
private _addCodeBlockAutoformats;
|
|
74
|
-
/**
|
|
75
|
-
* Adds autoformatting related to {@link module:horizontal-line/horizontalline~HorizontalLine}.
|
|
76
|
-
*
|
|
77
|
-
* When typed:
|
|
78
|
-
* - `` --- `` – Will be replaced with a horizontal line.
|
|
79
|
-
*/
|
|
80
|
-
private _addHorizontalLineAutoformats;
|
|
81
|
-
}
|
|
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
|
+
/**
|
|
8
|
+
* Enables a set of predefined autoformatting actions.
|
|
9
|
+
*
|
|
10
|
+
* For a detailed overview, check the {@glink features/autoformat Autoformatting} feature guide
|
|
11
|
+
* and the {@glink api/autoformat package page}.
|
|
12
|
+
*/
|
|
13
|
+
export default class Autoformat extends Plugin {
|
|
14
|
+
/**
|
|
15
|
+
* @inheritDoc
|
|
16
|
+
*/
|
|
17
|
+
static get requires(): readonly [typeof Delete];
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
static get pluginName(): "Autoformat";
|
|
22
|
+
/**
|
|
23
|
+
* @inheritDoc
|
|
24
|
+
*/
|
|
25
|
+
afterInit(): void;
|
|
26
|
+
/**
|
|
27
|
+
* Adds autoformatting related to the {@link module:list/list~List}.
|
|
28
|
+
*
|
|
29
|
+
* When typed:
|
|
30
|
+
* - `* ` or `- ` – A paragraph will be changed into a bulleted list.
|
|
31
|
+
* - `1. ` or `1) ` – A paragraph will be changed into a numbered list ("1" can be any digit or a list of digits).
|
|
32
|
+
* - `[] ` or `[ ] ` – A paragraph will be changed into a to-do list.
|
|
33
|
+
* - `[x] ` or `[ x ] ` – A paragraph will be changed into a checked to-do list.
|
|
34
|
+
*/
|
|
35
|
+
private _addListAutoformats;
|
|
36
|
+
/**
|
|
37
|
+
* Adds autoformatting related to the {@link module:basic-styles/bold~Bold},
|
|
38
|
+
* {@link module:basic-styles/italic~Italic}, {@link module:basic-styles/code~Code}
|
|
39
|
+
* and {@link module:basic-styles/strikethrough~Strikethrough}
|
|
40
|
+
*
|
|
41
|
+
* When typed:
|
|
42
|
+
* - `**foobar**` – `**` characters are removed and `foobar` is set to bold,
|
|
43
|
+
* - `__foobar__` – `__` characters are removed and `foobar` is set to bold,
|
|
44
|
+
* - `*foobar*` – `*` characters are removed and `foobar` is set to italic,
|
|
45
|
+
* - `_foobar_` – `_` characters are removed and `foobar` is set to italic,
|
|
46
|
+
* - ``` `foobar` – ``` ` ``` characters are removed and `foobar` is set to code,
|
|
47
|
+
* - `~~foobar~~` – `~~` characters are removed and `foobar` is set to strikethrough.
|
|
48
|
+
*/
|
|
49
|
+
private _addBasicStylesAutoformats;
|
|
50
|
+
/**
|
|
51
|
+
* Adds autoformatting related to {@link module:heading/heading~Heading}.
|
|
52
|
+
*
|
|
53
|
+
* It is using a number at the end of the command name to associate it with the proper trigger:
|
|
54
|
+
*
|
|
55
|
+
* * `heading` with a `heading1` value will be executed when typing `#`,
|
|
56
|
+
* * `heading` with a `heading2` value will be executed when typing `##`,
|
|
57
|
+
* * ... up to `heading6` for `######`.
|
|
58
|
+
*/
|
|
59
|
+
private _addHeadingAutoformats;
|
|
60
|
+
/**
|
|
61
|
+
* Adds autoformatting related to {@link module:block-quote/blockquote~BlockQuote}.
|
|
62
|
+
*
|
|
63
|
+
* When typed:
|
|
64
|
+
* * `> ` – A paragraph will be changed to a block quote.
|
|
65
|
+
*/
|
|
66
|
+
private _addBlockQuoteAutoformats;
|
|
67
|
+
/**
|
|
68
|
+
* Adds autoformatting related to {@link module:code-block/codeblock~CodeBlock}.
|
|
69
|
+
*
|
|
70
|
+
* When typed:
|
|
71
|
+
* - `` ``` `` – A paragraph will be changed to a code block.
|
|
72
|
+
*/
|
|
73
|
+
private _addCodeBlockAutoformats;
|
|
74
|
+
/**
|
|
75
|
+
* Adds autoformatting related to {@link module:horizontal-line/horizontalline~HorizontalLine}.
|
|
76
|
+
*
|
|
77
|
+
* When typed:
|
|
78
|
+
* - `` --- `` – Will be replaced with a horizontal line.
|
|
79
|
+
*/
|
|
80
|
+
private _addHorizontalLineAutoformats;
|
|
81
|
+
}
|
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
|
+
}
|