Преглед изворни кода

Extract table selection highlighting.

Maciej Gołaszewski пре 6 година
родитељ
комит
97507afadb

+ 4 - 45
packages/ckeditor5-table/src/tableselection.js

@@ -15,6 +15,7 @@ import { findAncestor } from './commands/utils';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import TableUtils from './tableutils';
+import { setupTableSelectionHighlighting } from './tableselection/converters';
 
 /**
  * The table selection plugin.
@@ -45,7 +46,6 @@ export default class TableSelection extends Plugin {
 		super( editor );
 
 		this._isSelecting = false;
-		this._highlighted = new Set();
 	}
 
 	/**
@@ -62,33 +62,10 @@ export default class TableSelection extends Plugin {
 	 * @inheritDoc
 	 */
 	init() {
-		const editor = this.editor;
-
-		this._tableUtils = editor.plugins.get( 'TableUtils' );
-		this._mouseHandler = new MouseSelectionHandler( this, editor.editing );
-
-		editor.conversion.for( 'editingDowncast' ).add( dispatcher => dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
-			const viewWriter = conversionApi.writer;
-			const viewSelection = viewWriter.document.selection;
-
-			if ( this._isSelecting ) {
-				this._clearHighlightedTableCells();
-
-				for ( const tableCell of this.getSelectedTableCells() ) {
-					const viewElement = conversionApi.mapper.toViewElement( tableCell );
+		this._tableUtils = this.editor.plugins.get( 'TableUtils' );
+		this._mouseHandler = new MouseSelectionHandler( this, this.editor.editing );
 
-					viewWriter.addClass( 'selected', viewElement );
-					this._highlighted.add( viewElement );
-				}
-
-				const ranges = viewSelection.getRanges();
-				const from = Array.from( ranges );
-
-				viewWriter.setSelection( from, { fake: true, label: 'TABLE' } );
-			} else {
-				this._clearHighlightedTableCells();
-			}
-		}, { priority: 'lowest' } ) );
+		setupTableSelectionHighlighting( this.editor, this );
 	}
 
 	/**
@@ -175,8 +152,6 @@ export default class TableSelection extends Plugin {
 		this._startElement = undefined;
 		this._endElement = undefined;
 		this._isSelecting = false;
-		this._clearHighlightedTableCells();
-		this._highlighted.clear();
 	}
 
 	/**
@@ -231,22 +206,6 @@ export default class TableSelection extends Plugin {
 			writer.setSelection( modelRanges );
 		} );
 	}
-
-	/**
-	 * Removes highlight from table cells.
-	 *
-	 * @TODO move to highlight handling.
-	 * @private
-	 */
-	_clearHighlightedTableCells() {
-		const previous = [ ...this._highlighted.values() ];
-
-		this.editor.editing.view.change( writer => {
-			for ( const previouslyHighlighted of previous ) {
-				writer.removeClass( 'selected', previouslyHighlighted );
-			}
-		} );
-	}
 }
 
 // Finds model table cell for given DOM event - ie. for 'mousedown'.

+ 44 - 0
packages/ckeditor5-table/src/tableselection/converters.js

@@ -0,0 +1,44 @@
+/**
+ * @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 table/tableselection/converters
+ */
+
+export function setupTableSelectionHighlighting( editor, tableSelection ) {
+	const highlighted = new Set();
+	editor.conversion.for( 'editingDowncast' ).add( dispatcher => dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
+		const viewWriter = conversionApi.writer;
+		const viewSelection = viewWriter.document.selection;
+
+		if ( tableSelection._isSelecting ) {
+			clearHighlightedTableCells( highlighted, editor.editing.view );
+
+			for ( const tableCell of tableSelection.getSelectedTableCells() ) {
+				const viewElement = conversionApi.mapper.toViewElement( tableCell );
+
+				viewWriter.addClass( 'selected', viewElement );
+				highlighted.add( viewElement );
+			}
+
+			const ranges = viewSelection.getRanges();
+			const from = Array.from( ranges );
+
+			viewWriter.setSelection( from, { fake: true, label: 'TABLE' } );
+		} else {
+			clearHighlightedTableCells( highlighted, editor.editing.view );
+		}
+	}, { priority: 'lowest' } ) );
+}
+
+function clearHighlightedTableCells( highlighted, view ) {
+	const previous = [ ...highlighted.values() ];
+
+	view.change( writer => {
+		for ( const previouslyHighlighted of previous ) {
+			writer.removeClass( 'selected', previouslyHighlighted );
+		}
+	} );
+}