@ckeditor/ckeditor5-engine 47.6.1 → 48.0.0-alpha.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 (258) hide show
  1. package/LICENSE.md +1 -1
  2. package/{src → dist}/engineconfig.d.ts +6 -15
  3. package/dist/index-editor.css +38 -15
  4. package/dist/index.css +37 -37
  5. package/dist/index.css.map +1 -1
  6. package/{src → dist}/index.d.ts +0 -1
  7. package/dist/index.js +588 -94
  8. package/dist/index.js.map +1 -1
  9. package/{src → dist}/model/model.d.ts +10 -4
  10. package/{src → dist}/model/selection.d.ts +1 -1
  11. package/{src → dist}/view/downcastwriter.d.ts +3 -2
  12. package/{src → dist}/view/element.d.ts +2 -2
  13. package/{src → dist}/view/matcher.d.ts +4 -2
  14. package/dist/view/styles/background.d.ts +18 -0
  15. package/{src → dist}/view/styles/border.d.ts +0 -12
  16. package/{src → dist}/view/styles/margin.d.ts +0 -13
  17. package/{src → dist}/view/styles/padding.d.ts +0 -13
  18. package/{src → dist}/view/styles/utils.d.ts +12 -0
  19. package/package.json +20 -39
  20. package/src/controller/datacontroller.js +0 -522
  21. package/src/controller/editingcontroller.js +0 -181
  22. package/src/conversion/conversion.js +0 -606
  23. package/src/conversion/conversionhelpers.js +0 -33
  24. package/src/conversion/downcastdispatcher.js +0 -563
  25. package/src/conversion/downcasthelpers.js +0 -2160
  26. package/src/conversion/mapper.js +0 -1050
  27. package/src/conversion/modelconsumable.js +0 -331
  28. package/src/conversion/upcastdispatcher.js +0 -470
  29. package/src/conversion/upcasthelpers.js +0 -952
  30. package/src/conversion/viewconsumable.js +0 -541
  31. package/src/dataprocessor/basichtmlwriter.js +0 -22
  32. package/src/dataprocessor/dataprocessor.js +0 -5
  33. package/src/dataprocessor/htmldataprocessor.js +0 -107
  34. package/src/dataprocessor/htmlwriter.js +0 -5
  35. package/src/dataprocessor/xmldataprocessor.js +0 -127
  36. package/src/dev-utils/model.js +0 -396
  37. package/src/dev-utils/operationreplayer.js +0 -116
  38. package/src/dev-utils/utils.js +0 -122
  39. package/src/dev-utils/view.js +0 -990
  40. package/src/engineconfig.js +0 -5
  41. package/src/index.js +0 -134
  42. package/src/legacyerrors.js +0 -17
  43. package/src/model/batch.js +0 -98
  44. package/src/model/differ.js +0 -1288
  45. package/src/model/document.js +0 -398
  46. package/src/model/documentfragment.js +0 -332
  47. package/src/model/documentselection.js +0 -1026
  48. package/src/model/element.js +0 -323
  49. package/src/model/history.js +0 -206
  50. package/src/model/item.js +0 -5
  51. package/src/model/liveposition.js +0 -93
  52. package/src/model/liverange.js +0 -121
  53. package/src/model/markercollection.js +0 -436
  54. package/src/model/model.js +0 -866
  55. package/src/model/node.js +0 -371
  56. package/src/model/nodelist.js +0 -244
  57. package/src/model/operation/attributeoperation.js +0 -172
  58. package/src/model/operation/detachoperation.js +0 -87
  59. package/src/model/operation/insertoperation.js +0 -153
  60. package/src/model/operation/markeroperation.js +0 -136
  61. package/src/model/operation/mergeoperation.js +0 -184
  62. package/src/model/operation/moveoperation.js +0 -179
  63. package/src/model/operation/nooperation.js +0 -48
  64. package/src/model/operation/operation.js +0 -78
  65. package/src/model/operation/operationfactory.js +0 -44
  66. package/src/model/operation/renameoperation.js +0 -128
  67. package/src/model/operation/rootattributeoperation.js +0 -173
  68. package/src/model/operation/rootoperation.js +0 -106
  69. package/src/model/operation/splitoperation.js +0 -214
  70. package/src/model/operation/transform.js +0 -2211
  71. package/src/model/operation/utils.js +0 -217
  72. package/src/model/position.js +0 -1041
  73. package/src/model/range.js +0 -880
  74. package/src/model/rootelement.js +0 -82
  75. package/src/model/schema.js +0 -1542
  76. package/src/model/selection.js +0 -814
  77. package/src/model/text.js +0 -92
  78. package/src/model/textproxy.js +0 -202
  79. package/src/model/treewalker.js +0 -313
  80. package/src/model/typecheckable.js +0 -16
  81. package/src/model/utils/autoparagraphing.js +0 -63
  82. package/src/model/utils/deletecontent.js +0 -509
  83. package/src/model/utils/getselectedcontent.js +0 -126
  84. package/src/model/utils/insertcontent.js +0 -750
  85. package/src/model/utils/insertobject.js +0 -135
  86. package/src/model/utils/modifyselection.js +0 -187
  87. package/src/model/utils/selection-post-fixer.js +0 -264
  88. package/src/model/writer.js +0 -1318
  89. package/src/view/attributeelement.js +0 -220
  90. package/src/view/containerelement.js +0 -91
  91. package/src/view/datatransfer.js +0 -106
  92. package/src/view/document.js +0 -139
  93. package/src/view/documentfragment.js +0 -251
  94. package/src/view/documentselection.js +0 -270
  95. package/src/view/domconverter.js +0 -1661
  96. package/src/view/downcastwriter.js +0 -1589
  97. package/src/view/editableelement.js +0 -74
  98. package/src/view/element.js +0 -1053
  99. package/src/view/elementdefinition.js +0 -5
  100. package/src/view/emptyelement.js +0 -83
  101. package/src/view/filler.js +0 -161
  102. package/src/view/item.js +0 -5
  103. package/src/view/matcher.js +0 -437
  104. package/src/view/node.js +0 -238
  105. package/src/view/observer/arrowkeysobserver.js +0 -40
  106. package/src/view/observer/bubblingemittermixin.js +0 -215
  107. package/src/view/observer/bubblingeventinfo.js +0 -49
  108. package/src/view/observer/clickobserver.js +0 -26
  109. package/src/view/observer/compositionobserver.js +0 -64
  110. package/src/view/observer/domeventdata.js +0 -63
  111. package/src/view/observer/domeventobserver.js +0 -81
  112. package/src/view/observer/fakeselectionobserver.js +0 -95
  113. package/src/view/observer/focusobserver.js +0 -166
  114. package/src/view/observer/inputobserver.js +0 -236
  115. package/src/view/observer/keyobserver.js +0 -36
  116. package/src/view/observer/mouseobserver.js +0 -26
  117. package/src/view/observer/mutationobserver.js +0 -219
  118. package/src/view/observer/observer.js +0 -92
  119. package/src/view/observer/pointerobserver.js +0 -26
  120. package/src/view/observer/selectionobserver.js +0 -318
  121. package/src/view/observer/tabobserver.js +0 -42
  122. package/src/view/observer/touchobserver.js +0 -26
  123. package/src/view/placeholder.js +0 -285
  124. package/src/view/position.js +0 -341
  125. package/src/view/range.js +0 -451
  126. package/src/view/rawelement.js +0 -115
  127. package/src/view/renderer.js +0 -1148
  128. package/src/view/rooteditableelement.js +0 -78
  129. package/src/view/selection.js +0 -594
  130. package/src/view/styles/background.d.ts +0 -33
  131. package/src/view/styles/background.js +0 -74
  132. package/src/view/styles/border.js +0 -316
  133. package/src/view/styles/margin.js +0 -34
  134. package/src/view/styles/padding.js +0 -34
  135. package/src/view/styles/utils.js +0 -219
  136. package/src/view/stylesmap.js +0 -941
  137. package/src/view/text.js +0 -110
  138. package/src/view/textproxy.js +0 -136
  139. package/src/view/tokenlist.js +0 -194
  140. package/src/view/treewalker.js +0 -389
  141. package/src/view/typecheckable.js +0 -19
  142. package/src/view/uielement.js +0 -194
  143. package/src/view/upcastwriter.js +0 -363
  144. package/src/view/view.js +0 -579
  145. package/theme/placeholder.css +0 -36
  146. package/theme/renderer.css +0 -9
  147. /package/{src → dist}/controller/datacontroller.d.ts +0 -0
  148. /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
  149. /package/{src → dist}/conversion/conversion.d.ts +0 -0
  150. /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
  151. /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
  152. /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
  153. /package/{src → dist}/conversion/mapper.d.ts +0 -0
  154. /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
  155. /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
  156. /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
  157. /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
  158. /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
  159. /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
  160. /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
  161. /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
  162. /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
  163. /package/{src → dist}/dev-utils/model.d.ts +0 -0
  164. /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
  165. /package/{src → dist}/dev-utils/utils.d.ts +0 -0
  166. /package/{src → dist}/dev-utils/view.d.ts +0 -0
  167. /package/{src → dist}/legacyerrors.d.ts +0 -0
  168. /package/{src → dist}/model/batch.d.ts +0 -0
  169. /package/{src → dist}/model/differ.d.ts +0 -0
  170. /package/{src → dist}/model/document.d.ts +0 -0
  171. /package/{src → dist}/model/documentfragment.d.ts +0 -0
  172. /package/{src → dist}/model/documentselection.d.ts +0 -0
  173. /package/{src → dist}/model/element.d.ts +0 -0
  174. /package/{src → dist}/model/history.d.ts +0 -0
  175. /package/{src → dist}/model/item.d.ts +0 -0
  176. /package/{src → dist}/model/liveposition.d.ts +0 -0
  177. /package/{src → dist}/model/liverange.d.ts +0 -0
  178. /package/{src → dist}/model/markercollection.d.ts +0 -0
  179. /package/{src → dist}/model/node.d.ts +0 -0
  180. /package/{src → dist}/model/nodelist.d.ts +0 -0
  181. /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
  182. /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
  183. /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
  184. /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
  185. /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
  186. /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
  187. /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
  188. /package/{src → dist}/model/operation/operation.d.ts +0 -0
  189. /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
  190. /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
  191. /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
  192. /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
  193. /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
  194. /package/{src → dist}/model/operation/transform.d.ts +0 -0
  195. /package/{src → dist}/model/operation/utils.d.ts +0 -0
  196. /package/{src → dist}/model/position.d.ts +0 -0
  197. /package/{src → dist}/model/range.d.ts +0 -0
  198. /package/{src → dist}/model/rootelement.d.ts +0 -0
  199. /package/{src → dist}/model/schema.d.ts +0 -0
  200. /package/{src → dist}/model/text.d.ts +0 -0
  201. /package/{src → dist}/model/textproxy.d.ts +0 -0
  202. /package/{src → dist}/model/treewalker.d.ts +0 -0
  203. /package/{src → dist}/model/typecheckable.d.ts +0 -0
  204. /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
  205. /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
  206. /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
  207. /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
  208. /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
  209. /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
  210. /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
  211. /package/{src → dist}/model/writer.d.ts +0 -0
  212. /package/{src → dist}/view/attributeelement.d.ts +0 -0
  213. /package/{src → dist}/view/containerelement.d.ts +0 -0
  214. /package/{src → dist}/view/datatransfer.d.ts +0 -0
  215. /package/{src → dist}/view/document.d.ts +0 -0
  216. /package/{src → dist}/view/documentfragment.d.ts +0 -0
  217. /package/{src → dist}/view/documentselection.d.ts +0 -0
  218. /package/{src → dist}/view/domconverter.d.ts +0 -0
  219. /package/{src → dist}/view/editableelement.d.ts +0 -0
  220. /package/{src → dist}/view/elementdefinition.d.ts +0 -0
  221. /package/{src → dist}/view/emptyelement.d.ts +0 -0
  222. /package/{src → dist}/view/filler.d.ts +0 -0
  223. /package/{src → dist}/view/item.d.ts +0 -0
  224. /package/{src → dist}/view/node.d.ts +0 -0
  225. /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
  226. /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
  227. /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
  228. /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
  229. /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
  230. /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
  231. /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
  232. /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
  233. /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
  234. /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
  235. /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
  236. /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
  237. /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
  238. /package/{src → dist}/view/observer/observer.d.ts +0 -0
  239. /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
  240. /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
  241. /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
  242. /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
  243. /package/{src → dist}/view/placeholder.d.ts +0 -0
  244. /package/{src → dist}/view/position.d.ts +0 -0
  245. /package/{src → dist}/view/range.d.ts +0 -0
  246. /package/{src → dist}/view/rawelement.d.ts +0 -0
  247. /package/{src → dist}/view/renderer.d.ts +0 -0
  248. /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
  249. /package/{src → dist}/view/selection.d.ts +0 -0
  250. /package/{src → dist}/view/stylesmap.d.ts +0 -0
  251. /package/{src → dist}/view/text.d.ts +0 -0
  252. /package/{src → dist}/view/textproxy.d.ts +0 -0
  253. /package/{src → dist}/view/tokenlist.d.ts +0 -0
  254. /package/{src → dist}/view/treewalker.d.ts +0 -0
  255. /package/{src → dist}/view/typecheckable.d.ts +0 -0
  256. /package/{src → dist}/view/uielement.d.ts +0 -0
  257. /package/{src → dist}/view/upcastwriter.d.ts +0 -0
  258. /package/{src → dist}/view/view.d.ts +0 -0
@@ -1,866 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module engine/model/model
7
- */
8
- import { Batch } from './batch.js';
9
- import { ModelDocument } from './document.js';
10
- import { MarkerCollection } from './markercollection.js';
11
- import { ModelPosition } from './position.js';
12
- import { ModelRange } from './range.js';
13
- import { ModelSelection } from './selection.js';
14
- import { OperationFactory } from './operation/operationfactory.js';
15
- import { ModelDocumentSelection } from './documentselection.js';
16
- import { ModelSchema } from './schema.js';
17
- import { ModelWriter } from './writer.js';
18
- import { ModelNode } from './node.js';
19
- import { autoParagraphEmptyRoots } from './utils/autoparagraphing.js';
20
- import { injectSelectionPostFixer } from './utils/selection-post-fixer.js';
21
- import { deleteContent } from './utils/deletecontent.js';
22
- import { getSelectedContent } from './utils/getselectedcontent.js';
23
- import { insertContent } from './utils/insertcontent.js';
24
- import { insertObject } from './utils/insertobject.js';
25
- import { modifySelection } from './utils/modifyselection.js';
26
- import { CKEditorError, ObservableMixin } from '@ckeditor/ckeditor5-utils';
27
- // @if CK_DEBUG_ENGINE // const { dumpTrees, initDocumentDumping } = require( '../dev-utils/utils' );
28
- // @if CK_DEBUG_ENGINE // const { OperationReplayer } = require( '../dev-utils/operationreplayer' ).default;
29
- /**
30
- * Editor's data model. Read about the model in the
31
- * {@glink framework/architecture/editing-engine engine architecture} guide.
32
- */
33
- export class Model extends /* #__PURE__ */ ObservableMixin() {
34
- /**
35
- * Model's marker collection.
36
- */
37
- markers;
38
- /**
39
- * Model's document.
40
- */
41
- document;
42
- /**
43
- * Model's schema.
44
- */
45
- schema;
46
- /**
47
- * Stores all configurations specific to editor instance.
48
- *
49
- * @internal
50
- */
51
- _config;
52
- /**
53
- * All callbacks added by {@link module:engine/model/model~Model#change} or
54
- * {@link module:engine/model/model~Model#enqueueChange} methods waiting to be executed.
55
- */
56
- _pendingChanges;
57
- /**
58
- * The last created and currently used writer instance.
59
- */
60
- _currentWriter;
61
- // @if CK_DEBUG_ENGINE // private _operationLogs: Array<string>;
62
- // @if CK_DEBUG_ENGINE // private _appliedOperations: Array<Operation>;
63
- constructor(config) {
64
- super();
65
- this.markers = new MarkerCollection();
66
- this.document = new ModelDocument(this);
67
- this.schema = new ModelSchema();
68
- this._config = config;
69
- this._pendingChanges = [];
70
- this._currentWriter = null;
71
- ['deleteContent', 'modifySelection', 'getSelectedContent', 'applyOperation']
72
- .forEach(methodName => this.decorate(methodName));
73
- // Adding operation validation with `highest` priority, so it is called before any other feature would like
74
- // to do anything with the operation. If the operation has incorrect parameters it should throw on the earliest occasion.
75
- this.on('applyOperation', (evt, args) => {
76
- const operation = args[0];
77
- operation._validate();
78
- }, { priority: 'highest' });
79
- // Register some default abstract entities.
80
- this.schema.register('$root', {
81
- isLimit: true
82
- });
83
- this.schema.register('$container', {
84
- allowIn: ['$root', '$container']
85
- });
86
- this.schema.register('$block', {
87
- allowIn: ['$root', '$container'],
88
- isBlock: true
89
- });
90
- this.schema.register('$blockObject', {
91
- allowWhere: '$block',
92
- isBlock: true,
93
- isObject: true
94
- });
95
- this.schema.register('$inlineObject', {
96
- allowWhere: '$text',
97
- allowAttributesOf: '$text',
98
- isInline: true,
99
- isObject: true
100
- });
101
- this.schema.register('$text', {
102
- allowIn: '$block',
103
- isInline: true,
104
- isContent: true
105
- });
106
- this.schema.register('$clipboardHolder', {
107
- allowContentOf: '$root',
108
- allowChildren: '$text',
109
- isLimit: true
110
- });
111
- this.schema.register('$documentFragment', {
112
- allowContentOf: '$root',
113
- allowChildren: '$text',
114
- isLimit: true
115
- });
116
- // An element needed by the `upcastElementToMarker` converter.
117
- // This element temporarily represents a marker boundary during the conversion process and is removed
118
- // at the end of the conversion. `UpcastDispatcher` or at least `Conversion` class looks like a
119
- // better place for this registration but both know nothing about `Schema`.
120
- this.schema.register('$marker');
121
- this.schema.addChildCheck(() => true, '$marker'); // Allow everywhere.
122
- injectSelectionPostFixer(this);
123
- // Post-fixer which takes care of adding empty paragraph elements to the empty roots.
124
- this.document.registerPostFixer(autoParagraphEmptyRoots);
125
- // The base implementation for "decorated" method with remapped arguments.
126
- this.on('insertContent', (evt, [content, selectable]) => {
127
- evt.return = insertContent(this, content, selectable);
128
- });
129
- // The base implementation for "decorated" method with remapped arguments.
130
- this.on('insertObject', (evt, [element, selection, options]) => {
131
- evt.return = insertObject(this, element, selection, options);
132
- });
133
- // The base implementation for "decorated" method with remapped arguments.
134
- this.on('canEditAt', evt => {
135
- const canEditAt = !this.document.isReadOnly;
136
- evt.return = canEditAt;
137
- if (!canEditAt) {
138
- // Prevent further processing if the selection is at non-editable place.
139
- evt.stop();
140
- }
141
- });
142
- // @if CK_DEBUG_ENGINE // initDocumentDumping( this.document );
143
- // @if CK_DEBUG_ENGINE // this.on( 'applyOperation', () => {
144
- // @if CK_DEBUG_ENGINE // dumpTrees( this.document, this.document.version );
145
- // @if CK_DEBUG_ENGINE // }, { priority: 'lowest' } );
146
- // @if CK_DEBUG_ENGINE // this._operationLogs = [];
147
- // @if CK_DEBUG_ENGINE // this._appliedOperations = [];
148
- }
149
- /**
150
- * The `change()` method is the primary way of changing the model. You should use it to modify all document nodes
151
- * (including detached nodes – i.e. nodes not added to the {@link module:engine/model/model~Model#document model document}),
152
- * the {@link module:engine/model/document~ModelDocument#selection document's selection}, and
153
- * {@link module:engine/model/model~Model#markers model markers}.
154
- *
155
- * ```ts
156
- * model.change( writer => {
157
- * writer.insertText( 'foo', paragraph, 'end' );
158
- * } );
159
- * ```
160
- *
161
- * All changes inside the change block use the same {@link module:engine/model/batch~Batch} so they are combined
162
- * into a single undo step.
163
- *
164
- * ```ts
165
- * model.change( writer => {
166
- * writer.insertText( 'foo', paragraph, 'end' ); // foo.
167
- *
168
- * model.change( writer => {
169
- * writer.insertText( 'bar', paragraph, 'end' ); // foobar.
170
- * } );
171
- *
172
- * writer.insertText( 'bom', paragraph, 'end' ); // foobarbom.
173
- * } );
174
- * ```
175
- *
176
- * The callback of the `change()` block is executed synchronously.
177
- *
178
- * You can also return a value from the change block.
179
- *
180
- * ```ts
181
- * const img = model.change( writer => {
182
- * return writer.createElement( 'img' );
183
- * } );
184
- * ```
185
- *
186
- * @see #enqueueChange
187
- * @typeParam TReturn The return type of the provided callback.
188
- * @param callback Callback function which may modify the model.
189
- */
190
- change(callback) {
191
- try {
192
- if (this._pendingChanges.length === 0) {
193
- // If this is the outermost block, create a new batch and start `_runPendingChanges` execution flow.
194
- this._pendingChanges.push({ batch: new Batch(), callback });
195
- return this._runPendingChanges()[0];
196
- }
197
- else {
198
- // If this is not the outermost block, just execute the callback.
199
- return callback(this._currentWriter);
200
- }
201
- }
202
- catch (err) {
203
- // @if CK_DEBUG // throw err;
204
- /* istanbul ignore next -- @preserve */
205
- CKEditorError.rethrowUnexpectedError(err, this);
206
- }
207
- }
208
- enqueueChange(batchOrType, callback) {
209
- try {
210
- if (!batchOrType) {
211
- batchOrType = new Batch();
212
- }
213
- else if (typeof batchOrType === 'function') {
214
- callback = batchOrType;
215
- batchOrType = new Batch();
216
- }
217
- else if (!(batchOrType instanceof Batch)) {
218
- batchOrType = new Batch(batchOrType);
219
- }
220
- this._pendingChanges.push({ batch: batchOrType, callback });
221
- if (this._pendingChanges.length == 1) {
222
- this._runPendingChanges();
223
- }
224
- }
225
- catch (err) {
226
- // @if CK_DEBUG // throw err;
227
- /* istanbul ignore next -- @preserve */
228
- CKEditorError.rethrowUnexpectedError(err, this);
229
- }
230
- }
231
- /**
232
- * {@link module:utils/observablemixin~Observable#decorate Decorated} function for applying
233
- * {@link module:engine/model/operation/operation~Operation operations} to the model.
234
- *
235
- * This is a low-level way of changing the model. It is exposed for very specific use cases (like the undo feature).
236
- * Normally, to modify the model, you will want to use {@link module:engine/model/writer~ModelWriter `Writer`}.
237
- * See also {@glink framework/architecture/editing-engine#changing-the-model Changing the model} section
238
- * of the {@glink framework/architecture/editing-engine Editing architecture} guide.
239
- *
240
- * @param operation The operation to apply.
241
- */
242
- applyOperation(operation) {
243
- // @if CK_DEBUG_ENGINE // console.log( 'Applying ' + operation );
244
- // @if CK_DEBUG_ENGINE // this._operationLogs.push( JSON.stringify( operation ) );
245
- // @if CK_DEBUG_ENGINE // this._appliedOperations.push( operation );
246
- operation._execute();
247
- }
248
- // @if CK_DEBUG_ENGINE // public getAppliedOperation(): string {
249
- // @if CK_DEBUG_ENGINE // if ( !this._appliedOperations ) {
250
- // @if CK_DEBUG_ENGINE // return '';
251
- // @if CK_DEBUG_ENGINE // }
252
- // @if CK_DEBUG_ENGINE // return this._appliedOperations.map( operation => JSON.stringify( operation ) ).join( '-------' );
253
- // @if CK_DEBUG_ENGINE // }
254
- // @if CK_DEBUG_ENGINE // public createReplayer( stringifiedOperations: string ): typeof OperationReplayer {
255
- // @if CK_DEBUG_ENGINE // return new OperationReplayer( this, '-------', stringifiedOperations );
256
- // @if CK_DEBUG_ENGINE // }
257
- /**
258
- * Inserts content at the position in the editor specified by the selection, as one would expect the paste
259
- * functionality to work.
260
- *
261
- * **Note**: If you want to insert an {@glink framework/deep-dive/schema#object-elements object element}
262
- * (e.g. a {@link module:widget/utils~toWidget widget}), see {@link #insertObject} instead.
263
- *
264
- * This is a high-level method. It takes the {@link #schema schema} into consideration when inserting
265
- * the content, clears the given selection's content before inserting nodes and moves the selection
266
- * to its target position at the end of the process.
267
- * It can split elements, merge them, wrap bare text nodes with paragraphs, etc. &ndash; just like the
268
- * pasting feature should do.
269
- *
270
- * For lower-level methods see {@link module:engine/model/writer~ModelWriter `Writer`}.
271
- *
272
- * This method, unlike {@link module:engine/model/writer~ModelWriter `Writer`}'s methods, does not have to be used
273
- * inside a {@link #change `change()` block}.
274
- *
275
- * # Conversion and schema
276
- *
277
- * Inserting elements and text nodes into the model is not enough to make CKEditor 5 render that content
278
- * to the user. CKEditor 5 implements a model-view-controller architecture and what `model.insertContent()` does
279
- * is only adding nodes to the model. Additionally, you need to define
280
- * {@glink framework/architecture/editing-engine#conversion converters} between the model and view
281
- * and define those nodes in the {@glink framework/architecture/editing-engine#schema schema}.
282
- *
283
- * So, while this method may seem similar to CKEditor 4 `editor.insertHtml()` (in fact, both methods
284
- * are used for paste-like content insertion), the CKEditor 5 method cannot be use to insert arbitrary HTML
285
- * unless converters are defined for all elements and attributes in that HTML.
286
- *
287
- * # Examples
288
- *
289
- * Using `insertContent()` with a manually created model structure:
290
- *
291
- * ```ts
292
- * // Let's create a document fragment containing such content as:
293
- * //
294
- * // <paragraph>foo</paragraph>
295
- * // <blockQuote>
296
- * // <paragraph>bar</paragraph>
297
- * // </blockQuote>
298
- * const docFrag = editor.model.change( writer => {
299
- * const p1 = writer.createElement( 'paragraph' );
300
- * const p2 = writer.createElement( 'paragraph' );
301
- * const blockQuote = writer.createElement( 'blockQuote' );
302
- * const docFrag = writer.createDocumentFragment();
303
- *
304
- * writer.append( p1, docFrag );
305
- * writer.append( blockQuote, docFrag );
306
- * writer.append( p2, blockQuote );
307
- * writer.insertText( 'foo', p1 );
308
- * writer.insertText( 'bar', p2 );
309
- *
310
- * return docFrag;
311
- * } );
312
- *
313
- * // insertContent() does not have to be used in a change() block. It can, though,
314
- * // so this code could be moved to the callback defined above.
315
- * editor.model.insertContent( docFrag );
316
- * ```
317
- *
318
- * Using `insertContent()` with an HTML string converted to a model document fragment (similar to the pasting mechanism):
319
- *
320
- * ```ts
321
- * // You can create your own HtmlDataProcessor instance or use editor.data.processor
322
- * // if you have not overridden the default one (which is the HtmlDataProcessor instance).
323
- * const htmlDP = new HtmlDataProcessor( viewDocument );
324
- *
325
- * // Convert an HTML string to a view document fragment:
326
- * const viewFragment = htmlDP.toView( htmlString );
327
- *
328
- * // Convert the view document fragment to a model document fragment
329
- * // in the context of $root. This conversion takes the schema into
330
- * // account so if, for example, the view document fragment contained a bare text node,
331
- * // this text node cannot be a child of $root, so it will be automatically
332
- * // wrapped with a <paragraph>. You can define the context yourself (in the second parameter),
333
- * // and e.g. convert the content like it would happen in a <paragraph>.
334
- * // Note: The clipboard feature uses a custom context called $clipboardHolder
335
- * // which has a loosened schema.
336
- * const modelFragment = editor.data.toModel( viewFragment );
337
- *
338
- * editor.model.insertContent( modelFragment );
339
- * ```
340
- *
341
- * By default this method will use the document selection but it can also be used with a position, range or selection instance.
342
- *
343
- * ```ts
344
- * // Insert text at the current document selection position.
345
- * editor.model.change( writer => {
346
- * editor.model.insertContent( writer.createText( 'x' ) );
347
- * } );
348
- *
349
- * // Insert text at a given position - the document selection will not be modified.
350
- * editor.model.change( writer => {
351
- * editor.model.insertContent( writer.createText( 'x' ), doc.getRoot(), 2 );
352
- *
353
- * // Which is a shorthand for:
354
- * editor.model.insertContent( writer.createText( 'x' ), writer.createPositionAt( doc.getRoot(), 2 ) );
355
- * } );
356
- * ```
357
- *
358
- * If you want the document selection to be moved to the inserted content, use the
359
- * {@link module:engine/model/writer~ModelWriter#setSelection `setSelection()`} method of the writer after inserting
360
- * the content:
361
- *
362
- * ```ts
363
- * editor.model.change( writer => {
364
- * const paragraph = writer.createElement( 'paragraph' );
365
- *
366
- * // Insert an empty paragraph at the beginning of the root.
367
- * editor.model.insertContent( paragraph, writer.createPositionAt( editor.model.document.getRoot(), 0 ) );
368
- *
369
- * // Move the document selection to the inserted paragraph.
370
- * writer.setSelection( paragraph, 'in' );
371
- * } );
372
- * ```
373
- *
374
- * If an instance of the {@link module:engine/model/selection~ModelSelection model selection} is passed as `selectable`,
375
- * the new content will be inserted at the passed selection (instead of document selection):
376
- *
377
- * ```ts
378
- * editor.model.change( writer => {
379
- * // Create a selection in a paragraph that will be used as a place of insertion.
380
- * const selection = writer.createSelection( paragraph, 'in' );
381
- *
382
- * // Insert the new text at the created selection.
383
- * editor.model.insertContent( writer.createText( 'x' ), selection );
384
- *
385
- * // insertContent() modifies the passed selection instance so it can be used to set the document selection.
386
- * // Note: This is not necessary when you passed the document selection to insertContent().
387
- * writer.setSelection( selection );
388
- * } );
389
- * ```
390
- *
391
- * @fires insertContent
392
- * @param content The content to insert.
393
- * @param selectable The selection into which the content should be inserted.
394
- * If not provided the current model document selection will be used.
395
- * @param placeOrOffset To be used when a model item was passed as `selectable`.
396
- * This param defines a position in relation to that item.
397
- * at the insertion position.
398
- */
399
- insertContent(content, selectable, placeOrOffset, ...rest) {
400
- const selection = normalizeSelectable(selectable, placeOrOffset);
401
- // Passing all call arguments so it acts like decorated method.
402
- return this.fire('insertContent', [content, selection, placeOrOffset, ...rest]);
403
- }
404
- /**
405
- * Inserts an {@glink framework/deep-dive/schema#object-elements object element} at a specific position in the editor content.
406
- *
407
- * This is a high-level API:
408
- * * It takes the {@link #schema schema} into consideration,
409
- * * It clears the content of passed `selectable` before inserting,
410
- * * It can move the selection at the end of the process,
411
- * * It will copy the selected block's attributes to preserve them upon insertion,
412
- * * It can split elements or wrap inline objects with paragraphs if they are not allowed in target position,
413
- * * etc.
414
- *
415
- * # Notes
416
- *
417
- * * If you want to insert a non-object content, see {@link #insertContent} instead.
418
- * * For lower-level API, see {@link module:engine/model/writer~ModelWriter `Writer`}.
419
- * * Unlike {@link module:engine/model/writer~ModelWriter `Writer`}, this method does not have to be used inside
420
- * a {@link #change `change()` block}.
421
- * * Inserting object into the model is not enough to make CKEditor 5 render that content to the user.
422
- * CKEditor 5 implements a model-view-controller architecture and what `model.insertObject()` does
423
- * is only adding nodes to the model. Additionally, you need to define
424
- * {@glink framework/architecture/editing-engine#conversion converters} between the model and view
425
- * and define those nodes in the {@glink framework/architecture/editing-engine#schema schema}.
426
- *
427
- * # Examples
428
- *
429
- * Use the following code to insert an object at the current selection and keep the selection on the inserted element:
430
- *
431
- * ```ts
432
- * const rawHtmlEmbedElement = writer.createElement( 'rawHtml' );
433
- *
434
- * model.insertObject( rawHtmlEmbedElement, null, null, {
435
- * setSelection: 'on'
436
- * } );
437
- * ```
438
- *
439
- * Use the following code to insert an object at the current selection and nudge the selection after the inserted object:
440
- *
441
- * ```ts
442
- * const pageBreakElement = writer.createElement( 'pageBreak' );
443
- *
444
- * model.insertObject( pageBreakElement, null, null, {
445
- * setSelection: 'after'
446
- * } );
447
- * ```
448
- *
449
- * Use the following code to insert an object at the current selection and avoid splitting the content (non-destructive insertion):
450
- *
451
- * ```ts
452
- * const tableElement = writer.createElement( 'table' );
453
- *
454
- * model.insertObject( tableElement, null, null, {
455
- * findOptimalPosition: 'auto'
456
- * } );
457
- * ```
458
- *
459
- * Use the following code to insert an object at the specific range (also: replace the content of the range):
460
- *
461
- * ```ts
462
- * const tableElement = writer.createElement( 'table' );
463
- * const range = model.createRangeOn( model.document.getRoot().getChild( 1 ) );
464
- *
465
- * model.insertObject( tableElement, range );
466
- * ```
467
- *
468
- * @param element An object to be inserted into the model document.
469
- * @param selectable A selectable where the content should be inserted. If not specified, the current
470
- * {@link module:engine/model/document~ModelDocument#selection document selection} will be used instead.
471
- * @param placeOrOffset Specifies the exact place or offset for the insertion to take place, relative to `selectable`.
472
- * @param options Additional options.
473
- * @param options.findOptimalPosition An option that, when set, adjusts the insertion position (relative to
474
- * `selectable` and `placeOrOffset`) so that the content of `selectable` is not split upon insertion (a.k.a. non-destructive insertion).
475
- * * When `'auto'`, the algorithm will decide whether to insert the object before or after `selectable` to avoid content splitting.
476
- * * When `'before'`, the closest position before `selectable` will be used that will not result in content splitting.
477
- * * When `'after'`, the closest position after `selectable` will be used that will not result in content splitting.
478
- *
479
- * Note that this option only works for block objects. Inline objects are inserted into text and do not split blocks.
480
- * @param options.setSelection An option that, when set, moves the
481
- * {@link module:engine/model/document~ModelDocument#selection document selection} after inserting the object.
482
- * * When `'on'`, the document selection will be set on the inserted object.
483
- * * When `'after'`, the document selection will move to the closest text node after the inserted object. If there is no
484
- * such text node, a paragraph will be created and the document selection will be moved inside it.
485
- * at the insertion position.
486
- */
487
- insertObject(element, selectable, placeOrOffset, options, ...rest) {
488
- const selection = normalizeSelectable(selectable, placeOrOffset);
489
- // Note that options are fired as 2 arguments for backward compatibility with the decorated method.
490
- // Passing all call arguments so it acts like decorated method.
491
- return this.fire('insertObject', [element, selection, options, options, ...rest]);
492
- }
493
- /**
494
- * Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
495
- *
496
- * **Note:** For the sake of predictability, the resulting selection should always be collapsed.
497
- * In cases where a feature wants to modify deleting behavior so selection isn't collapsed
498
- * (e.g. a table feature may want to keep row selection after pressing <kbd>Backspace</kbd>),
499
- * then that behavior should be implemented in the view's listener. At the same time, the table feature
500
- * will need to modify this method's behavior too, e.g. to "delete contents and then collapse
501
- * the selection inside the last selected cell" or "delete the row and collapse selection somewhere near".
502
- * That needs to be done in order to ensure that other features which use `deleteContent()` will work well with tables.
503
- *
504
- * @fires deleteContent
505
- * @param selection Selection of which the content should be deleted.
506
- * @param options.leaveUnmerged Whether to merge elements after removing the content of the selection.
507
- *
508
- * For example `<heading1>x[x</heading1><paragraph>y]y</paragraph>` will become:
509
- *
510
- * * `<heading1>x^y</heading1>` with the option disabled (`leaveUnmerged == false`)
511
- * * `<heading1>x^</heading1><paragraph>y</paragraph>` with enabled (`leaveUnmerged == true`).
512
- *
513
- * Note: {@link module:engine/model/schema~ModelSchema#isObject object} and {@link module:engine/model/schema~ModelSchema#isLimit limit}
514
- * elements will not be merged.
515
- *
516
- * @param options.doNotResetEntireContent Whether to skip replacing the entire content with a
517
- * paragraph when the entire content was selected.
518
- *
519
- * For example `<heading1>[x</heading1><paragraph>y]</paragraph>` will become:
520
- *
521
- * * `<paragraph>^</paragraph>` with the option disabled (`doNotResetEntireContent == false`)
522
- * * `<heading1>^</heading1>` with enabled (`doNotResetEntireContent == true`)
523
- *
524
- * @param options.doNotAutoparagraph Whether to create a paragraph if after content deletion selection is moved
525
- * to a place where text cannot be inserted.
526
- *
527
- * For example `<paragraph>x</paragraph>[<imageBlock src="foo.jpg"></imageBlock>]` will become:
528
- *
529
- * * `<paragraph>x</paragraph><paragraph>[]</paragraph>` with the option disabled (`doNotAutoparagraph == false`)
530
- * * `<paragraph>x[]</paragraph>` with the option enabled (`doNotAutoparagraph == true`).
531
- *
532
- * **Note:** if there is no valid position for the selection, the paragraph will always be created:
533
- *
534
- * `[<imageBlock src="foo.jpg"></imageBlock>]` -> `<paragraph>[]</paragraph>`.
535
- *
536
- * @param options.direction The direction in which the content is being consumed.
537
- * Deleting backward corresponds to using the <kbd>Backspace</kbd> key, while deleting content forward corresponds to
538
- * the <kbd>Shift</kbd>+<kbd>Backspace</kbd> keystroke.
539
- */
540
- deleteContent(selection, options) {
541
- deleteContent(this, selection, options);
542
- }
543
- /**
544
- * Modifies the selection. Currently, the supported modifications are:
545
- *
546
- * * Extending. The selection focus is moved in the specified `options.direction` with a step specified in `options.unit`.
547
- * Possible values for `unit` are:
548
- * * `'character'` (default) - moves selection by one user-perceived character. In most cases this means moving by one
549
- * character in `String` sense. However, unicode also defines "combing marks". These are special symbols, that combines
550
- * with a symbol before it ("base character") to create one user-perceived character. For example, `q̣̇` is a normal
551
- * letter `q` with two "combining marks": upper dot (`Ux0307`) and lower dot (`Ux0323`). For most actions, i.e. extending
552
- * selection by one position, it is correct to include both "base character" and all of it's "combining marks". That is
553
- * why `'character'` value is most natural and common method of modifying selection.
554
- * * `'codePoint'` - moves selection by one unicode code point. In contrary to, `'character'` unit, this will insert
555
- * selection between "base character" and "combining mark", because "combining marks" have their own unicode code points.
556
- * However, for technical reasons, unicode code points with values above `UxFFFF` are represented in native `String` by
557
- * two characters, called "surrogate pairs". Halves of "surrogate pairs" have a meaning only when placed next to each other.
558
- * For example `𨭎` is represented in `String` by `\uD862\uDF4E`. Both `\uD862` and `\uDF4E` do not have any meaning
559
- * outside the pair (are rendered as ? when alone). Position between them would be incorrect. In this case, selection
560
- * extension will include whole "surrogate pair".
561
- * * `'word'` - moves selection by a whole word.
562
- *
563
- * **Note:** if you extend a forward selection in a backward direction you will in fact shrink it.
564
- *
565
- * @fires modifySelection
566
- * @param selection The selection to modify.
567
- * @param options.direction The direction in which the selection should be modified.
568
- * @param options.unit The unit by which selection should be modified.
569
- * @param options.treatEmojiAsSingleUnit Whether multi-characer emoji sequences should be handled as single unit.
570
- */
571
- modifySelection(selection, options) {
572
- modifySelection(this, selection, options);
573
- }
574
- /**
575
- * Gets a clone of the selected content.
576
- *
577
- * For example, for the following selection:
578
- *
579
- * ```html
580
- * <paragraph>x</paragraph>
581
- * <blockQuote>
582
- * <paragraph>y</paragraph>
583
- * <heading1>fir[st</heading1>
584
- * </blockQuote>
585
- * <paragraph>se]cond</paragraph>
586
- * <paragraph>z</paragraph>
587
- * ```
588
- *
589
- * It will return a document fragment with such a content:
590
- *
591
- * ```html
592
- * <blockQuote>
593
- * <heading1>st</heading1>
594
- * </blockQuote>
595
- * <paragraph>se</paragraph>
596
- * ```
597
- *
598
- * @fires getSelectedContent
599
- * @param selection The selection of which content will be returned.
600
- */
601
- getSelectedContent(selection) {
602
- return getSelectedContent(this, selection);
603
- }
604
- /**
605
- * Checks whether the given {@link module:engine/model/range~ModelRange range} or
606
- * {@link module:engine/model/element~ModelElement element} has any meaningful content.
607
- *
608
- * Meaningful content is:
609
- *
610
- * * any text node (`options.ignoreWhitespaces` allows controlling whether this text node must also contain
611
- * any non-whitespace characters),
612
- * * or any {@link module:engine/model/schema~ModelSchema#isContent content element},
613
- * * or any {@link module:engine/model/markercollection~Marker marker} which
614
- * {@link module:engine/model/markercollection~Marker#_affectsData affects data}.
615
- *
616
- * This means that a range containing an empty `<paragraph></paragraph>` is not considered to have a meaningful content.
617
- * However, a range containing an `<imageBlock></imageBlock>` (which would normally be marked in the schema as an object element)
618
- * is considered non-empty.
619
- *
620
- * @param rangeOrElement Range or element to check.
621
- * @param options.ignoreWhitespaces Whether text node with whitespaces only should be considered empty.
622
- * @param options.ignoreMarkers Whether markers should be ignored.
623
- */
624
- hasContent(rangeOrElement, options = {}) {
625
- const range = rangeOrElement instanceof ModelRange ? rangeOrElement : ModelRange._createIn(rangeOrElement);
626
- if (range.isCollapsed) {
627
- return false;
628
- }
629
- const { ignoreWhitespaces = false, ignoreMarkers = false } = options;
630
- // Check if there are any markers which affects data in this given range.
631
- if (!ignoreMarkers) {
632
- for (const intersectingMarker of this.markers.getMarkersIntersectingRange(range)) {
633
- if (intersectingMarker.affectsData) {
634
- return true;
635
- }
636
- }
637
- }
638
- for (const item of range.getItems()) {
639
- if (this.schema.isContent(item)) {
640
- if (item.is('$textProxy')) {
641
- if (!ignoreWhitespaces) {
642
- return true;
643
- }
644
- else if (item.data.search(/\S/) !== -1) {
645
- return true;
646
- }
647
- }
648
- else {
649
- return true;
650
- }
651
- }
652
- }
653
- return false;
654
- }
655
- /**
656
- * Check whether given selectable is at a place in the model where it can be edited (returns `true`) or not (returns `false`).
657
- *
658
- * Should be used instead of {@link module:core/editor/editor~Editor#isReadOnly} to check whether a user action can happen at
659
- * given selectable. It may be decorated and used differently in different environment (e.g. multi-root editor can disable
660
- * a particular root).
661
- *
662
- * This method is decorated. Although this method accepts any parameter of `Selectable` type, the
663
- * {@link ~Model#event:canEditAt `canEditAt` event} is fired with `selectable` normalized to an instance of
664
- * {@link module:engine/model/selection~ModelSelection} or {@link module:engine/model/documentselection~ModelDocumentSelection}
665
- *
666
- * @fires canEditAt
667
- */
668
- canEditAt(selectable) {
669
- const selection = normalizeSelectable(selectable);
670
- return this.fire('canEditAt', [selection]);
671
- }
672
- /**
673
- * Creates a position from the given root and path in that root.
674
- *
675
- * Note: This method is also available as
676
- * {@link module:engine/model/writer~ModelWriter#createPositionFromPath `Writer#createPositionFromPath()`}.
677
- *
678
- * @param root Root of the position.
679
- * @param path Position path. See {@link module:engine/model/position~ModelPosition#path}.
680
- * @param stickiness Position stickiness. See {@link module:engine/model/position~ModelPositionStickiness}.
681
- */
682
- createPositionFromPath(root, path, stickiness) {
683
- return new ModelPosition(root, path, stickiness);
684
- }
685
- /**
686
- * Creates position at the given location. The location can be specified as:
687
- *
688
- * * a {@link module:engine/model/position~ModelPosition position},
689
- * * a parent element and offset in that element,
690
- * * a parent element and `'end'` (the position will be set at the end of that element),
691
- * * a {@link module:engine/model/item~ModelItem model item} and `'before'` or `'after'`
692
- * (the position will be set before or after the given model item).
693
- *
694
- * This method is a shortcut to other factory methods such as:
695
- *
696
- * * {@link module:engine/model/model~Model#createPositionBefore `createPositionBefore()`},
697
- * * {@link module:engine/model/model~Model#createPositionAfter `createPositionAfter()`}.
698
- *
699
- * Note: This method is also available as
700
- * {@link module:engine/model/writer~ModelWriter#createPositionAt `Writer#createPositionAt()`},
701
- *
702
- * @param itemOrPosition
703
- * @param offset Offset or one of the flags. Used only when first parameter is a {@link module:engine/model/item~ModelItem model item}.
704
- */
705
- createPositionAt(itemOrPosition, offset) {
706
- return ModelPosition._createAt(itemOrPosition, offset);
707
- }
708
- /**
709
- * Creates a new position after the given {@link module:engine/model/item~ModelItem model item}.
710
- *
711
- * Note: This method is also available as
712
- * {@link module:engine/model/writer~ModelWriter#createPositionAfter `Writer#createPositionAfter()`}.
713
- *
714
- * @param item Item after which the position should be placed.
715
- */
716
- createPositionAfter(item) {
717
- return ModelPosition._createAfter(item);
718
- }
719
- /**
720
- * Creates a new position before the given {@link module:engine/model/item~ModelItem model item}.
721
- *
722
- * Note: This method is also available as
723
- * {@link module:engine/model/writer~ModelWriter#createPositionBefore `Writer#createPositionBefore()`}.
724
- *
725
- * @param item Item before which the position should be placed.
726
- */
727
- createPositionBefore(item) {
728
- return ModelPosition._createBefore(item);
729
- }
730
- /**
731
- * Creates a range spanning from the `start` position to the `end` position.
732
- *
733
- * Note: This method is also available as
734
- * {@link module:engine/model/writer~ModelWriter#createRange `Writer#createRange()`}:
735
- *
736
- * ```ts
737
- * model.change( writer => {
738
- * const range = writer.createRange( start, end );
739
- * } );
740
- * ```
741
- *
742
- * @param start Start position.
743
- * @param end End position. If not set, the range will be collapsed to the `start` position.
744
- */
745
- createRange(start, end) {
746
- return new ModelRange(start, end);
747
- }
748
- /**
749
- * Creates a range inside the given element which starts before the first child of
750
- * that element and ends after the last child of that element.
751
- *
752
- * Note: This method is also available as
753
- * {@link module:engine/model/writer~ModelWriter#createRangeIn `Writer#createRangeIn()`}:
754
- *
755
- * ```ts
756
- * model.change( writer => {
757
- * const range = writer.createRangeIn( paragraph );
758
- * } );
759
- * ```
760
- *
761
- * @param element Element which is a parent for the range.
762
- */
763
- createRangeIn(element) {
764
- return ModelRange._createIn(element);
765
- }
766
- /**
767
- * Creates a range that starts before the given {@link module:engine/model/item~ModelItem model item} and ends after it.
768
- *
769
- * Note: This method is also available on `writer` instance as
770
- * {@link module:engine/model/writer~ModelWriter#createRangeOn `Writer.createRangeOn()`}:
771
- *
772
- * ```ts
773
- * model.change( writer => {
774
- * const range = writer.createRangeOn( paragraph );
775
- * } );
776
- * ```
777
- *
778
- * @param item
779
- */
780
- createRangeOn(item) {
781
- return ModelRange._createOn(item);
782
- }
783
- createSelection(...args) {
784
- return new ModelSelection(...args);
785
- }
786
- /**
787
- * Creates a {@link module:engine/model/batch~Batch} instance.
788
- *
789
- * **Note:** In most cases creating a batch instance is not necessary as they are created when using:
790
- *
791
- * * {@link #change `change()`},
792
- * * {@link #enqueueChange `enqueueChange()`}.
793
- *
794
- * @param type {@link module:engine/model/batch~Batch#constructor The type} of the batch.
795
- */
796
- createBatch(type) {
797
- return new Batch(type);
798
- }
799
- /**
800
- * Creates an operation instance from a JSON object (parsed JSON string).
801
- *
802
- * This is an alias for {@link module:engine/model/operation/operationfactory~OperationFactory.fromJSON `OperationFactory.fromJSON()`}.
803
- *
804
- * @param json Deserialized JSON object.
805
- */
806
- createOperationFromJSON(json) {
807
- return OperationFactory.fromJSON(json, this.document);
808
- }
809
- /**
810
- * Removes all events listeners set by model instance and destroys {@link module:engine/model/document~ModelDocument}.
811
- */
812
- destroy() {
813
- this.document.destroy();
814
- this.stopListening();
815
- }
816
- /**
817
- * Common part of {@link module:engine/model/model~Model#change} and {@link module:engine/model/model~Model#enqueueChange}
818
- * which calls callbacks and returns array of values returned by these callbacks.
819
- */
820
- _runPendingChanges() {
821
- const ret = [];
822
- this.fire('_beforeChanges');
823
- try {
824
- while (this._pendingChanges.length) {
825
- // Create a new writer using batch instance created for this chain of changes.
826
- const currentBatch = this._pendingChanges[0].batch;
827
- this._currentWriter = new ModelWriter(this, currentBatch);
828
- // Execute changes callback and gather the returned value.
829
- const callbackReturnValue = this._pendingChanges[0].callback(this._currentWriter);
830
- ret.push(callbackReturnValue);
831
- this.document._handleChangeBlock(this._currentWriter);
832
- this._pendingChanges.shift();
833
- this._currentWriter = null;
834
- }
835
- }
836
- finally {
837
- this._pendingChanges.length = 0;
838
- this._currentWriter = null;
839
- this.fire('_afterChanges');
840
- }
841
- return ret;
842
- }
843
- }
844
- /**
845
- * Normalizes a selectable to a Selection or ModelDocumentSelection.
846
- */
847
- function normalizeSelectable(selectable, placeOrOffset) {
848
- if (!selectable) {
849
- return;
850
- }
851
- if (selectable instanceof ModelSelection || selectable instanceof ModelDocumentSelection) {
852
- return selectable;
853
- }
854
- if (selectable instanceof ModelNode) {
855
- if (placeOrOffset || placeOrOffset === 0) {
856
- return new ModelSelection(selectable, placeOrOffset);
857
- }
858
- else if (selectable.is('rootElement')) {
859
- return new ModelSelection(selectable, 'in');
860
- }
861
- else {
862
- return new ModelSelection(selectable, 'on');
863
- }
864
- }
865
- return new ModelSelection(selectable);
866
- }