8
0
Quellcode durchsuchen

Simplify content normalizers to required functionality.

Mateusz Samsel vor 6 Jahren
Ursprung
Commit
a284608300

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

@@ -11,62 +11,29 @@ export default class ContentNormalizer {
 	constructor( { activationTrigger } ) {
 		this.activationTrigger = activationTrigger;
 
-		this._reset();
-
 		this._filters = new Set();
-		this._fullContentFilters = new Set();
 	}
 
-	setInputData( data ) {
-		this._reset();
-
+	transform( data ) {
 		const html = data.dataTransfer && data.dataTransfer.getData( 'text/html' );
 		const dataReadFirstTime = data.isTransformedWithPasteFromOffice === undefined;
 		const hasHtmlData = !!html;
 
 		if ( hasHtmlData && dataReadFirstTime && this.activationTrigger( html ) ) {
-			this.data = data;
-			this.isActive = true;
-			this.data.isTransformedWithPasteFromOffice = false;
+			this._applyFilters( data );
+			data.isTransformedWithPasteFromOffice = true;
 		}
 
 		return this;
 	}
 
-	addFilter( filterDefinition ) {
-		if ( filterDefinition.fullContent ) {
-			this._fullContentFilters.add( filterDefinition );
-		} else {
-			this._filters.add( filterDefinition );
-		}
+	addFilter( filterFn ) {
+		this._filters.add( filterFn );
 	}
 
-	exec() {
-		if ( !this.isActive ) {
-			return;
-		}
-
-		if ( !this.data.isTransformedWithPasteFromOffice ) {
-			this._applyFullContentFilters();
-		}
-
-		this.data.isTransformedWithPasteFromOffice = true;
-
-		return this;
-	}
-
-	_reset() {
-		this.data = null;
-		this.isActive = false;
-	}
-
-	_applyFullContentFilters() {
-		if ( !this._fullContentFilters.size ) {
-			return;
-		}
-
-		for ( const filter of this._fullContentFilters ) {
-			filter.exec( this.data );
+	_applyFilters( data ) {
+		for ( const filter of this._filters ) {
+			filter( data );
 		}
 	}
 }

+ 2 - 5
packages/ckeditor5-paste-from-office/src/normalizers/googledocs.js

@@ -15,11 +15,8 @@ export const googleDocsNormalizer = ( () => {
 		activationTrigger: contentString => /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( contentString )
 	} );
 
-	normalizer.addFilter( {
-		fullContent: true,
-		exec: data => {
-			removeBoldTagWrapper( data.content );
-		}
+	normalizer.addFilter( data => {
+		removeBoldTagWrapper( data.content );
 	} );
 
 	return normalizer;

+ 3 - 6
packages/ckeditor5-paste-from-office/src/normalizers/msword.js

@@ -19,13 +19,10 @@ export const mswordNormalizer = ( () => {
 			/xmlns:o="urn:schemas-microsoft-com/i.test( contentString )
 	} );
 
-	normalizer.addFilter( {
-		fullContent: true,
-		exec: data => {
-			const html = data.dataTransfer.getData( 'text/html' );
+	normalizer.addFilter( data => {
+		const html = data.dataTransfer.getData( 'text/html' );
 
-			data.content = _normalizeWordInput( html, data.dataTransfer );
-		}
+		data.content = _normalizeWordInput( html, data.dataTransfer );
 	} );
 
 	return normalizer;

+ 1 - 5
packages/ckeditor5-paste-from-office/src/pastefromoffice.js

@@ -61,11 +61,7 @@ export default class PasteFromOffice extends Plugin {
 			'inputTransformation',
 			( evt, data ) => {
 				for ( const normalizer of this._normalizers ) {
-					normalizer.setInputData( data );
-
-					if ( normalizer.isActive ) {
-						normalizer.exec();
-					}
+					normalizer.transform( data );
 				}
 			},
 			{ priority: 'high' }

+ 1 - 1
packages/ckeditor5-paste-from-office/tests/contentnormalizer.js

@@ -81,7 +81,7 @@ describe( 'ContentNormalizer', () => {
 					activationTrigger: sinonTrigger
 				} );
 
-				normalizer.setInputData( data );
+				normalizer.transform( data );
 			} );
 
 			it( 'should not be active', () => {