@milkdown/preset-gfm 6.5.4 → 7.0.0-next.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.
- package/lib/composed/commands.d.ts +8 -0
- package/lib/composed/commands.d.ts.map +1 -0
- package/lib/composed/index.d.ts +6 -0
- package/lib/composed/index.d.ts.map +1 -0
- package/lib/composed/inputrules.d.ts +3 -0
- package/lib/composed/inputrules.d.ts.map +1 -0
- package/lib/composed/keymap.d.ts +3 -0
- package/lib/composed/keymap.d.ts.map +1 -0
- package/lib/composed/plugins.d.ts +3 -0
- package/lib/composed/plugins.d.ts.map +1 -0
- package/lib/composed/schema.d.ts +3 -0
- package/lib/composed/schema.d.ts.map +1 -0
- package/lib/index.d.ts +4 -34
- package/lib/index.d.ts.map +1 -1
- package/lib/index.es.js +633 -2328
- package/lib/index.es.js.map +1 -1
- package/lib/mark/index.d.ts +2 -0
- package/lib/mark/index.d.ts.map +1 -0
- package/lib/mark/strike-through.d.ts +4 -0
- package/lib/mark/strike-through.d.ts.map +1 -0
- package/lib/node/footnote/definition.d.ts +2 -0
- package/lib/node/footnote/definition.d.ts.map +1 -0
- package/lib/{footnote → node/footnote}/index.d.ts +0 -0
- package/lib/node/footnote/index.d.ts.map +1 -0
- package/lib/node/footnote/reference.d.ts +2 -0
- package/lib/node/footnote/reference.d.ts.map +1 -0
- package/lib/node/index.d.ts +4 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/table/index.d.ts +32 -0
- package/lib/node/table/index.d.ts.map +1 -0
- package/lib/node/table/utils.d.ts +27 -0
- package/lib/node/table/utils.d.ts.map +1 -0
- package/lib/node/task-list-item.d.ts +2 -0
- package/lib/node/task-list-item.d.ts.map +1 -0
- package/lib/plugin/auto-insert-zero-space-plugin.d.ts +2 -0
- package/lib/plugin/auto-insert-zero-space-plugin.d.ts.map +1 -0
- package/lib/plugin/column-resizing-plugin.d.ts +2 -0
- package/lib/plugin/column-resizing-plugin.d.ts.map +1 -0
- package/lib/plugin/index.d.ts +5 -0
- package/lib/plugin/index.d.ts.map +1 -0
- package/lib/plugin/remark-gfm-plugin.d.ts +2 -0
- package/lib/plugin/remark-gfm-plugin.d.ts.map +1 -0
- package/lib/plugin/table-editing-plugin.d.ts +2 -0
- package/lib/plugin/table-editing-plugin.d.ts.map +1 -0
- package/package.json +13 -10
- package/src/composed/commands.ts +23 -0
- package/src/composed/index.ts +6 -0
- package/src/composed/inputrules.ts +7 -0
- package/src/composed/keymap.ts +9 -0
- package/src/composed/plugins.ts +10 -0
- package/src/composed/schema.ts +18 -0
- package/src/index.ts +5 -89
- package/src/mark/index.ts +2 -0
- package/src/mark/strike-through.ts +39 -0
- package/src/node/footnote/definition.ts +68 -0
- package/src/{footnote → node/footnote}/index.ts +0 -0
- package/src/node/footnote/reference.ts +58 -0
- package/src/node/index.ts +4 -0
- package/src/node/table/index.ts +283 -0
- package/src/node/table/utils.ts +489 -0
- package/src/node/task-list-item.ts +87 -0
- package/src/{table/plugin/auto-insert-zero-space.ts → plugin/auto-insert-zero-space-plugin.ts} +13 -15
- package/src/plugin/column-resizing-plugin.ts +4 -0
- package/src/plugin/index.ts +5 -0
- package/src/plugin/remark-gfm-plugin.ts +5 -0
- package/src/plugin/table-editing-plugin.ts +4 -0
- package/lib/footnote/definition.d.ts +0 -3
- package/lib/footnote/definition.d.ts.map +0 -1
- package/lib/footnote/index.d.ts.map +0 -1
- package/lib/footnote/reference.d.ts +0 -3
- package/lib/footnote/reference.d.ts.map +0 -1
- package/lib/footnote/utils.d.ts +0 -3
- package/lib/footnote/utils.d.ts.map +0 -1
- package/lib/strike-through.d.ts +0 -3
- package/lib/strike-through.d.ts.map +0 -1
- package/lib/supported-keys.d.ts +0 -28
- package/lib/supported-keys.d.ts.map +0 -1
- package/lib/table/command.d.ts +0 -4
- package/lib/table/command.d.ts.map +0 -1
- package/lib/table/index.d.ts +0 -10
- package/lib/table/index.d.ts.map +0 -1
- package/lib/table/nodes/index.d.ts +0 -12
- package/lib/table/nodes/index.d.ts.map +0 -1
- package/lib/table/operator-plugin/actions.d.ts +0 -20
- package/lib/table/operator-plugin/actions.d.ts.map +0 -1
- package/lib/table/operator-plugin/calc-pos.d.ts +0 -3
- package/lib/table/operator-plugin/calc-pos.d.ts.map +0 -1
- package/lib/table/operator-plugin/constant.d.ts +0 -6
- package/lib/table/operator-plugin/constant.d.ts.map +0 -1
- package/lib/table/operator-plugin/helper.d.ts +0 -7
- package/lib/table/operator-plugin/helper.d.ts.map +0 -1
- package/lib/table/operator-plugin/index.d.ts +0 -6
- package/lib/table/operator-plugin/index.d.ts.map +0 -1
- package/lib/table/operator-plugin/style.d.ts +0 -3
- package/lib/table/operator-plugin/style.d.ts.map +0 -1
- package/lib/table/operator-plugin/widget.d.ts +0 -8
- package/lib/table/operator-plugin/widget.d.ts.map +0 -1
- package/lib/table/plugin/auto-insert-zero-space.d.ts +0 -3
- package/lib/table/plugin/auto-insert-zero-space.d.ts.map +0 -1
- package/lib/table/plugin/cell-selection.d.ts +0 -40
- package/lib/table/plugin/cell-selection.d.ts.map +0 -1
- package/lib/table/plugin/column-resizing.d.ts +0 -18
- package/lib/table/plugin/column-resizing.d.ts.map +0 -1
- package/lib/table/plugin/commands.d.ts +0 -30
- package/lib/table/plugin/commands.d.ts.map +0 -1
- package/lib/table/plugin/copy-paste.d.ts +0 -14
- package/lib/table/plugin/copy-paste.d.ts.map +0 -1
- package/lib/table/plugin/fix-tables.d.ts +0 -7
- package/lib/table/plugin/fix-tables.d.ts.map +0 -1
- package/lib/table/plugin/index.d.ts +0 -4
- package/lib/table/plugin/index.d.ts.map +0 -1
- package/lib/table/plugin/schema.d.ts +0 -4
- package/lib/table/plugin/schema.d.ts.map +0 -1
- package/lib/table/plugin/table-editing.d.ts +0 -9
- package/lib/table/plugin/table-editing.d.ts.map +0 -1
- package/lib/table/plugin/table-map.d.ts +0 -44
- package/lib/table/plugin/table-map.d.ts.map +0 -1
- package/lib/table/plugin/table-view.d.ts +0 -15
- package/lib/table/plugin/table-view.d.ts.map +0 -1
- package/lib/table/plugin/types.d.ts +0 -15
- package/lib/table/plugin/types.d.ts.map +0 -1
- package/lib/table/plugin/util.d.ts +0 -16
- package/lib/table/plugin/util.d.ts.map +0 -1
- package/lib/table/utils.d.ts +0 -21
- package/lib/table/utils.d.ts.map +0 -1
- package/lib/task-list-item.d.ts +0 -9
- package/lib/task-list-item.d.ts.map +0 -1
- package/src/footnote/definition.ts +0 -187
- package/src/footnote/reference.ts +0 -178
- package/src/footnote/utils.ts +0 -4
- package/src/strike-through.ts +0 -43
- package/src/supported-keys.ts +0 -13
- package/src/table/command.ts +0 -20
- package/src/table/index.ts +0 -13
- package/src/table/nodes/index.ts +0 -189
- package/src/table/operator-plugin/actions.ts +0 -116
- package/src/table/operator-plugin/calc-pos.ts +0 -36
- package/src/table/operator-plugin/constant.ts +0 -7
- package/src/table/operator-plugin/helper.ts +0 -39
- package/src/table/operator-plugin/index.ts +0 -110
- package/src/table/operator-plugin/style.ts +0 -123
- package/src/table/operator-plugin/widget.ts +0 -57
- package/src/table/plugin/cell-selection.ts +0 -381
- package/src/table/plugin/column-resizing.ts +0 -288
- package/src/table/plugin/commands.ts +0 -594
- package/src/table/plugin/copy-paste.ts +0 -322
- package/src/table/plugin/fix-tables.ts +0 -132
- package/src/table/plugin/index.ts +0 -4
- package/src/table/plugin/schema.ts +0 -120
- package/src/table/plugin/table-editing.ts +0 -369
- package/src/table/plugin/table-map.ts +0 -345
- package/src/table/plugin/table-view.ts +0 -80
- package/src/table/plugin/types.ts +0 -16
- package/src/table/plugin/util.ts +0 -119
- package/src/table/utils.ts +0 -165
- 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
|
-
}
|
package/src/table/plugin/util.ts
DELETED
|
@@ -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
|
-
}
|
package/src/table/utils.ts
DELETED
|
@@ -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
|
-
}
|
package/src/task-list-item.ts
DELETED
|
@@ -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
|
-
})
|