@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.
- package/LICENSE.md +1 -1
- package/{src → dist}/engineconfig.d.ts +6 -15
- package/dist/index-editor.css +38 -15
- package/dist/index.css +37 -37
- package/dist/index.css.map +1 -1
- package/{src → dist}/index.d.ts +0 -1
- package/dist/index.js +588 -94
- package/dist/index.js.map +1 -1
- package/{src → dist}/model/model.d.ts +10 -4
- package/{src → dist}/model/selection.d.ts +1 -1
- package/{src → dist}/view/downcastwriter.d.ts +3 -2
- package/{src → dist}/view/element.d.ts +2 -2
- package/{src → dist}/view/matcher.d.ts +4 -2
- package/dist/view/styles/background.d.ts +18 -0
- package/{src → dist}/view/styles/border.d.ts +0 -12
- package/{src → dist}/view/styles/margin.d.ts +0 -13
- package/{src → dist}/view/styles/padding.d.ts +0 -13
- package/{src → dist}/view/styles/utils.d.ts +12 -0
- package/package.json +20 -39
- package/src/controller/datacontroller.js +0 -522
- package/src/controller/editingcontroller.js +0 -181
- package/src/conversion/conversion.js +0 -606
- package/src/conversion/conversionhelpers.js +0 -33
- package/src/conversion/downcastdispatcher.js +0 -563
- package/src/conversion/downcasthelpers.js +0 -2160
- package/src/conversion/mapper.js +0 -1050
- package/src/conversion/modelconsumable.js +0 -331
- package/src/conversion/upcastdispatcher.js +0 -470
- package/src/conversion/upcasthelpers.js +0 -952
- package/src/conversion/viewconsumable.js +0 -541
- package/src/dataprocessor/basichtmlwriter.js +0 -22
- package/src/dataprocessor/dataprocessor.js +0 -5
- package/src/dataprocessor/htmldataprocessor.js +0 -107
- package/src/dataprocessor/htmlwriter.js +0 -5
- package/src/dataprocessor/xmldataprocessor.js +0 -127
- package/src/dev-utils/model.js +0 -396
- package/src/dev-utils/operationreplayer.js +0 -116
- package/src/dev-utils/utils.js +0 -122
- package/src/dev-utils/view.js +0 -990
- package/src/engineconfig.js +0 -5
- package/src/index.js +0 -134
- package/src/legacyerrors.js +0 -17
- package/src/model/batch.js +0 -98
- package/src/model/differ.js +0 -1288
- package/src/model/document.js +0 -398
- package/src/model/documentfragment.js +0 -332
- package/src/model/documentselection.js +0 -1026
- package/src/model/element.js +0 -323
- package/src/model/history.js +0 -206
- package/src/model/item.js +0 -5
- package/src/model/liveposition.js +0 -93
- package/src/model/liverange.js +0 -121
- package/src/model/markercollection.js +0 -436
- package/src/model/model.js +0 -866
- package/src/model/node.js +0 -371
- package/src/model/nodelist.js +0 -244
- package/src/model/operation/attributeoperation.js +0 -172
- package/src/model/operation/detachoperation.js +0 -87
- package/src/model/operation/insertoperation.js +0 -153
- package/src/model/operation/markeroperation.js +0 -136
- package/src/model/operation/mergeoperation.js +0 -184
- package/src/model/operation/moveoperation.js +0 -179
- package/src/model/operation/nooperation.js +0 -48
- package/src/model/operation/operation.js +0 -78
- package/src/model/operation/operationfactory.js +0 -44
- package/src/model/operation/renameoperation.js +0 -128
- package/src/model/operation/rootattributeoperation.js +0 -173
- package/src/model/operation/rootoperation.js +0 -106
- package/src/model/operation/splitoperation.js +0 -214
- package/src/model/operation/transform.js +0 -2211
- package/src/model/operation/utils.js +0 -217
- package/src/model/position.js +0 -1041
- package/src/model/range.js +0 -880
- package/src/model/rootelement.js +0 -82
- package/src/model/schema.js +0 -1542
- package/src/model/selection.js +0 -814
- package/src/model/text.js +0 -92
- package/src/model/textproxy.js +0 -202
- package/src/model/treewalker.js +0 -313
- package/src/model/typecheckable.js +0 -16
- package/src/model/utils/autoparagraphing.js +0 -63
- package/src/model/utils/deletecontent.js +0 -509
- package/src/model/utils/getselectedcontent.js +0 -126
- package/src/model/utils/insertcontent.js +0 -750
- package/src/model/utils/insertobject.js +0 -135
- package/src/model/utils/modifyselection.js +0 -187
- package/src/model/utils/selection-post-fixer.js +0 -264
- package/src/model/writer.js +0 -1318
- package/src/view/attributeelement.js +0 -220
- package/src/view/containerelement.js +0 -91
- package/src/view/datatransfer.js +0 -106
- package/src/view/document.js +0 -139
- package/src/view/documentfragment.js +0 -251
- package/src/view/documentselection.js +0 -270
- package/src/view/domconverter.js +0 -1661
- package/src/view/downcastwriter.js +0 -1589
- package/src/view/editableelement.js +0 -74
- package/src/view/element.js +0 -1053
- package/src/view/elementdefinition.js +0 -5
- package/src/view/emptyelement.js +0 -83
- package/src/view/filler.js +0 -161
- package/src/view/item.js +0 -5
- package/src/view/matcher.js +0 -437
- package/src/view/node.js +0 -238
- package/src/view/observer/arrowkeysobserver.js +0 -40
- package/src/view/observer/bubblingemittermixin.js +0 -215
- package/src/view/observer/bubblingeventinfo.js +0 -49
- package/src/view/observer/clickobserver.js +0 -26
- package/src/view/observer/compositionobserver.js +0 -64
- package/src/view/observer/domeventdata.js +0 -63
- package/src/view/observer/domeventobserver.js +0 -81
- package/src/view/observer/fakeselectionobserver.js +0 -95
- package/src/view/observer/focusobserver.js +0 -166
- package/src/view/observer/inputobserver.js +0 -236
- package/src/view/observer/keyobserver.js +0 -36
- package/src/view/observer/mouseobserver.js +0 -26
- package/src/view/observer/mutationobserver.js +0 -219
- package/src/view/observer/observer.js +0 -92
- package/src/view/observer/pointerobserver.js +0 -26
- package/src/view/observer/selectionobserver.js +0 -318
- package/src/view/observer/tabobserver.js +0 -42
- package/src/view/observer/touchobserver.js +0 -26
- package/src/view/placeholder.js +0 -285
- package/src/view/position.js +0 -341
- package/src/view/range.js +0 -451
- package/src/view/rawelement.js +0 -115
- package/src/view/renderer.js +0 -1148
- package/src/view/rooteditableelement.js +0 -78
- package/src/view/selection.js +0 -594
- package/src/view/styles/background.d.ts +0 -33
- package/src/view/styles/background.js +0 -74
- package/src/view/styles/border.js +0 -316
- package/src/view/styles/margin.js +0 -34
- package/src/view/styles/padding.js +0 -34
- package/src/view/styles/utils.js +0 -219
- package/src/view/stylesmap.js +0 -941
- package/src/view/text.js +0 -110
- package/src/view/textproxy.js +0 -136
- package/src/view/tokenlist.js +0 -194
- package/src/view/treewalker.js +0 -389
- package/src/view/typecheckable.js +0 -19
- package/src/view/uielement.js +0 -194
- package/src/view/upcastwriter.js +0 -363
- package/src/view/view.js +0 -579
- package/theme/placeholder.css +0 -36
- package/theme/renderer.css +0 -9
- /package/{src → dist}/controller/datacontroller.d.ts +0 -0
- /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
- /package/{src → dist}/conversion/conversion.d.ts +0 -0
- /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
- /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
- /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
- /package/{src → dist}/conversion/mapper.d.ts +0 -0
- /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
- /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
- /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
- /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
- /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
- /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
- /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
- /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
- /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
- /package/{src → dist}/dev-utils/model.d.ts +0 -0
- /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
- /package/{src → dist}/dev-utils/utils.d.ts +0 -0
- /package/{src → dist}/dev-utils/view.d.ts +0 -0
- /package/{src → dist}/legacyerrors.d.ts +0 -0
- /package/{src → dist}/model/batch.d.ts +0 -0
- /package/{src → dist}/model/differ.d.ts +0 -0
- /package/{src → dist}/model/document.d.ts +0 -0
- /package/{src → dist}/model/documentfragment.d.ts +0 -0
- /package/{src → dist}/model/documentselection.d.ts +0 -0
- /package/{src → dist}/model/element.d.ts +0 -0
- /package/{src → dist}/model/history.d.ts +0 -0
- /package/{src → dist}/model/item.d.ts +0 -0
- /package/{src → dist}/model/liveposition.d.ts +0 -0
- /package/{src → dist}/model/liverange.d.ts +0 -0
- /package/{src → dist}/model/markercollection.d.ts +0 -0
- /package/{src → dist}/model/node.d.ts +0 -0
- /package/{src → dist}/model/nodelist.d.ts +0 -0
- /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
- /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
- /package/{src → dist}/model/operation/operation.d.ts +0 -0
- /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
- /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/transform.d.ts +0 -0
- /package/{src → dist}/model/operation/utils.d.ts +0 -0
- /package/{src → dist}/model/position.d.ts +0 -0
- /package/{src → dist}/model/range.d.ts +0 -0
- /package/{src → dist}/model/rootelement.d.ts +0 -0
- /package/{src → dist}/model/schema.d.ts +0 -0
- /package/{src → dist}/model/text.d.ts +0 -0
- /package/{src → dist}/model/textproxy.d.ts +0 -0
- /package/{src → dist}/model/treewalker.d.ts +0 -0
- /package/{src → dist}/model/typecheckable.d.ts +0 -0
- /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
- /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
- /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
- /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
- /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
- /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
- /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
- /package/{src → dist}/model/writer.d.ts +0 -0
- /package/{src → dist}/view/attributeelement.d.ts +0 -0
- /package/{src → dist}/view/containerelement.d.ts +0 -0
- /package/{src → dist}/view/datatransfer.d.ts +0 -0
- /package/{src → dist}/view/document.d.ts +0 -0
- /package/{src → dist}/view/documentfragment.d.ts +0 -0
- /package/{src → dist}/view/documentselection.d.ts +0 -0
- /package/{src → dist}/view/domconverter.d.ts +0 -0
- /package/{src → dist}/view/editableelement.d.ts +0 -0
- /package/{src → dist}/view/elementdefinition.d.ts +0 -0
- /package/{src → dist}/view/emptyelement.d.ts +0 -0
- /package/{src → dist}/view/filler.d.ts +0 -0
- /package/{src → dist}/view/item.d.ts +0 -0
- /package/{src → dist}/view/node.d.ts +0 -0
- /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
- /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
- /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
- /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/observer.d.ts +0 -0
- /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
- /package/{src → dist}/view/placeholder.d.ts +0 -0
- /package/{src → dist}/view/position.d.ts +0 -0
- /package/{src → dist}/view/range.d.ts +0 -0
- /package/{src → dist}/view/rawelement.d.ts +0 -0
- /package/{src → dist}/view/renderer.d.ts +0 -0
- /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
- /package/{src → dist}/view/selection.d.ts +0 -0
- /package/{src → dist}/view/stylesmap.d.ts +0 -0
- /package/{src → dist}/view/text.d.ts +0 -0
- /package/{src → dist}/view/textproxy.d.ts +0 -0
- /package/{src → dist}/view/tokenlist.d.ts +0 -0
- /package/{src → dist}/view/treewalker.d.ts +0 -0
- /package/{src → dist}/view/typecheckable.d.ts +0 -0
- /package/{src → dist}/view/uielement.d.ts +0 -0
- /package/{src → dist}/view/upcastwriter.d.ts +0 -0
- /package/{src → dist}/view/view.d.ts +0 -0
|
@@ -1,127 +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/dataprocessor/xmldataprocessor
|
|
7
|
-
*/
|
|
8
|
-
import { BasicHtmlWriter } from './basichtmlwriter.js';
|
|
9
|
-
import { ViewDomConverter } from '../view/domconverter.js';
|
|
10
|
-
/**
|
|
11
|
-
* The XML data processor class.
|
|
12
|
-
* This data processor implementation uses XML as input and output data.
|
|
13
|
-
* This class is needed because unlike HTML, XML allows to use any tag with any value.
|
|
14
|
-
* For example, `<link>Text</link>` is a valid XML but invalid HTML.
|
|
15
|
-
*/
|
|
16
|
-
export class XmlDataProcessor {
|
|
17
|
-
/**
|
|
18
|
-
* A list of namespaces allowed to use in the XML input.
|
|
19
|
-
*
|
|
20
|
-
* For example, registering namespaces [ 'attribute', 'container' ] allows to use `<attirbute:tagName></attribute:tagName>`
|
|
21
|
-
* and `<container:tagName></container:tagName>` input. It is mainly for debugging.
|
|
22
|
-
*/
|
|
23
|
-
namespaces;
|
|
24
|
-
/**
|
|
25
|
-
* DOM parser instance used to parse an XML string to an XML document.
|
|
26
|
-
*/
|
|
27
|
-
domParser;
|
|
28
|
-
/**
|
|
29
|
-
* DOM converter used to convert DOM elements to view elements.
|
|
30
|
-
*/
|
|
31
|
-
domConverter;
|
|
32
|
-
/**
|
|
33
|
-
* A basic HTML writer instance used to convert DOM elements to an XML string.
|
|
34
|
-
* There is no need to use a dedicated XML writer because the basic HTML writer works well in this case.
|
|
35
|
-
*/
|
|
36
|
-
htmlWriter;
|
|
37
|
-
skipComments = true;
|
|
38
|
-
/**
|
|
39
|
-
* Creates a new instance of the XML data processor class.
|
|
40
|
-
*
|
|
41
|
-
* @param document The view document instance.
|
|
42
|
-
* @param options Configuration options.
|
|
43
|
-
* @param options.namespaces A list of namespaces allowed to use in the XML input.
|
|
44
|
-
*/
|
|
45
|
-
constructor(document, options = {}) {
|
|
46
|
-
this.namespaces = options.namespaces || [];
|
|
47
|
-
this.domParser = new DOMParser();
|
|
48
|
-
this.domConverter = new ViewDomConverter(document, { renderingMode: 'data' });
|
|
49
|
-
this.htmlWriter = new BasicHtmlWriter();
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Converts the provided {@link module:engine/view/documentfragment~ViewDocumentFragment document fragment}
|
|
53
|
-
* to data format – in this case an XML string.
|
|
54
|
-
*
|
|
55
|
-
* @returns An XML string.
|
|
56
|
-
*/
|
|
57
|
-
toData(viewFragment) {
|
|
58
|
-
// Convert view DocumentFragment to DOM DocumentFragment.
|
|
59
|
-
const domFragment = this.domConverter.viewToDom(viewFragment);
|
|
60
|
-
// Convert DOM DocumentFragment to XML output.
|
|
61
|
-
// There is no need to use dedicated for XML serializing method because BasicHtmlWriter works well in this case.
|
|
62
|
-
return this.htmlWriter.getHtml(domFragment);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Converts the provided XML string to a view tree.
|
|
66
|
-
*
|
|
67
|
-
* @param data An XML string.
|
|
68
|
-
* @returns A converted view element.
|
|
69
|
-
*/
|
|
70
|
-
toView(data) {
|
|
71
|
-
// Convert input XML data to DOM DocumentFragment.
|
|
72
|
-
const domFragment = this._toDom(data);
|
|
73
|
-
// Convert DOM DocumentFragment to view DocumentFragment.
|
|
74
|
-
return this.domConverter.domToView(domFragment, {
|
|
75
|
-
keepOriginalCase: true,
|
|
76
|
-
skipComments: this.skipComments
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Registers a {@link module:engine/view/matcher~MatcherPattern} for view elements whose content should be treated as raw data
|
|
81
|
-
* and not processed during the conversion from XML to view elements.
|
|
82
|
-
*
|
|
83
|
-
* The raw data can be later accessed by a
|
|
84
|
-
* {@link module:engine/view/element~ViewElement#getCustomProperty custom property of a view element} called `"$rawContent"`.
|
|
85
|
-
*
|
|
86
|
-
* @param pattern Pattern matching all view elements whose content should be treated as raw data.
|
|
87
|
-
*/
|
|
88
|
-
registerRawContentMatcher(pattern) {
|
|
89
|
-
this.domConverter.registerRawContentMatcher(pattern);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* If the processor is set to use marked fillers, it will insert ` ` fillers wrapped in `<span>` elements
|
|
93
|
-
* (`<span data-cke-filler="true"> </span>`) instead of regular ` ` characters.
|
|
94
|
-
*
|
|
95
|
-
* This mode allows for a more precise handling of block fillers (so they do not leak into editor content) but
|
|
96
|
-
* bloats the editor data with additional markup.
|
|
97
|
-
*
|
|
98
|
-
* This mode may be required by some features and will be turned on by them automatically.
|
|
99
|
-
*
|
|
100
|
-
* @param type Whether to use the default or the marked ` ` block fillers.
|
|
101
|
-
*/
|
|
102
|
-
useFillerType(type) {
|
|
103
|
-
this.domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Converts an XML string to its DOM representation. Returns a document fragment containing nodes parsed from
|
|
107
|
-
* the provided data.
|
|
108
|
-
*/
|
|
109
|
-
_toDom(data) {
|
|
110
|
-
// Stringify namespaces.
|
|
111
|
-
const namespaces = this.namespaces.map(nsp => `xmlns:${nsp}="nsp"`).join(' ');
|
|
112
|
-
// Wrap data into root element with optional namespace definitions.
|
|
113
|
-
data = `<xml ${namespaces}>${data}</xml>`;
|
|
114
|
-
const parsedDocument = this.domParser.parseFromString(data, 'text/xml');
|
|
115
|
-
// Parse validation.
|
|
116
|
-
const parserError = parsedDocument.querySelector('parsererror');
|
|
117
|
-
if (parserError) {
|
|
118
|
-
throw new Error('Parse error - ' + parserError.textContent);
|
|
119
|
-
}
|
|
120
|
-
const fragment = parsedDocument.createDocumentFragment();
|
|
121
|
-
const nodes = parsedDocument.documentElement.childNodes;
|
|
122
|
-
while (nodes.length > 0) {
|
|
123
|
-
fragment.appendChild(nodes[0]);
|
|
124
|
-
}
|
|
125
|
-
return fragment;
|
|
126
|
-
}
|
|
127
|
-
}
|
package/src/dev-utils/model.js
DELETED
|
@@ -1,396 +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/dev-utils/model
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Collection of methods for manipulating the {@link module:engine/model/model model} for testing purposes.
|
|
10
|
-
*/
|
|
11
|
-
import { ModelRootElement } from '../model/rootelement.js';
|
|
12
|
-
import { Model } from '../model/model.js';
|
|
13
|
-
import { ModelRange } from '../model/range.js';
|
|
14
|
-
import { ModelPosition } from '../model/position.js';
|
|
15
|
-
import { ModelSelection } from '../model/selection.js';
|
|
16
|
-
import { ModelDocumentFragment } from '../model/documentfragment.js';
|
|
17
|
-
import { ModelDocumentSelection } from '../model/documentselection.js';
|
|
18
|
-
import { EditingView } from '../view/view.js';
|
|
19
|
-
import { ViewContainerElement } from '../view/containerelement.js';
|
|
20
|
-
import { ViewRootEditableElement } from '../view/rooteditableelement.js';
|
|
21
|
-
import { _parseView, _stringifyView } from '../../src/dev-utils/view.js';
|
|
22
|
-
import { Mapper } from '../conversion/mapper.js';
|
|
23
|
-
import { convertCollapsedSelection, convertRangeSelection, insertAttributesAndChildren, insertElement, insertText, insertUIElement, wrap } from '../conversion/downcasthelpers.js';
|
|
24
|
-
import { StylesProcessor } from '../view/stylesmap.js';
|
|
25
|
-
import { DowncastDispatcher } from '../conversion/downcastdispatcher.js';
|
|
26
|
-
import { UpcastDispatcher } from '../conversion/upcastdispatcher.js';
|
|
27
|
-
import { toMap } from '@ckeditor/ckeditor5-utils';
|
|
28
|
-
import { isPlainObject } from 'es-toolkit/compat';
|
|
29
|
-
/**
|
|
30
|
-
* Writes the content of a model {@link module:engine/model/document~ModelDocument document} to an HTML-like string.
|
|
31
|
-
*
|
|
32
|
-
* ```ts
|
|
33
|
-
* getData( editor.model ); // -> '<paragraph>Foo![]</paragraph>'
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* **Note:** A {@link module:engine/model/text~ModelText text} node that contains attributes will be represented as:
|
|
37
|
-
*
|
|
38
|
-
* ```xml
|
|
39
|
-
* <$text attribute="value">Text data</$text>
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* **Note:** Using this tool in production-grade code is not recommended. It was designed for development, prototyping,
|
|
43
|
-
* debugging and testing.
|
|
44
|
-
*
|
|
45
|
-
* @param options.withoutSelection Whether to write the selection. When set to `true`, the selection will
|
|
46
|
-
* not be included in the returned string.
|
|
47
|
-
* @param options.rootName The name of the root from which the data should be stringified. If not provided,
|
|
48
|
-
* the default `main` name will be used.
|
|
49
|
-
* @param options.convertMarkers Whether to include markers in the returned string.
|
|
50
|
-
* @returns The stringified data.
|
|
51
|
-
*/
|
|
52
|
-
export function _getModelData(model, options = {}) {
|
|
53
|
-
if (!(model instanceof Model)) {
|
|
54
|
-
throw new TypeError('Model needs to be an instance of module:engine/model/model~Model.');
|
|
55
|
-
}
|
|
56
|
-
const rootName = options.rootName || 'main';
|
|
57
|
-
const root = model.document.getRoot(rootName);
|
|
58
|
-
return _getModelData._stringify(root, options.withoutSelection ? null : model.document.selection, options.convertMarkers ? model.markers : null);
|
|
59
|
-
}
|
|
60
|
-
// Set stringify as getData private method - needed for testing/spying.
|
|
61
|
-
_getModelData._stringify = _stringifyModel;
|
|
62
|
-
/**
|
|
63
|
-
* Sets the content of a model {@link module:engine/model/document~ModelDocument document} provided as an HTML-like string.
|
|
64
|
-
*
|
|
65
|
-
* ```ts
|
|
66
|
-
* setData( editor.model, '<paragraph>Foo![]</paragraph>' );
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* **Note:** Remember to register elements in the {@link module:engine/model/model~Model#schema model's schema} before
|
|
70
|
-
* trying to use them.
|
|
71
|
-
*
|
|
72
|
-
* **Note:** To create a {@link module:engine/model/text~ModelText text} node that contains attributes use:
|
|
73
|
-
*
|
|
74
|
-
* ```xml
|
|
75
|
-
* <$text attribute="value">Text data</$text>
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* **Note:** Using this tool in production-grade code is not recommended. It was designed for development, prototyping,
|
|
79
|
-
* debugging and testing.
|
|
80
|
-
*
|
|
81
|
-
* @param data HTML-like string to write into the document.
|
|
82
|
-
* @param options.rootName Root name where parsed data will be stored. If not provided, the default `main`
|
|
83
|
-
* name will be used.
|
|
84
|
-
* @param options.selectionAttributes A list of attributes which will be passed to the selection.
|
|
85
|
-
* @param options.lastRangeBackward If set to `true`, the last range will be added as backward.
|
|
86
|
-
* @param options.batchType Batch type used for inserting elements. See {@link module:engine/model/batch~Batch#constructor}.
|
|
87
|
-
*/
|
|
88
|
-
export function _setModelData(model, data, options = {}) {
|
|
89
|
-
if (!(model instanceof Model)) {
|
|
90
|
-
throw new TypeError('Model needs to be an instance of module:engine/model/model~Model.');
|
|
91
|
-
}
|
|
92
|
-
let modelDocumentFragment;
|
|
93
|
-
let selection = null;
|
|
94
|
-
const modelRoot = model.document.getRoot(options.rootName || 'main');
|
|
95
|
-
// Parse data string to model.
|
|
96
|
-
const parsedResult = _setModelData._parse(data, model.schema, {
|
|
97
|
-
lastRangeBackward: options.lastRangeBackward,
|
|
98
|
-
selectionAttributes: options.selectionAttributes,
|
|
99
|
-
context: [modelRoot.name],
|
|
100
|
-
inlineObjectElements: options.inlineObjectElements
|
|
101
|
-
});
|
|
102
|
-
// Retrieve DocumentFragment and Selection from parsed model.
|
|
103
|
-
if ('model' in parsedResult) {
|
|
104
|
-
modelDocumentFragment = parsedResult.model;
|
|
105
|
-
selection = parsedResult.selection;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
modelDocumentFragment = parsedResult;
|
|
109
|
-
}
|
|
110
|
-
if (options.batchType !== undefined) {
|
|
111
|
-
model.enqueueChange(options.batchType, writeToModel);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
model.change(writeToModel);
|
|
115
|
-
}
|
|
116
|
-
function writeToModel(writer) {
|
|
117
|
-
// Replace existing model in document by new one.
|
|
118
|
-
writer.remove(writer.createRangeIn(modelRoot));
|
|
119
|
-
writer.insert(modelDocumentFragment, modelRoot);
|
|
120
|
-
// Clean up previous document selection.
|
|
121
|
-
writer.setSelection(null);
|
|
122
|
-
writer.removeSelectionAttribute(model.document.selection.getAttributeKeys());
|
|
123
|
-
// Update document selection if specified.
|
|
124
|
-
if (selection) {
|
|
125
|
-
const ranges = [];
|
|
126
|
-
for (const range of selection.getRanges()) {
|
|
127
|
-
const start = new ModelPosition(modelRoot, range.start.path);
|
|
128
|
-
const end = new ModelPosition(modelRoot, range.end.path);
|
|
129
|
-
ranges.push(new ModelRange(start, end));
|
|
130
|
-
}
|
|
131
|
-
writer.setSelection(ranges, { backward: selection.isBackward });
|
|
132
|
-
if (options.selectionAttributes) {
|
|
133
|
-
writer.setSelectionAttribute(selection.getAttributes());
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
// Set parse as setData private method - needed for testing/spying.
|
|
139
|
-
_setModelData._parse = _parseModel;
|
|
140
|
-
/**
|
|
141
|
-
* Converts model nodes to HTML-like string representation.
|
|
142
|
-
*
|
|
143
|
-
* **Note:** A {@link module:engine/model/text~ModelText text} node that contains attributes will be represented as:
|
|
144
|
-
*
|
|
145
|
-
* ```xml
|
|
146
|
-
* <$text attribute="value">Text data</$text>
|
|
147
|
-
* ```
|
|
148
|
-
*
|
|
149
|
-
* @param node A node to stringify.
|
|
150
|
-
* @param selectionOrPositionOrRange A selection instance whose ranges will be included in the returned string data.
|
|
151
|
-
* If a range instance is provided, it will be converted to a selection containing this range. If a position instance
|
|
152
|
-
* is provided, it will be converted to a selection containing one range collapsed at this position.
|
|
153
|
-
* @param markers Markers to include.
|
|
154
|
-
* @returns An HTML-like string representing the model.
|
|
155
|
-
*/
|
|
156
|
-
export function _stringifyModel(node, selectionOrPositionOrRange = null, markers = null) {
|
|
157
|
-
const model = new Model();
|
|
158
|
-
const mapper = new Mapper();
|
|
159
|
-
let selection = null;
|
|
160
|
-
let range;
|
|
161
|
-
// Create a range witch wraps passed node.
|
|
162
|
-
if (node instanceof ModelRootElement || node instanceof ModelDocumentFragment) {
|
|
163
|
-
range = model.createRangeIn(node);
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
// Node is detached - create new document fragment.
|
|
167
|
-
if (!node.parent) {
|
|
168
|
-
const fragment = new ModelDocumentFragment(node);
|
|
169
|
-
range = model.createRangeIn(fragment);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
range = new ModelRange(model.createPositionBefore(node), model.createPositionAfter(node));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
// Get selection from passed selection or position or range if at least one is specified.
|
|
176
|
-
if (selectionOrPositionOrRange instanceof ModelSelection) {
|
|
177
|
-
selection = selectionOrPositionOrRange;
|
|
178
|
-
}
|
|
179
|
-
else if (selectionOrPositionOrRange instanceof ModelDocumentSelection) {
|
|
180
|
-
selection = selectionOrPositionOrRange;
|
|
181
|
-
}
|
|
182
|
-
else if (selectionOrPositionOrRange instanceof ModelRange) {
|
|
183
|
-
selection = new ModelSelection(selectionOrPositionOrRange);
|
|
184
|
-
}
|
|
185
|
-
else if (selectionOrPositionOrRange instanceof ModelPosition) {
|
|
186
|
-
selection = new ModelSelection(selectionOrPositionOrRange);
|
|
187
|
-
}
|
|
188
|
-
// Set up conversion.
|
|
189
|
-
// Create a temporary view controller.
|
|
190
|
-
const stylesProcessor = new StylesProcessor();
|
|
191
|
-
const view = new EditingView(stylesProcessor);
|
|
192
|
-
const viewDocument = view.document;
|
|
193
|
-
const viewRoot = new ViewRootEditableElement(viewDocument, 'div');
|
|
194
|
-
// Create a temporary root element in view document.
|
|
195
|
-
viewRoot.rootName = 'main';
|
|
196
|
-
viewDocument.roots.add(viewRoot);
|
|
197
|
-
// Create and setup downcast dispatcher.
|
|
198
|
-
const downcastDispatcher = new DowncastDispatcher({ mapper, schema: model.schema });
|
|
199
|
-
// Bind root elements.
|
|
200
|
-
mapper.bindElements(node.root, viewRoot);
|
|
201
|
-
downcastDispatcher.on('insert:$text', insertText());
|
|
202
|
-
downcastDispatcher.on('insert', insertAttributesAndChildren(), { priority: 'lowest' });
|
|
203
|
-
downcastDispatcher.on('attribute', (evt, data, conversionApi) => {
|
|
204
|
-
if (data.item instanceof ModelSelection || data.item instanceof ModelDocumentSelection || data.item.is('$textProxy')) {
|
|
205
|
-
const converter = wrap((modelAttributeValue, { writer }) => {
|
|
206
|
-
return writer.createAttributeElement('model-text-with-attributes', { [data.attributeKey]: stringifyAttributeValue(modelAttributeValue) });
|
|
207
|
-
});
|
|
208
|
-
converter(evt, data, conversionApi);
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
downcastDispatcher.on('insert', insertElement(modelItem => {
|
|
212
|
-
// Stringify object types values for properly display as an output string.
|
|
213
|
-
const attributes = convertAttributes(modelItem.getAttributes(), stringifyAttributeValue);
|
|
214
|
-
return new ViewContainerElement(viewDocument, modelItem.name, attributes);
|
|
215
|
-
}));
|
|
216
|
-
downcastDispatcher.on('selection', convertRangeSelection());
|
|
217
|
-
downcastDispatcher.on('selection', convertCollapsedSelection());
|
|
218
|
-
downcastDispatcher.on('addMarker', insertUIElement((data, { writer }) => {
|
|
219
|
-
const name = data.markerName + ':' + (data.isOpening ? 'start' : 'end');
|
|
220
|
-
return writer.createUIElement(name);
|
|
221
|
-
}));
|
|
222
|
-
const markersMap = new Map();
|
|
223
|
-
if (markers) {
|
|
224
|
-
// To provide stable results, sort markers by name.
|
|
225
|
-
for (const marker of Array.from(markers).sort((a, b) => a.name < b.name ? 1 : -1)) {
|
|
226
|
-
markersMap.set(marker.name, marker.getRange());
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
// Convert model to view.
|
|
230
|
-
const writer = view._writer;
|
|
231
|
-
downcastDispatcher.convert(range, markersMap, writer);
|
|
232
|
-
// Convert model selection to view selection.
|
|
233
|
-
if (selection) {
|
|
234
|
-
downcastDispatcher.convertSelection(selection, markers || model.markers, writer);
|
|
235
|
-
}
|
|
236
|
-
// Parse view to data string.
|
|
237
|
-
let data = _stringifyView(viewRoot, viewDocument.selection, { sameSelectionCharacters: true });
|
|
238
|
-
// Removing unnecessary <div> and </div> added because `viewRoot` was also stringified alongside input data.
|
|
239
|
-
data = data.substr(5, data.length - 11);
|
|
240
|
-
view.destroy();
|
|
241
|
-
// Replace valid XML `model-text-with-attributes` element name to `$text`.
|
|
242
|
-
return data.replace(new RegExp('model-text-with-attributes', 'g'), '$text');
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Parses an HTML-like string and returns the model {@link module:engine/model/rootelement~ModelRootElement rootElement}.
|
|
246
|
-
*
|
|
247
|
-
* **Note:** To create a {@link module:engine/model/text~ModelText text} node that contains attributes use:
|
|
248
|
-
*
|
|
249
|
-
* ```xml
|
|
250
|
-
* <$text attribute="value">Text data</$text>
|
|
251
|
-
* ```
|
|
252
|
-
*
|
|
253
|
-
* @param data HTML-like string to be parsed.
|
|
254
|
-
* @param schema A schema instance used by converters for element validation.
|
|
255
|
-
* @param options Additional configuration.
|
|
256
|
-
* @param options.selectionAttributes A list of attributes which will be passed to the selection.
|
|
257
|
-
* @param options.lastRangeBackward If set to `true`, the last range will be added as backward.
|
|
258
|
-
* @param options.context The conversion context. If not provided, the default `'$root'` will be used.
|
|
259
|
-
* @returns Returns the parsed model node or an object with two fields: `model` and `selection`,
|
|
260
|
-
* when selection ranges were included in the data to parse.
|
|
261
|
-
*/
|
|
262
|
-
export function _parseModel(data, schema, options = {}) {
|
|
263
|
-
const mapper = new Mapper();
|
|
264
|
-
// Replace not accepted by XML `$text` tag name by valid one `model-text-with-attributes`.
|
|
265
|
-
data = data.replace(new RegExp('\\$text', 'g'), 'model-text-with-attributes');
|
|
266
|
-
// Parse data to view using view utils.
|
|
267
|
-
const parsedResult = _parseView(data, {
|
|
268
|
-
sameSelectionCharacters: true,
|
|
269
|
-
lastRangeBackward: !!options.lastRangeBackward,
|
|
270
|
-
inlineObjectElements: options.inlineObjectElements
|
|
271
|
-
});
|
|
272
|
-
// Retrieve DocumentFragment and Selection from parsed view.
|
|
273
|
-
let viewDocumentFragment;
|
|
274
|
-
let viewSelection = null;
|
|
275
|
-
let selection = null;
|
|
276
|
-
if ('view' in parsedResult && 'selection' in parsedResult) {
|
|
277
|
-
viewDocumentFragment = parsedResult.view;
|
|
278
|
-
viewSelection = parsedResult.selection;
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
viewDocumentFragment = parsedResult;
|
|
282
|
-
}
|
|
283
|
-
// Set up upcast dispatcher.
|
|
284
|
-
const modelController = new Model();
|
|
285
|
-
const upcastDispatcher = new UpcastDispatcher({ schema });
|
|
286
|
-
upcastDispatcher.on('documentFragment', convertToModelFragment(mapper));
|
|
287
|
-
upcastDispatcher.on('element:model-text-with-attributes', convertToModelText());
|
|
288
|
-
upcastDispatcher.on('element', convertToModelElement(mapper));
|
|
289
|
-
upcastDispatcher.on('text', convertToModelText());
|
|
290
|
-
// Convert view to model.
|
|
291
|
-
let model = modelController.change(writer => upcastDispatcher.convert(viewDocumentFragment.root, writer, options.context || '$root'));
|
|
292
|
-
mapper.bindElements(model, viewDocumentFragment.root);
|
|
293
|
-
// If root DocumentFragment contains only one element - return that element.
|
|
294
|
-
if (model.childCount == 1) {
|
|
295
|
-
model = model.getChild(0);
|
|
296
|
-
}
|
|
297
|
-
// Convert view selection to model selection.
|
|
298
|
-
if (viewSelection) {
|
|
299
|
-
const ranges = [];
|
|
300
|
-
// Convert ranges.
|
|
301
|
-
for (const viewRange of viewSelection.getRanges()) {
|
|
302
|
-
ranges.push(mapper.toModelRange(viewRange));
|
|
303
|
-
}
|
|
304
|
-
// Create new selection.
|
|
305
|
-
selection = new ModelSelection(ranges, { backward: viewSelection.isBackward });
|
|
306
|
-
// Set attributes to selection if specified.
|
|
307
|
-
for (const [key, value] of toMap(options.selectionAttributes || [])) {
|
|
308
|
-
selection.setAttribute(key, value);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
// Return model end selection when selection was specified.
|
|
312
|
-
if (selection) {
|
|
313
|
-
return { model, selection };
|
|
314
|
-
}
|
|
315
|
-
// Otherwise return model only.
|
|
316
|
-
return model;
|
|
317
|
-
}
|
|
318
|
-
// -- Converters view -> model -----------------------------------------------------
|
|
319
|
-
function convertToModelFragment(mapper) {
|
|
320
|
-
return (evt, data, conversionApi) => {
|
|
321
|
-
const childrenResult = conversionApi.convertChildren(data.viewItem, data.modelCursor);
|
|
322
|
-
mapper.bindElements(data.modelCursor.parent, data.viewItem);
|
|
323
|
-
data = Object.assign(data, childrenResult);
|
|
324
|
-
evt.stop();
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
function convertToModelElement(mapper) {
|
|
328
|
-
return (evt, data, conversionApi) => {
|
|
329
|
-
const elementName = data.viewItem.name;
|
|
330
|
-
if (!conversionApi.schema.checkChild(data.modelCursor, elementName)) {
|
|
331
|
-
throw new Error(`Element '${elementName}' was not allowed in given position.`);
|
|
332
|
-
}
|
|
333
|
-
// View attribute value is a string so we want to typecast it to the original type.
|
|
334
|
-
// E.g. `bold="true"` - value will be parsed from string `"true"` to boolean `true`.
|
|
335
|
-
const attributes = convertAttributes(data.viewItem.getAttributes(), parseAttributeValue);
|
|
336
|
-
const element = conversionApi.writer.createElement(data.viewItem.name, attributes);
|
|
337
|
-
conversionApi.writer.insert(element, data.modelCursor);
|
|
338
|
-
mapper.bindElements(element, data.viewItem);
|
|
339
|
-
conversionApi.convertChildren(data.viewItem, element);
|
|
340
|
-
data.modelRange = ModelRange._createOn(element);
|
|
341
|
-
data.modelCursor = data.modelRange.end;
|
|
342
|
-
evt.stop();
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
function convertToModelText() {
|
|
346
|
-
return (evt, data, conversionApi) => {
|
|
347
|
-
if (!conversionApi.schema.checkChild(data.modelCursor, '$text')) {
|
|
348
|
-
throw new Error('Text was not allowed in given position.');
|
|
349
|
-
}
|
|
350
|
-
let node;
|
|
351
|
-
if (data.viewItem.is('element')) {
|
|
352
|
-
// View attribute value is a string so we want to typecast it to the original type.
|
|
353
|
-
// E.g. `bold="true"` - value will be parsed from string `"true"` to boolean `true`.
|
|
354
|
-
const attributes = convertAttributes(data.viewItem.getAttributes(), parseAttributeValue);
|
|
355
|
-
const viewText = data.viewItem.getChild(0);
|
|
356
|
-
node = conversionApi.writer.createText(viewText.data, attributes);
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
node = conversionApi.writer.createText(data.viewItem.data);
|
|
360
|
-
}
|
|
361
|
-
conversionApi.writer.insert(node, data.modelCursor);
|
|
362
|
-
data.modelRange = ModelRange._createFromPositionAndShift(data.modelCursor, node.offsetSize);
|
|
363
|
-
data.modelCursor = data.modelRange.end;
|
|
364
|
-
evt.stop();
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
// Tries to get original type of attribute value using JSON parsing:
|
|
368
|
-
//
|
|
369
|
-
// `'true'` => `true`
|
|
370
|
-
// `'1'` => `1`
|
|
371
|
-
// `'{"x":1,"y":2}'` => `{ x: 1, y: 2 }`
|
|
372
|
-
//
|
|
373
|
-
// Parse error means that value should be a string:
|
|
374
|
-
//
|
|
375
|
-
// `'foobar'` => `'foobar'`
|
|
376
|
-
function parseAttributeValue(attribute) {
|
|
377
|
-
try {
|
|
378
|
-
return JSON.parse(attribute);
|
|
379
|
-
}
|
|
380
|
-
catch {
|
|
381
|
-
return attribute;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
// When value is an Object stringify it.
|
|
385
|
-
function stringifyAttributeValue(data) {
|
|
386
|
-
if (isPlainObject(data)) {
|
|
387
|
-
return JSON.stringify(data);
|
|
388
|
-
}
|
|
389
|
-
return data;
|
|
390
|
-
}
|
|
391
|
-
// Loop trough attributes map and converts each value by passed converter.
|
|
392
|
-
function* convertAttributes(attributes, converter) {
|
|
393
|
-
for (const [key, value] of attributes) {
|
|
394
|
-
yield [key, converter(value)];
|
|
395
|
-
}
|
|
396
|
-
}
|
|
@@ -1,116 +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/dev-utils/operationreplayer
|
|
7
|
-
*/
|
|
8
|
-
import { OperationFactory } from '../model/operation/operationfactory.js';
|
|
9
|
-
/**
|
|
10
|
-
* Operation replayer is a development tool created for easy replaying of operations on the document from stringified operations.
|
|
11
|
-
*
|
|
12
|
-
* @internal
|
|
13
|
-
*/
|
|
14
|
-
export class OperationReplayer {
|
|
15
|
-
_model;
|
|
16
|
-
_logSeparator;
|
|
17
|
-
_operationsToReplay;
|
|
18
|
-
/**
|
|
19
|
-
* @param model Data model.
|
|
20
|
-
* @param logSeparator Separator between operations.
|
|
21
|
-
* @param stringifiedOperations Operations to replay.
|
|
22
|
-
*/
|
|
23
|
-
constructor(model, logSeparator, stringifiedOperations) {
|
|
24
|
-
this._model = model;
|
|
25
|
-
this._logSeparator = logSeparator;
|
|
26
|
-
this.setStringifiedOperations(stringifiedOperations);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Parses the given string containing stringified operations and sets parsed operations as operations to replay.
|
|
30
|
-
*
|
|
31
|
-
* @param stringifiedOperations Stringified operations to replay.
|
|
32
|
-
*/
|
|
33
|
-
setStringifiedOperations(stringifiedOperations) {
|
|
34
|
-
if (stringifiedOperations === '') {
|
|
35
|
-
this._operationsToReplay = [];
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
this._operationsToReplay = stringifiedOperations
|
|
39
|
-
.split(this._logSeparator)
|
|
40
|
-
.map(stringifiedOperation => JSON.parse(stringifiedOperation));
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Returns operations to replay.
|
|
44
|
-
*/
|
|
45
|
-
getOperationsToReplay() {
|
|
46
|
-
return this._operationsToReplay;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Applies all operations with a delay between actions.
|
|
50
|
-
*
|
|
51
|
-
* @param timeInterval Time between applying operations.
|
|
52
|
-
*/
|
|
53
|
-
play(timeInterval = 1000) {
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias, consistent-this
|
|
55
|
-
const operationReplayer = this;
|
|
56
|
-
return new Promise((res, rej) => {
|
|
57
|
-
play();
|
|
58
|
-
function play() {
|
|
59
|
-
operationReplayer.applyNextOperation().then(isFinished => {
|
|
60
|
-
if (isFinished) {
|
|
61
|
-
return res();
|
|
62
|
-
}
|
|
63
|
-
setTimeout(play, timeInterval);
|
|
64
|
-
}).catch(err => {
|
|
65
|
-
rej(err);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Applies `numberOfOperations` operations, beginning after the last applied operation (or first, if no operations were applied).
|
|
72
|
-
*
|
|
73
|
-
* @param numberOfOperations The number of operations to apply.
|
|
74
|
-
*/
|
|
75
|
-
applyOperations(numberOfOperations) {
|
|
76
|
-
if (numberOfOperations <= 0) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
return this.applyNextOperation()
|
|
80
|
-
.then(isFinished => {
|
|
81
|
-
if (!isFinished) {
|
|
82
|
-
return this.applyOperations(numberOfOperations - 1);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Applies all operations to replay at once.
|
|
88
|
-
*/
|
|
89
|
-
applyAllOperations() {
|
|
90
|
-
return this.applyNextOperation()
|
|
91
|
-
.then(isFinished => {
|
|
92
|
-
if (!isFinished) {
|
|
93
|
-
return this.applyAllOperations();
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Applies the next operation to replay. Returns a promise with the `isFinished` parameter that is `true` if the last
|
|
99
|
-
* operation in the replayer has been applied, `false` otherwise.
|
|
100
|
-
*/
|
|
101
|
-
applyNextOperation() {
|
|
102
|
-
const model = this._model;
|
|
103
|
-
return new Promise(res => {
|
|
104
|
-
model.enqueueChange(writer => {
|
|
105
|
-
const operationJson = this._operationsToReplay.shift();
|
|
106
|
-
if (!operationJson) {
|
|
107
|
-
return res(true);
|
|
108
|
-
}
|
|
109
|
-
const operation = OperationFactory.fromJSON(operationJson, model.document);
|
|
110
|
-
writer.batch.addOperation(operation);
|
|
111
|
-
model.applyOperation(operation);
|
|
112
|
-
res(false);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|