8
0
فهرست منبع

Implement normalizers instances for google docs and ms word and extract them to separate files. Clean up in contentnormalizer api.

Mateusz Samsel 6 سال پیش
والد
کامیت
ad38786884

+ 2 - 2
packages/ckeditor5-paste-from-office/src/contentnormalizer.js

@@ -22,7 +22,7 @@ export default class ContentNormalizer {
 		return this._active;
 	}
 
-	addData( data ) {
+	setInputData( data ) {
 		const html = data.dataTransfer.getData( 'text/html' );
 
 		if ( html && this.activationTrigger( html ) ) {
@@ -48,7 +48,7 @@ export default class ContentNormalizer {
 		}
 	}
 
-	filter() {
+	exec() {
 		if ( this.data && !this.data.isTransformedWithPasteFromOffice ) {
 			this._applyFullContentFilters();
 

+ 26 - 0
packages/ckeditor5-paste-from-office/src/normalizers/googledocs.js

@@ -0,0 +1,26 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module paste-from-office/normalizer
+ */
+
+import ContentNormalizer from '../contentnormalizer';
+import { removeBoldTagWrapper } from '../filters/common';
+
+export const googleDocsNormalizer = ( () => {
+	const normalizer = new ContentNormalizer( {
+		activationTrigger: contentString => /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( contentString )
+	} );
+
+	normalizer.addFilter( {
+		fullContent: true,
+		exec: data => {
+			removeBoldTagWrapper( data.content );
+		}
+	} );
+
+	return normalizer;
+} )();

+ 49 - 0
packages/ckeditor5-paste-from-office/src/normalizers/msword.js

@@ -0,0 +1,49 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module paste-from-office/normalizer
+ */
+
+import ContentNormalizer from '../contentnormalizer';
+import { parseHtml } from '../filters/parse';
+import { transformListItemLikeElementsIntoLists } from '../filters/list';
+import { replaceImagesSourceWithBase64 } from '../filters/image';
+
+export const mswordNormalizer = ( () => {
+	const normalizer = new ContentNormalizer( {
+		activationTrigger: contentString =>
+			/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( contentString ) ||
+			/xmlns:o="urn:schemas-microsoft-com/i.test( contentString )
+	} );
+
+	normalizer.addFilter( {
+		fullContent: true,
+		exec: data => {
+			const html = data.dataTransfer.getData( 'text/html' );
+
+			data.content = _normalizeWordInput( html, data.dataTransfer );
+		}
+	} );
+
+	return normalizer;
+} )();
+
+//
+// Normalizes input pasted from Word to format suitable for editor {@link module:engine/model/model~Model}.
+//
+// @private
+// @param {String} input Word input.
+// @param {module:clipboard/datatransfer~DataTransfer} dataTransfer Data transfer instance.
+// @returns {module:engine/view/documentfragment~DocumentFragment} Normalized input.
+//
+function _normalizeWordInput( input, dataTransfer ) {
+	const { body, stylesString } = parseHtml( input );
+
+	transformListItemLikeElementsIntoLists( body, stylesString );
+	replaceImagesSourceWithBase64( body, dataTransfer.getData( 'text/rtf' ) );
+
+	return body;
+}

+ 14 - 124
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -9,12 +9,8 @@
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 
-import { parseHtml } from './filters/parse';
-import { transformListItemLikeElementsIntoLists } from './filters/list';
-import { replaceImagesSourceWithBase64 } from './filters/image';
-import { removeBoldTagWrapper } from './filters/common';
-import ContentNormalizer from './contentnormalizer';
-import Collection from '@ckeditor/ckeditor5-utils/src/collection';
+import { googleDocsNormalizer } from './normalizers/googledocs';
+import { mswordNormalizer } from './normalizers/msword';
 
 /**
  * The Paste from Office plugin.
@@ -33,10 +29,10 @@ export default class PasteFromOffice extends Plugin {
 	constructor( editor ) {
 		super( editor );
 
-		this._normalizers = new Collection();
+		this._normalizers = new Set();
 
-		this._normalizers.add( this._getWordNormalizer() );
-		this._normalizers.add( this._getGoogleDocsNormalizer() );
+		this._normalizers.add( mswordNormalizer );
+		this._normalizers.add( googleDocsNormalizer );
 	}
 
 	/**
@@ -55,122 +51,16 @@ export default class PasteFromOffice extends Plugin {
 		this.listenTo(
 			editor.plugins.get( 'Clipboard' ),
 			'inputTransformation',
-			this._inputTransformationListener.bind( this ),
+			( evt, data ) => {
+				for ( const normalizer of this._normalizers ) {
+					normalizer.setInputData( data );
+
+					if ( normalizer.isActive ) {
+						normalizer.exec();
+					}
+				}
+			},
 			{ priority: 'high' }
 		);
 	}
-
-	_getWordNormalizer() {
-		const wordNormalizer = new ContentNormalizer( {
-			activationTrigger: contentString =>
-				/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( contentString ) ||
-				/xmlns:o="urn:schemas-microsoft-com/i.test( contentString )
-		} );
-
-		wordNormalizer.addFilter( {
-			fullContent: true,
-			exec: data => {
-				const html = data.dataTransfer.getData( 'text/html' );
-
-				data.content = PasteFromOffice._normalizeWordInput( html, data.dataTransfer );
-			}
-		} );
-
-		return wordNormalizer;
-	}
-
-	_getGoogleDocsNormalizer() {
-		const googleDocsNormalizer = new ContentNormalizer( {
-			activationTrigger: contentString => /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( contentString )
-		} );
-
-		googleDocsNormalizer.addFilter( {
-			fullContent: true,
-			exec: data => {
-				removeBoldTagWrapper( data.content );
-			}
-		} );
-
-		return googleDocsNormalizer;
-	}
-
-	/**
-	 * Listener fired during {@link module:clipboard/clipboard~Clipboard#event:inputTransformation `inputTransformation` event}.
-	 * Detects if content comes from a recognized source and normalize it.
-	 *
-	 * **Note**: this function was exposed mainly for testing purposes and should not be called directly.
-	 *
-	 * @private
-	 * @param {module:utils/eventinfo~EventInfo} evt
-	 * @param {Object} data same structure like {@link module:clipboard/clipboard~Clipboard#event:inputTransformation input transformation}
-	 */
-	_inputTransformationListener( evt, data ) {
-		for ( const normalizer of this._normalizers ) {
-			normalizer.addData( data );
-
-			if ( normalizer.isActive ) {
-				normalizer.filter();
-			}
-		}
-	}
-
-	/**
-	 * Normalizes input pasted from Word to format suitable for editor {@link module:engine/model/model~Model}.
-	 *
-	 * **Note**: this function is exposed mainly for testing purposes and should not be called directly.
-	 *
-	 * @private
-	 * @param {String} input Word input.
-	 * @param {module:clipboard/datatransfer~DataTransfer} dataTransfer Data transfer instance.
-	 * @returns {module:engine/view/documentfragment~DocumentFragment} Normalized input.
-	 */
-	static _normalizeWordInput( input, dataTransfer ) {
-		const { body, stylesString } = parseHtml( input );
-
-		transformListItemLikeElementsIntoLists( body, stylesString );
-		replaceImagesSourceWithBase64( body, dataTransfer.getData( 'text/rtf' ) );
-
-		return body;
-	}
-
-	/**
-	 * Normalizes input pasted from Google Docs to format suitable for editor {@link module:engine/model/model~Model}.
-	 *
-	 * **Note**: this function is exposed mainly for testing purposes and should not be called directly.
-	 *
-	 * @private
-	 * @param {module:engine/view/documentfragment~DocumentFragment} documentFragment normalized clipboard data
-	 * @returns {module:engine/view/documentfragment~DocumentFragment} document fragment normalized with set of filters dedicated
-	 * for Google Docs
-	 */
-	static _normalizeGoogleDocsInput( documentFragment ) {
-		return removeBoldTagWrapper( documentFragment );
-	}
-
-	/**
-	 * Determines if given paste data came from the specific office-like application.
-	 * Currently recognized are:
-	 * * `'msword'` for Microsoft Words desktop app
-	 * * `'gdocs'` for Google Docs online app
-	 *
-	 * **Note**: this function is exposed mainly for testing purposes and should not be called directly.
-	 *
-	 * @private
-	 * @param {String} html the `text/html` string from data transfer
-	 * @return {String|null} name of source app of an html data or null
-	 */
-	static _getInputType( html ) {
-		if (
-			/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( html ) ||
-			/xmlns:o="urn:schemas-microsoft-com/i.test( html )
-		) {
-			// Microsoft Word detection
-			return 'msword';
-		} else if ( /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( html ) ) {
-			// Google Docs detection
-			return 'gdocs';
-		}
-
-		return null;
-	}
 }