@whity/ui 0.5.0 → 1.0.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.
- package/README.md +107 -107
- package/package.json +223 -192
- package/src/access-denied.stories.tsx +179 -179
- package/src/access-denied.tsx +305 -305
- package/src/action-proposal.tsx +338 -302
- package/src/actor-chip.tsx +71 -71
- package/src/agent-activity.tsx +163 -163
- package/src/agent-collaboration.stories.tsx +156 -156
- package/src/agent-run-status.tsx +196 -196
- package/src/alert-dialog.stories.tsx +45 -45
- package/src/alert-dialog.tsx +147 -147
- package/src/alert.stories.tsx +91 -91
- package/src/alert.tsx +89 -89
- package/src/app-sidebar.stories.tsx +195 -195
- package/src/app-sidebar.tsx +604 -500
- package/src/approval-queue.tsx +109 -108
- package/src/audience-group-picker.stories.tsx +198 -198
- package/src/audience-group-picker.tsx +362 -362
- package/src/audience-people-picker.stories.tsx +102 -102
- package/src/audience-people-picker.tsx +279 -279
- package/src/avatar-pattern.ts +138 -0
- package/src/avatar.tsx +125 -83
- package/src/badge.stories.tsx +105 -105
- package/src/badge.tsx +68 -68
- package/src/beta-warning.stories.tsx +79 -79
- package/src/beta-warning.tsx +189 -189
- package/src/bilingual-input.stories.tsx +82 -82
- package/src/bilingual-input.tsx +188 -183
- package/src/board.tsx +216 -0
- package/src/breadcrumb.stories.tsx +51 -51
- package/src/breadcrumb.tsx +90 -90
- package/src/built-on-whity.tsx +52 -0
- package/src/button.stories.tsx +87 -87
- package/src/button.tsx +125 -118
- package/src/card.stories.tsx +423 -423
- package/src/card.tsx +180 -180
- package/src/chart.stories.tsx +66 -66
- package/src/chart.tsx +228 -228
- package/src/checkbox.stories.tsx +62 -62
- package/src/checkbox.tsx +54 -54
- package/src/collapsible.stories.tsx +48 -48
- package/src/collapsible.tsx +34 -34
- package/src/color-card.stories.tsx +61 -61
- package/src/color-card.tsx +270 -270
- package/src/color-picker.stories.tsx +65 -65
- package/src/color-picker.tsx +425 -425
- package/src/column-matcher.tsx +84 -0
- package/src/command-palette.tsx +399 -306
- package/src/confirm-action.tsx +98 -0
- package/src/copy-block.stories.tsx +38 -38
- package/src/copy-block.tsx +85 -85
- package/src/data-table.stories.tsx +200 -200
- package/src/data-table.tsx +1687 -1106
- package/src/date-picker.stories.tsx +61 -61
- package/src/date-picker.tsx +457 -422
- package/src/design-cards.stories.tsx +551 -458
- package/src/dialog.stories.tsx +48 -48
- package/src/dialog.tsx +179 -166
- package/src/display-preferences.tsx +59 -0
- package/src/documents/barcode-svg.stories.tsx +72 -72
- package/src/documents/barcode-svg.tsx +79 -79
- package/src/dropdown-menu.stories.tsx +77 -77
- package/src/dropdown-menu.tsx +271 -271
- package/src/editor/agent-panel.tsx +222 -0
- package/src/editor/agents.stories.tsx +81 -0
- package/src/editor/agents.ts +55 -0
- package/src/editor/behaviour.ts +279 -279
- package/src/editor/block-ids.ts +44 -44
- package/src/editor/block-list.tsx +104 -101
- package/src/editor/block-toolbar.tsx +490 -477
- package/src/editor/bubble.tsx +119 -93
- package/src/editor/catalogue.ts +169 -103
- package/src/editor/collab-rail.tsx +746 -746
- package/src/editor/collab.stories.tsx +207 -207
- package/src/editor/collab.ts +87 -87
- package/src/editor/contract.ts +476 -408
- package/src/editor/convert.ts +414 -362
- package/src/editor/doc-editor.stories.tsx +98 -98
- package/src/editor/doc-editor.tsx +1492 -1287
- package/src/editor/editor.css +636 -536
- package/src/editor/extensions.ts +86 -86
- package/src/editor/field-bar.tsx +154 -0
- package/src/editor/field-picker.tsx +109 -107
- package/src/editor/fields.ts +100 -100
- package/src/editor/gutter.tsx +443 -367
- package/src/editor/index.ts +23 -22
- package/src/editor/label-sheet.tsx +289 -289
- package/src/editor/labels.ts +394 -310
- package/src/editor/link-tools.tsx +141 -141
- package/src/editor/node-views.tsx +461 -450
- package/src/editor/page-view.tsx +372 -353
- package/src/editor/phone.tsx +310 -0
- package/src/editor/plugin-block-view.tsx +230 -0
- package/src/editor/plugin-blocks.stories.tsx +96 -0
- package/src/editor/plugin-blocks.ts +52 -0
- package/src/editor/publish-check.tsx +201 -182
- package/src/editor/rich-blocks.stories.tsx +146 -146
- package/src/editor/sample.ts +40 -40
- package/src/editor/save-block-dialog.tsx +141 -0
- package/src/editor/saved-block-view.tsx +205 -0
- package/src/editor/saved-blocks.stories.tsx +105 -0
- package/src/editor/saved-parts.ts +192 -0
- package/src/editor/schema-rich.ts +156 -150
- package/src/editor/schema.ts +368 -315
- package/src/editor/services.tsx +205 -127
- package/src/editor/slash.ts +64 -64
- package/src/editor/suggesting.ts +372 -372
- package/src/editor/suggestions.ts +763 -763
- package/src/editor/table-keys.ts +66 -66
- package/src/editor/templates.stories.tsx +324 -324
- package/src/editor/toolbar.tsx +364 -358
- package/src/empty-state.stories.tsx +51 -51
- package/src/empty-state.tsx +126 -100
- package/src/empty-state.usage-notes.md +30 -30
- package/src/form.stories.tsx +112 -112
- package/src/form.tsx +176 -176
- package/src/globals.css +20 -20
- package/src/icon-button.tsx +85 -85
- package/src/icon-map.ts +335 -244
- package/src/icon.tsx +41 -41
- package/src/index.ts +92 -67
- package/src/input.stories.tsx +157 -157
- package/src/input.tsx +768 -759
- package/src/kpi-strip.tsx +47 -0
- package/src/localized-text.ts +76 -0
- package/src/locked-screen.stories.tsx +29 -29
- package/src/locked-screen.tsx +75 -61
- package/src/lockup.tsx +42 -0
- package/src/lottie.tsx +441 -0
- package/src/mark.tsx +62 -62
- package/src/math-text.stories.tsx +33 -33
- package/src/math-text.tsx +43 -43
- package/src/menu-bar.stories.tsx +198 -198
- package/src/menu-bar.tsx +373 -373
- package/src/message-thread.tsx +181 -0
- package/src/money/currency.ts +244 -230
- package/src/money/money-input.tsx +131 -0
- package/src/money/money.tsx +33 -0
- package/src/open-on-computer.tsx +150 -0
- package/src/otp-input.tsx +161 -0
- package/src/page-header.stories.tsx +95 -95
- package/src/page-header.tsx +122 -122
- package/src/page-shell.stories.tsx +42 -42
- package/src/page-shell.tsx +32 -32
- package/src/page-states.tsx +277 -0
- package/src/pagination.stories.tsx +45 -45
- package/src/pagination.tsx +98 -98
- package/src/paper-sheet.stories.tsx +88 -0
- package/src/paper-sheet.tsx +85 -0
- package/src/phone-input.tsx +252 -0
- package/src/phone-tab-bar.tsx +152 -0
- package/src/plugin-card.stories.tsx +208 -180
- package/src/plugin-card.tsx +931 -848
- package/src/popover.stories.tsx +48 -48
- package/src/popover.tsx +55 -55
- package/src/radio-group.stories.tsx +26 -26
- package/src/radio-group.tsx +44 -44
- package/src/role-summary.stories.tsx +143 -0
- package/src/role-summary.tsx +120 -0
- package/src/route-flow/editor.tsx +1087 -1014
- package/src/route-flow/model.ts +771 -716
- package/src/save-bar.tsx +94 -0
- package/src/scope-grant.tsx +139 -139
- package/src/screen-too-small.stories.tsx +61 -61
- package/src/screen-too-small.tsx +104 -104
- package/src/search-field.tsx +65 -0
- package/src/select.stories.tsx +44 -44
- package/src/select.tsx +197 -197
- package/src/separator.stories.tsx +34 -34
- package/src/separator.tsx +34 -34
- package/src/sheet.stories.tsx +49 -49
- package/src/sheet.tsx +168 -168
- package/src/skeleton.stories.tsx +28 -28
- package/src/skeleton.tsx +19 -19
- package/src/slider.stories.tsx +78 -78
- package/src/slider.tsx +300 -300
- package/src/spinner.stories.tsx +33 -33
- package/src/spinner.tsx +55 -55
- package/src/stepper.tsx +85 -0
- package/src/switch.stories.tsx +42 -42
- package/src/switch.tsx +41 -41
- package/src/switcher.stories.tsx +111 -111
- package/src/switcher.tsx +131 -131
- package/src/system-state.tsx +55 -0
- package/src/table.stories.tsx +48 -48
- package/src/table.tsx +121 -121
- package/src/tabs.stories.tsx +74 -74
- package/src/tabs.tsx +90 -90
- package/src/tag-input.stories.tsx +67 -67
- package/src/tag-input.tsx +117 -117
- package/src/tenant-logo.tsx +63 -63
- package/src/tenant-switcher.stories.tsx +38 -38
- package/src/tenant-switcher.tsx +185 -164
- package/src/textarea.stories.tsx +27 -27
- package/src/textarea.tsx +18 -18
- package/src/theme-mode.ts +74 -52
- package/src/time-grid.tsx +163 -0
- package/src/time-picker.stories.tsx +62 -62
- package/src/time-picker.tsx +616 -615
- package/src/toggle-group.tsx +139 -138
- package/src/tooltip.stories.tsx +43 -43
- package/src/tooltip.tsx +73 -73
- package/src/top-nav.stories.tsx +58 -58
- package/src/top-nav.tsx +324 -324
- package/src/undo-bar.tsx +107 -107
- package/src/utils.ts +45 -45
- package/src/view-toggle.stories.tsx +84 -84
- package/src/view-toggle.tsx +111 -110
package/src/editor/behaviour.ts
CHANGED
|
@@ -1,279 +1,279 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Extension,
|
|
3
|
-
InputRule,
|
|
4
|
-
markInputRule,
|
|
5
|
-
nodeInputRule,
|
|
6
|
-
type Editor,
|
|
7
|
-
} from "@tiptap/core"
|
|
8
|
-
import type { NodeType, Node as PmNode } from "@tiptap/pm/model"
|
|
9
|
-
import { NodeSelection, Plugin, PluginKey, TextSelection } from "@tiptap/pm/state"
|
|
10
|
-
import { Decoration, DecorationSet } from "@tiptap/pm/view"
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Editing behaviour for the document schema (schema.ts): flat lists with
|
|
14
|
-
* levels, Markdown shortcuts (E.28; ⌘Z straight after one turns it back into
|
|
15
|
-
* what was typed), Google Docs shortcuts, and moving blocks from the keyboard.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
const LIST_TYPES = new Set(["listItem", "checkItem"])
|
|
19
|
-
const MAX_LEVEL = 2
|
|
20
|
-
|
|
21
|
-
/** The top-level block holding the selection: its node, position and index. */
|
|
22
|
-
export function currentBlock(editor: Editor): { node: PmNode; pos: number; index: number } | null {
|
|
23
|
-
const { $from } = editor.state.selection
|
|
24
|
-
if ($from.depth < 1) {
|
|
25
|
-
const node = editor.state.doc.nodeAt($from.pos)
|
|
26
|
-
return node ? { node, pos: $from.pos, index: $from.index(0) } : null
|
|
27
|
-
}
|
|
28
|
-
return { node: $from.node(1), pos: $from.before(1), index: $from.index(0) }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Move the block holding the selection one place up (-1) or down (+1). */
|
|
32
|
-
export function moveBlock(editor: Editor, dir: -1 | 1): boolean {
|
|
33
|
-
const block = currentBlock(editor)
|
|
34
|
-
if (!block) return false
|
|
35
|
-
const { doc } = editor.state
|
|
36
|
-
const target = block.index + dir
|
|
37
|
-
if (target < 0 || target >= doc.childCount) return false
|
|
38
|
-
const { node, pos } = block
|
|
39
|
-
const offset = editor.state.selection.from - pos
|
|
40
|
-
const tr = editor.state.tr.delete(pos, pos + node.nodeSize)
|
|
41
|
-
let insertAt = 0
|
|
42
|
-
for (let i = 0; i < target; i++) insertAt += tr.doc.child(i).nodeSize
|
|
43
|
-
tr.insert(insertAt, node)
|
|
44
|
-
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(insertAt + offset, tr.doc.content.size))))
|
|
45
|
-
editor.view.dispatch(tr.scrollIntoView())
|
|
46
|
-
return true
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Put a copy of the current block right after it; the copy gets a new id. */
|
|
50
|
-
export function duplicateBlock(editor: Editor, pos?: number): boolean {
|
|
51
|
-
const at = pos ?? currentBlock(editor)?.pos
|
|
52
|
-
if (at === undefined) return false
|
|
53
|
-
const node = editor.state.doc.nodeAt(at)
|
|
54
|
-
if (!node) return false
|
|
55
|
-
const copy = node.type.create({ ...node.attrs, id: null }, node.content, node.marks)
|
|
56
|
-
editor.view.dispatch(editor.state.tr.insert(at + node.nodeSize, copy).scrollIntoView())
|
|
57
|
-
return true
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Delete the block at `pos` (or the current one). */
|
|
61
|
-
export function deleteBlock(editor: Editor, pos?: number): boolean {
|
|
62
|
-
const at = pos ?? currentBlock(editor)?.pos
|
|
63
|
-
if (at === undefined) return false
|
|
64
|
-
const node = editor.state.doc.nodeAt(at)
|
|
65
|
-
if (!node) return false
|
|
66
|
-
const tr = editor.state.tr.delete(at, at + node.nodeSize)
|
|
67
|
-
if (tr.doc.childCount === 0) tr.insert(0, editor.schema.nodes.paragraph.create())
|
|
68
|
-
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(at, tr.doc.content.size))))
|
|
69
|
-
editor.view.dispatch(tr.scrollIntoView())
|
|
70
|
-
return true
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Move the block at `from` (a position) to be the `index`-th block. */
|
|
74
|
-
export function moveBlockTo(editor: Editor, from: number, index: number): boolean {
|
|
75
|
-
const node = editor.state.doc.nodeAt(from)
|
|
76
|
-
if (!node) return false
|
|
77
|
-
const tr = editor.state.tr.delete(from, from + node.nodeSize)
|
|
78
|
-
const target = Math.max(0, Math.min(index, tr.doc.childCount))
|
|
79
|
-
let insertAt = 0
|
|
80
|
-
for (let i = 0; i < target; i++) insertAt += tr.doc.child(i).nodeSize
|
|
81
|
-
tr.insert(insertAt, node)
|
|
82
|
-
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(insertAt + 1, tr.doc.content.size))))
|
|
83
|
-
editor.view.dispatch(tr.scrollIntoView())
|
|
84
|
-
return true
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Turn the current block into another text block type, keeping its id and content. */
|
|
88
|
-
export function turnInto(editor: Editor, type: string, attrs: Record<string, unknown> = {}): boolean {
|
|
89
|
-
const block = currentBlock(editor)
|
|
90
|
-
if (!block) return false
|
|
91
|
-
return editor.chain().focus().setNode(type, { id: block.node.attrs.id, ...attrs }).run()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function setLevel(editor: Editor, delta: 1 | -1): boolean {
|
|
95
|
-
const block = currentBlock(editor)
|
|
96
|
-
if (!block || !LIST_TYPES.has(block.node.type.name)) return false
|
|
97
|
-
const level = Math.max(0, Math.min(MAX_LEVEL, Number(block.node.attrs.level) + delta))
|
|
98
|
-
if (level === block.node.attrs.level) return true
|
|
99
|
-
editor.view.dispatch(editor.state.tr.setNodeMarkup(block.pos, undefined, { ...block.node.attrs, level }))
|
|
100
|
-
return true
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/* --------------------------------------------------- list numbering */
|
|
104
|
-
|
|
105
|
-
const ALPHA = "abcdefghijklmnopqrstuvwxyz"
|
|
106
|
-
const ROMAN = ["i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii"]
|
|
107
|
-
const BULLETS = ["•", "◦", "▪"]
|
|
108
|
-
|
|
109
|
-
function numberLabel(n: number, level: number): string {
|
|
110
|
-
if (level === 1) return `${ALPHA[(n - 1) % 26]}.`
|
|
111
|
-
if (level === 2) return `${ROMAN[(n - 1) % ROMAN.length] ?? n}.`
|
|
112
|
-
return `${n}.`
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** The markers of flat list items. Numbering restarts after any other block. */
|
|
116
|
-
export function listMarkers(doc: PmNode): Map<number, string> {
|
|
117
|
-
const markers = new Map<number, string>()
|
|
118
|
-
let counters = [0, 0, 0]
|
|
119
|
-
doc.forEach((node, pos) => {
|
|
120
|
-
if (node.type.name !== "listItem" || node.attrs.kind !== "number") {
|
|
121
|
-
counters = [0, 0, 0]
|
|
122
|
-
if (node.type.name === "listItem") markers.set(pos, BULLETS[Number(node.attrs.level)] ?? "•")
|
|
123
|
-
return
|
|
124
|
-
}
|
|
125
|
-
const level = Number(node.attrs.level)
|
|
126
|
-
counters[level] += 1
|
|
127
|
-
for (let l = level + 1; l <= MAX_LEVEL; l++) counters[l] = 0
|
|
128
|
-
markers.set(pos, numberLabel(counters[level], level))
|
|
129
|
-
})
|
|
130
|
-
return markers
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const listMarkerPlugin = new Plugin({
|
|
134
|
-
key: new PluginKey("listMarkers"),
|
|
135
|
-
props: {
|
|
136
|
-
decorations(state) {
|
|
137
|
-
const decos: Decoration[] = []
|
|
138
|
-
for (const [pos, marker] of listMarkers(state.doc)) {
|
|
139
|
-
const node = state.doc.nodeAt(pos)
|
|
140
|
-
if (node) decos.push(Decoration.node(pos, pos + node.nodeSize, { "data-marker": marker }))
|
|
141
|
-
}
|
|
142
|
-
return DecorationSet.create(state.doc, decos)
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* A Markdown block shortcut at the start of a line: like TipTap's
|
|
149
|
-
* textblockTypeInputRule, but the block keeps its id (BlockId is stable).
|
|
150
|
-
*/
|
|
151
|
-
function blockRule(
|
|
152
|
-
find: RegExp,
|
|
153
|
-
type: NodeType,
|
|
154
|
-
attrs: Record<string, unknown> | ((match: RegExpMatchArray) => Record<string, unknown>) = {}
|
|
155
|
-
): InputRule {
|
|
156
|
-
return new InputRule({
|
|
157
|
-
find,
|
|
158
|
-
handler: ({ state, range, match }) => {
|
|
159
|
-
const $start = state.doc.resolve(range.from)
|
|
160
|
-
if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), type)) return null
|
|
161
|
-
const extra = typeof attrs === "function" ? attrs(match) : attrs
|
|
162
|
-
state.tr
|
|
163
|
-
.delete(range.from, range.to)
|
|
164
|
-
.setBlockType(range.from, range.from, type, { ...extra, id: $start.parent.attrs.id ?? null })
|
|
165
|
-
},
|
|
166
|
-
})
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/* ------------------------------------------------------- the extension */
|
|
170
|
-
|
|
171
|
-
export const DocumentBehaviour = Extension.create<{ onBlockMenu: (pos: number) => void }>({
|
|
172
|
-
name: "documentBehaviour",
|
|
173
|
-
|
|
174
|
-
addOptions: () => ({ onBlockMenu: () => {} }),
|
|
175
|
-
|
|
176
|
-
addProseMirrorPlugins() {
|
|
177
|
-
return [listMarkerPlugin]
|
|
178
|
-
},
|
|
179
|
-
|
|
180
|
-
addInputRules() {
|
|
181
|
-
const s = this.editor.schema
|
|
182
|
-
const n = s.nodes
|
|
183
|
-
const m = s.marks
|
|
184
|
-
return [
|
|
185
|
-
blockRule(/^#\s$/, n.heading, { level: 1 }),
|
|
186
|
-
blockRule(/^##\s$/, n.heading, { level: 2 }),
|
|
187
|
-
blockRule(/^###\s$/, n.heading, { level: 3 }),
|
|
188
|
-
blockRule(/^[-*]\s$/, n.listItem, { kind: "bullet", level: 0 }),
|
|
189
|
-
blockRule(/^1[.)]\s$/, n.listItem, { kind: "number", level: 0 }),
|
|
190
|
-
blockRule(/^\[ ?\]\s$/, n.checkItem, { level: 0, checked: false }),
|
|
191
|
-
blockRule(/^>\s$/, n.quote),
|
|
192
|
-
blockRule(/^```([a-z0-9-]*)[\s\n]$/, n.codeBlock, (match) => ({ language: match[1] || null })),
|
|
193
|
-
nodeInputRule({ find: /^(?:---|—-|___)$/, type: n.divider }),
|
|
194
|
-
markInputRule({ find: /(?:^|\s)(\*\*(?!\s)([^*]+)(?<!\s)\*\*)$/, type: m.bold }),
|
|
195
|
-
markInputRule({ find: /(?:^|\s)(_(?!\s)([^_]+)(?<!\s)_)$/, type: m.italic }),
|
|
196
|
-
markInputRule({ find: /(?:^|\s)(`(?!\s)([^`]+)(?<!\s)`)$/, type: m.code }),
|
|
197
|
-
markInputRule({ find: /(?:^|\s)(~~(?!\s)([^~]+)(?<!\s)~~)$/, type: m.strike }),
|
|
198
|
-
markInputRule({ find: /(?:^|\s)(==(?!\s)([^=]+)(?<!\s)==)$/, type: m.highlight }),
|
|
199
|
-
]
|
|
200
|
-
},
|
|
201
|
-
|
|
202
|
-
addKeyboardShortcuts() {
|
|
203
|
-
const editor = this.editor
|
|
204
|
-
const toggleMark = (name: string) => () => editor.chain().focus().toggleMark(name).run()
|
|
205
|
-
const listToggle = (type: string, attrs: Record<string, unknown>) => () => {
|
|
206
|
-
const block = currentBlock(editor)
|
|
207
|
-
const on = block?.node.type.name === type && (type !== "listItem" || block.node.attrs.kind === attrs.kind)
|
|
208
|
-
return on ? turnInto(editor, "paragraph") : turnInto(editor, type, { level: 0, ...attrs })
|
|
209
|
-
}
|
|
210
|
-
return {
|
|
211
|
-
// E.28: straight after a Markdown shortcut, ⌘Z turns it back into what was typed.
|
|
212
|
-
"Mod-z": () => editor.commands.undoInputRule() || editor.commands.undo(),
|
|
213
|
-
"Mod-Shift-z": () => editor.commands.redo(),
|
|
214
|
-
"Mod-y": () => editor.commands.redo(),
|
|
215
|
-
"Mod-b": toggleMark("bold"),
|
|
216
|
-
"Mod-i": toggleMark("italic"),
|
|
217
|
-
"Mod-u": toggleMark("underline"),
|
|
218
|
-
"Mod-Shift-x": toggleMark("strike"),
|
|
219
|
-
"Mod-e": toggleMark("code"),
|
|
220
|
-
"Mod-Shift-h": toggleMark("highlight"),
|
|
221
|
-
"Mod-Alt-0": () => turnInto(editor, "paragraph"),
|
|
222
|
-
"Mod-Alt-1": () => turnInto(editor, "heading", { level: 1 }),
|
|
223
|
-
"Mod-Alt-2": () => turnInto(editor, "heading", { level: 2 }),
|
|
224
|
-
"Mod-Alt-3": () => turnInto(editor, "heading", { level: 3 }),
|
|
225
|
-
"Mod-Shift-8": listToggle("listItem", { kind: "bullet" }),
|
|
226
|
-
"Mod-Shift-7": listToggle("listItem", { kind: "number" }),
|
|
227
|
-
"Mod-Shift-9": listToggle("checkItem", { checked: false }),
|
|
228
|
-
"Mod-d": () => duplicateBlock(editor),
|
|
229
|
-
// Esc selects the whole block; Enter on a selected block opens its menu (EditorBlockChrome).
|
|
230
|
-
Escape: () => {
|
|
231
|
-
const block = currentBlock(editor)
|
|
232
|
-
if (!block || editor.state.selection instanceof NodeSelection) return false
|
|
233
|
-
editor.view.dispatch(editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, block.pos)))
|
|
234
|
-
return true
|
|
235
|
-
},
|
|
236
|
-
"Alt-Shift-ArrowUp": () => moveBlock(editor, -1),
|
|
237
|
-
"Alt-Shift-ArrowDown": () => moveBlock(editor, 1),
|
|
238
|
-
Tab: () => setLevel(editor, 1),
|
|
239
|
-
"Shift-Tab": () => setLevel(editor, -1),
|
|
240
|
-
Enter: () => {
|
|
241
|
-
const sel = editor.state.selection
|
|
242
|
-
if (sel instanceof NodeSelection && sel.$from.depth === 0) {
|
|
243
|
-
this.options.onBlockMenu(sel.from)
|
|
244
|
-
return true
|
|
245
|
-
}
|
|
246
|
-
const block = currentBlock(editor)
|
|
247
|
-
if (!block || !LIST_TYPES.has(block.node.type.name) || !editor.state.selection.empty) return false
|
|
248
|
-
if (block.node.content.size === 0) {
|
|
249
|
-
// An empty item steps out a level, then out of the list.
|
|
250
|
-
if (Number(block.node.attrs.level) > 0) return setLevel(editor, -1)
|
|
251
|
-
return turnInto(editor, "paragraph")
|
|
252
|
-
}
|
|
253
|
-
const { kind, level } = block.node.attrs
|
|
254
|
-
return editor
|
|
255
|
-
.chain()
|
|
256
|
-
.splitBlock()
|
|
257
|
-
.command(({ tr, state }) => {
|
|
258
|
-
const pos = state.selection.$from.before(1)
|
|
259
|
-
const type = block.node.type
|
|
260
|
-
const attrs = type.name === "checkItem" ? { level, checked: false } : { kind, level }
|
|
261
|
-
tr.setNodeMarkup(pos, type, { ...attrs, id: null, suggestion: null })
|
|
262
|
-
return true
|
|
263
|
-
})
|
|
264
|
-
.run()
|
|
265
|
-
},
|
|
266
|
-
Backspace: () => {
|
|
267
|
-
const { selection } = editor.state
|
|
268
|
-
const block = currentBlock(editor)
|
|
269
|
-
if (!block || !selection.empty || selection.$from.parentOffset !== 0) return false
|
|
270
|
-
const name = block.node.type.name
|
|
271
|
-
// At the start of a list item, heading, quote or code block, Backspace makes it text first.
|
|
272
|
-
if (LIST_TYPES.has(name) || name === "heading" || name === "quote" || name === "codeBlock") {
|
|
273
|
-
return turnInto(editor, "paragraph")
|
|
274
|
-
}
|
|
275
|
-
return false
|
|
276
|
-
},
|
|
277
|
-
}
|
|
278
|
-
},
|
|
279
|
-
})
|
|
1
|
+
import {
|
|
2
|
+
Extension,
|
|
3
|
+
InputRule,
|
|
4
|
+
markInputRule,
|
|
5
|
+
nodeInputRule,
|
|
6
|
+
type Editor,
|
|
7
|
+
} from "@tiptap/core"
|
|
8
|
+
import type { NodeType, Node as PmNode } from "@tiptap/pm/model"
|
|
9
|
+
import { NodeSelection, Plugin, PluginKey, TextSelection } from "@tiptap/pm/state"
|
|
10
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Editing behaviour for the document schema (schema.ts): flat lists with
|
|
14
|
+
* levels, Markdown shortcuts (E.28; ⌘Z straight after one turns it back into
|
|
15
|
+
* what was typed), Google Docs shortcuts, and moving blocks from the keyboard.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const LIST_TYPES = new Set(["listItem", "checkItem"])
|
|
19
|
+
const MAX_LEVEL = 2
|
|
20
|
+
|
|
21
|
+
/** The top-level block holding the selection: its node, position and index. */
|
|
22
|
+
export function currentBlock(editor: Editor): { node: PmNode; pos: number; index: number } | null {
|
|
23
|
+
const { $from } = editor.state.selection
|
|
24
|
+
if ($from.depth < 1) {
|
|
25
|
+
const node = editor.state.doc.nodeAt($from.pos)
|
|
26
|
+
return node ? { node, pos: $from.pos, index: $from.index(0) } : null
|
|
27
|
+
}
|
|
28
|
+
return { node: $from.node(1), pos: $from.before(1), index: $from.index(0) }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Move the block holding the selection one place up (-1) or down (+1). */
|
|
32
|
+
export function moveBlock(editor: Editor, dir: -1 | 1): boolean {
|
|
33
|
+
const block = currentBlock(editor)
|
|
34
|
+
if (!block) return false
|
|
35
|
+
const { doc } = editor.state
|
|
36
|
+
const target = block.index + dir
|
|
37
|
+
if (target < 0 || target >= doc.childCount) return false
|
|
38
|
+
const { node, pos } = block
|
|
39
|
+
const offset = editor.state.selection.from - pos
|
|
40
|
+
const tr = editor.state.tr.delete(pos, pos + node.nodeSize)
|
|
41
|
+
let insertAt = 0
|
|
42
|
+
for (let i = 0; i < target; i++) insertAt += tr.doc.child(i).nodeSize
|
|
43
|
+
tr.insert(insertAt, node)
|
|
44
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(insertAt + offset, tr.doc.content.size))))
|
|
45
|
+
editor.view.dispatch(tr.scrollIntoView())
|
|
46
|
+
return true
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Put a copy of the current block right after it; the copy gets a new id. */
|
|
50
|
+
export function duplicateBlock(editor: Editor, pos?: number): boolean {
|
|
51
|
+
const at = pos ?? currentBlock(editor)?.pos
|
|
52
|
+
if (at === undefined) return false
|
|
53
|
+
const node = editor.state.doc.nodeAt(at)
|
|
54
|
+
if (!node) return false
|
|
55
|
+
const copy = node.type.create({ ...node.attrs, id: null }, node.content, node.marks)
|
|
56
|
+
editor.view.dispatch(editor.state.tr.insert(at + node.nodeSize, copy).scrollIntoView())
|
|
57
|
+
return true
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Delete the block at `pos` (or the current one). */
|
|
61
|
+
export function deleteBlock(editor: Editor, pos?: number): boolean {
|
|
62
|
+
const at = pos ?? currentBlock(editor)?.pos
|
|
63
|
+
if (at === undefined) return false
|
|
64
|
+
const node = editor.state.doc.nodeAt(at)
|
|
65
|
+
if (!node) return false
|
|
66
|
+
const tr = editor.state.tr.delete(at, at + node.nodeSize)
|
|
67
|
+
if (tr.doc.childCount === 0) tr.insert(0, editor.schema.nodes.paragraph.create())
|
|
68
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(at, tr.doc.content.size))))
|
|
69
|
+
editor.view.dispatch(tr.scrollIntoView())
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Move the block at `from` (a position) to be the `index`-th block. */
|
|
74
|
+
export function moveBlockTo(editor: Editor, from: number, index: number): boolean {
|
|
75
|
+
const node = editor.state.doc.nodeAt(from)
|
|
76
|
+
if (!node) return false
|
|
77
|
+
const tr = editor.state.tr.delete(from, from + node.nodeSize)
|
|
78
|
+
const target = Math.max(0, Math.min(index, tr.doc.childCount))
|
|
79
|
+
let insertAt = 0
|
|
80
|
+
for (let i = 0; i < target; i++) insertAt += tr.doc.child(i).nodeSize
|
|
81
|
+
tr.insert(insertAt, node)
|
|
82
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.min(insertAt + 1, tr.doc.content.size))))
|
|
83
|
+
editor.view.dispatch(tr.scrollIntoView())
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Turn the current block into another text block type, keeping its id and content. */
|
|
88
|
+
export function turnInto(editor: Editor, type: string, attrs: Record<string, unknown> = {}): boolean {
|
|
89
|
+
const block = currentBlock(editor)
|
|
90
|
+
if (!block) return false
|
|
91
|
+
return editor.chain().focus().setNode(type, { id: block.node.attrs.id, ...attrs }).run()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function setLevel(editor: Editor, delta: 1 | -1): boolean {
|
|
95
|
+
const block = currentBlock(editor)
|
|
96
|
+
if (!block || !LIST_TYPES.has(block.node.type.name)) return false
|
|
97
|
+
const level = Math.max(0, Math.min(MAX_LEVEL, Number(block.node.attrs.level) + delta))
|
|
98
|
+
if (level === block.node.attrs.level) return true
|
|
99
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(block.pos, undefined, { ...block.node.attrs, level }))
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* --------------------------------------------------- list numbering */
|
|
104
|
+
|
|
105
|
+
const ALPHA = "abcdefghijklmnopqrstuvwxyz"
|
|
106
|
+
const ROMAN = ["i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii"]
|
|
107
|
+
const BULLETS = ["•", "◦", "▪"]
|
|
108
|
+
|
|
109
|
+
function numberLabel(n: number, level: number): string {
|
|
110
|
+
if (level === 1) return `${ALPHA[(n - 1) % 26]}.`
|
|
111
|
+
if (level === 2) return `${ROMAN[(n - 1) % ROMAN.length] ?? n}.`
|
|
112
|
+
return `${n}.`
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The markers of flat list items. Numbering restarts after any other block. */
|
|
116
|
+
export function listMarkers(doc: PmNode): Map<number, string> {
|
|
117
|
+
const markers = new Map<number, string>()
|
|
118
|
+
let counters = [0, 0, 0]
|
|
119
|
+
doc.forEach((node, pos) => {
|
|
120
|
+
if (node.type.name !== "listItem" || node.attrs.kind !== "number") {
|
|
121
|
+
counters = [0, 0, 0]
|
|
122
|
+
if (node.type.name === "listItem") markers.set(pos, BULLETS[Number(node.attrs.level)] ?? "•")
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
const level = Number(node.attrs.level)
|
|
126
|
+
counters[level] += 1
|
|
127
|
+
for (let l = level + 1; l <= MAX_LEVEL; l++) counters[l] = 0
|
|
128
|
+
markers.set(pos, numberLabel(counters[level], level))
|
|
129
|
+
})
|
|
130
|
+
return markers
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const listMarkerPlugin = new Plugin({
|
|
134
|
+
key: new PluginKey("listMarkers"),
|
|
135
|
+
props: {
|
|
136
|
+
decorations(state) {
|
|
137
|
+
const decos: Decoration[] = []
|
|
138
|
+
for (const [pos, marker] of listMarkers(state.doc)) {
|
|
139
|
+
const node = state.doc.nodeAt(pos)
|
|
140
|
+
if (node) decos.push(Decoration.node(pos, pos + node.nodeSize, { "data-marker": marker }))
|
|
141
|
+
}
|
|
142
|
+
return DecorationSet.create(state.doc, decos)
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A Markdown block shortcut at the start of a line: like TipTap's
|
|
149
|
+
* textblockTypeInputRule, but the block keeps its id (BlockId is stable).
|
|
150
|
+
*/
|
|
151
|
+
function blockRule(
|
|
152
|
+
find: RegExp,
|
|
153
|
+
type: NodeType,
|
|
154
|
+
attrs: Record<string, unknown> | ((match: RegExpMatchArray) => Record<string, unknown>) = {}
|
|
155
|
+
): InputRule {
|
|
156
|
+
return new InputRule({
|
|
157
|
+
find,
|
|
158
|
+
handler: ({ state, range, match }) => {
|
|
159
|
+
const $start = state.doc.resolve(range.from)
|
|
160
|
+
if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), type)) return null
|
|
161
|
+
const extra = typeof attrs === "function" ? attrs(match) : attrs
|
|
162
|
+
state.tr
|
|
163
|
+
.delete(range.from, range.to)
|
|
164
|
+
.setBlockType(range.from, range.from, type, { ...extra, id: $start.parent.attrs.id ?? null })
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/* ------------------------------------------------------- the extension */
|
|
170
|
+
|
|
171
|
+
export const DocumentBehaviour = Extension.create<{ onBlockMenu: (pos: number) => void }>({
|
|
172
|
+
name: "documentBehaviour",
|
|
173
|
+
|
|
174
|
+
addOptions: () => ({ onBlockMenu: () => {} }),
|
|
175
|
+
|
|
176
|
+
addProseMirrorPlugins() {
|
|
177
|
+
return [listMarkerPlugin]
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
addInputRules() {
|
|
181
|
+
const s = this.editor.schema
|
|
182
|
+
const n = s.nodes
|
|
183
|
+
const m = s.marks
|
|
184
|
+
return [
|
|
185
|
+
blockRule(/^#\s$/, n.heading, { level: 1 }),
|
|
186
|
+
blockRule(/^##\s$/, n.heading, { level: 2 }),
|
|
187
|
+
blockRule(/^###\s$/, n.heading, { level: 3 }),
|
|
188
|
+
blockRule(/^[-*]\s$/, n.listItem, { kind: "bullet", level: 0 }),
|
|
189
|
+
blockRule(/^1[.)]\s$/, n.listItem, { kind: "number", level: 0 }),
|
|
190
|
+
blockRule(/^\[ ?\]\s$/, n.checkItem, { level: 0, checked: false }),
|
|
191
|
+
blockRule(/^>\s$/, n.quote),
|
|
192
|
+
blockRule(/^```([a-z0-9-]*)[\s\n]$/, n.codeBlock, (match) => ({ language: match[1] || null })),
|
|
193
|
+
nodeInputRule({ find: /^(?:---|—-|___)$/, type: n.divider }),
|
|
194
|
+
markInputRule({ find: /(?:^|\s)(\*\*(?!\s)([^*]+)(?<!\s)\*\*)$/, type: m.bold }),
|
|
195
|
+
markInputRule({ find: /(?:^|\s)(_(?!\s)([^_]+)(?<!\s)_)$/, type: m.italic }),
|
|
196
|
+
markInputRule({ find: /(?:^|\s)(`(?!\s)([^`]+)(?<!\s)`)$/, type: m.code }),
|
|
197
|
+
markInputRule({ find: /(?:^|\s)(~~(?!\s)([^~]+)(?<!\s)~~)$/, type: m.strike }),
|
|
198
|
+
markInputRule({ find: /(?:^|\s)(==(?!\s)([^=]+)(?<!\s)==)$/, type: m.highlight }),
|
|
199
|
+
]
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
addKeyboardShortcuts() {
|
|
203
|
+
const editor = this.editor
|
|
204
|
+
const toggleMark = (name: string) => () => editor.chain().focus().toggleMark(name).run()
|
|
205
|
+
const listToggle = (type: string, attrs: Record<string, unknown>) => () => {
|
|
206
|
+
const block = currentBlock(editor)
|
|
207
|
+
const on = block?.node.type.name === type && (type !== "listItem" || block.node.attrs.kind === attrs.kind)
|
|
208
|
+
return on ? turnInto(editor, "paragraph") : turnInto(editor, type, { level: 0, ...attrs })
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
// E.28: straight after a Markdown shortcut, ⌘Z turns it back into what was typed.
|
|
212
|
+
"Mod-z": () => editor.commands.undoInputRule() || editor.commands.undo(),
|
|
213
|
+
"Mod-Shift-z": () => editor.commands.redo(),
|
|
214
|
+
"Mod-y": () => editor.commands.redo(),
|
|
215
|
+
"Mod-b": toggleMark("bold"),
|
|
216
|
+
"Mod-i": toggleMark("italic"),
|
|
217
|
+
"Mod-u": toggleMark("underline"),
|
|
218
|
+
"Mod-Shift-x": toggleMark("strike"),
|
|
219
|
+
"Mod-e": toggleMark("code"),
|
|
220
|
+
"Mod-Shift-h": toggleMark("highlight"),
|
|
221
|
+
"Mod-Alt-0": () => turnInto(editor, "paragraph"),
|
|
222
|
+
"Mod-Alt-1": () => turnInto(editor, "heading", { level: 1 }),
|
|
223
|
+
"Mod-Alt-2": () => turnInto(editor, "heading", { level: 2 }),
|
|
224
|
+
"Mod-Alt-3": () => turnInto(editor, "heading", { level: 3 }),
|
|
225
|
+
"Mod-Shift-8": listToggle("listItem", { kind: "bullet" }),
|
|
226
|
+
"Mod-Shift-7": listToggle("listItem", { kind: "number" }),
|
|
227
|
+
"Mod-Shift-9": listToggle("checkItem", { checked: false }),
|
|
228
|
+
"Mod-d": () => duplicateBlock(editor),
|
|
229
|
+
// Esc selects the whole block; Enter on a selected block opens its menu (EditorBlockChrome).
|
|
230
|
+
Escape: () => {
|
|
231
|
+
const block = currentBlock(editor)
|
|
232
|
+
if (!block || editor.state.selection instanceof NodeSelection) return false
|
|
233
|
+
editor.view.dispatch(editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, block.pos)))
|
|
234
|
+
return true
|
|
235
|
+
},
|
|
236
|
+
"Alt-Shift-ArrowUp": () => moveBlock(editor, -1),
|
|
237
|
+
"Alt-Shift-ArrowDown": () => moveBlock(editor, 1),
|
|
238
|
+
Tab: () => setLevel(editor, 1),
|
|
239
|
+
"Shift-Tab": () => setLevel(editor, -1),
|
|
240
|
+
Enter: () => {
|
|
241
|
+
const sel = editor.state.selection
|
|
242
|
+
if (sel instanceof NodeSelection && sel.$from.depth === 0) {
|
|
243
|
+
this.options.onBlockMenu(sel.from)
|
|
244
|
+
return true
|
|
245
|
+
}
|
|
246
|
+
const block = currentBlock(editor)
|
|
247
|
+
if (!block || !LIST_TYPES.has(block.node.type.name) || !editor.state.selection.empty) return false
|
|
248
|
+
if (block.node.content.size === 0) {
|
|
249
|
+
// An empty item steps out a level, then out of the list.
|
|
250
|
+
if (Number(block.node.attrs.level) > 0) return setLevel(editor, -1)
|
|
251
|
+
return turnInto(editor, "paragraph")
|
|
252
|
+
}
|
|
253
|
+
const { kind, level } = block.node.attrs
|
|
254
|
+
return editor
|
|
255
|
+
.chain()
|
|
256
|
+
.splitBlock()
|
|
257
|
+
.command(({ tr, state }) => {
|
|
258
|
+
const pos = state.selection.$from.before(1)
|
|
259
|
+
const type = block.node.type
|
|
260
|
+
const attrs = type.name === "checkItem" ? { level, checked: false } : { kind, level }
|
|
261
|
+
tr.setNodeMarkup(pos, type, { ...attrs, id: null, suggestion: null })
|
|
262
|
+
return true
|
|
263
|
+
})
|
|
264
|
+
.run()
|
|
265
|
+
},
|
|
266
|
+
Backspace: () => {
|
|
267
|
+
const { selection } = editor.state
|
|
268
|
+
const block = currentBlock(editor)
|
|
269
|
+
if (!block || !selection.empty || selection.$from.parentOffset !== 0) return false
|
|
270
|
+
const name = block.node.type.name
|
|
271
|
+
// At the start of a list item, heading, quote or code block, Backspace makes it text first.
|
|
272
|
+
if (LIST_TYPES.has(name) || name === "heading" || name === "quote" || name === "codeBlock") {
|
|
273
|
+
return turnInto(editor, "paragraph")
|
|
274
|
+
}
|
|
275
|
+
return false
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
})
|
package/src/editor/block-ids.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { Extension } from "@tiptap/core"
|
|
2
|
-
import { Plugin, PluginKey, type Transaction } from "@tiptap/pm/state"
|
|
3
|
-
|
|
4
|
-
/** A short random id, unique within a document (BlockId in the contract). */
|
|
5
|
-
export function newBlockId(): string {
|
|
6
|
-
const bytes = new Uint8Array(9)
|
|
7
|
-
crypto.getRandomValues(bytes)
|
|
8
|
-
return Array.from(bytes, (b) => "abcdefghijklmnopqrstuvwxyz0123456789"[b % 36]).join("")
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Every block, nested ones included, keeps a stable, unique `id`: a new block (typed,
|
|
13
|
-
* split, pasted or duplicated) gets a fresh one, and a duplicate of an
|
|
14
|
-
* existing id is renamed, the first occurrence keeping it.
|
|
15
|
-
*/
|
|
16
|
-
export const BlockIds = Extension.create({
|
|
17
|
-
name: "blockIds",
|
|
18
|
-
addProseMirrorPlugins() {
|
|
19
|
-
return [
|
|
20
|
-
new Plugin({
|
|
21
|
-
key: new PluginKey("blockIds"),
|
|
22
|
-
appendTransaction: (transactions, _old, state) => {
|
|
23
|
-
if (!transactions.some((t) => t.docChanged)) return null
|
|
24
|
-
const seen = new Set<string>()
|
|
25
|
-
let tr: Transaction | null = null
|
|
26
|
-
// Every block, including those inside toggles and columns, has a document-wide unique id.
|
|
27
|
-
state.doc.descendants((node, pos) => {
|
|
28
|
-
if (!("id" in node.attrs)) return
|
|
29
|
-
const id = node.attrs.id as string | null
|
|
30
|
-
if (id && !seen.has(id)) {
|
|
31
|
-
seen.add(id)
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
const fresh = newBlockId()
|
|
35
|
-
seen.add(fresh)
|
|
36
|
-
tr = (tr ?? state.tr).setNodeMarkup(pos, undefined, { ...node.attrs, id: fresh })
|
|
37
|
-
})
|
|
38
|
-
// Id bookkeeping isn't an edit: it never becomes its own undo step.
|
|
39
|
-
return tr ? (tr as Transaction).setMeta("addToHistory", false) : null
|
|
40
|
-
},
|
|
41
|
-
}),
|
|
42
|
-
]
|
|
43
|
-
},
|
|
44
|
-
})
|
|
1
|
+
import { Extension } from "@tiptap/core"
|
|
2
|
+
import { Plugin, PluginKey, type Transaction } from "@tiptap/pm/state"
|
|
3
|
+
|
|
4
|
+
/** A short random id, unique within a document (BlockId in the contract). */
|
|
5
|
+
export function newBlockId(): string {
|
|
6
|
+
const bytes = new Uint8Array(9)
|
|
7
|
+
crypto.getRandomValues(bytes)
|
|
8
|
+
return Array.from(bytes, (b) => "abcdefghijklmnopqrstuvwxyz0123456789"[b % 36]).join("")
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Every block, nested ones included, keeps a stable, unique `id`: a new block (typed,
|
|
13
|
+
* split, pasted or duplicated) gets a fresh one, and a duplicate of an
|
|
14
|
+
* existing id is renamed, the first occurrence keeping it.
|
|
15
|
+
*/
|
|
16
|
+
export const BlockIds = Extension.create({
|
|
17
|
+
name: "blockIds",
|
|
18
|
+
addProseMirrorPlugins() {
|
|
19
|
+
return [
|
|
20
|
+
new Plugin({
|
|
21
|
+
key: new PluginKey("blockIds"),
|
|
22
|
+
appendTransaction: (transactions, _old, state) => {
|
|
23
|
+
if (!transactions.some((t) => t.docChanged)) return null
|
|
24
|
+
const seen = new Set<string>()
|
|
25
|
+
let tr: Transaction | null = null
|
|
26
|
+
// Every block, including those inside toggles and columns, has a document-wide unique id.
|
|
27
|
+
state.doc.descendants((node, pos) => {
|
|
28
|
+
if (!("id" in node.attrs)) return
|
|
29
|
+
const id = node.attrs.id as string | null
|
|
30
|
+
if (id && !seen.has(id)) {
|
|
31
|
+
seen.add(id)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
const fresh = newBlockId()
|
|
35
|
+
seen.add(fresh)
|
|
36
|
+
tr = (tr ?? state.tr).setNodeMarkup(pos, undefined, { ...node.attrs, id: fresh })
|
|
37
|
+
})
|
|
38
|
+
// Id bookkeeping isn't an edit: it never becomes its own undo step.
|
|
39
|
+
return tr ? (tr as Transaction).setMeta("addToHistory", false) : null
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
})
|