This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
converters.js
49 lines (40 loc) · 1.58 KB
/
converters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @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
*/
/**
* Adds a visual highlight style to selected table cells.
*
* @param {module:core/editor/editor~Editor} editor
* @param {module:table/tableselection~TableSelection} tableSelection
*/
export function setupTableSelectionHighlighting( editor, tableSelection ) {
const highlighted = new Set();
editor.conversion.for( 'editingDowncast' ).add( dispatcher => dispatcher.on( 'selection', ( evt, data, conversionApi ) => {
const view = editor.editing.view;
const viewWriter = conversionApi.writer;
const viewSelection = viewWriter.document.selection;
if ( tableSelection.hasMultiCellSelection ) {
clearHighlightedTableCells( highlighted, view );
for ( const tableCell of tableSelection.getSelectedTableCells() ) {
const viewElement = conversionApi.mapper.toViewElement( tableCell );
viewWriter.addClass( 'ck-editor__editable_selected', viewElement );
highlighted.add( viewElement );
}
viewWriter.setSelection( viewSelection.getRanges(), { fake: true, label: 'TABLE' } );
} else {
clearHighlightedTableCells( highlighted, view );
}
}, { priority: 'lowest' } ) );
}
function clearHighlightedTableCells( highlighted, view ) {
const previous = [ ...highlighted.values() ];
view.change( writer => {
for ( const previouslyHighlighted of previous ) {
writer.removeClass( 'ck-editor__editable_selected', previouslyHighlighted );
}
} );
}