@ckeditor/ckeditor5-enter 35.3.2 → 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 enter feature** – https://github.com/ckeditor/ckeditor5-enter <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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-enter",
3
- "version": "35.3.2",
3
+ "version": "36.0.0",
4
4
  "description": "Enter feature for CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -12,17 +12,17 @@
12
12
  ],
13
13
  "main": "src/index.js",
14
14
  "dependencies": {
15
- "@ckeditor/ckeditor5-core": "^35.3.2",
16
- "@ckeditor/ckeditor5-engine": "^35.3.2"
15
+ "@ckeditor/ckeditor5-core": "^36.0.0",
16
+ "@ckeditor/ckeditor5-engine": "^36.0.0"
17
17
  },
18
18
  "devDependencies": {
19
- "@ckeditor/ckeditor5-basic-styles": "^35.3.2",
20
- "@ckeditor/ckeditor5-editor-classic": "^35.3.2",
21
- "@ckeditor/ckeditor5-heading": "^35.3.2",
22
- "@ckeditor/ckeditor5-link": "^35.3.2",
23
- "@ckeditor/ckeditor5-paragraph": "^35.3.2",
24
- "@ckeditor/ckeditor5-typing": "^35.3.2",
25
- "@ckeditor/ckeditor5-undo": "^35.3.2",
19
+ "@ckeditor/ckeditor5-basic-styles": "^36.0.0",
20
+ "@ckeditor/ckeditor5-editor-classic": "^36.0.0",
21
+ "@ckeditor/ckeditor5-heading": "^36.0.0",
22
+ "@ckeditor/ckeditor5-link": "^36.0.0",
23
+ "@ckeditor/ckeditor5-paragraph": "^36.0.0",
24
+ "@ckeditor/ckeditor5-typing": "^36.0.0",
25
+ "@ckeditor/ckeditor5-undo": "^36.0.0",
26
26
  "typescript": "^4.8.4",
27
27
  "webpack": "^5.58.1",
28
28
  "webpack-cli": "^4.9.0"
package/src/enter.js CHANGED
@@ -1,15 +1,15 @@
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
5
  /**
6
6
  * @module enter/enter
7
7
  */
8
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
8
+ import { Plugin } from '@ckeditor/ckeditor5-core';
9
9
  import EnterCommand from './entercommand';
10
10
  import EnterObserver from './enterobserver';
11
11
  /**
12
- * This plugin handles the <kbd>Enter</kbd> key (hard line break) in the editor.
12
+ * This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
13
13
  *
14
14
  * See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
15
15
  *
@@ -1,14 +1,14 @@
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
5
  /**
6
6
  * @module enter/entercommand
7
7
  */
8
- import Command from '@ckeditor/ckeditor5-core/src/command';
8
+ import { Command } from '@ckeditor/ckeditor5-core';
9
9
  import { getCopyOnEnterAttributes } from './utils';
10
10
  /**
11
- * Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> key.
11
+ * Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
12
12
  *
13
13
  * @extends module:core/command~Command
14
14
  */
@@ -17,60 +17,75 @@ export default class EnterCommand extends Command {
17
17
  * @inheritDoc
18
18
  */
19
19
  execute() {
20
- const model = this.editor.model;
21
- const doc = model.document;
22
- model.change(writer => {
23
- enterBlock(this.editor.model, writer, doc.selection, model.schema);
20
+ this.editor.model.change(writer => {
21
+ this.enterBlock(writer);
24
22
  this.fire('afterExecute', { writer });
25
23
  });
26
24
  }
27
- }
28
- // Creates a new block in the way that the <kbd>Enter</kbd> key is expected to work.
29
- //
30
- // @param {module:engine/model~Model} model
31
- // @param {module:engine/model/writer~Writer} writer
32
- // @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
33
- // Selection on which the action should be performed.
34
- // @param {module:engine/model/schema~Schema} schema
35
- function enterBlock(model, writer, selection, schema) {
36
- const isSelectionEmpty = selection.isCollapsed;
37
- const range = selection.getFirstRange();
38
- const startElement = range.start.parent;
39
- const endElement = range.end.parent;
40
- // Don't touch the roots and other limit elements.
41
- if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
42
- // Delete the selected content but only if inside a single limit element.
43
- // Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
44
- // This is an edge case and it's hard to tell what should actually happen because such a selection
45
- // is not entirely valid.
46
- if (!isSelectionEmpty && startElement == endElement) {
47
- model.deleteContent(selection);
48
- }
49
- return;
50
- }
51
- if (isSelectionEmpty) {
52
- const attributesToCopy = getCopyOnEnterAttributes(writer.model.schema, selection.getAttributes());
53
- splitBlock(writer, range.start);
54
- writer.setSelectionAttribute(attributesToCopy);
55
- }
56
- else {
57
- const leaveUnmerged = !(range.start.isAtStart && range.end.isAtEnd);
58
- const isContainedWithinOneElement = (startElement == endElement);
59
- model.deleteContent(selection, { leaveUnmerged });
60
- if (leaveUnmerged) {
61
- // Partially selected elements.
62
- //
63
- // <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
64
- if (isContainedWithinOneElement) {
65
- splitBlock(writer, selection.focus);
25
+ /**
26
+ * Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
27
+ *
28
+ * <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
29
+ * <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
30
+ * <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
31
+ *
32
+ * In some cases, the split will not happen:
33
+ *
34
+ * // The selection parent is a limit element:
35
+ * <figcaption>A[bc]d</figcaption> -> <figcaption>A[]d</figcaption>
36
+ *
37
+ * // The selection spans over multiple elements:
38
+ * <h>x[x</h><p>y]y<p> -> <h>x</h><p>[]y</p>
39
+ *
40
+ * @param writer Writer to use when performing the enter action.
41
+ * @returns `true` if a block was split, `false` otherwise.
42
+ */
43
+ enterBlock(writer) {
44
+ const model = this.editor.model;
45
+ const selection = model.document.selection;
46
+ const schema = model.schema;
47
+ const isSelectionEmpty = selection.isCollapsed;
48
+ const range = selection.getFirstRange();
49
+ const startElement = range.start.parent;
50
+ const endElement = range.end.parent;
51
+ // Don't touch the roots and other limit elements.
52
+ if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
53
+ // Delete the selected content but only if inside a single limit element.
54
+ // Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
55
+ // This is an edge case and it's hard to tell what should actually happen because such a selection
56
+ // is not entirely valid.
57
+ if (!isSelectionEmpty && startElement == endElement) {
58
+ model.deleteContent(selection);
66
59
  }
67
- // Selection over multiple elements.
68
- //
69
- // <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
70
- else {
71
- writer.setSelection(endElement, 0);
60
+ return false;
61
+ }
62
+ if (isSelectionEmpty) {
63
+ const attributesToCopy = getCopyOnEnterAttributes(writer.model.schema, selection.getAttributes());
64
+ splitBlock(writer, range.start);
65
+ writer.setSelectionAttribute(attributesToCopy);
66
+ return true;
67
+ }
68
+ else {
69
+ const leaveUnmerged = !(range.start.isAtStart && range.end.isAtEnd);
70
+ const isContainedWithinOneElement = (startElement == endElement);
71
+ model.deleteContent(selection, { leaveUnmerged });
72
+ if (leaveUnmerged) {
73
+ // Partially selected elements.
74
+ //
75
+ // <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
76
+ if (isContainedWithinOneElement) {
77
+ splitBlock(writer, selection.focus);
78
+ return true;
79
+ }
80
+ // Selection over multiple elements.
81
+ //
82
+ // <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
83
+ else {
84
+ writer.setSelection(endElement, 0);
85
+ }
72
86
  }
73
87
  }
88
+ return false;
74
89
  }
75
90
  }
76
91
  function splitBlock(writer, splitPos) {
@@ -1,13 +1,11 @@
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
5
  /**
6
6
  * @module enter/enterobserver
7
7
  */
8
- import Observer from '@ckeditor/ckeditor5-engine/src/view/observer/observer';
9
- import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
10
- import BubblingEventInfo from '@ckeditor/ckeditor5-engine/src/view/observer/bubblingeventinfo';
8
+ import { Observer, DomEventData, BubblingEventInfo } from '@ckeditor/ckeditor5-engine';
11
9
  const ENTER_EVENT_TYPES = {
12
10
  insertParagraph: { isSoft: false },
13
11
  insertLineBreak: { isSoft: true }
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
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
5
  /**
package/src/shiftenter.js CHANGED
@@ -1,5 +1,5 @@
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
5
  /**
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import ShiftEnterCommand from './shiftentercommand';
9
9
  import EnterObserver from './enterobserver';
10
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
10
+ import { Plugin } from '@ckeditor/ckeditor5-core';
11
11
  /**
12
12
  * This plugin handles the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke (soft line break) in the editor.
13
13
  *
@@ -1,11 +1,11 @@
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
5
  /**
6
6
  * @module enter/shiftentercommand
7
7
  */
8
- import Command from '@ckeditor/ckeditor5-core/src/command';
8
+ import { Command } from '@ckeditor/ckeditor5-core';
9
9
  import { getCopyOnEnterAttributes } from './utils';
10
10
  /**
11
11
  * ShiftEnter command. It is used by the {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature} to handle
package/src/utils.js CHANGED
@@ -1,9 +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
5
  /**
6
- * Returns attributes that should be preserved on the enter key.
6
+ * Returns attributes that should be preserved on the enter keystroke.
7
7
  *
8
8
  * Filtering is realized based on `copyOnEnter` attribute property. Read more about attribute properties
9
9
  * {@link module:engine/model/schema~Schema#setAttributeProperties here}.