@daasboard/editor 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +5 -0
  2. package/README.md +6 -2
  3. package/dist/camera/CameraInput.d.ts +106 -0
  4. package/dist/camera/CameraInput.js +398 -0
  5. package/dist/clipboard/ObjectClipboard.d.ts +40 -0
  6. package/dist/clipboard/ObjectClipboard.js +243 -0
  7. package/dist/clipboard/PasteCommitError.d.ts +4 -0
  8. package/dist/clipboard/PasteCommitError.js +7 -0
  9. package/dist/clipboard/carrier.d.ts +5 -0
  10. package/dist/clipboard/carrier.js +59 -0
  11. package/dist/clipboard/objects.d.ts +21 -0
  12. package/dist/clipboard/objects.js +362 -0
  13. package/dist/clipboard/placement.d.ts +11 -0
  14. package/dist/clipboard/placement.js +21 -0
  15. package/dist/clipboard/systemImages.d.ts +1 -0
  16. package/dist/clipboard/systemImages.js +24 -0
  17. package/dist/index.d.ts +23 -0
  18. package/dist/index.js +22 -0
  19. package/dist/input/TouchInput.d.ts +159 -0
  20. package/dist/input/TouchInput.js +656 -0
  21. package/dist/input/visiblePart.d.ts +8 -0
  22. package/dist/input/visiblePart.js +15 -0
  23. package/dist/interaction/InteractionLayer.d.ts +83 -0
  24. package/dist/interaction/InteractionLayer.js +520 -0
  25. package/dist/props/PropertyController.d.ts +130 -0
  26. package/dist/props/PropertyController.js +1448 -0
  27. package/dist/props/PropertyRegistry.d.ts +38 -0
  28. package/dist/props/PropertyRegistry.js +21 -0
  29. package/dist/props/builtinProperties.d.ts +23 -0
  30. package/dist/props/builtinProperties.js +350 -0
  31. package/dist/props/defaults.d.ts +6 -0
  32. package/dist/props/defaults.js +29 -0
  33. package/dist/selection/SelectionModel.d.ts +25 -0
  34. package/dist/selection/SelectionModel.js +79 -0
  35. package/dist/text/CanvasEditorView.d.ts +99 -0
  36. package/dist/text/CanvasEditorView.js +308 -0
  37. package/dist/text/InputSink.d.ts +157 -0
  38. package/dist/text/InputSink.js +841 -0
  39. package/dist/text/TextEditSession.d.ts +173 -0
  40. package/dist/text/TextEditSession.js +1328 -0
  41. package/dist/text/clipboard.d.ts +11 -0
  42. package/dist/text/clipboard.js +489 -0
  43. package/dist/text/commands.d.ts +48 -0
  44. package/dist/text/commands.js +329 -0
  45. package/dist/text/contextMirror.d.ts +51 -0
  46. package/dist/text/contextMirror.js +292 -0
  47. package/dist/text/flat.d.ts +35 -0
  48. package/dist/text/flat.js +556 -0
  49. package/dist/text/graphemes.d.ts +16 -0
  50. package/dist/text/graphemes.js +219 -0
  51. package/dist/text/index.d.ts +13 -0
  52. package/dist/text/index.js +13 -0
  53. package/dist/text/inputReducer.d.ts +24 -0
  54. package/dist/text/inputReducer.js +44 -0
  55. package/dist/text/keymap.d.ts +17 -0
  56. package/dist/text/keymap.js +148 -0
  57. package/dist/text/schema.d.ts +12 -0
  58. package/dist/text/schema.js +44 -0
  59. package/dist/text/sessionFocus.d.ts +20 -0
  60. package/dist/text/sessionFocus.js +51 -0
  61. package/dist/text/types.d.ts +114 -0
  62. package/dist/text/types.js +1 -0
  63. package/dist/tools/CreateTool.d.ts +78 -0
  64. package/dist/tools/CreateTool.js +258 -0
  65. package/dist/tools/SelectTool.d.ts +155 -0
  66. package/dist/tools/SelectTool.js +808 -0
  67. package/dist/tools/ToolManager.d.ts +63 -0
  68. package/dist/tools/ToolManager.js +146 -0
  69. package/dist/tools/connectorEndpoints.d.ts +17 -0
  70. package/dist/tools/connectorEndpoints.js +53 -0
  71. package/dist/tools/frameRotation.d.ts +8 -0
  72. package/dist/tools/frameRotation.js +66 -0
  73. package/dist/tools/gradientHandles.d.ts +67 -0
  74. package/dist/tools/gradientHandles.js +110 -0
  75. package/dist/tools/presetPaths.d.ts +5 -0
  76. package/dist/tools/presetPaths.js +49 -0
  77. package/dist/tools/snapResize.d.ts +7 -0
  78. package/dist/tools/snapResize.js +65 -0
  79. package/dist/tools/snapping.d.ts +30 -0
  80. package/dist/tools/snapping.js +280 -0
  81. package/dist/tools/transform.d.ts +52 -0
  82. package/dist/tools/transform.js +277 -0
  83. package/package.json +36 -3
@@ -0,0 +1,11 @@
1
+ import { Slice } from "prosemirror-model";
2
+ import type { EditorState, Transaction } from "prosemirror-state";
3
+ export declare const RICH_TEXT_MIME = "application/x-daas-text+json";
4
+ export interface ClipboardContent {
5
+ text: string;
6
+ html?: string;
7
+ richText?: string;
8
+ }
9
+ export declare function serializeClipboard(slice: Slice, startsAtParagraphStart?: boolean): ClipboardContent;
10
+ export declare function parseClipboard(content: ClipboardContent, ownerDocument?: Document | undefined): Slice | null;
11
+ export declare function clipboardTransaction(state: EditorState, content: ClipboardContent, ownerDocument?: Document): Transaction | null;
@@ -0,0 +1,489 @@
1
+ import { HARD_LINE_BREAK, TEXT_CASES } from "@daasboard/doc";
2
+ import { DOMParser as PMDOMParser, Fragment, Slice } from "prosemirror-model";
3
+ import { CARRIER_ATTRIBUTE } from "../clipboard/carrier.js";
4
+ import { MAX_INDENT } from "./commands.js";
5
+ import { normalizePasteText } from "./inputReducer.js";
6
+ import { schema } from "./schema.js";
7
+ export const RICH_TEXT_MIME = "application/x-daas-text+json";
8
+ const escapeHTML = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
9
+ const record = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : {};
10
+ const cleanString = (v) => typeof v === "string" && v.length > 0 ? v : null;
11
+ const clampFontSize = (size) => Math.max(4, Math.min(400, size));
12
+ const BLOCK_TAGS = "p,div,h1,h2,h3,h4,h5,h6,li,blockquote,pre";
13
+ const IGNORED_TAGS = "script,style,iframe,object,embed,template,noscript,head,meta,link,base";
14
+ const COLOR_CONTEXTS = new WeakMap();
15
+ function clipboardColor(value, ownerDocument) {
16
+ if (typeof value !== "string")
17
+ return null;
18
+ const color = value.trim().toLowerCase();
19
+ if (/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/.test(color)) {
20
+ return color.length <= 5 ? "#" + [...color.slice(1)].map((c) => c + c).join("") : color;
21
+ }
22
+ const rgb = /^rgba?\(([^)]+)\)$/.exec(color);
23
+ if (rgb) {
24
+ const components = rgb[1].trim().split(/[\s,/]+/);
25
+ if (components.length < 3 || components.length > 4)
26
+ return null;
27
+ const numbers = components.map((part, i) => {
28
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?%?$/.test(part))
29
+ return NaN;
30
+ const scale = i === 3 ? 1 : 255;
31
+ const n = part.endsWith("%") ? parseFloat(part) * scale / 100 : Number(part);
32
+ return Math.max(0, Math.min(scale, n));
33
+ });
34
+ if (!numbers.every(Number.isFinite))
35
+ return null;
36
+ const bytes = numbers.map((n, i) => Math.round(i === 3 ? n * 255 : n));
37
+ if (bytes.length === 4 && bytes[3] === 255)
38
+ bytes.pop();
39
+ return "#" + bytes.map((n) => n.toString(16).padStart(2, "0")).join("");
40
+ }
41
+ if (!ownerDocument || !/^[a-z]+$/.test(color) || /^(currentcolor|inherit|initial|unset|revert)$/.test(color))
42
+ return null;
43
+ if (!COLOR_CONTEXTS.has(ownerDocument))
44
+ COLOR_CONTEXTS.set(ownerDocument, ownerDocument.createElement("canvas").getContext("2d"));
45
+ const context = COLOR_CONTEXTS.get(ownerDocument);
46
+ if (!context)
47
+ return null;
48
+ context.fillStyle = "#010203";
49
+ context.fillStyle = color;
50
+ const first = context.fillStyle;
51
+ context.fillStyle = "#040506";
52
+ context.fillStyle = color;
53
+ return first === context.fillStyle ? clipboardColor(first) : null;
54
+ }
55
+ function cleanRichText(text) {
56
+ return text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDFFF]/g, (part) => part.length === 2 ? part : "\uFFFD")
57
+ .replace(/\r\n?/g, "\n").replace(/[^\P{Cc}\t\n]/gu, "");
58
+ }
59
+ function normalizeRichSlice(slice) {
60
+ const source = [];
61
+ let inline = [];
62
+ const flushInline = () => {
63
+ if (inline.length)
64
+ source.push(schema.nodes.paragraph.create(null, inline));
65
+ inline = [];
66
+ };
67
+ slice.content.forEach((node) => {
68
+ if (node.isInline)
69
+ inline.push(node);
70
+ else {
71
+ flushInline();
72
+ source.push(node);
73
+ }
74
+ });
75
+ flushInline();
76
+ const paragraphs = [];
77
+ for (const para of source) {
78
+ let children = [];
79
+ para.forEach((child) => {
80
+ if (!child.isText) {
81
+ children.push(child);
82
+ return;
83
+ }
84
+ const parts = cleanRichText(child.text).split(/([\u2028\u2029\n])/u);
85
+ for (const part of parts) {
86
+ if (part === "\u2029") {
87
+ paragraphs.push(para.copy(Fragment.from(children)));
88
+ children = [];
89
+ }
90
+ else if (part === HARD_LINE_BREAK || part === "\n")
91
+ children.push(schema.nodes.hard_break.create(null, null, child.marks));
92
+ else if (part)
93
+ children.push(schema.text(part, child.marks));
94
+ }
95
+ });
96
+ paragraphs.push(para.copy(Fragment.from(children)));
97
+ }
98
+ return new Slice(Fragment.from(paragraphs), slice.content.firstChild?.isInline ? 1 : slice.openStart, slice.content.lastChild?.isInline ? 1 : slice.openEnd);
99
+ }
100
+ function safeHref(value) {
101
+ const href = cleanString(value)?.trim();
102
+ if (!href || /[\u0000-\u0020\u007f]/u.test(href))
103
+ return null;
104
+ const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(href)?.[1]?.toLowerCase();
105
+ return !scheme || ["http", "https", "mailto", "tel"].includes(scheme) ? href : null;
106
+ }
107
+ function paragraphAttrs(value) {
108
+ const attrs = record(value);
109
+ const align = ["left", "center", "right", "justify"].includes(String(attrs.align)) ? attrs.align : null;
110
+ const list = attrs.list === "bullet" || attrs.list === "number" || attrs.list === "task" ? attrs.list : null;
111
+ const indent = typeof attrs.indent === "number" && Number.isFinite(attrs.indent) ? Math.max(0, Math.min(MAX_INDENT, Math.round(attrs.indent))) : 0;
112
+ const lineHeight = typeof attrs.lineHeight === "number" && Number.isFinite(attrs.lineHeight) && attrs.lineHeight > 0 ? attrs.lineHeight : null;
113
+ const start = list === "number" && typeof attrs.start === "number" && Number.isInteger(attrs.start) && attrs.start >= 0 ? attrs.start : null;
114
+ const checked = list === "task" && attrs.checked === true ? true : null;
115
+ return { align, lineHeight, list, indent, start, checked };
116
+ }
117
+ function readMark(value, ownerDocument) {
118
+ const mark = record(value);
119
+ const attrs = record(mark.attrs);
120
+ const type = String(mark.type);
121
+ if (type === "italic" || type === "underline" || type === "strike")
122
+ return schema.marks[type].create({ value: attrs.value !== false });
123
+ if (type === "weight" || type === "bold") {
124
+ const weight = type === "bold" ? 700 : attrs.value;
125
+ return typeof weight === "number" && Number.isFinite(weight) && weight >= 100 && weight <= 900 ? schema.marks.weight.create({ value: weight }) : null;
126
+ }
127
+ if (type === "letterSpacing") {
128
+ const spacing = attrs.value;
129
+ return typeof spacing === "number" && Number.isFinite(spacing) ? schema.marks.letterSpacing.create({ value: spacing }) : null;
130
+ }
131
+ if (type === "textCase")
132
+ return TEXT_CASES.has(attrs.value) ? schema.marks.textCase.create({ value: attrs.value }) : null;
133
+ if (mark.type === "link") {
134
+ const href = safeHref(attrs.href);
135
+ return href ? schema.marks.link.create({ href }) : null;
136
+ }
137
+ if (mark.type === "size") {
138
+ const size = attrs.value;
139
+ return typeof size === "number" && Number.isFinite(size) && size > 0 ? schema.marks.size.create({ value: clampFontSize(size) }) : null;
140
+ }
141
+ if (mark.type === "color") {
142
+ const color = clipboardColor(attrs.value, ownerDocument);
143
+ return color ? schema.marks.color.create({ value: color }) : null;
144
+ }
145
+ if (mark.type === "family") {
146
+ const text = cleanString(attrs.value);
147
+ return text ? schema.marks[mark.type].create({ value: text }) : null;
148
+ }
149
+ return null;
150
+ }
151
+ function readRichSlice(raw, ownerDocument) {
152
+ try {
153
+ const payload = record(JSON.parse(raw));
154
+ if (payload.version !== 1)
155
+ return null;
156
+ const value = record(payload.slice);
157
+ if (!Array.isArray(value.content) || value.content.length === 0)
158
+ return null;
159
+ const paragraphs = [];
160
+ for (const item of value.content) {
161
+ const para = record(item);
162
+ if (para.type !== "paragraph" || (para.content !== undefined && !Array.isArray(para.content)))
163
+ return null;
164
+ const children = [];
165
+ for (const child of (para.content ?? [])) {
166
+ const node = record(child);
167
+ let marks = [];
168
+ if (Array.isArray(node.marks)) {
169
+ for (const raw of node.marks) {
170
+ const mark = readMark(raw, ownerDocument);
171
+ if (mark)
172
+ marks = mark.addToSet(marks);
173
+ }
174
+ }
175
+ if (node.type === "hard_break")
176
+ children.push(schema.nodes.hard_break.create(null, null, marks));
177
+ else if (node.type === "text" && typeof node.text === "string" && node.text.length > 0) {
178
+ const parts = cleanRichText(node.text).split(/[\n\u2028]/u);
179
+ parts.forEach((part, i) => {
180
+ if (i > 0)
181
+ children.push(schema.nodes.hard_break.create(null, null, marks));
182
+ if (part)
183
+ children.push(schema.text(part, marks));
184
+ });
185
+ }
186
+ else
187
+ return null;
188
+ }
189
+ paragraphs.push(schema.nodes.paragraph.create(paragraphAttrs(para.attrs), children));
190
+ }
191
+ if ((value.openStart !== undefined && value.openStart !== 0 && value.openStart !== 1) ||
192
+ (value.openEnd !== undefined && value.openEnd !== 0 && value.openEnd !== 1))
193
+ return null;
194
+ const slice = normalizeRichSlice(new Slice(Fragment.from(paragraphs), Number(value.openStart ?? 0), Number(value.openEnd ?? 0)));
195
+ return { slice, startsAtParagraphStart: payload.startsAtParagraphStart === true || (payload.startsAtParagraphStart === undefined && slice.openStart === 0) };
196
+ }
197
+ catch {
198
+ return null;
199
+ }
200
+ }
201
+ function inlineHTML(node) {
202
+ let html = node.type.name === "hard_break" ? "<br>" : escapeHTML(node.text ?? "");
203
+ for (let i = node.marks.length - 1; i >= 0; i--) {
204
+ const mark = node.marks[i];
205
+ const name = mark.type.name;
206
+ const tag = { italic: "em", underline: "u", strike: "s" }[name];
207
+ if (name === "weight") {
208
+ const weight = Number(mark.attrs.value);
209
+ html = weight === 700 ? `<strong>${html}</strong>` : `<span style="font-weight:${weight}">${html}</span>`;
210
+ }
211
+ else if (tag) {
212
+ if (mark.attrs.value !== false)
213
+ html = `<${tag}>${html}</${tag}>`;
214
+ else
215
+ html = `<span data-daas-off="${name}" style="${name === "italic" ? "font-style:normal" : "text-decoration:none"}">${html}</span>`;
216
+ }
217
+ else if (name === "link") {
218
+ const href = safeHref(mark.attrs.href);
219
+ if (href)
220
+ html = `<a href="${escapeHTML(href)}">${html}</a>`;
221
+ }
222
+ else if (name === "textCase") {
223
+ const v = TEXT_CASES.has(mark.attrs.value) ? String(mark.attrs.value) : "none";
224
+ html = `<span data-daas-textcase="${v}" style="text-transform:${v === "upper" ? "uppercase" : v === "lower" ? "lowercase" : "none"}">${html}</span>`;
225
+ }
226
+ else if (name === "size" || name === "color" || name === "family" || name === "letterSpacing") {
227
+ const property = { size: "font-size", color: "color", family: "font-family", letterSpacing: "letter-spacing" }[name];
228
+ const value = String(mark.attrs.value) + (name === "size" || name === "letterSpacing" ? "px" : "");
229
+ if (!/[;{}]/.test(value))
230
+ html = `<span style="${property}:${escapeHTML(value)}">${html}</span>`;
231
+ }
232
+ }
233
+ return html;
234
+ }
235
+ export function serializeClipboard(slice, startsAtParagraphStart = true) {
236
+ const paragraphs = [];
237
+ let openList = null;
238
+ const closeList = () => {
239
+ if (openList)
240
+ paragraphs.push(`</${openList.tag}>`);
241
+ openList = null;
242
+ };
243
+ slice.content.forEach((para, _offset, index) => {
244
+ let inline = "";
245
+ para.forEach((child) => { inline += inlineHTML(child); });
246
+ const attrs = paragraphAttrs(para.attrs);
247
+ const styles = ["white-space:pre-wrap"];
248
+ if (attrs.align)
249
+ styles.push(`text-align:${attrs.align}`);
250
+ if (attrs.indent)
251
+ styles.push(`margin-left:${Number(attrs.indent) * 24}px`);
252
+ if (attrs.lineHeight)
253
+ styles.push(`line-height:${attrs.lineHeight}`);
254
+ const common = ` data-daas-indent="${attrs.indent}"${index === 0 ? ` data-daas-copy-start="${startsAtParagraphStart}"` : ""}${styles.length ? ` style="${styles.join(";")}"` : ""}`;
255
+ if (attrs.list) {
256
+ const kind = String(attrs.list);
257
+ const tag = kind === "number" ? "ol" : "ul";
258
+ const indent = Number(attrs.indent);
259
+ if (openList?.kind !== kind || openList.indent !== indent) {
260
+ closeList();
261
+ paragraphs.push(kind === "task" ? `<ul data-daas-list="task">` : kind === "number" && attrs.start !== null ? `<ol start="${attrs.start}">` : `<${tag}>`);
262
+ openList = { tag, kind, indent };
263
+ }
264
+ paragraphs.push(`<li${common}${attrs.checked ? ` data-daas-checked="true"` : ""}>${inline}</li>`);
265
+ }
266
+ else {
267
+ closeList();
268
+ paragraphs.push(`<p${common}>${inline}</p>`);
269
+ }
270
+ });
271
+ closeList();
272
+ return {
273
+ text: slice.content.textBetween(0, slice.content.size, "\n", "\n").replaceAll(HARD_LINE_BREAK, "\n"),
274
+ html: paragraphs.join(""),
275
+ richText: JSON.stringify({ version: 1, startsAtParagraphStart, slice: slice.toJSON() }),
276
+ };
277
+ }
278
+ function elementParagraphAttrs(dom) {
279
+ let list = null;
280
+ let start = null;
281
+ let checked = null;
282
+ if (dom.tagName.toLowerCase() === "li") {
283
+ const parent = dom.parentElement;
284
+ const tag = parent?.tagName.toLowerCase();
285
+ list = tag === "ol" ? "number" : parent?.getAttribute("data-daas-list") === "task" ? "task" : "bullet";
286
+ if (list === "number" && parent?.hasAttribute("start") && dom.previousElementSibling === null)
287
+ start = Number(parent.getAttribute("start"));
288
+ if (list === "task" && dom.getAttribute("data-daas-checked") === "true")
289
+ checked = true;
290
+ }
291
+ let indent = Number(dom.getAttribute("data-daas-indent"));
292
+ if (!dom.hasAttribute("data-daas-indent")) {
293
+ let depth = 0;
294
+ for (let parent = dom.parentElement; parent; parent = parent.parentElement)
295
+ if (/^(UL|OL)$/.test(parent.tagName))
296
+ depth++;
297
+ indent = list ? Math.max(0, depth - 1) : Math.round(parseFloat(dom.style.marginLeft || "0") / 24);
298
+ }
299
+ const lineHeight = /^[0-9]*\.?[0-9]+$/.test(dom.style.lineHeight.trim()) ? Number(dom.style.lineHeight) : null;
300
+ return paragraphAttrs({ align: dom.style.textAlign || dom.getAttribute("align"), list, indent, lineHeight, start, checked });
301
+ }
302
+ function cssWeight(value) {
303
+ const v = String(value ?? "")
304
+ .trim()
305
+ .toLowerCase();
306
+ if (!v)
307
+ return undefined;
308
+ if (v === "bold" || v === "bolder")
309
+ return 700;
310
+ if (v === "normal" || v === "lighter")
311
+ return null;
312
+ const n = Number(v);
313
+ return Number.isFinite(n) && n >= 100 && n <= 900 ? n : undefined;
314
+ }
315
+ function htmlParser(ownerDocument) {
316
+ const rules = [
317
+ { tag: IGNORED_TAGS, ignore: true },
318
+ { tag: "div,blockquote", node: "paragraph", getAttrs: (dom) => {
319
+ const children = [...dom.childNodes].filter((child) => child.nodeType !== 8 && (child.nodeType !== 3 || !!child.textContent?.trim()));
320
+ return children.length > 0 && children.every((child) => child.nodeType === 1 && child.matches(`${BLOCK_TAGS},ul,ol`)) ? false : elementParagraphAttrs(dom);
321
+ } },
322
+ { tag: "li > p", skip: true },
323
+ { tag: "p,h1,h2,h3,h4,h5,h6,li,pre", node: "paragraph", getAttrs: (dom) => elementParagraphAttrs(dom) },
324
+ { tag: "br", node: "hard_break" },
325
+ { tag: "strong,b", mark: "weight", getAttrs: (dom) => {
326
+ const w = cssWeight(dom.style.fontWeight);
327
+ return w === null ? false : { value: w ?? 700 };
328
+ } },
329
+ { tag: "em,i", mark: "italic" },
330
+ { tag: "u", mark: "underline" },
331
+ { tag: "s,strike,del", mark: "strike" },
332
+ { tag: "span[data-daas-off=italic]", mark: "italic", getAttrs: () => ({ value: false }) },
333
+ { tag: "span[data-daas-off=underline]", mark: "underline", getAttrs: () => ({ value: false }) },
334
+ { tag: "span[data-daas-off=strike]", mark: "strike", getAttrs: () => ({ value: false }) },
335
+ { tag: "a[href]", mark: "link", getAttrs: (dom) => {
336
+ const href = safeHref(dom.getAttribute("href"));
337
+ return href ? { href } : false;
338
+ } },
339
+ { style: "font-weight", clearMark: (mark) => mark.type.name === "weight", getAttrs: (value) => (cssWeight(String(value)) === null ? null : false) },
340
+ { style: "font-weight", mark: "weight", getAttrs: (value) => {
341
+ const w = cssWeight(String(value));
342
+ return typeof w === "number" ? { value: w } : false;
343
+ } },
344
+ { style: "letter-spacing", mark: "letterSpacing", getAttrs: (value) => {
345
+ const match = /^(-?[0-9]+(?:\.[0-9]+)?)px$/.exec(String(value).trim());
346
+ return match ? { value: Number(match[1]) } : false;
347
+ } },
348
+ { tag: "span[data-daas-textcase]", mark: "textCase", getAttrs: (dom) => {
349
+ const v = dom.getAttribute("data-daas-textcase");
350
+ return TEXT_CASES.has(v) ? { value: v } : false;
351
+ } },
352
+ { style: "text-transform", mark: "textCase", getAttrs: (value) => (/^uppercase$/i.test(String(value).trim()) ? { value: "upper" } : /^lowercase$/i.test(String(value).trim()) ? { value: "lower" } : false) },
353
+ { style: "font-style", mark: "italic", getAttrs: (value) => (/^(italic|oblique)/.test(String(value)) ? null : /^normal$/.test(String(value).trim()) ? { value: false } : false) },
354
+ { style: "text-decoration", mark: "underline", consuming: false, getAttrs: (value) => String(value).includes("underline") ? null : false },
355
+ { style: "text-decoration", mark: "strike", getAttrs: (value) => String(value).includes("line-through") ? null : false },
356
+ { style: "color", mark: "color", getAttrs: (value) => {
357
+ const color = clipboardColor(value, ownerDocument);
358
+ return color ? { value: color } : false;
359
+ } },
360
+ { style: "font-family", mark: "family", getAttrs: (value) => value ? { value } : false },
361
+ { style: "font-size", mark: "size", getAttrs: (value) => {
362
+ const match = /^([0-9]+(?:\.[0-9]+)?(?:e[+-]?[0-9]+)?)(px|pt)$/i.exec(String(value));
363
+ const size = match ? Number(match[1]) * (match[2] === "pt" ? 4 / 3 : 1) : 0;
364
+ return size > 0 && Number.isFinite(size) ? { value: clampFontSize(size) } : false;
365
+ } },
366
+ ];
367
+ return new PMDOMParser(schema, rules);
368
+ }
369
+ function startsWithParagraph(node, inParagraph = false) {
370
+ for (const child of node.childNodes) {
371
+ if (child.nodeType === 8 || (child.nodeType === 3 && !child.textContent?.trim()))
372
+ continue;
373
+ if (child.nodeType !== 1)
374
+ return inParagraph;
375
+ const element = child;
376
+ if (element.matches(IGNORED_TAGS))
377
+ continue;
378
+ if (element.hasAttribute(CARRIER_ATTRIBUTE) && !element.firstElementChild && !element.textContent?.trim())
379
+ continue;
380
+ const boundary = element.getAttribute("data-daas-copy-start");
381
+ if (boundary === "true" || boundary === "false")
382
+ return boundary === "true";
383
+ return startsWithParagraph(element, inParagraph || element.matches(BLOCK_TAGS));
384
+ }
385
+ return inParagraph;
386
+ }
387
+ function contextualFragment(html, ownerDocument) {
388
+ const context = ownerDocument.createElement("template");
389
+ context.innerHTML = html;
390
+ const comments = ownerDocument.createTreeWalker(context.content, 128);
391
+ let start = null;
392
+ let end = null;
393
+ for (let node = comments.nextNode(); node; node = comments.nextNode()) {
394
+ if (!start && /^StartFragment$/i.test(node.nodeValue?.trim() ?? ""))
395
+ start = node;
396
+ else if (start && /^EndFragment$/i.test(node.nodeValue?.trim() ?? "")) {
397
+ end = node;
398
+ break;
399
+ }
400
+ }
401
+ if (!start || !end)
402
+ return null;
403
+ const range = ownerDocument.createRange();
404
+ range.setStartAfter(start);
405
+ range.setEndBefore(end);
406
+ let selected = range.cloneContents();
407
+ for (let ancestor = range.commonAncestorContainer; ancestor && ancestor !== context.content; ancestor = ancestor.parentNode) {
408
+ const wrapper = ancestor.cloneNode(false);
409
+ wrapper.appendChild(selected);
410
+ selected = context.content.ownerDocument.createDocumentFragment();
411
+ selected.appendChild(wrapper);
412
+ }
413
+ return selected;
414
+ }
415
+ function parseClipboardData(content, ownerDocument) {
416
+ if (content.richText) {
417
+ const parsed = readRichSlice(content.richText, ownerDocument);
418
+ if (parsed)
419
+ return parsed;
420
+ }
421
+ if (!content.html || !ownerDocument)
422
+ return null;
423
+ const template = ownerDocument.createElement("template");
424
+ const fragment = /<!--\s*StartFragment\s*-->([\s\S]*?)<!--\s*EndFragment\s*-->/i.exec(content.html);
425
+ template.innerHTML = fragment ? fragment[1] : content.html;
426
+ const startsAtParagraphStart = startsWithParagraph(template.content);
427
+ if (fragment) {
428
+ const contextual = contextualFragment(content.html, ownerDocument);
429
+ if (contextual)
430
+ template.content.replaceChildren(contextual);
431
+ }
432
+ for (const ignored of template.content.querySelectorAll(IGNORED_TAGS))
433
+ ignored.remove();
434
+ const tables = template.content.querySelectorAll("table");
435
+ if (tables.length && content.text)
436
+ return null;
437
+ for (const table of tables) {
438
+ const rows = ownerDocument.createDocumentFragment();
439
+ for (const row of table.querySelectorAll("tr")) {
440
+ if (row.closest("table") !== table)
441
+ continue;
442
+ const paragraph = ownerDocument.createElement("p");
443
+ paragraph.style.whiteSpace = "pre-wrap";
444
+ paragraph.textContent = [...row.children].filter((cell) => cell.matches("td,th"))
445
+ .map((cell) => cleanRichText(cell.textContent ?? "").replace(/[\n\u2028\u2029]/g, " ")).join("\t");
446
+ rows.append(paragraph);
447
+ }
448
+ table.replaceWith(rows);
449
+ }
450
+ const texts = ownerDocument.createTreeWalker(template.content, 4);
451
+ for (let node = texts.nextNode(); node; node = texts.nextNode())
452
+ node.nodeValue = cleanRichText(node.nodeValue ?? "");
453
+ const slice = normalizeRichSlice(htmlParser(ownerDocument).parseSlice(template.content));
454
+ return slice.content.size ? { slice, startsAtParagraphStart } : null;
455
+ }
456
+ export function parseClipboard(content, ownerDocument = typeof document === "undefined" ? undefined : document) {
457
+ return parseClipboardData(content, ownerDocument)?.slice ?? null;
458
+ }
459
+ export function clipboardTransaction(state, content, ownerDocument) {
460
+ const parsed = parseClipboardData(content, ownerDocument ?? (typeof document === "undefined" ? undefined : document));
461
+ let slice = parsed?.slice;
462
+ if (!slice) {
463
+ const plain = normalizePasteText(content.text);
464
+ if (!plain)
465
+ return null;
466
+ const marks = state.storedMarks ?? state.selection.$from.marks();
467
+ const attrs = state.selection.$from.parent.attrs;
468
+ const paragraphs = plain.split("\n").map((text) => {
469
+ const children = [];
470
+ text.split(HARD_LINE_BREAK).forEach((part, i) => {
471
+ if (i > 0)
472
+ children.push(schema.nodes.hard_break.create(null, null, marks));
473
+ if (part)
474
+ children.push(schema.text(part, marks));
475
+ });
476
+ return schema.nodes.paragraph.create(attrs, children);
477
+ });
478
+ slice = new Slice(Fragment.from(paragraphs), 1, 1);
479
+ }
480
+ const tr = state.tr.replaceSelection(slice);
481
+ if (slice.openStart === 0 || !parsed?.startsAtParagraphStart || state.selection.$from.parentOffset !== 0)
482
+ return tr;
483
+ const start = tr.mapping.map(state.selection.from, -1);
484
+ const $start = tr.doc.resolve(Math.min(start, tr.doc.content.size));
485
+ const first = slice.content.firstChild;
486
+ if ($start.depth > 0 && first?.type.name === "paragraph")
487
+ tr.setNodeMarkup($start.before(1), undefined, paragraphAttrs(first.attrs));
488
+ return tr;
489
+ }
@@ -0,0 +1,48 @@
1
+ import type { Mark, MarkType } from "prosemirror-model";
2
+ import { type Command, type EditorState, type Transaction } from "prosemirror-state";
3
+ import { type BooleanMarkKey, type ParagraphAttrKey } from "./schema.js";
4
+ import type { HeadlessEditorView } from "./types.js";
5
+ export type HeadlessCommand = (state: EditorState, dispatch?: (tr: Transaction) => void, view?: HeadlessEditorView) => boolean;
6
+ export declare const fromPm: (c: Command) => HeadlessCommand;
7
+ export declare function chain(...commands: readonly HeadlessCommand[]): HeadlessCommand;
8
+ export { nextGraphemeEnd, prevGraphemeStart, nextWordEnd, nextWordStart, prevWordStart, wordRangeAt } from "./graphemes.js";
9
+ export declare function moveChar(dir: "left" | "right", extend: boolean): HeadlessCommand;
10
+ export declare function moveWord(dir: "left" | "right", extend: boolean, windows?: boolean): HeadlessCommand;
11
+ export declare function lineStart(extend: boolean): HeadlessCommand;
12
+ export declare function lineEnd(extend: boolean): HeadlessCommand;
13
+ export declare function docStart(extend: boolean): HeadlessCommand;
14
+ export declare function docEnd(extend: boolean): HeadlessCommand;
15
+ export declare function moveVertically(dir: "up" | "down", extend: boolean): HeadlessCommand;
16
+ export declare const deleteCharBackward: HeadlessCommand;
17
+ export declare const deleteCharForward: HeadlessCommand;
18
+ export declare const deleteWordBackward: HeadlessCommand;
19
+ export declare const deleteWordForward: HeadlessCommand;
20
+ export declare const deleteWordForwardWindows: HeadlessCommand;
21
+ export declare const deleteToLineStart: HeadlessCommand;
22
+ export declare const deleteToLineEnd: HeadlessCommand;
23
+ export declare const selectAll: HeadlessCommand;
24
+ export declare function applyMark(state: EditorState, dispatch: ((tr: Transaction) => void) | undefined, type: MarkType, mark: Mark | null): boolean;
25
+ export declare function toggleMark(key: BooleanMarkKey): HeadlessCommand;
26
+ export declare const toggleBold: HeadlessCommand;
27
+ export declare function setParagraphAttr(name: ParagraphAttrKey, value: unknown): HeadlessCommand;
28
+ export declare const MAX_INDENT = 8;
29
+ export declare function indentListItem(delta: 1 | -1): HeadlessCommand;
30
+ export declare const exitEmptyListItem: HeadlessCommand;
31
+ export declare const exitListItemAtStart: HeadlessCommand;
32
+ export declare const splitParagraph: HeadlessCommand;
33
+ export declare const insertHardBreak: HeadlessCommand;
34
+ export declare const pm: {
35
+ deleteSelection: HeadlessCommand;
36
+ joinBackward: HeadlessCommand;
37
+ joinForward: HeadlessCommand;
38
+ selectNodeBackward: HeadlessCommand;
39
+ selectNodeForward: HeadlessCommand;
40
+ };
41
+ export declare const backspace: HeadlessCommand;
42
+ export declare const del: HeadlessCommand;
43
+ export declare const wordBackspace: HeadlessCommand;
44
+ export declare const wordDelete: HeadlessCommand;
45
+ export declare const windowsWordDelete: HeadlessCommand;
46
+ export declare const enter: HeadlessCommand;
47
+ export declare const lineBackspace: HeadlessCommand;
48
+ export declare const lineDelete: HeadlessCommand;