@milkdown/preset-gfm 6.5.4 → 7.0.0-next.1

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 (156) hide show
  1. package/lib/composed/commands.d.ts +8 -0
  2. package/lib/composed/commands.d.ts.map +1 -0
  3. package/lib/composed/index.d.ts +6 -0
  4. package/lib/composed/index.d.ts.map +1 -0
  5. package/lib/composed/inputrules.d.ts +3 -0
  6. package/lib/composed/inputrules.d.ts.map +1 -0
  7. package/lib/composed/keymap.d.ts +3 -0
  8. package/lib/composed/keymap.d.ts.map +1 -0
  9. package/lib/composed/plugins.d.ts +3 -0
  10. package/lib/composed/plugins.d.ts.map +1 -0
  11. package/lib/composed/schema.d.ts +3 -0
  12. package/lib/composed/schema.d.ts.map +1 -0
  13. package/lib/index.d.ts +4 -34
  14. package/lib/index.d.ts.map +1 -1
  15. package/lib/index.es.js +634 -2328
  16. package/lib/index.es.js.map +1 -1
  17. package/lib/mark/index.d.ts +2 -0
  18. package/lib/mark/index.d.ts.map +1 -0
  19. package/lib/mark/strike-through.d.ts +5 -0
  20. package/lib/mark/strike-through.d.ts.map +1 -0
  21. package/lib/node/footnote/definition.d.ts +2 -0
  22. package/lib/node/footnote/definition.d.ts.map +1 -0
  23. package/lib/{footnote → node/footnote}/index.d.ts +0 -0
  24. package/lib/node/footnote/index.d.ts.map +1 -0
  25. package/lib/node/footnote/reference.d.ts +2 -0
  26. package/lib/node/footnote/reference.d.ts.map +1 -0
  27. package/lib/node/index.d.ts +4 -0
  28. package/lib/node/index.d.ts.map +1 -0
  29. package/lib/node/table/index.d.ts +32 -0
  30. package/lib/node/table/index.d.ts.map +1 -0
  31. package/lib/node/table/utils.d.ts +26 -0
  32. package/lib/node/table/utils.d.ts.map +1 -0
  33. package/lib/node/task-list-item.d.ts +2 -0
  34. package/lib/node/task-list-item.d.ts.map +1 -0
  35. package/lib/plugin/auto-insert-zero-space-plugin.d.ts +2 -0
  36. package/lib/plugin/auto-insert-zero-space-plugin.d.ts.map +1 -0
  37. package/lib/plugin/column-resizing-plugin.d.ts +2 -0
  38. package/lib/plugin/column-resizing-plugin.d.ts.map +1 -0
  39. package/lib/plugin/index.d.ts +5 -0
  40. package/lib/plugin/index.d.ts.map +1 -0
  41. package/lib/plugin/remark-gfm-plugin.d.ts +2 -0
  42. package/lib/plugin/remark-gfm-plugin.d.ts.map +1 -0
  43. package/lib/plugin/table-editing-plugin.d.ts +2 -0
  44. package/lib/plugin/table-editing-plugin.d.ts.map +1 -0
  45. package/package.json +13 -10
  46. package/src/composed/commands.ts +24 -0
  47. package/src/composed/index.ts +6 -0
  48. package/src/composed/inputrules.ts +8 -0
  49. package/src/composed/keymap.ts +10 -0
  50. package/src/composed/plugins.ts +11 -0
  51. package/src/composed/schema.ts +20 -0
  52. package/src/index.ts +6 -89
  53. package/src/mark/index.ts +2 -0
  54. package/src/mark/strike-through.ts +46 -0
  55. package/src/node/footnote/definition.ts +70 -0
  56. package/src/{footnote → node/footnote}/index.ts +0 -0
  57. package/src/node/footnote/reference.ts +60 -0
  58. package/src/node/index.ts +4 -0
  59. package/src/node/table/index.ts +318 -0
  60. package/src/node/table/utils.ts +509 -0
  61. package/src/node/task-list-item.ts +88 -0
  62. package/src/{table/plugin/auto-insert-zero-space.ts → plugin/auto-insert-zero-space-plugin.ts} +14 -15
  63. package/src/plugin/column-resizing-plugin.ts +6 -0
  64. package/src/plugin/index.ts +5 -0
  65. package/src/plugin/remark-gfm-plugin.ts +6 -0
  66. package/src/plugin/table-editing-plugin.ts +6 -0
  67. package/lib/footnote/definition.d.ts +0 -3
  68. package/lib/footnote/definition.d.ts.map +0 -1
  69. package/lib/footnote/index.d.ts.map +0 -1
  70. package/lib/footnote/reference.d.ts +0 -3
  71. package/lib/footnote/reference.d.ts.map +0 -1
  72. package/lib/footnote/utils.d.ts +0 -3
  73. package/lib/footnote/utils.d.ts.map +0 -1
  74. package/lib/strike-through.d.ts +0 -3
  75. package/lib/strike-through.d.ts.map +0 -1
  76. package/lib/supported-keys.d.ts +0 -28
  77. package/lib/supported-keys.d.ts.map +0 -1
  78. package/lib/table/command.d.ts +0 -4
  79. package/lib/table/command.d.ts.map +0 -1
  80. package/lib/table/index.d.ts +0 -10
  81. package/lib/table/index.d.ts.map +0 -1
  82. package/lib/table/nodes/index.d.ts +0 -12
  83. package/lib/table/nodes/index.d.ts.map +0 -1
  84. package/lib/table/operator-plugin/actions.d.ts +0 -20
  85. package/lib/table/operator-plugin/actions.d.ts.map +0 -1
  86. package/lib/table/operator-plugin/calc-pos.d.ts +0 -3
  87. package/lib/table/operator-plugin/calc-pos.d.ts.map +0 -1
  88. package/lib/table/operator-plugin/constant.d.ts +0 -6
  89. package/lib/table/operator-plugin/constant.d.ts.map +0 -1
  90. package/lib/table/operator-plugin/helper.d.ts +0 -7
  91. package/lib/table/operator-plugin/helper.d.ts.map +0 -1
  92. package/lib/table/operator-plugin/index.d.ts +0 -6
  93. package/lib/table/operator-plugin/index.d.ts.map +0 -1
  94. package/lib/table/operator-plugin/style.d.ts +0 -3
  95. package/lib/table/operator-plugin/style.d.ts.map +0 -1
  96. package/lib/table/operator-plugin/widget.d.ts +0 -8
  97. package/lib/table/operator-plugin/widget.d.ts.map +0 -1
  98. package/lib/table/plugin/auto-insert-zero-space.d.ts +0 -3
  99. package/lib/table/plugin/auto-insert-zero-space.d.ts.map +0 -1
  100. package/lib/table/plugin/cell-selection.d.ts +0 -40
  101. package/lib/table/plugin/cell-selection.d.ts.map +0 -1
  102. package/lib/table/plugin/column-resizing.d.ts +0 -18
  103. package/lib/table/plugin/column-resizing.d.ts.map +0 -1
  104. package/lib/table/plugin/commands.d.ts +0 -30
  105. package/lib/table/plugin/commands.d.ts.map +0 -1
  106. package/lib/table/plugin/copy-paste.d.ts +0 -14
  107. package/lib/table/plugin/copy-paste.d.ts.map +0 -1
  108. package/lib/table/plugin/fix-tables.d.ts +0 -7
  109. package/lib/table/plugin/fix-tables.d.ts.map +0 -1
  110. package/lib/table/plugin/index.d.ts +0 -4
  111. package/lib/table/plugin/index.d.ts.map +0 -1
  112. package/lib/table/plugin/schema.d.ts +0 -4
  113. package/lib/table/plugin/schema.d.ts.map +0 -1
  114. package/lib/table/plugin/table-editing.d.ts +0 -9
  115. package/lib/table/plugin/table-editing.d.ts.map +0 -1
  116. package/lib/table/plugin/table-map.d.ts +0 -44
  117. package/lib/table/plugin/table-map.d.ts.map +0 -1
  118. package/lib/table/plugin/table-view.d.ts +0 -15
  119. package/lib/table/plugin/table-view.d.ts.map +0 -1
  120. package/lib/table/plugin/types.d.ts +0 -15
  121. package/lib/table/plugin/types.d.ts.map +0 -1
  122. package/lib/table/plugin/util.d.ts +0 -16
  123. package/lib/table/plugin/util.d.ts.map +0 -1
  124. package/lib/table/utils.d.ts +0 -21
  125. package/lib/table/utils.d.ts.map +0 -1
  126. package/lib/task-list-item.d.ts +0 -9
  127. package/lib/task-list-item.d.ts.map +0 -1
  128. package/src/footnote/definition.ts +0 -187
  129. package/src/footnote/reference.ts +0 -178
  130. package/src/footnote/utils.ts +0 -4
  131. package/src/strike-through.ts +0 -43
  132. package/src/supported-keys.ts +0 -13
  133. package/src/table/command.ts +0 -20
  134. package/src/table/index.ts +0 -13
  135. package/src/table/nodes/index.ts +0 -189
  136. package/src/table/operator-plugin/actions.ts +0 -116
  137. package/src/table/operator-plugin/calc-pos.ts +0 -36
  138. package/src/table/operator-plugin/constant.ts +0 -7
  139. package/src/table/operator-plugin/helper.ts +0 -39
  140. package/src/table/operator-plugin/index.ts +0 -110
  141. package/src/table/operator-plugin/style.ts +0 -123
  142. package/src/table/operator-plugin/widget.ts +0 -57
  143. package/src/table/plugin/cell-selection.ts +0 -381
  144. package/src/table/plugin/column-resizing.ts +0 -288
  145. package/src/table/plugin/commands.ts +0 -594
  146. package/src/table/plugin/copy-paste.ts +0 -322
  147. package/src/table/plugin/fix-tables.ts +0 -132
  148. package/src/table/plugin/index.ts +0 -4
  149. package/src/table/plugin/schema.ts +0 -120
  150. package/src/table/plugin/table-editing.ts +0 -369
  151. package/src/table/plugin/table-map.ts +0 -345
  152. package/src/table/plugin/table-view.ts +0 -80
  153. package/src/table/plugin/types.ts +0 -16
  154. package/src/table/plugin/util.ts +0 -119
  155. package/src/table/utils.ts +0 -165
  156. package/src/task-list-item.ts +0 -159
@@ -1,80 +0,0 @@
1
- /* Copyright 2021, Milkdown by Mirone. */
2
- import type { Node } from '@milkdown/prose/model'
3
- import type { NodeView } from '@milkdown/prose/view'
4
-
5
- export class TableView implements NodeView {
6
- public dom: HTMLElement
7
- public contentDOM: HTMLElement
8
- public table: HTMLTableElement
9
- public colgroup: HTMLTableColElement
10
-
11
- constructor(public node: Node, public cellMinWidth: number) {
12
- this.node = node
13
- this.cellMinWidth = cellMinWidth
14
- this.dom = document.createElement('div')
15
- this.dom.className = 'tableWrapper'
16
- this.table = this.dom.appendChild(document.createElement('table'))
17
- this.colgroup = this.table.appendChild(document.createElement('colgroup'))
18
- updateColumns(node, this.colgroup, this.table, cellMinWidth)
19
- this.contentDOM = this.table.appendChild(document.createElement('tbody'))
20
- }
21
-
22
- update(node: Node) {
23
- if (node.type != this.node.type)
24
- return false
25
- this.node = node
26
- updateColumns(node, this.colgroup, this.table, this.cellMinWidth)
27
- return true
28
- }
29
-
30
- ignoreMutation(record: MutationRecord) {
31
- return record.type == 'attributes' && (record.target == this.table || this.colgroup.contains(record.target))
32
- }
33
- }
34
-
35
- export function updateColumns(
36
- node: Node,
37
- colgroup: HTMLTableColElement,
38
- table: HTMLTableElement,
39
- cellMinWidth: number,
40
- overrideCol?: number,
41
- overrideValue?: number,
42
- ) {
43
- let totalWidth = 0
44
- let fixedWidth = true
45
- let nextDOM = colgroup.firstChild
46
- const row = node.firstChild as Node
47
- for (let i = 0, col = 0; i < row.childCount; i++) {
48
- const { colspan, colwidth } = row.child(i).attrs
49
- for (let j = 0; j < colspan; j++, col++) {
50
- const hasWidth = overrideCol == col ? overrideValue : colwidth && colwidth[j]
51
- const cssWidth = hasWidth ? `${hasWidth}px` : ''
52
- totalWidth += hasWidth || cellMinWidth
53
- if (!hasWidth)
54
- fixedWidth = false
55
- if (!nextDOM) {
56
- colgroup.appendChild(document.createElement('col')).style.width = cssWidth
57
- }
58
- else {
59
- if ((nextDOM as HTMLElement).style.width != cssWidth)
60
- (nextDOM as HTMLElement).style.width = cssWidth
61
- nextDOM = nextDOM.nextSibling
62
- }
63
- }
64
- }
65
-
66
- while (nextDOM) {
67
- const after = nextDOM.nextSibling
68
- nextDOM.parentNode?.removeChild(nextDOM)
69
- nextDOM = after
70
- }
71
-
72
- if (fixedWidth) {
73
- table.style.width = `${totalWidth}px`
74
- table.style.minWidth = ''
75
- }
76
- else {
77
- table.style.width = ''
78
- table.style.minWidth = `${totalWidth}px`
79
- }
80
- }
@@ -1,16 +0,0 @@
1
- /* Copyright 2021, Milkdown by Mirone. */
2
-
3
- export type getFromDOM<T> = (dom: Element) => T
4
- export type setDOMAttr = <Value>(value: Value, attrs: Record<string, unknown>) => void
5
-
6
- export interface CellAttributes<T = unknown> {
7
- default: T
8
- getFromDOM?: getFromDOM<T>
9
- setDOMAttr?: setDOMAttr
10
- }
11
-
12
- export interface TableNodesOptions {
13
- tableGroup?: string
14
- cellContent: string
15
- cellAttributes: { [key: string]: CellAttributes }
16
- }
@@ -1,119 +0,0 @@
1
- /* Copyright 2021, Milkdown by Mirone. */
2
- import type { Attrs, Node, ResolvedPos } from '@milkdown/prose/model'
3
- import type { 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')
12
- return $pos.node(0).resolve($pos.before(d + 1))
13
- }
14
- return undefined
15
- }
16
-
17
- export function cellWrapping($pos: ResolvedPos) {
18
- for (let d = $pos.depth; d > 0; d--) {
19
- // Sometimes the cell can be in the same depth.
20
- const role = $pos.node(d).type.spec.tableRole
21
- if (role === 'cell' || role === 'header_cell')
22
- return $pos.node(d)
23
- }
24
- return null
25
- }
26
-
27
- export function pointsAtCell($pos: ResolvedPos): Node | null {
28
- if ($pos.parent.type.spec.tableRole == 'row')
29
- return $pos.nodeAfter
30
- return null
31
- }
32
-
33
- export function moveCellForward($pos: ResolvedPos) {
34
- return $pos.node(0).resolve($pos.pos + ($pos.nodeAfter as Node).nodeSize)
35
- }
36
-
37
- export function inSameTable($a: ResolvedPos, $b: ResolvedPos) {
38
- return $a.depth == $b.depth && $a.pos >= $b.start(-1) && $a.pos <= $b.end(-1)
39
- }
40
-
41
- export function nextCell($pos: ResolvedPos, axis: string, dir: number) {
42
- const start = $pos.start(-1)
43
- const map = TableMap.get($pos.node(-1))
44
- const moved = map.nextCell($pos.pos - start, axis, dir)
45
- return moved == null ? null : $pos.node(0).resolve(start + moved)
46
- }
47
-
48
- export function setAttr<T>(attrs: Attrs, name: string, value: T) {
49
- const result: Record<string, unknown> = {}
50
- for (const prop in attrs) result[prop] = attrs[prop]
51
- result[name] = value
52
- return result as Attrs
53
- }
54
-
55
- export function removeColSpan(attrs: Attrs, pos: number, n = 1) {
56
- const result = setAttr(attrs, 'colspan', attrs.colspan - n) as Record<string, unknown>
57
- if (result.colwidth) {
58
- const widths = result.colwidth as number[]
59
- result.colwidth = widths.slice()
60
- widths.splice(pos, n)
61
- if (!widths.some(w => w > 0))
62
- result.colwidth = null
63
- }
64
- return result
65
- }
66
-
67
- export function isInTable(state: EditorState) {
68
- const $head = state.selection.$head
69
- for (let d = $head.depth; d > 0; d--) {
70
- if ($head.node(d).type.spec.tableRole == 'row')
71
- return true
72
- }
73
- return false
74
- }
75
-
76
- export function selectionCell(state: EditorState) {
77
- const sel = state.selection
78
- if (sel instanceof CellSelection)
79
- return sel.$anchorCell.pos > sel.$headCell.pos ? sel.$anchorCell : sel.$headCell
80
- else if ((sel as NodeSelection).node && (sel as NodeSelection).node.type.spec.tableRole == 'cell')
81
- return sel.$anchor
82
-
83
- return cellAround(sel.$head) || cellNear(sel.$head)
84
- }
85
-
86
- function cellNear($pos: ResolvedPos) {
87
- for (let after = $pos.nodeAfter, pos = $pos.pos; after; after = after.firstChild, pos++) {
88
- const role = after.type.spec.tableRole
89
- if (role == 'cell' || role == 'header_cell')
90
- return $pos.doc.resolve(pos)
91
- }
92
- for (let before = $pos.nodeBefore, pos = $pos.pos; before; before = before.lastChild, pos--) {
93
- const role = before.type.spec.tableRole
94
- if (role == 'cell' || role == 'header_cell')
95
- return $pos.doc.resolve(pos - before.nodeSize)
96
- }
97
-
98
- return undefined
99
- }
100
-
101
- export function addColSpan(attrs: Attrs, pos: number, n = 1) {
102
- const result = setAttr(attrs, 'colspan', attrs.colspan + n) as Record<string, unknown>
103
- if (result.colwidth) {
104
- const widths = result.colwidth as number[]
105
- result.colwidth = widths.slice()
106
- for (let i = 0; i < n; i++) widths.splice(pos, 0, 0)
107
- }
108
- return result as Attrs
109
- }
110
-
111
- export function columnIsHeader(map: TableMap, table: Node, col: number) {
112
- const headerCell = tableNodeTypes(table.type.schema).header_cell
113
- for (let row = 0; row < map.height; row++) {
114
- const pos = map.map[col + row * map.width] as number
115
- if ((table.nodeAt(pos) as Node).type != headerCell)
116
- return false
117
- }
118
- return true
119
- }
@@ -1,165 +0,0 @@
1
- /* Copyright 2021, Milkdown by Mirone. */
2
- import { cloneTr, findParentNode } from '@milkdown/prose'
3
- import type { Node as ProsemirrorNode, Schema } from '@milkdown/prose/model'
4
- import type { Selection, Transaction } from '@milkdown/prose/state'
5
-
6
- import { CellSelection } from './plugin/cell-selection'
7
- import { tableNodeTypes } from './plugin/schema'
8
- import type { Rect } from './plugin/table-map'
9
- import { TableMap } from './plugin/table-map'
10
-
11
- export interface CellPos {
12
- pos: number
13
- start: number
14
- node: ProsemirrorNode
15
- }
16
-
17
- export const findTable = (selection: Selection) =>
18
- findParentNode(node => node.type.spec.tableRole === 'table')(selection)
19
-
20
- export const getCellsInColumn
21
- = (columnIndex: number) =>
22
- (selection: Selection): CellPos[] | undefined => {
23
- const table = findTable(selection)
24
- if (!table)
25
- return undefined
26
- const map = TableMap.get(table.node)
27
- if (columnIndex < 0 || columnIndex >= map.width)
28
- return undefined
29
-
30
- return map
31
- .cellsInRect({ left: columnIndex, right: columnIndex + 1, top: 0, bottom: map.height })
32
- .map((pos) => {
33
- const node = table.node.nodeAt(pos)
34
- if (!node)
35
- return undefined
36
- const start = pos + table.start
37
- return {
38
- pos: start,
39
- start: start + 1,
40
- node,
41
- }
42
- })
43
- .filter((x): x is CellPos => x != null)
44
- }
45
-
46
- export const getCellsInRow
47
- = (rowIndex: number) =>
48
- (selection: Selection): CellPos[] | undefined => {
49
- const table = findTable(selection)
50
- if (!table)
51
- return undefined
52
- const map = TableMap.get(table.node)
53
- if (rowIndex < 0 || rowIndex >= map.height)
54
- return undefined
55
-
56
- return map
57
- .cellsInRect({ left: 0, right: map.width, top: rowIndex, bottom: rowIndex + 1 })
58
- .map((pos) => {
59
- const node = table.node.nodeAt(pos)
60
- if (!node)
61
- return undefined
62
- const start = pos + table.start
63
- return {
64
- pos: start,
65
- start: start + 1,
66
- node,
67
- }
68
- })
69
- .filter((x): x is CellPos => x != null)
70
- }
71
-
72
- export const createTable = (schema: Schema, rowsCount = 3, colsCount = 3) => {
73
- const { cell: tableCell, header_cell: tableHeader, row: tableRow, table } = tableNodeTypes(schema)
74
-
75
- const cells = Array(colsCount)
76
- .fill(0)
77
- .map(() => tableCell.createAndFill(null) as ProsemirrorNode)
78
-
79
- const headerCells = Array(colsCount)
80
- .fill(0)
81
- .map(() => tableHeader.createAndFill(null) as ProsemirrorNode)
82
-
83
- const rows = Array(rowsCount)
84
- .fill(0)
85
- .map((_, i) => tableRow.create(null, i === 0 ? headerCells : cells))
86
-
87
- return table.create(null, rows)
88
- }
89
-
90
- export const selectLine = (type: 'row' | 'col') => (index: number) => (tr: Transaction) => {
91
- const table = findTable(tr.selection)
92
- const isRowSelection = type === 'row'
93
- if (table) {
94
- const map = TableMap.get(table.node)
95
-
96
- // Check if the index is valid
97
- if (index >= 0 && index < (isRowSelection ? map.height : map.width)) {
98
- const lastCell = map.positionAt(
99
- isRowSelection ? index : map.height - 1,
100
- isRowSelection ? map.width - 1 : index,
101
- table.node,
102
- )
103
- const $lastCell = tr.doc.resolve(table.start + lastCell)
104
-
105
- const createCellSelection = isRowSelection ? CellSelection.rowSelection : CellSelection.colSelection
106
-
107
- const firstCell = map.positionAt(isRowSelection ? index : 0, isRowSelection ? 0 : index, table.node)
108
- const $firstCell = tr.doc.resolve(table.start + firstCell)
109
- return cloneTr(tr.setSelection(createCellSelection($lastCell, $firstCell) as unknown as Selection))
110
- }
111
- }
112
- return tr
113
- }
114
-
115
- export const getCellsInTable = (selection: Selection) => {
116
- const table = findTable(selection)
117
- if (!table)
118
- return
119
-
120
- const map = TableMap.get(table.node)
121
- const cells = map.cellsInRect({
122
- left: 0,
123
- right: map.width,
124
- top: 0,
125
- bottom: map.height,
126
- })
127
- return cells.map((nodePos) => {
128
- const node = table.node.nodeAt(nodePos)
129
- const pos = nodePos + table.start
130
- return { pos, start: pos + 1, node }
131
- })
132
- }
133
-
134
- export const selectTable = (tr: Transaction) => {
135
- const cells = getCellsInTable(tr.selection)
136
- if (cells && cells[0]) {
137
- const $firstCell = tr.doc.resolve(cells[0].pos)
138
- const last = cells[cells.length - 1]
139
- if (last) {
140
- const $lastCell = tr.doc.resolve(last.pos)
141
- return cloneTr(tr.setSelection(new CellSelection($lastCell, $firstCell) as unknown as Selection))
142
- }
143
- }
144
- return tr
145
- }
146
-
147
- export function addRowWithAlignment(tr: Transaction, { map, tableStart, table }: Required<Rect>, row: number) {
148
- const rowPos = Array(row)
149
- .fill(0)
150
- .reduce((acc, _, i) => {
151
- return acc + table.child(i).nodeSize
152
- }, tableStart)
153
-
154
- const { cell: cellType, row: rowType } = tableNodeTypes(table.type.schema)
155
-
156
- const cells = Array(map.width)
157
- .fill(0)
158
- .map((_, col) => {
159
- const headerCol = table.nodeAt(map.map[col] as number)
160
- return cellType.createAndFill({ alignment: headerCol?.attrs.alignment }) as ProsemirrorNode
161
- })
162
-
163
- tr.insert(rowPos, rowType.create(null, cells))
164
- return tr
165
- }
@@ -1,159 +0,0 @@
1
- /* Copyright 2021, Milkdown by Mirone. */
2
- import type { ThemeTaskListItemType } from '@milkdown/core'
3
- import { createCmd, createCmdKey, editorViewCtx } from '@milkdown/core'
4
- import { expectDomTypeError } from '@milkdown/exception'
5
- import { wrapIn } from '@milkdown/prose/commands'
6
- import { wrappingInputRule } from '@milkdown/prose/inputrules'
7
- import { liftListItem, sinkListItem, splitListItem } from '@milkdown/prose/schema-list'
8
- import type { NodeView } from '@milkdown/prose/view'
9
- import { createNode, createShortcut } from '@milkdown/utils'
10
-
11
- import { SupportedKeys } from './supported-keys'
12
-
13
- type Keys = Extract<keyof SupportedKeys, 'SinkListItem' | 'LiftListItem' | 'NextListItem' | 'TaskList'>
14
-
15
- export const SplitTaskListItem = createCmdKey('SplitTaskListItem')
16
- export const SinkTaskListItem = createCmdKey('SinkTaskListItem')
17
- export const LiftTaskListItem = createCmdKey('LiftTaskListItem')
18
- export const TurnIntoTaskList = createCmdKey('TurnIntoTaskList')
19
-
20
- export const taskListItem = createNode<Keys>((utils) => {
21
- const id = 'task_list_item'
22
-
23
- return {
24
- id,
25
- schema: ctx => ({
26
- group: 'listItem',
27
- content: 'paragraph block*',
28
- defining: true,
29
- priority: 60,
30
- attrs: {
31
- checked: {
32
- default: false,
33
- },
34
- },
35
- parseDOM: [
36
- {
37
- tag: 'li[data-type="task-item"]',
38
- getAttrs: (dom) => {
39
- if (!(dom instanceof HTMLElement))
40
- throw expectDomTypeError(dom)
41
-
42
- return { checked: dom.dataset.checked === 'true' }
43
- },
44
- },
45
- ],
46
- toDOM: (node) => {
47
- const checkbox = document.createElement('input')
48
- checkbox.type = 'checkbox'
49
- checkbox.checked = node.attrs.checked
50
- checkbox.className = utils.getClassName(node.attrs, 'task-list-item_checkbox')
51
- checkbox.onchange = (event) => {
52
- const target = event.target
53
- if (!(target instanceof HTMLInputElement))
54
- return
55
- const view = ctx.get(editorViewCtx)
56
-
57
- if (!view.editable) {
58
- checkbox.checked = !checkbox.checked
59
-
60
- return
61
- }
62
-
63
- const { top, left } = target.getBoundingClientRect()
64
- const result = view.posAtCoords({ top, left })
65
- if (!result)
66
- return
67
-
68
- const { tr } = view.state
69
-
70
- view.dispatch(
71
- tr.setNodeMarkup(result.inside, undefined, {
72
- checked: target.checked,
73
- }),
74
- )
75
- }
76
- return [
77
- 'li',
78
- {
79
- 'data-type': 'task-item',
80
- 'data-checked': node.attrs.checked ? 'true' : 'false',
81
- 'class': utils.getClassName(node.attrs, 'task-list-item'),
82
- },
83
- checkbox,
84
- ['span', { class: utils.getClassName(node.attrs, 'task-list-item_body') }, 0],
85
- ]
86
- },
87
- parseMarkdown: {
88
- match: ({ type, checked }) => {
89
- return type === 'listItem' && checked !== null
90
- },
91
- runner: (state, node, type) => {
92
- state.openNode(type, { checked: node.checked as boolean })
93
- state.next(node.children)
94
- state.closeNode()
95
- },
96
- },
97
- toMarkdown: {
98
- match: node => node.type.name === id,
99
- runner: (state, node) => {
100
- state.openNode('listItem', undefined, { checked: node.attrs.checked })
101
- state.next(node.content)
102
- state.closeNode()
103
- },
104
- },
105
- }),
106
- inputRules: nodeType => [
107
- wrappingInputRule(/^\s*(\[([ |x])\])\s$/, nodeType, match => ({
108
- checked: match[match.length - 1] === 'x',
109
- })),
110
- ],
111
- commands: nodeType => [
112
- createCmd(SplitTaskListItem, () => splitListItem(nodeType)),
113
- createCmd(SinkTaskListItem, () => sinkListItem(nodeType)),
114
- createCmd(LiftTaskListItem, () => liftListItem(nodeType)),
115
- createCmd(TurnIntoTaskList, () => wrapIn(nodeType)),
116
- ],
117
- shortcuts: {
118
- [SupportedKeys.NextListItem]: createShortcut(SplitTaskListItem, 'Enter'),
119
- [SupportedKeys.SinkListItem]: createShortcut(SinkTaskListItem, 'Mod-]'),
120
- [SupportedKeys.LiftListItem]: createShortcut(LiftTaskListItem, 'Mod-['),
121
- [SupportedKeys.TaskList]: createShortcut(TurnIntoTaskList, 'Mod-Alt-9'),
122
- },
123
- view: () => (node, view, getPos) => {
124
- let currNode = node
125
-
126
- const renderer = utils.themeManager.get<ThemeTaskListItemType>('task-list-item', {
127
- editable: () => view.editable,
128
- onChange: (selected) => {
129
- const { tr } = view.state
130
- view.dispatch(
131
- tr.setNodeMarkup(getPos(), undefined, {
132
- checked: selected,
133
- }),
134
- )
135
- },
136
- })
137
-
138
- if (!renderer)
139
- return {} as NodeView
140
-
141
- const { dom, contentDOM, onUpdate } = renderer
142
- onUpdate(currNode)
143
-
144
- return {
145
- dom,
146
- contentDOM,
147
- update: (updatedNode) => {
148
- if (updatedNode.type.name !== id)
149
- return false
150
-
151
- currNode = updatedNode
152
- onUpdate(currNode)
153
-
154
- return true
155
- },
156
- }
157
- },
158
- }
159
- })