@milkdown/preset-gfm 7.15.4 → 7.16.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 (42) hide show
  1. package/lib/index.js +11 -309
  2. package/lib/index.js.map +1 -1
  3. package/lib/node/table/command.d.ts.map +1 -1
  4. package/lib/node/table/utils/get-all-cells-in-table.d.ts.map +1 -1
  5. package/lib/node/table/utils/get-cells-in-col.d.ts.map +1 -1
  6. package/lib/node/table/utils/get-cells-in-row.d.ts.map +1 -1
  7. package/lib/node/table/utils/index.d.ts +0 -3
  8. package/lib/node/table/utils/index.d.ts.map +1 -1
  9. package/lib/tsconfig.tsbuildinfo +1 -1
  10. package/package.json +8 -9
  11. package/src/node/table/command.ts +12 -36
  12. package/src/node/table/utils/get-all-cells-in-table.ts +1 -3
  13. package/src/node/table/utils/get-cells-in-col.ts +1 -3
  14. package/src/node/table/utils/get-cells-in-row.ts +1 -3
  15. package/src/node/table/utils/index.ts +0 -3
  16. package/lib/node/table/utils/convert-rows-to-table.d.ts +0 -3
  17. package/lib/node/table/utils/convert-rows-to-table.d.ts.map +0 -1
  18. package/lib/node/table/utils/convert-table-to-rows.d.ts +0 -3
  19. package/lib/node/table/utils/convert-table-to-rows.d.ts.map +0 -1
  20. package/lib/node/table/utils/find-table.d.ts +0 -3
  21. package/lib/node/table/utils/find-table.d.ts.map +0 -1
  22. package/lib/node/table/utils/get-selection-range-in-col.d.ts +0 -4
  23. package/lib/node/table/utils/get-selection-range-in-col.d.ts.map +0 -1
  24. package/lib/node/table/utils/get-selection-range-in-row.d.ts +0 -4
  25. package/lib/node/table/utils/get-selection-range-in-row.d.ts.map +0 -1
  26. package/lib/node/table/utils/move-col.d.ts +0 -10
  27. package/lib/node/table/utils/move-col.d.ts.map +0 -1
  28. package/lib/node/table/utils/move-row-in-array-of-rows.d.ts +0 -3
  29. package/lib/node/table/utils/move-row-in-array-of-rows.d.ts.map +0 -1
  30. package/lib/node/table/utils/move-row.d.ts +0 -10
  31. package/lib/node/table/utils/move-row.d.ts.map +0 -1
  32. package/lib/node/table/utils/transpose.d.ts +0 -2
  33. package/lib/node/table/utils/transpose.d.ts.map +0 -1
  34. package/src/node/table/utils/convert-rows-to-table.ts +0 -43
  35. package/src/node/table/utils/convert-table-to-rows.ts +0 -65
  36. package/src/node/table/utils/find-table.ts +0 -10
  37. package/src/node/table/utils/get-selection-range-in-col.ts +0 -86
  38. package/src/node/table/utils/get-selection-range-in-row.ts +0 -86
  39. package/src/node/table/utils/move-col.ts +0 -73
  40. package/src/node/table/utils/move-row-in-array-of-rows.ts +0 -29
  41. package/src/node/table/utils/move-row.ts +0 -71
  42. package/src/node/table/utils/transpose.ts +0 -26
@@ -12,6 +12,8 @@ import {
12
12
  isInTable,
13
13
  selectedRect,
14
14
  setCellAttr,
15
+ moveTableRow,
16
+ moveTableColumn,
15
17
  } from '@milkdown/prose/tables'
16
18
  import { $command } from '@milkdown/utils'
17
19
 
@@ -20,8 +22,6 @@ import { tableSchema } from './schema'
20
22
  import {
21
23
  addRowWithAlignment,
22
24
  createTable,
23
- moveCol,
24
- moveRow,
25
25
  selectCol,
26
26
  selectRow,
27
27
  selectTable,
@@ -110,23 +110,11 @@ export const moveRowCommand = $command(
110
110
  'MoveRow',
111
111
  () =>
112
112
  ({ from, to, pos }: { from?: number; to?: number; pos?: number } = {}) =>
113
- (state, dispatch) => {
114
- const { tr } = state
115
- if (
116
- moveRow({
117
- tr,
118
- origin: from ?? 0,
119
- target: to ?? 0,
120
- pos: pos ?? state.selection.from,
121
- select: true,
122
- })
123
- ) {
124
- dispatch?.(tr)
125
- return true
126
- }
127
-
128
- return false
129
- }
113
+ moveTableRow({
114
+ from: from ?? 0,
115
+ to: to ?? 0,
116
+ pos,
117
+ })
130
118
  )
131
119
 
132
120
  withMeta(moveRowCommand, {
@@ -140,23 +128,11 @@ export const moveColCommand = $command(
140
128
  'MoveCol',
141
129
  () =>
142
130
  ({ from, to, pos }: { from?: number; to?: number; pos?: number } = {}) =>
143
- (state, dispatch) => {
144
- const { tr } = state
145
- if (
146
- moveCol({
147
- tr,
148
- origin: from ?? 0,
149
- target: to ?? 0,
150
- pos: pos ?? state.selection.from,
151
- select: true,
152
- })
153
- ) {
154
- dispatch?.(tr)
155
- return true
156
- }
157
-
158
- return false
159
- }
131
+ moveTableColumn({
132
+ from: from ?? 0,
133
+ to: to ?? 0,
134
+ pos,
135
+ })
160
136
  )
161
137
 
162
138
  withMeta(moveColCommand, {
@@ -1,8 +1,6 @@
1
1
  import type { Selection } from '@milkdown/prose/state'
2
2
 
3
- import { TableMap } from '@milkdown/prose/tables'
4
-
5
- import { findTable } from './find-table'
3
+ import { findTable, TableMap } from '@milkdown/prose/tables'
6
4
 
7
5
  /// Get all cells in a table.
8
6
  export function getAllCellsInTable(selection: Selection) {
@@ -1,11 +1,9 @@
1
1
  import type { Selection } from '@milkdown/prose/state'
2
2
 
3
- import { TableMap } from '@milkdown/prose/tables'
3
+ import { findTable, TableMap } from '@milkdown/prose/tables'
4
4
 
5
5
  import type { CellPos } from './types'
6
6
 
7
- import { findTable } from './find-table'
8
-
9
7
  /// Get cells in a column of a table.
10
8
  export function getCellsInCol(
11
9
  columnIndexes: number | number[],
@@ -1,11 +1,9 @@
1
1
  import type { Selection } from '@milkdown/prose/state'
2
2
 
3
- import { TableMap } from '@milkdown/prose/tables'
3
+ import { findTable, TableMap } from '@milkdown/prose/tables'
4
4
 
5
5
  import type { CellPos } from './types'
6
6
 
7
- import { findTable } from './find-table'
8
-
9
7
  /// Get cells in a row of a table.
10
8
  export function getCellsInRow(
11
9
  rowIndex: number | number[],
@@ -1,11 +1,8 @@
1
1
  export * from './create-table'
2
- export * from './find-table'
3
2
  export * from './get-cells-in-col'
4
3
  export * from './get-cells-in-row'
5
4
  export * from './select-line'
6
5
  export * from './types'
7
6
  export * from './add-row-with-alignment'
8
- export * from './move-row'
9
- export * from './move-col'
10
7
  export * from './select-table'
11
8
  export * from './get-all-cells-in-table'
@@ -1,3 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model';
2
- export declare function convertRowsToTable(tableNode: Node, arrayOfNodes: (Node | null)[][]): Node;
3
- //# sourceMappingURL=convert-rows-to-table.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"convert-rows-to-table.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/convert-rows-to-table.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAOjD,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,IAAI,EACf,YAAY,EAAE,CAAC,IAAI,GAAG,IAAI,CAAC,EAAE,EAAE,QAiChC"}
@@ -1,3 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model';
2
- export declare function convertTableToRows(tableNode: Node): (Node | null)[][];
3
- //# sourceMappingURL=convert-table-to-rows.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"convert-table-to-rows.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/convert-table-to-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AA4BjD,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,IAAI,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,EAAE,EAAE,CAoCrE"}
@@ -1,3 +0,0 @@
1
- import type { ResolvedPos } from '@milkdown/prose/model';
2
- export declare function findTable($pos: ResolvedPos): import("@milkdown/prose").ContentNodeWithPos | undefined;
3
- //# sourceMappingURL=find-table.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"find-table.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/find-table.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAKxD,wBAAgB,SAAS,CAAC,IAAI,EAAE,WAAW,4DAI1C"}
@@ -1,4 +0,0 @@
1
- import type { Transaction } from '@milkdown/prose/state';
2
- import type { CellSelectionRange } from './types';
3
- export declare function getSelectionRangeInCol(tr: Transaction, startColIndex: number, endColIndex?: number): CellSelectionRange | undefined;
4
- //# sourceMappingURL=get-selection-range-in-col.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-selection-range-in-col.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/get-selection-range-in-col.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAExD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAKjD,wBAAgB,sBAAsB,CACpC,EAAE,EAAE,WAAW,EACf,aAAa,EAAE,MAAM,EACrB,WAAW,GAAE,MAAsB,GAClC,kBAAkB,GAAG,SAAS,CA0EhC"}
@@ -1,4 +0,0 @@
1
- import type { Transaction } from '@milkdown/prose/state';
2
- import type { CellSelectionRange } from './types';
3
- export declare function getSelectionRangeInRow(tr: Transaction, startRowIndex: number, endRowIndex?: number): CellSelectionRange | undefined;
4
- //# sourceMappingURL=get-selection-range-in-row.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-selection-range-in-row.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/get-selection-range-in-row.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAExD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAKjD,wBAAgB,sBAAsB,CACpC,EAAE,EAAE,WAAW,EACf,aAAa,EAAE,MAAM,EACrB,WAAW,GAAE,MAAsB,GAClC,kBAAkB,GAAG,SAAS,CA0EhC"}
@@ -1,10 +0,0 @@
1
- import type { Transaction } from '@milkdown/prose/state';
2
- export interface MoveColParams {
3
- tr: Transaction;
4
- origin: number;
5
- target: number;
6
- pos: number;
7
- select?: boolean;
8
- }
9
- export declare function moveCol(moveColParams: MoveColParams): boolean;
10
- //# sourceMappingURL=move-col.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"move-col.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/move-col.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAWxD,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,WAAW,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAKD,wBAAgB,OAAO,CAAC,aAAa,EAAE,aAAa,WAmCnD"}
@@ -1,3 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model';
2
- export declare function moveRowInArrayOfRows(rows: (Node | null)[][], indexesOrigin: number[], indexesTarget: number[], directionOverride: -1 | 1 | 0): (Node | null)[][];
3
- //# sourceMappingURL=move-row-in-array-of-rows.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"move-row-in-array-of-rows.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/move-row-in-array-of-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAGjD,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,CAAC,IAAI,GAAG,IAAI,CAAC,EAAE,EAAE,EACvB,aAAa,EAAE,MAAM,EAAE,EACvB,aAAa,EAAE,MAAM,EAAE,EACvB,iBAAiB,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,qBAqB9B"}
@@ -1,10 +0,0 @@
1
- import type { Transaction } from '@milkdown/prose/state';
2
- export interface MoveRowParams {
3
- tr: Transaction;
4
- origin: number;
5
- target: number;
6
- pos: number;
7
- select?: boolean;
8
- }
9
- export declare function moveRow(moveRowParams: MoveRowParams): boolean;
10
- //# sourceMappingURL=move-row.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"move-row.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/move-row.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAUxD,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,WAAW,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAKD,wBAAgB,OAAO,CAAC,aAAa,EAAE,aAAa,WAmCnD"}
@@ -1,2 +0,0 @@
1
- export declare function transpose<T>(array: T[][]): T[][];
2
- //# sourceMappingURL=transpose.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transpose.d.ts","sourceRoot":"","sources":["../../../../src/node/table/utils/transpose.ts"],"names":[],"mappings":"AAuBA,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,GAC6B,CAAC,EAAE,EAAE,CAC1E"}
@@ -1,43 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model'
2
-
3
- import { TableMap } from '@milkdown/prose/tables'
4
-
5
- /// @internal
6
- ///
7
- /// Convert an array of rows to a table node.
8
- export function convertRowsToTable(
9
- tableNode: Node,
10
- arrayOfNodes: (Node | null)[][]
11
- ) {
12
- const rowsPM = []
13
- const map = TableMap.get(tableNode)
14
- for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
15
- const row = tableNode.child(rowIndex)
16
- const rowCells = []
17
-
18
- for (let colIndex = 0; colIndex < map.width; colIndex++) {
19
- if (!arrayOfNodes[rowIndex]![colIndex]) continue
20
-
21
- const cellPos = map.map[rowIndex * map.width + colIndex]!
22
-
23
- const cell = arrayOfNodes[rowIndex]![colIndex]!
24
- const oldCell = tableNode.nodeAt(cellPos)!
25
- const newCell = oldCell.type.createChecked(
26
- Object.assign({}, cell.attrs),
27
- cell.content,
28
- cell.marks
29
- )
30
- rowCells.push(newCell)
31
- }
32
-
33
- rowsPM.push(row.type.createChecked(row.attrs, rowCells, row.marks))
34
- }
35
-
36
- const newTable = tableNode.type.createChecked(
37
- tableNode.attrs,
38
- rowsPM,
39
- tableNode.marks
40
- )
41
-
42
- return newTable
43
- }
@@ -1,65 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model'
2
-
3
- import { TableMap } from '@milkdown/prose/tables'
4
-
5
- /// @internal
6
- ///
7
- /// This function will transform the table node into a matrix of rows and columns
8
- /// respecting merged cells, for example this table:
9
- ///
10
- /// ```
11
- /// ┌──────┬──────┬─────────────┐
12
- /// │ A1 │ B1 │ C1 │
13
- /// ├──────┼──────┴──────┬──────┤
14
- /// │ A2 │ B2 │ │
15
- /// ├──────┼─────────────┤ D1 │
16
- /// │ A3 │ B3 │ C3 │ │
17
- /// └──────┴──────┴──────┴──────┘
18
- /// ```
19
- ///
20
- /// will be converted to the below:
21
- ///
22
- /// ```javascript
23
- /// [
24
- /// [A1, B1, C1, null],
25
- /// [A2, B2, null, D1],
26
- /// [A3, B3, C3, null],
27
- /// ]
28
- /// ```
29
- export function convertTableToRows(tableNode: Node): (Node | null)[][] {
30
- const map = TableMap.get(tableNode)
31
- const rows: (Node | null)[][] = []
32
- const rowCount = map.height
33
- const colCount = map.width
34
- for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
35
- const row: (Node | null)[] = []
36
- for (let colIndex = 0; colIndex < colCount; colIndex++) {
37
- let cellIndex = rowIndex * colCount + colIndex
38
- let cellPos = map.map[cellIndex]
39
- if (rowIndex > 0) {
40
- const topCellIndex = cellIndex - colCount
41
- const topCellPos = map.map[topCellIndex]
42
- if (cellPos === topCellPos) {
43
- row.push(null)
44
- continue
45
- }
46
- }
47
- if (colIndex > 0) {
48
- const leftCellIndex = cellIndex - 1
49
- const leftCellPos = map.map[leftCellIndex]
50
- if (cellPos === leftCellPos) {
51
- row.push(null)
52
- continue
53
- }
54
- }
55
- if (!cellPos) {
56
- row.push(null)
57
- } else {
58
- row.push(tableNode.nodeAt(cellPos))
59
- }
60
- }
61
- rows.push(row)
62
- }
63
-
64
- return rows
65
- }
@@ -1,10 +0,0 @@
1
- import type { ResolvedPos } from '@milkdown/prose/model'
2
-
3
- import { findParentNodeClosestToPos } from '@milkdown/prose'
4
-
5
- /// Find the table node with position information for target pos.
6
- export function findTable($pos: ResolvedPos) {
7
- return findParentNodeClosestToPos(
8
- (node) => node.type.spec.tableRole === 'table'
9
- )($pos)
10
- }
@@ -1,86 +0,0 @@
1
- import type { Transaction } from '@milkdown/prose/state'
2
-
3
- import type { CellSelectionRange } from './types'
4
-
5
- import { getCellsInCol } from './get-cells-in-col'
6
- import { getCellsInRow } from './get-cells-in-row'
7
-
8
- export function getSelectionRangeInCol(
9
- tr: Transaction,
10
- startColIndex: number,
11
- endColIndex: number = startColIndex
12
- ): CellSelectionRange | undefined {
13
- let startIndex = startColIndex
14
- let endIndex = endColIndex
15
-
16
- // looking for selection start column (startIndex)
17
- for (let i = startColIndex; i >= 0; i--) {
18
- const cells = getCellsInCol(i, tr.selection)
19
- if (cells) {
20
- cells.forEach((cell) => {
21
- const maybeEndIndex = cell.node.attrs.colspan + i - 1
22
- if (maybeEndIndex >= startIndex) {
23
- startIndex = i
24
- }
25
- if (maybeEndIndex > endIndex) {
26
- endIndex = maybeEndIndex
27
- }
28
- })
29
- }
30
- }
31
- // looking for selection end column (endIndex)
32
- for (let i = startColIndex; i <= endIndex; i++) {
33
- const cells = getCellsInCol(i, tr.selection)
34
- if (cells) {
35
- cells.forEach((cell) => {
36
- const maybeEndIndex = cell.node.attrs.colspan + i - 1
37
- if (cell.node.attrs.colspan > 1 && maybeEndIndex > endIndex) {
38
- endIndex = maybeEndIndex
39
- }
40
- })
41
- }
42
- }
43
-
44
- // filter out columns without cells (where all rows have colspan > 1 in the same column)
45
- const indexes = []
46
- for (let i = startIndex; i <= endIndex; i++) {
47
- const maybeCells = getCellsInCol(i, tr.selection)
48
- if (maybeCells && maybeCells.length > 0) {
49
- indexes.push(i)
50
- }
51
- }
52
- startIndex = indexes[0]!
53
- endIndex = indexes[indexes.length - 1]!
54
-
55
- const firstSelectedColumnCells = getCellsInCol(startIndex, tr.selection)
56
- const firstRowCells = getCellsInRow(0, tr.selection)
57
- if (!firstSelectedColumnCells || !firstRowCells) {
58
- return
59
- }
60
-
61
- const $anchor = tr.doc.resolve(
62
- firstSelectedColumnCells[firstSelectedColumnCells.length - 1]!.pos
63
- )
64
-
65
- let headCell
66
- for (let i = endIndex; i >= startIndex; i--) {
67
- const columnCells = getCellsInCol(i, tr.selection)
68
- if (columnCells && columnCells.length > 0) {
69
- for (let j = firstRowCells.length - 1; j >= 0; j--) {
70
- if (firstRowCells[j]!.pos === columnCells[0]!.pos) {
71
- headCell = columnCells[0]
72
- break
73
- }
74
- }
75
- if (headCell) {
76
- break
77
- }
78
- }
79
- }
80
- if (!headCell) {
81
- return
82
- }
83
-
84
- const $head = tr.doc.resolve(headCell.pos)
85
- return { $anchor, $head, indexes }
86
- }
@@ -1,86 +0,0 @@
1
- import type { Transaction } from '@milkdown/prose/state'
2
-
3
- import type { CellSelectionRange } from './types'
4
-
5
- import { getCellsInCol } from './get-cells-in-col'
6
- import { getCellsInRow } from './get-cells-in-row'
7
-
8
- export function getSelectionRangeInRow(
9
- tr: Transaction,
10
- startRowIndex: number,
11
- endRowIndex: number = startRowIndex
12
- ): CellSelectionRange | undefined {
13
- let startIndex = startRowIndex
14
- let endIndex = endRowIndex
15
-
16
- // looking for selection start row (startIndex)
17
- for (let i = startRowIndex; i >= 0; i--) {
18
- const cells = getCellsInRow(i, tr.selection)
19
- if (cells) {
20
- cells.forEach((cell) => {
21
- const maybeEndIndex = cell.node.attrs.rowspan + i - 1
22
- if (maybeEndIndex >= startIndex) {
23
- startIndex = i
24
- }
25
- if (maybeEndIndex > endIndex) {
26
- endIndex = maybeEndIndex
27
- }
28
- })
29
- }
30
- }
31
- // looking for selection end row (endIndex)
32
- for (let i = startRowIndex; i <= endIndex; i++) {
33
- const cells = getCellsInRow(i, tr.selection)
34
- if (cells) {
35
- cells.forEach((cell) => {
36
- const maybeEndIndex = cell.node.attrs.rowspan + i - 1
37
- if (cell.node.attrs.rowspan > 1 && maybeEndIndex > endIndex) {
38
- endIndex = maybeEndIndex
39
- }
40
- })
41
- }
42
- }
43
-
44
- // filter out rows without cells (where all columns have rowspan > 1 in the same row)
45
- const indexes = []
46
- for (let i = startIndex; i <= endIndex; i++) {
47
- const maybeCells = getCellsInRow(i, tr.selection)
48
- if (maybeCells && maybeCells.length > 0) {
49
- indexes.push(i)
50
- }
51
- }
52
- startIndex = indexes[0]!
53
- endIndex = indexes[indexes.length - 1]!
54
-
55
- const firstSelectedRowCells = getCellsInRow(startIndex, tr.selection)
56
- const firstColumnCells = getCellsInCol(0, tr.selection)
57
- if (!firstSelectedRowCells || !firstColumnCells) {
58
- return
59
- }
60
-
61
- const $anchor = tr.doc.resolve(
62
- firstSelectedRowCells[firstSelectedRowCells.length - 1]!.pos
63
- )
64
-
65
- let headCell
66
- for (let i = endIndex; i >= startIndex; i--) {
67
- const rowCells = getCellsInRow(i, tr.selection)
68
- if (rowCells && rowCells.length > 0) {
69
- for (let j = firstColumnCells.length - 1; j >= 0; j--) {
70
- if (firstColumnCells[j]!.pos === rowCells[0]!.pos) {
71
- headCell = rowCells[0]
72
- break
73
- }
74
- }
75
- if (headCell) {
76
- break
77
- }
78
- }
79
- }
80
- if (!headCell) {
81
- return
82
- }
83
-
84
- const $head = tr.doc.resolve(headCell.pos)
85
- return { $anchor, $head, indexes }
86
- }
@@ -1,73 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model'
2
- import type { Transaction } from '@milkdown/prose/state'
3
-
4
- import { CellSelection, TableMap } from '@milkdown/prose/tables'
5
-
6
- import { convertRowsToTable } from './convert-rows-to-table'
7
- import { convertTableToRows } from './convert-table-to-rows'
8
- import { findTable } from './find-table'
9
- import { getSelectionRangeInCol } from './get-selection-range-in-col'
10
- import { moveRowInArrayOfRows } from './move-row-in-array-of-rows'
11
- import { transpose } from './transpose'
12
-
13
- export interface MoveColParams {
14
- tr: Transaction
15
- origin: number
16
- target: number
17
- pos: number
18
- select?: boolean
19
- }
20
-
21
- /// If the selection is in a table,
22
- /// Move the columns at `origin` to `target` in current table.
23
- /// The `select` is true by default, which means the selection will be set to the moved column.
24
- export function moveCol(moveColParams: MoveColParams) {
25
- const { tr, origin, target, select, pos } = moveColParams
26
- const $pos = tr.doc.resolve(pos)
27
- const table = findTable($pos)
28
- if (!table) return false
29
-
30
- const indexesOriginColumn = getSelectionRangeInCol(tr, origin)?.indexes
31
- const indexesTargetColumn = getSelectionRangeInCol(tr, target)?.indexes
32
-
33
- if (!indexesOriginColumn || !indexesTargetColumn) return false
34
-
35
- if (indexesOriginColumn.includes(target)) return false
36
-
37
- const newTable = moveTableCol(
38
- table.node,
39
- indexesOriginColumn,
40
- indexesTargetColumn,
41
- 0
42
- )
43
-
44
- tr.replaceWith(table.pos, table.pos + table.node.nodeSize, newTable)
45
-
46
- if (!select) return true
47
-
48
- const map = TableMap.get(newTable)
49
- const start = table.start
50
- const index = target
51
- const lastCell = map.positionAt(map.height - 1, index, newTable)
52
- const $lastCell = tr.doc.resolve(start + lastCell)
53
-
54
- const firstCell = map.positionAt(0, index, newTable)
55
- const $firstCell = tr.doc.resolve(start + firstCell)
56
-
57
- tr.setSelection(CellSelection.colSelection($lastCell, $firstCell))
58
- return true
59
- }
60
-
61
- function moveTableCol(
62
- table: Node,
63
- indexesOrigin: number[],
64
- indexesTarget: number[],
65
- direction: -1 | 1 | 0
66
- ) {
67
- let rows = transpose(convertTableToRows(table))
68
-
69
- rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget, direction)
70
- rows = transpose(rows)
71
-
72
- return convertRowsToTable(table, rows)
73
- }
@@ -1,29 +0,0 @@
1
- import type { Node } from '@milkdown/prose/model'
2
-
3
- /// @internal
4
- export function moveRowInArrayOfRows(
5
- rows: (Node | null)[][],
6
- indexesOrigin: number[],
7
- indexesTarget: number[],
8
- directionOverride: -1 | 1 | 0
9
- ) {
10
- const direction = indexesOrigin[0]! > indexesTarget[0]! ? -1 : 1
11
-
12
- const rowsExtracted = rows.splice(indexesOrigin[0]!, indexesOrigin.length)
13
- const positionOffset = rowsExtracted.length % 2 === 0 ? 1 : 0
14
- let target: number
15
-
16
- if (directionOverride === -1 && direction === 1) {
17
- target = indexesTarget[0]! - 1
18
- } else if (directionOverride === 1 && direction === -1) {
19
- target = indexesTarget[indexesTarget.length - 1]! - positionOffset + 1
20
- } else {
21
- target =
22
- direction === -1
23
- ? indexesTarget[0]!
24
- : indexesTarget[indexesTarget.length - 1]! - positionOffset
25
- }
26
-
27
- rows.splice(target, 0, ...rowsExtracted)
28
- return rows
29
- }