@ckeditor/ckeditor5-table 32.0.0 → 34.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE.md +2 -2
  2. package/README.md +2 -1
  3. package/build/table.js +2 -2
  4. package/build/translations/el.js +1 -0
  5. package/build/translations/en-au.js +1 -1
  6. package/build/translations/es.js +1 -1
  7. package/build/translations/hr.js +1 -1
  8. package/build/translations/lv.js +1 -1
  9. package/build/translations/sk.js +1 -1
  10. package/build/translations/ur.js +1 -0
  11. package/ckeditor5-metadata.json +19 -0
  12. package/lang/translations/el.po +261 -0
  13. package/lang/translations/en-au.po +3 -3
  14. package/lang/translations/es.po +32 -32
  15. package/lang/translations/hr.po +3 -3
  16. package/lang/translations/lv.po +40 -40
  17. package/lang/translations/sk.po +3 -3
  18. package/lang/translations/ur.po +261 -0
  19. package/package.json +26 -21
  20. package/src/commands/insertcolumncommand.js +4 -4
  21. package/src/commands/insertrowcommand.js +4 -4
  22. package/src/commands/inserttablecommand.js +1 -5
  23. package/src/commands/mergecellcommand.js +4 -5
  24. package/src/commands/mergecellscommand.js +5 -4
  25. package/src/commands/removecolumncommand.js +8 -7
  26. package/src/commands/removerowcommand.js +5 -6
  27. package/src/commands/selectcolumncommand.js +4 -4
  28. package/src/commands/selectrowcommand.js +5 -5
  29. package/src/commands/setheadercolumncommand.js +5 -4
  30. package/src/commands/setheaderrowcommand.js +7 -4
  31. package/src/commands/splitcellcommand.js +4 -4
  32. package/src/converters/downcast.js +76 -407
  33. package/src/converters/{table-cell-refresh-post-fixer.js → table-cell-refresh-handler.js} +8 -19
  34. package/src/converters/table-headings-refresh-handler.js +68 -0
  35. package/src/index.js +3 -0
  36. package/src/plaintableoutput.js +151 -0
  37. package/src/tablecellproperties/commands/tablecellpropertycommand.js +4 -3
  38. package/src/tableclipboard.js +18 -15
  39. package/src/tablecolumnresize/constants.js +32 -0
  40. package/src/tablecolumnresize/converters.js +126 -0
  41. package/src/tablecolumnresize/tablecolumnresizeediting.js +758 -0
  42. package/src/tablecolumnresize/utils.js +367 -0
  43. package/src/tablecolumnresize.js +36 -0
  44. package/src/tableediting.js +51 -32
  45. package/src/tablekeyboard.js +73 -70
  46. package/src/tablemouse.js +6 -4
  47. package/src/tableselection.js +9 -8
  48. package/src/tableutils.js +310 -0
  49. package/theme/table.css +1 -1
  50. package/theme/tablecolumnresize.css +59 -0
  51. package/src/converters/table-heading-rows-refresh-post-fixer.js +0 -72
  52. package/src/utils/selection.js +0 -276
package/src/index.js CHANGED
@@ -7,6 +7,7 @@
7
7
  * @module table
8
8
  */
9
9
 
10
+ export { default as PlainTableOutput } from './plaintableoutput';
10
11
  export { default as Table } from './table';
11
12
  export { default as TableEditing } from './tableediting';
12
13
  export { default as TableUI } from './tableui';
@@ -25,3 +26,5 @@ export { default as TableMouse } from './tablemouse';
25
26
  export { default as TableKeyboard } from './tablekeyboard';
26
27
  export { default as TableSelection } from './tableselection';
27
28
  export { default as TableUtils } from './tableutils';
29
+ export { default as TableColumnResize } from './tablecolumnresize';
30
+ export { default as TableColumnResizeEditing } from './tablecolumnresize/tablecolumnresizeediting';
@@ -0,0 +1,151 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module table/plaintableoutput
8
+ */
9
+
10
+ import { Plugin } from 'ckeditor5/src/core';
11
+ import Table from './table';
12
+
13
+ /**
14
+ * The plain table output feature.
15
+ *
16
+ * @extends module:core/plugin~Plugin
17
+ */
18
+ export default class PlainTableOutput extends Plugin {
19
+ /**
20
+ * @inheritDoc
21
+ */
22
+ static get pluginName() {
23
+ return 'PlainTableOutput';
24
+ }
25
+
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ static get requires() {
30
+ return [ Table ];
31
+ }
32
+
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ init() {
37
+ const editor = this.editor;
38
+
39
+ // Override default table data downcast converter.
40
+ editor.conversion.for( 'dataDowncast' ).elementToStructure( {
41
+ model: 'table',
42
+ view: downcastTableElement,
43
+ converterPriority: 'high'
44
+ } );
45
+
46
+ // Make sure table <caption> is downcasted into <caption> in the data pipeline when necessary.
47
+ if ( editor.plugins.has( 'TableCaption' ) ) {
48
+ editor.conversion.for( 'dataDowncast' ).elementToElement( {
49
+ model: 'caption',
50
+ view: ( modelElement, { writer } ) => {
51
+ if ( modelElement.parent.name === 'table' ) {
52
+ return writer.createContainerElement( 'caption' );
53
+ }
54
+ },
55
+ converterPriority: 'high'
56
+ } );
57
+ }
58
+
59
+ // Handle border-style, border-color, border-width and background-color table attributes.
60
+ if ( editor.plugins.has( 'TableProperties' ) ) {
61
+ downcastTableBorderAndBackgroundAttributes( editor );
62
+ }
63
+ }
64
+ }
65
+
66
+ // The plain table downcast converter callback.
67
+ //
68
+ // @private
69
+ // @param {module:engine/model/element~Element} Table model element.
70
+ // @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi The conversion API object.
71
+ // @returns {module:engine/view/containerelement~ContainerElement} Created element.
72
+ function downcastTableElement( table, { writer } ) {
73
+ const headingRows = table.getAttribute( 'headingRows' ) || 0;
74
+
75
+ // Table head rows slot.
76
+ const headRowsSlot = writer.createSlot( element =>
77
+ element.is( 'element', 'tableRow' ) && element.index < headingRows
78
+ );
79
+
80
+ // Table body rows slot.
81
+ const bodyRowsSlot = writer.createSlot( element =>
82
+ element.is( 'element', 'tableRow' ) && element.index >= headingRows
83
+ );
84
+
85
+ // Table children slot.
86
+ const childrenSlot = writer.createSlot( element => !element.is( 'element', 'tableRow' ) );
87
+
88
+ // Table <thead> element with all the heading rows.
89
+ const theadElement = writer.createContainerElement( 'thead', null, headRowsSlot );
90
+
91
+ // Table <tbody> element with all the body rows.
92
+ const tbodyElement = writer.createContainerElement( 'tbody', null, bodyRowsSlot );
93
+
94
+ // Table contents element containing <thead> and <tbody> when necessary.
95
+ const tableContentElements = [];
96
+
97
+ if ( headingRows ) {
98
+ tableContentElements.push( theadElement );
99
+ }
100
+
101
+ if ( headingRows < table.childCount ) {
102
+ tableContentElements.push( tbodyElement );
103
+ }
104
+
105
+ // Create table structure.
106
+ //
107
+ // <table>
108
+ // {children-slot-like-caption}
109
+ // <thead>
110
+ // {table-head-rows-slot}
111
+ // </thead>
112
+ // <tbody>
113
+ // {table-body-rows-slot}
114
+ // </tbody>
115
+ // </table>
116
+ return writer.createContainerElement( 'table', null, [ childrenSlot, ...tableContentElements ] );
117
+ }
118
+
119
+ // Register table border and background attributes converters.
120
+ //
121
+ // @private
122
+ // @param {module:core/editor/editor~Editor} editor
123
+ function downcastTableBorderAndBackgroundAttributes( editor ) {
124
+ const modelAttributes = {
125
+ 'border-width': 'tableBorderWidth',
126
+ 'border-color': 'tableBorderColor',
127
+ 'border-style': 'tableBorderStyle',
128
+ 'background-color': 'tableBackgroundColor'
129
+ };
130
+
131
+ for ( const [ styleName, modelAttribute ] of Object.entries( modelAttributes ) ) {
132
+ editor.conversion.for( 'dataDowncast' ).add( dispatcher => {
133
+ return dispatcher.on( `attribute:${ modelAttribute }:table`, ( evt, data, conversionApi ) => {
134
+ const { item, attributeNewValue } = data;
135
+ const { mapper, writer } = conversionApi;
136
+
137
+ if ( !conversionApi.consumable.consume( item, evt.name ) ) {
138
+ return;
139
+ }
140
+
141
+ const table = mapper.toViewElement( item );
142
+
143
+ if ( attributeNewValue ) {
144
+ writer.setStyle( styleName, attributeNewValue, table );
145
+ } else {
146
+ writer.removeStyle( styleName, table );
147
+ }
148
+ }, { priority: 'high' } );
149
+ } );
150
+ }
151
+ }
@@ -8,7 +8,6 @@
8
8
  */
9
9
 
10
10
  import { Command } from 'ckeditor5/src/core';
11
- import { getSelectionAffectedTableCells } from '../../utils/selection';
12
11
 
13
12
  /**
14
13
  * The table cell attribute command.
@@ -51,7 +50,8 @@ export default class TableCellPropertyCommand extends Command {
51
50
  */
52
51
  refresh() {
53
52
  const editor = this.editor;
54
- const selectedTableCells = getSelectionAffectedTableCells( editor.model.document.selection );
53
+ const tableUtils = this.editor.plugins.get( 'TableUtils' );
54
+ const selectedTableCells = tableUtils.getSelectionAffectedTableCells( editor.model.document.selection );
55
55
 
56
56
  this.isEnabled = !!selectedTableCells.length;
57
57
  this.value = this._getSingleValue( selectedTableCells );
@@ -70,7 +70,8 @@ export default class TableCellPropertyCommand extends Command {
70
70
  execute( options = {} ) {
71
71
  const { value, batch } = options;
72
72
  const model = this.editor.model;
73
- const tableCells = getSelectionAffectedTableCells( model.document.selection );
73
+ const tableUtils = this.editor.plugins.get( 'TableUtils' );
74
+ const tableCells = tableUtils.getSelectionAffectedTableCells( model.document.selection );
74
75
  const valueToSet = this._getValueToSet( value );
75
76
 
76
77
  model.enqueueChange( batch, writer => {
@@ -12,7 +12,6 @@ import { Plugin } from 'ckeditor5/src/core';
12
12
  import TableSelection from './tableselection';
13
13
  import TableWalker from './tablewalker';
14
14
  import TableUtils from './tableutils';
15
- import { getColumnIndexes, getRowIndexes, getSelectionAffectedTableCells, isSelectionRectangular, sortRanges } from './utils/selection';
16
15
  import {
17
16
  cropTableToDimensions,
18
17
  getHorizontallyOverlappingCells,
@@ -122,7 +121,7 @@ export default class TableClipboard extends Plugin {
122
121
  return;
123
122
  }
124
123
 
125
- const selectedTableCells = getSelectionAffectedTableCells( model.document.selection );
124
+ const selectedTableCells = tableUtils.getSelectionAffectedTableCells( model.document.selection );
126
125
 
127
126
  if ( !selectedTableCells.length ) {
128
127
  removeEmptyRowsColumns( pastedTable, tableUtils );
@@ -171,7 +170,7 @@ export default class TableClipboard extends Plugin {
171
170
  if ( this.editor.plugins.get( 'TableSelection' ).isEnabled ) {
172
171
  // Selection ranges must be sorted because the first and last selection ranges are considered
173
172
  // as anchor/focus cell ranges for multi-cell selection.
174
- const selectionRanges = sortRanges( cellsToSelect.map( cell => writer.createRangeOn( cell ) ) );
173
+ const selectionRanges = tableUtils.sortRanges( cellsToSelect.map( cell => writer.createRangeOn( cell ) ) );
175
174
 
176
175
  writer.setSelection( selectionRanges );
177
176
  } else {
@@ -311,17 +310,21 @@ export default class TableClipboard extends Plugin {
311
310
 
312
311
  return cellToInsert;
313
312
  }
313
+
314
+ /**
315
+ * Extracts the table for pasting into a table.
316
+ *
317
+ * @protected
318
+ * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
319
+ * @param {module:engine/model/model~Model} model The editor model.
320
+ * @returns {module:engine/model/element~Element|null}
321
+ */
322
+ getTableIfOnlyTableInContent( content, model ) {
323
+ return getTableIfOnlyTableInContent( content, model );
324
+ }
314
325
  }
315
326
 
316
- /**
317
- * Extract table for pasting into table.
318
- *
319
- * @private
320
- * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
321
- * @param {module:engine/model/model~Model} model The editor model.
322
- * @returns {module:engine/model/element~Element|null}
323
- */
324
- export function getTableIfOnlyTableInContent( content, model ) {
327
+ function getTableIfOnlyTableInContent( content, model ) {
325
328
  if ( !content.is( 'documentFragment' ) && !content.is( 'element' ) ) {
326
329
  return null;
327
330
  }
@@ -381,8 +384,8 @@ export function getTableIfOnlyTableInContent( content, model ) {
381
384
  function prepareTableForPasting( selectedTableCells, pastedDimensions, writer, tableUtils ) {
382
385
  const selectedTable = selectedTableCells[ 0 ].findAncestor( 'table' );
383
386
 
384
- const columnIndexes = getColumnIndexes( selectedTableCells );
385
- const rowIndexes = getRowIndexes( selectedTableCells );
387
+ const columnIndexes = tableUtils.getColumnIndexes( selectedTableCells );
388
+ const rowIndexes = tableUtils.getRowIndexes( selectedTableCells );
386
389
 
387
390
  const selection = {
388
391
  firstColumn: columnIndexes.first,
@@ -403,7 +406,7 @@ function prepareTableForPasting( selectedTableCells, pastedDimensions, writer, t
403
406
 
404
407
  // In case of expanding selection we do not reset the selection so in this case we will always try to fix selection
405
408
  // like in the case of a non-rectangular area. This might be fixed by re-setting selected cells array but this shortcut is safe.
406
- if ( shouldExpandSelection || !isSelectionRectangular( selectedTableCells, tableUtils ) ) {
409
+ if ( shouldExpandSelection || !tableUtils.isSelectionRectangular( selectedTableCells ) ) {
407
410
  // For a non-rectangular selection (ie in which some cells sticks out from a virtual selection rectangle) we need to create
408
411
  // a table layout that has a rectangular selection. This will split cells so the selection become rectangular.
409
412
  // Beyond this point we will operate on fixed content table.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module table/tablecolumnresize/constants
8
+ */
9
+
10
+ /* istanbul ignore file */
11
+
12
+ /**
13
+ * The minimum column width given as a percentage value. Used in situations when the table is not yet rendered, so it is impossible to
14
+ * calculate how many percentage of the table width would be {@link ~COLUMN_MIN_WIDTH_IN_PIXELS minimum column width in pixels}.
15
+ *
16
+ * @const {Number}
17
+ */
18
+ export const COLUMN_MIN_WIDTH_AS_PERCENTAGE = 5;
19
+
20
+ /**
21
+ * The minimum column width in pixels when the maximum table width is known.
22
+ *
23
+ * @const {Number}
24
+ */
25
+ export const COLUMN_MIN_WIDTH_IN_PIXELS = 40;
26
+
27
+ /**
28
+ * Determines how many digits after the decimal point are used to store the column width as a percentage value.
29
+ *
30
+ * @const {Number}
31
+ */
32
+ export const COLUMN_WIDTH_PRECISION = 2;
@@ -0,0 +1,126 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module table/tablecolumnresize/converters
8
+ */
9
+
10
+ /* istanbul ignore file */
11
+
12
+ import { getNumberOfColumn } from './utils';
13
+
14
+ /**
15
+ * Returns a helper for converting a view `<colgroup>` and `<col>` elements to the model table `columnWidths` attribute.
16
+ *
17
+ * Only the inline width, provided as a percentage value, in the `<col>` element is taken into account. If there are not enough `<col>`
18
+ * elements matching this condition, the special value 'auto' is returned. It indicates that the width of a column will be automatically
19
+ * calculated in the
20
+ * {@link module:table/tablecolumnresize/tablecolumnresizeediting~TableColumnResizeEditing#_setupPostFixer post-fixer}, depending
21
+ * on the available table space.
22
+ *
23
+ * @param {module:core/editor/editor~Editor} editor The editor instance.
24
+ * @returns {Function} Conversion helper.
25
+ */
26
+ export function upcastColgroupElement( editor ) {
27
+ return dispatcher => dispatcher.on( 'element:colgroup', ( evt, data, conversionApi ) => {
28
+ const modelTable = data.modelCursor.findAncestor( 'table' );
29
+
30
+ if ( !modelTable ) {
31
+ return;
32
+ }
33
+
34
+ const modelWriter = conversionApi.writer;
35
+ const viewColgroupElement = data.viewItem;
36
+ const numberOfColumns = getNumberOfColumn( modelTable, editor );
37
+
38
+ const columnWidthsAttribute = [ ...Array( numberOfColumns ).keys() ]
39
+ .map( columnIndex => {
40
+ const viewChild = viewColgroupElement.getChild( columnIndex );
41
+
42
+ if ( !viewChild || !viewChild.is( 'element', 'col' ) ) {
43
+ return 'auto';
44
+ }
45
+
46
+ const viewColWidth = viewChild.getStyle( 'width' );
47
+
48
+ if ( !viewColWidth || !viewColWidth.endsWith( '%' ) ) {
49
+ return 'auto';
50
+ }
51
+
52
+ return viewColWidth;
53
+ } )
54
+ .join( ',' );
55
+
56
+ modelWriter.setAttribute( 'columnWidths', columnWidthsAttribute, modelTable );
57
+ } );
58
+ }
59
+
60
+ /**
61
+ * Returns a helper for converting a model table `columnWidths` attribute to view `<colgroup>` and `<col>` elements.
62
+ *
63
+ * @returns {Function} Conversion helper.
64
+ */
65
+ export function downcastTableColumnWidthsAttribute() {
66
+ return dispatcher => dispatcher.on( 'attribute:columnWidths:table', ( evt, data, conversionApi ) => {
67
+ const viewWriter = conversionApi.writer;
68
+ const modelTable = data.item;
69
+
70
+ const viewTable = [ ...conversionApi.mapper.toViewElement( modelTable ).getChildren() ]
71
+ .find( viewChild => viewChild.is( 'element', 'table' ) );
72
+
73
+ if ( data.attributeNewValue ) {
74
+ if ( data.attributeNewValue !== data.attributeOldValue ) {
75
+ insertColgroupElement( viewWriter, viewTable, data.attributeNewValue );
76
+ }
77
+ } else {
78
+ removeColgroupElement( viewWriter, viewTable );
79
+ }
80
+ } );
81
+ }
82
+
83
+ // Inserts the `<colgroup>` with `<col>` elements as the first child in the view table. Each `<col>` element represents a single column
84
+ // and it has the inline width style set, taken from the appropriate slot from the `columnWidths` table attribute.
85
+ //
86
+ // @private
87
+ // @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter View writer instance.
88
+ // @param {module:engine/view/element~Element} viewTable View table.
89
+ // @param {String} columnWidthsAttribute Column width attribute from model table.
90
+ function insertColgroupElement( viewWriter, viewTable, columnWidthsAttribute ) {
91
+ const columnWidths = columnWidthsAttribute.split( ',' );
92
+
93
+ let viewColgroupElement = [ ...viewTable.getChildren() ].find( viewElement => viewElement.is( 'element', 'colgroup' ) );
94
+
95
+ if ( !viewColgroupElement ) {
96
+ viewColgroupElement = viewWriter.createContainerElement( 'colgroup' );
97
+ }
98
+
99
+ for ( const viewChild of [ ...viewColgroupElement.getChildren() ] ) {
100
+ viewWriter.remove( viewChild );
101
+ }
102
+
103
+ for ( const columnIndex of Array( columnWidths.length ).keys() ) {
104
+ const viewColElement = viewWriter.createEmptyElement( 'col' );
105
+
106
+ viewWriter.setStyle( 'width', columnWidths[ columnIndex ], viewColElement );
107
+ viewWriter.insert( viewWriter.createPositionAt( viewColgroupElement, 'end' ), viewColElement );
108
+ }
109
+
110
+ viewWriter.insert( viewWriter.createPositionAt( viewTable, 'start' ), viewColgroupElement );
111
+ }
112
+
113
+ // Removes the `<colgroup>` with `<col>` elements from the view table.
114
+ //
115
+ // @private
116
+ // @param {module:engine/view/downcastwriter~DowncastWriter} viewWriter View writer instance.
117
+ // @param {module:engine/view/element~Element} viewTable View table.
118
+ function removeColgroupElement( viewWriter, viewTable ) {
119
+ const viewColgroupElement = [ ...viewTable.getChildren() ].find( viewElement => viewElement.is( 'element', 'colgroup' ) );
120
+
121
+ if ( !viewColgroupElement ) {
122
+ return;
123
+ }
124
+
125
+ viewWriter.remove( viewColgroupElement );
126
+ }