@milkdown/preset-gfm 6.1.4 → 6.3.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 (85) hide show
  1. package/lib/auto-link.d.ts.map +1 -1
  2. package/lib/footnote/definition.d.ts +1 -5
  3. package/lib/footnote/definition.d.ts.map +1 -1
  4. package/lib/footnote/reference.d.ts +1 -5
  5. package/lib/footnote/reference.d.ts.map +1 -1
  6. package/lib/index.d.ts +1 -3
  7. package/lib/index.d.ts.map +1 -1
  8. package/lib/index.es.js +1979 -891
  9. package/lib/index.es.js.map +1 -1
  10. package/lib/strike-through.d.ts +1 -5
  11. package/lib/strike-through.d.ts.map +1 -1
  12. package/lib/supported-keys.d.ts +1 -0
  13. package/lib/supported-keys.d.ts.map +1 -1
  14. package/lib/table/command.d.ts +1 -1
  15. package/lib/table/command.d.ts.map +1 -1
  16. package/lib/table/nodes/index.d.ts +2 -23
  17. package/lib/table/nodes/index.d.ts.map +1 -1
  18. package/lib/table/operator-plugin/actions.d.ts +1 -1
  19. package/lib/table/operator-plugin/actions.d.ts.map +1 -1
  20. package/lib/table/operator-plugin/calc-pos.d.ts.map +1 -1
  21. package/lib/table/operator-plugin/helper.d.ts +1 -1
  22. package/lib/table/operator-plugin/helper.d.ts.map +1 -1
  23. package/lib/table/operator-plugin/index.d.ts +1 -1
  24. package/lib/table/operator-plugin/index.d.ts.map +1 -1
  25. package/lib/table/operator-plugin/widget.d.ts +4 -4
  26. package/lib/table/operator-plugin/widget.d.ts.map +1 -1
  27. package/lib/table/plugin/auto-insert-zero-space.d.ts +3 -0
  28. package/lib/table/plugin/auto-insert-zero-space.d.ts.map +1 -0
  29. package/lib/table/plugin/cell-selection.d.ts +38 -0
  30. package/lib/table/plugin/cell-selection.d.ts.map +1 -0
  31. package/lib/table/plugin/column-resizing.d.ts +17 -0
  32. package/lib/table/plugin/column-resizing.d.ts.map +1 -0
  33. package/lib/table/plugin/commands.d.ts +30 -0
  34. package/lib/table/plugin/commands.d.ts.map +1 -0
  35. package/lib/table/plugin/copy-paste.d.ts +13 -0
  36. package/lib/table/plugin/copy-paste.d.ts.map +1 -0
  37. package/lib/table/plugin/fix-tables.d.ts +6 -0
  38. package/lib/table/plugin/fix-tables.d.ts.map +1 -0
  39. package/lib/table/plugin/index.d.ts +4 -0
  40. package/lib/table/plugin/index.d.ts.map +1 -0
  41. package/lib/table/plugin/schema.d.ts +4 -0
  42. package/lib/table/plugin/schema.d.ts.map +1 -0
  43. package/lib/table/plugin/table-editing.d.ts +9 -0
  44. package/lib/table/plugin/table-editing.d.ts.map +1 -0
  45. package/lib/table/plugin/table-map.d.ts +44 -0
  46. package/lib/table/plugin/table-map.d.ts.map +1 -0
  47. package/lib/table/plugin/table-view.d.ts +15 -0
  48. package/lib/table/plugin/table-view.d.ts.map +1 -0
  49. package/lib/table/plugin/types.d.ts +15 -0
  50. package/lib/table/plugin/types.d.ts.map +1 -0
  51. package/lib/table/plugin/util.d.ts +16 -0
  52. package/lib/table/plugin/util.d.ts.map +1 -0
  53. package/lib/table/utils.d.ts +6 -6
  54. package/lib/table/utils.d.ts.map +1 -1
  55. package/lib/task-list-item.d.ts +1 -5
  56. package/lib/task-list-item.d.ts.map +1 -1
  57. package/package.json +8 -7
  58. package/src/auto-link.ts +4 -3
  59. package/src/footnote/definition.ts +3 -1
  60. package/src/footnote/reference.ts +2 -1
  61. package/src/table/command.ts +3 -3
  62. package/src/table/nodes/index.ts +7 -3
  63. package/src/table/operator-plugin/actions.ts +4 -4
  64. package/src/table/operator-plugin/calc-pos.ts +4 -2
  65. package/src/table/operator-plugin/helper.ts +2 -1
  66. package/src/table/operator-plugin/index.ts +1 -1
  67. package/src/table/operator-plugin/widget.ts +4 -14
  68. package/src/table/plugin/auto-insert-zero-space.ts +51 -0
  69. package/src/table/plugin/cell-selection.ts +352 -0
  70. package/src/table/plugin/column-resizing.ts +260 -0
  71. package/src/table/plugin/commands.ts +551 -0
  72. package/src/table/plugin/copy-paste.ts +306 -0
  73. package/src/table/plugin/fix-tables.ts +117 -0
  74. package/src/table/plugin/index.ts +4 -0
  75. package/src/table/plugin/schema.ts +114 -0
  76. package/src/table/plugin/table-editing.ts +275 -0
  77. package/src/table/plugin/table-map.ts +280 -0
  78. package/src/table/plugin/table-view.ts +76 -0
  79. package/src/table/plugin/types.ts +16 -0
  80. package/src/table/plugin/util.ts +107 -0
  81. package/src/table/utils.ts +31 -22
  82. package/src/task-list-item.ts +4 -2
  83. package/lib/table/nodes/schema.d.ts +0 -2
  84. package/lib/table/nodes/schema.d.ts.map +0 -1
  85. package/src/table/nodes/schema.ts +0 -16
@@ -0,0 +1,107 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import { Attrs, Node, ResolvedPos } from '@milkdown/prose/model';
3
+ import { EditorState, NodeSelection } from '@milkdown/prose/state';
4
+
5
+ import { CellSelection } from './cell-selection';
6
+ import { tableNodeTypes } from './schema';
7
+ import { TableMap } from './table-map';
8
+
9
+ export function cellAround($pos: ResolvedPos) {
10
+ for (let d = $pos.depth - 1; d > 0; d--)
11
+ if ($pos.node(d).type.spec['tableRole'] == 'row') return $pos.node(0).resolve($pos.before(d + 1));
12
+ return undefined;
13
+ }
14
+
15
+ export function cellWrapping($pos: ResolvedPos) {
16
+ for (let d = $pos.depth; d > 0; d--) {
17
+ // Sometimes the cell can be in the same depth.
18
+ const role = $pos.node(d).type.spec['tableRole'];
19
+ if (role === 'cell' || role === 'header_cell') return $pos.node(d);
20
+ }
21
+ return null;
22
+ }
23
+
24
+ export function pointsAtCell($pos: ResolvedPos): Node | null {
25
+ if ($pos.parent.type.spec['tableRole'] == 'row') return $pos.nodeAfter;
26
+ return null;
27
+ }
28
+
29
+ export function moveCellForward($pos: ResolvedPos) {
30
+ return $pos.node(0).resolve($pos.pos + ($pos.nodeAfter as Node).nodeSize);
31
+ }
32
+
33
+ export function inSameTable($a: ResolvedPos, $b: ResolvedPos) {
34
+ return $a.depth == $b.depth && $a.pos >= $b.start(-1) && $a.pos <= $b.end(-1);
35
+ }
36
+
37
+ export function nextCell($pos: ResolvedPos, axis: string, dir: number) {
38
+ const start = $pos.start(-1),
39
+ map = TableMap.get($pos.node(-1));
40
+ const moved = map.nextCell($pos.pos - start, axis, dir);
41
+ return moved == null ? null : $pos.node(0).resolve(start + moved);
42
+ }
43
+
44
+ export function setAttr<T>(attrs: Attrs, name: string, value: T) {
45
+ const result: Record<string, unknown> = {};
46
+ for (const prop in attrs) result[prop] = attrs[prop];
47
+ result[name] = value;
48
+ return result as Attrs;
49
+ }
50
+
51
+ export function removeColSpan(attrs: Attrs, pos: number, n = 1) {
52
+ const result = setAttr(attrs, 'colspan', attrs['colspan'] - n) as Record<string, unknown>;
53
+ if (result['colwidth']) {
54
+ const widths = result['colwidth'] as number[];
55
+ result['colwidth'] = widths.slice();
56
+ widths.splice(pos, n);
57
+ if (!widths.some((w) => w > 0)) result['colwidth'] = null;
58
+ }
59
+ return result;
60
+ }
61
+
62
+ export function isInTable(state: EditorState) {
63
+ const $head = state.selection.$head;
64
+ for (let d = $head.depth; d > 0; d--) if ($head.node(d).type.spec['tableRole'] == 'row') return true;
65
+ return false;
66
+ }
67
+
68
+ export function selectionCell(state: EditorState) {
69
+ const sel = state.selection;
70
+ if (sel instanceof CellSelection) {
71
+ return sel.$anchorCell.pos > sel.$headCell.pos ? sel.$anchorCell : sel.$headCell;
72
+ } else if ((sel as NodeSelection).node && (sel as NodeSelection).node.type.spec['tableRole'] == 'cell') {
73
+ return sel.$anchor;
74
+ }
75
+ return cellAround(sel.$head) || cellNear(sel.$head);
76
+ }
77
+
78
+ function cellNear($pos: ResolvedPos) {
79
+ for (let after = $pos.nodeAfter, pos = $pos.pos; after; after = after.firstChild, pos++) {
80
+ const role = after.type.spec['tableRole'];
81
+ if (role == 'cell' || role == 'header_cell') return $pos.doc.resolve(pos);
82
+ }
83
+ for (let before = $pos.nodeBefore, pos = $pos.pos; before; before = before.lastChild, pos--) {
84
+ const role = before.type.spec['tableRole'];
85
+ if (role == 'cell' || role == 'header_cell') return $pos.doc.resolve(pos - before.nodeSize);
86
+ }
87
+ return;
88
+ }
89
+
90
+ export function addColSpan(attrs: Attrs, pos: number, n = 1) {
91
+ const result = setAttr(attrs, 'colspan', attrs['colspan'] + n) as Record<string, unknown>;
92
+ if (result['colwidth']) {
93
+ const widths = result['colwidth'] as number[];
94
+ result['colwidth'] = widths.slice();
95
+ for (let i = 0; i < n; i++) widths.splice(pos, 0, 0);
96
+ }
97
+ return result as Attrs;
98
+ }
99
+
100
+ export function columnIsHeader(map: TableMap, table: Node, col: number) {
101
+ const headerCell = tableNodeTypes(table.type.schema).header_cell;
102
+ for (let row = 0; row < map.height; row++) {
103
+ const pos = map.map[col + row * map.width] as number;
104
+ if ((table.nodeAt(pos) as Node).type != headerCell) return false;
105
+ }
106
+ return true;
107
+ }
@@ -2,7 +2,10 @@
2
2
  import { cloneTr, findParentNode } from '@milkdown/prose';
3
3
  import { Node as ProsemirrorNode, Schema } from '@milkdown/prose/model';
4
4
  import { Selection, Transaction } from '@milkdown/prose/state';
5
- import { CellSelection, TableMap, tableNodeTypes, TableRect } from '@milkdown/prose/tables';
5
+
6
+ import { CellSelection } from './plugin/cell-selection';
7
+ import { tableNodeTypes } from './plugin/schema';
8
+ import { Rect, TableMap } from './plugin/table-map';
6
9
 
7
10
  export type CellPos = {
8
11
  pos: number;
@@ -23,16 +26,19 @@ export const getCellsInColumn =
23
26
  return undefined;
24
27
  }
25
28
 
26
- return map.cellsInRect({ left: columnIndex, right: columnIndex + 1, top: 0, bottom: map.height }).map((pos) => {
27
- const node = table.node.nodeAt(pos);
28
- if (!node) throw new Error();
29
- const start = pos + table.start;
30
- return {
31
- pos: start,
32
- start: start + 1,
33
- node,
34
- };
35
- });
29
+ return map
30
+ .cellsInRect({ left: columnIndex, right: columnIndex + 1, top: 0, bottom: map.height })
31
+ .map((pos) => {
32
+ const node = table.node.nodeAt(pos);
33
+ if (!node) return;
34
+ const start = pos + table.start;
35
+ return {
36
+ pos: start,
37
+ start: start + 1,
38
+ node,
39
+ };
40
+ })
41
+ .filter((x): x is CellPos => x != null);
36
42
  };
37
43
 
38
44
  export const getCellsInRow =
@@ -45,16 +51,19 @@ export const getCellsInRow =
45
51
  return undefined;
46
52
  }
47
53
 
48
- return map.cellsInRect({ left: 0, right: map.width, top: rowIndex, bottom: rowIndex + 1 }).map((pos) => {
49
- const node = table.node.nodeAt(pos);
50
- if (!node) throw new Error();
51
- const start = pos + table.start;
52
- return {
53
- pos: start,
54
- start: start + 1,
55
- node,
56
- };
57
- });
54
+ return map
55
+ .cellsInRect({ left: 0, right: map.width, top: rowIndex, bottom: rowIndex + 1 })
56
+ .map((pos) => {
57
+ const node = table.node.nodeAt(pos);
58
+ if (!node) return;
59
+ const start = pos + table.start;
60
+ return {
61
+ pos: start,
62
+ start: start + 1,
63
+ node,
64
+ };
65
+ })
66
+ .filter((x): x is CellPos => x != null);
58
67
  };
59
68
 
60
69
  export const createTable = (schema: Schema, rowsCount = 3, colsCount = 3) => {
@@ -132,7 +141,7 @@ export const selectTable = (tr: Transaction) => {
132
141
  return tr;
133
142
  };
134
143
 
135
- export function addRowWithAlignment(tr: Transaction, { map, tableStart, table }: TableRect, row: number) {
144
+ export function addRowWithAlignment(tr: Transaction, { map, tableStart, table }: Required<Rect>, row: number) {
136
145
  const rowPos = Array(row)
137
146
  .fill(0)
138
147
  .reduce((acc, _, i) => {
@@ -1,8 +1,10 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
  import { createCmd, createCmdKey, editorViewCtx, ThemeTaskListItemType } from '@milkdown/core';
3
+ import { expectDomTypeError } from '@milkdown/exception';
3
4
  import { wrapIn } from '@milkdown/prose/commands';
4
5
  import { wrappingInputRule } from '@milkdown/prose/inputrules';
5
6
  import { liftListItem, sinkListItem, splitListItem } from '@milkdown/prose/schema-list';
7
+ import { NodeView } from '@milkdown/prose/view';
6
8
  import { createNode, createShortcut } from '@milkdown/utils';
7
9
 
8
10
  import { SupportedKeys } from './supported-keys';
@@ -34,7 +36,7 @@ export const taskListItem = createNode<Keys>((utils) => {
34
36
  tag: 'li[data-type="task-item"]',
35
37
  getAttrs: (dom) => {
36
38
  if (!(dom instanceof HTMLElement)) {
37
- throw new Error();
39
+ throw expectDomTypeError(dom);
38
40
  }
39
41
  return { checked: dom.dataset['checked'] === 'true' };
40
42
  },
@@ -130,7 +132,7 @@ export const taskListItem = createNode<Keys>((utils) => {
130
132
  },
131
133
  });
132
134
 
133
- if (!renderer) return {};
135
+ if (!renderer) return {} as NodeView;
134
136
 
135
137
  const { dom, contentDOM, onUpdate } = renderer;
136
138
  onUpdate(currNode);
@@ -1,2 +0,0 @@
1
- export declare const schema: import("@milkdown/prose/tables").TableNodes;
2
- //# sourceMappingURL=schema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/table/nodes/schema.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,MAAM,6CAYjB,CAAC"}
@@ -1,16 +0,0 @@
1
- /* Copyright 2021, Milkdown by Mirone. */
2
- import { tableNodes as tableNodesSpecCreator } from '@milkdown/prose/tables';
3
-
4
- export const schema = tableNodesSpecCreator({
5
- tableGroup: 'block',
6
- cellContent: 'paragraph',
7
- cellAttributes: {
8
- alignment: {
9
- default: 'left',
10
- getFromDOM: (dom) => (dom as HTMLElement).style.textAlign || 'left',
11
- setDOMAttr: (value, attrs) => {
12
- attrs.style = `text-align: ${value || 'left'}`;
13
- },
14
- },
15
- },
16
- });