Repository navigation
Expand file tree
/
Copy pathcanvas-inline.ts
More file actions
50 lines (49 loc) · 1.67 KB
/
Copy pathcanvas-inline.ts
File metadata and controls
50 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { fail } from "../../shared/errors.js";
/** Only native inline envelopes inside rich text contribute identity or references. */
export function canvasInlineElements(value: any) {
const elements: {
type: string;
id: string;
label: string;
userId?: string;
documentId?: string;
}[] = [];
const uuid =
/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i;
const visit = (node: any) => {
if (!node || typeof node !== "object") return;
if (node.tag === "Text" && Array.isArray(node.data?.richText?.value))
for (const paragraph of node.data.richText.value) {
if (!Array.isArray(paragraph?.children)) continue;
for (const child of paragraph.children) {
if (child.type !== "inline" || !child.inline) continue;
const atom = child.inline;
if (
!["custom:user-mention", "custom:document-reference"].includes(
atom.type,
)
)
continue;
const key =
atom.type === "custom:user-mention" ? "userId" : "documentId";
if (
typeof atom.id !== "string" ||
!uuid.test(atom.id) ||
typeof atom.label !== "string" ||
atom.label.length > 1000 ||
!uuid.test(atom.data?.[key] ?? "")
)
fail(400, "画板行内引用无效", { code: "canvas_inline_invalid" });
elements.push({
type: atom.type,
id: atom.id,
label: atom.label,
[key]: atom.data[key],
});
}
}
if (Array.isArray(node.children)) node.children.forEach(visit);
};
visit(value.scene);
return elements;
}