This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
171 lines (146 loc) · 5.5 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module widget/utils
*/
import HighlightStack from './highlightstack';
const widgetSymbol = Symbol( 'isWidget' );
const labelSymbol = Symbol( 'label' );
/**
* CSS class added to each widget element.
*
* @const {String}
*/
export const WIDGET_CLASS_NAME = 'ck-widget';
/**
* CSS class added to currently selected widget element.
*
* @const {String}
*/
export const WIDGET_SELECTED_CLASS_NAME = 'ck-widget_selected';
/**
* Returns `true` if given {@link module:engine/view/element~Element} is a widget.
*
* @param {module:engine/view/element~Element} element
* @returns {Boolean}
*/
export function isWidget( element ) {
return !!element.getCustomProperty( widgetSymbol );
}
/**
* Converts given {@link module:engine/view/element~Element} to widget in following way:
* * sets `contenteditable` attribute to `"true"`,
* * adds custom `getFillerOffset` method returning `null`,
* * adds `ck-widget` CSS class,
* * adds custom property allowing to recognize widget elements by using {@link ~isWidget},
* * implements `addHighlight` and `removeHighlight` custom properties to handle view highlight on widgets.
*
* @param {module:engine/view/element~Element} element
* @param {module:engine/view/writer~Writer} writer
* @param {Object} [options={}]
* @param {String|Function} [options.label] Element's label provided to {@link ~setLabel} function. It can be passed as
* a plain string or a function returning a string.
* @returns {module:engine/view/element~Element} Returns same element.
*/
export function toWidget( element, writer, options = {} ) {
writer.setAttribute( 'contenteditable', 'false', element );
writer.addClass( WIDGET_CLASS_NAME, element );
writer.setCustomProperty( widgetSymbol, true, element );
element.getFillerOffset = getFillerOffset;
if ( options.label ) {
setLabel( element, options.label, writer );
}
setHighlightHandling(
element,
writer,
( element, descriptor, writer ) => writer.addClass( normalizeToArray( descriptor.class ), element ),
( element, descriptor, writer ) => writer.removeClass( normalizeToArray( descriptor.class ), element )
);
return element;
// Normalizes CSS class in descriptor that can be provided in form of an array or a string.
function normalizeToArray( classes ) {
return Array.isArray( classes ) ? classes : [ classes ];
}
}
/**
* Sets highlight handling methods. Uses {@link module:widget/highlightstack~HighlightStack} to
* properly determine which highlight descriptor should be used at given time.
*
* @param {module:engine/view/element~Element} element
* @param {module:engine/view/writer~Writer} writer
* @param {Function} add
* @param {Function} remove
*/
export function setHighlightHandling( element, writer, add, remove ) {
const stack = new HighlightStack();
stack.on( 'change:top', ( evt, data ) => {
if ( data.oldDescriptor ) {
remove( element, data.oldDescriptor, data.writer );
}
if ( data.newDescriptor ) {
add( element, data.newDescriptor, data.writer );
}
} );
writer.setCustomProperty( 'addHighlight', ( element, descriptor, writer ) => stack.add( descriptor, writer ), element );
writer.setCustomProperty( 'removeHighlight', ( element, id, writer ) => stack.remove( id, writer ), element );
}
/**
* Sets label for given element.
* It can be passed as a plain string or a function returning a string. Function will be called each time label is retrieved by
* {@link ~getLabel}.
*
* @param {module:engine/view/element~Element} element
* @param {String|Function} labelOrCreator
* * @param {module:engine/view/writer~Writer} writer
*/
export function setLabel( element, labelOrCreator, writer ) {
writer.setCustomProperty( labelSymbol, labelOrCreator, element );
}
/**
* Returns label for provided element.
*
* @param {module:engine/view/element~Element} element
* @return {String}
*/
export function getLabel( element ) {
const labelCreator = element.getCustomProperty( labelSymbol );
if ( !labelCreator ) {
return '';
}
return typeof labelCreator == 'function' ? labelCreator() : labelCreator;
}
/**
* Adds functionality to provided {module:engine/view/editableelement~EditableElement} to act as a widget's editable:
* * adds `ck-editable` CSS class,
* * sets `contenteditable` as `true` when {module:engine/view/editableelement~EditableElement#isReadOnly} is `false`
* otherwise set `false`,
* * adds `ck-editable_focused` CSS class when editable is focused and removes it when it's blurred.
*
* @param {module:engine/view/editableelement~EditableElement} editable
* @returns {module:engine/view/editableelement~EditableElement} Returns same element that was provided in `editable` param.
*/
export function toWidgetEditable( editable, writer ) {
writer.addClass( 'ck-editable', editable );
// Set initial contenteditable value.
writer.setAttribute( 'contenteditable', editable.isReadOnly ? 'false' : 'true', editable );
// Bind contenteditable property to element#isReadOnly.
editable.on( 'change:isReadOnly', ( evt, property, is ) => {
writer.setAttribute( 'contenteditable', is ? 'false' : 'true', editable );
} );
editable.on( 'change:isFocused', ( evt, property, is ) => {
if ( is ) {
writer.addClass( 'ck-editable_focused', editable );
} else {
writer.removeClass( 'ck-editable_focused', editable );
}
} );
return editable;
}
// Default filler offset function applied to all widget elements.
//
// @returns {null}
function getFillerOffset() {
return null;
}