@milkdown/preset-gfm 7.15.0 → 7.15.2

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 (65) hide show
  1. package/lib/index.js +265 -205
  2. package/lib/index.js.map +1 -1
  3. package/lib/mark/strike-through.d.ts.map +1 -1
  4. package/lib/node/table/command.d.ts.map +1 -1
  5. package/lib/node/table/utils/add-row-with-alignment.d.ts +5 -0
  6. package/lib/node/table/utils/add-row-with-alignment.d.ts.map +1 -0
  7. package/lib/node/table/utils/convert-rows-to-table.d.ts +3 -0
  8. package/lib/node/table/utils/convert-rows-to-table.d.ts.map +1 -0
  9. package/lib/node/table/utils/convert-table-to-rows.d.ts +3 -0
  10. package/lib/node/table/utils/convert-table-to-rows.d.ts.map +1 -0
  11. package/lib/node/table/utils/create-table.d.ts +4 -0
  12. package/lib/node/table/utils/create-table.d.ts.map +1 -0
  13. package/lib/node/table/utils/find-table.d.ts +3 -0
  14. package/lib/node/table/utils/find-table.d.ts.map +1 -0
  15. package/lib/node/table/utils/get-all-cells-in-table.d.ts +7 -0
  16. package/lib/node/table/utils/get-all-cells-in-table.d.ts.map +1 -0
  17. package/lib/node/table/utils/get-cells-in-col.d.ts +4 -0
  18. package/lib/node/table/utils/get-cells-in-col.d.ts.map +1 -0
  19. package/lib/node/table/utils/get-cells-in-row.d.ts +4 -0
  20. package/lib/node/table/utils/get-cells-in-row.d.ts.map +1 -0
  21. package/lib/node/table/utils/get-selection-range-in-col.d.ts +4 -0
  22. package/lib/node/table/utils/get-selection-range-in-col.d.ts.map +1 -0
  23. package/lib/node/table/utils/get-selection-range-in-row.d.ts +4 -0
  24. package/lib/node/table/utils/get-selection-range-in-row.d.ts.map +1 -0
  25. package/lib/node/table/utils/index.d.ts +12 -0
  26. package/lib/node/table/utils/index.d.ts.map +1 -0
  27. package/lib/node/table/utils/move-col.d.ts +10 -0
  28. package/lib/node/table/utils/move-col.d.ts.map +1 -0
  29. package/lib/node/table/utils/move-row-in-array-of-rows.d.ts +3 -0
  30. package/lib/node/table/utils/move-row-in-array-of-rows.d.ts.map +1 -0
  31. package/lib/node/table/utils/move-row.d.ts +10 -0
  32. package/lib/node/table/utils/move-row.d.ts.map +1 -0
  33. package/lib/node/table/utils/select-line.d.ts +5 -0
  34. package/lib/node/table/utils/select-line.d.ts.map +1 -0
  35. package/lib/node/table/utils/select-table.d.ts +3 -0
  36. package/lib/node/table/utils/select-table.d.ts.map +1 -0
  37. package/lib/node/table/utils/transpose.d.ts +2 -0
  38. package/lib/node/table/utils/transpose.d.ts.map +1 -0
  39. package/lib/node/table/utils/types.d.ts +12 -0
  40. package/lib/node/table/utils/types.d.ts.map +1 -0
  41. package/lib/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +8 -8
  43. package/src/mark/strike-through.ts +4 -1
  44. package/src/node/table/command.ts +28 -10
  45. package/src/node/table/utils/add-row-with-alignment.ts +32 -0
  46. package/src/node/table/utils/convert-rows-to-table.ts +43 -0
  47. package/src/node/table/utils/convert-table-to-rows.ts +65 -0
  48. package/src/node/table/utils/create-table.ts +31 -0
  49. package/src/node/table/utils/find-table.ts +10 -0
  50. package/src/node/table/utils/get-all-cells-in-table.ts +24 -0
  51. package/src/node/table/utils/get-cells-in-col.ts +35 -0
  52. package/src/node/table/utils/get-cells-in-row.ts +37 -0
  53. package/src/node/table/utils/get-selection-range-in-col.ts +86 -0
  54. package/src/node/table/utils/get-selection-range-in-row.ts +86 -0
  55. package/src/node/table/utils/index.ts +11 -0
  56. package/src/node/table/utils/move-col.ts +73 -0
  57. package/src/node/table/utils/move-row-in-array-of-rows.ts +29 -0
  58. package/src/node/table/utils/move-row.ts +71 -0
  59. package/src/node/table/utils/select-line.ts +61 -0
  60. package/src/node/table/utils/select-table.ts +20 -0
  61. package/src/node/table/utils/transpose.ts +26 -0
  62. package/src/node/table/utils/types.ts +16 -0
  63. package/lib/node/table/utils.d.ts +0 -41
  64. package/lib/node/table/utils.d.ts.map +0 -1
  65. package/src/node/table/utils.ts +0 -564
@@ -0,0 +1,71 @@
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 { getSelectionRangeInRow } from './get-selection-range-in-row'
10
+ import { moveRowInArrayOfRows } from './move-row-in-array-of-rows'
11
+
12
+ export interface MoveRowParams {
13
+ tr: Transaction
14
+ origin: number
15
+ target: number
16
+ pos: number
17
+ select?: boolean
18
+ }
19
+
20
+ /// If the selection is in a table,
21
+ /// Move the rows at `origin` and `target` in current table.
22
+ /// The `select` is true by default, which means the selection will be set to the moved row.
23
+ export function moveRow(moveRowParams: MoveRowParams) {
24
+ const { tr, origin, target, select, pos } = moveRowParams
25
+ const $pos = tr.doc.resolve(pos)
26
+ const table = findTable($pos)
27
+ if (!table) return false
28
+
29
+ const indexesOriginRow = getSelectionRangeInRow(tr, origin)?.indexes
30
+ const indexesTargetRow = getSelectionRangeInRow(tr, target)?.indexes
31
+
32
+ if (!indexesOriginRow || !indexesTargetRow) return false
33
+
34
+ if (indexesOriginRow.includes(target)) return false
35
+
36
+ const newTable = moveTableRow(
37
+ table.node,
38
+ indexesOriginRow,
39
+ indexesTargetRow,
40
+ 0
41
+ )
42
+
43
+ tr.replaceWith(table.pos, table.pos + table.node.nodeSize, newTable)
44
+
45
+ if (!select) return true
46
+
47
+ const map = TableMap.get(newTable)
48
+ const start = table.start
49
+ const index = target
50
+ const lastCell = map.positionAt(index, map.width - 1, newTable)
51
+ const $lastCell = tr.doc.resolve(start + lastCell)
52
+
53
+ const firstCell = map.positionAt(index, 0, newTable)
54
+ const $firstCell = tr.doc.resolve(start + firstCell)
55
+
56
+ tr.setSelection(CellSelection.rowSelection($lastCell, $firstCell))
57
+ return true
58
+ }
59
+
60
+ function moveTableRow(
61
+ table: Node,
62
+ indexesOrigin: number[],
63
+ indexesTarget: number[],
64
+ direction: -1 | 1 | 0
65
+ ) {
66
+ let rows = convertTableToRows(table)
67
+
68
+ rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget, direction)
69
+
70
+ return convertRowsToTable(table, rows)
71
+ }
@@ -0,0 +1,61 @@
1
+ import type { Selection, Transaction } from '@milkdown/prose/state'
2
+
3
+ import { cloneTr, findParentNodeClosestToPos } from '@milkdown/prose'
4
+ import { CellSelection, TableMap } from '@milkdown/prose/tables'
5
+
6
+ /// @internal
7
+ export function selectLine(type: 'row' | 'col') {
8
+ return (index: number, pos?: number) => (tr: Transaction) => {
9
+ pos = pos ?? tr.selection.from
10
+ const $pos = tr.doc.resolve(pos)
11
+ const $node = findParentNodeClosestToPos(
12
+ (node) => node.type.name === 'table'
13
+ )($pos)
14
+ const table = $node
15
+ ? {
16
+ node: $node.node,
17
+ from: $node.start,
18
+ }
19
+ : undefined
20
+
21
+ const isRowSelection = type === 'row'
22
+ if (table) {
23
+ const map = TableMap.get(table.node)
24
+
25
+ // Check if the index is valid
26
+ if (index >= 0 && index < (isRowSelection ? map.height : map.width)) {
27
+ const lastCell = map.positionAt(
28
+ isRowSelection ? index : map.height - 1,
29
+ isRowSelection ? map.width - 1 : index,
30
+ table.node
31
+ )
32
+ const $lastCell = tr.doc.resolve(table.from + lastCell)
33
+
34
+ const createCellSelection = isRowSelection
35
+ ? CellSelection.rowSelection
36
+ : CellSelection.colSelection
37
+
38
+ const firstCell = map.positionAt(
39
+ isRowSelection ? index : 0,
40
+ isRowSelection ? 0 : index,
41
+ table.node
42
+ )
43
+ const $firstCell = tr.doc.resolve(table.from + firstCell)
44
+ return cloneTr(
45
+ tr.setSelection(
46
+ createCellSelection($lastCell, $firstCell) as unknown as Selection
47
+ )
48
+ )
49
+ }
50
+ }
51
+ return tr
52
+ }
53
+ }
54
+
55
+ /// If the selection is in a table,
56
+ /// select the {index} row.
57
+ export const selectRow = selectLine('row')
58
+
59
+ /// If the selection is in a table,
60
+ /// select the {index} column.
61
+ export const selectCol = selectLine('col')
@@ -0,0 +1,20 @@
1
+ import type { Transaction } from '@milkdown/prose/state'
2
+
3
+ import { cloneTr } from '@milkdown/prose'
4
+ import { CellSelection } from '@milkdown/prose/tables'
5
+
6
+ import { getAllCellsInTable } from './get-all-cells-in-table'
7
+
8
+ /// Select a possible table in current selection.
9
+ export function selectTable(tr: Transaction) {
10
+ const cells = getAllCellsInTable(tr.selection)
11
+ if (cells && cells[0]) {
12
+ const $firstCell = tr.doc.resolve(cells[0].pos)
13
+ const last = cells[cells.length - 1]
14
+ if (last) {
15
+ const $lastCell = tr.doc.resolve(last.pos)
16
+ return cloneTr(tr.setSelection(new CellSelection($lastCell, $firstCell)))
17
+ }
18
+ }
19
+ return tr
20
+ }
@@ -0,0 +1,26 @@
1
+ /// @internal
2
+ ///
3
+ /// Transposes a 2D array by flipping columns to rows.
4
+ ///
5
+ /// Transposition is a familiar algebra concept where the matrix is flipped
6
+ /// along its diagonal. For more details, see:
7
+ /// https://en.wikipedia.org/wiki/Transpose
8
+ ///
9
+ /// ```typescript
10
+ /// const arr = [
11
+ /// ['a1', 'a2', 'a3'],
12
+ /// ['b1', 'b2', 'b3'],
13
+ /// ['c1', 'c2', 'c3'],
14
+ /// ['d1', 'd2', 'd3'],
15
+ /// ];
16
+ ///
17
+ /// const result = transpose(arr);
18
+ /// result === [
19
+ /// ['a1', 'b1', 'c1', 'd1'],
20
+ /// ['a2', 'b2', 'c2', 'd2'],
21
+ /// ['a3', 'b3', 'c3', 'd3'],
22
+ /// ]
23
+ /// ```
24
+ export function transpose<T>(array: T[][]) {
25
+ return array[0]!.map((_, i) => array.map((column) => column[i])) as T[][]
26
+ }
@@ -0,0 +1,16 @@
1
+ import type { Node, ResolvedPos } from '@milkdown/prose/model'
2
+
3
+ /// @internal
4
+ export interface CellPos {
5
+ pos: number
6
+ start: number
7
+ node: Node
8
+ }
9
+
10
+ /// @internal
11
+ export type CellSelectionRange = {
12
+ $anchor: ResolvedPos
13
+ $head: ResolvedPos
14
+ // an array of column/row indexes
15
+ indexes: number[]
16
+ }
@@ -1,41 +0,0 @@
1
- import type { Ctx } from '@milkdown/ctx';
2
- import type { ContentNodeWithPos } from '@milkdown/prose';
3
- import type { Node, ResolvedPos } from '@milkdown/prose/model';
4
- import type { Selection, Transaction } from '@milkdown/prose/state';
5
- import type { TableRect } from '@milkdown/prose/tables';
6
- export interface CellPos {
7
- pos: number;
8
- start: number;
9
- node: Node;
10
- }
11
- export declare function createTable(ctx: Ctx, rowsCount?: number, colsCount?: number): Node;
12
- export declare function findTable($pos: ResolvedPos): ContentNodeWithPos | undefined;
13
- export declare function getCellsInCol(columnIndex: number, selection: Selection): CellPos[] | undefined;
14
- export declare function getCellsInRow(rowIndex: number, selection: Selection): CellPos[] | undefined;
15
- export declare function getAllCellsInTable(selection: Selection): {
16
- pos: number;
17
- start: number;
18
- node: Node | null;
19
- }[] | undefined;
20
- export declare function selectTable(tr: Transaction): Transaction;
21
- export declare function addRowWithAlignment(ctx: Ctx, tr: Transaction, { map, tableStart, table }: TableRect, row: number): Transaction;
22
- export declare function selectLine(type: 'row' | 'col'): (index: number, pos?: number) => (tr: Transaction) => Transaction;
23
- export declare const selectRow: (index: number, pos?: number) => (tr: Transaction) => Transaction;
24
- export declare const selectCol: (index: number, pos?: number) => (tr: Transaction) => Transaction;
25
- export interface MoveColParams {
26
- tr: Transaction;
27
- origin: number;
28
- target: number;
29
- select?: boolean;
30
- pos?: number;
31
- }
32
- export declare function moveCol(moveColParams: MoveColParams): Transaction;
33
- export interface MoveRowParams {
34
- tr: Transaction;
35
- origin: number;
36
- target: number;
37
- select?: boolean;
38
- pos?: number;
39
- }
40
- export declare function moveRow(moveRowParams: MoveRowParams): Transaction;
41
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/node/table/utils.ts"],"names":[],"mappings":"AAAA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