8
0
Pārlūkot izejas kodu

Updated: autoformat engine names.

Maksymilian Barnaś 9 gadi atpakaļ
vecāks
revīzija
1e1adbd760

+ 11 - 4
packages/ckeditor5-autoformat/src/autoformat.js

@@ -3,7 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
-import AutoformatEngine from './autoformatengine.js';
+import BlockAutoformatEngine from './blockautoformatengine.js';
+import InlineAutoformatEngine from './inlineautoformatengine.js';
 import Feature from '../core/feature.js';
 import HeadingEngine from '../heading/headingengine.js';
 import ListEngine from '../list/listengine.js';
@@ -32,6 +33,7 @@ export default class Autoformat extends Feature {
 	init() {
 		this._addListAutoformats();
 		this._addHeadingAutoformats();
+		this._addBoldAutoformats();
 	}
 
 	/**
@@ -48,8 +50,8 @@ export default class Autoformat extends Feature {
 	 * @private
 	 */
 	_addListAutoformats() {
-		new AutoformatEngine( this.editor, /^[\*\-]\s$/, 'bulletedList' );
-		new AutoformatEngine( this.editor, /^\d+[\.|)]?\s$/, 'numberedList' );
+		new BlockAutoformatEngine( this.editor, /^[\*\-]\s$/, 'bulletedList' );
+		new BlockAutoformatEngine( this.editor, /^\d+[\.|)]?\s$/, 'numberedList' );
 	}
 
 	/**
@@ -63,7 +65,7 @@ export default class Autoformat extends Feature {
 	 * @private
 	 */
 	_addHeadingAutoformats() {
-		new AutoformatEngine( this.editor, /^(#{1,3})\s$/, ( context ) => {
+		new BlockAutoformatEngine( this.editor, /^(#{1,3})\s$/, ( context ) => {
 			const { batch, match } = context;
 			const headingLevel = match[ 1 ].length;
 
@@ -73,4 +75,9 @@ export default class Autoformat extends Feature {
 			} );
 		} );
 	}
+
+	_addBoldAutoformats() {
+		// new InlineAutoformatEngine( this.editor, new RegExp( /(\*\*.+?\*\*)/g ), 'bold' );
+		new InlineAutoformatEngine( this.editor, '*', 'italic' );
+	}
 }

packages/ckeditor5-autoformat/src/autoformatengine.js → packages/ckeditor5-autoformat/src/blockautoformatengine.js


+ 13 - 39
packages/ckeditor5-autoformat/src/inline.js

@@ -3,7 +3,6 @@
  * For licensing, see LICENSE.md.
  */
 
-import Feature from '../core/feature.js';
 import Range from '../engine/model/range.js';
 import RootElement from '../engine/model/rootelement.js';
 
@@ -14,79 +13,54 @@ import RootElement from '../engine/model/rootelement.js';
  * @memberOf paragraph
  * @extends ckeditor5.Feature
  */
-export default class Inline extends Feature {
-	/**
-	 * @inheritDoc
-	 */
-	init() {
-		const editor = this.editor;
+export default class InlineAutoformatEngine {
+
+	constructor( editor, delimiter, command ) {
+		this.editor = editor;
+		const pattern = new RegExp( `(\\${ delimiter }.+?\\${ delimiter })`, 'g' );
 		const doc = editor.document;
-		let blockEvents = false;
 
 		// Listen to model changes and add attributes.
-		this.listenTo( doc, 'change', ( evt, type, data ) => {
-			if ( type === 'insert' && !blockEvents ) {
+		editor.document.on( 'change', ( evt, type, data ) => {
+			if ( type === 'insert' ) {
 				const insertPosition = data.range.start;
 				const insertBlock = findTopmostBlock( insertPosition );
 
 				applyAttributes( insertBlock );
 			} else
-			if ( type === 'remove' && !blockEvents ) {
+			if ( type === 'remove' ) {
 				const removePosition = data.sourcePosition;
 				const removeBlock = findTopmostBlock( removePosition );
 
 				if ( removeBlock !== null ) {
 					applyAttributes( removeBlock );
 				}
-			} else
-			if ( type === 'move' ) {
-				const movePosition = data.sourcePosition;
-				const moveBlock = findTopmostBlock( movePosition );
-
-				applyAttributes( moveBlock );
-
-				const destPosition = data.range.start;
-				const destBlock = findTopmostBlock( destPosition );
-
-				applyAttributes( destBlock );
 			}
 		} );
 
 		function applyAttributes( block ) {
 			const text = getText( block );
-			const regexp = new RegExp( /(\*\*.+?\*\*)|(\*.+?\*)/g );
-
-			let delimiterLen;
 			let result;
 
-			while ( ( result = regexp.exec( text ) ) !== null ) {
+			while ( ( result = pattern.exec( text ) ) !== null ) {
 				let matched;
-				let attr;
 
 				if ( result[ 1 ] ) {
 					matched = result[ 1 ];
-					attr = 'bold';
-					delimiterLen = 2;
 				} else {
 					return;
 				}
 
-				// if ( result[ 2 ] ) {
-				// 	matched = result[ 2 ];
-				// 	attr = 'italic';
-				// 	delimiterLen = 1;
-				// }
-
 				const index = result.index;
 
 				doc.enqueueChanges( () => {
 					const batch = doc.batch();
 					const rangeToDeleteStart = Range.createFromParentsAndOffsets(
 						block, index,
-						block, index + delimiterLen
+						block, index + delimiter.length
 					);
 					const rangeToDeleteEnd = Range.createFromParentsAndOffsets(
-						block, index + matched.length - delimiterLen,
+						block, index + matched.length - delimiter.length,
 						block, index + matched.length
 					);
 
@@ -95,10 +69,10 @@ export default class Inline extends Feature {
 
 					const range = Range.createFromParentsAndOffsets(
 						block, index,
-						block, index + matched.length - delimiterLen * 2
+						block, index + matched.length - delimiter.length * 2
 					);
 
-					batch.setAttribute( range, attr, true );
+					batch.setAttribute( range, command, true );
 				} );
 			}
 		}