htmlembedediting.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module html-embed/htmlembedediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  10. import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
  11. import { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  13. import HtmlEmbedInsertCommand from './htmlembedinsertcommand';
  14. import HtmlEmbedUpdateCommand from './htmlembedupdatecommand';
  15. import pencilIcon from '@ckeditor/ckeditor5-core/theme/icons/pencil.svg';
  16. import eyeIcon from '../theme/icons/eye.svg';
  17. import '../theme/htmlembed.css';
  18. const DISPLAY_PREVIEW_CLASS = 'raw-html--display-preview';
  19. /**
  20. * The HTML embed editing feature.
  21. *
  22. * @extends module:core/plugin~Plugin
  23. */
  24. export default class HtmlEmbedEditing extends Plugin {
  25. /**
  26. * @inheritDoc
  27. */
  28. static get pluginName() {
  29. return 'HtmlEmbedEditing';
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. constructor( editor ) {
  35. super( editor );
  36. editor.config.define( 'htmlEmbed', {
  37. previewsInData: false,
  38. sanitizeHtml: rawHtml => {
  39. /**
  40. * When using the HTML embed feature with `htmlEmbed.previewsInData=true` option, it is strongly recommended to
  41. * define a sanitize function that will clean up an input HTML in order to avoid XSS vulnerability.
  42. *
  43. * For a detailed overview, check the {@glink features/html-embed HTML embed feature} documentation.
  44. *
  45. * @error html-embed-provide-sanitize-function
  46. */
  47. logWarning( 'html-embed-provide-sanitize-function' );
  48. return {
  49. html: rawHtml,
  50. hasModified: false
  51. };
  52. }
  53. } );
  54. }
  55. /**
  56. * @inheritDoc
  57. */
  58. init() {
  59. const editor = this.editor;
  60. const schema = editor.model.schema;
  61. schema.register( 'rawHtml', {
  62. isObject: true,
  63. allowWhere: '$block',
  64. allowAttributes: [ 'value' ]
  65. } );
  66. editor.commands.add( 'htmlEmbedUpdate', new HtmlEmbedUpdateCommand( editor ) );
  67. editor.commands.add( 'htmlEmbedInsert', new HtmlEmbedInsertCommand( editor ) );
  68. this._setupConversion();
  69. }
  70. /**
  71. * Prepares converters for the feature.
  72. *
  73. * @private
  74. */
  75. _setupConversion() {
  76. const editor = this.editor;
  77. const t = editor.t;
  78. const view = editor.editing.view;
  79. const htmlEmbedConfig = editor.config.get( 'htmlEmbed' );
  80. const upcastWriter = new UpcastWriter( view.document );
  81. const htmlProcessor = new HtmlDataProcessor( view.document );
  82. editor.conversion.for( 'upcast' ).elementToElement( {
  83. view: {
  84. name: 'div',
  85. classes: 'raw-html-embed'
  86. },
  87. model: ( viewElement, { writer } ) => {
  88. // Note: The below line has a side-effect – the children are *moved* to the DF so
  89. // viewElement becomes empty. It's fine here.
  90. const fragment = upcastWriter.createDocumentFragment( viewElement.getChildren() );
  91. const innerHtml = htmlProcessor.toData( fragment );
  92. return writer.createElement( 'rawHtml', {
  93. value: innerHtml
  94. } );
  95. }
  96. } );
  97. editor.conversion.for( 'dataDowncast' ).elementToElement( {
  98. model: 'rawHtml',
  99. view: ( modelElement, { writer } ) => {
  100. return writer.createRawElement( 'div', { class: 'raw-html-embed' }, function( domElement ) {
  101. domElement.innerHTML = modelElement.getAttribute( 'value' ) || '';
  102. } );
  103. }
  104. } );
  105. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  106. model: 'rawHtml',
  107. view: ( modelElement, { writer } ) => {
  108. const widgetLabel = t( 'HTML snippet' );
  109. const placeholder = t( 'Paste the raw code here.' );
  110. const widgetView = writer.createContainerElement( 'div', {
  111. class: 'raw-html'
  112. } );
  113. const rawHtmlContainer = writer.createContainerElement( 'div', {
  114. 'data-cke-ignore-events': true
  115. } );
  116. // Whether to show a preview mode or editing area.
  117. writer.setCustomProperty( 'isEditingSourceActive', false, rawHtmlContainer );
  118. const textareaAttributes = {
  119. placeholder,
  120. disabled: true,
  121. readonly: editor.isReadOnly,
  122. class: 'ck ck-input ck-input-text raw-html__source'
  123. };
  124. // The editing raw HTML field.
  125. const sourceElement = writer.createUIElement( 'textarea', textareaAttributes, function( domDocument ) {
  126. const root = this.toDomElement( domDocument );
  127. writer.setCustomProperty( 'domElement', root, sourceElement );
  128. root.value = modelElement.getAttribute( 'value' ) || '';
  129. root.addEventListener( 'input', () => {
  130. editor.execute( 'htmlEmbedUpdate', root.value );
  131. } );
  132. return root;
  133. } );
  134. const buttonAttributes = {
  135. class: 'ck ck-button ck-on raw-html__switch-mode'
  136. };
  137. // The switch button between preview and editing HTML.
  138. const toggleButton = writer.createUIElement( 'button', buttonAttributes, function( domDocument ) {
  139. const root = this.toDomElement( domDocument );
  140. root.innerHTML = pencilIcon;
  141. root.firstChild.classList.add( 'ck', 'ck-icon', 'ck-button__icon' );
  142. writer.setCustomProperty( 'domElement', root, toggleButton );
  143. root.addEventListener( 'click', evt => {
  144. view.change( writer => {
  145. if ( htmlEmbedConfig.previewsInData ) {
  146. if ( widgetView.hasClass( DISPLAY_PREVIEW_CLASS ) ) {
  147. writer.removeClass( DISPLAY_PREVIEW_CLASS, widgetView );
  148. } else {
  149. writer.addClass( DISPLAY_PREVIEW_CLASS, widgetView );
  150. }
  151. }
  152. const textarea = sourceElement.getCustomProperty( 'domElement' );
  153. textarea.disabled = !textarea.disabled;
  154. root.innerHTML = textarea.disabled ? pencilIcon : eyeIcon;
  155. root.firstChild.classList.add( 'ck', 'ck-icon', 'ck-button__icon' );
  156. } );
  157. evt.preventDefault();
  158. } );
  159. return root;
  160. } );
  161. writer.insert( writer.createPositionAt( widgetView, 0 ), rawHtmlContainer );
  162. writer.insert( writer.createPositionAt( rawHtmlContainer, 0 ), toggleButton );
  163. writer.insert( writer.createPositionAt( rawHtmlContainer, 1 ), sourceElement );
  164. // The container that renders the HTML should be created only when `htmlEmbed.previewsInData=true` in the config.
  165. if ( htmlEmbedConfig.previewsInData ) {
  166. writer.addClass( [ 'raw-html--preview-enabled', DISPLAY_PREVIEW_CLASS ], widgetView );
  167. const previewContainer = writer.createRawElement( 'div', { class: 'raw-html__preview' }, function( domElement ) {
  168. writer.setCustomProperty( 'domElement', domElement, previewContainer );
  169. const sanitizeOutput = htmlEmbedConfig.sanitizeHtml( modelElement.getAttribute( 'value' ) || '' );
  170. domElement.innerHTML = sanitizeOutput.html;
  171. } );
  172. writer.insert( writer.createPositionAt( rawHtmlContainer, 2 ), previewContainer );
  173. }
  174. // Listen to read-only changes.
  175. this.listenTo( editor, 'change:isReadOnly', ( evt, name, value ) => {
  176. sourceElement.getCustomProperty( 'domElement' ).readOnly = value;
  177. } );
  178. return toRawHtmlWidget( widgetView, writer, widgetLabel );
  179. }
  180. } );
  181. editor.conversion.for( 'editingDowncast' ).add( downcastRawHtmlValueAttribute( htmlEmbedConfig ) );
  182. }
  183. }
  184. // Returns a converter that handles the `value` attribute of the `rawHtml` element.
  185. //
  186. // It updates the source (`textarea`) value and passes an HTML to the preview element if `htmlEmbed.previewsInData=true`.
  187. //
  188. // @params {module:html-embed/htmlembed~HtmlEmbedConfig} htmlEmbedConfig
  189. // @returns {Function}
  190. function downcastRawHtmlValueAttribute( htmlEmbedConfig ) {
  191. return dispatcher => {
  192. dispatcher.on( 'attribute:value:rawHtml', ( evt, data, conversionApi ) => {
  193. const widgetView = conversionApi.mapper.toViewElement( data.item );
  194. const rawHtmlContainer = widgetView.getChild( 0 );
  195. const textareaDomElement = rawHtmlContainer.getChild( 1 ).getCustomProperty( 'domElement' );
  196. if ( textareaDomElement ) {
  197. textareaDomElement.value = data.item.getAttribute( 'value' );
  198. }
  199. if ( htmlEmbedConfig.previewsInData ) {
  200. const previewDomElement = rawHtmlContainer.getChild( 2 ).getCustomProperty( 'domElement' );
  201. if ( previewDomElement ) {
  202. const sanitizeOutput = htmlEmbedConfig.sanitizeHtml( data.item.getAttribute( 'value' ) );
  203. previewDomElement.innerHTML = sanitizeOutput.html;
  204. }
  205. }
  206. } );
  207. };
  208. }
  209. // Converts a given {@link module:engine/view/element~Element} to a raw html widget:
  210. // * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the raw html widget element.
  211. // * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
  212. //
  213. // @param {module:engine/view/element~Element} viewElement
  214. // @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
  215. // @param {String} label The element's label.
  216. // @returns {module:engine/view/element~Element}
  217. function toRawHtmlWidget( viewElement, writer, label ) {
  218. writer.setCustomProperty( 'rawHtml', true, viewElement );
  219. return toWidget( viewElement, writer, { label } );
  220. }