-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
tableediting.ts
297 lines (254 loc) · 9.13 KB
/
tableediting.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tableediting
*/
import { Plugin, type Editor } from 'ckeditor5/src/core.js';
import type { PositionOffset, ViewElement, SlotFilter } from 'ckeditor5/src/engine.js';
import upcastTable, { ensureParagraphInTableCell, skipEmptyTableRow, upcastTableFigure } from './converters/upcasttable.js';
import { convertParagraphInTableCell, downcastCell, downcastRow, downcastTable } from './converters/downcast.js';
import InsertTableCommand from './commands/inserttablecommand.js';
import InsertRowCommand from './commands/insertrowcommand.js';
import InsertColumnCommand from './commands/insertcolumncommand.js';
import SplitCellCommand from './commands/splitcellcommand.js';
import MergeCellCommand from './commands/mergecellcommand.js';
import RemoveRowCommand from './commands/removerowcommand.js';
import RemoveColumnCommand from './commands/removecolumncommand.js';
import SetHeaderRowCommand from './commands/setheaderrowcommand.js';
import SetHeaderColumnCommand from './commands/setheadercolumncommand.js';
import MergeCellsCommand from './commands/mergecellscommand.js';
import SelectRowCommand from './commands/selectrowcommand.js';
import SelectColumnCommand from './commands/selectcolumncommand.js';
import TableUtils from '../src/tableutils.js';
import injectTableLayoutPostFixer from './converters/table-layout-post-fixer.js';
import injectTableCellParagraphPostFixer from './converters/table-cell-paragraph-post-fixer.js';
import tableHeadingsRefreshHandler from './converters/table-headings-refresh-handler.js';
import tableCellRefreshHandler from './converters/table-cell-refresh-handler.js';
import '../theme/tableediting.css';
/**
* The table editing feature.
*/
export default class TableEditing extends Plugin {
/**
* Handlers for creating additional slots in the table.
*/
private _additionalSlots: Array<AdditionalSlot>;
/**
* @inheritDoc
*/
public static get pluginName() {
return 'TableEditing' as const;
}
/**
* @inheritDoc
*/
public static get requires() {
return [ TableUtils ] as const;
}
/**
* @inheritDoc
*/
constructor( editor: Editor ) {
super( editor );
this._additionalSlots = [];
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const model = editor.model;
const schema = model.schema;
const conversion = editor.conversion;
const tableUtils = editor.plugins.get( TableUtils );
schema.register( 'table', {
inheritAllFrom: '$blockObject',
allowAttributes: [ 'headingRows', 'headingColumns' ]
} );
schema.register( 'tableRow', {
allowIn: 'table',
isLimit: true
} );
schema.register( 'tableCell', {
allowContentOf: '$container',
allowIn: 'tableRow',
allowAttributes: [ 'colspan', 'rowspan' ],
isLimit: true,
isSelectable: true
} );
// Figure conversion.
conversion.for( 'upcast' ).add( upcastTableFigure() );
// Table conversion.
conversion.for( 'upcast' ).add( upcastTable() );
conversion.for( 'editingDowncast' ).elementToStructure( {
model: {
name: 'table',
attributes: [ 'headingRows' ]
},
view: downcastTable( tableUtils, {
asWidget: true,
additionalSlots: this._additionalSlots
} )
} );
conversion.for( 'dataDowncast' ).elementToStructure( {
model: {
name: 'table',
attributes: [ 'headingRows' ]
},
view: downcastTable( tableUtils, {
additionalSlots: this._additionalSlots
} )
} );
// Table row conversion.
conversion.for( 'upcast' ).elementToElement( { model: 'tableRow', view: 'tr' } );
conversion.for( 'upcast' ).add( skipEmptyTableRow() );
conversion.for( 'downcast' ).elementToElement( {
model: 'tableRow',
view: downcastRow()
} );
// Table cell conversion.
conversion.for( 'upcast' ).elementToElement( { model: 'tableCell', view: 'td' } );
conversion.for( 'upcast' ).elementToElement( { model: 'tableCell', view: 'th' } );
conversion.for( 'upcast' ).add( ensureParagraphInTableCell( 'td' ) );
conversion.for( 'upcast' ).add( ensureParagraphInTableCell( 'th' ) );
conversion.for( 'editingDowncast' ).elementToElement( {
model: 'tableCell',
view: downcastCell( { asWidget: true } )
} );
conversion.for( 'dataDowncast' ).elementToElement( {
model: 'tableCell',
view: downcastCell()
} );
// Duplicates code - needed to properly refresh paragraph inside a table cell.
conversion.for( 'editingDowncast' ).elementToElement( {
model: 'paragraph',
view: convertParagraphInTableCell( { asWidget: true } ),
converterPriority: 'high'
} );
conversion.for( 'dataDowncast' ).elementToElement( {
model: 'paragraph',
view: convertParagraphInTableCell(),
converterPriority: 'high'
} );
// Table attributes conversion.
conversion.for( 'downcast' ).attributeToAttribute( { model: 'colspan', view: 'colspan' } );
conversion.for( 'upcast' ).attributeToAttribute( {
model: { key: 'colspan', value: upcastCellSpan( 'colspan' ) },
view: 'colspan'
} );
conversion.for( 'downcast' ).attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
conversion.for( 'upcast' ).attributeToAttribute( {
model: { key: 'rowspan', value: upcastCellSpan( 'rowspan' ) },
view: 'rowspan'
} );
// Define the config.
editor.config.define( 'table.defaultHeadings.rows', 0 );
editor.config.define( 'table.defaultHeadings.columns', 0 );
// Define all the commands.
editor.commands.add( 'insertTable', new InsertTableCommand( editor ) );
editor.commands.add( 'insertTableRowAbove', new InsertRowCommand( editor, { order: 'above' } ) );
editor.commands.add( 'insertTableRowBelow', new InsertRowCommand( editor, { order: 'below' } ) );
editor.commands.add( 'insertTableColumnLeft', new InsertColumnCommand( editor, { order: 'left' } ) );
editor.commands.add( 'insertTableColumnRight', new InsertColumnCommand( editor, { order: 'right' } ) );
editor.commands.add( 'removeTableRow', new RemoveRowCommand( editor ) );
editor.commands.add( 'removeTableColumn', new RemoveColumnCommand( editor ) );
editor.commands.add( 'splitTableCellVertically', new SplitCellCommand( editor, { direction: 'vertically' } ) );
editor.commands.add( 'splitTableCellHorizontally', new SplitCellCommand( editor, { direction: 'horizontally' } ) );
editor.commands.add( 'mergeTableCells', new MergeCellsCommand( editor ) );
editor.commands.add( 'mergeTableCellRight', new MergeCellCommand( editor, { direction: 'right' } ) );
editor.commands.add( 'mergeTableCellLeft', new MergeCellCommand( editor, { direction: 'left' } ) );
editor.commands.add( 'mergeTableCellDown', new MergeCellCommand( editor, { direction: 'down' } ) );
editor.commands.add( 'mergeTableCellUp', new MergeCellCommand( editor, { direction: 'up' } ) );
editor.commands.add( 'setTableColumnHeader', new SetHeaderColumnCommand( editor ) );
editor.commands.add( 'setTableRowHeader', new SetHeaderRowCommand( editor ) );
editor.commands.add( 'selectTableRow', new SelectRowCommand( editor ) );
editor.commands.add( 'selectTableColumn', new SelectColumnCommand( editor ) );
injectTableLayoutPostFixer( model );
injectTableCellParagraphPostFixer( model );
this.listenTo( model.document, 'change:data', () => {
tableHeadingsRefreshHandler( model, editor.editing );
tableCellRefreshHandler( model, editor.editing );
} );
}
/**
* Registers downcast handler for the additional table slot.
*/
public registerAdditionalSlot( slotHandler: AdditionalSlot ): void {
this._additionalSlots.push( slotHandler );
}
}
/**
* Returns fixed colspan and rowspan attrbutes values.
*
* @param type colspan or rowspan.
* @returns conversion value function.
*/
function upcastCellSpan( type: string ) {
return ( cell: ViewElement ) => {
const span = parseInt( cell.getAttribute( type )! );
if ( Number.isNaN( span ) || span <= 0 ) {
return null;
}
return span;
};
}
/**
* By default, only the `tableRow` elements from the `table` model are downcast inside the `<table>` and
* all other elements are pushed outside the table. This handler allows creating additional slots inside
* the table for other elements.
*
* Take this model as an example:
*
* ```xml
* <table>
* <tableRow>...</tableRow>
* <tableRow>...</tableRow>
* <tableColumnGroup>...</tableColumnGroup>
* </table>
* ```
*
* By default, downcasting result will be:
*
* ```xml
* <table>
* <tbody>
* <tr>...</tr>
* <tr>...</tr>
* </tbody>
* </table>
* <colgroup>...</colgroup>
* ```
*
* To allow the `tableColumnGroup` element at the end of the table, use the following configuration:
*
* ```ts
* const additionalSlot = {
* filter: element => element.is( 'element', 'tableColumnGroup' ),
* positionOffset: 'end'
* }
* ```
*
* Now, the downcast result will be:
*
* ```xml
* <table>
* <tbody>
* <tr>...</tr>
* <tr>...</tr>
* </tbody>
* <colgroup>...</colgroup>
* </table>
* ```
*/
export interface AdditionalSlot {
/**
* Filter for elements that should be placed inside given slot.
*/
filter: SlotFilter;
/**
* Position of the slot within the table.
*/
positionOffset: PositionOffset;
}