-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
htmlembedediting.js
363 lines (302 loc) · 10.4 KB
/
htmlembedediting.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module html-embed/htmlembedediting
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
import { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
import InsertHtmlEmbedCommand from './inserthtmlembedcommand';
import UpdateHtmlEmbedCommand from './updatehtmlembedcommand';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
import pencilIcon from '@ckeditor/ckeditor5-core/theme/icons/pencil.svg';
import checkIcon from '@ckeditor/ckeditor5-core/theme/icons/check.svg';
import cancelIcon from '@ckeditor/ckeditor5-core/theme/icons/cancel.svg';
import '../theme/htmlembed.css';
/**
* The HTML embed editing feature.
*
* @extends module:core/plugin~Plugin
*/
export default class HtmlEmbedEditing extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'HtmlEmbedEditing';
}
/**
* @inheritDoc
*/
constructor( editor ) {
super( editor );
editor.config.define( 'htmlEmbed', {
showPreviews: false,
sanitizeHtml: rawHtml => {
/**
* When using the HTML embed feature with `htmlEmbed.showPreviews=true` option, it is strongly recommended to
* define a sanitize function that will clean up an input HTML in order to avoid XSS vulnerability.
*
* For a detailed overview, check the {@glink features/html-embed HTML embed feature} documentation.
*
* @error html-embed-provide-sanitize-function
*/
logWarning( 'html-embed-provide-sanitize-function' );
return {
html: rawHtml,
hasChanged: false
};
}
} );
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const schema = editor.model.schema;
schema.register( 'rawHtml', {
isObject: true,
allowWhere: '$block',
allowAttributes: [ 'value' ]
} );
editor.commands.add( 'updateHtmlEmbed', new UpdateHtmlEmbedCommand( editor ) );
editor.commands.add( 'insertHtmlEmbed', new InsertHtmlEmbedCommand( editor ) );
this._setupConversion();
}
/**
* Prepares converters for the feature.
*
* @private
*/
_setupConversion() {
const editor = this.editor;
const t = editor.t;
const view = editor.editing.view;
const htmlEmbedConfig = editor.config.get( 'htmlEmbed' );
const upcastWriter = new UpcastWriter( view.document );
const htmlProcessor = new HtmlDataProcessor( view.document );
editor.conversion.for( 'upcast' ).elementToElement( {
view: {
name: 'div',
classes: 'raw-html-embed'
},
model: ( viewElement, { writer } ) => {
// Note: The below line has a side-effect – the children are *moved* to the DF so
// viewElement becomes empty. It's fine here.
const fragment = upcastWriter.createDocumentFragment( viewElement.getChildren() );
const innerHtml = htmlProcessor.toData( fragment );
return writer.createElement( 'rawHtml', {
value: innerHtml
} );
}
} );
editor.conversion.for( 'dataDowncast' ).elementToElement( {
model: 'rawHtml',
view: ( modelElement, { writer } ) => {
return writer.createRawElement( 'div', { class: 'raw-html-embed' }, function( domElement ) {
domElement.innerHTML = modelElement.getAttribute( 'value' ) || '';
} );
}
} );
editor.conversion.for( 'editingDowncast' ).elementToElement( {
triggerBy: {
attributes: [ 'value' ]
},
model: 'rawHtml',
view: ( modelElement, { writer } ) => {
let domContentWrapper, state, props;
const viewContainer = writer.createContainerElement( 'div', {
class: 'raw-html-embed'
} );
// Widget cannot be a raw element because the widget system would not be able
// to add its UI to it. Thus, we need this wrapper.
const viewContentWrapper = writer.createRawElement( 'div', {
class: 'raw-html-embed__content-wrapper'
}, function( domElement ) {
domContentWrapper = domElement;
renderContent( { domElement, editor, state, props } );
} );
// API exposed on each raw HTML embed widget so other features can control a particular widget.
const rawHtmlApi = {
makeEditable() {
state = Object.assign( {}, state, {
isEditable: true
} );
renderContent( { domElement: domContentWrapper, editor, state, props } );
view.change( writer => {
writer.setAttribute( 'data-cke-ignore-events', 'true', viewContentWrapper );
} );
// This could be potentially pulled to a separate method called focusTextarea().
domContentWrapper.querySelector( 'textarea' ).focus();
},
save( newValue ) {
// If the value didn't change, we just cancel. If it changed,
// it's enough to update the model – the entire widget will be reconverted.
if ( newValue !== state.getRawHtmlValue() ) {
editor.execute( 'updateHtmlEmbed', newValue );
} else {
this.cancel();
}
},
cancel() {
state = Object.assign( {}, state, {
isEditable: false
} );
renderContent( { domElement: domContentWrapper, editor, state, props } );
view.change( writer => {
writer.removeAttribute( 'data-cke-ignore-events', viewContentWrapper );
} );
}
};
state = {
showPreviews: htmlEmbedConfig.showPreviews,
isEditable: false,
getRawHtmlValue: () => modelElement.getAttribute( 'value' ) || ''
};
props = {
sanitizeHtml: htmlEmbedConfig.sanitizeHtml,
textareaPlaceholder: t( 'Paste raw HTML here...' ),
onEditClick() {
rawHtmlApi.makeEditable();
},
onSaveClick( newValue ) {
rawHtmlApi.save( newValue );
},
onCancelClick() {
rawHtmlApi.cancel();
}
};
writer.insert( writer.createPositionAt( viewContainer, 0 ), viewContentWrapper );
writer.setCustomProperty( 'rawHtmlApi', rawHtmlApi, viewContainer );
writer.setCustomProperty( 'rawHtml', true, viewContainer );
return toWidget( viewContainer, writer, {
widgetLabel: t( 'HTML snippet' ),
hasSelectionHandle: true
} );
}
} );
function renderContent( { domElement, editor, state, props } ) {
// Remove all children;
domElement.textContent = '';
const domDocument = domElement.ownerDocument;
let domTextarea;
if ( state.isEditable ) {
const textareaProps = {
isDisabled: false,
placeholder: props.textareaPlaceholder
};
domTextarea = createDomTextarea( { domDocument, state, props: textareaProps } );
domElement.append( domTextarea );
} else if ( state.showPreviews ) {
const previewContainerProps = {
sanitizeHtml: props.sanitizeHtml
};
domElement.append( createPreviewContainer( { domDocument, state, props: previewContainerProps } ) );
} else {
const textareaProps = {
isDisabled: true,
placeholder: props.textareaPlaceholder
};
domElement.append( createDomTextarea( { domDocument, state, props: textareaProps } ) );
}
const buttonsWrapperProps = {
onEditClick: props.onEditClick,
onSaveClick: () => {
props.onSaveClick( domTextarea.value );
},
onCancelClick: props.onCancelClick
};
domElement.prepend( createDomButtonsWrapper( { editor, domDocument, state, props: buttonsWrapperProps } ) );
}
function createDomButtonsWrapper( { editor, domDocument, state, props } ) {
const domButtonsWrapper = createElement( domDocument, 'div', {
class: 'raw-html-embed__buttons-wrapper'
} );
// TODO these should be cached and we should only clone here these cached nodes!
const domEditButton = createDomButton( editor.locale, 'edit' );
const domSaveButton = createDomButton( editor.locale, 'save' );
const domCancelButton = createDomButton( editor.locale, 'cancel' );
if ( state.isEditable ) {
const clonedDomSaveButton = domSaveButton.cloneNode( true );
const clonedDomCancelButton = domCancelButton.cloneNode( true );
clonedDomSaveButton.addEventListener( 'click', evt => {
evt.preventDefault();
props.onSaveClick( );
} );
clonedDomCancelButton.addEventListener( 'click', evt => {
evt.preventDefault();
props.onCancelClick( );
} );
domButtonsWrapper.appendChild( clonedDomSaveButton );
domButtonsWrapper.appendChild( clonedDomCancelButton );
} else {
const clonedDomEditButton = domEditButton.cloneNode( true );
clonedDomEditButton.addEventListener( 'click', evt => {
evt.preventDefault();
props.onEditClick();
} );
domButtonsWrapper.appendChild( clonedDomEditButton );
}
return domButtonsWrapper;
}
function createDomTextarea( { domDocument, state, props } ) {
const domTextarea = createElement( domDocument, 'textarea', {
placeholder: props.placeholder,
class: 'ck ck-reset ck-input ck-input-text raw-html-embed__source'
} );
domTextarea.disabled = props.isDisabled;
domTextarea.value = state.getRawHtmlValue();
return domTextarea;
}
function createPreviewContainer( { domDocument, state, props } ) {
const domPreviewContainer = createElement( domDocument, 'div', {
class: 'raw-html-embed__preview'
} );
const sanitizeOutput = props.sanitizeHtml( state.getRawHtmlValue() );
domPreviewContainer.innerHTML = sanitizeOutput.html;
return domPreviewContainer;
}
}
}
// Returns a toggle mode button DOM element that can be cloned and used in conversion.
//
// @param {module:utils/locale~Locale} locale Editor locale.
// @param {'edit'|'save'|'cancel'} type Type of button to create.
// @returns {HTMLElement}
function createDomButton( locale, type ) {
const t = locale.t;
const buttonView = new ButtonView( locale );
buttonView.set( {
tooltipPosition: 'sw',
icon: pencilIcon,
tooltip: true
} );
buttonView.render();
if ( type === 'edit' ) {
buttonView.set( {
icon: pencilIcon,
label: t( 'Edit source' ),
class: 'raw-html-embed__edit-button'
} );
} else if ( type === 'save' ) {
buttonView.set( {
icon: checkIcon,
label: t( 'Save changes' ),
class: 'raw-html-embed__save-button'
} );
} else {
buttonView.set( {
icon: cancelIcon,
label: t( 'Cancel' ),
class: 'raw-html-embed__cancel-button'
} );
}
buttonView.destroy();
return buttonView.element.cloneNode( true );
}