@ckeditor/ckeditor5-widget 38.1.0 → 38.1.1

Sign up to get free protection for your applications and to get access to all the features.
package/src/utils.d.ts CHANGED
@@ -1,198 +1,198 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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 widget/utils
7
- */
8
- import { type GetCallback } from '@ckeditor/ckeditor5-utils';
9
- import { type HighlightDescriptor, type MapperViewToModelPositionEvent, type DocumentSelection, type DowncastWriter, type Model, type Range, type Selection, type ViewEditableElement, type ViewElement, type ViewTypeCheckable } from '@ckeditor/ckeditor5-engine';
10
- /**
11
- * CSS class added to each widget element.
12
- */
13
- export declare const WIDGET_CLASS_NAME = "ck-widget";
14
- /**
15
- * CSS class added to currently selected widget element.
16
- */
17
- export declare const WIDGET_SELECTED_CLASS_NAME = "ck-widget_selected";
18
- /**
19
- * Returns `true` if given {@link module:engine/view/node~Node} is an {@link module:engine/view/element~Element} and a widget.
20
- */
21
- export declare function isWidget(node: ViewTypeCheckable): boolean;
22
- /**
23
- * Converts the given {@link module:engine/view/element~Element} to a widget in the following way:
24
- *
25
- * * sets the `contenteditable` attribute to `"false"`,
26
- * * adds the `ck-widget` CSS class,
27
- * * adds a custom {@link module:engine/view/element~Element#getFillerOffset `getFillerOffset()`} method returning `null`,
28
- * * adds a custom property allowing to recognize widget elements by using {@link ~isWidget `isWidget()`},
29
- * * implements the {@link ~setHighlightHandling view highlight on widgets}.
30
- *
31
- * This function needs to be used in conjunction with
32
- * {@link module:engine/conversion/downcasthelpers~DowncastHelpers downcast conversion helpers}
33
- * like {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`}.
34
- * Moreover, typically you will want to use `toWidget()` only for `editingDowncast`, while keeping the `dataDowncast` clean.
35
- *
36
- * For example, in order to convert a `<widget>` model element to `<div class="widget">` in the view, you can define
37
- * such converters:
38
- *
39
- * ```ts
40
- * editor.conversion.for( 'editingDowncast' )
41
- * .elementToElement( {
42
- * model: 'widget',
43
- * view: ( modelItem, { writer } ) => {
44
- * const div = writer.createContainerElement( 'div', { class: 'widget' } );
45
- *
46
- * return toWidget( div, writer, { label: 'some widget' } );
47
- * }
48
- * } );
49
- *
50
- * editor.conversion.for( 'dataDowncast' )
51
- * .elementToElement( {
52
- * model: 'widget',
53
- * view: ( modelItem, { writer } ) => {
54
- * return writer.createContainerElement( 'div', { class: 'widget' } );
55
- * }
56
- * } );
57
- * ```
58
- *
59
- * See the full source code of the widget (with a nested editable) schema definition and converters in
60
- * [this sample](https://github.com/ckeditor/ckeditor5-widget/blob/master/tests/manual/widget-with-nestededitable.js).
61
- *
62
- * @param options Additional options.
63
- * @param options.label Element's label provided to the {@link ~setLabel} function. It can be passed as
64
- * a plain string or a function returning a string. It represents the widget for assistive technologies (like screen readers).
65
- * @param options.hasSelectionHandle If `true`, the widget will have a selection handle added.
66
- * @returns Returns the same element.
67
- */
68
- export declare function toWidget(element: ViewElement, writer: DowncastWriter, options?: {
69
- label?: string | (() => string);
70
- hasSelectionHandle?: boolean;
71
- }): ViewElement;
72
- /**
73
- * Sets highlight handling methods. Uses {@link module:widget/highlightstack~HighlightStack} to
74
- * properly determine which highlight descriptor should be used at given time.
75
- */
76
- export declare function setHighlightHandling(element: ViewElement, writer: DowncastWriter, add?: (element: ViewElement, descriptor: HighlightDescriptor, writer: DowncastWriter) => void, remove?: (element: ViewElement, descriptor: HighlightDescriptor, writer: DowncastWriter) => void): void;
77
- /**
78
- * Sets label for given element.
79
- * It can be passed as a plain string or a function returning a string. Function will be called each time label is retrieved by
80
- * {@link ~getLabel `getLabel()`}.
81
- */
82
- export declare function setLabel(element: ViewElement, labelOrCreator: string | (() => string)): void;
83
- /**
84
- * Returns the label of the provided element.
85
- */
86
- export declare function getLabel(element: ViewElement): string;
87
- /**
88
- * Adds functionality to the provided {@link module:engine/view/editableelement~EditableElement} to act as a widget's editable:
89
- *
90
- * * sets the `contenteditable` attribute to `true` when {@link module:engine/view/editableelement~EditableElement#isReadOnly} is `false`,
91
- * otherwise sets it to `false`,
92
- * * adds the `ck-editor__editable` and `ck-editor__nested-editable` CSS classes,
93
- * * adds the `ck-editor__nested-editable_focused` CSS class when the editable is focused and removes it when it is blurred.
94
- * * implements the {@link ~setHighlightHandling view highlight on widget's editable}.
95
- *
96
- * Similarly to {@link ~toWidget `toWidget()`} this function should be used in `editingDowncast` only and it is usually
97
- * used together with {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`}.
98
- *
99
- * For example, in order to convert a `<nested>` model element to `<div class="nested">` in the view, you can define
100
- * such converters:
101
- *
102
- * ```ts
103
- * editor.conversion.for( 'editingDowncast' )
104
- * .elementToElement( {
105
- * model: 'nested',
106
- * view: ( modelItem, { writer } ) => {
107
- * const div = writer.createEditableElement( 'div', { class: 'nested' } );
108
- *
109
- * return toWidgetEditable( nested, writer, { label: 'label for editable' } );
110
- * }
111
- * } );
112
- *
113
- * editor.conversion.for( 'dataDowncast' )
114
- * .elementToElement( {
115
- * model: 'nested',
116
- * view: ( modelItem, { writer } ) => {
117
- * return writer.createContainerElement( 'div', { class: 'nested' } );
118
- * }
119
- * } );
120
- * ```
121
- *
122
- * See the full source code of the widget (with nested editable) schema definition and converters in
123
- * [this sample](https://github.com/ckeditor/ckeditor5-widget/blob/master/tests/manual/widget-with-nestededitable.js).
124
- *
125
- * @param options Additional options.
126
- * @param options.label Editable's label used by assistive technologies (e.g. screen readers).
127
- * @returns Returns the same element that was provided in the `editable` parameter
128
- */
129
- export declare function toWidgetEditable(editable: ViewEditableElement, writer: DowncastWriter, options?: {
130
- label?: string;
131
- }): ViewEditableElement;
132
- /**
133
- * Returns a model range which is optimal (in terms of UX) for inserting a widget block.
134
- *
135
- * For instance, if a selection is in the middle of a paragraph, the collapsed range before this paragraph
136
- * will be returned so that it is not split. If the selection is at the end of a paragraph,
137
- * the collapsed range after this paragraph will be returned.
138
- *
139
- * Note: If the selection is placed in an empty block, the range in that block will be returned. If that range
140
- * is then passed to {@link module:engine/model/model~Model#insertContent}, the block will be fully replaced
141
- * by the inserted widget block.
142
- *
143
- * @param selection The selection based on which the insertion position should be calculated.
144
- * @param model Model instance.
145
- * @returns The optimal range.
146
- */
147
- export declare function findOptimalInsertionRange(selection: Selection | DocumentSelection, model: Model): Range;
148
- /**
149
- * A util to be used in order to map view positions to correct model positions when implementing a widget
150
- * which renders non-empty view element for an empty model element.
151
- *
152
- * For example:
153
- *
154
- * ```
155
- * // Model:
156
- * <placeholder type="name"></placeholder>
157
- *
158
- * // View:
159
- * <span class="placeholder">name</span>
160
- * ```
161
- *
162
- * In such case, view positions inside `<span>` cannot be correctly mapped to the model (because the model element is empty).
163
- * To handle mapping positions inside `<span class="placeholder">` to the model use this util as follows:
164
- *
165
- * ```ts
166
- * editor.editing.mapper.on(
167
- * 'viewToModelPosition',
168
- * viewToModelPositionOutsideModelElement( model, viewElement => viewElement.hasClass( 'placeholder' ) )
169
- * );
170
- * ```
171
- *
172
- * The callback will try to map the view offset of selection to an expected model position.
173
- *
174
- * 1. When the position is at the end (or in the middle) of the inline widget:
175
- *
176
- * ```
177
- * // View:
178
- * <p>foo <span class="placeholder">name|</span> bar</p>
179
- *
180
- * // Model:
181
- * <paragraph>foo <placeholder type="name"></placeholder>| bar</paragraph>
182
- * ```
183
- *
184
- * 2. When the position is at the beginning of the inline widget:
185
- *
186
- * ```
187
- * // View:
188
- * <p>foo <span class="placeholder">|name</span> bar</p>
189
- *
190
- * // Model:
191
- * <paragraph>foo |<placeholder type="name"></placeholder> bar</paragraph>
192
- * ```
193
- *
194
- * @param model Model instance on which the callback operates.
195
- * @param viewElementMatcher Function that is passed a view element and should return `true` if the custom mapping
196
- * should be applied to the given view element.
197
- */
198
- export declare function viewToModelPositionOutsideModelElement(model: Model, viewElementMatcher: (element: ViewElement) => boolean): GetCallback<MapperViewToModelPositionEvent>;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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 widget/utils
7
+ */
8
+ import { type GetCallback } from '@ckeditor/ckeditor5-utils';
9
+ import { type HighlightDescriptor, type MapperViewToModelPositionEvent, type DocumentSelection, type DowncastWriter, type Model, type Range, type Selection, type ViewEditableElement, type ViewElement, type ViewTypeCheckable } from '@ckeditor/ckeditor5-engine';
10
+ /**
11
+ * CSS class added to each widget element.
12
+ */
13
+ export declare const WIDGET_CLASS_NAME = "ck-widget";
14
+ /**
15
+ * CSS class added to currently selected widget element.
16
+ */
17
+ export declare const WIDGET_SELECTED_CLASS_NAME = "ck-widget_selected";
18
+ /**
19
+ * Returns `true` if given {@link module:engine/view/node~Node} is an {@link module:engine/view/element~Element} and a widget.
20
+ */
21
+ export declare function isWidget(node: ViewTypeCheckable): boolean;
22
+ /**
23
+ * Converts the given {@link module:engine/view/element~Element} to a widget in the following way:
24
+ *
25
+ * * sets the `contenteditable` attribute to `"false"`,
26
+ * * adds the `ck-widget` CSS class,
27
+ * * adds a custom {@link module:engine/view/element~Element#getFillerOffset `getFillerOffset()`} method returning `null`,
28
+ * * adds a custom property allowing to recognize widget elements by using {@link ~isWidget `isWidget()`},
29
+ * * implements the {@link ~setHighlightHandling view highlight on widgets}.
30
+ *
31
+ * This function needs to be used in conjunction with
32
+ * {@link module:engine/conversion/downcasthelpers~DowncastHelpers downcast conversion helpers}
33
+ * like {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`}.
34
+ * Moreover, typically you will want to use `toWidget()` only for `editingDowncast`, while keeping the `dataDowncast` clean.
35
+ *
36
+ * For example, in order to convert a `<widget>` model element to `<div class="widget">` in the view, you can define
37
+ * such converters:
38
+ *
39
+ * ```ts
40
+ * editor.conversion.for( 'editingDowncast' )
41
+ * .elementToElement( {
42
+ * model: 'widget',
43
+ * view: ( modelItem, { writer } ) => {
44
+ * const div = writer.createContainerElement( 'div', { class: 'widget' } );
45
+ *
46
+ * return toWidget( div, writer, { label: 'some widget' } );
47
+ * }
48
+ * } );
49
+ *
50
+ * editor.conversion.for( 'dataDowncast' )
51
+ * .elementToElement( {
52
+ * model: 'widget',
53
+ * view: ( modelItem, { writer } ) => {
54
+ * return writer.createContainerElement( 'div', { class: 'widget' } );
55
+ * }
56
+ * } );
57
+ * ```
58
+ *
59
+ * See the full source code of the widget (with a nested editable) schema definition and converters in
60
+ * [this sample](https://github.com/ckeditor/ckeditor5-widget/blob/master/tests/manual/widget-with-nestededitable.js).
61
+ *
62
+ * @param options Additional options.
63
+ * @param options.label Element's label provided to the {@link ~setLabel} function. It can be passed as
64
+ * a plain string or a function returning a string. It represents the widget for assistive technologies (like screen readers).
65
+ * @param options.hasSelectionHandle If `true`, the widget will have a selection handle added.
66
+ * @returns Returns the same element.
67
+ */
68
+ export declare function toWidget(element: ViewElement, writer: DowncastWriter, options?: {
69
+ label?: string | (() => string);
70
+ hasSelectionHandle?: boolean;
71
+ }): ViewElement;
72
+ /**
73
+ * Sets highlight handling methods. Uses {@link module:widget/highlightstack~HighlightStack} to
74
+ * properly determine which highlight descriptor should be used at given time.
75
+ */
76
+ export declare function setHighlightHandling(element: ViewElement, writer: DowncastWriter, add?: (element: ViewElement, descriptor: HighlightDescriptor, writer: DowncastWriter) => void, remove?: (element: ViewElement, descriptor: HighlightDescriptor, writer: DowncastWriter) => void): void;
77
+ /**
78
+ * Sets label for given element.
79
+ * It can be passed as a plain string or a function returning a string. Function will be called each time label is retrieved by
80
+ * {@link ~getLabel `getLabel()`}.
81
+ */
82
+ export declare function setLabel(element: ViewElement, labelOrCreator: string | (() => string)): void;
83
+ /**
84
+ * Returns the label of the provided element.
85
+ */
86
+ export declare function getLabel(element: ViewElement): string;
87
+ /**
88
+ * Adds functionality to the provided {@link module:engine/view/editableelement~EditableElement} to act as a widget's editable:
89
+ *
90
+ * * sets the `contenteditable` attribute to `true` when {@link module:engine/view/editableelement~EditableElement#isReadOnly} is `false`,
91
+ * otherwise sets it to `false`,
92
+ * * adds the `ck-editor__editable` and `ck-editor__nested-editable` CSS classes,
93
+ * * adds the `ck-editor__nested-editable_focused` CSS class when the editable is focused and removes it when it is blurred.
94
+ * * implements the {@link ~setHighlightHandling view highlight on widget's editable}.
95
+ *
96
+ * Similarly to {@link ~toWidget `toWidget()`} this function should be used in `editingDowncast` only and it is usually
97
+ * used together with {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`}.
98
+ *
99
+ * For example, in order to convert a `<nested>` model element to `<div class="nested">` in the view, you can define
100
+ * such converters:
101
+ *
102
+ * ```ts
103
+ * editor.conversion.for( 'editingDowncast' )
104
+ * .elementToElement( {
105
+ * model: 'nested',
106
+ * view: ( modelItem, { writer } ) => {
107
+ * const div = writer.createEditableElement( 'div', { class: 'nested' } );
108
+ *
109
+ * return toWidgetEditable( nested, writer, { label: 'label for editable' } );
110
+ * }
111
+ * } );
112
+ *
113
+ * editor.conversion.for( 'dataDowncast' )
114
+ * .elementToElement( {
115
+ * model: 'nested',
116
+ * view: ( modelItem, { writer } ) => {
117
+ * return writer.createContainerElement( 'div', { class: 'nested' } );
118
+ * }
119
+ * } );
120
+ * ```
121
+ *
122
+ * See the full source code of the widget (with nested editable) schema definition and converters in
123
+ * [this sample](https://github.com/ckeditor/ckeditor5-widget/blob/master/tests/manual/widget-with-nestededitable.js).
124
+ *
125
+ * @param options Additional options.
126
+ * @param options.label Editable's label used by assistive technologies (e.g. screen readers).
127
+ * @returns Returns the same element that was provided in the `editable` parameter
128
+ */
129
+ export declare function toWidgetEditable(editable: ViewEditableElement, writer: DowncastWriter, options?: {
130
+ label?: string;
131
+ }): ViewEditableElement;
132
+ /**
133
+ * Returns a model range which is optimal (in terms of UX) for inserting a widget block.
134
+ *
135
+ * For instance, if a selection is in the middle of a paragraph, the collapsed range before this paragraph
136
+ * will be returned so that it is not split. If the selection is at the end of a paragraph,
137
+ * the collapsed range after this paragraph will be returned.
138
+ *
139
+ * Note: If the selection is placed in an empty block, the range in that block will be returned. If that range
140
+ * is then passed to {@link module:engine/model/model~Model#insertContent}, the block will be fully replaced
141
+ * by the inserted widget block.
142
+ *
143
+ * @param selection The selection based on which the insertion position should be calculated.
144
+ * @param model Model instance.
145
+ * @returns The optimal range.
146
+ */
147
+ export declare function findOptimalInsertionRange(selection: Selection | DocumentSelection, model: Model): Range;
148
+ /**
149
+ * A util to be used in order to map view positions to correct model positions when implementing a widget
150
+ * which renders non-empty view element for an empty model element.
151
+ *
152
+ * For example:
153
+ *
154
+ * ```
155
+ * // Model:
156
+ * <placeholder type="name"></placeholder>
157
+ *
158
+ * // View:
159
+ * <span class="placeholder">name</span>
160
+ * ```
161
+ *
162
+ * In such case, view positions inside `<span>` cannot be correctly mapped to the model (because the model element is empty).
163
+ * To handle mapping positions inside `<span class="placeholder">` to the model use this util as follows:
164
+ *
165
+ * ```ts
166
+ * editor.editing.mapper.on(
167
+ * 'viewToModelPosition',
168
+ * viewToModelPositionOutsideModelElement( model, viewElement => viewElement.hasClass( 'placeholder' ) )
169
+ * );
170
+ * ```
171
+ *
172
+ * The callback will try to map the view offset of selection to an expected model position.
173
+ *
174
+ * 1. When the position is at the end (or in the middle) of the inline widget:
175
+ *
176
+ * ```
177
+ * // View:
178
+ * <p>foo <span class="placeholder">name|</span> bar</p>
179
+ *
180
+ * // Model:
181
+ * <paragraph>foo <placeholder type="name"></placeholder>| bar</paragraph>
182
+ * ```
183
+ *
184
+ * 2. When the position is at the beginning of the inline widget:
185
+ *
186
+ * ```
187
+ * // View:
188
+ * <p>foo <span class="placeholder">|name</span> bar</p>
189
+ *
190
+ * // Model:
191
+ * <paragraph>foo |<placeholder type="name"></placeholder> bar</paragraph>
192
+ * ```
193
+ *
194
+ * @param model Model instance on which the callback operates.
195
+ * @param viewElementMatcher Function that is passed a view element and should return `true` if the custom mapping
196
+ * should be applied to the given view element.
197
+ */
198
+ export declare function viewToModelPositionOutsideModelElement(model: Model, viewElementMatcher: (element: ViewElement) => boolean): GetCallback<MapperViewToModelPositionEvent>;