htmlembedediting.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 HTMLEmbedInsertCommand from './htmlembedinsertcommand';
  12. import HTMLEmbedUpdateCommand from './htmlembedupdatecommand';
  13. import { toRawHtmlWidget } from './utils';
  14. import { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. import htmlEmbedModeIcon from '../theme/icons/htmlembedmode.svg';
  16. import '../theme/htmlembed.css';
  17. /**
  18. * The HTML embed editing feature.
  19. *
  20. * @extends module:core/plugin~Plugin
  21. */
  22. export default class HTMLEmbedEditing extends Plugin {
  23. /**
  24. * @inheritDoc
  25. */
  26. static get pluginName() {
  27. return 'HTMLEmbedEditing';
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. constructor( editor ) {
  33. super( editor );
  34. editor.config.define( 'htmlEmbed', {
  35. previewsInData: false,
  36. sanitizeHtml: rawHtml => {
  37. /**
  38. * When using the HTML embed feature with `htmlEmbed.previewsInData=true` option, it's strongly recommended to
  39. * define a sanitize function that will clean an input HTML in order to avoid XSS vulnerability.
  40. * TODO: Add a link to the feature documentation.
  41. *
  42. * @error html-embed-provide-sanitize-function
  43. * @param {String} name The name of the component.
  44. */
  45. logWarning( 'html-embed-provide-sanitize-function' );
  46. return {
  47. html: rawHtml,
  48. hasModified: false
  49. };
  50. }
  51. } );
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. init() {
  57. const editor = this.editor;
  58. const schema = editor.model.schema;
  59. const htmlEmbedInsertCommand = new HTMLEmbedInsertCommand( editor );
  60. const htmlEmbedUpdateCommand = new HTMLEmbedUpdateCommand( editor );
  61. schema.register( 'rawHtml', {
  62. isObject: true,
  63. allowWhere: '$block',
  64. allowAttributes: [ 'value' ]
  65. } );
  66. editor.commands.add( 'htmlEmbedUpdate', htmlEmbedUpdateCommand );
  67. editor.commands.add( 'htmlEmbedInsert', htmlEmbedInsertCommand );
  68. this._setupConversion();
  69. }
  70. /**
  71. * Set-ups 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. const fragment = upcastWriter.createDocumentFragment( viewElement.getChildren() );
  89. const innerHtml = htmlProcessor.toData( fragment );
  90. return writer.createElement( 'rawHtml', {
  91. value: innerHtml
  92. } );
  93. }
  94. } );
  95. editor.conversion.for( 'dataDowncast' ).elementToElement( {
  96. model: 'rawHtml',
  97. view: ( modelElement, { writer } ) => {
  98. return writer.createRawElement( 'div', { class: 'raw-html-embed' }, function( domElement ) {
  99. domElement.innerHTML = modelElement.getAttribute( 'value' ) || '';
  100. } );
  101. }
  102. } );
  103. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  104. model: 'rawHtml',
  105. view: ( modelElement, { writer } ) => {
  106. const widgetLabel = t( 'HTML snippet' );
  107. const placeholder = t( 'Paste the raw code here.' );
  108. const widgetView = writer.createContainerElement( 'div', {
  109. class: 'raw-html'
  110. } );
  111. const rawHtmlContainer = writer.createContainerElement( 'div', {
  112. 'data-cke-ignore-events': true
  113. } );
  114. // Whether to show a preview mode or editing area.
  115. writer.setCustomProperty( 'isEditingSourceActive', false, rawHtmlContainer );
  116. const textareaAttributes = {
  117. placeholder,
  118. disabled: true,
  119. class: 'raw-html__source'
  120. };
  121. // The editing raw HTML field.
  122. const sourceElement = writer.createUIElement( 'textarea', textareaAttributes, function( domDocument ) {
  123. const root = this.toDomElement( domDocument );
  124. writer.setCustomProperty( 'domElement', root, sourceElement );
  125. root.value = modelElement.getAttribute( 'value' ) || '';
  126. root.addEventListener( 'input', () => {
  127. editor.execute( 'htmlEmbedUpdate', root.value );
  128. } );
  129. return root;
  130. } );
  131. // The switch button between preview and editing HTML.
  132. const toggleButton = writer.createUIElement( 'div', { class: 'raw-html__switch-mode' }, function( domDocument ) {
  133. const root = this.toDomElement( domDocument );
  134. root.innerHTML = htmlEmbedModeIcon;
  135. writer.setCustomProperty( 'domElement', root, toggleButton );
  136. root.addEventListener( 'click', evt => {
  137. view.change( writer => {
  138. const isEditingSourceActive = rawHtmlContainer.getCustomProperty( 'isEditingSourceActive' );
  139. if ( htmlEmbedConfig.previewsInData ) {
  140. if ( !isEditingSourceActive ) {
  141. writer.removeClass( 'raw-html--display-preview', widgetView );
  142. } else {
  143. writer.addClass( 'raw-html--display-preview', widgetView );
  144. }
  145. }
  146. const textarea = sourceElement.getCustomProperty( 'domElement' );
  147. textarea.disabled = !textarea.disabled;
  148. writer.setCustomProperty( 'isEditingSourceActive', !isEditingSourceActive, rawHtmlContainer );
  149. } );
  150. evt.preventDefault();
  151. } );
  152. return root;
  153. } );
  154. writer.insert( writer.createPositionAt( widgetView, 0 ), rawHtmlContainer );
  155. writer.insert( writer.createPositionAt( rawHtmlContainer, 0 ), toggleButton );
  156. writer.insert( writer.createPositionAt( rawHtmlContainer, 1 ), sourceElement );
  157. // The container that renders the HTML should be created only when `htmlEmbed.previewsInData=true` in the config.
  158. if ( htmlEmbedConfig.previewsInData ) {
  159. writer.addClass( 'raw-html--preview-enabled', widgetView );
  160. writer.addClass( 'raw-html--display-preview', widgetView );
  161. const previewContainer = writer.createRawElement( 'div', { class: 'raw-html__preview' }, function( domElement ) {
  162. writer.setCustomProperty( 'domElement', domElement, previewContainer );
  163. if ( htmlEmbedConfig.previewsInData ) {
  164. const sanitizeOutput = htmlEmbedConfig.sanitizeHtml( modelElement.getAttribute( 'value' ) || '' );
  165. domElement.innerHTML = sanitizeOutput.html;
  166. }
  167. } );
  168. writer.insert( writer.createPositionAt( rawHtmlContainer, 2 ), previewContainer );
  169. }
  170. return toRawHtmlWidget( widgetView, writer, widgetLabel );
  171. }
  172. } );
  173. editor.conversion.for( 'editingDowncast' ).add( downcastRawHtmlValueAttribute( htmlEmbedConfig ) );
  174. }
  175. }
  176. // Returns a converter that handles the `value` attribute of the `rawHtml` element.
  177. //
  178. // It updates the source (`textarea`) value and passes an HTML to the preview element in `htmlEmbed.previewsInData=true` mode.
  179. //
  180. // @params {module:html-embed/htmlembed~MediaEmbedConfig} htmlEmbedConfig
  181. // @returns {Function}
  182. function downcastRawHtmlValueAttribute( htmlEmbedConfig ) {
  183. return dispatcher => {
  184. dispatcher.on( 'attribute:value:rawHtml', ( evt, data, conversionApi ) => {
  185. const widgetView = conversionApi.mapper.toViewElement( data.item );
  186. const rawHtmlContainer = widgetView.getChild( 0 );
  187. const textareaDomElement = rawHtmlContainer.getChild( 1 ).getCustomProperty( 'domElement' );
  188. if ( textareaDomElement ) {
  189. textareaDomElement.value = data.item.getAttribute( 'value' );
  190. }
  191. if ( htmlEmbedConfig.previewsInData ) {
  192. const previewDomElement = rawHtmlContainer.getChild( 2 ).getCustomProperty( 'domElement' );
  193. if ( previewDomElement ) {
  194. const sanitizeOutput = htmlEmbedConfig.sanitizeHtml( data.item.getAttribute( 'value' ) );
  195. previewDomElement.innerHTML = sanitizeOutput.html;
  196. }
  197. }
  198. } );
  199. };
  200. }