@ckeditor/ckeditor5-enter 0.0.0-internal-20241017.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.
Files changed (202) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +17 -0
  3. package/README.md +22 -0
  4. package/dist/augmentation.d.ts +19 -0
  5. package/dist/enter.d.ts +30 -0
  6. package/dist/entercommand.d.ts +56 -0
  7. package/dist/enterobserver.d.ts +48 -0
  8. package/dist/index-content.css +4 -0
  9. package/dist/index-editor.css +4 -0
  10. package/dist/index.css +4 -0
  11. package/dist/index.d.ts +17 -0
  12. package/dist/index.js +382 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/shiftenter.d.ts +27 -0
  15. package/dist/shiftentercommand.d.ts +38 -0
  16. package/dist/translations/ar.d.ts +8 -0
  17. package/dist/translations/ar.js +5 -0
  18. package/dist/translations/ar.umd.js +11 -0
  19. package/dist/translations/bg.d.ts +8 -0
  20. package/dist/translations/bg.js +5 -0
  21. package/dist/translations/bg.umd.js +11 -0
  22. package/dist/translations/bn.d.ts +8 -0
  23. package/dist/translations/bn.js +5 -0
  24. package/dist/translations/bn.umd.js +11 -0
  25. package/dist/translations/ca.d.ts +8 -0
  26. package/dist/translations/ca.js +5 -0
  27. package/dist/translations/ca.umd.js +11 -0
  28. package/dist/translations/cs.d.ts +8 -0
  29. package/dist/translations/cs.js +5 -0
  30. package/dist/translations/cs.umd.js +11 -0
  31. package/dist/translations/da.d.ts +8 -0
  32. package/dist/translations/da.js +5 -0
  33. package/dist/translations/da.umd.js +11 -0
  34. package/dist/translations/de.d.ts +8 -0
  35. package/dist/translations/de.js +5 -0
  36. package/dist/translations/de.umd.js +11 -0
  37. package/dist/translations/el.d.ts +8 -0
  38. package/dist/translations/el.js +5 -0
  39. package/dist/translations/el.umd.js +11 -0
  40. package/dist/translations/en.d.ts +8 -0
  41. package/dist/translations/en.js +5 -0
  42. package/dist/translations/en.umd.js +11 -0
  43. package/dist/translations/es.d.ts +8 -0
  44. package/dist/translations/es.js +5 -0
  45. package/dist/translations/es.umd.js +11 -0
  46. package/dist/translations/et.d.ts +8 -0
  47. package/dist/translations/et.js +5 -0
  48. package/dist/translations/et.umd.js +11 -0
  49. package/dist/translations/fi.d.ts +8 -0
  50. package/dist/translations/fi.js +5 -0
  51. package/dist/translations/fi.umd.js +11 -0
  52. package/dist/translations/fr.d.ts +8 -0
  53. package/dist/translations/fr.js +5 -0
  54. package/dist/translations/fr.umd.js +11 -0
  55. package/dist/translations/gl.d.ts +8 -0
  56. package/dist/translations/gl.js +5 -0
  57. package/dist/translations/gl.umd.js +11 -0
  58. package/dist/translations/he.d.ts +8 -0
  59. package/dist/translations/he.js +5 -0
  60. package/dist/translations/he.umd.js +11 -0
  61. package/dist/translations/hi.d.ts +8 -0
  62. package/dist/translations/hi.js +5 -0
  63. package/dist/translations/hi.umd.js +11 -0
  64. package/dist/translations/hr.d.ts +8 -0
  65. package/dist/translations/hr.js +5 -0
  66. package/dist/translations/hr.umd.js +11 -0
  67. package/dist/translations/hu.d.ts +8 -0
  68. package/dist/translations/hu.js +5 -0
  69. package/dist/translations/hu.umd.js +11 -0
  70. package/dist/translations/id.d.ts +8 -0
  71. package/dist/translations/id.js +5 -0
  72. package/dist/translations/id.umd.js +11 -0
  73. package/dist/translations/it.d.ts +8 -0
  74. package/dist/translations/it.js +5 -0
  75. package/dist/translations/it.umd.js +11 -0
  76. package/dist/translations/ja.d.ts +8 -0
  77. package/dist/translations/ja.js +5 -0
  78. package/dist/translations/ja.umd.js +11 -0
  79. package/dist/translations/ko.d.ts +8 -0
  80. package/dist/translations/ko.js +5 -0
  81. package/dist/translations/ko.umd.js +11 -0
  82. package/dist/translations/lt.d.ts +8 -0
  83. package/dist/translations/lt.js +5 -0
  84. package/dist/translations/lt.umd.js +11 -0
  85. package/dist/translations/lv.d.ts +8 -0
  86. package/dist/translations/lv.js +5 -0
  87. package/dist/translations/lv.umd.js +11 -0
  88. package/dist/translations/ms.d.ts +8 -0
  89. package/dist/translations/ms.js +5 -0
  90. package/dist/translations/ms.umd.js +11 -0
  91. package/dist/translations/nl.d.ts +8 -0
  92. package/dist/translations/nl.js +5 -0
  93. package/dist/translations/nl.umd.js +11 -0
  94. package/dist/translations/no.d.ts +8 -0
  95. package/dist/translations/no.js +5 -0
  96. package/dist/translations/no.umd.js +11 -0
  97. package/dist/translations/pl.d.ts +8 -0
  98. package/dist/translations/pl.js +5 -0
  99. package/dist/translations/pl.umd.js +11 -0
  100. package/dist/translations/pt-br.d.ts +8 -0
  101. package/dist/translations/pt-br.js +5 -0
  102. package/dist/translations/pt-br.umd.js +11 -0
  103. package/dist/translations/pt.d.ts +8 -0
  104. package/dist/translations/pt.js +5 -0
  105. package/dist/translations/pt.umd.js +11 -0
  106. package/dist/translations/ro.d.ts +8 -0
  107. package/dist/translations/ro.js +5 -0
  108. package/dist/translations/ro.umd.js +11 -0
  109. package/dist/translations/ru.d.ts +8 -0
  110. package/dist/translations/ru.js +5 -0
  111. package/dist/translations/ru.umd.js +11 -0
  112. package/dist/translations/sk.d.ts +8 -0
  113. package/dist/translations/sk.js +5 -0
  114. package/dist/translations/sk.umd.js +11 -0
  115. package/dist/translations/sr-latn.d.ts +8 -0
  116. package/dist/translations/sr-latn.js +5 -0
  117. package/dist/translations/sr-latn.umd.js +11 -0
  118. package/dist/translations/sr.d.ts +8 -0
  119. package/dist/translations/sr.js +5 -0
  120. package/dist/translations/sr.umd.js +11 -0
  121. package/dist/translations/sv.d.ts +8 -0
  122. package/dist/translations/sv.js +5 -0
  123. package/dist/translations/sv.umd.js +11 -0
  124. package/dist/translations/th.d.ts +8 -0
  125. package/dist/translations/th.js +5 -0
  126. package/dist/translations/th.umd.js +11 -0
  127. package/dist/translations/tr.d.ts +8 -0
  128. package/dist/translations/tr.js +5 -0
  129. package/dist/translations/tr.umd.js +11 -0
  130. package/dist/translations/uk.d.ts +8 -0
  131. package/dist/translations/uk.js +5 -0
  132. package/dist/translations/uk.umd.js +11 -0
  133. package/dist/translations/vi.d.ts +8 -0
  134. package/dist/translations/vi.js +5 -0
  135. package/dist/translations/vi.umd.js +11 -0
  136. package/dist/translations/zh-cn.d.ts +8 -0
  137. package/dist/translations/zh-cn.js +5 -0
  138. package/dist/translations/zh-cn.umd.js +11 -0
  139. package/dist/translations/zh.d.ts +8 -0
  140. package/dist/translations/zh.js +5 -0
  141. package/dist/translations/zh.umd.js +11 -0
  142. package/dist/utils.d.ts +22 -0
  143. package/lang/contexts.json +4 -0
  144. package/lang/translations/ar.po +26 -0
  145. package/lang/translations/bg.po +26 -0
  146. package/lang/translations/bn.po +26 -0
  147. package/lang/translations/ca.po +26 -0
  148. package/lang/translations/cs.po +26 -0
  149. package/lang/translations/da.po +26 -0
  150. package/lang/translations/de.po +26 -0
  151. package/lang/translations/el.po +26 -0
  152. package/lang/translations/en.po +26 -0
  153. package/lang/translations/es.po +26 -0
  154. package/lang/translations/et.po +26 -0
  155. package/lang/translations/fi.po +26 -0
  156. package/lang/translations/fr.po +26 -0
  157. package/lang/translations/gl.po +26 -0
  158. package/lang/translations/he.po +26 -0
  159. package/lang/translations/hi.po +26 -0
  160. package/lang/translations/hr.po +26 -0
  161. package/lang/translations/hu.po +26 -0
  162. package/lang/translations/id.po +26 -0
  163. package/lang/translations/it.po +26 -0
  164. package/lang/translations/ja.po +26 -0
  165. package/lang/translations/ko.po +26 -0
  166. package/lang/translations/lt.po +26 -0
  167. package/lang/translations/lv.po +26 -0
  168. package/lang/translations/ms.po +26 -0
  169. package/lang/translations/nl.po +26 -0
  170. package/lang/translations/no.po +26 -0
  171. package/lang/translations/pl.po +26 -0
  172. package/lang/translations/pt-br.po +26 -0
  173. package/lang/translations/pt.po +26 -0
  174. package/lang/translations/ro.po +26 -0
  175. package/lang/translations/ru.po +26 -0
  176. package/lang/translations/sk.po +26 -0
  177. package/lang/translations/sr-latn.po +26 -0
  178. package/lang/translations/sr.po +26 -0
  179. package/lang/translations/sv.po +26 -0
  180. package/lang/translations/th.po +26 -0
  181. package/lang/translations/tr.po +26 -0
  182. package/lang/translations/uk.po +26 -0
  183. package/lang/translations/vi.po +26 -0
  184. package/lang/translations/zh-cn.po +26 -0
  185. package/lang/translations/zh.po +26 -0
  186. package/package.json +39 -0
  187. package/src/augmentation.d.ts +15 -0
  188. package/src/augmentation.js +5 -0
  189. package/src/enter.d.ts +26 -0
  190. package/src/enter.js +61 -0
  191. package/src/entercommand.d.ts +52 -0
  192. package/src/entercommand.js +96 -0
  193. package/src/enterobserver.d.ts +44 -0
  194. package/src/enterobserver.js +61 -0
  195. package/src/index.d.ts +13 -0
  196. package/src/index.js +10 -0
  197. package/src/shiftenter.d.ts +23 -0
  198. package/src/shiftenter.js +79 -0
  199. package/src/shiftentercommand.d.ts +34 -0
  200. package/src/shiftentercommand.js +118 -0
  201. package/src/utils.d.ts +18 -0
  202. package/src/utils.js +20 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ Changelog
2
+ =========
3
+
4
+ All changes in the package are documented in https://github.com/ckeditor/ckeditor5/blob/master/CHANGELOG.md.
package/LICENSE.md ADDED
@@ -0,0 +1,17 @@
1
+ Software License Agreement
2
+ ==========================
3
+
4
+ **CKEditor&nbsp;5 enter feature** – https://github.com/ckeditor/ckeditor5-enter <br>
5
+ Copyright (c) 2003–2024, [CKSource Holding sp. z o.o.](https://cksource.com) All rights reserved.
6
+
7
+ Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html).
8
+
9
+ Sources of Intellectual Property Included in CKEditor
10
+ -----------------------------------------------------
11
+
12
+ Where not otherwise indicated, all CKEditor content is authored by CKSource engineers and consists of CKSource-owned intellectual property. In some specific instances, CKEditor will incorporate work done by developers outside of CKSource with their express permission.
13
+
14
+ Trademarks
15
+ ----------
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.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ CKEditor&nbsp;5 enter feature
2
+ ========================================
3
+
4
+ [![npm version](https://badge.fury.io/js/%40ckeditor%2Fckeditor5-enter.svg)](https://www.npmjs.com/package/@ckeditor/ckeditor5-enter)
5
+ [![Coverage Status](https://coveralls.io/repos/github/ckeditor/ckeditor5/badge.svg?branch=master)](https://coveralls.io/github/ckeditor/ckeditor5?branch=master)
6
+ [![Build Status](https://travis-ci.com/ckeditor/ckeditor5.svg?branch=master)](https://app.travis-ci.com/github/ckeditor/ckeditor5)
7
+
8
+ This package implements the <kbd>Enter</kbd> and <kbd>Shift</kbd>+<kbd>Enter</kbd> (soft break) support for CKEditor&nbsp;5.
9
+
10
+ ## Documentation
11
+
12
+ See the [`@ckeditor/ckeditor5-enter` package](https://ckeditor.com/docs/ckeditor5/latest/api/enter.html) page in [CKEditor&nbsp;5 documentation](https://ckeditor.com/docs/ckeditor5/latest/).
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install ckeditor5
18
+ ```
19
+
20
+ ## License
21
+
22
+ Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html). For full details about the license, please check the `LICENSE.md` file or [https://ckeditor.com/legal/ckeditor-oss-license](https://ckeditor.com/legal/ckeditor-oss-license).
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ import type { Enter, ShiftEnter, EnterCommand, ShiftEnterCommand } from './index.js';
10
+ declare module '@ckeditor/ckeditor5-core' {
11
+ interface PluginsMap {
12
+ [Enter.pluginName]: Enter;
13
+ [ShiftEnter.pluginName]: ShiftEnter;
14
+ }
15
+ interface CommandsMap {
16
+ enter: EnterCommand;
17
+ shiftEnter: ShiftEnterCommand;
18
+ }
19
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module enter/enter
11
+ */
12
+ import { Plugin } from '@ckeditor/ckeditor5-core';
13
+ /**
14
+ * This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
15
+ *
16
+ * See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
17
+ *
18
+ * For more information about this feature see the {@glink api/enter package page}.
19
+ */
20
+ export default class Enter extends Plugin {
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ static get pluginName(): "Enter";
25
+ /**
26
+ * @inheritDoc
27
+ */
28
+ static get isOfficialPlugin(): true;
29
+ init(): void;
30
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module enter/entercommand
11
+ */
12
+ import { Command } from '@ckeditor/ckeditor5-core';
13
+ import type { Writer } from '@ckeditor/ckeditor5-engine';
14
+ /**
15
+ * Enter command used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
16
+ */
17
+ export default class EnterCommand extends Command {
18
+ /**
19
+ * @inheritDoc
20
+ */
21
+ execute(): void;
22
+ /**
23
+ * Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
24
+ *
25
+ * ```
26
+ * <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
27
+ * <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
28
+ * <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
29
+ * ```
30
+ *
31
+ * In some cases, the split will not happen:
32
+ *
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
+ *
41
+ * @param writer Writer to use when performing the enter action.
42
+ * @returns Boolean indicating if the block was split.
43
+ */
44
+ enterBlock(writer: Writer): boolean;
45
+ }
46
+ /**
47
+ * Fired after the the {@link module:enter/entercommand~EnterCommand} is finished executing.
48
+ *
49
+ * @eventName ~EnterCommand#afterExecute
50
+ */
51
+ export type EnterCommandAfterExecuteEvent = {
52
+ name: 'afterExecute';
53
+ args: [{
54
+ writer: Writer;
55
+ }];
56
+ };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module enter/enterobserver
11
+ */
12
+ import { Observer, DomEventData, type EditingView, type BubblingEvent } from '@ckeditor/ckeditor5-engine';
13
+ /**
14
+ * Enter observer introduces the {@link module:engine/view/document~Document#event:enter `Document#enter`} event.
15
+ */
16
+ export default class EnterObserver extends Observer {
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ constructor(view: EditingView);
21
+ /**
22
+ * @inheritDoc
23
+ */
24
+ observe(): void;
25
+ /**
26
+ * @inheritDoc
27
+ */
28
+ stopObserving(): void;
29
+ }
30
+ /**
31
+ * Fired when the user presses the <kbd>Enter</kbd> key.
32
+ *
33
+ * Note: This event is fired by the {@link module:enter/enterobserver~EnterObserver observer}
34
+ * (usually registered by the {@link module:enter/enter~Enter Enter feature} and
35
+ * {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature}).
36
+ *
37
+ * @eventName module:engine/view/document~Document#enter
38
+ */
39
+ export type ViewDocumentEnterEvent = BubblingEvent<{
40
+ name: 'enter';
41
+ args: [EnterEventData];
42
+ }>;
43
+ export interface EnterEventData extends DomEventData<InputEvent> {
44
+ /**
45
+ * Whether it is a soft enter (<kbd>Shift</kbd>+<kbd>Enter</kbd>) or a hard enter (<kbd>Enter</kbd>).
46
+ */
47
+ isSoft: boolean;
48
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
package/dist/index.css ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module enter
11
+ */
12
+ export { default as Enter } from './enter.js';
13
+ export { default as ShiftEnter } from './shiftenter.js';
14
+ export type { ViewDocumentEnterEvent } from './enterobserver.js';
15
+ export type { default as EnterCommand, EnterCommandAfterExecuteEvent } from './entercommand.js';
16
+ export type { default as ShiftEnterCommand } from './shiftentercommand.js';
17
+ import './augmentation.js';
package/dist/index.js ADDED
@@ -0,0 +1,382 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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 { Command, Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
6
+ import { Observer, BubblingEventInfo, DomEventData } from '@ckeditor/ckeditor5-engine/dist/index.js';
7
+ import { env } from '@ckeditor/ckeditor5-utils/dist/index.js';
8
+
9
+ /**
10
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
11
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
12
+ */ /**
13
+ * @module enter/utils
14
+ */ /**
15
+ * Returns attributes that should be preserved on the enter keystroke.
16
+ *
17
+ * Filtering is realized based on `copyOnEnter` attribute property. Read more about attribute properties
18
+ * {@link module:engine/model/schema~Schema#setAttributeProperties here}.
19
+ *
20
+ * @param schema Model's schema.
21
+ * @param allAttributes Attributes to filter.
22
+ */ function* getCopyOnEnterAttributes(schema, allAttributes) {
23
+ for (const attribute of allAttributes){
24
+ if (attribute && schema.getAttributeProperties(attribute[0]).copyOnEnter) {
25
+ yield attribute;
26
+ }
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Enter command used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> keystroke.
32
+ */ class EnterCommand extends Command {
33
+ /**
34
+ * @inheritDoc
35
+ */ execute() {
36
+ this.editor.model.change((writer)=>{
37
+ this.enterBlock(writer);
38
+ this.fire('afterExecute', {
39
+ writer
40
+ });
41
+ });
42
+ }
43
+ /**
44
+ * Splits a block where the document selection is placed, in the way how the <kbd>Enter</kbd> key is expected to work:
45
+ *
46
+ * ```
47
+ * <p>Foo[]bar</p> -> <p>Foo</p><p>[]bar</p>
48
+ * <p>Foobar[]</p> -> <p>Foobar</p><p>[]</p>
49
+ * <p>Fo[ob]ar</p> -> <p>Fo</p><p>[]ar</p>
50
+ * ```
51
+ *
52
+ * In some cases, the split will not happen:
53
+ *
54
+ * ```
55
+ * // The selection parent is a limit element:
56
+ * <figcaption>A[bc]d</figcaption> -> <figcaption>A[]d</figcaption>
57
+ *
58
+ * // The selection spans over multiple elements:
59
+ * <h>x[x</h><p>y]y<p> -> <h>x</h><p>[]y</p>
60
+ * ```
61
+ *
62
+ * @param writer Writer to use when performing the enter action.
63
+ * @returns Boolean indicating if the block was split.
64
+ */ enterBlock(writer) {
65
+ const model = this.editor.model;
66
+ const selection = model.document.selection;
67
+ const schema = model.schema;
68
+ const isSelectionEmpty = selection.isCollapsed;
69
+ const range = selection.getFirstRange();
70
+ const startElement = range.start.parent;
71
+ const endElement = range.end.parent;
72
+ // Don't touch the roots and other limit elements.
73
+ if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
74
+ // Delete the selected content but only if inside a single limit element.
75
+ // Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
76
+ // This is an edge case and it's hard to tell what should actually happen because such a selection
77
+ // is not entirely valid.
78
+ if (!isSelectionEmpty && startElement == endElement) {
79
+ model.deleteContent(selection);
80
+ }
81
+ return false;
82
+ }
83
+ if (isSelectionEmpty) {
84
+ const attributesToCopy = getCopyOnEnterAttributes(writer.model.schema, selection.getAttributes());
85
+ splitBlock(writer, range.start);
86
+ writer.setSelectionAttribute(attributesToCopy);
87
+ return true;
88
+ } else {
89
+ const leaveUnmerged = !(range.start.isAtStart && range.end.isAtEnd);
90
+ const isContainedWithinOneElement = startElement == endElement;
91
+ model.deleteContent(selection, {
92
+ leaveUnmerged
93
+ });
94
+ if (leaveUnmerged) {
95
+ // Partially selected elements.
96
+ //
97
+ // <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
98
+ if (isContainedWithinOneElement) {
99
+ splitBlock(writer, selection.focus);
100
+ return true;
101
+ } else {
102
+ writer.setSelection(endElement, 0);
103
+ }
104
+ }
105
+ }
106
+ return false;
107
+ }
108
+ }
109
+ function splitBlock(writer, splitPos) {
110
+ writer.split(splitPos);
111
+ writer.setSelection(splitPos.parent.nextSibling, 0);
112
+ }
113
+
114
+ const ENTER_EVENT_TYPES = {
115
+ insertParagraph: {
116
+ isSoft: false
117
+ },
118
+ insertLineBreak: {
119
+ isSoft: true
120
+ }
121
+ };
122
+ /**
123
+ * Enter observer introduces the {@link module:engine/view/document~Document#event:enter `Document#enter`} event.
124
+ */ class EnterObserver extends Observer {
125
+ /**
126
+ * @inheritDoc
127
+ */ constructor(view){
128
+ super(view);
129
+ const doc = this.document;
130
+ let shiftPressed = false;
131
+ doc.on('keydown', (evt, data)=>{
132
+ shiftPressed = data.shiftKey;
133
+ });
134
+ doc.on('beforeinput', (evt, data)=>{
135
+ if (!this.isEnabled) {
136
+ return;
137
+ }
138
+ let inputType = data.inputType;
139
+ // See https://github.com/ckeditor/ckeditor5/issues/13321.
140
+ if (env.isSafari && shiftPressed && inputType == 'insertParagraph') {
141
+ inputType = 'insertLineBreak';
142
+ }
143
+ const domEvent = data.domEvent;
144
+ const enterEventSpec = ENTER_EVENT_TYPES[inputType];
145
+ if (!enterEventSpec) {
146
+ return;
147
+ }
148
+ const event = new BubblingEventInfo(doc, 'enter', data.targetRanges[0]);
149
+ doc.fire(event, new DomEventData(view, domEvent, {
150
+ isSoft: enterEventSpec.isSoft
151
+ }));
152
+ // Stop `beforeinput` event if `enter` event was stopped.
153
+ // https://github.com/ckeditor/ckeditor5/issues/753
154
+ if (event.stop.called) {
155
+ evt.stop();
156
+ }
157
+ });
158
+ }
159
+ /**
160
+ * @inheritDoc
161
+ */ observe() {}
162
+ /**
163
+ * @inheritDoc
164
+ */ stopObserving() {}
165
+ }
166
+
167
+ /**
168
+ * This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
169
+ *
170
+ * See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
171
+ *
172
+ * For more information about this feature see the {@glink api/enter package page}.
173
+ */ class Enter extends Plugin {
174
+ /**
175
+ * @inheritDoc
176
+ */ static get pluginName() {
177
+ return 'Enter';
178
+ }
179
+ /**
180
+ * @inheritDoc
181
+ */ static get isOfficialPlugin() {
182
+ return true;
183
+ }
184
+ init() {
185
+ const editor = this.editor;
186
+ const view = editor.editing.view;
187
+ const viewDocument = view.document;
188
+ const t = this.editor.t;
189
+ view.addObserver(EnterObserver);
190
+ editor.commands.add('enter', new EnterCommand(editor));
191
+ this.listenTo(viewDocument, 'enter', (evt, data)=>{
192
+ // When not in composition, we handle the action, so prevent the default one.
193
+ // When in composition, it's the browser who modify the DOM (renderer is disabled).
194
+ if (!viewDocument.isComposing) {
195
+ data.preventDefault();
196
+ }
197
+ // The soft enter key is handled by the ShiftEnter plugin.
198
+ if (data.isSoft) {
199
+ return;
200
+ }
201
+ editor.execute('enter');
202
+ view.scrollToTheSelection();
203
+ }, {
204
+ priority: 'low'
205
+ });
206
+ // Add the information about the keystroke to the accessibility database.
207
+ editor.accessibility.addKeystrokeInfos({
208
+ keystrokes: [
209
+ {
210
+ label: t('Insert a hard break (a new paragraph)'),
211
+ keystroke: 'Enter'
212
+ }
213
+ ]
214
+ });
215
+ }
216
+ }
217
+
218
+ /**
219
+ * ShiftEnter command. It is used by the {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature} to handle
220
+ * the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke.
221
+ */ class ShiftEnterCommand extends Command {
222
+ /**
223
+ * @inheritDoc
224
+ */ execute() {
225
+ const model = this.editor.model;
226
+ const doc = model.document;
227
+ model.change((writer)=>{
228
+ softBreakAction(model, writer, doc.selection);
229
+ this.fire('afterExecute', {
230
+ writer
231
+ });
232
+ });
233
+ }
234
+ /**
235
+ * @inheritDoc
236
+ */ refresh() {
237
+ const model = this.editor.model;
238
+ const doc = model.document;
239
+ this.isEnabled = isEnabled(model.schema, doc.selection);
240
+ }
241
+ }
242
+ /**
243
+ * Checks whether the ShiftEnter command should be enabled in the specified selection.
244
+ */ function isEnabled(schema, selection) {
245
+ // At this moment it is okay to support single range selections only.
246
+ // But in the future we may need to change that.
247
+ if (selection.rangeCount > 1) {
248
+ return false;
249
+ }
250
+ const anchorPos = selection.anchor;
251
+ // Check whether the break element can be inserted in the current selection anchor.
252
+ if (!anchorPos || !schema.checkChild(anchorPos, 'softBreak')) {
253
+ return false;
254
+ }
255
+ const range = selection.getFirstRange();
256
+ const startElement = range.start.parent;
257
+ const endElement = range.end.parent;
258
+ // Do not modify the content if selection is cross-limit elements.
259
+ if ((isInsideLimitElement(startElement, schema) || isInsideLimitElement(endElement, schema)) && startElement !== endElement) {
260
+ return false;
261
+ }
262
+ return true;
263
+ }
264
+ /**
265
+ * Creates a break in the way that the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke is expected to work.
266
+ */ function softBreakAction(model, writer, selection) {
267
+ const isSelectionEmpty = selection.isCollapsed;
268
+ const range = selection.getFirstRange();
269
+ const startElement = range.start.parent;
270
+ const endElement = range.end.parent;
271
+ const isContainedWithinOneElement = startElement == endElement;
272
+ if (isSelectionEmpty) {
273
+ const attributesToCopy = getCopyOnEnterAttributes(model.schema, selection.getAttributes());
274
+ insertBreak(model, writer, range.end);
275
+ writer.removeSelectionAttribute(selection.getAttributeKeys());
276
+ writer.setSelectionAttribute(attributesToCopy);
277
+ } else {
278
+ const leaveUnmerged = !(range.start.isAtStart && range.end.isAtEnd);
279
+ model.deleteContent(selection, {
280
+ leaveUnmerged
281
+ });
282
+ // Selection within one element:
283
+ //
284
+ // <h>x[xx]x</h> -> <h>x^x</h> -> <h>x<br>^x</h>
285
+ if (isContainedWithinOneElement) {
286
+ insertBreak(model, writer, selection.focus);
287
+ } else {
288
+ // Move the selection to the 2nd element (last step of the example above).
289
+ if (leaveUnmerged) {
290
+ writer.setSelection(endElement, 0);
291
+ }
292
+ }
293
+ }
294
+ }
295
+ function insertBreak(model, writer, position) {
296
+ const breakLineElement = writer.createElement('softBreak');
297
+ model.insertContent(breakLineElement, position);
298
+ writer.setSelection(breakLineElement, 'after');
299
+ }
300
+ /**
301
+ * Checks whether the specified `element` is a child of the limit element.
302
+ *
303
+ * Checking whether the `<p>` element is inside a limit element:
304
+ * - `<$root><p>Text.</p></$root> => false`
305
+ * - `<$root><limitElement><p>Text</p></limitElement></$root> => true`
306
+ */ function isInsideLimitElement(element, schema) {
307
+ // `$root` is a limit element but in this case is an invalid element.
308
+ if (element.is('rootElement')) {
309
+ return false;
310
+ }
311
+ return schema.isLimit(element) || isInsideLimitElement(element.parent, schema);
312
+ }
313
+
314
+ /**
315
+ * This plugin handles the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke (soft line break) in the editor.
316
+ *
317
+ * See also the {@link module:enter/enter~Enter} plugin.
318
+ *
319
+ * For more information about this feature see the {@glink api/enter package page}.
320
+ */ class ShiftEnter extends Plugin {
321
+ /**
322
+ * @inheritDoc
323
+ */ static get pluginName() {
324
+ return 'ShiftEnter';
325
+ }
326
+ /**
327
+ * @inheritDoc
328
+ */ static get isOfficialPlugin() {
329
+ return true;
330
+ }
331
+ init() {
332
+ const editor = this.editor;
333
+ const schema = editor.model.schema;
334
+ const conversion = editor.conversion;
335
+ const view = editor.editing.view;
336
+ const viewDocument = view.document;
337
+ const t = this.editor.t;
338
+ // Configure the schema.
339
+ schema.register('softBreak', {
340
+ allowWhere: '$text',
341
+ isInline: true
342
+ });
343
+ // Configure converters.
344
+ conversion.for('upcast').elementToElement({
345
+ model: 'softBreak',
346
+ view: 'br'
347
+ });
348
+ conversion.for('downcast').elementToElement({
349
+ model: 'softBreak',
350
+ view: (modelElement, { writer })=>writer.createEmptyElement('br')
351
+ });
352
+ view.addObserver(EnterObserver);
353
+ editor.commands.add('shiftEnter', new ShiftEnterCommand(editor));
354
+ this.listenTo(viewDocument, 'enter', (evt, data)=>{
355
+ // When not in composition, we handle the action, so prevent the default one.
356
+ // When in composition, it's the browser who modify the DOM (renderer is disabled).
357
+ if (!viewDocument.isComposing) {
358
+ data.preventDefault();
359
+ }
360
+ // The hard enter key is handled by the Enter plugin.
361
+ if (!data.isSoft) {
362
+ return;
363
+ }
364
+ editor.execute('shiftEnter');
365
+ view.scrollToTheSelection();
366
+ }, {
367
+ priority: 'low'
368
+ });
369
+ // Add the information about the keystroke to the accessibility database.
370
+ editor.accessibility.addKeystrokeInfos({
371
+ keystrokes: [
372
+ {
373
+ label: t('Insert a soft break (a <code>&lt;br&gt;</code> element)'),
374
+ keystroke: 'Shift+Enter'
375
+ }
376
+ ]
377
+ });
378
+ }
379
+ }
380
+
381
+ export { Enter, ShiftEnter };
382
+ //# sourceMappingURL=index.js.map