@contrail/document-table 1.0.5 → 1.0.12

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.
package/lib/index.d.ts CHANGED
@@ -3,5 +3,6 @@ export * from './table-constants';
3
3
  export * from './table-copy.service';
4
4
  export * from './table-merge.service';
5
5
  export * from './table-paste.service';
6
+ export * from './table-rotate.service';
6
7
  export * from './table.service';
7
8
  export * from './table-range';
package/lib/index.js CHANGED
@@ -19,5 +19,6 @@ __exportStar(require("./table-constants"), exports);
19
19
  __exportStar(require("./table-copy.service"), exports);
20
20
  __exportStar(require("./table-merge.service"), exports);
21
21
  __exportStar(require("./table-paste.service"), exports);
22
+ __exportStar(require("./table-rotate.service"), exports);
22
23
  __exportStar(require("./table.service"), exports);
23
24
  __exportStar(require("./table-range"), exports);
@@ -98,6 +98,7 @@ class TableCopyService {
98
98
  }
99
99
  newTableElement.rowIds = rowIds;
100
100
  newTableElement.columnIds = columnIds;
101
+ console.log(newChildElements);
101
102
  return [newTableElement, ...newChildElements];
102
103
  }
103
104
  static createTableFromCells(element, rows, columns, cells) {
@@ -189,6 +190,7 @@ class TableCopyService {
189
190
  },
190
191
  style: util_1.ObjectUtil.cloneDeep(cell.style),
191
192
  position: { x: 0, y: 0 },
193
+ rotate: Object.assign({}, cell.rotate),
192
194
  });
193
195
  cellElements.push(element);
194
196
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TableMergeService = void 0;
4
+ const documents_1 = require("@contrail/documents");
4
5
  const util_1 = require("@contrail/util");
5
6
  const table_service_1 = require("./table.service");
6
7
  const table_range_1 = require("./table-range");
@@ -66,23 +67,42 @@ class TableMergeService {
66
67
  element.rowspan = 1;
67
68
  if (tableRange.isStart(rowIndex, columnIndex)) {
68
69
  firstCell = element;
69
- let width = 0;
70
- tableRange.eachColumn((columnIndex) => {
71
- const columnId = tableElement.columnIds[columnIndex];
72
- const column = childElements.find((e) => e.type === 'column' && e.id === columnId);
73
- width = width + column.size.width;
74
- });
75
- if (colspan > 1) {
76
- element.colspan = colspan;
70
+ if (table_service_1.TableService.isVerticalCell(element)) {
71
+ let height = 0;
72
+ tableRange.eachRow((rowIndex) => {
73
+ const rowId = tableElement.rowIds[rowIndex];
74
+ const row = childElements.find((e) => e.type === 'row' && e.id === rowId);
75
+ height = height + row.size.height;
76
+ });
77
+ if (colspan > 1) {
78
+ element.colspan = colspan;
79
+ }
80
+ if (rowspan > 1) {
81
+ element.rowspan = rowspan;
82
+ }
83
+ element.size.height = height;
84
+ element.size.width = table_service_1.TableService.getCellSize(element).height;
77
85
  }
78
- if (rowspan > 1) {
79
- element.rowspan = rowspan;
86
+ else {
87
+ let width = 0;
88
+ tableRange.eachColumn((columnIndex) => {
89
+ const columnId = tableElement.columnIds[columnIndex];
90
+ const column = childElements.find((e) => e.type === 'column' && e.id === columnId);
91
+ width = width + column.size.width;
92
+ });
93
+ if (colspan > 1) {
94
+ element.colspan = colspan;
95
+ }
96
+ if (rowspan > 1) {
97
+ element.rowspan = rowspan;
98
+ }
99
+ element.size.width = width;
100
+ element.size.height = table_service_1.TableService.getCellSize(element).height;
80
101
  }
81
- element.size.width = width;
82
- element.size.height = table_service_1.TableService.getCellSize(element).height;
83
102
  }
84
103
  else {
85
104
  element.style = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(table_service_1.TableService.DEFAULT_CELL_STYLE), firstCell.style);
105
+ element.rotate = Object.assign({}, firstCell.rotate);
86
106
  table_service_1.TableService.clearCell(element);
87
107
  }
88
108
  elementsToUpdate.push({ change: element, undo: elementUndo });
@@ -100,6 +120,8 @@ class TableMergeService {
100
120
  throw new Error(table_service_1.TABLE_ERROR.INVALID_RANGE_TO_UNMERGE);
101
121
  const tableUndo = Object.assign(Object.assign({}, (0, table_service_1.tableData)(tableElement)), { size: Object.assign({}, tableElement.size) });
102
122
  const elementsToUpdate = [];
123
+ const columnWidthsToUpdate = new Map();
124
+ const rowHeightsToUpdate = new Map();
103
125
  let firstCell;
104
126
  tableRange.each((rowIndex, columnIndex) => {
105
127
  const rowId = tableElement.rowIds[rowIndex];
@@ -113,10 +135,23 @@ class TableMergeService {
113
135
  element.rowspan = 1;
114
136
  if (tableRange.isStart(rowIndex, columnIndex)) {
115
137
  firstCell = element;
116
- element.size.width = column.size.width;
117
- element.size.height = table_service_1.TableService.getCellSize(element).height;
118
- if (element.size.height > row.size.height) {
119
- elementsToUpdate.push(table_service_1.TableService.resizeRow(tableElement, childElements, rowId, element.size.height).elementsToUpdate[1]);
138
+ if (table_service_1.TableService.isVerticalCell(element)) {
139
+ element.size.height = row.size.height;
140
+ element.size.width = table_service_1.TableService.getCellSize(element).height;
141
+ if (element.size.width > column.size.width) {
142
+ if (!columnWidthsToUpdate.has(column.id) || columnWidthsToUpdate.get(column.id) < element.size.width) {
143
+ columnWidthsToUpdate.set(column.id, element.size.width);
144
+ }
145
+ }
146
+ }
147
+ else {
148
+ element.size.width = column.size.width;
149
+ element.size.height = table_service_1.TableService.getCellSize(element).height;
150
+ if (element.size.height > row.size.height) {
151
+ if (!rowHeightsToUpdate.has(row.id) || rowHeightsToUpdate.get(row.id) < element.size.height) {
152
+ rowHeightsToUpdate.set(row.id, element.size.height);
153
+ }
154
+ }
120
155
  }
121
156
  }
122
157
  else {
@@ -129,6 +164,20 @@ class TableMergeService {
129
164
  throw new Error();
130
165
  }
131
166
  });
167
+ for (const [columnId, width] of columnWidthsToUpdate) {
168
+ const updatedColumn = table_service_1.TableService.resizeColumn(tableElement, childElements, columnId, width).elementsToUpdate[1];
169
+ const updatedRowAndCells = table_service_1.TableService.autoFitRows(tableElement, childElements, columnId).elementsToUpdate;
170
+ const uniqueUpdatedRowAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedRowAndCells, elementsToUpdate);
171
+ elementsToUpdate.push(updatedColumn);
172
+ elementsToUpdate.push(...uniqueUpdatedRowAndCells);
173
+ }
174
+ for (const [rowId, height] of rowHeightsToUpdate) {
175
+ const updatedRow = table_service_1.TableService.resizeRow(tableElement, childElements, rowId, height).elementsToUpdate[1];
176
+ const updatedColumnAndCells = table_service_1.TableService.autoFitColumns(tableElement, childElements, rowId).elementsToUpdate;
177
+ const uniqueUpdatedColumnAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedColumnAndCells, elementsToUpdate);
178
+ elementsToUpdate.push(updatedRow);
179
+ elementsToUpdate.push(...uniqueUpdatedColumnAndCells);
180
+ }
132
181
  return {
133
182
  elementsToUpdate: [
134
183
  {
@@ -24,6 +24,8 @@ class TablePasteService {
24
24
  const existingCells = childElements.filter((e) => e.type === 'cell');
25
25
  const elementsToCreate = [];
26
26
  const elementsToUpdate = [];
27
+ const columnWidthsToUpdate = new Map();
28
+ const rowHeightsToUpdate = new Map();
27
29
  let existingMergedRanges = table_merge_service_1.TableMergeService.getMergedRanges(tableElement, childElements);
28
30
  for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
29
31
  const rowToPaste = rowsToPaste[rowIndex - targetRow];
@@ -38,10 +40,9 @@ class TablePasteService {
38
40
  existingRow = newRow;
39
41
  this.log('added row', rowIndex, existingRowId, existingRow);
40
42
  }
41
- if (rowToPaste.size.height > existingRow.size.height) {
42
- this.log('resized row', rowIndex, existingRowId, existingRow);
43
- elementsToUpdate.push(table_service_1.TableService.resizeRow(tableElement, childElements, existingRowId, rowToPaste.size.height)
44
- .elementsToUpdate[1]);
43
+ if (rowToPaste.size.height > existingRow.size.height &&
44
+ (!rowHeightsToUpdate.has(existingRowId) || rowHeightsToUpdate.get(existingRowId) < rowToPaste.size.height)) {
45
+ rowHeightsToUpdate.set(existingRowId, rowToPaste.size.height);
45
46
  }
46
47
  for (let columnIndex = startColumn; columnIndex <= endColumn; columnIndex++) {
47
48
  const columnToPaste = columnsToPaste[columnIndex - targetColumn];
@@ -56,14 +57,10 @@ class TablePasteService {
56
57
  existingColumn = newColumn;
57
58
  this.log('added column', columnIndex, existingColumnId, existingColumn);
58
59
  }
59
- if (columnToPaste.size.width > existingColumn.size.width) {
60
- const updatedColumn = table_service_1.TableService.resizeColumn(tableElement, childElements, existingColumnId, columnToPaste.size.width).elementsToUpdate[1];
61
- const updatedRowAndCells = table_service_1.TableService.autoFitRows(tableElement, childElements, existingColumnId).elementsToUpdate;
62
- const uniqueUpdatedRowAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedRowAndCells, elementsToUpdate);
63
- elementsToUpdate.push(updatedColumn);
64
- elementsToUpdate.push(...uniqueUpdatedRowAndCells);
65
- this.log('resized column', columnIndex, existingColumnId, existingColumn);
66
- this.log('resized column cells and row', uniqueUpdatedRowAndCells, uniqueUpdatedRowAndCells);
60
+ if (columnToPaste.size.width > existingColumn.size.width &&
61
+ (!columnWidthsToUpdate.has(existingColumnId) ||
62
+ columnWidthsToUpdate.get(existingColumnId) < columnToPaste.size.width)) {
63
+ columnWidthsToUpdate.set(existingColumnId, columnToPaste.size.width);
67
64
  }
68
65
  const cellToPaste = cellsToPaste.find((e) => e.rowId === rowToPaste.id && e.columnId === columnToPaste.id);
69
66
  if (!cellToPaste)
@@ -82,6 +79,20 @@ class TablePasteService {
82
79
  }
83
80
  }
84
81
  }
82
+ for (const [columnId, width] of columnWidthsToUpdate) {
83
+ const updatedColumn = table_service_1.TableService.resizeColumn(tableElement, childElements, columnId, width).elementsToUpdate[1];
84
+ const updatedRowAndCells = table_service_1.TableService.autoFitRows(tableElement, childElements, columnId).elementsToUpdate;
85
+ const uniqueUpdatedRowAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedRowAndCells, elementsToUpdate);
86
+ elementsToUpdate.push(updatedColumn);
87
+ elementsToUpdate.push(...uniqueUpdatedRowAndCells);
88
+ }
89
+ for (const [rowId, height] of rowHeightsToUpdate) {
90
+ const updatedRow = table_service_1.TableService.resizeRow(tableElement, childElements, rowId, height).elementsToUpdate[1];
91
+ const updatedColumnAndCells = table_service_1.TableService.autoFitColumns(tableElement, childElements, rowId).elementsToUpdate;
92
+ const uniqueUpdatedColumnAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedColumnAndCells, elementsToUpdate);
93
+ elementsToUpdate.push(updatedRow);
94
+ elementsToUpdate.push(...uniqueUpdatedColumnAndCells);
95
+ }
85
96
  return { elementsToCreate, elementsToUpdate };
86
97
  }
87
98
  static pasteTableCells(tableElement, childElements, elementsToPaste, targetRow, targetColumn) {
@@ -89,6 +100,7 @@ class TablePasteService {
89
100
  const rowsToPaste = elementsToPaste.filter((e) => e.type === 'row');
90
101
  const columnsToPaste = elementsToPaste.filter((e) => e.type === 'column');
91
102
  const cellsToPaste = elementsToPaste.filter((e) => e.type === 'cell');
103
+ this.log(elementsToPaste);
92
104
  const startRow = targetRow;
93
105
  const endRow = startRow + tableToPaste.rowIds.length - 1;
94
106
  const startColumn = targetColumn;
@@ -99,6 +111,8 @@ class TablePasteService {
99
111
  for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
100
112
  const rowToPaste = rowsToPaste[rowIndex - targetRow];
101
113
  const existingRowId = tableElement.rowIds[rowIndex];
114
+ const existingRow = childElements.find((e) => e.type === 'row' && e.id === existingRowId) ||
115
+ elementsToCreate.find((e) => e.type === 'row' && e.id === existingRowId);
102
116
  for (let columnIndex = startColumn; columnIndex <= endColumn; columnIndex++) {
103
117
  const columnToPaste = columnsToPaste[columnIndex - targetColumn];
104
118
  const existingColumnId = tableElement.columnIds[columnIndex];
@@ -117,11 +131,21 @@ class TablePasteService {
117
131
  cell.style = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(table_service_1.TableService.DEFAULT_CELL_STYLE), util_1.ObjectUtil.cloneDeep(cellToPaste.style));
118
132
  cell.colspan = cellToPaste.colspan > 1 ? cellToPaste.colspan : 1;
119
133
  cell.rowspan = cellToPaste.rowspan > 1 ? cellToPaste.rowspan : 1;
134
+ if (cellToPaste.rotate)
135
+ cell.rotate = Object.assign({}, cellToPaste.rotate);
120
136
  const cellMergedRanged = table_merge_service_1.TableMergeService.getMergedRange(tableElement, cell);
121
- cell.size.width = cellMergedRanged
122
- ? table_service_1.TableService.getColumnsWidth(tableElement, [...childElements, ...elementsToCreate], cellMergedRanged.startColumn, cellMergedRanged.endColumn)
123
- : existingColumn.size.width;
124
- cell.size.height = table_service_1.TableService.getCellSize(cell).height;
137
+ if (table_service_1.TableService.isVerticalCell(cell)) {
138
+ cell.size.height = cellMergedRanged
139
+ ? table_service_1.TableService.getRowsHeight(tableElement, [...childElements, ...elementsToCreate], cellMergedRanged.startRow, cellMergedRanged.endRow)
140
+ : existingRow.size.height;
141
+ cell.size.width = table_service_1.TableService.getCellSize(cell).height;
142
+ }
143
+ else {
144
+ cell.size.width = cellMergedRanged
145
+ ? table_service_1.TableService.getColumnsWidth(tableElement, [...childElements, ...elementsToCreate], cellMergedRanged.startColumn, cellMergedRanged.endColumn)
146
+ : existingColumn.size.width;
147
+ cell.size.height = table_service_1.TableService.getCellSize(cell).height;
148
+ }
125
149
  if (!createdCell && !elementsToUpdate.find(({ change }) => change.id === cell.id)) {
126
150
  cellUndo.colspan = cellUndo.colspan > 1 ? cellUndo.colspan : 1;
127
151
  cellUndo.rowspan = cellUndo.rowspan > 1 ? cellUndo.rowspan : 1;
@@ -0,0 +1,4 @@
1
+ import { DocumentElement, DocumentElementChanges } from '@contrail/documents';
2
+ export declare class TableRotateService {
3
+ static rotateCells(tableElement: DocumentElement, childElements: DocumentElement[], cells: DocumentElement[], angle: number): DocumentElementChanges;
4
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableRotateService = void 0;
4
+ const documents_1 = require("@contrail/documents");
5
+ const table_service_1 = require("./table.service");
6
+ const util_1 = require("@contrail/util");
7
+ class TableRotateService {
8
+ static rotateCells(tableElement, childElements, cells, angle) {
9
+ const elementsToUpdate = [];
10
+ const columnWidthsToUpdate = new Map();
11
+ const rowHeightsToUpdate = new Map();
12
+ for (const cell of cells) {
13
+ const row = childElements.find((e) => e.type === 'row' && e.id === cell.rowId);
14
+ const column = childElements.find((e) => e.type === 'column' && e.id === cell.columnId);
15
+ if (!row || !column)
16
+ continue;
17
+ const cellUndo = util_1.ObjectUtil.cloneDeep(cell);
18
+ cell.rotate = { angle };
19
+ if (angle === 270) {
20
+ cellUndo.rotate = cellUndo.rotate || { angle: 0 };
21
+ cell.size.height = row.size.height;
22
+ cell.size.width = table_service_1.TableService.getCellSize(cell).height;
23
+ if (cell.size.width > column.size.width) {
24
+ if (!columnWidthsToUpdate.has(column.id) || columnWidthsToUpdate.get(column.id) < cell.size.width) {
25
+ columnWidthsToUpdate.set(column.id, cell.size.width);
26
+ }
27
+ }
28
+ }
29
+ else {
30
+ cellUndo.rotate = cellUndo.rotate || { angle: 270 };
31
+ cell.size.width = column.size.width;
32
+ cell.size.height = table_service_1.TableService.getCellSize(cell).height;
33
+ if (cell.size.height > row.size.height) {
34
+ if (!rowHeightsToUpdate.has(row.id) || rowHeightsToUpdate.get(row.id) < cell.size.height) {
35
+ rowHeightsToUpdate.set(row.id, cell.size.height);
36
+ }
37
+ }
38
+ }
39
+ elementsToUpdate.push({ change: cell, undo: cellUndo });
40
+ }
41
+ for (const [columnId, width] of columnWidthsToUpdate) {
42
+ const updatedColumn = table_service_1.TableService.resizeColumn(tableElement, childElements, columnId, width).elementsToUpdate[1];
43
+ const updatedRowAndCells = table_service_1.TableService.autoFitRows(tableElement, childElements, columnId).elementsToUpdate;
44
+ const uniqueUpdatedRowAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedRowAndCells, elementsToUpdate);
45
+ elementsToUpdate.push(updatedColumn);
46
+ elementsToUpdate.push(...uniqueUpdatedRowAndCells);
47
+ }
48
+ for (const [rowId, height] of rowHeightsToUpdate) {
49
+ const updatedRow = table_service_1.TableService.resizeRow(tableElement, childElements, rowId, height).elementsToUpdate[1];
50
+ const updatedColumnAndCells = table_service_1.TableService.autoFitColumns(tableElement, childElements, rowId).elementsToUpdate;
51
+ const uniqueUpdatedColumnAndCells = (0, documents_1.uniqueElementsToUpdate)(updatedColumnAndCells, elementsToUpdate);
52
+ elementsToUpdate.push(updatedRow);
53
+ elementsToUpdate.push(...uniqueUpdatedColumnAndCells);
54
+ }
55
+ return { elementsToUpdate };
56
+ }
57
+ }
58
+ exports.TableRotateService = TableRotateService;
@@ -31,14 +31,17 @@ export declare class TableService {
31
31
  static resizeRow(tableElement: DocumentElement, childElements: DocumentElement[], rowId: string, height: number): DocumentElementChanges;
32
32
  static resizeColumn(tableElement: DocumentElement, childElements: DocumentElement[], columnId: string, width: number): DocumentElementChanges;
33
33
  private static getColumns;
34
- static getColumnsWidth(tableElement: DocumentElement, childElements: DocumentElement[], startIndex: number, endIndex: number): number;
34
+ static getColumnsWidth(tableElement: DocumentElement, childElements: DocumentElement[], startIndex: number, endIndex: number, skipIndex?: number): number;
35
35
  private static getRows;
36
- private static getRowsHeight;
36
+ static getRowsHeight(tableElement: DocumentElement, childElements: DocumentElement[], startIndex: number, endIndex: number, skipIndex?: number): number;
37
37
  private static getCellMinHeight;
38
+ private static getCellMinWidth;
38
39
  static autoFitRows(tableElement: DocumentElement, childElements: DocumentElement[], columnId: string): DocumentElementChanges;
40
+ static autoFitColumns(tableElement: DocumentElement, childElements: DocumentElement[], rowId: string): DocumentElementChanges;
39
41
  static clearCell(element: DocumentElement): void;
40
42
  static getCellSize(element: DocumentElement, text?: string): SizeDefinition;
41
43
  static getSize(text: string, style: string): SizeDefinition;
42
44
  static getTableChildElements(tableId: string, elements: DocumentElement[]): DocumentElement[];
43
45
  static updateCellText(tableElement: DocumentElement, childElements: DocumentElement[], rowIndex: number, columnIndex: number, text: string): DocumentElementChanges;
46
+ static isVerticalCell(element: DocumentElement): boolean;
44
47
  }
@@ -486,12 +486,12 @@ class TableService {
486
486
  }
487
487
  return columns;
488
488
  }
489
- static getColumnsWidth(tableElement, childElements, startIndex, endIndex) {
489
+ static getColumnsWidth(tableElement, childElements, startIndex, endIndex, skipIndex) {
490
490
  const columns = this.getColumns(tableElement, childElements);
491
491
  let width = 0;
492
492
  for (let i = startIndex; i <= endIndex; i++) {
493
493
  const column = columns[i];
494
- if (column) {
494
+ if (column && (skipIndex == null || skipIndex !== i)) {
495
495
  width = width + column.size.width;
496
496
  }
497
497
  }
@@ -519,6 +519,8 @@ class TableService {
519
519
  return height;
520
520
  }
521
521
  static getCellMinHeight(cell, tableElement, childElements) {
522
+ if (TableService.isVerticalCell(cell))
523
+ return table_constants_1.TABLE_ROW_MIN_HEIGHT;
522
524
  let height = cell.size.height;
523
525
  const rowIndex = tableElement.rowIds.indexOf(cell.rowId);
524
526
  const columnIndex = tableElement.columnIds.indexOf(cell.columnId);
@@ -540,6 +542,30 @@ class TableService {
540
542
  }
541
543
  return height;
542
544
  }
545
+ static getCellMinWidth(cell, tableElement, childElements) {
546
+ if (!TableService.isVerticalCell(cell))
547
+ return table_constants_1.TABLE_COLUMN_MIN_WIDTH;
548
+ let width = cell.size.width;
549
+ const rowIndex = tableElement.rowIds.indexOf(cell.rowId);
550
+ const columnIndex = tableElement.columnIds.indexOf(cell.columnId);
551
+ if (rowIndex < 0 || columnIndex < 0)
552
+ throw new Error(TABLE_ERROR.INVALID_TABLE);
553
+ const cellRange = new table_range_1.TableRange(rowIndex, rowIndex, columnIndex, columnIndex);
554
+ const mergedRanges = table_merge_service_1.TableMergeService.getMergedRanges(tableElement, childElements);
555
+ const mergedRange = mergedRanges.find((range) => cellRange.within(range));
556
+ if (mergedRange) {
557
+ const mergedRangeStart = mergedRange && cellRange.equals(mergedRange.start());
558
+ const mergedCell = mergedRangeStart
559
+ ? cell
560
+ : childElements.find((e) => e.type === 'cell' &&
561
+ e.rowId === tableElement.rowIds[mergedRange.startRow] &&
562
+ e.columnId === tableElement.columnIds[mergedRange.startColumn]);
563
+ width =
564
+ mergedCell.size.width -
565
+ TableService.getColumnsWidth(tableElement, childElements, mergedRange.startColumn, mergedRange.endColumn, columnIndex);
566
+ }
567
+ return width;
568
+ }
543
569
  static autoFitRows(tableElement, childElements, columnId) {
544
570
  var _a;
545
571
  const elementsToUpdate = [];
@@ -549,7 +575,7 @@ class TableService {
549
575
  for (const [rowIndex, rowId] of tableElement.rowIds.entries()) {
550
576
  const row = childElements.find((e) => e.type === 'row' && e.id === rowId);
551
577
  const cell = childElements.find((e) => e.type === 'cell' && e.rowId === rowId && e.columnId === columnId);
552
- if (!row || !cell)
578
+ if (!row || !cell || TableService.isVerticalCell(cell))
553
579
  continue;
554
580
  const cellUndo = util_1.ObjectUtil.cloneDeep(cell);
555
581
  const cellRange = new table_range_1.TableRange(rowIndex, rowIndex, columnIndex, columnIndex);
@@ -589,15 +615,68 @@ class TableService {
589
615
  }
590
616
  return { elementsToUpdate };
591
617
  }
618
+ static autoFitColumns(tableElement, childElements, rowId) {
619
+ var _a;
620
+ const elementsToUpdate = [];
621
+ const rowIndex = tableElement.rowIds.indexOf(rowId);
622
+ const row = childElements.find((e) => e.type === 'row' && e.id === rowId);
623
+ const mergedRanges = table_merge_service_1.TableMergeService.getMergedRanges(tableElement, childElements);
624
+ for (const [columnIndex, columnId] of tableElement.columnIds.entries()) {
625
+ const column = childElements.find((e) => e.type === 'column' && e.id === columnId);
626
+ const cell = childElements.find((e) => e.type === 'cell' && e.rowId === rowId && e.columnId === columnId);
627
+ if (!column || !cell || !TableService.isVerticalCell(cell))
628
+ continue;
629
+ const cellUndo = util_1.ObjectUtil.cloneDeep(cell);
630
+ const cellRange = new table_range_1.TableRange(rowIndex, rowIndex, columnIndex, columnIndex);
631
+ const mergedRange = mergedRanges.find((range) => cellRange.within(range));
632
+ const mergedRangeStart = mergedRange && cellRange.equals(mergedRange.start());
633
+ const mergedRangeOther = mergedRange && !cellRange.equals(mergedRange.start());
634
+ if (mergedRangeOther) {
635
+ const mergedCell = childElements.find((e) => e.type === 'cell' &&
636
+ e.rowId === tableElement.rowIds[mergedRange.startRow] &&
637
+ e.columnId === tableElement.columnIds[mergedRange.startColumn]);
638
+ if (columnIndex === mergedRange.endColumn && rowIndex !== mergedRange.startRow) {
639
+ const mergedCellUndo = util_1.ObjectUtil.cloneDeep(mergedCell);
640
+ mergedCell.size.height = TableService.getRowsHeight(tableElement, childElements, mergedRange.startRow, mergedRange.endRow);
641
+ mergedCell.size.width = TableService.getCellSize(mergedCell).height;
642
+ elementsToUpdate.push({
643
+ change: mergedCell,
644
+ undo: mergedCellUndo,
645
+ });
646
+ }
647
+ }
648
+ cell.size.height = mergedRangeStart
649
+ ? TableService.getRowsHeight(tableElement, childElements, mergedRange.startRow, mergedRange.endRow)
650
+ : row.size.height;
651
+ cell.size.width = TableService.getCellSize(cell).height;
652
+ elementsToUpdate.push({
653
+ change: cell,
654
+ undo: cellUndo,
655
+ });
656
+ if (columnIndex === ((_a = mergedRange === null || mergedRange === void 0 ? void 0 : mergedRange.endColumn) !== null && _a !== void 0 ? _a : columnIndex)) {
657
+ const cellWidth = TableService.getCellMinWidth(cell, tableElement, childElements);
658
+ if (cellWidth > column.size.width) {
659
+ elementsToUpdate.push(TableService.resizeColumn(tableElement, childElements, columnId, cellWidth).elementsToUpdate[1]);
660
+ }
661
+ }
662
+ }
663
+ return { elementsToUpdate };
664
+ }
592
665
  static clearCell(element) {
593
666
  element.text = '';
594
- element.size.height = table_constants_1.TABLE_ROW_MIN_HEIGHT;
667
+ if (TableService.isVerticalCell(element)) {
668
+ element.size.width = table_constants_1.TABLE_COLUMN_MIN_WIDTH;
669
+ }
670
+ else {
671
+ element.size.height = table_constants_1.TABLE_ROW_MIN_HEIGHT;
672
+ }
595
673
  }
596
674
  static getCellSize(element, text) {
597
675
  var _a, _b, _c, _d, _e, _f;
598
676
  const fontFamily = `${(_c = (_b = (_a = element.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.family) !== null && _c !== void 0 ? _c : table_constants_1.DEFAULT_TEXT_FONT_FAMILY}`;
599
677
  const fontSize = `${(_f = (_e = (_d = element.style) === null || _d === void 0 ? void 0 : _d.font) === null || _e === void 0 ? void 0 : _e.size) !== null && _f !== void 0 ? _f : table_constants_1.DEFAULT_TEXT_FONT_SIZE}pt`;
600
- const fixedWidth = `${element.size.width - table_constants_1.TABLE_TEXT_PADDING * 2}px`;
678
+ const width = TableService.isVerticalCell(element) ? element.size.height : element.size.width;
679
+ const fixedWidth = `${width - table_constants_1.TABLE_TEXT_PADDING * 2}px`;
601
680
  const size = this.getSize(text !== null && text !== void 0 ? text : element.text, `width: ${fixedWidth}; height: auto; font-family: ${fontFamily}; font-size: ${fontSize};`);
602
681
  return { height: Math.max(size.height + table_constants_1.TABLE_TEXT_PADDING * 2, table_constants_1.TABLE_ROW_MIN_HEIGHT), width: size.width };
603
682
  }
@@ -623,13 +702,19 @@ class TableService {
623
702
  const columnId = tableElement.columnIds[columnIndex];
624
703
  const cell = childElements.find((e) => e.type === 'cell' && e.rowId === rowId && e.columnId === columnId);
625
704
  const row = childElements.find((e) => e.type === 'row' && e.id === rowId);
705
+ const column = childElements.find((e) => e.type === 'column' && e.id === columnId);
626
706
  if (!rowId || !columnId || !cell || !row) {
627
707
  throw new Error(TABLE_ERROR.INVALID_TABLE);
628
708
  }
629
709
  const elementsToUpdate = [];
630
710
  const cellUndo = Object.assign(Object.assign({}, tableData(cell)), { text: cell.text, size: Object.assign({}, cell.size) });
631
711
  cell.text = text;
632
- cell.size.height = TableService.getCellSize(cell).height;
712
+ if (TableService.isVerticalCell(cell)) {
713
+ cell.size.width = TableService.getCellSize(cell).height;
714
+ }
715
+ else {
716
+ cell.size.height = TableService.getCellSize(cell).height;
717
+ }
633
718
  elementsToUpdate.push({
634
719
  change: Object.assign(Object.assign({}, tableData(cell)), { text: cell.text, size: cell.size }),
635
720
  undo: cellUndo,
@@ -637,10 +722,17 @@ class TableService {
637
722
  if (cell.size.height > row.size.height) {
638
723
  elementsToUpdate.push(...TableService.resizeRow(tableElement, childElements, rowId, cell.size.height).elementsToUpdate);
639
724
  }
725
+ if (cell.size.width > column.size.width) {
726
+ elementsToUpdate.push(...TableService.resizeColumn(tableElement, childElements, columnId, cell.size.width).elementsToUpdate);
727
+ }
640
728
  return {
641
729
  elementsToUpdate,
642
730
  };
643
731
  }
732
+ static isVerticalCell(element) {
733
+ var _a;
734
+ return element && element.type === 'cell' && ((_a = element.rotate) === null || _a === void 0 ? void 0 : _a.angle) === 270;
735
+ }
644
736
  }
645
737
  exports.TableService = TableService;
646
738
  TableService.DEFAULT_CELL_STYLE = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/document-table",
3
- "version": "1.0.5",
3
+ "version": "1.0.12",
4
4
  "description": "Library for document tables",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@contrail/actions": "^1.0.17",
42
- "@contrail/documents": "^1.3.2-alpha.0",
42
+ "@contrail/documents": "^1.3.2",
43
43
  "@contrail/types": "^3.1.3",
44
44
  "@contrail/util": "^1.1.18",
45
45
  "reflect-metadata": "^0.1.13"