-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathhtmlembedediting.js
106 lines (90 loc) · 3.25 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
/**
* @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 sanitizeHtml from 'sanitize-html';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import HTMLEmbedCommand from './htmlembedcommand';
import '../theme/htmlembed.css';
import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
import { stringify as viewStringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
/**
* The HTML embed editing feature.
*
* @extends module:core/plugin~Plugin
*/
export default class HTMLEmbedEditing extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'HTMLEmbedEditing';
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const t = editor.t;
const schema = editor.model.schema;
const conversion = editor.conversion;
schema.register( 'rawHtml', {
isObject: true,
allowWhere: '$block',
allowAttributes: [ 'value' ]
} );
conversion.for( 'upcast' ).elementToElement( {
view: {
name: 'div',
classes: 'raw-html-embed'
},
model: ( viewElement, { writer } ) => {
// Replace all view elements with their string presentations.
const innerHTML = [ ...viewElement.getChildren() ]
.map( child => viewStringify( child ) )
.join( '' );
return writer.createElement( 'rawHtml', {
value: innerHTML
} );
}
} );
conversion.for( 'dataDowncast' ).elementToElement( {
model: 'rawHtml',
view: ( modelElement, { writer } ) => {
return writer.createRawElement( 'div', { class: 'raw-html-embed' }, function( domElement ) {
domElement.innerHTML = modelElement.getAttribute( 'value' );
} );
}
} );
conversion.for( 'editingDowncast' ).elementToElement( {
model: 'rawHtml',
view: ( modelElement, { writer } ) => {
const label = t( 'HTML snippet' );
const viewWrapper = writer.createContainerElement( 'div' );
// TODO: `viewWrapper` should not be here but `toWidget` can be used only with the container element.
const rawElement = writer.createRawElement( 'div', { class: 'raw-html-embed' }, function( domElement ) {
domElement.innerHTML = sanitizeHtml( modelElement.getAttribute( 'value' ) );
} );
writer.insert( writer.createPositionAt( viewWrapper, 0 ), rawElement );
return toRawHtmlWidget( viewWrapper, writer, label );
}
} );
editor.commands.add( 'htmlEmbed', new HTMLEmbedCommand( editor ) );
}
}
// Converts a given {@link module:engine/view/element~Element} to a html widget:
// * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to
// recognize the html widget element.
// * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
//
// @param {module:engine/view/element~Element} viewElement
// @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
// @param {String} label The element's label.
// @returns {module:engine/view/element~Element}
function toRawHtmlWidget( viewElement, writer, label ) {
writer.setCustomProperty( 'rawHTML', true, viewElement );
return toWidget( viewElement, writer, { label } );
}