@ckeditor/ckeditor5-autoformat 35.4.0 → 36.0.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 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-2022, [CKSource Holding sp. z o.o.](https://cksource.com) All rights reserved.
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
 
@@ -1,4 +1,4 @@
1
1
  /*!
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md.
4
4
  */(()=>{var t={704:(t,e,o)=>{t.exports=o(79)("./src/core.js")},492:(t,e,o)=>{t.exports=o(79)("./src/engine.js")},181:(t,e,o)=>{t.exports=o(79)("./src/typing.js")},209:(t,e,o)=>{t.exports=o(79)("./src/utils.js")},79:t=>{"use strict";t.exports=CKEditor5.dll}},e={};function o(i){var s=e[i];if(void 0!==s)return s.exports;var n=e[i]={exports:{}};return t[i](n,n.exports,o),n.exports}o.d=(t,e)=>{for(var i in e)o.o(e,i)&&!o.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};(()=>{"use strict";o.r(i),o.d(i,{Autoformat:()=>c});var t=o(704),e=o(181),s=o(492),n=o(209);function r(t,e,o,i){let r,d=null;"function"==typeof i?r=i:(d=t.commands.get(i),r=()=>{t.execute(i)}),t.model.document.on("change:data",((a,c)=>{if(d&&!d.isEnabled||!e.isEnabled)return;const l=(0,n.first)(t.model.document.selection.getRanges());if(!l.isCollapsed)return;if(c.isUndo||!c.isLocal)return;const u=Array.from(t.model.document.differ.getChanges()),h=u[0];if(1!=u.length||"insert"!==h.type||"$text"!=h.name||1!=h.length)return;const g=h.position.parent;if(g.is("element","codeBlock"))return;if(g.is("element","listItem")&&"function"!=typeof i&&!["numberedList","bulletedList","todoList"].includes(i))return;if(d&&!0===d.value)return;const m=g.getChild(0),f=t.model.createRangeOn(m);if(!f.containsRange(l)&&!l.end.isEqual(f.end))return;const p=o.exec(m.data.substr(0,l.end.offset));p&&t.model.enqueueChange((e=>{const o=e.createPositionAt(g,0),i=e.createPositionAt(g,p[0].length),n=new s.LiveRange(o,i);if(!1!==r({match:p})){e.remove(n);const o=t.model.document.selection.getFirstRange(),i=e.createRangeIn(g);!g.isEmpty||i.isEqual(o)||i.containsRange(o,!0)||e.remove(g)}n.detach(),t.model.enqueueChange((()=>{t.plugins.get("Delete").requestUndoOnBackspace()}))}))}))}function d(t,e,o,i){let s,n;o instanceof RegExp?s=o:n=o,n=n||(t=>{let e;const o=[],i=[];for(;null!==(e=s.exec(t))&&!(e&&e.length<4);){let{index:t,1:s,2:n,3:r}=e;const d=s+n+r;t+=e[0].length-d.length;const a=[t,t+s.length],c=[t+s.length+n.length,t+s.length+n.length+r.length];o.push(a),o.push(c),i.push([t+s.length,t+s.length+n.length])}return{remove:o,format:i}}),t.model.document.on("change:data",((o,s)=>{if(s.isUndo||!s.isLocal||!e.isEnabled)return;const r=t.model,d=r.document.selection;if(!d.isCollapsed)return;const c=Array.from(r.document.differ.getChanges()),l=c[0];if(1!=c.length||"insert"!==l.type||"$text"!=l.name||1!=l.length)return;const u=d.focus,h=u.parent,{text:g,range:m}=function(t,e){let o=t.start;const i=Array.from(t.getItems()).reduce(((t,i)=>!i.is("$text")&&!i.is("$textProxy")||i.getAttribute("code")?(o=e.createPositionAfter(i),""):t+i.data),"");return{text:i,range:e.createRange(o,t.end)}}(r.createRange(r.createPositionAt(h,0),u),r),f=n(g),p=a(m.start,f.format,r),x=a(m.start,f.remove,r);p.length&&x.length&&r.enqueueChange((e=>{if(!1!==i(e,p)){for(const t of x.reverse())e.remove(t);r.enqueueChange((()=>{t.plugins.get("Delete").requestUndoOnBackspace()}))}}))}))}function a(t,e,o){return e.filter((t=>void 0!==t[0]&&void 0!==t[1])).map((e=>o.createRange(t.getShiftedBy(e[0]),t.getShiftedBy(e[1]))))}class c extends t.Plugin{static get requires(){return[e.Delete]}static get pluginName(){return"Autoformat"}afterInit(){this._addListAutoformats(),this._addBasicStylesAutoformats(),this._addHeadingAutoformats(),this._addBlockQuoteAutoformats(),this._addCodeBlockAutoformats(),this._addHorizontalLineAutoformats()}_addListAutoformats(){const t=this.editor.commands;t.get("bulletedList")&&r(this.editor,this,/^[*-]\s$/,"bulletedList"),t.get("numberedList")&&r(this.editor,this,/^1[.|)]\s$/,"numberedList"),t.get("todoList")&&r(this.editor,this,/^\[\s?\]\s$/,"todoList"),t.get("checkTodoList")&&r(this.editor,this,/^\[\s?x\s?\]\s$/,(()=>{this.editor.execute("todoList"),this.editor.execute("checkTodoList")}))}_addBasicStylesAutoformats(){const t=this.editor.commands;if(t.get("bold")){const t=l(this.editor,"bold");d(this.editor,this,/(?:^|\s)(\*\*)([^*]+)(\*\*)$/g,t),d(this.editor,this,/(?:^|\s)(__)([^_]+)(__)$/g,t)}if(t.get("italic")){const t=l(this.editor,"italic");d(this.editor,this,/(?:^|\s)(\*)([^*_]+)(\*)$/g,t),d(this.editor,this,/(?:^|\s)(_)([^_]+)(_)$/g,t)}if(t.get("code")){const t=l(this.editor,"code");d(this.editor,this,/(`)([^`]+)(`)$/g,t)}if(t.get("strikethrough")){const t=l(this.editor,"strikethrough");d(this.editor,this,/(~~)([^~]+)(~~)$/g,t)}}_addHeadingAutoformats(){const t=this.editor.commands.get("heading");t&&t.modelElements.filter((t=>t.match(/^heading[1-6]$/))).forEach((e=>{const o=e[7],i=new RegExp(`^(#{${o}})\\s$`);r(this.editor,this,i,(()=>{if(!t.isEnabled||t.value===e)return!1;this.editor.execute("heading",{value:e})}))}))}_addBlockQuoteAutoformats(){this.editor.commands.get("blockQuote")&&r(this.editor,this,/^>\s$/,"blockQuote")}_addCodeBlockAutoformats(){const t=this.editor,e=t.model.document.selection;t.commands.get("codeBlock")&&r(t,this,/^```$/,(()=>{if(e.getFirstPosition().parent.is("element","listItem"))return!1;this.editor.execute("codeBlock",{usePreviousLanguageChoice:!0})}))}_addHorizontalLineAutoformats(){this.editor.commands.get("horizontalLine")&&r(this.editor,this,/^---$/,"horizontalLine")}}function l(t,e){return(o,i)=>{if(!t.commands.get(e).isEnabled)return!1;const s=t.model.schema.getValidRanges(i,e);for(const t of s)o.setAttribute(e,!0,t);o.removeSelectionAttribute(e)}}})(),(window.CKEditor5=window.CKEditor5||{}).autoformat=i})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-autoformat",
3
- "version": "35.4.0",
3
+ "version": "36.0.0",
4
4
  "description": "Autoformatting feature for CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -12,24 +12,25 @@
12
12
  ],
13
13
  "main": "src/index.js",
14
14
  "dependencies": {
15
- "ckeditor5": "^35.4.0"
15
+ "ckeditor5": "^36.0.0"
16
16
  },
17
17
  "devDependencies": {
18
- "@ckeditor/ckeditor5-basic-styles": "^35.4.0",
19
- "@ckeditor/ckeditor5-block-quote": "^35.4.0",
20
- "@ckeditor/ckeditor5-code-block": "^35.4.0",
21
- "@ckeditor/ckeditor5-core": "^35.4.0",
22
- "@ckeditor/ckeditor5-dev-utils": "^31.0.0",
23
- "@ckeditor/ckeditor5-editor-classic": "^35.4.0",
24
- "@ckeditor/ckeditor5-engine": "^35.4.0",
25
- "@ckeditor/ckeditor5-enter": "^35.4.0",
26
- "@ckeditor/ckeditor5-heading": "^35.4.0",
27
- "@ckeditor/ckeditor5-horizontal-line": "^35.4.0",
28
- "@ckeditor/ckeditor5-list": "^35.4.0",
29
- "@ckeditor/ckeditor5-paragraph": "^35.4.0",
30
- "@ckeditor/ckeditor5-theme-lark": "^35.4.0",
31
- "@ckeditor/ckeditor5-typing": "^35.4.0",
32
- "@ckeditor/ckeditor5-undo": "^35.4.0",
18
+ "@ckeditor/ckeditor5-basic-styles": "^36.0.0",
19
+ "@ckeditor/ckeditor5-block-quote": "^36.0.0",
20
+ "@ckeditor/ckeditor5-code-block": "^36.0.0",
21
+ "@ckeditor/ckeditor5-core": "^36.0.0",
22
+ "@ckeditor/ckeditor5-dev-utils": "^32.0.0",
23
+ "@ckeditor/ckeditor5-editor-classic": "^36.0.0",
24
+ "@ckeditor/ckeditor5-engine": "^36.0.0",
25
+ "@ckeditor/ckeditor5-enter": "^36.0.0",
26
+ "@ckeditor/ckeditor5-heading": "^36.0.0",
27
+ "@ckeditor/ckeditor5-horizontal-line": "^36.0.0",
28
+ "@ckeditor/ckeditor5-list": "^36.0.0",
29
+ "@ckeditor/ckeditor5-paragraph": "^36.0.0",
30
+ "@ckeditor/ckeditor5-theme-lark": "^36.0.0",
31
+ "@ckeditor/ckeditor5-typing": "^36.0.0",
32
+ "@ckeditor/ckeditor5-undo": "^36.0.0",
33
+ "typescript": "^4.8.4",
33
34
  "webpack": "^5.58.1",
34
35
  "webpack-cli": "^4.9.0"
35
36
  },
@@ -48,7 +49,8 @@
48
49
  },
49
50
  "files": [
50
51
  "lang",
51
- "src",
52
+ "src/**/*.js",
53
+ "src/**/*.d.ts",
52
54
  "theme",
53
55
  "build",
54
56
  "ckeditor5-metadata.json",
@@ -58,6 +60,8 @@
58
60
  "eslint-plugin-ckeditor5-rules"
59
61
  ],
60
62
  "scripts": {
61
- "dll:build": "webpack"
63
+ "dll:build": "webpack",
64
+ "build": "tsc -p ./tsconfig.release.json",
65
+ "postversion": "npm run build"
62
66
  }
63
67
  }
package/src/autoformat.js CHANGED
@@ -1,241 +1,189 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module autoformat/autoformat
8
- */
9
-
10
5
  import { Plugin } from 'ckeditor5/src/core';
11
6
  import { Delete } from 'ckeditor5/src/typing';
12
-
13
7
  import blockAutoformatEditing from './blockautoformatediting';
14
8
  import inlineAutoformatEditing from './inlineautoformatediting';
15
-
16
9
  /**
17
10
  * Enables a set of predefined autoformatting actions.
18
11
  *
19
12
  * For a detailed overview, check the {@glink features/autoformat Autoformatting feature documentation}
20
13
  * and the {@glink api/autoformat package page}.
21
- *
22
- * @extends module:core/plugin~Plugin
23
14
  */
24
15
  export default class Autoformat extends Plugin {
25
- /**
26
- * @inheritdoc
27
- */
28
- static get requires() {
29
- return [ Delete ];
30
- }
31
-
32
- /**
33
- * @inheritDoc
34
- */
35
- static get pluginName() {
36
- return 'Autoformat';
37
- }
38
-
39
- /**
40
- * @inheritDoc
41
- */
42
- afterInit() {
43
- this._addListAutoformats();
44
- this._addBasicStylesAutoformats();
45
- this._addHeadingAutoformats();
46
- this._addBlockQuoteAutoformats();
47
- this._addCodeBlockAutoformats();
48
- this._addHorizontalLineAutoformats();
49
- }
50
-
51
- /**
52
- * Adds autoformatting related to the {@link module:list/list~List}.
53
- *
54
- * When typed:
55
- * - `* ` or `- ` &ndash; A paragraph will be changed to a bulleted list.
56
- * - `1. ` or `1) ` &ndash; A paragraph will be changed to a numbered list ("1" can be any digit or a list of digits).
57
- * - `[] ` or `[ ] ` &ndash; A paragraph will be changed to a to-do list.
58
- * - `[x] ` or `[ x ] ` &ndash; A paragraph will be changed to a checked to-do list.
59
- *
60
- * @private
61
- */
62
- _addListAutoformats() {
63
- const commands = this.editor.commands;
64
-
65
- if ( commands.get( 'bulletedList' ) ) {
66
- blockAutoformatEditing( this.editor, this, /^[*-]\s$/, 'bulletedList' );
67
- }
68
-
69
- if ( commands.get( 'numberedList' ) ) {
70
- blockAutoformatEditing( this.editor, this, /^1[.|)]\s$/, 'numberedList' );
71
- }
72
-
73
- if ( commands.get( 'todoList' ) ) {
74
- blockAutoformatEditing( this.editor, this, /^\[\s?\]\s$/, 'todoList' );
75
- }
76
-
77
- if ( commands.get( 'checkTodoList' ) ) {
78
- blockAutoformatEditing( this.editor, this, /^\[\s?x\s?\]\s$/, () => {
79
- this.editor.execute( 'todoList' );
80
- this.editor.execute( 'checkTodoList' );
81
- } );
82
- }
83
- }
84
-
85
- /**
86
- * Adds autoformatting related to the {@link module:basic-styles/bold~Bold},
87
- * {@link module:basic-styles/italic~Italic}, {@link module:basic-styles/code~Code}
88
- * and {@link module:basic-styles/strikethrough~Strikethrough}
89
- *
90
- * When typed:
91
- * - `**foobar**` &ndash; `**` characters are removed and `foobar` is set to bold,
92
- * - `__foobar__` &ndash; `__` characters are removed and `foobar` is set to bold,
93
- * - `*foobar*` &ndash; `*` characters are removed and `foobar` is set to italic,
94
- * - `_foobar_` &ndash; `_` characters are removed and `foobar` is set to italic,
95
- * - ``` `foobar` &ndash; ``` ` ``` characters are removed and `foobar` is set to code,
96
- * - `~~foobar~~` &ndash; `~~` characters are removed and `foobar` is set to strikethrough.
97
- *
98
- * @private
99
- */
100
- _addBasicStylesAutoformats() {
101
- const commands = this.editor.commands;
102
-
103
- if ( commands.get( 'bold' ) ) {
104
- const boldCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'bold' );
105
-
106
- inlineAutoformatEditing( this.editor, this, /(?:^|\s)(\*\*)([^*]+)(\*\*)$/g, boldCallback );
107
- inlineAutoformatEditing( this.editor, this, /(?:^|\s)(__)([^_]+)(__)$/g, boldCallback );
108
- }
109
-
110
- if ( commands.get( 'italic' ) ) {
111
- const italicCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'italic' );
112
-
113
- // The italic autoformatter cannot be triggered by the bold markers, so we need to check the
114
- // text before the pattern (e.g. `(?:^|[^\*])`).
115
- inlineAutoformatEditing( this.editor, this, /(?:^|\s)(\*)([^*_]+)(\*)$/g, italicCallback );
116
- inlineAutoformatEditing( this.editor, this, /(?:^|\s)(_)([^_]+)(_)$/g, italicCallback );
117
- }
118
-
119
- if ( commands.get( 'code' ) ) {
120
- const codeCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'code' );
121
-
122
- inlineAutoformatEditing( this.editor, this, /(`)([^`]+)(`)$/g, codeCallback );
123
- }
124
-
125
- if ( commands.get( 'strikethrough' ) ) {
126
- const strikethroughCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'strikethrough' );
127
-
128
- inlineAutoformatEditing( this.editor, this, /(~~)([^~]+)(~~)$/g, strikethroughCallback );
129
- }
130
- }
131
-
132
- /**
133
- * Adds autoformatting related to {@link module:heading/heading~Heading}.
134
- *
135
- * It is using a number at the end of the command name to associate it with the proper trigger:
136
- *
137
- * * `heading` with value `heading1` will be executed when typing `#`,
138
- * * `heading` with value `heading2` will be executed when typing `##`,
139
- * * ... up to `heading6` and `######`.
140
- *
141
- * @private
142
- */
143
- _addHeadingAutoformats() {
144
- const command = this.editor.commands.get( 'heading' );
145
-
146
- if ( command ) {
147
- command.modelElements
148
- .filter( name => name.match( /^heading[1-6]$/ ) )
149
- .forEach( modelName => {
150
- const level = modelName[ 7 ];
151
- const pattern = new RegExp( `^(#{${ level }})\\s$` );
152
-
153
- blockAutoformatEditing( this.editor, this, pattern, () => {
154
- // Should only be active if command is enabled and heading style associated with pattern is inactive.
155
- if ( !command.isEnabled || command.value === modelName ) {
156
- return false;
157
- }
158
-
159
- this.editor.execute( 'heading', { value: modelName } );
160
- } );
161
- } );
162
- }
163
- }
164
-
165
- /**
166
- * Adds autoformatting related to {@link module:block-quote/blockquote~BlockQuote}.
167
- *
168
- * When typed:
169
- * * `> ` &ndash; A paragraph will be changed to a block quote.
170
- *
171
- * @private
172
- */
173
- _addBlockQuoteAutoformats() {
174
- if ( this.editor.commands.get( 'blockQuote' ) ) {
175
- blockAutoformatEditing( this.editor, this, /^>\s$/, 'blockQuote' );
176
- }
177
- }
178
-
179
- /**
180
- * Adds autoformatting related to {@link module:code-block/codeblock~CodeBlock}.
181
- *
182
- * When typed:
183
- * - `` ``` `` &ndash; A paragraph will be changed to a code block.
184
- *
185
- * @private
186
- */
187
- _addCodeBlockAutoformats() {
188
- const editor = this.editor;
189
- const selection = editor.model.document.selection;
190
-
191
- if ( editor.commands.get( 'codeBlock' ) ) {
192
- blockAutoformatEditing( editor, this, /^```$/, () => {
193
- if ( selection.getFirstPosition().parent.is( 'element', 'listItem' ) ) {
194
- return false;
195
- }
196
- this.editor.execute( 'codeBlock', {
197
- usePreviousLanguageChoice: true
198
- } );
199
- } );
200
- }
201
- }
202
-
203
- /**
204
- * Adds autoformatting related to {@link module:horizontal-line/horizontalline~HorizontalLine}.
205
- *
206
- * When typed:
207
- * - `` --- `` &ndash; Will be replaced with a horizontal line.
208
- *
209
- * @private
210
- */
211
- _addHorizontalLineAutoformats() {
212
- if ( this.editor.commands.get( 'horizontalLine' ) ) {
213
- blockAutoformatEditing( this.editor, this, /^---$/, 'horizontalLine' );
214
- }
215
- }
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 `- ` &ndash; A paragraph will be changed to a bulleted list.
44
+ * - `1. ` or `1) ` &ndash; A paragraph will be changed to a numbered list ("1" can be any digit or a list of digits).
45
+ * - `[] ` or `[ ] ` &ndash; A paragraph will be changed to a to-do list.
46
+ * - `[x] ` or `[ x ] ` &ndash; A paragraph will be changed to 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**` &ndash; `**` characters are removed and `foobar` is set to bold,
73
+ * - `__foobar__` &ndash; `__` characters are removed and `foobar` is set to bold,
74
+ * - `*foobar*` &ndash; `*` characters are removed and `foobar` is set to italic,
75
+ * - `_foobar_` &ndash; `_` characters are removed and `foobar` is set to italic,
76
+ * - ``` `foobar` &ndash; ``` ` ``` characters are removed and `foobar` is set to code,
77
+ * - `~~foobar~~` &ndash; `~~` 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 value `heading1` will be executed when typing `#`,
108
+ * * `heading` with value `heading2` will be executed when typing `##`,
109
+ * * ... up to `heading6` and `######`.
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
+ * * `> ` &ndash; 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
+ * - `` ``` `` &ndash; 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
+ * - `` --- `` &ndash; 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
+ }
216
171
  }
217
-
218
- // Helper function for getting `inlineAutoformatEditing` callbacks that checks if command is enabled.
219
- //
220
- // @param {module:core/editor/editor~Editor} editor
221
- // @param {String} attributeKey
222
- // @returns {Function}
223
- function getCallbackFunctionForInlineAutoformat( editor, attributeKey ) {
224
- return ( writer, rangesToFormat ) => {
225
- const command = editor.commands.get( attributeKey );
226
-
227
- if ( !command.isEnabled ) {
228
- return false;
229
- }
230
-
231
- const validRanges = editor.model.schema.getValidRanges( rangesToFormat, attributeKey );
232
-
233
- for ( const range of validRanges ) {
234
- writer.setAttribute( attributeKey, true, range );
235
- }
236
-
237
- // After applying attribute to the text, remove given attribute from the selection.
238
- // This way user is able to type a text without attribute used by auto formatter.
239
- writer.removeSelectionAttribute( attributeKey );
240
- };
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
+ };
241
189
  }
@@ -1,11 +1,9 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  import { LiveRange } from 'ckeditor5/src/engine';
7
6
  import { first } from 'ckeditor5/src/utils';
8
-
9
7
  /**
10
8
  * The block autoformatting engine. It allows to format various block patterns. For example,
11
9
  * it can be configured to turn a paragraph starting with `*` and followed by a space into a list item.
@@ -20,7 +18,6 @@ import { first } from 'ckeditor5/src/utils';
20
18
  *
21
19
  * @module autoformat/blockautoformatediting
22
20
  */
23
-
24
21
  /**
25
22
  * Creates a listener triggered on {@link module:engine/model/document~Document#event:change:data `change:data`} event in the document.
26
23
  * Calls the callback when inserted text matches the regular expression or the command name
@@ -30,128 +27,110 @@ import { first } from 'ckeditor5/src/utils';
30
27
  *
31
28
  * To convert a paragraph to heading 1 when `- ` is typed, using just the command name:
32
29
  *
33
- * blockAutoformatEditing( editor, plugin, /^\- $/, 'heading1' );
30
+ * ```ts
31
+ * blockAutoformatEditing( editor, plugin, /^\- $/, 'heading1' );
32
+ * ```
34
33
  *
35
34
  * To convert a paragraph to heading 1 when `- ` is typed, using just the callback:
36
35
  *
37
- * blockAutoformatEditing( editor, plugin, /^\- $/, ( context ) => {
38
- * const { match } = context;
39
- * const headingLevel = match[ 1 ].length;
36
+ * ```ts
37
+ * blockAutoformatEditing( editor, plugin, /^\- $/, ( context ) => {
38
+ * const { match } = context;
39
+ * const headingLevel = match[ 1 ].length;
40
40
  *
41
- * editor.execute( 'heading', {
42
- * formatId: `heading${ headingLevel }`
43
- * } );
44
- * } );
41
+ * editor.execute( 'heading', {
42
+ * formatId: `heading${ headingLevel }`
43
+ * } );
44
+ * } );
45
+ * ```
45
46
  *
46
- * @param {module:core/editor/editor~Editor} editor The editor instance.
47
- * @param {module:autoformat/autoformat~Autoformat} plugin The autoformat plugin instance.
48
- * @param {RegExp} pattern The regular expression to execute on just inserted text. The regular expression is tested against the text
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
49
50
  * from the beginning until the caret position.
50
- * @param {Function|String} callbackOrCommand The callback to execute or the command to run when the text is matched.
51
+ * @param callbackOrCommand The callback to execute or the command to run when the text is matched.
51
52
  * In case of providing the callback, it receives the following parameter:
52
53
  * * {Object} match RegExp.exec() result of matching the pattern to inserted text.
53
54
  */
54
- export default function blockAutoformatEditing( editor, plugin, pattern, callbackOrCommand ) {
55
- let callback;
56
- let command = null;
57
-
58
- if ( typeof callbackOrCommand == 'function' ) {
59
- callback = callbackOrCommand;
60
- } else {
61
- // We assume that the actual command name was provided.
62
- command = editor.commands.get( callbackOrCommand );
63
-
64
- callback = () => {
65
- editor.execute( callbackOrCommand );
66
- };
67
- }
68
-
69
- editor.model.document.on( 'change:data', ( evt, batch ) => {
70
- if ( command && !command.isEnabled || !plugin.isEnabled ) {
71
- return;
72
- }
73
-
74
- const range = first( editor.model.document.selection.getRanges() );
75
-
76
- if ( !range.isCollapsed ) {
77
- return;
78
- }
79
-
80
- if ( batch.isUndo || !batch.isLocal ) {
81
- return;
82
- }
83
-
84
- const changes = Array.from( editor.model.document.differ.getChanges() );
85
- const entry = changes[ 0 ];
86
-
87
- // Typing is represented by only a single change.
88
- if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1 ) {
89
- return;
90
- }
91
-
92
- const blockToFormat = entry.position.parent;
93
-
94
- // Block formatting should be disabled in codeBlocks (#5800).
95
- if ( blockToFormat.is( 'element', 'codeBlock' ) ) {
96
- return;
97
- }
98
-
99
- // Only list commands and custom callbacks can be applied inside a list.
100
- if ( blockToFormat.is( 'element', 'listItem' ) &&
101
- typeof callbackOrCommand !== 'function' &&
102
- ![ 'numberedList', 'bulletedList', 'todoList' ].includes( callbackOrCommand )
103
- ) {
104
- return;
105
- }
106
-
107
- // In case a command is bound, do not re-execute it over an existing block style which would result with a style removal.
108
- // Instead just drop processing so that autoformat trigger text is not lost. E.g. writing "# " in a level 1 heading.
109
- if ( command && command.value === true ) {
110
- return;
111
- }
112
-
113
- const firstNode = blockToFormat.getChild( 0 );
114
- const firstNodeRange = editor.model.createRangeOn( firstNode );
115
-
116
- // Range is only expected to be within or at the very end of the first text node.
117
- if ( !firstNodeRange.containsRange( range ) && !range.end.isEqual( firstNodeRange.end ) ) {
118
- return;
119
- }
120
-
121
- const match = pattern.exec( firstNode.data.substr( 0, range.end.offset ) );
122
-
123
- // ...and this text node's data match the pattern.
124
- if ( !match ) {
125
- return;
126
- }
127
-
128
- // Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
129
- editor.model.enqueueChange( writer => {
130
- // Matched range.
131
- const start = writer.createPositionAt( blockToFormat, 0 );
132
- const end = writer.createPositionAt( blockToFormat, match[ 0 ].length );
133
- const range = new LiveRange( start, end );
134
-
135
- const wasChanged = callback( { match } );
136
-
137
- // Remove matched text.
138
- if ( wasChanged !== false ) {
139
- writer.remove( range );
140
-
141
- const selectionRange = editor.model.document.selection.getFirstRange();
142
- const blockRange = writer.createRangeIn( blockToFormat );
143
-
144
- // If the block is empty and the document selection has been moved when
145
- // applying formatting (e.g. is now in newly created block).
146
- if ( blockToFormat.isEmpty && !blockRange.isEqual( selectionRange ) && !blockRange.containsRange( selectionRange, true ) ) {
147
- writer.remove( blockToFormat );
148
- }
149
- }
150
- range.detach();
151
-
152
- editor.model.enqueueChange( () => {
153
- editor.plugins.get( 'Delete' ).requestUndoOnBackspace();
154
- } );
155
- } );
156
- } );
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 with 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
+ editor.plugins.get('Delete').requestUndoOnBackspace();
133
+ });
134
+ });
135
+ });
157
136
  }
package/src/index.js CHANGED
@@ -1,10 +1,8 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module autoformat
8
7
  */
9
-
10
8
  export { default as Autoformat } from './autoformat';
@@ -1,23 +1,7 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * The inline autoformatting engine. It allows to format various inline patterns. For example,
8
- * it can be configured to make "foo" bold when typed `**foo**` (the `**` markers will be removed).
9
- *
10
- * The autoformatting operation is integrated with the undo manager,
11
- * so the autoformatting step can be undone if the user's intention was not to format the text.
12
- *
13
- * See the {@link module:autoformat/inlineautoformatediting~inlineAutoformatEditing `inlineAutoformatEditing`} documentation
14
- * to learn how to create custom inline autoformatters. You can also use
15
- * the {@link module:autoformat/autoformat~Autoformat} feature which enables a set of default autoformatters
16
- * (lists, headings, bold and italic).
17
- *
18
- * @module autoformat/inlineautoformatediting
19
- */
20
-
21
5
  /**
22
6
  * Enables autoformatting mechanism for a given {@link module:core/editor/editor~Editor}.
23
7
  *
@@ -26,193 +10,164 @@
26
10
  * the autoformatting engine checks the text on the left of the selection
27
11
  * and executes the provided action if the text matches given criteria (regular expression or callback).
28
12
  *
29
- * @param {module:core/editor/editor~Editor} editor The editor instance.
30
- * @param {module:autoformat/autoformat~Autoformat} plugin The autoformat plugin instance.
31
- * @param {Function|RegExp} testRegexpOrCallback The regular expression or callback to execute on text.
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.
32
16
  * Provided regular expression *must* have three capture groups. The first and the third capture group
33
17
  * should match opening and closing delimiters. The second capture group should match the text to format.
34
18
  *
35
- * // Matches the `**bold text**` pattern.
36
- * // There are three capturing groups:
37
- * // - The first to match the starting `**` delimiter.
38
- * // - The second to match the text to format.
39
- * // - The third to match the ending `**` delimiter.
40
- * inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, formatCallback );
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
+ * ```
41
27
  *
42
28
  * When a function is provided instead of the regular expression, it will be executed with the text to match as a parameter.
43
29
  * The function should return proper "ranges" to delete and format.
44
30
  *
45
- * {
46
- * remove: [
47
- * [ 0, 1 ], // Remove the first letter from the given text.
48
- * [ 5, 6 ] // Remove the 6th letter from the given text.
49
- * ],
50
- * format: [
51
- * [ 1, 5 ] // Format all letters from 2nd to 5th.
52
- * ]
53
- * }
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
+ * ```
54
42
  *
55
- * @param {Function} formatCallback A callback to apply actual formatting.
43
+ * @param formatCallback A callback to apply actual formatting.
56
44
  * It should return `false` if changes should not be applied (e.g. if a command is disabled).
57
45
  *
58
- * inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
59
- * const command = editor.commands.get( 'bold' );
46
+ * ```ts
47
+ * inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
48
+ * const command = editor.commands.get( 'bold' );
60
49
  *
61
- * if ( !command.isEnabled ) {
62
- * return false;
63
- * }
50
+ * if ( !command.isEnabled ) {
51
+ * return false;
52
+ * }
64
53
  *
65
- * const validRanges = editor.model.schema.getValidRanges( rangesToFormat, 'bold' );
54
+ * const validRanges = editor.model.schema.getValidRanges( rangesToFormat, 'bold' );
66
55
  *
67
- * for ( let range of validRanges ) {
68
- * writer.setAttribute( 'bold', true, range );
69
- * }
70
- * } );
56
+ * for ( let range of validRanges ) {
57
+ * writer.setAttribute( 'bold', true, range );
58
+ * }
59
+ * } );
60
+ * ```
71
61
  */
72
- export default function inlineAutoformatEditing( editor, plugin, testRegexpOrCallback, formatCallback ) {
73
- let regExp;
74
- let testCallback;
75
-
76
- if ( testRegexpOrCallback instanceof RegExp ) {
77
- regExp = testRegexpOrCallback;
78
- } else {
79
- testCallback = testRegexpOrCallback;
80
- }
81
-
82
- // A test callback run on changed text.
83
- testCallback = testCallback || ( text => {
84
- let result;
85
- const remove = [];
86
- const format = [];
87
-
88
- while ( ( result = regExp.exec( text ) ) !== null ) {
89
- // There should be full match and 3 capture groups.
90
- if ( result && result.length < 4 ) {
91
- break;
92
- }
93
-
94
- let {
95
- index,
96
- '1': leftDel,
97
- '2': content,
98
- '3': rightDel
99
- } = result;
100
-
101
- // Real matched string - there might be some non-capturing groups so we need to recalculate starting index.
102
- const found = leftDel + content + rightDel;
103
- index += result[ 0 ].length - found.length;
104
-
105
- // Start and End offsets of delimiters to remove.
106
- const delStart = [
107
- index,
108
- index + leftDel.length
109
- ];
110
- const delEnd = [
111
- index + leftDel.length + content.length,
112
- index + leftDel.length + content.length + rightDel.length
113
- ];
114
-
115
- remove.push( delStart );
116
- remove.push( delEnd );
117
-
118
- format.push( [ index + leftDel.length, index + leftDel.length + content.length ] );
119
- }
120
-
121
- return {
122
- remove,
123
- format
124
- };
125
- } );
126
-
127
- editor.model.document.on( 'change:data', ( evt, batch ) => {
128
- if ( batch.isUndo || !batch.isLocal || !plugin.isEnabled ) {
129
- return;
130
- }
131
-
132
- const model = editor.model;
133
- const selection = model.document.selection;
134
-
135
- // Do nothing if selection is not collapsed.
136
- if ( !selection.isCollapsed ) {
137
- return;
138
- }
139
-
140
- const changes = Array.from( model.document.differ.getChanges() );
141
- const entry = changes[ 0 ];
142
-
143
- // Typing is represented by only a single change.
144
- if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1 ) {
145
- return;
146
- }
147
-
148
- const focus = selection.focus;
149
- const block = focus.parent;
150
- const { text, range } = getTextAfterCode( model.createRange( model.createPositionAt( block, 0 ), focus ), model );
151
- const testOutput = testCallback( text );
152
- const rangesToFormat = testOutputToRanges( range.start, testOutput.format, model );
153
- const rangesToRemove = testOutputToRanges( range.start, testOutput.remove, model );
154
-
155
- if ( !( rangesToFormat.length && rangesToRemove.length ) ) {
156
- return;
157
- }
158
-
159
- // Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
160
- model.enqueueChange( writer => {
161
- // Apply format.
162
- const hasChanged = formatCallback( writer, rangesToFormat );
163
-
164
- // Strict check on `false` to have backward compatibility (when callbacks were returning `undefined`).
165
- if ( hasChanged === false ) {
166
- return;
167
- }
168
-
169
- // Remove delimiters - use reversed order to not mix the offsets while removing.
170
- for ( const range of rangesToRemove.reverse() ) {
171
- writer.remove( range );
172
- }
173
-
174
- model.enqueueChange( () => {
175
- editor.plugins.get( 'Delete' ).requestUndoOnBackspace();
176
- } );
177
- } );
178
- } );
62
+ export default 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
+ editor.plugins.get('Delete').requestUndoOnBackspace();
142
+ });
143
+ });
144
+ });
179
145
  }
180
-
181
- // Converts output of the test function provided to the inlineAutoformatEditing and converts it to the model ranges
182
- // inside provided block.
183
- //
184
- // @private
185
- // @param {module:engine/model/position~Position} start
186
- // @param {Array.<Array>} arrays
187
- // @param {module:engine/model/model~Model} model
188
- function testOutputToRanges( start, arrays, model ) {
189
- return arrays
190
- .filter( array => ( array[ 0 ] !== undefined && array[ 1 ] !== undefined ) )
191
- .map( array => {
192
- return model.createRange( start.getShiftedBy( array[ 0 ] ), start.getShiftedBy( array[ 1 ] ) );
193
- } );
146
+ /**
147
+ * Converts output of the test function provided to the inlineAutoformatEditing and converts it to the model ranges
148
+ * inside provided block.
149
+ */
150
+ function testOutputToRanges(start, arrays, model) {
151
+ return arrays
152
+ .filter(array => (array[0] !== undefined && array[1] !== undefined))
153
+ .map(array => {
154
+ return model.createRange(start.getShiftedBy(array[0]), start.getShiftedBy(array[1]));
155
+ });
194
156
  }
195
-
196
- // Returns the last text line after the last code element from the given range.
197
- // It is similar to {@link module:typing/utils/getlasttextline.getLastTextLine `getLastTextLine()`},
198
- // but it ignores any text before the last `code`.
199
- //
200
- // @param {module:engine/model/range~Range} range
201
- // @param {module:engine/model/model~Model} model
202
- // @returns {module:typing/utils/getlasttextline~LastTextLineData}
203
- function getTextAfterCode( range, model ) {
204
- let start = range.start;
205
-
206
- const text = Array.from( range.getItems() ).reduce( ( rangeText, node ) => {
207
- // Trim text to a last occurrence of an inline element and update range start.
208
- if ( !( node.is( '$text' ) || node.is( '$textProxy' ) ) || node.getAttribute( 'code' ) ) {
209
- start = model.createPositionAfter( node );
210
-
211
- return '';
212
- }
213
-
214
- return rangeText + node.data;
215
- }, '' );
216
-
217
- return { text, range: model.createRange( start, range.end ) };
157
+ /**
158
+ * Returns the last text line after the last code element from the given range.
159
+ * It is similar to {@link module:typing/utils/getlasttextline.getLastTextLine `getLastTextLine()`},
160
+ * but it ignores any text before the last `code`.
161
+ */
162
+ function getTextAfterCode(range, model) {
163
+ let start = range.start;
164
+ const text = Array.from(range.getItems()).reduce((rangeText, node) => {
165
+ // Trim text to a last occurrence of an inline element and update range start.
166
+ if (!(node.is('$text') || node.is('$textProxy')) || node.getAttribute('code')) {
167
+ start = model.createPositionAfter(node);
168
+ return '';
169
+ }
170
+ return rangeText + node.data;
171
+ }, '');
172
+ return { text, range: model.createRange(start, range.end) };
218
173
  }