@ckeditor/ckeditor5-engine 0.0.0-internal-20241017.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/LICENSE.md +21 -0
- package/README.md +36 -0
- package/dist/controller/datacontroller.d.ts +339 -0
- package/dist/controller/editingcontroller.d.ts +102 -0
- package/dist/conversion/conversion.d.ts +482 -0
- package/dist/conversion/conversionhelpers.d.ts +30 -0
- package/dist/conversion/downcastdispatcher.d.ts +566 -0
- package/dist/conversion/downcasthelpers.d.ts +1194 -0
- package/dist/conversion/mapper.d.ts +507 -0
- package/dist/conversion/modelconsumable.d.ts +205 -0
- package/dist/conversion/upcastdispatcher.d.ts +496 -0
- package/dist/conversion/upcasthelpers.d.ts +503 -0
- package/dist/conversion/viewconsumable.d.ts +373 -0
- package/dist/dataprocessor/basichtmlwriter.d.ts +22 -0
- package/dist/dataprocessor/dataprocessor.d.ts +65 -0
- package/dist/dataprocessor/htmldataprocessor.d.ts +80 -0
- package/dist/dataprocessor/htmlwriter.d.ts +20 -0
- package/dist/dataprocessor/xmldataprocessor.d.ts +94 -0
- package/dist/dev-utils/model.d.ts +130 -0
- package/dist/dev-utils/operationreplayer.d.ts +55 -0
- package/dist/dev-utils/utils.d.ts +41 -0
- package/dist/dev-utils/view.d.ts +324 -0
- package/dist/index-content.css +4 -0
- package/dist/index-editor.css +24 -0
- package/dist/index.css +49 -0
- package/dist/index.css.map +1 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.js +37775 -0
- package/dist/index.js.map +1 -0
- package/dist/model/batch.d.ts +110 -0
- package/dist/model/differ.d.ts +511 -0
- package/dist/model/document.d.ts +278 -0
- package/dist/model/documentfragment.d.ts +204 -0
- package/dist/model/documentselection.d.ts +424 -0
- package/dist/model/element.d.ts +169 -0
- package/dist/model/history.d.ts +118 -0
- package/dist/model/item.d.ts +18 -0
- package/dist/model/liveposition.d.ts +81 -0
- package/dist/model/liverange.d.ts +106 -0
- package/dist/model/markercollection.d.ts +339 -0
- package/dist/model/model.d.ts +923 -0
- package/dist/model/node.d.ts +259 -0
- package/dist/model/nodelist.d.ts +95 -0
- package/dist/model/operation/attributeoperation.d.ts +107 -0
- package/dist/model/operation/detachoperation.d.ts +64 -0
- package/dist/model/operation/insertoperation.d.ts +94 -0
- package/dist/model/operation/markeroperation.d.ts +95 -0
- package/dist/model/operation/mergeoperation.d.ts +104 -0
- package/dist/model/operation/moveoperation.d.ts +100 -0
- package/dist/model/operation/nooperation.d.ts +42 -0
- package/dist/model/operation/operation.d.ts +100 -0
- package/dist/model/operation/operationfactory.d.ts +22 -0
- package/dist/model/operation/renameoperation.d.ts +87 -0
- package/dist/model/operation/rootattributeoperation.d.ts +102 -0
- package/dist/model/operation/rootoperation.d.ts +80 -0
- package/dist/model/operation/splitoperation.d.ts +113 -0
- package/dist/model/operation/transform.d.ts +104 -0
- package/dist/model/operation/utils.d.ts +75 -0
- package/dist/model/position.d.ts +542 -0
- package/dist/model/range.d.ts +462 -0
- package/dist/model/rootelement.d.ts +64 -0
- package/dist/model/schema.d.ts +1334 -0
- package/dist/model/selection.d.ts +486 -0
- package/dist/model/text.d.ts +70 -0
- package/dist/model/textproxy.d.ts +148 -0
- package/dist/model/treewalker.d.ts +190 -0
- package/dist/model/typecheckable.d.ts +289 -0
- package/dist/model/utils/autoparagraphing.d.ts +41 -0
- package/dist/model/utils/deletecontent.d.ts +62 -0
- package/dist/model/utils/getselectedcontent.d.ts +34 -0
- package/dist/model/utils/insertcontent.d.ts +50 -0
- package/dist/model/utils/insertobject.d.ts +51 -0
- package/dist/model/utils/modifyselection.d.ts +52 -0
- package/dist/model/utils/selection-post-fixer.d.ts +78 -0
- package/dist/model/writer.d.ts +855 -0
- package/dist/view/attributeelement.d.ts +112 -0
- package/dist/view/containerelement.d.ts +53 -0
- package/dist/view/datatransfer.d.ts +83 -0
- package/dist/view/document.d.ts +188 -0
- package/dist/view/documentfragment.d.ts +157 -0
- package/dist/view/documentselection.d.ts +310 -0
- package/dist/view/domconverter.d.ts +665 -0
- package/dist/view/downcastwriter.d.ts +1000 -0
- package/dist/view/editableelement.d.ts +66 -0
- package/dist/view/element.d.ts +472 -0
- package/dist/view/elementdefinition.d.ts +91 -0
- package/dist/view/emptyelement.d.ts +45 -0
- package/dist/view/filler.d.ts +115 -0
- package/dist/view/item.d.ts +18 -0
- package/dist/view/matcher.d.ts +490 -0
- package/dist/view/node.d.ts +166 -0
- package/dist/view/observer/arrowkeysobserver.d.ts +49 -0
- package/dist/view/observer/bubblingemittermixin.d.ts +170 -0
- package/dist/view/observer/bubblingeventinfo.d.ts +51 -0
- package/dist/view/observer/clickobserver.d.ts +47 -0
- package/dist/view/observer/compositionobserver.d.ts +86 -0
- package/dist/view/observer/domeventdata.d.ts +54 -0
- package/dist/view/observer/domeventobserver.d.ts +82 -0
- package/dist/view/observer/fakeselectionobserver.d.ts +51 -0
- package/dist/view/observer/focusobserver.d.ts +98 -0
- package/dist/view/observer/inputobserver.d.ts +90 -0
- package/dist/view/observer/keyobserver.d.ts +70 -0
- package/dist/view/observer/mouseobserver.d.ts +93 -0
- package/dist/view/observer/mutationobserver.d.ts +119 -0
- package/dist/view/observer/observer.d.ts +93 -0
- package/dist/view/observer/selectionobserver.d.ts +151 -0
- package/dist/view/observer/tabobserver.d.ts +50 -0
- package/dist/view/placeholder.d.ts +100 -0
- package/dist/view/position.d.ts +192 -0
- package/dist/view/range.d.ts +283 -0
- package/dist/view/rawelement.d.ts +77 -0
- package/dist/view/renderer.d.ts +281 -0
- package/dist/view/rooteditableelement.d.ts +45 -0
- package/dist/view/selection.d.ts +379 -0
- package/dist/view/styles/background.d.ts +37 -0
- package/dist/view/styles/border.d.ts +47 -0
- package/dist/view/styles/margin.d.ts +33 -0
- package/dist/view/styles/padding.d.ts +33 -0
- package/dist/view/styles/utils.d.ts +97 -0
- package/dist/view/stylesmap.d.ts +677 -0
- package/dist/view/text.d.ts +78 -0
- package/dist/view/textproxy.d.ts +101 -0
- package/dist/view/treewalker.d.ts +199 -0
- package/dist/view/typecheckable.d.ts +452 -0
- package/dist/view/uielement.d.ts +100 -0
- package/dist/view/upcastwriter.d.ts +421 -0
- package/dist/view/view.d.ts +488 -0
- package/package.json +49 -0
- package/src/controller/datacontroller.d.ts +335 -0
- package/src/controller/datacontroller.js +481 -0
- package/src/controller/editingcontroller.d.ts +98 -0
- package/src/controller/editingcontroller.js +191 -0
- package/src/conversion/conversion.d.ts +478 -0
- package/src/conversion/conversion.js +601 -0
- package/src/conversion/conversionhelpers.d.ts +26 -0
- package/src/conversion/conversionhelpers.js +32 -0
- package/src/conversion/downcastdispatcher.d.ts +562 -0
- package/src/conversion/downcastdispatcher.js +548 -0
- package/src/conversion/downcasthelpers.d.ts +1190 -0
- package/src/conversion/downcasthelpers.js +2161 -0
- package/src/conversion/mapper.d.ts +503 -0
- package/src/conversion/mapper.js +536 -0
- package/src/conversion/modelconsumable.d.ts +201 -0
- package/src/conversion/modelconsumable.js +333 -0
- package/src/conversion/upcastdispatcher.d.ts +492 -0
- package/src/conversion/upcastdispatcher.js +460 -0
- package/src/conversion/upcasthelpers.d.ts +499 -0
- package/src/conversion/upcasthelpers.js +943 -0
- package/src/conversion/viewconsumable.d.ts +369 -0
- package/src/conversion/viewconsumable.js +535 -0
- package/src/dataprocessor/basichtmlwriter.d.ts +18 -0
- package/src/dataprocessor/basichtmlwriter.js +20 -0
- package/src/dataprocessor/dataprocessor.d.ts +61 -0
- package/src/dataprocessor/dataprocessor.js +5 -0
- package/src/dataprocessor/htmldataprocessor.d.ts +76 -0
- package/src/dataprocessor/htmldataprocessor.js +96 -0
- package/src/dataprocessor/htmlwriter.d.ts +16 -0
- package/src/dataprocessor/htmlwriter.js +5 -0
- package/src/dataprocessor/xmldataprocessor.d.ts +90 -0
- package/src/dataprocessor/xmldataprocessor.js +108 -0
- package/src/dev-utils/model.d.ts +126 -0
- package/src/dev-utils/model.js +397 -0
- package/src/dev-utils/operationreplayer.d.ts +51 -0
- package/src/dev-utils/operationreplayer.js +112 -0
- package/src/dev-utils/utils.d.ts +37 -0
- package/src/dev-utils/utils.js +80 -0
- package/src/dev-utils/view.d.ts +320 -0
- package/src/dev-utils/view.js +970 -0
- package/src/index.d.ts +118 -0
- package/src/index.js +83 -0
- package/src/model/batch.d.ts +106 -0
- package/src/model/batch.js +96 -0
- package/src/model/differ.d.ts +507 -0
- package/src/model/differ.js +1285 -0
- package/src/model/document.d.ts +274 -0
- package/src/model/document.js +360 -0
- package/src/model/documentfragment.d.ts +200 -0
- package/src/model/documentfragment.js +306 -0
- package/src/model/documentselection.d.ts +420 -0
- package/src/model/documentselection.js +993 -0
- package/src/model/element.d.ts +165 -0
- package/src/model/element.js +281 -0
- package/src/model/history.d.ts +114 -0
- package/src/model/history.js +207 -0
- package/src/model/item.d.ts +14 -0
- package/src/model/item.js +5 -0
- package/src/model/liveposition.d.ts +77 -0
- package/src/model/liveposition.js +93 -0
- package/src/model/liverange.d.ts +102 -0
- package/src/model/liverange.js +120 -0
- package/src/model/markercollection.d.ts +335 -0
- package/src/model/markercollection.js +403 -0
- package/src/model/model.d.ts +919 -0
- package/src/model/model.js +838 -0
- package/src/model/node.d.ts +255 -0
- package/src/model/node.js +373 -0
- package/src/model/nodelist.d.ts +91 -0
- package/src/model/nodelist.js +170 -0
- package/src/model/operation/attributeoperation.d.ts +103 -0
- package/src/model/operation/attributeoperation.js +148 -0
- package/src/model/operation/detachoperation.d.ts +60 -0
- package/src/model/operation/detachoperation.js +77 -0
- package/src/model/operation/insertoperation.d.ts +90 -0
- package/src/model/operation/insertoperation.js +135 -0
- package/src/model/operation/markeroperation.d.ts +91 -0
- package/src/model/operation/markeroperation.js +107 -0
- package/src/model/operation/mergeoperation.d.ts +100 -0
- package/src/model/operation/mergeoperation.js +167 -0
- package/src/model/operation/moveoperation.d.ts +96 -0
- package/src/model/operation/moveoperation.js +164 -0
- package/src/model/operation/nooperation.d.ts +38 -0
- package/src/model/operation/nooperation.js +48 -0
- package/src/model/operation/operation.d.ts +96 -0
- package/src/model/operation/operation.js +59 -0
- package/src/model/operation/operationfactory.d.ts +18 -0
- package/src/model/operation/operationfactory.js +44 -0
- package/src/model/operation/renameoperation.d.ts +83 -0
- package/src/model/operation/renameoperation.js +115 -0
- package/src/model/operation/rootattributeoperation.d.ts +98 -0
- package/src/model/operation/rootattributeoperation.js +155 -0
- package/src/model/operation/rootoperation.d.ts +76 -0
- package/src/model/operation/rootoperation.js +90 -0
- package/src/model/operation/splitoperation.d.ts +109 -0
- package/src/model/operation/splitoperation.js +194 -0
- package/src/model/operation/transform.d.ts +100 -0
- package/src/model/operation/transform.js +2119 -0
- package/src/model/operation/utils.d.ts +71 -0
- package/src/model/operation/utils.js +217 -0
- package/src/model/position.d.ts +538 -0
- package/src/model/position.js +977 -0
- package/src/model/range.d.ts +458 -0
- package/src/model/range.js +872 -0
- package/src/model/rootelement.d.ts +60 -0
- package/src/model/rootelement.js +74 -0
- package/src/model/schema.d.ts +1330 -0
- package/src/model/schema.js +1524 -0
- package/src/model/selection.d.ts +482 -0
- package/src/model/selection.js +789 -0
- package/src/model/text.d.ts +66 -0
- package/src/model/text.js +85 -0
- package/src/model/textproxy.d.ts +144 -0
- package/src/model/textproxy.js +189 -0
- package/src/model/treewalker.d.ts +186 -0
- package/src/model/treewalker.js +244 -0
- package/src/model/typecheckable.d.ts +285 -0
- package/src/model/typecheckable.js +16 -0
- package/src/model/utils/autoparagraphing.d.ts +37 -0
- package/src/model/utils/autoparagraphing.js +63 -0
- package/src/model/utils/deletecontent.d.ts +58 -0
- package/src/model/utils/deletecontent.js +488 -0
- package/src/model/utils/getselectedcontent.d.ts +30 -0
- package/src/model/utils/getselectedcontent.js +125 -0
- package/src/model/utils/insertcontent.d.ts +46 -0
- package/src/model/utils/insertcontent.js +661 -0
- package/src/model/utils/insertobject.d.ts +47 -0
- package/src/model/utils/insertobject.js +135 -0
- package/src/model/utils/modifyselection.d.ts +48 -0
- package/src/model/utils/modifyselection.js +186 -0
- package/src/model/utils/selection-post-fixer.d.ts +74 -0
- package/src/model/utils/selection-post-fixer.js +260 -0
- package/src/model/writer.d.ts +851 -0
- package/src/model/writer.js +1306 -0
- package/src/view/attributeelement.d.ts +108 -0
- package/src/view/attributeelement.js +185 -0
- package/src/view/containerelement.d.ts +49 -0
- package/src/view/containerelement.js +80 -0
- package/src/view/datatransfer.d.ts +79 -0
- package/src/view/datatransfer.js +98 -0
- package/src/view/document.d.ts +184 -0
- package/src/view/document.js +122 -0
- package/src/view/documentfragment.d.ts +153 -0
- package/src/view/documentfragment.js +234 -0
- package/src/view/documentselection.d.ts +306 -0
- package/src/view/documentselection.js +256 -0
- package/src/view/domconverter.d.ts +661 -0
- package/src/view/domconverter.js +1523 -0
- package/src/view/downcastwriter.d.ts +996 -0
- package/src/view/downcastwriter.js +1696 -0
- package/src/view/editableelement.d.ts +62 -0
- package/src/view/editableelement.js +62 -0
- package/src/view/element.d.ts +468 -0
- package/src/view/element.js +724 -0
- package/src/view/elementdefinition.d.ts +87 -0
- package/src/view/elementdefinition.js +5 -0
- package/src/view/emptyelement.d.ts +41 -0
- package/src/view/emptyelement.js +73 -0
- package/src/view/filler.d.ts +111 -0
- package/src/view/filler.js +150 -0
- package/src/view/item.d.ts +14 -0
- package/src/view/item.js +5 -0
- package/src/view/matcher.d.ts +486 -0
- package/src/view/matcher.js +507 -0
- package/src/view/node.d.ts +162 -0
- package/src/view/node.js +226 -0
- package/src/view/observer/arrowkeysobserver.d.ts +45 -0
- package/src/view/observer/arrowkeysobserver.js +40 -0
- package/src/view/observer/bubblingemittermixin.d.ts +166 -0
- package/src/view/observer/bubblingemittermixin.js +172 -0
- package/src/view/observer/bubblingeventinfo.d.ts +47 -0
- package/src/view/observer/bubblingeventinfo.js +37 -0
- package/src/view/observer/clickobserver.d.ts +43 -0
- package/src/view/observer/clickobserver.js +29 -0
- package/src/view/observer/compositionobserver.d.ts +82 -0
- package/src/view/observer/compositionobserver.js +62 -0
- package/src/view/observer/domeventdata.d.ts +50 -0
- package/src/view/observer/domeventdata.js +47 -0
- package/src/view/observer/domeventobserver.d.ts +78 -0
- package/src/view/observer/domeventobserver.js +84 -0
- package/src/view/observer/fakeselectionobserver.d.ts +47 -0
- package/src/view/observer/fakeselectionobserver.js +91 -0
- package/src/view/observer/focusobserver.d.ts +94 -0
- package/src/view/observer/focusobserver.js +116 -0
- package/src/view/observer/inputobserver.d.ts +86 -0
- package/src/view/observer/inputobserver.js +166 -0
- package/src/view/observer/keyobserver.d.ts +66 -0
- package/src/view/observer/keyobserver.js +39 -0
- package/src/view/observer/mouseobserver.d.ts +89 -0
- package/src/view/observer/mouseobserver.js +29 -0
- package/src/view/observer/mutationobserver.d.ts +115 -0
- package/src/view/observer/mutationobserver.js +203 -0
- package/src/view/observer/observer.d.ts +89 -0
- package/src/view/observer/observer.js +84 -0
- package/src/view/observer/selectionobserver.d.ts +147 -0
- package/src/view/observer/selectionobserver.js +220 -0
- package/src/view/observer/tabobserver.d.ts +46 -0
- package/src/view/observer/tabobserver.js +42 -0
- package/src/view/placeholder.d.ts +96 -0
- package/src/view/placeholder.js +267 -0
- package/src/view/position.d.ts +188 -0
- package/src/view/position.js +322 -0
- package/src/view/range.d.ts +279 -0
- package/src/view/range.js +430 -0
- package/src/view/rawelement.d.ts +73 -0
- package/src/view/rawelement.js +105 -0
- package/src/view/renderer.d.ts +277 -0
- package/src/view/renderer.js +1056 -0
- package/src/view/rooteditableelement.d.ts +41 -0
- package/src/view/rooteditableelement.js +69 -0
- package/src/view/selection.d.ts +375 -0
- package/src/view/selection.js +559 -0
- package/src/view/styles/background.d.ts +33 -0
- package/src/view/styles/background.js +74 -0
- package/src/view/styles/border.d.ts +43 -0
- package/src/view/styles/border.js +316 -0
- package/src/view/styles/margin.d.ts +29 -0
- package/src/view/styles/margin.js +34 -0
- package/src/view/styles/padding.d.ts +29 -0
- package/src/view/styles/padding.js +34 -0
- package/src/view/styles/utils.d.ts +93 -0
- package/src/view/styles/utils.js +219 -0
- package/src/view/stylesmap.d.ts +673 -0
- package/src/view/stylesmap.js +762 -0
- package/src/view/text.d.ts +74 -0
- package/src/view/text.js +93 -0
- package/src/view/textproxy.d.ts +97 -0
- package/src/view/textproxy.js +124 -0
- package/src/view/treewalker.d.ts +195 -0
- package/src/view/treewalker.js +327 -0
- package/src/view/typecheckable.d.ts +448 -0
- package/src/view/typecheckable.js +19 -0
- package/src/view/uielement.d.ts +96 -0
- package/src/view/uielement.js +183 -0
- package/src/view/upcastwriter.d.ts +417 -0
- package/src/view/upcastwriter.js +359 -0
- package/src/view/view.d.ts +484 -0
- package/src/view/view.js +560 -0
- package/theme/placeholder.css +36 -0
- package/theme/renderer.css +9 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module engine/view/observer/selectionobserver
|
|
7
|
+
*/
|
|
8
|
+
/* global setInterval, clearInterval */
|
|
9
|
+
import Observer from './observer.js';
|
|
10
|
+
import MutationObserver from './mutationobserver.js';
|
|
11
|
+
import FocusObserver from './focusobserver.js';
|
|
12
|
+
import { env } from '@ckeditor/ckeditor5-utils';
|
|
13
|
+
import { debounce } from 'lodash-es';
|
|
14
|
+
/**
|
|
15
|
+
* Selection observer class observes selection changes in the document. If a selection changes on the document this
|
|
16
|
+
* observer checks if the DOM selection is different from the {@link module:engine/view/document~Document#selection view selection}.
|
|
17
|
+
* The selection observer fires {@link module:engine/view/document~Document#event:selectionChange} event only if
|
|
18
|
+
* a selection change was the only change in the document and the DOM selection is different from the view selection.
|
|
19
|
+
*
|
|
20
|
+
* This observer also manages the {@link module:engine/view/document~Document#isSelecting} property of the view document.
|
|
21
|
+
*
|
|
22
|
+
* Note that this observer is attached by the {@link module:engine/view/view~View} and is available by default.
|
|
23
|
+
*/
|
|
24
|
+
export default class SelectionObserver extends Observer {
|
|
25
|
+
constructor(view) {
|
|
26
|
+
super(view);
|
|
27
|
+
this.mutationObserver = view.getObserver(MutationObserver);
|
|
28
|
+
this.focusObserver = view.getObserver(FocusObserver);
|
|
29
|
+
this.selection = this.document.selection;
|
|
30
|
+
this.domConverter = view.domConverter;
|
|
31
|
+
this._documents = new WeakSet();
|
|
32
|
+
this._fireSelectionChangeDoneDebounced = debounce(data => {
|
|
33
|
+
this.document.fire('selectionChangeDone', data);
|
|
34
|
+
}, 200);
|
|
35
|
+
this._clearInfiniteLoopInterval = setInterval(() => this._clearInfiniteLoop(), 1000);
|
|
36
|
+
this._documentIsSelectingInactivityTimeoutDebounced = debounce(() => (this.document.isSelecting = false), 5000);
|
|
37
|
+
this._loopbackCounter = 0;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @inheritDoc
|
|
41
|
+
*/
|
|
42
|
+
observe(domElement) {
|
|
43
|
+
const domDocument = domElement.ownerDocument;
|
|
44
|
+
const startDocumentIsSelecting = () => {
|
|
45
|
+
this.document.isSelecting = true;
|
|
46
|
+
// Let's activate the safety timeout each time the document enters the "is selecting" state.
|
|
47
|
+
this._documentIsSelectingInactivityTimeoutDebounced();
|
|
48
|
+
};
|
|
49
|
+
const endDocumentIsSelecting = () => {
|
|
50
|
+
if (!this.document.isSelecting) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
// Make sure that model selection is up-to-date at the end of selecting process.
|
|
54
|
+
// Sometimes `selectionchange` events could arrive after the `mouseup` event and that selection could be already outdated.
|
|
55
|
+
this._handleSelectionChange(domDocument);
|
|
56
|
+
this.document.isSelecting = false;
|
|
57
|
+
// The safety timeout can be canceled when the document leaves the "is selecting" state.
|
|
58
|
+
this._documentIsSelectingInactivityTimeoutDebounced.cancel();
|
|
59
|
+
};
|
|
60
|
+
// The document has the "is selecting" state while the user keeps making (extending) the selection
|
|
61
|
+
// (e.g. by holding the mouse button and moving the cursor). The state resets when they either released
|
|
62
|
+
// the mouse button or interrupted the process by pressing or releasing any key.
|
|
63
|
+
this.listenTo(domElement, 'selectstart', startDocumentIsSelecting, { priority: 'highest' });
|
|
64
|
+
this.listenTo(domElement, 'keydown', endDocumentIsSelecting, { priority: 'highest', useCapture: true });
|
|
65
|
+
this.listenTo(domElement, 'keyup', endDocumentIsSelecting, { priority: 'highest', useCapture: true });
|
|
66
|
+
// Add document-wide listeners only once. This method could be called for multiple editing roots.
|
|
67
|
+
if (this._documents.has(domDocument)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// This listener is using capture mode to make sure that selection is upcasted before any other
|
|
71
|
+
// handler would like to check it and update (for example table multi cell selection).
|
|
72
|
+
this.listenTo(domDocument, 'mouseup', endDocumentIsSelecting, { priority: 'highest', useCapture: true });
|
|
73
|
+
this.listenTo(domDocument, 'selectionchange', () => {
|
|
74
|
+
// @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
|
|
75
|
+
// @if CK_DEBUG_TYPING // _debouncedLine();
|
|
76
|
+
// @if CK_DEBUG_TYPING // const domSelection = domDocument.defaultView!.getSelection();
|
|
77
|
+
// @if CK_DEBUG_TYPING // console.group( '%c[SelectionObserver]%c selectionchange', 'color: green', ''
|
|
78
|
+
// @if CK_DEBUG_TYPING // );
|
|
79
|
+
// @if CK_DEBUG_TYPING // console.info( '%c[SelectionObserver]%c DOM Selection:', 'font-weight: bold; color: green', '',
|
|
80
|
+
// @if CK_DEBUG_TYPING // { node: domSelection!.anchorNode, offset: domSelection!.anchorOffset },
|
|
81
|
+
// @if CK_DEBUG_TYPING // { node: domSelection!.focusNode, offset: domSelection!.focusOffset }
|
|
82
|
+
// @if CK_DEBUG_TYPING // );
|
|
83
|
+
// @if CK_DEBUG_TYPING // }
|
|
84
|
+
// The Renderer is disabled while composing on non-android browsers, so we can't update the view selection
|
|
85
|
+
// because the DOM and view tree drifted apart. Position mapping could fail because of it.
|
|
86
|
+
if (this.document.isComposing && !env.isAndroid) {
|
|
87
|
+
// @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
|
|
88
|
+
// @if CK_DEBUG_TYPING // console.info( '%c[SelectionObserver]%c Selection change ignored (isComposing)',
|
|
89
|
+
// @if CK_DEBUG_TYPING // 'font-weight: bold; color: green', ''
|
|
90
|
+
// @if CK_DEBUG_TYPING // );
|
|
91
|
+
// @if CK_DEBUG_TYPING // console.groupEnd();
|
|
92
|
+
// @if CK_DEBUG_TYPING // }
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this._handleSelectionChange(domDocument);
|
|
96
|
+
// @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
|
|
97
|
+
// @if CK_DEBUG_TYPING // console.groupEnd();
|
|
98
|
+
// @if CK_DEBUG_TYPING // }
|
|
99
|
+
// Defer the safety timeout when the selection changes (e.g. the user keeps extending the selection
|
|
100
|
+
// using their mouse).
|
|
101
|
+
this._documentIsSelectingInactivityTimeoutDebounced();
|
|
102
|
+
});
|
|
103
|
+
// Update the model DocumentSelection just after the Renderer and the SelectionObserver are locked.
|
|
104
|
+
// We do this synchronously (without waiting for the `selectionchange` DOM event) as browser updates
|
|
105
|
+
// the DOM selection (but not visually) to span the text that is under composition and could be replaced.
|
|
106
|
+
this.listenTo(this.view.document, 'compositionstart', () => {
|
|
107
|
+
// @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
|
|
108
|
+
// @if CK_DEBUG_TYPING // const domSelection = domDocument.defaultView!.getSelection();
|
|
109
|
+
// @if CK_DEBUG_TYPING // console.group( '%c[SelectionObserver]%c update selection on compositionstart', 'color: green', ''
|
|
110
|
+
// @if CK_DEBUG_TYPING // );
|
|
111
|
+
// @if CK_DEBUG_TYPING // console.info( '%c[SelectionObserver]%c DOM Selection:', 'font-weight: bold; color: green', '',
|
|
112
|
+
// @if CK_DEBUG_TYPING // { node: domSelection!.anchorNode, offset: domSelection!.anchorOffset },
|
|
113
|
+
// @if CK_DEBUG_TYPING // { node: domSelection!.focusNode, offset: domSelection!.focusOffset }
|
|
114
|
+
// @if CK_DEBUG_TYPING // );
|
|
115
|
+
// @if CK_DEBUG_TYPING // }
|
|
116
|
+
this._handleSelectionChange(domDocument);
|
|
117
|
+
// @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
|
|
118
|
+
// @if CK_DEBUG_TYPING // console.groupEnd();
|
|
119
|
+
// @if CK_DEBUG_TYPING // }
|
|
120
|
+
}, { priority: 'lowest' });
|
|
121
|
+
this._documents.add(domDocument);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @inheritDoc
|
|
125
|
+
*/
|
|
126
|
+
stopObserving(domElement) {
|
|
127
|
+
this.stopListening(domElement);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @inheritDoc
|
|
131
|
+
*/
|
|
132
|
+
destroy() {
|
|
133
|
+
super.destroy();
|
|
134
|
+
clearInterval(this._clearInfiniteLoopInterval);
|
|
135
|
+
this._fireSelectionChangeDoneDebounced.cancel();
|
|
136
|
+
this._documentIsSelectingInactivityTimeoutDebounced.cancel();
|
|
137
|
+
}
|
|
138
|
+
/* istanbul ignore next -- @preserve */
|
|
139
|
+
_reportInfiniteLoop() {
|
|
140
|
+
// @if CK_DEBUG // throw new Error(
|
|
141
|
+
// @if CK_DEBUG // 'Selection change observer detected an infinite rendering loop.\n\n' +
|
|
142
|
+
// @if CK_DEBUG // '⚠️⚠️ Report this error on https://github.com/ckeditor/ckeditor5/issues/11658.'
|
|
143
|
+
// @if CK_DEBUG // );
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Selection change listener. {@link module:engine/view/observer/mutationobserver~MutationObserver#flush Flush} mutations, check if
|
|
147
|
+
* a selection changes and fires {@link module:engine/view/document~Document#event:selectionChange} event on every change
|
|
148
|
+
* and {@link module:engine/view/document~Document#event:selectionChangeDone} when a selection stop changing.
|
|
149
|
+
*
|
|
150
|
+
* @param domDocument DOM document.
|
|
151
|
+
*/
|
|
152
|
+
_handleSelectionChange(domDocument) {
|
|
153
|
+
if (!this.isEnabled) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const domSelection = domDocument.defaultView.getSelection();
|
|
157
|
+
if (this.checkShouldIgnoreEventFromTarget(domSelection.anchorNode)) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
// Ensure the mutation event will be before selection event on all browsers.
|
|
161
|
+
this.mutationObserver.flush();
|
|
162
|
+
const newViewSelection = this.domConverter.domSelectionToView(domSelection);
|
|
163
|
+
// Do not convert selection change if the new view selection has no ranges in it.
|
|
164
|
+
//
|
|
165
|
+
// It means that the DOM selection is in some way incorrect. Ranges that were in the DOM selection could not be
|
|
166
|
+
// converted to the view. This happens when the DOM selection was moved outside of the editable element.
|
|
167
|
+
if (newViewSelection.rangeCount == 0) {
|
|
168
|
+
this.view.hasDomSelection = false;
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
this.view.hasDomSelection = true;
|
|
172
|
+
// Mark the latest focus change as complete (we got new selection after the focus so the selection is in the focused element).
|
|
173
|
+
this.focusObserver.flush();
|
|
174
|
+
if (this.selection.isEqual(newViewSelection) && this.domConverter.isDomSelectionCorrect(domSelection)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// Ensure we are not in the infinite loop (#400).
|
|
178
|
+
// This counter is reset each second. 60 selection changes in 1 second is enough high number
|
|
179
|
+
// to be very difficult (impossible) to achieve using just keyboard keys (during normal editor use).
|
|
180
|
+
if (++this._loopbackCounter > 60) {
|
|
181
|
+
// Selection change observer detected an infinite rendering loop.
|
|
182
|
+
// Most probably you try to put the selection in the position which is not allowed
|
|
183
|
+
// by the browser and browser fixes it automatically what causes `selectionchange` event on
|
|
184
|
+
// which a loopback through a model tries to re-render the wrong selection and again.
|
|
185
|
+
this._reportInfiniteLoop();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (this.selection.isSimilar(newViewSelection)) {
|
|
189
|
+
// If selection was equal and we are at this point of algorithm, it means that it was incorrect.
|
|
190
|
+
// Just re-render it, no need to fire any events, etc.
|
|
191
|
+
this.view.forceRender();
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
const data = {
|
|
195
|
+
oldSelection: this.selection,
|
|
196
|
+
newSelection: newViewSelection,
|
|
197
|
+
domSelection
|
|
198
|
+
};
|
|
199
|
+
// @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
|
|
200
|
+
// @if CK_DEBUG_TYPING // console.info( '%c[SelectionObserver]%c Fire selection change:',
|
|
201
|
+
// @if CK_DEBUG_TYPING // 'font-weight: bold; color: green', '',
|
|
202
|
+
// @if CK_DEBUG_TYPING // newViewSelection.getFirstRange()
|
|
203
|
+
// @if CK_DEBUG_TYPING // );
|
|
204
|
+
// @if CK_DEBUG_TYPING // }
|
|
205
|
+
// Prepare data for new selection and fire appropriate events.
|
|
206
|
+
this.document.fire('selectionChange', data);
|
|
207
|
+
// Call `#_fireSelectionChangeDoneDebounced` every time when `selectionChange` event is fired.
|
|
208
|
+
// This function is debounced what means that `selectionChangeDone` event will be fired only when
|
|
209
|
+
// defined int the function time will elapse since the last time the function was called.
|
|
210
|
+
// So `selectionChangeDone` will be fired when selection will stop changing.
|
|
211
|
+
this._fireSelectionChangeDoneDebounced(data);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Clears `SelectionObserver` internal properties connected with preventing infinite loop.
|
|
216
|
+
*/
|
|
217
|
+
_clearInfiniteLoop() {
|
|
218
|
+
this._loopbackCounter = 0;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module engine/view/observer/tabobserver
|
|
7
|
+
*/
|
|
8
|
+
import type View from '../view.js';
|
|
9
|
+
import Observer from './observer.js';
|
|
10
|
+
import type { KeyEventData } from './keyobserver.js';
|
|
11
|
+
import type { BubblingEvent } from './bubblingemittermixin.js';
|
|
12
|
+
/**
|
|
13
|
+
* Tab observer introduces the {@link module:engine/view/document~Document#event:tab `Document#tab`} event.
|
|
14
|
+
*
|
|
15
|
+
* Note that because {@link module:engine/view/observer/tabobserver~TabObserver} is attached by the
|
|
16
|
+
* {@link module:engine/view/view~View}, this event is available by default.
|
|
17
|
+
*/
|
|
18
|
+
export default class TabObserver extends Observer {
|
|
19
|
+
/**
|
|
20
|
+
* @inheritDoc
|
|
21
|
+
*/
|
|
22
|
+
constructor(view: View);
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/
|
|
26
|
+
observe(): void;
|
|
27
|
+
/**
|
|
28
|
+
* @inheritDoc
|
|
29
|
+
*/
|
|
30
|
+
stopObserving(): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Event fired when the user presses a tab key.
|
|
34
|
+
*
|
|
35
|
+
* Introduced by {@link module:engine/view/observer/tabobserver~TabObserver}.
|
|
36
|
+
*
|
|
37
|
+
* Note that because {@link module:engine/view/observer/tabobserver~TabObserver} is attached by the
|
|
38
|
+
* {@link module:engine/view/view~View}, this event is available by default.
|
|
39
|
+
*
|
|
40
|
+
* @eventName module:engine/view/document~Document#tab
|
|
41
|
+
* @param data
|
|
42
|
+
*/
|
|
43
|
+
export type ViewDocumentTabEvent = BubblingEvent<{
|
|
44
|
+
name: 'tab';
|
|
45
|
+
args: [data: KeyEventData];
|
|
46
|
+
}>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
import Observer from './observer.js';
|
|
6
|
+
import BubblingEventInfo from './bubblingeventinfo.js';
|
|
7
|
+
import { keyCodes } from '@ckeditor/ckeditor5-utils';
|
|
8
|
+
/**
|
|
9
|
+
* Tab observer introduces the {@link module:engine/view/document~Document#event:tab `Document#tab`} event.
|
|
10
|
+
*
|
|
11
|
+
* Note that because {@link module:engine/view/observer/tabobserver~TabObserver} is attached by the
|
|
12
|
+
* {@link module:engine/view/view~View}, this event is available by default.
|
|
13
|
+
*/
|
|
14
|
+
export default class TabObserver extends Observer {
|
|
15
|
+
/**
|
|
16
|
+
* @inheritDoc
|
|
17
|
+
*/
|
|
18
|
+
constructor(view) {
|
|
19
|
+
super(view);
|
|
20
|
+
const doc = this.document;
|
|
21
|
+
doc.on('keydown', (evt, data) => {
|
|
22
|
+
if (!this.isEnabled ||
|
|
23
|
+
data.keyCode != keyCodes.tab ||
|
|
24
|
+
data.ctrlKey) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const event = new BubblingEventInfo(doc, 'tab', doc.selection.getFirstRange());
|
|
28
|
+
doc.fire(event, data);
|
|
29
|
+
if (event.stop.called) {
|
|
30
|
+
evt.stop();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @inheritDoc
|
|
36
|
+
*/
|
|
37
|
+
observe() { }
|
|
38
|
+
/**
|
|
39
|
+
* @inheritDoc
|
|
40
|
+
*/
|
|
41
|
+
stopObserving() { }
|
|
42
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module engine/view/placeholder
|
|
7
|
+
*/
|
|
8
|
+
import '../../theme/placeholder.css';
|
|
9
|
+
import type DowncastWriter from './downcastwriter.js';
|
|
10
|
+
import type EditableElement from './editableelement.js';
|
|
11
|
+
import type Element from './element.js';
|
|
12
|
+
import type View from './view.js';
|
|
13
|
+
/**
|
|
14
|
+
* A helper that enables a placeholder on the provided view element (also updates its visibility).
|
|
15
|
+
* The placeholder is a CSS pseudo–element (with a text content) attached to the element.
|
|
16
|
+
*
|
|
17
|
+
* To change the placeholder text, change value of the `placeholder` property in the provided `element`.
|
|
18
|
+
*
|
|
19
|
+
* To disable the placeholder, use {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} helper.
|
|
20
|
+
*
|
|
21
|
+
* @param options Configuration options of the placeholder.
|
|
22
|
+
* @param options.view Editing view instance.
|
|
23
|
+
* @param options.element Element that will gain a placeholder. See `options.isDirectHost` to learn more.
|
|
24
|
+
* @param options.isDirectHost If set `false`, the placeholder will not be enabled directly
|
|
25
|
+
* in the passed `element` but in one of its children (selected automatically, i.e. a first empty child element).
|
|
26
|
+
* Useful when attaching placeholders to elements that can host other elements (not just text), for instance,
|
|
27
|
+
* editable root elements.
|
|
28
|
+
* @param options.text Placeholder text. It's **deprecated** and will be removed soon. Use
|
|
29
|
+
* {@link module:engine/view/placeholder~PlaceholderableElement#placeholder `options.element.placeholder`} instead.
|
|
30
|
+
* @param options.keepOnFocus If set `true`, the placeholder stay visible when the host element is focused.
|
|
31
|
+
*/
|
|
32
|
+
export declare function enablePlaceholder({ view, element, text, isDirectHost, keepOnFocus }: {
|
|
33
|
+
view: View;
|
|
34
|
+
element: PlaceholderableElement | EditableElement;
|
|
35
|
+
isDirectHost?: boolean;
|
|
36
|
+
text?: string;
|
|
37
|
+
keepOnFocus?: boolean;
|
|
38
|
+
}): void;
|
|
39
|
+
/**
|
|
40
|
+
* Disables the placeholder functionality from a given element.
|
|
41
|
+
*
|
|
42
|
+
* See {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} to learn more.
|
|
43
|
+
*/
|
|
44
|
+
export declare function disablePlaceholder(view: View, element: Element): void;
|
|
45
|
+
/**
|
|
46
|
+
* Shows a placeholder in the provided element by changing related attributes and CSS classes.
|
|
47
|
+
*
|
|
48
|
+
* **Note**: This helper will not update the placeholder visibility nor manage the
|
|
49
|
+
* it in any way in the future. What it does is a one–time state change of an element. Use
|
|
50
|
+
* {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} and
|
|
51
|
+
* {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} for full
|
|
52
|
+
* placeholder functionality.
|
|
53
|
+
*
|
|
54
|
+
* **Note**: This helper will blindly show the placeholder directly in the root editable element if
|
|
55
|
+
* one is passed, which could result in a visual clash if the editable element has some children
|
|
56
|
+
* (for instance, an empty paragraph). Use {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`}
|
|
57
|
+
* in that case or make sure the correct element is passed to the helper.
|
|
58
|
+
*
|
|
59
|
+
* @returns `true`, if any changes were made to the `element`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function showPlaceholder(writer: DowncastWriter, element: Element): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Hides a placeholder in the element by changing related attributes and CSS classes.
|
|
64
|
+
*
|
|
65
|
+
* **Note**: This helper will not update the placeholder visibility nor manage the
|
|
66
|
+
* it in any way in the future. What it does is a one–time state change of an element. Use
|
|
67
|
+
* {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} and
|
|
68
|
+
* {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} for full
|
|
69
|
+
* placeholder functionality.
|
|
70
|
+
*
|
|
71
|
+
* @returns `true`, if any changes were made to the `element`.
|
|
72
|
+
*/
|
|
73
|
+
export declare function hidePlaceholder(writer: DowncastWriter, element: Element): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Checks if a placeholder should be displayed in the element.
|
|
76
|
+
*
|
|
77
|
+
* **Note**: This helper will blindly check the possibility of showing a placeholder directly in the
|
|
78
|
+
* root editable element if one is passed, which may not be the expected result. If an element can
|
|
79
|
+
* host other elements (not just text), most likely one of its children should be checked instead
|
|
80
|
+
* because it will be the final host for the placeholder. Use
|
|
81
|
+
* {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} in that case or make
|
|
82
|
+
* sure the correct element is passed to the helper.
|
|
83
|
+
*
|
|
84
|
+
* @param element Element that holds the placeholder.
|
|
85
|
+
* @param keepOnFocus Focusing the element will keep the placeholder visible.
|
|
86
|
+
*/
|
|
87
|
+
export declare function needsPlaceholder(element: Element, keepOnFocus: boolean): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Element that could have a placeholder.
|
|
90
|
+
*/
|
|
91
|
+
export interface PlaceholderableElement extends Element {
|
|
92
|
+
/**
|
|
93
|
+
* The text of element's placeholder.
|
|
94
|
+
*/
|
|
95
|
+
placeholder?: string;
|
|
96
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module engine/view/placeholder
|
|
7
|
+
*/
|
|
8
|
+
import '../../theme/placeholder.css';
|
|
9
|
+
import { logWarning } from '@ckeditor/ckeditor5-utils';
|
|
10
|
+
// Each document stores information about its placeholder elements and check functions.
|
|
11
|
+
const documentPlaceholders = new WeakMap();
|
|
12
|
+
let hasDisplayedPlaceholderDeprecationWarning = false;
|
|
13
|
+
/**
|
|
14
|
+
* A helper that enables a placeholder on the provided view element (also updates its visibility).
|
|
15
|
+
* The placeholder is a CSS pseudo–element (with a text content) attached to the element.
|
|
16
|
+
*
|
|
17
|
+
* To change the placeholder text, change value of the `placeholder` property in the provided `element`.
|
|
18
|
+
*
|
|
19
|
+
* To disable the placeholder, use {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} helper.
|
|
20
|
+
*
|
|
21
|
+
* @param options Configuration options of the placeholder.
|
|
22
|
+
* @param options.view Editing view instance.
|
|
23
|
+
* @param options.element Element that will gain a placeholder. See `options.isDirectHost` to learn more.
|
|
24
|
+
* @param options.isDirectHost If set `false`, the placeholder will not be enabled directly
|
|
25
|
+
* in the passed `element` but in one of its children (selected automatically, i.e. a first empty child element).
|
|
26
|
+
* Useful when attaching placeholders to elements that can host other elements (not just text), for instance,
|
|
27
|
+
* editable root elements.
|
|
28
|
+
* @param options.text Placeholder text. It's **deprecated** and will be removed soon. Use
|
|
29
|
+
* {@link module:engine/view/placeholder~PlaceholderableElement#placeholder `options.element.placeholder`} instead.
|
|
30
|
+
* @param options.keepOnFocus If set `true`, the placeholder stay visible when the host element is focused.
|
|
31
|
+
*/
|
|
32
|
+
export function enablePlaceholder({ view, element, text, isDirectHost = true, keepOnFocus = false }) {
|
|
33
|
+
const doc = view.document;
|
|
34
|
+
// Use a single a single post fixer per—document to update all placeholders.
|
|
35
|
+
if (!documentPlaceholders.has(doc)) {
|
|
36
|
+
documentPlaceholders.set(doc, new Map());
|
|
37
|
+
// If a post-fixer callback makes a change, it should return `true` so other post–fixers
|
|
38
|
+
// can re–evaluate the document again.
|
|
39
|
+
doc.registerPostFixer(writer => updateDocumentPlaceholders(doc, writer));
|
|
40
|
+
// Update placeholders on isComposing state change since rendering is disabled while in composition mode.
|
|
41
|
+
doc.on('change:isComposing', () => {
|
|
42
|
+
view.change(writer => updateDocumentPlaceholders(doc, writer));
|
|
43
|
+
}, { priority: 'high' });
|
|
44
|
+
}
|
|
45
|
+
if (element.is('editableElement')) {
|
|
46
|
+
element.on('change:placeholder', (evtInfo, evt, text) => {
|
|
47
|
+
setPlaceholder(text);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (element.placeholder) {
|
|
51
|
+
setPlaceholder(element.placeholder);
|
|
52
|
+
}
|
|
53
|
+
else if (text) {
|
|
54
|
+
setPlaceholder(text);
|
|
55
|
+
}
|
|
56
|
+
if (text) {
|
|
57
|
+
showPlaceholderTextDeprecationWarning();
|
|
58
|
+
}
|
|
59
|
+
function setPlaceholder(text) {
|
|
60
|
+
// Store information about the element placeholder under its document.
|
|
61
|
+
documentPlaceholders.get(doc).set(element, {
|
|
62
|
+
text,
|
|
63
|
+
isDirectHost,
|
|
64
|
+
keepOnFocus,
|
|
65
|
+
hostElement: isDirectHost ? element : null
|
|
66
|
+
});
|
|
67
|
+
// Update the placeholders right away.
|
|
68
|
+
view.change(writer => updateDocumentPlaceholders(doc, writer));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Disables the placeholder functionality from a given element.
|
|
73
|
+
*
|
|
74
|
+
* See {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} to learn more.
|
|
75
|
+
*/
|
|
76
|
+
export function disablePlaceholder(view, element) {
|
|
77
|
+
const doc = element.document;
|
|
78
|
+
if (!documentPlaceholders.has(doc)) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
view.change(writer => {
|
|
82
|
+
const placeholders = documentPlaceholders.get(doc);
|
|
83
|
+
const config = placeholders.get(element);
|
|
84
|
+
writer.removeAttribute('data-placeholder', config.hostElement);
|
|
85
|
+
hidePlaceholder(writer, config.hostElement);
|
|
86
|
+
placeholders.delete(element);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Shows a placeholder in the provided element by changing related attributes and CSS classes.
|
|
91
|
+
*
|
|
92
|
+
* **Note**: This helper will not update the placeholder visibility nor manage the
|
|
93
|
+
* it in any way in the future. What it does is a one–time state change of an element. Use
|
|
94
|
+
* {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} and
|
|
95
|
+
* {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} for full
|
|
96
|
+
* placeholder functionality.
|
|
97
|
+
*
|
|
98
|
+
* **Note**: This helper will blindly show the placeholder directly in the root editable element if
|
|
99
|
+
* one is passed, which could result in a visual clash if the editable element has some children
|
|
100
|
+
* (for instance, an empty paragraph). Use {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`}
|
|
101
|
+
* in that case or make sure the correct element is passed to the helper.
|
|
102
|
+
*
|
|
103
|
+
* @returns `true`, if any changes were made to the `element`.
|
|
104
|
+
*/
|
|
105
|
+
export function showPlaceholder(writer, element) {
|
|
106
|
+
if (!element.hasClass('ck-placeholder')) {
|
|
107
|
+
writer.addClass('ck-placeholder', element);
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Hides a placeholder in the element by changing related attributes and CSS classes.
|
|
114
|
+
*
|
|
115
|
+
* **Note**: This helper will not update the placeholder visibility nor manage the
|
|
116
|
+
* it in any way in the future. What it does is a one–time state change of an element. Use
|
|
117
|
+
* {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} and
|
|
118
|
+
* {@link module:engine/view/placeholder~disablePlaceholder `disablePlaceholder()`} for full
|
|
119
|
+
* placeholder functionality.
|
|
120
|
+
*
|
|
121
|
+
* @returns `true`, if any changes were made to the `element`.
|
|
122
|
+
*/
|
|
123
|
+
export function hidePlaceholder(writer, element) {
|
|
124
|
+
if (element.hasClass('ck-placeholder')) {
|
|
125
|
+
writer.removeClass('ck-placeholder', element);
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Checks if a placeholder should be displayed in the element.
|
|
132
|
+
*
|
|
133
|
+
* **Note**: This helper will blindly check the possibility of showing a placeholder directly in the
|
|
134
|
+
* root editable element if one is passed, which may not be the expected result. If an element can
|
|
135
|
+
* host other elements (not just text), most likely one of its children should be checked instead
|
|
136
|
+
* because it will be the final host for the placeholder. Use
|
|
137
|
+
* {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`} in that case or make
|
|
138
|
+
* sure the correct element is passed to the helper.
|
|
139
|
+
*
|
|
140
|
+
* @param element Element that holds the placeholder.
|
|
141
|
+
* @param keepOnFocus Focusing the element will keep the placeholder visible.
|
|
142
|
+
*/
|
|
143
|
+
export function needsPlaceholder(element, keepOnFocus) {
|
|
144
|
+
if (!element.isAttached()) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
// Anything but uiElement(s) counts as content.
|
|
148
|
+
const hasContent = Array.from(element.getChildren())
|
|
149
|
+
.some(element => !element.is('uiElement'));
|
|
150
|
+
if (hasContent) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
const doc = element.document;
|
|
154
|
+
const viewSelection = doc.selection;
|
|
155
|
+
const selectionAnchor = viewSelection.anchor;
|
|
156
|
+
if (doc.isComposing && selectionAnchor && selectionAnchor.parent === element) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
// Skip the focus check and make the placeholder visible already regardless of document focus state.
|
|
160
|
+
if (keepOnFocus) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
// If the document is blurred.
|
|
164
|
+
if (!doc.isFocused) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
// If document is focused and the element is empty but the selection is not anchored inside it.
|
|
168
|
+
return !!selectionAnchor && selectionAnchor.parent !== element;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Updates all placeholders associated with a document in a post–fixer callback.
|
|
172
|
+
*
|
|
173
|
+
* @returns True if any changes were made to the view document.
|
|
174
|
+
*/
|
|
175
|
+
function updateDocumentPlaceholders(doc, writer) {
|
|
176
|
+
const placeholders = documentPlaceholders.get(doc);
|
|
177
|
+
const directHostElements = [];
|
|
178
|
+
let wasViewModified = false;
|
|
179
|
+
// First set placeholders on the direct hosts.
|
|
180
|
+
for (const [element, config] of placeholders) {
|
|
181
|
+
if (config.isDirectHost) {
|
|
182
|
+
directHostElements.push(element);
|
|
183
|
+
if (updatePlaceholder(writer, element, config)) {
|
|
184
|
+
wasViewModified = true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// Then set placeholders on the indirect hosts but only on those that does not already have an direct host placeholder.
|
|
189
|
+
for (const [element, config] of placeholders) {
|
|
190
|
+
if (config.isDirectHost) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const hostElement = getChildPlaceholderHostSubstitute(element);
|
|
194
|
+
// When not a direct host, it could happen that there is no child element
|
|
195
|
+
// capable of displaying a placeholder.
|
|
196
|
+
if (!hostElement) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
// Don't override placeholder if the host element already has some direct placeholder.
|
|
200
|
+
if (directHostElements.includes(hostElement)) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
// Update the host element (used for setting and removing the placeholder).
|
|
204
|
+
config.hostElement = hostElement;
|
|
205
|
+
if (updatePlaceholder(writer, element, config)) {
|
|
206
|
+
wasViewModified = true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return wasViewModified;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Updates a single placeholder in a post–fixer callback.
|
|
213
|
+
*
|
|
214
|
+
* @returns True if any changes were made to the view document.
|
|
215
|
+
*/
|
|
216
|
+
function updatePlaceholder(writer, element, config) {
|
|
217
|
+
const { text, isDirectHost, hostElement } = config;
|
|
218
|
+
let wasViewModified = false;
|
|
219
|
+
// This may be necessary when updating the placeholder text to something else.
|
|
220
|
+
if (hostElement.getAttribute('data-placeholder') !== text) {
|
|
221
|
+
writer.setAttribute('data-placeholder', text, hostElement);
|
|
222
|
+
wasViewModified = true;
|
|
223
|
+
}
|
|
224
|
+
// If the host element is not a direct host then placeholder is needed only when there is only one element.
|
|
225
|
+
const isOnlyChild = isDirectHost || element.childCount == 1;
|
|
226
|
+
if (isOnlyChild && needsPlaceholder(hostElement, config.keepOnFocus)) {
|
|
227
|
+
if (showPlaceholder(writer, hostElement)) {
|
|
228
|
+
wasViewModified = true;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else if (hidePlaceholder(writer, hostElement)) {
|
|
232
|
+
wasViewModified = true;
|
|
233
|
+
}
|
|
234
|
+
return wasViewModified;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Gets a child element capable of displaying a placeholder if a parent element can host more
|
|
238
|
+
* than just text (for instance, when it is a root editable element). The child element
|
|
239
|
+
* can then be used in other placeholder helpers as a substitute of its parent.
|
|
240
|
+
*/
|
|
241
|
+
function getChildPlaceholderHostSubstitute(parent) {
|
|
242
|
+
if (parent.childCount) {
|
|
243
|
+
const firstChild = parent.getChild(0);
|
|
244
|
+
if (firstChild.is('element') && !firstChild.is('uiElement') && !firstChild.is('attributeElement')) {
|
|
245
|
+
return firstChild;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Displays a deprecation warning message in the console, but only once per page load.
|
|
252
|
+
*/
|
|
253
|
+
function showPlaceholderTextDeprecationWarning() {
|
|
254
|
+
if (!hasDisplayedPlaceholderDeprecationWarning) {
|
|
255
|
+
/**
|
|
256
|
+
* The "text" option in the {@link module:engine/view/placeholder~enablePlaceholder `enablePlaceholder()`}
|
|
257
|
+
* function is deprecated and will be removed soon.
|
|
258
|
+
*
|
|
259
|
+
* See the {@glink updating/guides/update-to-39#view-element-placeholder Migration to v39} guide for
|
|
260
|
+
* more information on how to apply this change.
|
|
261
|
+
*
|
|
262
|
+
* @error enableplaceholder-deprecated-text-option
|
|
263
|
+
*/
|
|
264
|
+
logWarning('enableplaceholder-deprecated-text-option');
|
|
265
|
+
}
|
|
266
|
+
hasDisplayedPlaceholderDeprecationWarning = true;
|
|
267
|
+
}
|