8
0
Kamil Piechaczek 5 лет назад
Родитель
Сommit
cf79f7d510

+ 23 - 11
packages/ckeditor5-html-embed/src/htmlembedediting.js

@@ -10,14 +10,17 @@
 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 HTMLEmbedInsertCommand from './htmlembedinsertcommand';
 import HTMLEmbedUpdateCommand from './htmlembedupdatecommand';
-import { toRawHtmlWidget } from './utils';
-import { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 import htmlEmbedModeIcon from '../theme/icons/htmlembedmode.svg';
+
 import '../theme/htmlembed.css';
 
+const DISPLAY_PREVIEW_CLASS = 'raw-html--display-preview';
+
 /**
  * The HTML embed editing feature.
  *
@@ -164,20 +167,16 @@ export default class HTMLEmbedEditing extends Plugin {
 
 					root.addEventListener( 'click', evt => {
 						view.change( writer => {
-							const isEditingSourceActive = rawHtmlContainer.getCustomProperty( 'isEditingSourceActive' );
-
 							if ( htmlEmbedConfig.previewsInData ) {
-								if ( !isEditingSourceActive ) {
-									writer.removeClass( 'raw-html--display-preview', widgetView );
+								if ( widgetView.hasClass( DISPLAY_PREVIEW_CLASS ) ) {
+									writer.removeClass( DISPLAY_PREVIEW_CLASS, widgetView );
 								} else {
-									writer.addClass( 'raw-html--display-preview', widgetView );
+									writer.addClass( DISPLAY_PREVIEW_CLASS, widgetView );
 								}
 							}
 
 							const textarea = sourceElement.getCustomProperty( 'domElement' );
 							textarea.disabled = !textarea.disabled;
-
-							writer.setCustomProperty( 'isEditingSourceActive', !isEditingSourceActive, rawHtmlContainer );
 						} );
 
 						evt.preventDefault();
@@ -192,8 +191,7 @@ export default class HTMLEmbedEditing extends Plugin {
 
 				// The container that renders the HTML should be created only when `htmlEmbed.previewsInData=true` in the config.
 				if ( htmlEmbedConfig.previewsInData ) {
-					writer.addClass( 'raw-html--preview-enabled', widgetView );
-					writer.addClass( 'raw-html--display-preview', widgetView );
+					writer.addClass( [ 'raw-html--preview-enabled', DISPLAY_PREVIEW_CLASS ], widgetView );
 
 					const previewContainer = writer.createRawElement( 'div', { class: 'raw-html__preview' }, function( domElement ) {
 						writer.setCustomProperty( 'domElement', domElement, previewContainer );
@@ -245,3 +243,17 @@ function downcastRawHtmlValueAttribute( htmlEmbedConfig ) {
 		} );
 	};
 }
+
+// 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 } );
+}

+ 1 - 0
packages/ckeditor5-html-embed/src/htmlembedinsertcommand.js

@@ -41,6 +41,7 @@ export default class HTMLEmbedInsertCommand extends Command {
 			const rawHtmlElement = writer.createElement( 'rawHtml' );
 
 			model.insertContent( rawHtmlElement );
+			writer.setSelection( rawHtmlElement, 'on' );
 		} );
 	}
 }

+ 23 - 1
packages/ckeditor5-html-embed/src/htmlembedui.js

@@ -10,7 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
 import htmlEmbedIcon from '../theme/icons/htmlembed.svg';
-import { getSelectedRawHtmlViewWidget } from './utils';
+import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
 
 /**
  * The HTML embed UI plugin.
@@ -54,3 +54,25 @@ export default class HTMLEmbedUI extends Plugin {
 		} );
 	}
 }
+
+// Returns a raw html widget editing view element if one is selected.
+//
+// @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
+// @returns {module:engine/view/element~Element|null}
+function getSelectedRawHtmlViewWidget( selection ) {
+	const viewElement = selection.getSelectedElement();
+
+	if ( viewElement && isRawHtmlWidget( viewElement ) ) {
+		return viewElement;
+	}
+
+	return null;
+}
+
+// Checks if a given view element is a raw html widget.
+//
+// @param {module:engine/view/element~Element} viewElement
+// @returns {Boolean}
+function isRawHtmlWidget( viewElement ) {
+	return !!viewElement.getCustomProperty( 'rawHtml' ) && isWidget( viewElement );
+}

+ 14 - 1
packages/ckeditor5-html-embed/src/htmlembedupdatecommand.js

@@ -7,7 +7,6 @@
  * @module html-embed/htmlembedupdatecommand
  */
 
-import { getSelectedRawHtmlModelWidget } from './utils';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 /**
@@ -51,3 +50,17 @@ export default class HTMLEmbedUpdateCommand extends Command {
 		}
 	}
 }
+
+// Returns a selected raw html element in the model, if any.
+//
+// @param {module:engine/model/selection~Selection} selection
+// @returns {module:engine/model/element~Element|null}
+function getSelectedRawHtmlModelWidget( selection ) {
+	const selectedElement = selection.getSelectedElement();
+
+	if ( selectedElement && selectedElement.is( 'element', 'rawHtml' ) ) {
+		return selectedElement;
+	}
+
+	return null;
+}

+ 0 - 88
packages/ckeditor5-html-embed/src/utils.js

@@ -1,88 +0,0 @@
-/**
- * @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/utils
- */
-
-import { isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
-
-/**
- * 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}
- */
-export function toRawHtmlWidget( viewElement, writer, label ) {
-	writer.setCustomProperty( 'rawHtml', true, viewElement );
-
-	return toWidget( viewElement, writer, { label } );
-}
-
-/**
- * Returns a raw html widget editing view element if one is selected.
- *
- * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
- * @returns {module:engine/view/element~Element|null}
- */
-export function getSelectedRawHtmlViewWidget( selection ) {
-	const viewElement = selection.getSelectedElement();
-
-	if ( viewElement && isRawHtmlWidget( viewElement ) ) {
-		return viewElement;
-	}
-
-	return null;
-}
-
-/**
- * Checks if a given view element is a raw html widget.
- *
- * @param {module:engine/view/element~Element} viewElement
- * @returns {Boolean}
- */
-export function isRawHtmlWidget( viewElement ) {
-	return !!viewElement.getCustomProperty( 'rawHtml' ) && isWidget( viewElement );
-}
-
-/**
- * Returns a selected raw html element in the model, if any.
- *
- * @param {module:engine/model/selection~Selection} selection
- * @returns {module:engine/model/element~Element|null}
- */
-export function getSelectedRawHtmlModelWidget( selection ) {
-	const selectedElement = selection.getSelectedElement();
-
-	if ( selectedElement && selectedElement.is( 'element', 'rawHtml' ) ) {
-		return selectedElement;
-	}
-
-	return null;
-}
-
-/**
- * Creates a raw html element and inserts it into the model.
- *
- * **Note**: This method will use {@link module:engine/model/model~Model#insertContent `model.insertContent()`} logic of inserting content
- * if no `insertPosition` is passed.
- *
- * @param {module:engine/model/model~Model} model
- * @param {String} [rawHtml='']
- */
-export function insertRawHtml( model, rawHtml = null ) {
-	model.change( writer => {
-		const rawHtmlElement = writer.createElement( 'rawHtml' );
-
-		model.insertContent( rawHtmlElement );
-
-		writer.setSelection( rawHtmlElement, 'on' );
-		writer.setAttribute( 'value', rawHtml, rawHtmlElement );
-	} );
-}

+ 5 - 7
packages/ckeditor5-html-embed/tests/htmlembedediting.js

@@ -11,7 +11,7 @@ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictest
 import HTMLEmbedUpdateCommand from '../src/htmlembedupdatecommand';
 import HTMLEmbedInsertCommand from '../src/htmlembedinsertcommand';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
-import { isRawHtmlWidget } from '../src/utils';
+import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
 
 describe( 'HTMLEMbedEditing', () => {
 	let element, editor, model, view, viewDocument;
@@ -261,9 +261,6 @@ describe( 'HTMLEMbedEditing', () => {
 			it.skip( 'should update the `value` attribute after applying changes in the edit source element', () => {
 			} );
 
-			it.skip( 'should remove DOM events when destroying the plugin', () => {
-			} );
-
 			it.skip( 'should show the preview element by default', () => {
 			} );
 		} );
@@ -337,11 +334,12 @@ describe( 'HTMLEMbedEditing', () => {
 			it.skip( 'should re-render the preview element after applying changes in the edit source element', () => {
 			} );
 
-			it.skip( 'should remove DOM events when destroying the plugin', () => {
-			} );
-
 			it.skip( 'should show the preview element by default', () => {
 			} );
 		} );
 	} );
 } );
+
+function isRawHtmlWidget( viewElement ) {
+	return !!viewElement.getCustomProperty( 'rawHtml' ) && isWidget( viewElement );
+}

+ 0 - 7
packages/ckeditor5-html-embed/tests/utils.js

@@ -1,7 +0,0 @@
-/**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-describe( 'utils', () => {
-} );