@atlaskit/editor-plugin-table 12.2.6 → 12.2.7
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/CHANGELOG.md +8 -0
- package/package.json +4 -4
- package/afm-cc/tsconfig.json +0 -123
- package/afm-dev-agents/tsconfig.json +0 -123
- package/afm-jira/tsconfig.json +0 -123
- package/afm-passionfruit/tsconfig.json +0 -123
- package/afm-post-office/tsconfig.json +0 -123
- package/afm-rovo-extension/tsconfig.json +0 -123
- package/afm-townsquare/tsconfig.json +0 -123
- package/afm-volt/tsconfig.json +0 -114
- package/build/tsconfig.json +0 -23
- package/docs/0-intro.tsx +0 -57
- package/src/index.ts +0 -21
- package/src/nodeviews/ExternalDropTargets.tsx +0 -91
- package/src/nodeviews/OverflowShadowsObserver.ts +0 -156
- package/src/nodeviews/TableCell.ts +0 -134
- package/src/nodeviews/TableComponent.tsx +0 -1590
- package/src/nodeviews/TableComponentWithSharedState.tsx +0 -278
- package/src/nodeviews/TableContainer.tsx +0 -926
- package/src/nodeviews/TableNodeViewBase.ts +0 -29
- package/src/nodeviews/TableResizer.tsx +0 -884
- package/src/nodeviews/TableRow.ts +0 -830
- package/src/nodeviews/TableStickyScrollbar.ts +0 -211
- package/src/nodeviews/__mocks__/OverflowShadowsObserver.ts +0 -15
- package/src/nodeviews/__mocks__/OverridableMock.ts +0 -26
- package/src/nodeviews/table-container-styles.ts +0 -9
- package/src/nodeviews/table-node-views.ts +0 -76
- package/src/nodeviews/table.tsx +0 -530
- package/src/nodeviews/toDOM.ts +0 -244
- package/src/nodeviews/types.ts +0 -36
- package/src/nodeviews/update-overflow-shadows.ts +0 -11
- package/src/pm-plugins/analytics/actions.ts +0 -21
- package/src/pm-plugins/analytics/commands.ts +0 -47
- package/src/pm-plugins/analytics/plugin-factory.ts +0 -9
- package/src/pm-plugins/analytics/plugin-key.ts +0 -5
- package/src/pm-plugins/analytics/plugin.ts +0 -80
- package/src/pm-plugins/analytics/reducer.ts +0 -27
- package/src/pm-plugins/analytics/types.ts +0 -20
- package/src/pm-plugins/analytics/utils/moved-event.ts +0 -51
- package/src/pm-plugins/commands/clear.ts +0 -43
- package/src/pm-plugins/commands/collapse.ts +0 -17
- package/src/pm-plugins/commands/column-resize.ts +0 -478
- package/src/pm-plugins/commands/commands-with-analytics.ts +0 -715
- package/src/pm-plugins/commands/delete.ts +0 -42
- package/src/pm-plugins/commands/display-mode.ts +0 -18
- package/src/pm-plugins/commands/go-to-next-cell.ts +0 -198
- package/src/pm-plugins/commands/hover.ts +0 -242
- package/src/pm-plugins/commands/index.ts +0 -51
- package/src/pm-plugins/commands/insert.ts +0 -438
- package/src/pm-plugins/commands/misc.ts +0 -811
- package/src/pm-plugins/commands/referentiality.ts +0 -15
- package/src/pm-plugins/commands/selection.ts +0 -537
- package/src/pm-plugins/commands/sort.ts +0 -102
- package/src/pm-plugins/commands/split-cell.ts +0 -28
- package/src/pm-plugins/commands/toggle.ts +0 -109
- package/src/pm-plugins/create-plugin-config.ts +0 -17
- package/src/pm-plugins/decorations/plugin.ts +0 -107
- package/src/pm-plugins/decorations/utils/column-controls.ts +0 -91
- package/src/pm-plugins/decorations/utils/column-resizing.ts +0 -71
- package/src/pm-plugins/decorations/utils/compose-decorations.ts +0 -9
- package/src/pm-plugins/decorations/utils/types.ts +0 -16
- package/src/pm-plugins/default-table-selection.ts +0 -14
- package/src/pm-plugins/drag-and-drop/actions.ts +0 -48
- package/src/pm-plugins/drag-and-drop/commands-with-analytics.ts +0 -222
- package/src/pm-plugins/drag-and-drop/commands.ts +0 -194
- package/src/pm-plugins/drag-and-drop/consts.ts +0 -7
- package/src/pm-plugins/drag-and-drop/plugin-factory.ts +0 -33
- package/src/pm-plugins/drag-and-drop/plugin-key.ts +0 -5
- package/src/pm-plugins/drag-and-drop/plugin.ts +0 -398
- package/src/pm-plugins/drag-and-drop/reducer.ts +0 -38
- package/src/pm-plugins/drag-and-drop/types.ts +0 -18
- package/src/pm-plugins/drag-and-drop/utils/autoscrollers.ts +0 -49
- package/src/pm-plugins/drag-and-drop/utils/getDragBehaviour.ts +0 -9
- package/src/pm-plugins/drag-and-drop/utils/monitor.ts +0 -73
- package/src/pm-plugins/handlers.ts +0 -161
- package/src/pm-plugins/keymap.ts +0 -436
- package/src/pm-plugins/main.ts +0 -433
- package/src/pm-plugins/plugin-factory.ts +0 -42
- package/src/pm-plugins/plugin-key.ts +0 -8
- package/src/pm-plugins/reducer.ts +0 -145
- package/src/pm-plugins/safari-delete-composition-text-issue-workaround.ts +0 -102
- package/src/pm-plugins/sticky-headers/commands.ts +0 -8
- package/src/pm-plugins/sticky-headers/plugin-key.ts +0 -5
- package/src/pm-plugins/sticky-headers/plugin-state.ts +0 -52
- package/src/pm-plugins/sticky-headers/plugin.ts +0 -12
- package/src/pm-plugins/sticky-headers/types.ts +0 -20
- package/src/pm-plugins/sticky-headers/util.ts +0 -18
- package/src/pm-plugins/table-analytics.ts +0 -100
- package/src/pm-plugins/table-local-id.ts +0 -213
- package/src/pm-plugins/table-resizing/commands.ts +0 -116
- package/src/pm-plugins/table-resizing/event-handlers.ts +0 -352
- package/src/pm-plugins/table-resizing/plugin-factory.ts +0 -29
- package/src/pm-plugins/table-resizing/plugin-key.ts +0 -5
- package/src/pm-plugins/table-resizing/plugin.ts +0 -94
- package/src/pm-plugins/table-resizing/reducer.ts +0 -37
- package/src/pm-plugins/table-resizing/utils/colgroup.ts +0 -306
- package/src/pm-plugins/table-resizing/utils/column-state.ts +0 -120
- package/src/pm-plugins/table-resizing/utils/consts.ts +0 -11
- package/src/pm-plugins/table-resizing/utils/content-width.ts +0 -118
- package/src/pm-plugins/table-resizing/utils/dom.ts +0 -132
- package/src/pm-plugins/table-resizing/utils/misc.ts +0 -282
- package/src/pm-plugins/table-resizing/utils/resize-column.ts +0 -34
- package/src/pm-plugins/table-resizing/utils/resize-logic.ts +0 -289
- package/src/pm-plugins/table-resizing/utils/resize-state.ts +0 -417
- package/src/pm-plugins/table-resizing/utils/scale-table.ts +0 -290
- package/src/pm-plugins/table-resizing/utils/types.ts +0 -25
- package/src/pm-plugins/table-resizing/utils/unit-to-number.ts +0 -1
- package/src/pm-plugins/table-selection-keymap.ts +0 -64
- package/src/pm-plugins/table-size-selector.ts +0 -39
- package/src/pm-plugins/table-width-in-comment-fix.ts +0 -113
- package/src/pm-plugins/table-width.ts +0 -153
- package/src/pm-plugins/transforms/column-width.ts +0 -249
- package/src/pm-plugins/transforms/delete-columns.ts +0 -281
- package/src/pm-plugins/transforms/delete-rows.ts +0 -154
- package/src/pm-plugins/transforms/fix-tables.ts +0 -249
- package/src/pm-plugins/transforms/merge.ts +0 -301
- package/src/pm-plugins/transforms/replace-table.ts +0 -38
- package/src/pm-plugins/transforms/split.ts +0 -90
- package/src/pm-plugins/utils/alignment.ts +0 -33
- package/src/pm-plugins/utils/analytics.ts +0 -238
- package/src/pm-plugins/utils/collapse.ts +0 -93
- package/src/pm-plugins/utils/column-controls.ts +0 -250
- package/src/pm-plugins/utils/create.ts +0 -64
- package/src/pm-plugins/utils/decoration.ts +0 -672
- package/src/pm-plugins/utils/dom.ts +0 -251
- package/src/pm-plugins/utils/drag-menu.tsx +0 -491
- package/src/pm-plugins/utils/get-allow-add-column-custom-step.ts +0 -10
- package/src/pm-plugins/utils/guidelines.ts +0 -30
- package/src/pm-plugins/utils/merged-cells.ts +0 -239
- package/src/pm-plugins/utils/nodes.ts +0 -162
- package/src/pm-plugins/utils/paste.ts +0 -386
- package/src/pm-plugins/utils/row-controls.ts +0 -211
- package/src/pm-plugins/utils/selection.ts +0 -17
- package/src/pm-plugins/utils/snapping.ts +0 -136
- package/src/pm-plugins/utils/table.ts +0 -60
- package/src/pm-plugins/utils/update-plugin-state-decorations.ts +0 -13
- package/src/pm-plugins/view-mode-sort/consts.ts +0 -3
- package/src/pm-plugins/view-mode-sort/index.ts +0 -291
- package/src/pm-plugins/view-mode-sort/plugin-key.ts +0 -7
- package/src/pm-plugins/view-mode-sort/types.ts +0 -23
- package/src/pm-plugins/view-mode-sort/utils.ts +0 -136
- package/src/tablePlugin.tsx +0 -971
- package/src/tablePluginType.ts +0 -102
- package/src/types/index.ts +0 -592
- package/src/ui/ColumnResizeWidget/index.tsx +0 -61
- package/src/ui/ContentComponent.tsx +0 -311
- package/src/ui/DragHandle/HandleIconComponent.tsx +0 -21
- package/src/ui/DragHandle/index.tsx +0 -391
- package/src/ui/DragPreview/index.tsx +0 -51
- package/src/ui/FloatingAlignmentButtons/FloatingAlignmentButtons.tsx +0 -59
- package/src/ui/FloatingContextualButton/FixedButton.tsx +0 -203
- package/src/ui/FloatingContextualButton/index.tsx +0 -168
- package/src/ui/FloatingContextualButton/styles.ts +0 -69
- package/src/ui/FloatingContextualMenu/ContextualMenu.tsx +0 -931
- package/src/ui/FloatingContextualMenu/index.tsx +0 -141
- package/src/ui/FloatingContextualMenu/styles.ts +0 -77
- package/src/ui/FloatingDeleteButton/DeleteButton.tsx +0 -54
- package/src/ui/FloatingDeleteButton/getPopUpOptions.ts +0 -65
- package/src/ui/FloatingDeleteButton/index.tsx +0 -383
- package/src/ui/FloatingDeleteButton/types.ts +0 -3
- package/src/ui/FloatingDragMenu/DragMenu.tsx +0 -668
- package/src/ui/FloatingDragMenu/DropdownMenu.tsx +0 -221
- package/src/ui/FloatingDragMenu/index.tsx +0 -136
- package/src/ui/FloatingDragMenu/styles.ts +0 -83
- package/src/ui/FloatingInsertButton/InsertButton.tsx +0 -263
- package/src/ui/FloatingInsertButton/getPopupOptions.ts +0 -131
- package/src/ui/FloatingInsertButton/index.tsx +0 -314
- package/src/ui/FloatingToolbarLabel/FloatingToolbarLabel.tsx +0 -31
- package/src/ui/SizeSelector/index.tsx +0 -74
- package/src/ui/TableFloatingColumnControls/ColumnControls/index.tsx +0 -397
- package/src/ui/TableFloatingColumnControls/ColumnDropTargets/ColumnDropTarget.tsx +0 -105
- package/src/ui/TableFloatingColumnControls/ColumnDropTargets/index.tsx +0 -63
- package/src/ui/TableFloatingColumnControls/index.tsx +0 -151
- package/src/ui/TableFloatingControls/CornerControls/ClassicCornerControls.tsx +0 -106
- package/src/ui/TableFloatingControls/CornerControls/DragCornerControls.tsx +0 -143
- package/src/ui/TableFloatingControls/CornerControls/types.ts +0 -12
- package/src/ui/TableFloatingControls/FloatingControlsWithSelection.tsx +0 -88
- package/src/ui/TableFloatingControls/NumberColumn/index.tsx +0 -175
- package/src/ui/TableFloatingControls/RowControls/ClassicControls.tsx +0 -131
- package/src/ui/TableFloatingControls/RowControls/DragControls.tsx +0 -429
- package/src/ui/TableFloatingControls/RowDropTarget/index.tsx +0 -96
- package/src/ui/TableFloatingControls/index.tsx +0 -275
- package/src/ui/TableFullWidthLabel/index.tsx +0 -38
- package/src/ui/common-styles.ts +0 -1218
- package/src/ui/consts.ts +0 -109
- package/src/ui/event-handlers.ts +0 -662
- package/src/ui/global-styles.tsx +0 -55
- package/src/ui/hooks/useInternalTablePluginStateSelector.ts +0 -38
- package/src/ui/icons/AddColLeftIcon.tsx +0 -37
- package/src/ui/icons/AddColRightIcon.tsx +0 -37
- package/src/ui/icons/AddRowAboveIcon.tsx +0 -22
- package/src/ui/icons/AddRowBelowIcon.tsx +0 -39
- package/src/ui/icons/DragHandleDisabledIcon.tsx +0 -25
- package/src/ui/icons/DragHandleIcon.tsx +0 -16
- package/src/ui/icons/DragInMotionIcon.tsx +0 -54
- package/src/ui/icons/MergeCellsIcon.tsx +0 -26
- package/src/ui/icons/MinimisedHandle.tsx +0 -15
- package/src/ui/icons/SortingIconWrapper.tsx +0 -43
- package/src/ui/icons/SplitCellIcon.tsx +0 -34
- package/src/ui/toolbar.tsx +0 -1153
- package/src/ui/ui-styles.ts +0 -960
- package/tsconfig.app.json +0 -135
- package/tsconfig.dev.json +0 -54
- package/tsconfig.json +0 -18
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
import type { CellAttributes } from '@atlaskit/adf-schema';
|
|
2
|
-
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
3
|
-
import { Fragment } from '@atlaskit/editor-prosemirror/model';
|
|
4
|
-
import type { Transaction } from '@atlaskit/editor-prosemirror/state';
|
|
5
|
-
import { Selection } from '@atlaskit/editor-prosemirror/state';
|
|
6
|
-
import { CellSelection } from '@atlaskit/editor-tables/cell-selection';
|
|
7
|
-
import type { Rect } from '@atlaskit/editor-tables/table-map';
|
|
8
|
-
import { TableMap } from '@atlaskit/editor-tables/table-map';
|
|
9
|
-
import { findTable, getSelectionRect } from '@atlaskit/editor-tables/utils';
|
|
10
|
-
|
|
11
|
-
// re-creates table node with merged cells
|
|
12
|
-
export function mergeCells(tr: Transaction): Transaction {
|
|
13
|
-
const { selection } = tr;
|
|
14
|
-
if (!(selection instanceof CellSelection) || !canMergeCells(tr)) {
|
|
15
|
-
return tr;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Ignored via go/ees005
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
20
|
-
const rect = getSelectionRect(selection)!;
|
|
21
|
-
// Ignored via go/ees005
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
23
|
-
const table = findTable(selection)!;
|
|
24
|
-
const map = TableMap.get(table.node);
|
|
25
|
-
const seen: number[] = [];
|
|
26
|
-
const selectedCells = map.cellsInRect(rect);
|
|
27
|
-
let mergedCellPos;
|
|
28
|
-
|
|
29
|
-
const rows: PMNode[] = [];
|
|
30
|
-
for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
|
|
31
|
-
const rowCells: PMNode[] = [];
|
|
32
|
-
const row = table.node.child(rowIndex);
|
|
33
|
-
|
|
34
|
-
for (let colIndex = 0; colIndex < map.width; colIndex++) {
|
|
35
|
-
const cellPos = map.map[rowIndex * map.width + colIndex];
|
|
36
|
-
const cell = table.node.nodeAt(cellPos);
|
|
37
|
-
if (!cell || seen.indexOf(cellPos) > -1) {
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
seen.push(cellPos);
|
|
41
|
-
|
|
42
|
-
// merged cell
|
|
43
|
-
if (colIndex === rect.left && rowIndex === rect.top) {
|
|
44
|
-
mergedCellPos = cellPos;
|
|
45
|
-
// merge content of the selected cells, dropping empty cells
|
|
46
|
-
let content = isEmptyCell(cell) ? Fragment.empty : cell.content;
|
|
47
|
-
const seenContent: number[] = [mergedCellPos];
|
|
48
|
-
for (let i = rect.top; i < rect.bottom; i++) {
|
|
49
|
-
for (let j = rect.left; j < rect.right; j++) {
|
|
50
|
-
const pos = map.map[i * map.width + j];
|
|
51
|
-
if (seenContent.indexOf(pos) === -1) {
|
|
52
|
-
seenContent.push(pos);
|
|
53
|
-
const copyCell = table.node.nodeAt(pos);
|
|
54
|
-
if (copyCell && !isEmptyCell(copyCell)) {
|
|
55
|
-
content = content.append(copyCell.content);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const rowspan = rect.bottom - rect.top;
|
|
61
|
-
if (rowspan < 1) {
|
|
62
|
-
return tr;
|
|
63
|
-
}
|
|
64
|
-
// update colspan and rowspan of the merged cell to span the selection
|
|
65
|
-
const attrs = addColSpan(
|
|
66
|
-
{
|
|
67
|
-
...cell.attrs,
|
|
68
|
-
rowspan,
|
|
69
|
-
},
|
|
70
|
-
cell.attrs.colspan,
|
|
71
|
-
rect.right - rect.left - cell.attrs.colspan,
|
|
72
|
-
);
|
|
73
|
-
const newCell =
|
|
74
|
-
content === Fragment.empty
|
|
75
|
-
? cell.type.createAndFill(attrs, content, cell.marks)
|
|
76
|
-
: cell.type.createChecked(attrs, content, cell.marks);
|
|
77
|
-
// Ignored via go/ees005
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
79
|
-
rowCells.push(newCell!);
|
|
80
|
-
} else if (selectedCells.indexOf(cellPos) === -1) {
|
|
81
|
-
// if its one of the selected cells, but not the merged cell, we get rid of it
|
|
82
|
-
// otherwise we keep the cell
|
|
83
|
-
rowCells.push(cell);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (rowCells.length) {
|
|
88
|
-
rows.push(row.type.createChecked(row.attrs, rowCells, row.marks));
|
|
89
|
-
} else {
|
|
90
|
-
// empty row, we need to fix rowspans for rows above the current one
|
|
91
|
-
for (let i = rows.length - 1; i >= 0; i--) {
|
|
92
|
-
const prevRow = rows[i];
|
|
93
|
-
const cells: PMNode[] = [];
|
|
94
|
-
let rowChanged = false;
|
|
95
|
-
|
|
96
|
-
for (let j = 0; j < prevRow.childCount; j++) {
|
|
97
|
-
const cell = prevRow.child(j);
|
|
98
|
-
const { rowspan } = cell.attrs;
|
|
99
|
-
if (rowspan && rowspan + i - 1 >= rows.length) {
|
|
100
|
-
rowChanged = true;
|
|
101
|
-
if (rowspan < 2) {
|
|
102
|
-
return tr;
|
|
103
|
-
}
|
|
104
|
-
cells.push(
|
|
105
|
-
cell.type.createChecked(
|
|
106
|
-
{
|
|
107
|
-
...cell.attrs,
|
|
108
|
-
rowspan: rowspan - 1,
|
|
109
|
-
},
|
|
110
|
-
cell.content,
|
|
111
|
-
cell.marks,
|
|
112
|
-
),
|
|
113
|
-
);
|
|
114
|
-
} else {
|
|
115
|
-
cells.push(cell);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (rowChanged) {
|
|
119
|
-
rows[i] = row.type.createChecked(prevRow.attrs, cells, prevRow.marks);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// empty tables? cancel merging like nothing happened
|
|
126
|
-
if (!rows.length) {
|
|
127
|
-
return tr;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const newTable = table.node.type.createChecked(table.node.attrs, rows, table.node.marks);
|
|
131
|
-
|
|
132
|
-
const fixedTable = mergeEmptyColumns(newTable);
|
|
133
|
-
if (fixedTable === null) {
|
|
134
|
-
return tr;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return tr
|
|
138
|
-
.replaceWith(table.pos, table.pos + table.node.nodeSize, fixedTable)
|
|
139
|
-
.setSelection(Selection.near(tr.doc.resolve((mergedCellPos || 0) + table.start)));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function canMergeCells(tr: Transaction): boolean {
|
|
143
|
-
const { selection } = tr;
|
|
144
|
-
if (
|
|
145
|
-
!(selection instanceof CellSelection) ||
|
|
146
|
-
selection.$anchorCell.pos === selection.$headCell.pos
|
|
147
|
-
) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const rect = getSelectionRect(selection);
|
|
152
|
-
if (!rect) {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
const table = selection.$anchorCell.node(-1);
|
|
156
|
-
const map = TableMap.get(table);
|
|
157
|
-
if (cellsOverlapRectangle(map, rect)) {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return true;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function isEmptyCell(cell: PMNode) {
|
|
165
|
-
const { content } = cell;
|
|
166
|
-
return (
|
|
167
|
-
content.childCount === 1 &&
|
|
168
|
-
content.firstChild &&
|
|
169
|
-
content.firstChild.isTextblock &&
|
|
170
|
-
content.firstChild.childCount === 0
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function addColSpan(attrs: CellAttributes, pos: number, span: number = 1) {
|
|
175
|
-
const newAttrs = {
|
|
176
|
-
...attrs,
|
|
177
|
-
colspan: (attrs.colspan || 1) + span,
|
|
178
|
-
};
|
|
179
|
-
if (newAttrs.colwidth) {
|
|
180
|
-
newAttrs.colwidth = newAttrs.colwidth.slice();
|
|
181
|
-
for (let i = 0; i < span; i++) {
|
|
182
|
-
newAttrs.colwidth.splice(pos, 0, 0);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return newAttrs;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function cellsOverlapRectangle({ width, height, map }: TableMap, rect: Rect) {
|
|
190
|
-
let indexTop = rect.top * width + rect.left;
|
|
191
|
-
let indexLeft = indexTop;
|
|
192
|
-
|
|
193
|
-
let indexBottom = (rect.bottom - 1) * width + rect.left;
|
|
194
|
-
let indexRight = indexTop + (rect.right - rect.left - 1);
|
|
195
|
-
|
|
196
|
-
for (let i = rect.top; i < rect.bottom; i++) {
|
|
197
|
-
if (
|
|
198
|
-
(rect.left > 0 && map[indexLeft] === map[indexLeft - 1]) ||
|
|
199
|
-
(rect.right < width && map[indexRight] === map[indexRight + 1])
|
|
200
|
-
) {
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
indexLeft += width;
|
|
204
|
-
indexRight += width;
|
|
205
|
-
}
|
|
206
|
-
for (let i = rect.left; i < rect.right; i++) {
|
|
207
|
-
if (
|
|
208
|
-
(rect.top > 0 && map[indexTop] === map[indexTop - width]) ||
|
|
209
|
-
(rect.bottom < height && map[indexBottom] === map[indexBottom + width])
|
|
210
|
-
) {
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
indexTop++;
|
|
215
|
-
indexBottom++;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return false;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// returns an array of numbers, each number indicates the minimum colSpan in each column
|
|
222
|
-
function getEmptyColumnIndexes(table: PMNode): Set<number> {
|
|
223
|
-
const map = TableMap.get(table);
|
|
224
|
-
const emptyColumnIndexes = new Set<number>();
|
|
225
|
-
|
|
226
|
-
// Loop throuh each column
|
|
227
|
-
for (let colIndex = 0; colIndex < map.width; colIndex++) {
|
|
228
|
-
// Get the cells in each row for this column
|
|
229
|
-
const cellPositions = map.cellsInRect({
|
|
230
|
-
left: colIndex,
|
|
231
|
-
right: colIndex + 1,
|
|
232
|
-
top: 0,
|
|
233
|
-
bottom: map.height,
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// If no cells exist in that column it is empty
|
|
237
|
-
if (!cellPositions.length) {
|
|
238
|
-
emptyColumnIndexes.add(colIndex);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return emptyColumnIndexes;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
export function mergeEmptyColumns(table: PMNode): PMNode | null {
|
|
246
|
-
const rows: PMNode[] = [];
|
|
247
|
-
const map = TableMap.get(table);
|
|
248
|
-
const emptyColumnIndexes = getEmptyColumnIndexes(table);
|
|
249
|
-
|
|
250
|
-
// We don't need to remove any so return early.
|
|
251
|
-
if (emptyColumnIndexes.size === 0) {
|
|
252
|
-
return table;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
|
|
256
|
-
const cellsByCols: Record<string, PMNode> = {};
|
|
257
|
-
|
|
258
|
-
// Work backwards so that calculating colwidths is easier with Array.slice
|
|
259
|
-
for (let colIndex = map.width - 1; colIndex >= 0; colIndex--) {
|
|
260
|
-
const cellPos = map.map[colIndex + rowIndex * map.width];
|
|
261
|
-
const rect = map.findCell(cellPos);
|
|
262
|
-
let cell = cellsByCols[rect.left] || table.nodeAt(cellPos);
|
|
263
|
-
|
|
264
|
-
if (rect.top !== rowIndex) {
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// If this column is empty, we want to decrement the colspan of its corresponding
|
|
269
|
-
// cell as this column is being "merged"
|
|
270
|
-
if (emptyColumnIndexes.has(colIndex)) {
|
|
271
|
-
const { colspan, colwidth } = cell.attrs;
|
|
272
|
-
|
|
273
|
-
if (colspan > 1) {
|
|
274
|
-
cell = cell.type.createChecked(
|
|
275
|
-
{
|
|
276
|
-
...cell.attrs,
|
|
277
|
-
colspan: colspan - 1,
|
|
278
|
-
colwidth: colwidth ? colwidth.slice(0, colspan) : null,
|
|
279
|
-
},
|
|
280
|
-
cell.content,
|
|
281
|
-
cell.marks,
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
cellsByCols[rect.left] = cell;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const rowCells = Object.values(cellsByCols);
|
|
290
|
-
const row = table.child(rowIndex);
|
|
291
|
-
if (row) {
|
|
292
|
-
rows.push(row.type.createChecked(row.attrs, rowCells, row.marks));
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (!rows.length) {
|
|
297
|
-
return null;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return table.type.createChecked(table.attrs, rows, table.marks);
|
|
301
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics';
|
|
2
|
-
import type { EditorAnalyticsAPI, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
|
|
3
|
-
import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
|
|
4
|
-
import type { EditorState, Transaction } from '@atlaskit/editor-prosemirror/state';
|
|
5
|
-
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
|
|
6
|
-
import { findTable, isTableSelected } from '@atlaskit/editor-tables/utils';
|
|
7
|
-
|
|
8
|
-
import { getSelectedTableInfo } from '../utils/analytics';
|
|
9
|
-
|
|
10
|
-
export const replaceSelectedTable = (
|
|
11
|
-
state: EditorState,
|
|
12
|
-
content: string | Slice,
|
|
13
|
-
inputMethod: INPUT_METHOD.KEYBOARD | INPUT_METHOD.CLIPBOARD,
|
|
14
|
-
editorAnalyticsAPI?: EditorAnalyticsAPI,
|
|
15
|
-
): Transaction => {
|
|
16
|
-
if (isTableSelected(state.selection)) {
|
|
17
|
-
const table = findTable(state.selection);
|
|
18
|
-
if (table) {
|
|
19
|
-
const slice: Slice =
|
|
20
|
-
typeof content === 'string'
|
|
21
|
-
? new Slice(Fragment.from(state.schema.text(content)), 0, 0)
|
|
22
|
-
: content;
|
|
23
|
-
const tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice);
|
|
24
|
-
tr.setSelection(TextSelection.create(tr.doc, table.pos + slice.size + 1));
|
|
25
|
-
|
|
26
|
-
const { totalRowCount, totalColumnCount } = getSelectedTableInfo(state.selection);
|
|
27
|
-
editorAnalyticsAPI?.attachAnalyticsEvent({
|
|
28
|
-
action: TABLE_ACTION.REPLACED,
|
|
29
|
-
actionSubject: ACTION_SUBJECT.TABLE,
|
|
30
|
-
attributes: { totalColumnCount, totalRowCount, inputMethod },
|
|
31
|
-
eventType: EVENT_TYPE.TRACK,
|
|
32
|
-
})(tr);
|
|
33
|
-
|
|
34
|
-
return tr;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return state.tr;
|
|
38
|
-
};
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type { CellAttributes } from '@atlaskit/adf-schema';
|
|
2
|
-
import type { Node as ProseMirrorNode } from '@atlaskit/editor-prosemirror/model';
|
|
3
|
-
import type { Transaction } from '@atlaskit/editor-prosemirror/state';
|
|
4
|
-
import { TableMap } from '@atlaskit/editor-tables/table-map';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Helper to split all the cells in a range of columns
|
|
8
|
-
* @param tr
|
|
9
|
-
* @param tablePos
|
|
10
|
-
* @param columnStart - Start of the rect included (rect.left)
|
|
11
|
-
* @param columnEnd - End of the rect not included (rect.right)
|
|
12
|
-
*/
|
|
13
|
-
export function splitCellsInColumns(
|
|
14
|
-
tr: Transaction,
|
|
15
|
-
tablePos: number,
|
|
16
|
-
columnStart: number,
|
|
17
|
-
columnEnd: number,
|
|
18
|
-
): Transaction {
|
|
19
|
-
const mapStart = tr.mapping.maps.length;
|
|
20
|
-
const table = tr.doc.nodeAt(tablePos);
|
|
21
|
-
if (!table) {
|
|
22
|
-
return tr;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const tableStart = tr.doc.resolve(tablePos).start(1);
|
|
26
|
-
const map = TableMap.get(table);
|
|
27
|
-
|
|
28
|
-
for (let column = columnStart; column < columnEnd; column++) {
|
|
29
|
-
for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
|
|
30
|
-
const cellIndex = rowIndex * map.width + column;
|
|
31
|
-
const cellPos = map.map[cellIndex];
|
|
32
|
-
|
|
33
|
-
// Check if the cell is contained by another by another row/column
|
|
34
|
-
const hasMergedCellsBefore =
|
|
35
|
-
(column > 0 && map.map[cellIndex - 1] === cellPos) ||
|
|
36
|
-
(rowIndex > 0 && map.map[(rowIndex - 1) * map.width + column] === cellPos);
|
|
37
|
-
// Check if the cell contains another row/column
|
|
38
|
-
const hasMergedCellsAfter =
|
|
39
|
-
(column < map.width - 1 && map.map[cellIndex + 1] === cellPos) ||
|
|
40
|
-
(rowIndex < map.height - 1 && map.map[(rowIndex + 1) * map.width + column] === cellPos);
|
|
41
|
-
if (!hasMergedCellsBefore && hasMergedCellsAfter) {
|
|
42
|
-
// Is a merged cell that start in this row/column
|
|
43
|
-
const cellNode = table.nodeAt(cellPos) as ProseMirrorNode;
|
|
44
|
-
if (!cellNode) {
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
const { colwidth, colspan = 1, rowspan = 1 } = cellNode.attrs as CellAttributes;
|
|
48
|
-
|
|
49
|
-
let mapping = tr.mapping.slice(mapStart);
|
|
50
|
-
|
|
51
|
-
// Update current node with the simple colspan
|
|
52
|
-
const baseAttrs: CellAttributes = {
|
|
53
|
-
...(cellNode.attrs as CellAttributes),
|
|
54
|
-
colspan: 1,
|
|
55
|
-
rowspan: 1,
|
|
56
|
-
};
|
|
57
|
-
// Add the new cells
|
|
58
|
-
for (let cellRowIndex = rowIndex; cellRowIndex < rowIndex + rowspan; cellRowIndex++) {
|
|
59
|
-
for (let i = 0; i < colspan; i++) {
|
|
60
|
-
const mapping = tr.mapping.slice(mapStart);
|
|
61
|
-
const cellPos = map.positionAt(cellRowIndex, column + i, table);
|
|
62
|
-
tr.insert(
|
|
63
|
-
mapping.map(cellPos + tableStart),
|
|
64
|
-
// Ignored via go/ees005
|
|
65
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
66
|
-
cellNode.type.createAndFill({
|
|
67
|
-
...baseAttrs,
|
|
68
|
-
colwidth: colwidth ? [colwidth[i]] : undefined,
|
|
69
|
-
})!,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Delete the original cell
|
|
75
|
-
mapping = tr.mapping.slice(mapStart);
|
|
76
|
-
tr.delete(
|
|
77
|
-
mapping.map(cellPos + tableStart),
|
|
78
|
-
mapping.map(cellPos + tableStart + cellNode.nodeSize),
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
// Skip rows based on rowspan
|
|
82
|
-
if (rowspan && rowspan > 1) {
|
|
83
|
-
rowIndex += rowspan - 1;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return tr;
|
|
90
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { TableLayout } from '@atlaskit/adf-schema';
|
|
2
|
-
import type { Node as PmNode } from '@atlaskit/editor-prosemirror/model';
|
|
3
|
-
|
|
4
|
-
import type { AlignmentOptions } from '../../types';
|
|
5
|
-
import { FULL_WIDTH_EDITOR_CONTENT_WIDTH } from '../table-resizing/utils/consts';
|
|
6
|
-
|
|
7
|
-
export const ALIGN_START = 'align-start';
|
|
8
|
-
export const ALIGN_CENTER = 'center';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Normalise table layout attribute an alignment value ('center' or 'align-start'), returns
|
|
12
|
-
* center if layout equals a breakout value (e.g. 'default', 'wide', 'full-width')
|
|
13
|
-
*/
|
|
14
|
-
export const normaliseAlignment = (layout: TableLayout): AlignmentOptions =>
|
|
15
|
-
layout === ALIGN_CENTER || layout === ALIGN_START ? layout : ALIGN_CENTER;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* We don't want to switch alignment in Full-width editor
|
|
19
|
-
*/
|
|
20
|
-
export const shouldChangeAlignmentToCenterResized = (
|
|
21
|
-
isTableAlignmentEnabled: boolean | undefined,
|
|
22
|
-
tableNode: PmNode,
|
|
23
|
-
lineLength: number | undefined,
|
|
24
|
-
updatedTableWidth: number,
|
|
25
|
-
) =>
|
|
26
|
-
Boolean(
|
|
27
|
-
isTableAlignmentEnabled &&
|
|
28
|
-
tableNode &&
|
|
29
|
-
tableNode.attrs.layout === ALIGN_START &&
|
|
30
|
-
lineLength &&
|
|
31
|
-
updatedTableWidth > lineLength &&
|
|
32
|
-
lineLength < FULL_WIDTH_EDITOR_CONTENT_WIDTH,
|
|
33
|
-
);
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
AnalyticsEventPayload,
|
|
5
|
-
AnalyticsEventPayloadCallback,
|
|
6
|
-
EditorAnalyticsAPI,
|
|
7
|
-
TableEventPayload,
|
|
8
|
-
} from '@atlaskit/editor-common/analytics';
|
|
9
|
-
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics';
|
|
10
|
-
import type { HigherOrderCommand } from '@atlaskit/editor-common/types';
|
|
11
|
-
import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
|
|
12
|
-
import type { Selection } from '@atlaskit/editor-prosemirror/state';
|
|
13
|
-
import { TableMap } from '@atlaskit/editor-tables/table-map';
|
|
14
|
-
import { findTable, getSelectionRect } from '@atlaskit/editor-tables/utils';
|
|
15
|
-
|
|
16
|
-
import { hasTableBeenResized } from '../table-resizing/utils/colgroup';
|
|
17
|
-
|
|
18
|
-
import { getTableWidth } from './nodes';
|
|
19
|
-
|
|
20
|
-
export function getSelectedTableInfo(selection: Selection): {
|
|
21
|
-
table: ReturnType<typeof findTable> | undefined;
|
|
22
|
-
map: TableMap | undefined;
|
|
23
|
-
totalRowCount: number;
|
|
24
|
-
totalColumnCount: number;
|
|
25
|
-
} {
|
|
26
|
-
let map;
|
|
27
|
-
let totalRowCount = 0;
|
|
28
|
-
let totalColumnCount = 0;
|
|
29
|
-
|
|
30
|
-
const table = findTable(selection);
|
|
31
|
-
if (table) {
|
|
32
|
-
map = TableMap.get(table.node);
|
|
33
|
-
totalRowCount = map.height;
|
|
34
|
-
totalColumnCount = map.width;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
table,
|
|
39
|
-
map,
|
|
40
|
-
totalRowCount,
|
|
41
|
-
totalColumnCount,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function getSelectedCellInfo(selection: Selection) {
|
|
46
|
-
let horizontalCells = 1;
|
|
47
|
-
let verticalCells = 1;
|
|
48
|
-
let totalCells = 1;
|
|
49
|
-
|
|
50
|
-
const { table, map, totalRowCount, totalColumnCount } = getSelectedTableInfo(selection);
|
|
51
|
-
|
|
52
|
-
if (table && map) {
|
|
53
|
-
const rect = getSelectionRect(selection);
|
|
54
|
-
if (rect) {
|
|
55
|
-
totalCells = map.cellsInRect(rect).length;
|
|
56
|
-
horizontalCells = rect.right - rect.left;
|
|
57
|
-
verticalCells = rect.bottom - rect.top;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return {
|
|
62
|
-
totalRowCount,
|
|
63
|
-
totalColumnCount,
|
|
64
|
-
horizontalCells,
|
|
65
|
-
verticalCells,
|
|
66
|
-
totalCells,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const withEditorAnalyticsAPI =
|
|
71
|
-
(payload: AnalyticsEventPayload | AnalyticsEventPayloadCallback) =>
|
|
72
|
-
(editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null): HigherOrderCommand => {
|
|
73
|
-
return (command) => (state, dispatch, view) =>
|
|
74
|
-
command(
|
|
75
|
-
state,
|
|
76
|
-
(tr) => {
|
|
77
|
-
const dynamicPayload = payload instanceof Function ? payload(state) : payload;
|
|
78
|
-
|
|
79
|
-
if (dynamicPayload) {
|
|
80
|
-
editorAnalyticsAPI?.attachAnalyticsEvent(dynamicPayload)(tr);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (dispatch) {
|
|
84
|
-
dispatch(tr);
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
},
|
|
88
|
-
view,
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
interface UseMeasureFramerateConfig {
|
|
93
|
-
maxSamples?: number;
|
|
94
|
-
minFrames?: number;
|
|
95
|
-
minTimeMs?: number;
|
|
96
|
-
sampleRateMs?: number;
|
|
97
|
-
timeoutMs?: number;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export const generateResizedPayload = (props: {
|
|
101
|
-
originalNode: PMNode;
|
|
102
|
-
resizedNode: PMNode;
|
|
103
|
-
}): TableEventPayload => {
|
|
104
|
-
const tableMap = TableMap.get(props.resizedNode);
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
action: TABLE_ACTION.RESIZED,
|
|
108
|
-
actionSubject: ACTION_SUBJECT.TABLE,
|
|
109
|
-
eventType: EVENT_TYPE.TRACK,
|
|
110
|
-
attributes: {
|
|
111
|
-
newWidth: props.resizedNode.attrs.width,
|
|
112
|
-
prevWidth: props.originalNode.attrs.width ?? null,
|
|
113
|
-
nodeSize: props.resizedNode.nodeSize,
|
|
114
|
-
totalTableWidth: hasTableBeenResized(props.resizedNode)
|
|
115
|
-
? getTableWidth(props.resizedNode)
|
|
116
|
-
: null,
|
|
117
|
-
totalRowCount: tableMap.height,
|
|
118
|
-
totalColumnCount: tableMap.width,
|
|
119
|
-
},
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export const reduceResizeFrameRateSamples = (frameRateSamples: number[]) => {
|
|
124
|
-
if (frameRateSamples.length > 1) {
|
|
125
|
-
const frameRateSum = frameRateSamples.reduce((sum, frameRate, index) => {
|
|
126
|
-
if (index === 0) {
|
|
127
|
-
return sum;
|
|
128
|
-
} else {
|
|
129
|
-
return sum + frameRate;
|
|
130
|
-
}
|
|
131
|
-
}, 0);
|
|
132
|
-
const averageFrameRate = Math.round(frameRateSum / (frameRateSamples.length - 1));
|
|
133
|
-
return [frameRateSamples[0], averageFrameRate];
|
|
134
|
-
} else {
|
|
135
|
-
return frameRateSamples;
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export const generateResizeFrameRatePayloads = (props: {
|
|
140
|
-
docSize: number;
|
|
141
|
-
frameRateSamples: number[];
|
|
142
|
-
originalNode: PMNode;
|
|
143
|
-
}): TableEventPayload[] => {
|
|
144
|
-
const reducedResizeFrameRateSamples = reduceResizeFrameRateSamples(props.frameRateSamples);
|
|
145
|
-
return reducedResizeFrameRateSamples.map((frameRateSample, index) => ({
|
|
146
|
-
action: TABLE_ACTION.RESIZE_PERF_SAMPLING,
|
|
147
|
-
actionSubject: ACTION_SUBJECT.TABLE,
|
|
148
|
-
eventType: EVENT_TYPE.OPERATIONAL,
|
|
149
|
-
attributes: {
|
|
150
|
-
frameRate: frameRateSample,
|
|
151
|
-
nodeSize: props.originalNode.nodeSize,
|
|
152
|
-
docSize: props.docSize,
|
|
153
|
-
isInitialSample: index === 0,
|
|
154
|
-
},
|
|
155
|
-
}));
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Measures the framerate of a component over a given time period.
|
|
160
|
-
*/
|
|
161
|
-
export const useMeasureFramerate = (config: UseMeasureFramerateConfig = {}) => {
|
|
162
|
-
const {
|
|
163
|
-
maxSamples = 10,
|
|
164
|
-
minFrames = 5,
|
|
165
|
-
minTimeMs = 500,
|
|
166
|
-
sampleRateMs = 1000,
|
|
167
|
-
timeoutMs = 200,
|
|
168
|
-
} = config;
|
|
169
|
-
|
|
170
|
-
const frameCount = useRef(0);
|
|
171
|
-
const lastTime = useRef(0);
|
|
172
|
-
const timeoutId = useRef<NodeJS.Timeout | undefined>();
|
|
173
|
-
const frameRateSamples = useRef<number[]>([]);
|
|
174
|
-
|
|
175
|
-
useEffect(() => {
|
|
176
|
-
return () => {
|
|
177
|
-
if (timeoutId.current) {
|
|
178
|
-
clearTimeout(timeoutId.current);
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
}, []);
|
|
182
|
-
|
|
183
|
-
const startMeasure = () => {
|
|
184
|
-
frameCount.current = 0;
|
|
185
|
-
lastTime.current = performance.now();
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Returns an array of frame rate samples as integers.
|
|
190
|
-
*/
|
|
191
|
-
const endMeasure = () => {
|
|
192
|
-
const samples = frameRateSamples.current;
|
|
193
|
-
frameRateSamples.current = [];
|
|
194
|
-
return samples;
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
const sampleFrameRate = (delay = 0) => {
|
|
198
|
-
const currentTime = performance.now();
|
|
199
|
-
const deltaTime = currentTime - lastTime.current - delay;
|
|
200
|
-
const isValidSample = deltaTime > minTimeMs && frameCount.current >= minFrames;
|
|
201
|
-
if (isValidSample) {
|
|
202
|
-
const frameRate = Math.round(frameCount.current / (deltaTime / 1000));
|
|
203
|
-
frameRateSamples.current.push(frameRate);
|
|
204
|
-
}
|
|
205
|
-
frameCount.current = 0;
|
|
206
|
-
lastTime.current = 0;
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Counts the number of frames that occur within a given time period. Intended to be called
|
|
211
|
-
* inside a `requestAnimationFrame` callback.
|
|
212
|
-
*/
|
|
213
|
-
const countFrames = () => {
|
|
214
|
-
if (frameRateSamples.current.length >= maxSamples && timeoutId.current) {
|
|
215
|
-
clearTimeout(timeoutId.current);
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Allows us to keep counting frames even if `startMeasure` is not called
|
|
221
|
-
*/
|
|
222
|
-
if (lastTime.current === 0) {
|
|
223
|
-
lastTime.current = performance.now();
|
|
224
|
-
}
|
|
225
|
-
frameCount.current++;
|
|
226
|
-
|
|
227
|
-
if (timeoutId.current) {
|
|
228
|
-
clearTimeout(timeoutId.current);
|
|
229
|
-
}
|
|
230
|
-
if (performance.now() - lastTime.current > sampleRateMs) {
|
|
231
|
-
sampleFrameRate();
|
|
232
|
-
} else {
|
|
233
|
-
timeoutId.current = setTimeout(() => sampleFrameRate(timeoutMs), timeoutMs);
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
return { startMeasure, endMeasure, countFrames };
|
|
238
|
-
};
|