@milkdown/preset-gfm 5.3.1 → 5.3.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 (52) hide show
  1. package/lib/auto-link.d.ts +1 -15
  2. package/lib/auto-link.d.ts.map +1 -1
  3. package/lib/index.d.ts +2 -2
  4. package/lib/index.d.ts.map +1 -1
  5. package/lib/index.es.js +768 -16
  6. package/lib/index.es.js.map +1 -1
  7. package/lib/supported-keys.d.ts.map +1 -1
  8. package/lib/table/command.d.ts +3 -0
  9. package/lib/table/command.d.ts.map +1 -0
  10. package/lib/table/index.d.ts +10 -0
  11. package/lib/table/index.d.ts.map +1 -0
  12. package/lib/table/nodes/index.d.ts +32 -0
  13. package/lib/table/nodes/index.d.ts.map +1 -0
  14. package/lib/table/nodes/schema.d.ts +2 -0
  15. package/lib/table/nodes/schema.d.ts.map +1 -0
  16. package/lib/table/nodes/style.d.ts +3 -0
  17. package/lib/table/nodes/style.d.ts.map +1 -0
  18. package/lib/table/operator-plugin/actions.d.ts +19 -0
  19. package/lib/table/operator-plugin/actions.d.ts.map +1 -0
  20. package/lib/table/operator-plugin/calc-pos.d.ts +3 -0
  21. package/lib/table/operator-plugin/calc-pos.d.ts.map +1 -0
  22. package/lib/table/operator-plugin/constant.d.ts +6 -0
  23. package/lib/table/operator-plugin/constant.d.ts.map +1 -0
  24. package/lib/table/operator-plugin/helper.d.ts +6 -0
  25. package/lib/table/operator-plugin/helper.d.ts.map +1 -0
  26. package/lib/table/operator-plugin/index.d.ts +6 -0
  27. package/lib/table/operator-plugin/index.d.ts.map +1 -0
  28. package/lib/table/operator-plugin/style.d.ts +3 -0
  29. package/lib/table/operator-plugin/style.d.ts.map +1 -0
  30. package/lib/table/operator-plugin/widget.d.ts +8 -0
  31. package/lib/table/operator-plugin/widget.d.ts.map +1 -0
  32. package/lib/table/utils.d.ts +20 -0
  33. package/lib/table/utils.d.ts.map +1 -0
  34. package/lib/task-list-item.d.ts.map +1 -1
  35. package/package.json +8 -15
  36. package/src/auto-link.ts +4 -9
  37. package/src/index.ts +13 -6
  38. package/src/supported-keys.ts +2 -1
  39. package/src/table/command.ts +16 -0
  40. package/src/table/index.ts +13 -0
  41. package/src/table/nodes/index.ts +169 -0
  42. package/src/table/nodes/schema.ts +16 -0
  43. package/src/table/nodes/style.ts +170 -0
  44. package/src/table/operator-plugin/actions.ts +115 -0
  45. package/src/table/operator-plugin/calc-pos.ts +25 -0
  46. package/src/table/operator-plugin/constant.ts +7 -0
  47. package/src/table/operator-plugin/helper.ts +38 -0
  48. package/src/table/operator-plugin/index.ts +98 -0
  49. package/src/table/operator-plugin/style.ts +51 -0
  50. package/src/table/operator-plugin/widget.ts +66 -0
  51. package/src/table/utils.ts +158 -0
  52. package/src/task-list-item.ts +2 -1
@@ -0,0 +1,66 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+
3
+ import { Ctx, themeToolCtx } from '@milkdown/core';
4
+ import { Decoration, WidgetDecorationSpec } from '@milkdown/prose';
5
+
6
+ import { CellPos, selectLine, selectTable } from '../utils';
7
+ import { ToolTipPos } from './constant';
8
+
9
+ const calculateClassName = (pos: ToolTipPos) => {
10
+ switch (pos) {
11
+ case ToolTipPos.Left: {
12
+ return 'milkdown-cell-left';
13
+ }
14
+ case ToolTipPos.Top: {
15
+ return 'milkdown-cell-top';
16
+ }
17
+ case ToolTipPos.Point:
18
+ default: {
19
+ return 'milkdown-cell-point';
20
+ }
21
+ }
22
+ };
23
+
24
+ export function createWidget(ctx: Ctx, cell: CellPos, pos: ToolTipPos.Point): Decoration<WidgetDecorationSpec>;
25
+ export function createWidget(
26
+ ctx: Ctx,
27
+ cell: CellPos,
28
+ pos: ToolTipPos.Left,
29
+ index: number,
30
+ ): Decoration<WidgetDecorationSpec>;
31
+ export function createWidget(
32
+ ctx: Ctx,
33
+ cell: CellPos,
34
+ pos: ToolTipPos.Top,
35
+ index: number,
36
+ ): Decoration<WidgetDecorationSpec>;
37
+ export function createWidget(ctx: Ctx, cell: CellPos, pos: ToolTipPos, index = 0) {
38
+ const widget = Decoration.widget(cell.pos + 1, (view) => {
39
+ const div = document.createElement('div');
40
+ div.classList.add(calculateClassName(pos));
41
+ if (pos === ToolTipPos.Point) {
42
+ div.appendChild(ctx.get(themeToolCtx).slots.icon('select'));
43
+ }
44
+ div.addEventListener('mousedown', (e) => {
45
+ if (!view) return;
46
+
47
+ e.preventDefault();
48
+ switch (pos) {
49
+ case ToolTipPos.Point: {
50
+ view.dispatch(selectTable(view.state.tr));
51
+ return;
52
+ }
53
+ case ToolTipPos.Left: {
54
+ view.dispatch(selectLine('row')(index)(view.state.tr));
55
+ return;
56
+ }
57
+ case ToolTipPos.Top: {
58
+ view.dispatch(selectLine('col')(index)(view.state.tr));
59
+ return;
60
+ }
61
+ }
62
+ });
63
+ return div;
64
+ });
65
+ return widget;
66
+ }
@@ -0,0 +1,158 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import {
3
+ CellSelection,
4
+ cloneTr,
5
+ findParentNode,
6
+ Node as ProsemirrorNode,
7
+ Schema,
8
+ Selection,
9
+ TableMap,
10
+ tableNodeTypes,
11
+ TableRect,
12
+ Transaction,
13
+ } from '@milkdown/prose';
14
+
15
+ export type CellPos = {
16
+ pos: number;
17
+ start: number;
18
+ node: ProsemirrorNode;
19
+ };
20
+
21
+ export const findTable = (selection: Selection) =>
22
+ findParentNode((node) => node.type.spec.tableRole === 'table')(selection);
23
+
24
+ export const getCellsInColumn =
25
+ (columnIndex: number) =>
26
+ (selection: Selection): CellPos[] | undefined => {
27
+ const table = findTable(selection);
28
+ if (!table) return undefined;
29
+ const map = TableMap.get(table.node);
30
+ if (columnIndex < 0 || columnIndex >= map.width) {
31
+ return undefined;
32
+ }
33
+
34
+ return map.cellsInRect({ left: columnIndex, right: columnIndex + 1, top: 0, bottom: map.height }).map((pos) => {
35
+ const node = table.node.nodeAt(pos);
36
+ if (!node) throw new Error();
37
+ const start = pos + table.start;
38
+ return {
39
+ pos: start,
40
+ start: start + 1,
41
+ node,
42
+ };
43
+ });
44
+ };
45
+
46
+ export const getCellsInRow =
47
+ (rowIndex: number) =>
48
+ (selection: Selection): CellPos[] | undefined => {
49
+ const table = findTable(selection);
50
+ if (!table) return undefined;
51
+ const map = TableMap.get(table.node);
52
+ if (rowIndex < 0 || rowIndex >= map.height) {
53
+ return undefined;
54
+ }
55
+
56
+ return map.cellsInRect({ left: 0, right: map.width, top: rowIndex, bottom: rowIndex + 1 }).map((pos) => {
57
+ const node = table.node.nodeAt(pos);
58
+ if (!node) throw new Error();
59
+ const start = pos + table.start;
60
+ return {
61
+ pos: start,
62
+ start: start + 1,
63
+ node,
64
+ };
65
+ });
66
+ };
67
+
68
+ export const createTable = (schema: Schema, rowsCount = 3, colsCount = 3) => {
69
+ const { cell: tableCell, header_cell: tableHeader, row: tableRow, table } = tableNodeTypes(schema);
70
+
71
+ const cells = Array(colsCount)
72
+ .fill(0)
73
+ .map(() => tableCell.createAndFill(null) as ProsemirrorNode);
74
+
75
+ const headerCells = Array(colsCount)
76
+ .fill(0)
77
+ .map(() => tableHeader.createAndFill(null) as ProsemirrorNode);
78
+
79
+ const rows = Array(rowsCount)
80
+ .fill(0)
81
+ .map((_, i) => tableRow.create(null, i === 0 ? headerCells : cells));
82
+
83
+ return table.create(null, rows);
84
+ };
85
+
86
+ export const selectLine = (type: 'row' | 'col') => (index: number) => (tr: Transaction) => {
87
+ const table = findTable(tr.selection);
88
+ const isRowSelection = type === 'row';
89
+ if (table) {
90
+ const map = TableMap.get(table.node);
91
+
92
+ // Check if the index is valid
93
+ if (index >= 0 && index < (isRowSelection ? map.height : map.width)) {
94
+ const lastCell = map.positionAt(
95
+ isRowSelection ? index : map.height - 1,
96
+ isRowSelection ? map.width - 1 : index,
97
+ table.node,
98
+ );
99
+ const $lastCell = tr.doc.resolve(table.start + lastCell);
100
+
101
+ const createCellSelection = isRowSelection ? CellSelection.rowSelection : CellSelection.colSelection;
102
+
103
+ const firstCell = map.positionAt(isRowSelection ? index : 0, isRowSelection ? 0 : index, table.node);
104
+ const $firstCell = tr.doc.resolve(table.start + firstCell);
105
+ return cloneTr(tr.setSelection(createCellSelection($lastCell, $firstCell) as unknown as Selection));
106
+ }
107
+ }
108
+ return tr;
109
+ };
110
+
111
+ export const getCellsInTable = (selection: Selection) => {
112
+ const table = findTable(selection);
113
+ if (!table) {
114
+ return;
115
+ }
116
+ const map = TableMap.get(table.node);
117
+ const cells = map.cellsInRect({
118
+ left: 0,
119
+ right: map.width,
120
+ top: 0,
121
+ bottom: map.height,
122
+ });
123
+ return cells.map((nodePos) => {
124
+ const node = table.node.nodeAt(nodePos);
125
+ const pos = nodePos + table.start;
126
+ return { pos, start: pos + 1, node };
127
+ });
128
+ };
129
+
130
+ export const selectTable = (tr: Transaction) => {
131
+ const cells = getCellsInTable(tr.selection);
132
+ if (cells) {
133
+ const $firstCell = tr.doc.resolve(cells[0].pos);
134
+ const $lastCell = tr.doc.resolve(cells[cells.length - 1].pos);
135
+ return cloneTr(tr.setSelection(new CellSelection($lastCell, $firstCell) as unknown as Selection));
136
+ }
137
+ return tr;
138
+ };
139
+
140
+ export function addRowWithAlignment(tr: Transaction, { map, tableStart, table }: TableRect, row: number) {
141
+ const rowPos = Array(row)
142
+ .fill(0)
143
+ .reduce((acc, _, i) => {
144
+ return acc + table.child(i).nodeSize;
145
+ }, tableStart);
146
+
147
+ const { cell: cellType, row: rowType } = tableNodeTypes(table.type.schema);
148
+
149
+ const cells = Array(map.width)
150
+ .fill(0)
151
+ .map((_, col) => {
152
+ const headerCol = table.nodeAt(map.map[col]);
153
+ return cellType.createAndFill({ alignment: headerCol?.attrs.alignment }) as ProsemirrorNode;
154
+ });
155
+
156
+ tr.insert(rowPos, rowType.create(null, cells));
157
+ return tr;
158
+ }
@@ -79,13 +79,14 @@ export const taskListItem = createNode<Keys>((utils) => {
79
79
  },
80
80
  parseDOM: [
81
81
  {
82
- tag: 'li[data-type="task-list-item"]',
82
+ tag: 'li[data-type="task-item"]',
83
83
  getAttrs: (dom) => {
84
84
  if (!(dom instanceof HTMLElement)) {
85
85
  throw new Error();
86
86
  }
87
87
  return { checked: dom.dataset.checked === 'true' };
88
88
  },
89
+ priority: 60,
89
90
  },
90
91
  ],
91
92
  toDOM: (node) => [