@ckeditor/ckeditor5-enter 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 +4 -4
- package/src/augmentation.d.ts +15 -15
- package/src/augmentation.js +5 -5
- package/src/enter.d.ts +22 -22
- package/src/enter.js +45 -45
- package/src/entercommand.d.ts +52 -52
- package/src/entercommand.js +96 -96
- package/src/enterobserver.d.ts +44 -44
- package/src/enterobserver.js +61 -61
- package/src/index.d.ts +13 -13
- package/src/index.js +10 -10
- package/src/shiftenter.d.ts +19 -19
- package/src/shiftenter.js +63 -63
- package/src/shiftentercommand.d.ts +34 -34
- package/src/shiftentercommand.js +118 -118
- package/src/utils.d.ts +18 -18
- package/src/utils.js +20 -20
package/LICENSE.md
CHANGED
|
@@ -2,7 +2,7 @@ Software License Agreement
|
|
|
2
2
|
==========================
|
|
3
3
|
|
|
4
4
|
**CKEditor 5 enter feature** – https://github.com/ckeditor/ckeditor5-enter <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-enter",
|
|
3
|
-
"version": "40.
|
|
3
|
+
"version": "40.1.0",
|
|
4
4
|
"description": "Enter feature for CKEditor 5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckeditor",
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
],
|
|
13
13
|
"main": "src/index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@ckeditor/ckeditor5-core": "40.
|
|
16
|
-
"@ckeditor/ckeditor5-engine": "40.
|
|
17
|
-
"@ckeditor/ckeditor5-utils": "40.
|
|
15
|
+
"@ckeditor/ckeditor5-core": "40.1.0",
|
|
16
|
+
"@ckeditor/ckeditor5-engine": "40.1.0",
|
|
17
|
+
"@ckeditor/ckeditor5-utils": "40.1.0"
|
|
18
18
|
},
|
|
19
19
|
"author": "CKSource (http://cksource.com/)",
|
|
20
20
|
"license": "GPL-2.0-or-later",
|
package/src/augmentation.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
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 { Enter, ShiftEnter, EnterCommand, ShiftEnterCommand } from './index';
|
|
6
|
-
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
-
interface PluginsMap {
|
|
8
|
-
[Enter.pluginName]: Enter;
|
|
9
|
-
[ShiftEnter.pluginName]: ShiftEnter;
|
|
10
|
-
}
|
|
11
|
-
interface CommandsMap {
|
|
12
|
-
enter: EnterCommand;
|
|
13
|
-
shiftEnter: ShiftEnterCommand;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
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 { Enter, ShiftEnter, EnterCommand, ShiftEnterCommand } from './index';
|
|
6
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
7
|
+
interface PluginsMap {
|
|
8
|
+
[Enter.pluginName]: Enter;
|
|
9
|
+
[ShiftEnter.pluginName]: ShiftEnter;
|
|
10
|
+
}
|
|
11
|
+
interface CommandsMap {
|
|
12
|
+
enter: EnterCommand;
|
|
13
|
+
shiftEnter: ShiftEnterCommand;
|
|
14
|
+
}
|
|
15
|
+
}
|
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/enter.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module enter/enter
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
-
/**
|
|
10
|
-
* This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
|
|
11
|
-
*
|
|
12
|
-
* See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
|
|
13
|
-
*
|
|
14
|
-
* For more information about this feature see the {@glink api/enter package page}.
|
|
15
|
-
*/
|
|
16
|
-
export default class Enter extends Plugin {
|
|
17
|
-
/**
|
|
18
|
-
* @inheritDoc
|
|
19
|
-
*/
|
|
20
|
-
static get pluginName(): "Enter";
|
|
21
|
-
init(): void;
|
|
22
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module enter/enter
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
/**
|
|
10
|
+
* This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
|
|
11
|
+
*
|
|
12
|
+
* See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
|
|
13
|
+
*
|
|
14
|
+
* For more information about this feature see the {@glink api/enter package page}.
|
|
15
|
+
*/
|
|
16
|
+
export default class Enter extends Plugin {
|
|
17
|
+
/**
|
|
18
|
+
* @inheritDoc
|
|
19
|
+
*/
|
|
20
|
+
static get pluginName(): "Enter";
|
|
21
|
+
init(): void;
|
|
22
|
+
}
|
package/src/enter.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module enter/enter
|
|
7
|
-
*/
|
|
8
|
-
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
-
import EnterCommand from './entercommand';
|
|
10
|
-
import EnterObserver from './enterobserver';
|
|
11
|
-
/**
|
|
12
|
-
* This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
|
|
13
|
-
*
|
|
14
|
-
* See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
|
|
15
|
-
*
|
|
16
|
-
* For more information about this feature see the {@glink api/enter package page}.
|
|
17
|
-
*/
|
|
18
|
-
export default class Enter extends Plugin {
|
|
19
|
-
/**
|
|
20
|
-
* @inheritDoc
|
|
21
|
-
*/
|
|
22
|
-
static get pluginName() {
|
|
23
|
-
return 'Enter';
|
|
24
|
-
}
|
|
25
|
-
init() {
|
|
26
|
-
const editor = this.editor;
|
|
27
|
-
const view = editor.editing.view;
|
|
28
|
-
const viewDocument = view.document;
|
|
29
|
-
view.addObserver(EnterObserver);
|
|
30
|
-
editor.commands.add('enter', new EnterCommand(editor));
|
|
31
|
-
this.listenTo(viewDocument, 'enter', (evt, data) => {
|
|
32
|
-
// When not in composition, we handle the action, so prevent the default one.
|
|
33
|
-
// When in composition, it's the browser who modify the DOM (renderer is disabled).
|
|
34
|
-
if (!viewDocument.isComposing) {
|
|
35
|
-
data.preventDefault();
|
|
36
|
-
}
|
|
37
|
-
// The soft enter key is handled by the ShiftEnter plugin.
|
|
38
|
-
if (data.isSoft) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
editor.execute('enter');
|
|
42
|
-
view.scrollToTheSelection();
|
|
43
|
-
}, { priority: 'low' });
|
|
44
|
-
}
|
|
45
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module enter/enter
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import EnterCommand from './entercommand';
|
|
10
|
+
import EnterObserver from './enterobserver';
|
|
11
|
+
/**
|
|
12
|
+
* This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
|
|
13
|
+
*
|
|
14
|
+
* See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
|
|
15
|
+
*
|
|
16
|
+
* For more information about this feature see the {@glink api/enter package page}.
|
|
17
|
+
*/
|
|
18
|
+
export default class Enter extends Plugin {
|
|
19
|
+
/**
|
|
20
|
+
* @inheritDoc
|
|
21
|
+
*/
|
|
22
|
+
static get pluginName() {
|
|
23
|
+
return 'Enter';
|
|
24
|
+
}
|
|
25
|
+
init() {
|
|
26
|
+
const editor = this.editor;
|
|
27
|
+
const view = editor.editing.view;
|
|
28
|
+
const viewDocument = view.document;
|
|
29
|
+
view.addObserver(EnterObserver);
|
|
30
|
+
editor.commands.add('enter', new EnterCommand(editor));
|
|
31
|
+
this.listenTo(viewDocument, 'enter', (evt, data) => {
|
|
32
|
+
// When not in composition, we handle the action, so prevent the default one.
|
|
33
|
+
// When in composition, it's the browser who modify the DOM (renderer is disabled).
|
|
34
|
+
if (!viewDocument.isComposing) {
|
|
35
|
+
data.preventDefault();
|
|
36
|
+
}
|
|
37
|
+
// The soft enter key is handled by the ShiftEnter plugin.
|
|
38
|
+
if (data.isSoft) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
editor.execute('enter');
|
|
42
|
+
view.scrollToTheSelection();
|
|
43
|
+
}, { priority: 'low' });
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/entercommand.d.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module enter/entercommand
|
|
7
|
-
*/
|
|
8
|
-
import { Command } from '@ckeditor/ckeditor5-core';
|
|
9
|
-
import type { Writer } from '@ckeditor/ckeditor5-engine';
|
|
10
|
-
/**
|
|
11
|
-
* Enter command used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
|
|
12
|
-
*/
|
|
13
|
-
export default class EnterCommand extends Command {
|
|
14
|
-
/**
|
|
15
|
-
* @inheritDoc
|
|
16
|
-
*/
|
|
17
|
-
execute(): void;
|
|
18
|
-
/**
|
|
19
|
-
* Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
|
|
20
|
-
*
|
|
21
|
-
* ```
|
|
22
|
-
* <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
|
|
23
|
-
* <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
|
|
24
|
-
* <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* In some cases, the split will not happen:
|
|
28
|
-
*
|
|
29
|
-
* ```
|
|
30
|
-
* // The selection parent is a limit element:
|
|
31
|
-
* <figcaption>A[bc]d</figcaption> -> <figcaption>A[]d</figcaption>
|
|
32
|
-
*
|
|
33
|
-
* // The selection spans over multiple elements:
|
|
34
|
-
* <h>x[x</h><p>y]y<p> -> <h>x</h><p>[]y</p>
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @param writer Writer to use when performing the enter action.
|
|
38
|
-
* @returns Boolean indicating if the block was split.
|
|
39
|
-
*/
|
|
40
|
-
enterBlock(writer: Writer): boolean;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Fired after the the {@link module:enter/entercommand~EnterCommand} is finished executing.
|
|
44
|
-
*
|
|
45
|
-
* @eventName ~EnterCommand#afterExecute
|
|
46
|
-
*/
|
|
47
|
-
export type EnterCommandAfterExecuteEvent = {
|
|
48
|
-
name: 'afterExecute';
|
|
49
|
-
args: [{
|
|
50
|
-
writer: Writer;
|
|
51
|
-
}];
|
|
52
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module enter/entercommand
|
|
7
|
+
*/
|
|
8
|
+
import { Command } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import type { Writer } from '@ckeditor/ckeditor5-engine';
|
|
10
|
+
/**
|
|
11
|
+
* Enter command used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
|
|
12
|
+
*/
|
|
13
|
+
export default class EnterCommand extends Command {
|
|
14
|
+
/**
|
|
15
|
+
* @inheritDoc
|
|
16
|
+
*/
|
|
17
|
+
execute(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
* <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
|
|
23
|
+
* <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
|
|
24
|
+
* <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* In some cases, the split will not happen:
|
|
28
|
+
*
|
|
29
|
+
* ```
|
|
30
|
+
* // The selection parent is a limit element:
|
|
31
|
+
* <figcaption>A[bc]d</figcaption> -> <figcaption>A[]d</figcaption>
|
|
32
|
+
*
|
|
33
|
+
* // The selection spans over multiple elements:
|
|
34
|
+
* <h>x[x</h><p>y]y<p> -> <h>x</h><p>[]y</p>
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param writer Writer to use when performing the enter action.
|
|
38
|
+
* @returns Boolean indicating if the block was split.
|
|
39
|
+
*/
|
|
40
|
+
enterBlock(writer: Writer): boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fired after the the {@link module:enter/entercommand~EnterCommand} is finished executing.
|
|
44
|
+
*
|
|
45
|
+
* @eventName ~EnterCommand#afterExecute
|
|
46
|
+
*/
|
|
47
|
+
export type EnterCommandAfterExecuteEvent = {
|
|
48
|
+
name: 'afterExecute';
|
|
49
|
+
args: [{
|
|
50
|
+
writer: Writer;
|
|
51
|
+
}];
|
|
52
|
+
};
|
package/src/entercommand.js
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module enter/entercommand
|
|
7
|
-
*/
|
|
8
|
-
import { Command } from '@ckeditor/ckeditor5-core';
|
|
9
|
-
import { getCopyOnEnterAttributes } from './utils';
|
|
10
|
-
/**
|
|
11
|
-
* Enter command used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
|
|
12
|
-
*/
|
|
13
|
-
export default class EnterCommand extends Command {
|
|
14
|
-
/**
|
|
15
|
-
* @inheritDoc
|
|
16
|
-
*/
|
|
17
|
-
execute() {
|
|
18
|
-
this.editor.model.change(writer => {
|
|
19
|
-
this.enterBlock(writer);
|
|
20
|
-
this.fire('afterExecute', { writer });
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
|
|
25
|
-
*
|
|
26
|
-
* ```
|
|
27
|
-
* <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
|
|
28
|
-
* <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
|
|
29
|
-
* <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* In some cases, the split will not happen:
|
|
33
|
-
*
|
|
34
|
-
* ```
|
|
35
|
-
* // The selection parent is a limit element:
|
|
36
|
-
* <figcaption>A[bc]d</figcaption> -> <figcaption>A[]d</figcaption>
|
|
37
|
-
*
|
|
38
|
-
* // The selection spans over multiple elements:
|
|
39
|
-
* <h>x[x</h><p>y]y<p> -> <h>x</h><p>[]y</p>
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* @param writer Writer to use when performing the enter action.
|
|
43
|
-
* @returns Boolean indicating if the block was split.
|
|
44
|
-
*/
|
|
45
|
-
enterBlock(writer) {
|
|
46
|
-
const model = this.editor.model;
|
|
47
|
-
const selection = model.document.selection;
|
|
48
|
-
const schema = model.schema;
|
|
49
|
-
const isSelectionEmpty = selection.isCollapsed;
|
|
50
|
-
const range = selection.getFirstRange();
|
|
51
|
-
const startElement = range.start.parent;
|
|
52
|
-
const endElement = range.end.parent;
|
|
53
|
-
// Don't touch the roots and other limit elements.
|
|
54
|
-
if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
|
|
55
|
-
// Delete the selected content but only if inside a single limit element.
|
|
56
|
-
// Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
|
|
57
|
-
// This is an edge case and it's hard to tell what should actually happen because such a selection
|
|
58
|
-
// is not entirely valid.
|
|
59
|
-
if (!isSelectionEmpty && startElement == endElement) {
|
|
60
|
-
model.deleteContent(selection);
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
if (isSelectionEmpty) {
|
|
65
|
-
const attributesToCopy = getCopyOnEnterAttributes(writer.model.schema, selection.getAttributes());
|
|
66
|
-
splitBlock(writer, range.start);
|
|
67
|
-
writer.setSelectionAttribute(attributesToCopy);
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
const leaveUnmerged = !(range.start.isAtStart && range.end.isAtEnd);
|
|
72
|
-
const isContainedWithinOneElement = (startElement == endElement);
|
|
73
|
-
model.deleteContent(selection, { leaveUnmerged });
|
|
74
|
-
if (leaveUnmerged) {
|
|
75
|
-
// Partially selected elements.
|
|
76
|
-
//
|
|
77
|
-
// <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
|
|
78
|
-
if (isContainedWithinOneElement) {
|
|
79
|
-
splitBlock(writer, selection.focus);
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
// Selection over multiple elements.
|
|
83
|
-
//
|
|
84
|
-
// <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
|
|
85
|
-
else {
|
|
86
|
-
writer.setSelection(endElement, 0);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function splitBlock(writer, splitPos) {
|
|
94
|
-
writer.split(splitPos);
|
|
95
|
-
writer.setSelection(splitPos.parent.nextSibling, 0);
|
|
96
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module enter/entercommand
|
|
7
|
+
*/
|
|
8
|
+
import { Command } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import { getCopyOnEnterAttributes } from './utils';
|
|
10
|
+
/**
|
|
11
|
+
* Enter command used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
|
|
12
|
+
*/
|
|
13
|
+
export default class EnterCommand extends Command {
|
|
14
|
+
/**
|
|
15
|
+
* @inheritDoc
|
|
16
|
+
*/
|
|
17
|
+
execute() {
|
|
18
|
+
this.editor.model.change(writer => {
|
|
19
|
+
this.enterBlock(writer);
|
|
20
|
+
this.fire('afterExecute', { writer });
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
* <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
|
|
28
|
+
* <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
|
|
29
|
+
* <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* In some cases, the split will not happen:
|
|
33
|
+
*
|
|
34
|
+
* ```
|
|
35
|
+
* // The selection parent is a limit element:
|
|
36
|
+
* <figcaption>A[bc]d</figcaption> -> <figcaption>A[]d</figcaption>
|
|
37
|
+
*
|
|
38
|
+
* // The selection spans over multiple elements:
|
|
39
|
+
* <h>x[x</h><p>y]y<p> -> <h>x</h><p>[]y</p>
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param writer Writer to use when performing the enter action.
|
|
43
|
+
* @returns Boolean indicating if the block was split.
|
|
44
|
+
*/
|
|
45
|
+
enterBlock(writer) {
|
|
46
|
+
const model = this.editor.model;
|
|
47
|
+
const selection = model.document.selection;
|
|
48
|
+
const schema = model.schema;
|
|
49
|
+
const isSelectionEmpty = selection.isCollapsed;
|
|
50
|
+
const range = selection.getFirstRange();
|
|
51
|
+
const startElement = range.start.parent;
|
|
52
|
+
const endElement = range.end.parent;
|
|
53
|
+
// Don't touch the roots and other limit elements.
|
|
54
|
+
if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
|
|
55
|
+
// Delete the selected content but only if inside a single limit element.
|
|
56
|
+
// Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
|
|
57
|
+
// This is an edge case and it's hard to tell what should actually happen because such a selection
|
|
58
|
+
// is not entirely valid.
|
|
59
|
+
if (!isSelectionEmpty && startElement == endElement) {
|
|
60
|
+
model.deleteContent(selection);
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (isSelectionEmpty) {
|
|
65
|
+
const attributesToCopy = getCopyOnEnterAttributes(writer.model.schema, selection.getAttributes());
|
|
66
|
+
splitBlock(writer, range.start);
|
|
67
|
+
writer.setSelectionAttribute(attributesToCopy);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const leaveUnmerged = !(range.start.isAtStart && range.end.isAtEnd);
|
|
72
|
+
const isContainedWithinOneElement = (startElement == endElement);
|
|
73
|
+
model.deleteContent(selection, { leaveUnmerged });
|
|
74
|
+
if (leaveUnmerged) {
|
|
75
|
+
// Partially selected elements.
|
|
76
|
+
//
|
|
77
|
+
// <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
|
|
78
|
+
if (isContainedWithinOneElement) {
|
|
79
|
+
splitBlock(writer, selection.focus);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
// Selection over multiple elements.
|
|
83
|
+
//
|
|
84
|
+
// <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
|
|
85
|
+
else {
|
|
86
|
+
writer.setSelection(endElement, 0);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function splitBlock(writer, splitPos) {
|
|
94
|
+
writer.split(splitPos);
|
|
95
|
+
writer.setSelection(splitPos.parent.nextSibling, 0);
|
|
96
|
+
}
|
package/src/enterobserver.d.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module enter/enterobserver
|
|
7
|
-
*/
|
|
8
|
-
import { Observer, DomEventData, type View, type BubblingEvent } from '@ckeditor/ckeditor5-engine';
|
|
9
|
-
/**
|
|
10
|
-
* Enter observer introduces the {@link module:engine/view/document~Document#event:enter `Document#enter`} event.
|
|
11
|
-
*/
|
|
12
|
-
export default class EnterObserver extends Observer {
|
|
13
|
-
/**
|
|
14
|
-
* @inheritDoc
|
|
15
|
-
*/
|
|
16
|
-
constructor(view: View);
|
|
17
|
-
/**
|
|
18
|
-
* @inheritDoc
|
|
19
|
-
*/
|
|
20
|
-
observe(): void;
|
|
21
|
-
/**
|
|
22
|
-
* @inheritDoc
|
|
23
|
-
*/
|
|
24
|
-
stopObserving(): void;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Fired when the user presses the <kbd>Enter</kbd> key.
|
|
28
|
-
*
|
|
29
|
-
* Note: This event is fired by the {@link module:enter/enterobserver~EnterObserver observer}
|
|
30
|
-
* (usually registered by the {@link module:enter/enter~Enter Enter feature} and
|
|
31
|
-
* {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature}).
|
|
32
|
-
*
|
|
33
|
-
* @eventName module:engine/view/document~Document#enter
|
|
34
|
-
*/
|
|
35
|
-
export type ViewDocumentEnterEvent = BubblingEvent<{
|
|
36
|
-
name: 'enter';
|
|
37
|
-
args: [EnterEventData];
|
|
38
|
-
}>;
|
|
39
|
-
export interface EnterEventData extends DomEventData<InputEvent> {
|
|
40
|
-
/**
|
|
41
|
-
* Whether it is a soft enter (<kbd>Shift</kbd>+<kbd>Enter</kbd>) or a hard enter (<kbd>Enter</kbd>).
|
|
42
|
-
*/
|
|
43
|
-
isSoft: boolean;
|
|
44
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module enter/enterobserver
|
|
7
|
+
*/
|
|
8
|
+
import { Observer, DomEventData, type View, type BubblingEvent } from '@ckeditor/ckeditor5-engine';
|
|
9
|
+
/**
|
|
10
|
+
* Enter observer introduces the {@link module:engine/view/document~Document#event:enter `Document#enter`} event.
|
|
11
|
+
*/
|
|
12
|
+
export default class EnterObserver extends Observer {
|
|
13
|
+
/**
|
|
14
|
+
* @inheritDoc
|
|
15
|
+
*/
|
|
16
|
+
constructor(view: View);
|
|
17
|
+
/**
|
|
18
|
+
* @inheritDoc
|
|
19
|
+
*/
|
|
20
|
+
observe(): void;
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
stopObserving(): void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Fired when the user presses the <kbd>Enter</kbd> key.
|
|
28
|
+
*
|
|
29
|
+
* Note: This event is fired by the {@link module:enter/enterobserver~EnterObserver observer}
|
|
30
|
+
* (usually registered by the {@link module:enter/enter~Enter Enter feature} and
|
|
31
|
+
* {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature}).
|
|
32
|
+
*
|
|
33
|
+
* @eventName module:engine/view/document~Document#enter
|
|
34
|
+
*/
|
|
35
|
+
export type ViewDocumentEnterEvent = BubblingEvent<{
|
|
36
|
+
name: 'enter';
|
|
37
|
+
args: [EnterEventData];
|
|
38
|
+
}>;
|
|
39
|
+
export interface EnterEventData extends DomEventData<InputEvent> {
|
|
40
|
+
/**
|
|
41
|
+
* Whether it is a soft enter (<kbd>Shift</kbd>+<kbd>Enter</kbd>) or a hard enter (<kbd>Enter</kbd>).
|
|
42
|
+
*/
|
|
43
|
+
isSoft: boolean;
|
|
44
|
+
}
|