@gantfdt/semiword 0.0.0-stage → 1.0.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/LICENSE +6 -0
- package/README.md +258 -2
- package/dist-lib/FlowchartEditor-EFUVACVN.mjs +442 -0
- package/dist-lib/FlowchartEditor-EFUVACVN.mjs.map +1 -0
- package/dist-lib/MindmapEditor-CCJRPI4I.mjs +141 -0
- package/dist-lib/MindmapEditor-CCJRPI4I.mjs.map +1 -0
- package/dist-lib/WhiteboardEditor-STZ7W7XX.mjs +120 -0
- package/dist-lib/WhiteboardEditor-STZ7W7XX.mjs.map +1 -0
- package/dist-lib/ai.d.mts +33 -0
- package/dist-lib/ai.mjs +14 -0
- package/dist-lib/ai.mjs.map +1 -0
- package/dist-lib/chunk-347SCO7S.mjs +3 -0
- package/dist-lib/chunk-347SCO7S.mjs.map +1 -0
- package/dist-lib/chunk-4F4NEVHE.mjs +100 -0
- package/dist-lib/chunk-4F4NEVHE.mjs.map +1 -0
- package/dist-lib/chunk-4W6PPBHQ.mjs +474 -0
- package/dist-lib/chunk-4W6PPBHQ.mjs.map +1 -0
- package/dist-lib/chunk-7AAZUM6M.mjs +45 -0
- package/dist-lib/chunk-7AAZUM6M.mjs.map +1 -0
- package/dist-lib/chunk-BBZ5ZBEK.mjs +1510 -0
- package/dist-lib/chunk-BBZ5ZBEK.mjs.map +1 -0
- package/dist-lib/chunk-CREU4TZJ.mjs +24 -0
- package/dist-lib/chunk-CREU4TZJ.mjs.map +1 -0
- package/dist-lib/chunk-EEUFVA37.mjs +4024 -0
- package/dist-lib/chunk-EEUFVA37.mjs.map +1 -0
- package/dist-lib/chunk-EOIJPQZI.mjs +370 -0
- package/dist-lib/chunk-EOIJPQZI.mjs.map +1 -0
- package/dist-lib/chunk-EOUNN3NA.mjs +1517 -0
- package/dist-lib/chunk-EOUNN3NA.mjs.map +1 -0
- package/dist-lib/chunk-FOIP7DEC.mjs +12 -0
- package/dist-lib/chunk-FOIP7DEC.mjs.map +1 -0
- package/dist-lib/chunk-GGIOCI2I.mjs +2442 -0
- package/dist-lib/chunk-GGIOCI2I.mjs.map +1 -0
- package/dist-lib/chunk-IICCIMZU.mjs +5272 -0
- package/dist-lib/chunk-IICCIMZU.mjs.map +1 -0
- package/dist-lib/chunk-JC5ZZPSL.mjs +600 -0
- package/dist-lib/chunk-JC5ZZPSL.mjs.map +1 -0
- package/dist-lib/chunk-K7Z56Q4M.mjs +6327 -0
- package/dist-lib/chunk-K7Z56Q4M.mjs.map +1 -0
- package/dist-lib/chunk-NPPS63UI.mjs +285 -0
- package/dist-lib/chunk-NPPS63UI.mjs.map +1 -0
- package/dist-lib/chunk-PFA3XERW.mjs +19 -0
- package/dist-lib/chunk-PFA3XERW.mjs.map +1 -0
- package/dist-lib/chunk-QWDJU3H7.mjs +18 -0
- package/dist-lib/chunk-QWDJU3H7.mjs.map +1 -0
- package/dist-lib/chunk-TOA6CRSJ.mjs +3917 -0
- package/dist-lib/chunk-TOA6CRSJ.mjs.map +1 -0
- package/dist-lib/chunk-W627LLF4.mjs +16 -0
- package/dist-lib/chunk-W627LLF4.mjs.map +1 -0
- package/dist-lib/chunk-XAEALEVP.mjs +547 -0
- package/dist-lib/chunk-XAEALEVP.mjs.map +1 -0
- package/dist-lib/chunk-ZM3WULHX.mjs +910 -0
- package/dist-lib/chunk-ZM3WULHX.mjs.map +1 -0
- package/dist-lib/context-DKwyjx1q.d.mts +37 -0
- package/dist-lib/designer.d.mts +215 -0
- package/dist-lib/designer.mjs +15 -0
- package/dist-lib/designer.mjs.map +1 -0
- package/dist-lib/document.d.mts +12 -0
- package/dist-lib/document.mjs +4 -0
- package/dist-lib/document.mjs.map +1 -0
- package/dist-lib/editor.d.mts +408 -0
- package/dist-lib/editor.mjs +19 -0
- package/dist-lib/editor.mjs.map +1 -0
- package/dist-lib/gant-ai.d.mts +40 -0
- package/dist-lib/gant-ai.mjs +217 -0
- package/dist-lib/gant-ai.mjs.map +1 -0
- package/dist-lib/index.d.mts +22 -0
- package/dist-lib/index.mjs +19 -0
- package/dist-lib/index.mjs.map +1 -0
- package/dist-lib/interface-RYndHNLS.d.mts +91 -0
- package/dist-lib/io.d.mts +75 -0
- package/dist-lib/io.mjs +9 -0
- package/dist-lib/io.mjs.map +1 -0
- package/dist-lib/memoryHistoryStore-DY47xjO_.d.mts +280 -0
- package/dist-lib/mindmapConstants-X75XUXV3.mjs +4 -0
- package/dist-lib/mindmapConstants-X75XUXV3.mjs.map +1 -0
- package/dist-lib/resolve-BMcwblyP.d.mts +114 -0
- package/dist-lib/sdk.d.mts +60 -0
- package/dist-lib/sdk.mjs +137 -0
- package/dist-lib/sdk.mjs.map +1 -0
- package/dist-lib/shapes-5SMSF6ML.mjs +4 -0
- package/dist-lib/shapes-5SMSF6ML.mjs.map +1 -0
- package/dist-lib/storage.d.mts +91 -0
- package/dist-lib/storage.mjs +5 -0
- package/dist-lib/storage.mjs.map +1 -0
- package/dist-lib/template.d.mts +155 -0
- package/dist-lib/template.mjs +6 -0
- package/dist-lib/template.mjs.map +1 -0
- package/dist-lib/types-CO6ppyIY.d.mts +18 -0
- package/dist-lib/types-CUsV1hUm.d.mts +49 -0
- package/dist-lib/types-ClQ38-C_.d.mts +256 -0
- package/dist-lib/types-D1D2miCr.d.mts +117 -0
- package/dist-lib/types-DiyyKlc1.d.mts +355 -0
- package/dist-lib/types-s1JYaGtG.d.mts +56 -0
- package/dist-lib/types-vl8k8NDK.d.mts +562 -0
- package/dist-lib/useDocumentPersistence-DOpnQsZQ.d.mts +228 -0
- package/package.json +282 -4
|
@@ -0,0 +1,1517 @@
|
|
|
1
|
+
import { tr } from './chunk-W627LLF4.mjs';
|
|
2
|
+
import { genId } from './chunk-CREU4TZJ.mjs';
|
|
3
|
+
import { TableMap } from '@tiptap/pm/tables';
|
|
4
|
+
import { Slice } from '@tiptap/pm/model';
|
|
5
|
+
import { Extension } from '@tiptap/core';
|
|
6
|
+
import { PluginKey, Plugin } from '@tiptap/pm/state';
|
|
7
|
+
|
|
8
|
+
// src/editor/features/template/utils/meta.ts
|
|
9
|
+
function readTemplateBlockMeta(node) {
|
|
10
|
+
if (!node?.attrs) return null;
|
|
11
|
+
const meta = node.attrs.templateBlock;
|
|
12
|
+
if (meta && typeof meta === "object" && meta.schemaVersion === 1 && typeof meta.blockId === "string" && meta.blockId && typeof meta.kind === "string" && typeof meta.nodeType === "string") {
|
|
13
|
+
const sanitized = {
|
|
14
|
+
...meta,
|
|
15
|
+
// validation 缺失或结构非法时归一为最宽松规则,供下游 adapter.isFilled 安全访问 meta.validation.type。
|
|
16
|
+
validation: meta.validation && typeof meta.validation === "object" && typeof meta.validation.type === "string" ? meta.validation : { type: "node-present" }
|
|
17
|
+
};
|
|
18
|
+
if (sanitized.pin !== "top" && sanitized.pin !== "bottom") delete sanitized.pin;
|
|
19
|
+
if (!isStringArray(sanitized.before)) delete sanitized.before;
|
|
20
|
+
if (!isStringArray(sanitized.after)) delete sanitized.after;
|
|
21
|
+
if (typeof sanitized.groupId !== "string" || !sanitized.groupId) delete sanitized.groupId;
|
|
22
|
+
if (typeof sanitized.groupLabel !== "string" || !sanitized.groupLabel) delete sanitized.groupLabel;
|
|
23
|
+
return sanitized;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
function isStringArray(value) {
|
|
28
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
29
|
+
}
|
|
30
|
+
function renderTemplateBlockMeta(meta) {
|
|
31
|
+
if (!meta) return {};
|
|
32
|
+
return { "data-template-block": JSON.stringify(meta) };
|
|
33
|
+
}
|
|
34
|
+
function parseTemplateBlockMeta(el) {
|
|
35
|
+
const raw = el.getAttribute("data-template-block");
|
|
36
|
+
if (!raw) return null;
|
|
37
|
+
try {
|
|
38
|
+
return readTemplateBlockMeta({ attrs: { templateBlock: JSON.parse(raw) } });
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function getProtectedContentNode(node) {
|
|
44
|
+
if (!node) return null;
|
|
45
|
+
if (node.type.name !== "protectedBlock") return node;
|
|
46
|
+
return node.firstChild;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/editor/features/template/utils/structure.ts
|
|
50
|
+
function effectivePin(meta) {
|
|
51
|
+
return meta.pin === "top" || meta.pin === "bottom" ? meta.pin : null;
|
|
52
|
+
}
|
|
53
|
+
function collectEntries(doc) {
|
|
54
|
+
const entries = [];
|
|
55
|
+
doc.forEach((node) => {
|
|
56
|
+
const meta = readTemplateBlockMeta(node);
|
|
57
|
+
entries.push(
|
|
58
|
+
meta ? { meta, pin: effectivePin(meta), groupId: meta.groupId ?? null, blockId: meta.blockId } : { meta: null, pin: null, groupId: null, blockId: null }
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
return entries;
|
|
62
|
+
}
|
|
63
|
+
var declaresCache = /* @__PURE__ */ new WeakMap();
|
|
64
|
+
function declaresTemplateStructure(doc) {
|
|
65
|
+
const cached = declaresCache.get(doc);
|
|
66
|
+
if (cached !== void 0) return cached;
|
|
67
|
+
let declared = false;
|
|
68
|
+
for (let i = 0; i < doc.childCount; i += 1) {
|
|
69
|
+
const meta = readTemplateBlockMeta(doc.child(i));
|
|
70
|
+
if (meta && (effectivePin(meta) !== null || meta.groupId || meta.before?.length || meta.after?.length)) {
|
|
71
|
+
declared = true;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
declaresCache.set(doc, declared);
|
|
76
|
+
return declared;
|
|
77
|
+
}
|
|
78
|
+
function findPinSequenceViolation(pins) {
|
|
79
|
+
let phase = 0;
|
|
80
|
+
for (let index = 0; index < pins.length; index += 1) {
|
|
81
|
+
const pin = pins[index];
|
|
82
|
+
if (pin === "bottom") {
|
|
83
|
+
phase = 2;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (phase === 2) return { index, kind: "content-after-bottom" };
|
|
87
|
+
if (pin === "top") {
|
|
88
|
+
if (phase === 1) return { index, kind: "top-after-middle" };
|
|
89
|
+
} else {
|
|
90
|
+
phase = 1;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
function findGroupViolation(entries) {
|
|
96
|
+
const lastIndexByGroup = /* @__PURE__ */ new Map();
|
|
97
|
+
const pinByGroup = /* @__PURE__ */ new Map();
|
|
98
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
99
|
+
const { groupId, pin } = entries[index];
|
|
100
|
+
if (!groupId) continue;
|
|
101
|
+
const lastIndex = lastIndexByGroup.get(groupId);
|
|
102
|
+
if (lastIndex === void 0) {
|
|
103
|
+
lastIndexByGroup.set(groupId, index);
|
|
104
|
+
pinByGroup.set(groupId, pin);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (lastIndex !== index - 1) return { index: lastIndex + 1, groupId, kind: "not-contiguous" };
|
|
108
|
+
if (pinByGroup.get(groupId) !== pin) return { index, groupId, kind: "pin-mismatch" };
|
|
109
|
+
lastIndexByGroup.set(groupId, index);
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
function checkPinOrder(entries) {
|
|
114
|
+
return findPinSequenceViolation(entries.map((entry) => entry.pin)) === null;
|
|
115
|
+
}
|
|
116
|
+
function checkOrderConstraints(entries) {
|
|
117
|
+
const indexByBlockId = /* @__PURE__ */ new Map();
|
|
118
|
+
entries.forEach((entry, index) => {
|
|
119
|
+
if (entry.blockId) indexByBlockId.set(entry.blockId, index);
|
|
120
|
+
});
|
|
121
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
122
|
+
const meta = entries[index].meta;
|
|
123
|
+
if (!meta) continue;
|
|
124
|
+
for (const target of meta.before ?? []) {
|
|
125
|
+
const targetIndex = indexByBlockId.get(target);
|
|
126
|
+
if (targetIndex !== void 0 && index >= targetIndex) return false;
|
|
127
|
+
}
|
|
128
|
+
for (const target of meta.after ?? []) {
|
|
129
|
+
const targetIndex = indexByBlockId.get(target);
|
|
130
|
+
if (targetIndex !== void 0 && index <= targetIndex) return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
function checkGroupContiguity(entries) {
|
|
136
|
+
return findGroupViolation(entries) === null;
|
|
137
|
+
}
|
|
138
|
+
var structureCache = /* @__PURE__ */ new WeakMap();
|
|
139
|
+
function checkTemplateStructure(doc) {
|
|
140
|
+
const cached = structureCache.get(doc);
|
|
141
|
+
if (cached !== void 0) return cached;
|
|
142
|
+
if (!declaresTemplateStructure(doc)) {
|
|
143
|
+
structureCache.set(doc, true);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
const entries = collectEntries(doc);
|
|
147
|
+
const result = checkPinOrder(entries) && checkOrderConstraints(entries) && checkGroupContiguity(entries);
|
|
148
|
+
structureCache.set(doc, result);
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
function inspectTemplateStructure(doc) {
|
|
152
|
+
const entries = collectEntries(doc);
|
|
153
|
+
return {
|
|
154
|
+
pinOrderOk: checkPinOrder(entries),
|
|
155
|
+
orderConstraintsOk: checkOrderConstraints(entries),
|
|
156
|
+
groupContiguityOk: checkGroupContiguity(entries)
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function collectLogicalTableCells(table) {
|
|
160
|
+
const map = TableMap.get(table);
|
|
161
|
+
const cells = [];
|
|
162
|
+
for (let row = 0; row < map.height; row += 1) {
|
|
163
|
+
for (let column = 0; column < map.width; column += 1) {
|
|
164
|
+
const pos = map.map[row * map.width + column];
|
|
165
|
+
const cell = table.nodeAt(pos);
|
|
166
|
+
if (!cell) throw new Error(`\u65E0\u6CD5\u89E3\u6790\u8868\u683C\u5355\u5143\u683C (${row},${column})`);
|
|
167
|
+
cells.push({ row, column, text: cell.textContent });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return cells;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/editor/features/template/utils/adapters.ts
|
|
174
|
+
var AdapterRegistry = class {
|
|
175
|
+
constructor() {
|
|
176
|
+
this.byKind = /* @__PURE__ */ new Map();
|
|
177
|
+
this.byNodeType = /* @__PURE__ */ new Map();
|
|
178
|
+
}
|
|
179
|
+
/** 注册适配器,override 选项允许覆盖同 kind 的已有 adapter。 */
|
|
180
|
+
register(adapter, options = {}) {
|
|
181
|
+
const existing = this.byKind.get(adapter.kind);
|
|
182
|
+
if (existing && existing !== adapter && !options.override) {
|
|
183
|
+
throw new Error(`[idocs] Template block kind "${adapter.kind}" is already registered.`);
|
|
184
|
+
}
|
|
185
|
+
this.byKind.set(adapter.kind, adapter);
|
|
186
|
+
for (const nodeType of adapter.sourceNodeTypes) {
|
|
187
|
+
if (nodeType === "*") continue;
|
|
188
|
+
const occupied = this.byNodeType.get(nodeType);
|
|
189
|
+
if (occupied && occupied.kind !== adapter.kind) {
|
|
190
|
+
console.warn(
|
|
191
|
+
`[idocs] Template sourceNodeType "${nodeType}" \u5DF2\u88AB kind "${occupied.kind}" \u58F0\u660E\uFF0C\u73B0\u88AB kind "${adapter.kind}" \u8986\u76D6\u3002`
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
this.byNodeType.set(nodeType, adapter);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/** 按 kind 获取适配器。 */
|
|
198
|
+
get(kind) {
|
|
199
|
+
return this.byKind.get(kind);
|
|
200
|
+
}
|
|
201
|
+
/** 检查 kind 是否已注册。 */
|
|
202
|
+
has(kind) {
|
|
203
|
+
return this.byKind.has(kind);
|
|
204
|
+
}
|
|
205
|
+
/** 按节点类型获取适配器。 */
|
|
206
|
+
getByNodeType(nodeType) {
|
|
207
|
+
return this.byNodeType.get(nodeType);
|
|
208
|
+
}
|
|
209
|
+
/** 获取所有已注册的适配器。 */
|
|
210
|
+
all() {
|
|
211
|
+
return [...this.byKind.values()];
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
function stripTemplateAttrs(node) {
|
|
215
|
+
const { templateBlock, templateSlot, ...attrs } = node.attrs ?? {};
|
|
216
|
+
const result = { ...node, attrs };
|
|
217
|
+
if (node.content) result.content = node.content.map(stripTemplateAttrs);
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
function normalizeText(text) {
|
|
221
|
+
return text.replace(/[\u200B-\u200D\uFEFF]/g, "").replace(/\s/g, "").trim();
|
|
222
|
+
}
|
|
223
|
+
function isSafeSrc(src) {
|
|
224
|
+
if (typeof src !== "string" || !src.trim()) return false;
|
|
225
|
+
const value = src.trim();
|
|
226
|
+
if (value.startsWith("/") || value.startsWith("./") || value.startsWith("../")) return true;
|
|
227
|
+
try {
|
|
228
|
+
return ["https:", "http:", "data:", "blob:"].includes(new URL(value).protocol);
|
|
229
|
+
} catch {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function isTextFilled(node, meta) {
|
|
234
|
+
if (meta.validation.type === "node-present") return true;
|
|
235
|
+
if (meta.validation.type !== "non-whitespace-text") return true;
|
|
236
|
+
const value = normalizeText(node.textContent);
|
|
237
|
+
if (!value) return false;
|
|
238
|
+
if (!meta.validation.ignoreInitialContent) return true;
|
|
239
|
+
return value !== normalizeText(meta.initialText ?? "");
|
|
240
|
+
}
|
|
241
|
+
var genericAdapter = {
|
|
242
|
+
kind: "generic",
|
|
243
|
+
sourceNodeTypes: ["*"],
|
|
244
|
+
instantiate: ({ sourceNode }) => ({ runtimeNode: sourceNode }),
|
|
245
|
+
isFilled: isTextFilled,
|
|
246
|
+
toUnlockedNode: (node) => stripTemplateAttrs(node.toJSON())
|
|
247
|
+
};
|
|
248
|
+
var imageAdapter = {
|
|
249
|
+
kind: "image",
|
|
250
|
+
sourceNodeTypes: ["image", "templateImage"],
|
|
251
|
+
instantiate({ sourceNode, slot }) {
|
|
252
|
+
const runtimeNode = {
|
|
253
|
+
type: "templateImage",
|
|
254
|
+
attrs: {
|
|
255
|
+
src: sourceNode.attrs?.src ?? "",
|
|
256
|
+
alt: sourceNode.attrs?.alt ?? "",
|
|
257
|
+
title: sourceNode.attrs?.title ?? "",
|
|
258
|
+
// 设计器声明的占位尺寸:空态用作占位框大小,已填态用作图片宽度(高度按比例)。
|
|
259
|
+
width: typeof sourceNode.attrs?.width === "number" ? sourceNode.attrs.width : null,
|
|
260
|
+
height: typeof sourceNode.attrs?.height === "number" ? sourceNode.attrs.height : null,
|
|
261
|
+
slotKey: slot.key,
|
|
262
|
+
slotLabel: slot.label
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
return { runtimeNode };
|
|
266
|
+
},
|
|
267
|
+
isFilled(node) {
|
|
268
|
+
const uploadState = node.attrs?.uploadState;
|
|
269
|
+
return uploadState !== "uploading" && uploadState !== "error" && isSafeSrc(node.attrs?.src);
|
|
270
|
+
},
|
|
271
|
+
toUnlockedNode(node) {
|
|
272
|
+
if (!isSafeSrc(node.attrs?.src)) return { type: "paragraph" };
|
|
273
|
+
return {
|
|
274
|
+
type: "image",
|
|
275
|
+
attrs: {
|
|
276
|
+
src: node.attrs.src,
|
|
277
|
+
alt: node.attrs.alt ?? null,
|
|
278
|
+
title: node.attrs.title ?? null,
|
|
279
|
+
width: typeof node.attrs.width === "number" ? node.attrs.width : null,
|
|
280
|
+
height: typeof node.attrs.height === "number" ? node.attrs.height : null
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
function collectRequiredCells(node, cellTypes) {
|
|
286
|
+
const cells = [];
|
|
287
|
+
node.descendants((child) => {
|
|
288
|
+
if (cellTypes.has(child.type.name)) {
|
|
289
|
+
if (child.attrs.required === true) cells.push(child);
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
});
|
|
294
|
+
return cells;
|
|
295
|
+
}
|
|
296
|
+
function isTableFilled(node, meta) {
|
|
297
|
+
const requiredCells = collectRequiredCells(node, /* @__PURE__ */ new Set(["tableCell", "tableHeader"]));
|
|
298
|
+
if (requiredCells.length > 0) {
|
|
299
|
+
return requiredCells.every((cell) => cell.textContent.trim().length > 0);
|
|
300
|
+
}
|
|
301
|
+
const rule = meta.validation;
|
|
302
|
+
if (rule.type === "table-any-data-cell") {
|
|
303
|
+
let filled = false;
|
|
304
|
+
node.descendants((child) => {
|
|
305
|
+
if (child.type.name === "tableCell" && child.attrs.fillable === true && child.textContent.trim()) {
|
|
306
|
+
filled = true;
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
return !filled;
|
|
310
|
+
});
|
|
311
|
+
return filled;
|
|
312
|
+
}
|
|
313
|
+
if (rule.type === "table-required-cells") {
|
|
314
|
+
const requiredCells2 = Array.isArray(rule.cells) ? rule.cells : [];
|
|
315
|
+
const cells = collectLogicalTableCells(node);
|
|
316
|
+
return requiredCells2.every(
|
|
317
|
+
(target) => cells.some((cell) => cell.row === target.row && cell.column === target.column && cell.text.trim())
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
var tableAdapter = {
|
|
323
|
+
kind: "table",
|
|
324
|
+
sourceNodeTypes: ["table"],
|
|
325
|
+
instantiate: ({ sourceNode }) => ({ runtimeNode: sourceNode }),
|
|
326
|
+
isFilled: isTableFilled,
|
|
327
|
+
toUnlockedNode: (node) => stripTemplateAttrs(node.toJSON())
|
|
328
|
+
};
|
|
329
|
+
function isFixedTableFilled(node, meta) {
|
|
330
|
+
const requiredCells = collectRequiredCells(node, /* @__PURE__ */ new Set(["fixedTableCell"]));
|
|
331
|
+
if (requiredCells.length > 0) {
|
|
332
|
+
return requiredCells.every((cell) => cell.textContent.trim().length > 0);
|
|
333
|
+
}
|
|
334
|
+
if (meta.validation.type !== "table-any-data-cell") return true;
|
|
335
|
+
let filled = false;
|
|
336
|
+
node.descendants((child) => {
|
|
337
|
+
if (child.type.name === "fixedTableCell" && child.attrs.covered !== true && // 防御性:covered 格本不该 fillable。
|
|
338
|
+
child.attrs.fillable === true && child.textContent.trim()) {
|
|
339
|
+
filled = true;
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
return !filled;
|
|
343
|
+
});
|
|
344
|
+
return filled;
|
|
345
|
+
}
|
|
346
|
+
var fixedTableAdapter = {
|
|
347
|
+
kind: "fixedTable",
|
|
348
|
+
sourceNodeTypes: ["fixedTable"],
|
|
349
|
+
instantiate: ({ sourceNode }) => ({ runtimeNode: sourceNode }),
|
|
350
|
+
isFilled: isFixedTableFilled,
|
|
351
|
+
toUnlockedNode: (node) => stripTemplateAttrs(node.toJSON())
|
|
352
|
+
};
|
|
353
|
+
var codeBlockAdapter = {
|
|
354
|
+
kind: "codeBlock",
|
|
355
|
+
sourceNodeTypes: ["codeBlock"],
|
|
356
|
+
instantiate: ({ sourceNode }) => ({ runtimeNode: sourceNode }),
|
|
357
|
+
isFilled: isTextFilled,
|
|
358
|
+
toUnlockedNode: (node) => stripTemplateAttrs(node.toJSON())
|
|
359
|
+
};
|
|
360
|
+
function defaultValidationFor(kind) {
|
|
361
|
+
if (kind === "image") return { type: "image-src" };
|
|
362
|
+
if (kind === "table") return { type: "table-any-data-cell" };
|
|
363
|
+
if (kind === "codeBlock") return { type: "non-whitespace-text" };
|
|
364
|
+
if (kind === "generic") return { type: "non-whitespace-text" };
|
|
365
|
+
return { type: "node-present" };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// src/editor/features/template/utils/registry.ts
|
|
369
|
+
var registry = new AdapterRegistry();
|
|
370
|
+
var extensionsByKind = /* @__PURE__ */ new Map();
|
|
371
|
+
var templateDefinitions = /* @__PURE__ */ new Map();
|
|
372
|
+
function registerTemplateBlocks(registrations, options = {}) {
|
|
373
|
+
const list = Array.isArray(registrations) ? registrations : [registrations];
|
|
374
|
+
for (const item of list) {
|
|
375
|
+
const registration = "adapter" in item ? item : { adapter: item };
|
|
376
|
+
registry.register(registration.adapter, options);
|
|
377
|
+
if (registration.extensions) {
|
|
378
|
+
extensionsByKind.set(registration.adapter.kind, registration.extensions);
|
|
379
|
+
} else if (options.override) {
|
|
380
|
+
extensionsByKind.delete(registration.adapter.kind);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return registry;
|
|
384
|
+
}
|
|
385
|
+
function getTemplateBlockRegistry() {
|
|
386
|
+
return registry;
|
|
387
|
+
}
|
|
388
|
+
function registerTemplates(definitions) {
|
|
389
|
+
const list = Array.isArray(definitions) ? definitions : [definitions];
|
|
390
|
+
for (const definition of list) {
|
|
391
|
+
if (definition?.id) {
|
|
392
|
+
templateDefinitions.set(definition.id, definition);
|
|
393
|
+
} else {
|
|
394
|
+
console.warn("[template] registerTemplates \u5FFD\u7565\u4E86\u4E00\u6761\u65E0 id \u7684\u6A21\u677F\u5B9A\u4E49:", definition);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
function getRegisteredTemplates() {
|
|
399
|
+
return [...templateDefinitions.values()];
|
|
400
|
+
}
|
|
401
|
+
function getRegisteredTemplateBlockExtensions() {
|
|
402
|
+
return [...extensionsByKind.values()].flat();
|
|
403
|
+
}
|
|
404
|
+
registerTemplateBlocks([genericAdapter, imageAdapter, tableAdapter, fixedTableAdapter, codeBlockAdapter]);
|
|
405
|
+
|
|
406
|
+
// src/editor/features/template/types.ts
|
|
407
|
+
var TEMPLATE_PIN_ATTR = "templatePin";
|
|
408
|
+
var TEMPLATE_BLOCK_REF_ATTR = "templateBlockRef";
|
|
409
|
+
var TEMPLATE_BEFORE_ATTR = "templateBefore";
|
|
410
|
+
var TEMPLATE_AFTER_ATTR = "templateAfter";
|
|
411
|
+
var TEMPLATE_GROUP_ID_ATTR = "templateGroupId";
|
|
412
|
+
var TEMPLATE_GROUP_LABEL_ATTR = "templateGroupLabel";
|
|
413
|
+
var TEMPLATE_DATA_BINDING_ATTR = "templateDataBinding";
|
|
414
|
+
|
|
415
|
+
// src/editor/features/template/utils/compile.ts
|
|
416
|
+
var DEFAULT_COMPILE_LIMITS = {
|
|
417
|
+
maxJsonDepth: 64,
|
|
418
|
+
maxNodeCount: 5e3,
|
|
419
|
+
maxTemplateBlocks: 200,
|
|
420
|
+
maxNameLength: 200,
|
|
421
|
+
maxIdLength: 100,
|
|
422
|
+
maxVersionLength: 64
|
|
423
|
+
};
|
|
424
|
+
function compileTemplateDefinition(definition, schema, adapters, limits = DEFAULT_COMPILE_LIMITS) {
|
|
425
|
+
const issues = [];
|
|
426
|
+
if (!isTemplateShape(definition))
|
|
427
|
+
return { ok: false, issues: [{ code: "INVALID_SHAPE", message: "\u6A21\u677F\u5B9A\u4E49\u7ED3\u6784\u4E0D\u5408\u6CD5" }] };
|
|
428
|
+
if (definition.schemaVersion !== 2)
|
|
429
|
+
issues.push({ code: "UNSUPPORTED_SCHEMA_VERSION", message: "schemaVersion \u5FC5\u987B\u4E3A 2" });
|
|
430
|
+
if (!definition.id || definition.id.length > limits.maxIdLength)
|
|
431
|
+
issues.push({ code: "INVALID_ID", message: "id \u5FC5\u987B\u975E\u7A7A\u4E14\u4E0D\u8D85\u4E0A\u9650" });
|
|
432
|
+
if (!definition.version || definition.version.length > limits.maxVersionLength)
|
|
433
|
+
issues.push({ code: "INVALID_VERSION", message: "version \u5FC5\u586B\u4E14\u4E0D\u8D85\u4E0A\u9650" });
|
|
434
|
+
if (!definition.name || definition.name.length > limits.maxNameLength)
|
|
435
|
+
issues.push({ code: "INVALID_NAME", message: "name \u5FC5\u987B\u975E\u7A7A\u4E14\u4E0D\u8D85\u4E0A\u9650" });
|
|
436
|
+
const content = definition.content;
|
|
437
|
+
if (!content || content.type !== "doc" || !Array.isArray(content.content) || content.content.length === 0) {
|
|
438
|
+
return {
|
|
439
|
+
ok: false,
|
|
440
|
+
issues: [...issues, { code: "INVALID_ROOT", message: "content \u5FC5\u987B\u662F\u5305\u542B\u81F3\u5C11\u4E00\u4E2A\u9876\u5C42\u5757\u7684 doc" }]
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
const stats = computeStats(content);
|
|
444
|
+
if (stats.depth > limits.maxJsonDepth) issues.push({ code: "TOO_DEEP", message: `JSON \u6DF1\u5EA6 ${stats.depth} \u8D85\u9650` });
|
|
445
|
+
if (stats.nodeCount > limits.maxNodeCount)
|
|
446
|
+
issues.push({ code: "TOO_LARGE", message: `\u8282\u70B9\u603B\u6570 ${stats.nodeCount} \u8D85\u9650` });
|
|
447
|
+
if (content.content.length > limits.maxTemplateBlocks)
|
|
448
|
+
issues.push({ code: "TOO_MANY_BLOCKS", message: `\u6A21\u677F\u9876\u5C42\u5757\u603B\u6570 ${content.content.length} \u8D85\u9650` });
|
|
449
|
+
walkNodes(content, (node, path, depth) => {
|
|
450
|
+
if (node.type && node.type !== "text" && !schema.nodes[node.type]) {
|
|
451
|
+
issues.push({ code: "UNSUPPORTED_NODE_TYPE", message: `\u8282\u70B9\u7C7B\u578B ${node.type} \u4E0D\u652F\u6301`, path });
|
|
452
|
+
}
|
|
453
|
+
node.marks?.forEach((mark) => {
|
|
454
|
+
if (mark.type && !schema.marks[mark.type])
|
|
455
|
+
issues.push({ code: "UNSUPPORTED_MARK", message: `mark ${mark.type} \u4E0D\u652F\u6301`, path });
|
|
456
|
+
});
|
|
457
|
+
const attrs = node.attrs ?? {};
|
|
458
|
+
if (attrs[TEMPLATE_DATA_BINDING_ATTR] !== void 0) {
|
|
459
|
+
issues.push({
|
|
460
|
+
code: "UNRESOLVED_DATA_BINDING",
|
|
461
|
+
message: "\u6A21\u677F\u5305\u542B\u672A\u89E3\u6790\u7684\u540E\u7AEF\u6570\u636E\u7ED1\u5B9A\uFF0C\u8BF7\u5148\u89E3\u6790\u6570\u636E\u518D\u5957\u7528",
|
|
462
|
+
path
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
if (attrs.templateBlock !== void 0)
|
|
466
|
+
issues.push({ code: "RUNTIME_META_IN_DEFINITION", message: "\u5B9A\u4E49\u4E2D\u4E0D\u5F97\u51FA\u73B0\u8FD0\u884C\u65F6 templateBlock", path });
|
|
467
|
+
if (attrs.templateSlot !== void 0 && depth !== 1) {
|
|
468
|
+
issues.push({ code: "NESTED_TEMPLATE_SLOT", message: "templateSlot \u53EA\u80FD\u58F0\u660E\u5728 doc \u7684\u9876\u5C42\u5757\u4E0A", path });
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
const slots = {};
|
|
472
|
+
const slotKeys = /* @__PURE__ */ new Set();
|
|
473
|
+
content.content.forEach((node, index) => {
|
|
474
|
+
const path = `$.content[${index}]`;
|
|
475
|
+
const slot = node.attrs?.templateSlot;
|
|
476
|
+
if (!slot) return;
|
|
477
|
+
issues.push(...validateSlot(slot, node, path, schema, adapters, slotKeys));
|
|
478
|
+
if (typeof slot.key === "string" && slot.key && typeof slot.kind === "string" && slot.kind && typeof slot.label === "string" && slot.label) {
|
|
479
|
+
slots[slot.key] = {
|
|
480
|
+
key: slot.key,
|
|
481
|
+
kind: slot.kind,
|
|
482
|
+
label: slot.label,
|
|
483
|
+
required: slot.required ?? true,
|
|
484
|
+
validation: slot.validation,
|
|
485
|
+
editable: slot.editable
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
issues.push(...validateStructureCarriers(content));
|
|
490
|
+
if (issues.length) return { ok: false, issues };
|
|
491
|
+
const normalized = {
|
|
492
|
+
schemaVersion: 2,
|
|
493
|
+
id: definition.id,
|
|
494
|
+
version: definition.version,
|
|
495
|
+
name: definition.name,
|
|
496
|
+
description: definition.description,
|
|
497
|
+
dataSource: definition.dataSource,
|
|
498
|
+
content
|
|
499
|
+
};
|
|
500
|
+
return { ok: true, template: { definition: normalized, slots } };
|
|
501
|
+
}
|
|
502
|
+
function instantiateCompiledTemplate(compiled, schema, adapters, idFactory = genId) {
|
|
503
|
+
const blockIds = [];
|
|
504
|
+
const runtimeContent = [];
|
|
505
|
+
const issues = [];
|
|
506
|
+
const generatedBlockIds = /* @__PURE__ */ new Set();
|
|
507
|
+
const sourceBlocks = compiled.definition.content.content ?? [];
|
|
508
|
+
const plannedBlockIds = [];
|
|
509
|
+
const blockIdByRef = /* @__PURE__ */ new Map();
|
|
510
|
+
sourceBlocks.forEach((sourceBlock) => {
|
|
511
|
+
const attrs = sourceBlock.attrs ?? {};
|
|
512
|
+
const rawSlot = attrs.templateSlot;
|
|
513
|
+
const declaredSlot = rawSlot?.key ? compiled.slots[rawSlot.key] : void 0;
|
|
514
|
+
if (declaredSlot && !declaredSlot.required) {
|
|
515
|
+
plannedBlockIds.push(null);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const blockId = idFactory();
|
|
519
|
+
plannedBlockIds.push(blockId);
|
|
520
|
+
const ref = attrs[TEMPLATE_BLOCK_REF_ATTR];
|
|
521
|
+
if (typeof ref === "string" && ref) blockIdByRef.set(ref, blockId);
|
|
522
|
+
});
|
|
523
|
+
sourceBlocks.forEach((sourceBlock, index) => {
|
|
524
|
+
const attrs = sourceBlock.attrs ?? {};
|
|
525
|
+
const rawSlot = attrs.templateSlot;
|
|
526
|
+
const declaredSlot = rawSlot?.key ? compiled.slots[rawSlot.key] : void 0;
|
|
527
|
+
if (declaredSlot && !declaredSlot.required) {
|
|
528
|
+
runtimeContent.push(stripDefinitionAttrs(sourceBlock));
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
const adapter = declaredSlot ? adapters.get(declaredSlot.kind) : void 0;
|
|
532
|
+
if (declaredSlot && !adapter) {
|
|
533
|
+
issues.push({
|
|
534
|
+
code: "NO_ADAPTER",
|
|
535
|
+
message: `kind "${declaredSlot.kind}" \u672A\u6CE8\u518C`,
|
|
536
|
+
path: `$.content[${index}]`,
|
|
537
|
+
slotKey: declaredSlot.key
|
|
538
|
+
});
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const blockId = plannedBlockIds[index];
|
|
542
|
+
if (generatedBlockIds.has(blockId)) {
|
|
543
|
+
issues.push({
|
|
544
|
+
code: "DUPLICATE_BLOCK_ID",
|
|
545
|
+
message: `\u751F\u6210\u4E86\u91CD\u590D blockId ${blockId}`,
|
|
546
|
+
path: `$.content[${index}]`,
|
|
547
|
+
slotKey: declaredSlot?.key
|
|
548
|
+
});
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
generatedBlockIds.add(blockId);
|
|
552
|
+
const cleanSource = stripDefinitionAttrs(sourceBlock);
|
|
553
|
+
const structureMeta = buildStructureMeta(attrs, blockIdByRef);
|
|
554
|
+
if (!declaredSlot) {
|
|
555
|
+
const fixedMeta = {
|
|
556
|
+
schemaVersion: 1,
|
|
557
|
+
blockId,
|
|
558
|
+
kind: "generic",
|
|
559
|
+
nodeType: cleanSource.type ?? "unknown",
|
|
560
|
+
label: extractText(cleanSource).trim().slice(0, 40) || (cleanSource.type ?? "\u56FA\u5B9A\u5757"),
|
|
561
|
+
isSlot: false,
|
|
562
|
+
required: false,
|
|
563
|
+
slotKey: "",
|
|
564
|
+
validation: { type: "node-present" },
|
|
565
|
+
editable: false,
|
|
566
|
+
...structureMeta
|
|
567
|
+
};
|
|
568
|
+
runtimeContent.push(wrapProtected(fixedMeta, cleanSource));
|
|
569
|
+
blockIds.push(blockId);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
const output = adapter.instantiate({ sourceNode: cleanSource, blockId, slot: declaredSlot });
|
|
573
|
+
const editable = declaredSlot.editable ?? (declaredSlot.kind === "generic" || declaredSlot.kind === "codeBlock");
|
|
574
|
+
const meta = {
|
|
575
|
+
schemaVersion: 1,
|
|
576
|
+
blockId,
|
|
577
|
+
kind: declaredSlot.kind,
|
|
578
|
+
nodeType: output.runtimeNode.type ?? cleanSource.type ?? "unknown",
|
|
579
|
+
label: declaredSlot.label,
|
|
580
|
+
isSlot: true,
|
|
581
|
+
required: true,
|
|
582
|
+
slotKey: declaredSlot.key,
|
|
583
|
+
validation: declaredSlot.validation ?? defaultValidationFor(declaredSlot.kind),
|
|
584
|
+
initialText: extractText(cleanSource).trim() || void 0,
|
|
585
|
+
editable,
|
|
586
|
+
...structureMeta
|
|
587
|
+
};
|
|
588
|
+
runtimeContent.push(wrapProtected(meta, output.runtimeNode));
|
|
589
|
+
blockIds.push(blockId);
|
|
590
|
+
});
|
|
591
|
+
if (issues.length) return { ok: false, issues };
|
|
592
|
+
const content = { type: "doc", content: runtimeContent };
|
|
593
|
+
try {
|
|
594
|
+
schema.nodeFromJSON(content).check();
|
|
595
|
+
} catch (error) {
|
|
596
|
+
return {
|
|
597
|
+
ok: false,
|
|
598
|
+
issues: [
|
|
599
|
+
{
|
|
600
|
+
code: "SCHEMA_CHECK_FAILED",
|
|
601
|
+
message: `schema \u6821\u9A8C\u5931\u8D25\uFF1A${error instanceof Error ? error.message : String(error)}`
|
|
602
|
+
}
|
|
603
|
+
]
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
return { ok: true, content, blockIds };
|
|
607
|
+
}
|
|
608
|
+
function validateStructureCarriers(content) {
|
|
609
|
+
const issues = [];
|
|
610
|
+
const blocks = content.content ?? [];
|
|
611
|
+
const freeRefs = /* @__PURE__ */ new Set();
|
|
612
|
+
const carriers = blocks.map((node, index) => {
|
|
613
|
+
const attrs = node.attrs ?? {};
|
|
614
|
+
const slot = attrs.templateSlot;
|
|
615
|
+
const free = slot?.required === false;
|
|
616
|
+
if (free) {
|
|
617
|
+
const freeRef = attrs[TEMPLATE_BLOCK_REF_ATTR];
|
|
618
|
+
if (typeof freeRef === "string" && freeRef) freeRefs.add(freeRef);
|
|
619
|
+
}
|
|
620
|
+
const parsed = free ? {} : readStructureCarriers(attrs);
|
|
621
|
+
return {
|
|
622
|
+
index,
|
|
623
|
+
pin: parsed.pin ?? null,
|
|
624
|
+
groupId: parsed.groupId ?? null,
|
|
625
|
+
blockRef: parsed.blockRef ?? null,
|
|
626
|
+
before: parsed.before ?? [],
|
|
627
|
+
after: parsed.after ?? []
|
|
628
|
+
};
|
|
629
|
+
});
|
|
630
|
+
const declares = carriers.some(
|
|
631
|
+
(item) => item.pin !== null || item.groupId || item.before.length > 0 || item.after.length > 0
|
|
632
|
+
);
|
|
633
|
+
if (!declares) return issues;
|
|
634
|
+
const pinViolation = findPinSequenceViolation(carriers.map((item) => item.pin));
|
|
635
|
+
if (pinViolation) {
|
|
636
|
+
const path = `$.content[${pinViolation.index}]`;
|
|
637
|
+
issues.push(
|
|
638
|
+
pinViolation.kind === "top-after-middle" ? { code: "HEADER_AFTER_BODY", message: "\u7F6E\u9876\u5757\u5FC5\u987B\u4F4D\u4E8E\u6240\u6709\u5176\u5B83\u5757\u4E4B\u524D", path } : { code: "BLOCK_AFTER_FOOTER", message: "\u7F6E\u5E95\u5757\u4E4B\u540E\u4E0D\u80FD\u518D\u6709\u5176\u5B83\u5757", path }
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
const groupViolation = findGroupViolation(carriers);
|
|
642
|
+
if (groupViolation) {
|
|
643
|
+
const path = `$.content[${groupViolation.index}]`;
|
|
644
|
+
issues.push(
|
|
645
|
+
groupViolation.kind === "pin-mismatch" ? { code: "GROUP_PIN_MISMATCH", message: `\u7EC4\u4EF6\u7EC4 "${groupViolation.groupId}" \u7684\u6210\u5458\u9489\u4F4F\u72B6\u6001\u4E0D\u4E00\u81F4`, path } : {
|
|
646
|
+
code: "GROUP_NOT_CONTIGUOUS",
|
|
647
|
+
message: `\u7EC4\u4EF6\u7EC4 ${groupViolation.groupId} \u7684\u6210\u5458\u5FC5\u987B\u76F8\u90BB\uFF08\u6210\u5458\u4E4B\u95F4\u4E0D\u80FD\u63D2\u5165\u5176\u5B83\u5757\uFF09`,
|
|
648
|
+
path
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const firstRefIndex = /* @__PURE__ */ new Map();
|
|
653
|
+
blocks.forEach((node, index) => {
|
|
654
|
+
const ref = node.attrs?.[TEMPLATE_BLOCK_REF_ATTR];
|
|
655
|
+
if (typeof ref !== "string" || !ref) return;
|
|
656
|
+
if (firstRefIndex.has(ref)) {
|
|
657
|
+
issues.push({
|
|
658
|
+
code: "ORDER_REF_DUPLICATE",
|
|
659
|
+
message: `templateBlockRef "${ref}" \u91CD\u590D\u5B9A\u4E49`,
|
|
660
|
+
path: `$.content[${index}]`
|
|
661
|
+
});
|
|
662
|
+
} else {
|
|
663
|
+
firstRefIndex.set(ref, index);
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
const definedRefs = /* @__PURE__ */ new Set();
|
|
667
|
+
const indexByRef = /* @__PURE__ */ new Map();
|
|
668
|
+
for (const item of carriers) {
|
|
669
|
+
if (!item.blockRef) continue;
|
|
670
|
+
definedRefs.add(item.blockRef);
|
|
671
|
+
indexByRef.set(item.blockRef, item.index);
|
|
672
|
+
}
|
|
673
|
+
for (const item of carriers) {
|
|
674
|
+
const path = `$.content[${item.index}]`;
|
|
675
|
+
const source = item.blockRef ? `\u5757 "${item.blockRef}"` : `\u7B2C ${item.index + 1} \u4E2A\u5757`;
|
|
676
|
+
const reported = /* @__PURE__ */ new Set();
|
|
677
|
+
const checkRefs = (refs, direction) => {
|
|
678
|
+
for (const ref of new Set(refs)) {
|
|
679
|
+
if (item.blockRef && ref === item.blockRef) {
|
|
680
|
+
if (!reported.has(ref)) {
|
|
681
|
+
reported.add(ref);
|
|
682
|
+
issues.push({
|
|
683
|
+
code: "ORDER_SELF_REFERENCE",
|
|
684
|
+
message: `\u5757 "${ref}" \u4E0D\u5F97\u5728\u81EA\u8EAB before/after \u4E2D\u5F15\u7528\u81EA\u5DF1`,
|
|
685
|
+
path
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
if (!definedRefs.has(ref)) {
|
|
691
|
+
if (!reported.has(ref)) {
|
|
692
|
+
reported.add(ref);
|
|
693
|
+
issues.push(
|
|
694
|
+
freeRefs.has(ref) ? {
|
|
695
|
+
code: "ORDER_TARGET_FREE",
|
|
696
|
+
message: `before/after \u5F15\u7528\u7684\u5757 "${ref}" \u662F\u81EA\u7531\u5757\uFF08\u8FD0\u884C\u65F6\u4F1A\u88AB\u5265\u79BB\uFF09\uFF0C\u7EA6\u675F\u4F1A\u7A7A\u8F6C`,
|
|
697
|
+
path
|
|
698
|
+
} : { code: "ORDER_TARGET_MISSING", message: `before/after \u5F15\u7528\u7684\u5757 "${ref}" \u4E0D\u5B58\u5728`, path }
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
const targetIndex = indexByRef.get(ref);
|
|
704
|
+
if (direction === "before" && item.index >= targetIndex) {
|
|
705
|
+
issues.push({
|
|
706
|
+
code: "ORDER_CONSTRAINT_VIOLATION",
|
|
707
|
+
message: `${source} \u5FC5\u987B\u4F4D\u4E8E "${ref}" \u4E4B\u4E0A\uFF08\u5F53\u524D\u987A\u5E8F\u4E0D\u6EE1\u8DB3\uFF09`,
|
|
708
|
+
path
|
|
709
|
+
});
|
|
710
|
+
} else if (direction === "after" && item.index <= targetIndex) {
|
|
711
|
+
issues.push({
|
|
712
|
+
code: "ORDER_CONSTRAINT_VIOLATION",
|
|
713
|
+
message: `${source} \u5FC5\u987B\u4F4D\u4E8E "${ref}" \u4E4B\u4E0B\uFF08\u5F53\u524D\u987A\u5E8F\u4E0D\u6EE1\u8DB3\uFF09`,
|
|
714
|
+
path
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
checkRefs(item.before, "before");
|
|
720
|
+
checkRefs(item.after, "after");
|
|
721
|
+
}
|
|
722
|
+
issues.push(...checkOrderConstraintCycles(carriers));
|
|
723
|
+
return issues;
|
|
724
|
+
}
|
|
725
|
+
function checkOrderConstraintCycles(carriers) {
|
|
726
|
+
const cyclic = findOrderConstraintCyclicRefs(carriers);
|
|
727
|
+
if (cyclic.length === 0) return [];
|
|
728
|
+
return [{ code: "ORDER_CONSTRAINT_CYCLE", message: `\u987A\u5E8F\u7EA6\u675F\u5B58\u5728\u73AF\uFF0C\u6D89\u53CA\u5757\uFF1A${cyclic.join(", ")}` }];
|
|
729
|
+
}
|
|
730
|
+
function findOrderConstraintCyclicRefs(carriers) {
|
|
731
|
+
const nodes = carriers.filter((item) => !!item.blockRef);
|
|
732
|
+
if (nodes.length === 0) return [];
|
|
733
|
+
const refSet = new Set(nodes.map((item) => item.blockRef));
|
|
734
|
+
const edges = /* @__PURE__ */ new Map();
|
|
735
|
+
const addEdge = (from, to) => {
|
|
736
|
+
if (from === to) return;
|
|
737
|
+
let targets = edges.get(from);
|
|
738
|
+
if (!targets) {
|
|
739
|
+
targets = /* @__PURE__ */ new Set();
|
|
740
|
+
edges.set(from, targets);
|
|
741
|
+
}
|
|
742
|
+
targets.add(to);
|
|
743
|
+
};
|
|
744
|
+
for (const item of nodes) {
|
|
745
|
+
const from = item.blockRef;
|
|
746
|
+
for (const ref of item.before ?? []) if (refSet.has(ref)) addEdge(from, ref);
|
|
747
|
+
for (const ref of item.after ?? []) if (refSet.has(ref)) addEdge(ref, from);
|
|
748
|
+
}
|
|
749
|
+
for (const top of nodes.filter((item) => item.pin === "top")) {
|
|
750
|
+
for (const other of nodes) if (other.pin !== "top") addEdge(top.blockRef, other.blockRef);
|
|
751
|
+
}
|
|
752
|
+
for (const bottom of nodes.filter((item) => item.pin === "bottom")) {
|
|
753
|
+
for (const other of nodes) if (other.pin !== "bottom") addEdge(other.blockRef, bottom.blockRef);
|
|
754
|
+
}
|
|
755
|
+
const indegree = /* @__PURE__ */ new Map();
|
|
756
|
+
nodes.forEach((item) => indegree.set(item.blockRef, 0));
|
|
757
|
+
for (const targets of edges.values()) {
|
|
758
|
+
for (const to of targets) indegree.set(to, (indegree.get(to) ?? 0) + 1);
|
|
759
|
+
}
|
|
760
|
+
const queue = nodes.filter((item) => indegree.get(item.blockRef) === 0).map((item) => item.blockRef);
|
|
761
|
+
let visited = 0;
|
|
762
|
+
while (queue.length > 0) {
|
|
763
|
+
const ref = queue.shift();
|
|
764
|
+
visited += 1;
|
|
765
|
+
for (const to of edges.get(ref) ?? []) {
|
|
766
|
+
const degree = (indegree.get(to) ?? 0) - 1;
|
|
767
|
+
indegree.set(to, degree);
|
|
768
|
+
if (degree === 0) queue.push(to);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
if (visited >= nodes.length) return [];
|
|
772
|
+
return nodes.filter((item) => (indegree.get(item.blockRef) ?? 0) > 0).map((item) => item.blockRef);
|
|
773
|
+
}
|
|
774
|
+
function validateSlot(slot, node, path, schema, adapters, keys) {
|
|
775
|
+
return [
|
|
776
|
+
...validateSlotIdentity(slot, path, keys),
|
|
777
|
+
...validateSlotKind(slot, node, path, adapters),
|
|
778
|
+
...validateSlotTableCells(slot, node, path, schema),
|
|
779
|
+
...validateSlotCodeBlockHint(slot, node, path)
|
|
780
|
+
];
|
|
781
|
+
}
|
|
782
|
+
function validateSlotIdentity(slot, path, keys) {
|
|
783
|
+
const issues = [];
|
|
784
|
+
if (typeof slot.key !== "string" || !slot.key)
|
|
785
|
+
issues.push({ code: "INVALID_SLOT_KEY", message: "slot.key \u5FC5\u987B\u975E\u7A7A", path });
|
|
786
|
+
else if (keys.has(slot.key))
|
|
787
|
+
issues.push({ code: "DUPLICATE_SLOT_KEY", message: `slot key "${slot.key}" \u91CD\u590D`, path, slotKey: slot.key });
|
|
788
|
+
else keys.add(slot.key);
|
|
789
|
+
if (typeof slot.label !== "string" || !slot.label)
|
|
790
|
+
issues.push({ code: "INVALID_SLOT_LABEL", message: "slot.label \u5FC5\u987B\u975E\u7A7A", path, slotKey: slot.key });
|
|
791
|
+
if (slot.required !== void 0 && typeof slot.required !== "boolean")
|
|
792
|
+
issues.push({ code: "INVALID_SLOT_REQUIRED", message: "slot.required \u5FC5\u987B boolean", path, slotKey: slot.key });
|
|
793
|
+
return issues;
|
|
794
|
+
}
|
|
795
|
+
function validateSlotKind(slot, node, path, adapters) {
|
|
796
|
+
const issues = [];
|
|
797
|
+
if (typeof slot.kind !== "string" || !slot.kind) {
|
|
798
|
+
issues.push({ code: "INVALID_SLOT_KIND", message: "slot.kind \u5FC5\u987B\u975E\u7A7A", path, slotKey: slot.key });
|
|
799
|
+
return issues;
|
|
800
|
+
}
|
|
801
|
+
const adapter = adapters.get(slot.kind);
|
|
802
|
+
if (!adapter) issues.push({ code: "NO_ADAPTER", message: `kind "${slot.kind}" \u672A\u6CE8\u518C`, path, slotKey: slot.key });
|
|
803
|
+
else if (!adapter.sourceNodeTypes.includes("*") && node.type && !adapter.sourceNodeTypes.includes(node.type)) {
|
|
804
|
+
issues.push({
|
|
805
|
+
code: "NODE_TYPE_MISMATCH",
|
|
806
|
+
message: `kind ${slot.kind} \u4E0D\u652F\u6301\u8282\u70B9 ${node.type}`,
|
|
807
|
+
path,
|
|
808
|
+
slotKey: slot.key
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
return issues;
|
|
812
|
+
}
|
|
813
|
+
function validateSlotTableCells(slot, node, path, schema) {
|
|
814
|
+
if (slot.validation?.type !== "table-required-cells" || node.type !== "table") return [];
|
|
815
|
+
const issues = [];
|
|
816
|
+
try {
|
|
817
|
+
const coordinates = collectTableCellCoords(node, schema);
|
|
818
|
+
const requiredCells = Array.isArray(slot.validation.cells) ? slot.validation.cells : [];
|
|
819
|
+
requiredCells.forEach((cell) => {
|
|
820
|
+
if (!coordinates.some((candidate) => candidate.row === cell.row && candidate.column === cell.column)) {
|
|
821
|
+
issues.push({
|
|
822
|
+
code: "REQUIRED_CELL_OUT_OF_RANGE",
|
|
823
|
+
message: `cell (${cell.row},${cell.column}) \u4E0D\u5B58\u5728`,
|
|
824
|
+
path,
|
|
825
|
+
slotKey: slot.key
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
} catch (error) {
|
|
830
|
+
issues.push({
|
|
831
|
+
code: "SCHEMA_CHECK_FAILED",
|
|
832
|
+
message: `\u8868\u683C schema \u6821\u9A8C\u5931\u8D25\uFF1A${error instanceof Error ? error.message : String(error)}`,
|
|
833
|
+
path,
|
|
834
|
+
slotKey: slot.key
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
return issues;
|
|
838
|
+
}
|
|
839
|
+
function validateSlotCodeBlockHint(slot, node, path) {
|
|
840
|
+
if (slot.kind !== "codeBlock" || !extractText(node).trim()) return [];
|
|
841
|
+
if (slot.validation?.type !== "non-whitespace-text" || !slot.validation.ignoreInitialContent) {
|
|
842
|
+
return [
|
|
843
|
+
{ code: "CODE_HINT_AS_VALUE", message: "\u4EE3\u7801\u5757\u9884\u586B\u5185\u5BB9\u5FC5\u987B\u914D\u7F6E ignoreInitialContent", path, slotKey: slot.key }
|
|
844
|
+
];
|
|
845
|
+
}
|
|
846
|
+
return [];
|
|
847
|
+
}
|
|
848
|
+
function readStructureCarriers(attrs) {
|
|
849
|
+
const carriers = {};
|
|
850
|
+
const pin = attrs[TEMPLATE_PIN_ATTR];
|
|
851
|
+
if (pin === "top" || pin === "bottom") carriers.pin = pin;
|
|
852
|
+
const groupId = attrs[TEMPLATE_GROUP_ID_ATTR];
|
|
853
|
+
if (typeof groupId === "string" && groupId) carriers.groupId = groupId;
|
|
854
|
+
const groupLabel = attrs[TEMPLATE_GROUP_LABEL_ATTR];
|
|
855
|
+
if (typeof groupLabel === "string" && groupLabel) carriers.groupLabel = groupLabel;
|
|
856
|
+
const blockRef = attrs[TEMPLATE_BLOCK_REF_ATTR];
|
|
857
|
+
if (typeof blockRef === "string" && blockRef) carriers.blockRef = blockRef;
|
|
858
|
+
const before = attrs[TEMPLATE_BEFORE_ATTR];
|
|
859
|
+
if (isStringArray2(before)) carriers.before = before;
|
|
860
|
+
const after = attrs[TEMPLATE_AFTER_ATTR];
|
|
861
|
+
if (isStringArray2(after)) carriers.after = after;
|
|
862
|
+
return carriers;
|
|
863
|
+
}
|
|
864
|
+
function buildStructureMeta(attrs, blockIdByRef) {
|
|
865
|
+
const carriers = readStructureCarriers(attrs);
|
|
866
|
+
const meta = {};
|
|
867
|
+
if (carriers.pin) meta.pin = carriers.pin;
|
|
868
|
+
if (carriers.groupId) meta.groupId = carriers.groupId;
|
|
869
|
+
if (carriers.groupLabel) meta.groupLabel = carriers.groupLabel;
|
|
870
|
+
const before = resolveBlockRefs(carriers.before, blockIdByRef);
|
|
871
|
+
if (before.length > 0) meta.before = before;
|
|
872
|
+
const after = resolveBlockRefs(carriers.after, blockIdByRef);
|
|
873
|
+
if (after.length > 0) meta.after = after;
|
|
874
|
+
return meta;
|
|
875
|
+
}
|
|
876
|
+
function resolveBlockRefs(refs, blockIdByRef) {
|
|
877
|
+
if (!refs) return [];
|
|
878
|
+
const resolved = [];
|
|
879
|
+
for (const ref of refs) {
|
|
880
|
+
const blockId = blockIdByRef.get(ref);
|
|
881
|
+
if (blockId) resolved.push(blockId);
|
|
882
|
+
}
|
|
883
|
+
return resolved;
|
|
884
|
+
}
|
|
885
|
+
function isStringArray2(value) {
|
|
886
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
887
|
+
}
|
|
888
|
+
function stripDefinitionAttrs(node) {
|
|
889
|
+
const {
|
|
890
|
+
templateSlot,
|
|
891
|
+
templateBlock,
|
|
892
|
+
[TEMPLATE_PIN_ATTR]: _pin,
|
|
893
|
+
[TEMPLATE_BLOCK_REF_ATTR]: _blockRef,
|
|
894
|
+
[TEMPLATE_BEFORE_ATTR]: _before,
|
|
895
|
+
[TEMPLATE_AFTER_ATTR]: _after,
|
|
896
|
+
[TEMPLATE_GROUP_ID_ATTR]: _groupId,
|
|
897
|
+
[TEMPLATE_GROUP_LABEL_ATTR]: _groupLabel,
|
|
898
|
+
...attrs
|
|
899
|
+
} = node.attrs ?? {};
|
|
900
|
+
return { ...structuredClone(node), attrs };
|
|
901
|
+
}
|
|
902
|
+
function wrapProtected(meta, content) {
|
|
903
|
+
return { type: "protectedBlock", attrs: { templateBlock: meta }, content: [content] };
|
|
904
|
+
}
|
|
905
|
+
function collectTableCellCoords(node, schema) {
|
|
906
|
+
const table = schema.nodeFromJSON(node);
|
|
907
|
+
table.check();
|
|
908
|
+
return collectLogicalTableCells(table).map(({ row, column }) => ({ row, column }));
|
|
909
|
+
}
|
|
910
|
+
function extractText(node) {
|
|
911
|
+
if (!node) return "";
|
|
912
|
+
return `${node.text ?? ""}${node.content?.map(extractText).join("") ?? ""}`;
|
|
913
|
+
}
|
|
914
|
+
function walkNodes(node, visit, path = "$", depth = 0) {
|
|
915
|
+
visit(node, path, depth);
|
|
916
|
+
node.content?.forEach((child, index) => walkNodes(child, visit, `${path}.content[${index}]`, depth + 1));
|
|
917
|
+
}
|
|
918
|
+
function computeStats(node) {
|
|
919
|
+
let maxDepth = 0;
|
|
920
|
+
let nodeCount = 0;
|
|
921
|
+
walkNodes(node, (_child, _path, depth) => {
|
|
922
|
+
nodeCount += 1;
|
|
923
|
+
maxDepth = Math.max(maxDepth, depth);
|
|
924
|
+
});
|
|
925
|
+
return { depth: maxDepth, nodeCount };
|
|
926
|
+
}
|
|
927
|
+
function isTemplateShape(value) {
|
|
928
|
+
if (!value || typeof value !== "object") return false;
|
|
929
|
+
const definition = value;
|
|
930
|
+
return typeof definition.schemaVersion === "number" && typeof definition.id === "string" && typeof definition.version === "string" && typeof definition.name === "string" && !!definition.content && typeof definition.content === "object";
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// src/editor/features/template/utils/lockedSkeleton.ts
|
|
934
|
+
var SKELETON_DROP_ATTRS = {
|
|
935
|
+
tableCell: /* @__PURE__ */ new Set(["colwidth"]),
|
|
936
|
+
tableHeader: /* @__PURE__ */ new Set(["colwidth"]),
|
|
937
|
+
tableRow: /* @__PURE__ */ new Set(["rowHeight"]),
|
|
938
|
+
// 纵向合并的派生值(rowspanSync 维护):不进骨架,否则同步事务被门控判定为「改了结构」而拒绝。
|
|
939
|
+
fixedTable: /* @__PURE__ */ new Set(["hasRowspan"]),
|
|
940
|
+
fixedTableCell: /* @__PURE__ */ new Set(["spanHeight", "spanOverflow"])
|
|
941
|
+
};
|
|
942
|
+
var SKELETON_APPEARANCE_ATTRS = {
|
|
943
|
+
paragraph: /* @__PURE__ */ new Set(["textAlign"]),
|
|
944
|
+
heading: /* @__PURE__ */ new Set(["textAlign"]),
|
|
945
|
+
fixedTableCell: /* @__PURE__ */ new Set(["textAlign", "backgroundColor", "width", "widthUnit", "bold"])
|
|
946
|
+
};
|
|
947
|
+
function serializeSkeletonAttrs(typeName, attrs) {
|
|
948
|
+
const drop = SKELETON_DROP_ATTRS[typeName];
|
|
949
|
+
const appearance = SKELETON_APPEARANCE_ATTRS[typeName];
|
|
950
|
+
const out = {};
|
|
951
|
+
let has = false;
|
|
952
|
+
for (const key of Object.keys(attrs)) {
|
|
953
|
+
if (drop?.has(key) || appearance?.has(key)) continue;
|
|
954
|
+
const value = attrs[key];
|
|
955
|
+
if (value === null || value === void 0) continue;
|
|
956
|
+
out[key] = value;
|
|
957
|
+
has = true;
|
|
958
|
+
}
|
|
959
|
+
return has ? out : void 0;
|
|
960
|
+
}
|
|
961
|
+
function structureSkeletonAttrs(meta) {
|
|
962
|
+
const attrs = {};
|
|
963
|
+
if (meta.pin !== void 0) attrs.pin = meta.pin;
|
|
964
|
+
if (meta.before !== void 0) attrs.before = meta.before;
|
|
965
|
+
if (meta.after !== void 0) attrs.after = meta.after;
|
|
966
|
+
if (meta.groupId !== void 0) attrs.groupId = meta.groupId;
|
|
967
|
+
if (meta.groupLabel !== void 0) attrs.groupLabel = meta.groupLabel;
|
|
968
|
+
return attrs;
|
|
969
|
+
}
|
|
970
|
+
function skeletonNode(node) {
|
|
971
|
+
const typeName = node.type.name;
|
|
972
|
+
if (typeName === "templateImage") {
|
|
973
|
+
return { t: "templateImage", a: { slotKey: node.attrs.slotKey ?? "" } };
|
|
974
|
+
}
|
|
975
|
+
if (typeName === "templateInlineSlot") {
|
|
976
|
+
return { t: "templateInlineSlot", a: { slotKey: node.attrs.slotKey ?? "" } };
|
|
977
|
+
}
|
|
978
|
+
if (typeName === "table" && node.attrs.templateTableMode === "data") {
|
|
979
|
+
const headerRow = node.firstChild;
|
|
980
|
+
const fixedRows = [];
|
|
981
|
+
node.forEach((row, _offset, rowIndex) => {
|
|
982
|
+
if (rowIndex > 0 && row.attrs.templateRowFixed === true) {
|
|
983
|
+
fixedRows.push(skeletonNode(row));
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
return {
|
|
987
|
+
t: "table",
|
|
988
|
+
a: {
|
|
989
|
+
templateTableMode: "data",
|
|
990
|
+
templateDataGlobalFixed: node.attrs.templateDataGlobalFixed === true
|
|
991
|
+
},
|
|
992
|
+
c: headerRow ? [skeletonNode(headerRow), ...fixedRows] : fixedRows
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
const meta = readTemplateBlockMeta(node);
|
|
996
|
+
if (typeName === "protectedBlock" && meta && meta.isSlot === false) {
|
|
997
|
+
const result2 = {
|
|
998
|
+
t: "protectedBlock",
|
|
999
|
+
a: { blockId: meta.blockId, isSlot: false, ...structureSkeletonAttrs(meta) }
|
|
1000
|
+
};
|
|
1001
|
+
const inner = node.firstChild;
|
|
1002
|
+
if (inner) {
|
|
1003
|
+
const innerSkeleton = skeletonNode(inner);
|
|
1004
|
+
if (innerSkeleton) result2.c = [innerSkeleton];
|
|
1005
|
+
}
|
|
1006
|
+
return result2;
|
|
1007
|
+
}
|
|
1008
|
+
if (typeName === "protectedBlock" && meta?.editable === true) {
|
|
1009
|
+
return {
|
|
1010
|
+
t: "protectedBlock",
|
|
1011
|
+
a: {
|
|
1012
|
+
blockId: meta.blockId,
|
|
1013
|
+
kind: meta.kind,
|
|
1014
|
+
editable: true,
|
|
1015
|
+
innerType: node.firstChild?.type.name ?? "",
|
|
1016
|
+
...structureSkeletonAttrs(meta)
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
if ((typeName === "tableCell" || typeName === "tableHeader" || typeName === "fixedTableCell") && typeof node.attrs.cellFormula === "string" && node.attrs.cellFormula.length > 0) {
|
|
1021
|
+
const result2 = { t: typeName };
|
|
1022
|
+
const attrs2 = serializeSkeletonAttrs(typeName, node.attrs);
|
|
1023
|
+
if (attrs2) result2.a = attrs2;
|
|
1024
|
+
return result2;
|
|
1025
|
+
}
|
|
1026
|
+
if ((typeName === "tableCell" || typeName === "tableHeader") && node.attrs.fillable === true) {
|
|
1027
|
+
return {
|
|
1028
|
+
t: typeName,
|
|
1029
|
+
a: {
|
|
1030
|
+
fillable: true,
|
|
1031
|
+
colspan: node.attrs.colspan ?? 1,
|
|
1032
|
+
rowspan: node.attrs.rowspan ?? 1,
|
|
1033
|
+
cellFormula: node.attrs.cellFormula ?? null
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
if (typeName === "fixedTableCell" && node.attrs.fillable === true) {
|
|
1038
|
+
return {
|
|
1039
|
+
t: typeName,
|
|
1040
|
+
a: {
|
|
1041
|
+
fillable: true,
|
|
1042
|
+
required: node.attrs.required === true,
|
|
1043
|
+
rowspan: typeof node.attrs.rowspan === "number" && node.attrs.rowspan > 1 ? node.attrs.rowspan : 1,
|
|
1044
|
+
cellFormula: node.attrs.cellFormula ?? null
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
const result = { t: typeName };
|
|
1049
|
+
const attrs = serializeSkeletonAttrs(typeName, node.attrs);
|
|
1050
|
+
if (attrs) result.a = attrs;
|
|
1051
|
+
if (node.isText) {
|
|
1052
|
+
result.text = node.text ?? "";
|
|
1053
|
+
if (node.marks.length) result.m = node.marks.map((mark) => mark.type.name);
|
|
1054
|
+
return result;
|
|
1055
|
+
}
|
|
1056
|
+
const children = [];
|
|
1057
|
+
node.forEach((child) => children.push(skeletonNode(child)));
|
|
1058
|
+
if (children.length) result.c = children;
|
|
1059
|
+
return result;
|
|
1060
|
+
}
|
|
1061
|
+
var skeletonCache = /* @__PURE__ */ new WeakMap();
|
|
1062
|
+
var fillSkeletonCache = /* @__PURE__ */ new WeakMap();
|
|
1063
|
+
function lockedSkeleton(doc, options) {
|
|
1064
|
+
const fillMode = options?.fillMode === true;
|
|
1065
|
+
const cache = fillMode ? fillSkeletonCache : skeletonCache;
|
|
1066
|
+
const cached = cache.get(doc);
|
|
1067
|
+
if (cached !== void 0) return cached;
|
|
1068
|
+
const children = [];
|
|
1069
|
+
doc.forEach((child) => {
|
|
1070
|
+
if (readTemplateBlockMeta(child)) {
|
|
1071
|
+
children.push(skeletonNode(child));
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
if (fillMode && !(child.type.name === "paragraph" && child.content.size === 0)) {
|
|
1075
|
+
children.push(skeletonNode(child));
|
|
1076
|
+
}
|
|
1077
|
+
});
|
|
1078
|
+
const result = JSON.stringify(children);
|
|
1079
|
+
cache.set(doc, result);
|
|
1080
|
+
return result;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// src/editor/features/template/utils/state.ts
|
|
1084
|
+
function auditDocument(doc) {
|
|
1085
|
+
const entries = {};
|
|
1086
|
+
let locked = false;
|
|
1087
|
+
doc.descendants((node, pos) => {
|
|
1088
|
+
const meta = readTemplateBlockMeta(node);
|
|
1089
|
+
if (meta) {
|
|
1090
|
+
locked = true;
|
|
1091
|
+
const existing = entries[meta.blockId];
|
|
1092
|
+
if (existing) existing.occurrence += 1;
|
|
1093
|
+
else {
|
|
1094
|
+
entries[meta.blockId] = {
|
|
1095
|
+
blockId: meta.blockId,
|
|
1096
|
+
meta,
|
|
1097
|
+
pos,
|
|
1098
|
+
nodeType: getProtectedContentNode(node)?.type.name ?? node.type.name,
|
|
1099
|
+
occurrence: 1
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
return false;
|
|
1103
|
+
}
|
|
1104
|
+
if (!locked) {
|
|
1105
|
+
if (node.type.name === "templateImage" && node.attrs.slotKey) locked = true;
|
|
1106
|
+
else if (node.type.name === "templateInlineSlot") locked = true;
|
|
1107
|
+
else if ((node.type.name === "tableCell" || node.type.name === "tableHeader") && node.attrs.fillable === true)
|
|
1108
|
+
locked = true;
|
|
1109
|
+
}
|
|
1110
|
+
return true;
|
|
1111
|
+
});
|
|
1112
|
+
const issues = [];
|
|
1113
|
+
for (const entry of Object.values(entries)) {
|
|
1114
|
+
const { meta } = entry;
|
|
1115
|
+
if (entry.occurrence !== 1) {
|
|
1116
|
+
issues.push(issueFor(meta, "duplicate", "DUPLICATE_BLOCK_ID", `\u6A21\u677F\u5757\u300C${meta.label}\u300D\u91CD\u590D`, entry.pos));
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
return { status: issues.length === 0 ? "ready" : "corrupted", issues, index: { entries }, locked };
|
|
1120
|
+
}
|
|
1121
|
+
function isEditAllowedUnderLock(before, after, options) {
|
|
1122
|
+
try {
|
|
1123
|
+
return lockedSkeleton(before, options) === lockedSkeleton(after, options);
|
|
1124
|
+
} catch {
|
|
1125
|
+
return false;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
function issueFor(meta, type, errorCode, message, pos) {
|
|
1129
|
+
return {
|
|
1130
|
+
type,
|
|
1131
|
+
blockId: meta.blockId,
|
|
1132
|
+
slotKey: meta.slotKey,
|
|
1133
|
+
kind: meta.kind,
|
|
1134
|
+
label: meta.label,
|
|
1135
|
+
required: meta.required,
|
|
1136
|
+
errorCode,
|
|
1137
|
+
message,
|
|
1138
|
+
pos
|
|
1139
|
+
};
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// src/editor/features/template/extensions/integrityExtension.ts
|
|
1143
|
+
var TEMPLATE_APPLY_META = "idocsTemplateApply";
|
|
1144
|
+
var Y_SYNC_META_KEYS = ["y-sync", "y-sync$"];
|
|
1145
|
+
function isRemoteSyncTransaction(tr2) {
|
|
1146
|
+
return Y_SYNC_META_KEYS.some((key) => {
|
|
1147
|
+
const meta = tr2.getMeta(key);
|
|
1148
|
+
return meta?.isChangeOrigin === true;
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
function isDragReorderTransaction(tr2) {
|
|
1152
|
+
const candidateIds = [];
|
|
1153
|
+
for (const step of tr2.steps) {
|
|
1154
|
+
const slice = step.slice;
|
|
1155
|
+
if (!slice) continue;
|
|
1156
|
+
let insertedBlockId = null;
|
|
1157
|
+
let insertedCount = 0;
|
|
1158
|
+
slice.content.forEach((node) => {
|
|
1159
|
+
const meta = readTemplateBlockMeta(node);
|
|
1160
|
+
if (meta) {
|
|
1161
|
+
insertedCount += 1;
|
|
1162
|
+
insertedBlockId = meta.blockId;
|
|
1163
|
+
}
|
|
1164
|
+
});
|
|
1165
|
+
if (insertedCount === 1 && insertedBlockId) candidateIds.push(insertedBlockId);
|
|
1166
|
+
}
|
|
1167
|
+
if (candidateIds.length === 0) return false;
|
|
1168
|
+
const beforeIds = /* @__PURE__ */ new Set();
|
|
1169
|
+
const afterIds = /* @__PURE__ */ new Set();
|
|
1170
|
+
tr2.before.forEach((node) => {
|
|
1171
|
+
const meta = readTemplateBlockMeta(node);
|
|
1172
|
+
if (meta) beforeIds.add(meta.blockId);
|
|
1173
|
+
});
|
|
1174
|
+
tr2.doc.forEach((node) => {
|
|
1175
|
+
const meta = readTemplateBlockMeta(node);
|
|
1176
|
+
if (meta) afterIds.add(meta.blockId);
|
|
1177
|
+
});
|
|
1178
|
+
if (beforeIds.size === 0 || beforeIds.size !== afterIds.size) return false;
|
|
1179
|
+
for (const id of beforeIds) if (!afterIds.has(id)) return false;
|
|
1180
|
+
return candidateIds.some((id) => beforeIds.has(id));
|
|
1181
|
+
}
|
|
1182
|
+
function passesStructureGate(structureOkBefore, tr2) {
|
|
1183
|
+
if (!structureOkBefore) return true;
|
|
1184
|
+
return checkTemplateStructure(tr2.doc);
|
|
1185
|
+
}
|
|
1186
|
+
function structureRejectReason(tr2) {
|
|
1187
|
+
const detail = inspectTemplateStructure(tr2.doc);
|
|
1188
|
+
if (!detail.orderConstraintsOk) return "order-constraint";
|
|
1189
|
+
if (!detail.pinOrderOk) return "pin-order";
|
|
1190
|
+
return "group-contiguity";
|
|
1191
|
+
}
|
|
1192
|
+
var TEMPLATE_STATE_KEY = new PluginKey("idocs-template-state");
|
|
1193
|
+
function createIntegrityPlugin(options) {
|
|
1194
|
+
const { onIntegrityError, onStructureViolation, onStructureReject, fillMode = false, onFillEditReject } = options;
|
|
1195
|
+
return new Plugin({
|
|
1196
|
+
key: TEMPLATE_STATE_KEY,
|
|
1197
|
+
state: {
|
|
1198
|
+
init: (_config, editorState) => {
|
|
1199
|
+
const audit = auditDocument(editorState.doc);
|
|
1200
|
+
if (audit.status !== "ready") {
|
|
1201
|
+
onIntegrityError?.({
|
|
1202
|
+
status: audit.status,
|
|
1203
|
+
message: audit.issues.map((i) => i.message).join("; "),
|
|
1204
|
+
issues: audit.issues
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
const structureOk = checkTemplateStructure(editorState.doc);
|
|
1208
|
+
if (!structureOk) {
|
|
1209
|
+
onStructureViolation?.(inspectTemplateStructure(editorState.doc));
|
|
1210
|
+
}
|
|
1211
|
+
return {
|
|
1212
|
+
index: audit.index,
|
|
1213
|
+
integrity: audit.status,
|
|
1214
|
+
locked: audit.locked,
|
|
1215
|
+
structureOk
|
|
1216
|
+
};
|
|
1217
|
+
},
|
|
1218
|
+
apply(tr2, value, _oldState, newState) {
|
|
1219
|
+
if (!tr2.docChanged) return value;
|
|
1220
|
+
const audit = auditDocument(newState.doc);
|
|
1221
|
+
if (audit.status !== "ready" && value.integrity === "ready") {
|
|
1222
|
+
onIntegrityError?.({
|
|
1223
|
+
status: audit.status,
|
|
1224
|
+
message: audit.issues.map((i) => i.message).join("; "),
|
|
1225
|
+
issues: audit.issues
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
const structureOk = checkTemplateStructure(newState.doc);
|
|
1229
|
+
if (value.structureOk && !structureOk) {
|
|
1230
|
+
onStructureViolation?.(inspectTemplateStructure(newState.doc));
|
|
1231
|
+
}
|
|
1232
|
+
return {
|
|
1233
|
+
index: audit.index,
|
|
1234
|
+
integrity: audit.status,
|
|
1235
|
+
locked: audit.locked,
|
|
1236
|
+
structureOk
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
},
|
|
1240
|
+
filterTransaction(tr2, state) {
|
|
1241
|
+
const pluginState = TEMPLATE_STATE_KEY.getState(state);
|
|
1242
|
+
if (!pluginState || !tr2.docChanged) return true;
|
|
1243
|
+
if (!pluginState.locked) return true;
|
|
1244
|
+
if (tr2.getMeta(TEMPLATE_APPLY_META)) return true;
|
|
1245
|
+
if (pluginState.integrity !== "ready") return true;
|
|
1246
|
+
if (isRemoteSyncTransaction(tr2)) return true;
|
|
1247
|
+
if (!fillMode && isDragReorderTransaction(tr2)) {
|
|
1248
|
+
if (passesStructureGate(pluginState.structureOk, tr2)) return true;
|
|
1249
|
+
onStructureReject?.({ reason: structureRejectReason(tr2) });
|
|
1250
|
+
return false;
|
|
1251
|
+
}
|
|
1252
|
+
if (isEditAllowedUnderLock(state.doc, tr2.doc, { fillMode })) {
|
|
1253
|
+
if (passesStructureGate(pluginState.structureOk, tr2)) return true;
|
|
1254
|
+
onStructureReject?.({ reason: structureRejectReason(tr2) });
|
|
1255
|
+
return false;
|
|
1256
|
+
}
|
|
1257
|
+
if (fillMode) onFillEditReject?.();
|
|
1258
|
+
return false;
|
|
1259
|
+
}
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
function createIntegrityExtension(options) {
|
|
1263
|
+
const plugin = createIntegrityPlugin(options);
|
|
1264
|
+
return Extension.create({ name: "idocsTemplateIntegrity", addProseMirrorPlugins: () => [plugin] });
|
|
1265
|
+
}
|
|
1266
|
+
function getTemplatePluginState(state) {
|
|
1267
|
+
return TEMPLATE_STATE_KEY.getState(state);
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
// src/editor/features/template/utils/controller.ts
|
|
1271
|
+
var TemplateController = class {
|
|
1272
|
+
constructor(options) {
|
|
1273
|
+
/** 默认模板加载失败时保留原因,供业务显式 validate 时返回。 */
|
|
1274
|
+
this.initialTemplateFailure = null;
|
|
1275
|
+
this.editor = options.editor;
|
|
1276
|
+
this.getConfig = options.getConfig;
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* 初始化模板。
|
|
1280
|
+
* 若配置了 initialTemplateId 且当前是空文档,自动套用该模板。
|
|
1281
|
+
* 已有内容(如加载已保存的文档)不覆盖。
|
|
1282
|
+
*/
|
|
1283
|
+
initialize() {
|
|
1284
|
+
const config = this.getConfig();
|
|
1285
|
+
if (!config?.initialTemplateId || !this.isDocEmpty()) return null;
|
|
1286
|
+
const result = this.apply({ definitionId: config.initialTemplateId, mode: "replace-empty" });
|
|
1287
|
+
if (result.ok === false) this.initialTemplateFailure = { message: result.message };
|
|
1288
|
+
return result;
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* 读取初始模板加载失败原因(成功或尚未初始化返回 null)。
|
|
1292
|
+
* 供上层(如预览页预备编辑器)判断空白表单链路是否可用,不改变本控制器其它行为。
|
|
1293
|
+
*/
|
|
1294
|
+
getInitialTemplateFailure() {
|
|
1295
|
+
return this.initialTemplateFailure?.message ?? null;
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* 应用模板到文档。
|
|
1299
|
+
* 编译定义 → 实例化(生成 blockId、写入自描述 meta)→ 提交事务。
|
|
1300
|
+
* 完整性插件对比前后文档:新增块自动放行。
|
|
1301
|
+
*/
|
|
1302
|
+
apply(request) {
|
|
1303
|
+
const readyError = this.checkEditable();
|
|
1304
|
+
if (readyError) return readyError;
|
|
1305
|
+
const definition = this.getConfig()?.definitions?.find((item) => item.id === request.definitionId) ?? getRegisteredTemplates().find((item) => item.id === request.definitionId);
|
|
1306
|
+
if (!definition)
|
|
1307
|
+
return { ok: false, code: "TEMPLATE_NOT_FOUND", message: tr("\u6A21\u677F ${0} \u672A\u627E\u5230", [request.definitionId]) };
|
|
1308
|
+
const compiled = compileTemplateDefinition(definition, this.editor.schema, getTemplateBlockRegistry());
|
|
1309
|
+
if (compiled.ok === false) return invalidDefinition(compiled.issues);
|
|
1310
|
+
const mode = request.mode ?? "replace-empty";
|
|
1311
|
+
if (mode === "replace-empty" && !this.isDocEmpty())
|
|
1312
|
+
return { ok: false, code: "DOCUMENT_NOT_EMPTY", message: tr("\u6587\u6863\u975E\u7A7A\u6216\u5DF2\u6709\u6A21\u677F\u5757") };
|
|
1313
|
+
if (mode === "insert" && this.selectionIntersectsProtectedBlock()) {
|
|
1314
|
+
return { ok: false, code: "SELECTION_INTERSECTS_TEMPLATE_BLOCK", message: tr("\u9009\u533A\u4F4D\u4E8E\u6216\u5305\u542B\u53D7\u4FDD\u62A4\u6A21\u677F\u5757") };
|
|
1315
|
+
}
|
|
1316
|
+
const instantiated = instantiateCompiledTemplate(compiled.template, this.editor.schema, getTemplateBlockRegistry());
|
|
1317
|
+
if (instantiated.ok === false) return invalidDefinition(instantiated.issues);
|
|
1318
|
+
const templateDoc = this.editor.schema.nodeFromJSON(instantiated.content);
|
|
1319
|
+
const { state, view } = this.editor;
|
|
1320
|
+
const transaction = state.tr;
|
|
1321
|
+
transaction.setMeta(TEMPLATE_APPLY_META, true);
|
|
1322
|
+
transaction.setMeta("addToHistory", false);
|
|
1323
|
+
if (mode === "replace-empty") transaction.replaceWith(0, state.doc.content.size, templateDoc.content);
|
|
1324
|
+
else transaction.replaceSelection(new Slice(templateDoc.content, 0, 0));
|
|
1325
|
+
if (mode === "insert") {
|
|
1326
|
+
const preViolated = declaresTemplateStructure(state.doc) && !checkTemplateStructure(state.doc);
|
|
1327
|
+
const resultDoc = transaction.doc;
|
|
1328
|
+
if (!preViolated && declaresTemplateStructure(resultDoc) && !checkTemplateStructure(resultDoc)) {
|
|
1329
|
+
return {
|
|
1330
|
+
ok: false,
|
|
1331
|
+
code: "STRUCTURE_VIOLATION",
|
|
1332
|
+
message: tr("\u63D2\u5165\u4F4D\u7F6E\u4E0D\u6EE1\u8DB3\u6A21\u677F\u7ED3\u6784\u7EA6\u675F\uFF08\u7F6E\u9876\u987B\u7F6E\u9876\u3001\u7F6E\u5E95\u987B\u7F6E\u5E95\u3001\u987A\u5E8F\u7EA6\u675F\u987B\u6EE1\u8DB3\u3001\u7EC4\u4EF6\u7EC4\u89C6\u4E3A\u4E00\u4F53\uFF09")
|
|
1333
|
+
};
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
view.dispatch(transaction);
|
|
1337
|
+
const index = getTemplatePluginState(this.editor.state)?.index;
|
|
1338
|
+
if (!index || !instantiated.blockIds.every((id) => index.entries[id])) {
|
|
1339
|
+
return { ok: false, code: "INTEGRITY_VIOLATION", message: tr("\u6A21\u677F\u5E94\u7528\u4E8B\u52A1\u672A\u88AB\u63A5\u53D7") };
|
|
1340
|
+
}
|
|
1341
|
+
this.initialTemplateFailure = null;
|
|
1342
|
+
return { ok: true, value: { blockIds: instantiated.blockIds } };
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* 校验模板必填项是否填写。
|
|
1346
|
+
* 遍历所有必填槽位,通过 adapter.isFilled 检查内容,触发 onValidate 回调。
|
|
1347
|
+
*/
|
|
1348
|
+
validate() {
|
|
1349
|
+
if (!this.isReady()) return { status: "not-ready", valid: false, issues: [] };
|
|
1350
|
+
const pluginState = getTemplatePluginState(this.editor.state);
|
|
1351
|
+
if (!pluginState) return { status: "not-ready", valid: false, issues: [] };
|
|
1352
|
+
if (pluginState.integrity !== "ready") {
|
|
1353
|
+
const result2 = { status: "corrupted", valid: false, issues: [] };
|
|
1354
|
+
this.getConfig()?.onValidate?.(result2);
|
|
1355
|
+
return result2;
|
|
1356
|
+
}
|
|
1357
|
+
if (this.initialTemplateFailure) {
|
|
1358
|
+
const initialTemplateId = this.getConfig()?.initialTemplateId ?? "";
|
|
1359
|
+
const result2 = {
|
|
1360
|
+
status: "ready",
|
|
1361
|
+
valid: false,
|
|
1362
|
+
issues: [
|
|
1363
|
+
{
|
|
1364
|
+
type: "invalid",
|
|
1365
|
+
blockId: "",
|
|
1366
|
+
slotKey: "",
|
|
1367
|
+
kind: "generic",
|
|
1368
|
+
label: initialTemplateId || tr("\u9ED8\u8BA4\u6A21\u677F"),
|
|
1369
|
+
required: true,
|
|
1370
|
+
errorCode: "INITIAL_TEMPLATE_NOT_APPLIED",
|
|
1371
|
+
message: tr("\u9ED8\u8BA4\u6A21\u677F\u300C${0}\u300D\u672A\u52A0\u8F7D\uFF1A${1}", [
|
|
1372
|
+
initialTemplateId || tr("\u672A\u6307\u5B9A"),
|
|
1373
|
+
this.initialTemplateFailure.message
|
|
1374
|
+
])
|
|
1375
|
+
}
|
|
1376
|
+
]
|
|
1377
|
+
};
|
|
1378
|
+
this.getConfig()?.onValidate?.(result2);
|
|
1379
|
+
return result2;
|
|
1380
|
+
}
|
|
1381
|
+
const issues = [];
|
|
1382
|
+
for (const entry of Object.values(pluginState.index.entries)) {
|
|
1383
|
+
const { meta } = entry;
|
|
1384
|
+
if (!meta.isSlot || !meta.required) continue;
|
|
1385
|
+
const content = getProtectedContentNode(this.editor.state.doc.nodeAt(entry.pos));
|
|
1386
|
+
const adapter = getTemplateBlockRegistry().get(meta.kind);
|
|
1387
|
+
if (!content || !adapter) continue;
|
|
1388
|
+
let filled;
|
|
1389
|
+
try {
|
|
1390
|
+
filled = adapter.isFilled(content, meta);
|
|
1391
|
+
} catch (e) {
|
|
1392
|
+
issues.push({
|
|
1393
|
+
type: "invalid",
|
|
1394
|
+
blockId: meta.blockId,
|
|
1395
|
+
slotKey: meta.slotKey,
|
|
1396
|
+
kind: meta.kind,
|
|
1397
|
+
label: meta.label,
|
|
1398
|
+
required: true,
|
|
1399
|
+
errorCode: "VALIDATION_ERROR",
|
|
1400
|
+
message: tr("\u5FC5\u586B\u6A21\u5757\u300C${0}\u300D\u6821\u9A8C\u5F02\u5E38\uFF1A${1}", [meta.label, e instanceof Error ? e.message : String(e)]),
|
|
1401
|
+
pos: entry.pos
|
|
1402
|
+
});
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
if (!filled) {
|
|
1406
|
+
issues.push({
|
|
1407
|
+
type: "unfilled",
|
|
1408
|
+
blockId: meta.blockId,
|
|
1409
|
+
slotKey: meta.slotKey,
|
|
1410
|
+
kind: meta.kind,
|
|
1411
|
+
label: meta.label,
|
|
1412
|
+
required: true,
|
|
1413
|
+
errorCode: "UNFILLED_BLOCK",
|
|
1414
|
+
message: tr("\u5FC5\u586B\u6A21\u5757\u300C${0}\u300D\u672A\u586B\u5199", [meta.label]),
|
|
1415
|
+
pos: entry.pos
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
this.editor.state.doc.descendants((node, pos) => {
|
|
1420
|
+
if (node.type.name !== "templateInlineSlot") return true;
|
|
1421
|
+
const value = String(node.attrs.value ?? "");
|
|
1422
|
+
if (node.attrs.required === true && !value.trim()) {
|
|
1423
|
+
const label = node.attrs.slotLabel || node.attrs.slotKey || "\u5185\u8054\u586B\u5199\u9879";
|
|
1424
|
+
issues.push({
|
|
1425
|
+
type: "unfilled",
|
|
1426
|
+
blockId: "",
|
|
1427
|
+
slotKey: node.attrs.slotKey || "",
|
|
1428
|
+
kind: "generic",
|
|
1429
|
+
label,
|
|
1430
|
+
required: true,
|
|
1431
|
+
errorCode: "UNFILLED_INLINE_SLOT",
|
|
1432
|
+
message: tr("\u5FC5\u586B\u586B\u5199\u9879\u300C${0}\u300D\u672A\u586B\u5199", [label]),
|
|
1433
|
+
pos
|
|
1434
|
+
});
|
|
1435
|
+
}
|
|
1436
|
+
return false;
|
|
1437
|
+
});
|
|
1438
|
+
const result = { status: "ready", valid: issues.length === 0, issues };
|
|
1439
|
+
this.getConfig()?.onValidate?.(result);
|
|
1440
|
+
return result;
|
|
1441
|
+
}
|
|
1442
|
+
/** 检查是否允许通过 setContent/clear 写入普通内容(文档已套模板时禁止,避免误删受保护块)。 */
|
|
1443
|
+
canReplaceWithPlainContent() {
|
|
1444
|
+
const readyError = this.checkEditable();
|
|
1445
|
+
if (readyError) return readyError;
|
|
1446
|
+
const index = getTemplatePluginState(this.editor.state)?.index;
|
|
1447
|
+
if (index && Object.keys(index.entries).length > 0) {
|
|
1448
|
+
return { ok: false, code: "DOCUMENT_NOT_EMPTY", message: tr("\u5F53\u524D\u6587\u6863\u5DF2\u5957\u7528\u6A21\u677F\uFF0C\u65E0\u6CD5\u6574\u4F53\u66FF\u6362") };
|
|
1449
|
+
}
|
|
1450
|
+
return { ok: true, value: void 0 };
|
|
1451
|
+
}
|
|
1452
|
+
/** 聚焦到指定模板块,将光标定位到该块并滚动到可见区域。 */
|
|
1453
|
+
focusBlock(blockId) {
|
|
1454
|
+
const entry = getTemplatePluginState(this.editor.state)?.index.entries[blockId];
|
|
1455
|
+
if (!entry) return { ok: false, code: "MISSING_BLOCK", message: tr("block ${0} \u672A\u627E\u5230", [blockId]) };
|
|
1456
|
+
this.editor.commands.setNodeSelection(entry.pos);
|
|
1457
|
+
this.editor.commands.scrollIntoView();
|
|
1458
|
+
this.editor.commands.focus();
|
|
1459
|
+
return { ok: true, value: void 0 };
|
|
1460
|
+
}
|
|
1461
|
+
/** 检查编辑器是否已就绪(未销毁且 view 存在)。 */
|
|
1462
|
+
isReady() {
|
|
1463
|
+
return !this.editor.isDestroyed && !!this.editor.view;
|
|
1464
|
+
}
|
|
1465
|
+
/** 检查编辑器是否可编辑,返回错误对象或 null。 */
|
|
1466
|
+
checkEditable() {
|
|
1467
|
+
if (!this.isReady()) return { ok: false, code: "NOT_READY", message: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
1468
|
+
if (!this.editor.isEditable) return { ok: false, code: "READ_ONLY", message: tr("\u7F16\u8F91\u5668\u53EA\u8BFB") };
|
|
1469
|
+
return null;
|
|
1470
|
+
}
|
|
1471
|
+
/** 检查当前选区是否与受保护块相交。 */
|
|
1472
|
+
selectionIntersectsProtectedBlock() {
|
|
1473
|
+
const { selection, doc } = this.editor.state;
|
|
1474
|
+
for (let depth = selection.$from.depth; depth >= 0; depth -= 1) {
|
|
1475
|
+
if (readTemplateBlockMeta(selection.$from.node(depth))) return true;
|
|
1476
|
+
}
|
|
1477
|
+
let intersects = false;
|
|
1478
|
+
if (!selection.empty)
|
|
1479
|
+
doc.nodesBetween(selection.from, selection.to, (node) => {
|
|
1480
|
+
if (readTemplateBlockMeta(node)) {
|
|
1481
|
+
intersects = true;
|
|
1482
|
+
return false;
|
|
1483
|
+
}
|
|
1484
|
+
return true;
|
|
1485
|
+
});
|
|
1486
|
+
return intersects;
|
|
1487
|
+
}
|
|
1488
|
+
/** 检查文档是否为空(单个空段落且无受保护块)。 */
|
|
1489
|
+
isDocEmpty() {
|
|
1490
|
+
const { doc } = this.editor.state;
|
|
1491
|
+
const empty = doc.childCount === 1 && doc.firstChild?.type.name === "paragraph" && doc.firstChild.content.size === 0;
|
|
1492
|
+
let hasProtected = false;
|
|
1493
|
+
doc.descendants((node) => {
|
|
1494
|
+
if (readTemplateBlockMeta(node)) {
|
|
1495
|
+
hasProtected = true;
|
|
1496
|
+
return false;
|
|
1497
|
+
}
|
|
1498
|
+
return true;
|
|
1499
|
+
});
|
|
1500
|
+
return empty && !hasProtected;
|
|
1501
|
+
}
|
|
1502
|
+
};
|
|
1503
|
+
function createTemplateController(options) {
|
|
1504
|
+
return new TemplateController(options);
|
|
1505
|
+
}
|
|
1506
|
+
function invalidDefinition(issues) {
|
|
1507
|
+
return { ok: false, code: "INVALID_DEFINITION", message: issues.map((issue) => issue.message).join("; ") };
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
// src/editor/features/template/utils/stripInternals.ts
|
|
1511
|
+
function stripTemplateDataAttributes(html) {
|
|
1512
|
+
return html.replace(/<span\b[^>]*\bidocs-template-inline-slot\b[^>]*>([\s\S]*?)<\/span>/gi, "$1").replace(/\s*data-template-id="[^"]*"/gi, "").replace(/\s*data-template-meta="[^"]*"/gi, "").replace(/\s*data-template-block="[^"]*"/gi, "").replace(/\s*data-template-protected-block(?:="[^"]*")?/gi, "").replace(/\s*data-template-editable="[^"]*"/gi, "").replace(/\s*data-template-placeholder(?:="[^"]*")?/gi, "").replace(/\s*data-template-image(?:="[^"]*")?/gi, "").replace(/\s*data-template-inline-slot(?:="[^"]*")?/gi, "").replace(/\s*data-template-table-mode="[^"]*"/gi, "").replace(/\s*data-template-data-global-fixed="[^"]*"/gi, "").replace(/\s*data-template-row-default="[^"]*"/gi, "").replace(/\s*data-template-row-fixed="[^"]*"/gi, "").replace(/\s*data-template-cell-fixed="[^"]*"/gi, "").replace(/\s*data-template-cell-fixed-local="[^"]*"/gi, "").replace(/\s*data-fillable="[^"]*"/gi, "").replace(/\s*data-bold="[^"]*"/gi, "").replace(/\s*data-background-color="[^"]*"/gi, "").replace(/\s*data-width-ratio="[^"]*"/gi, "").replace(/\s*data-width-unit="[^"]*"/gi, "").replace(/\s*data-row-height="[^"]*"/gi, "").replace(/\s*data-slot-key="[^"]*"/gi, "").replace(/\s*data-slot-label="[^"]*"/gi, "").replace(/\s*data-required="[^"]*"/gi, "").replace(/\s*data-column-type="[^"]*"/gi, "").replace(/\s*data-column-options="[^"]*"/gi, "").replace(/\s*data-column-date-format="[^"]*"/gi, "").replace(/\s*data-cell-type="[^"]*"/gi, "").replace(/\s*data-cell-options="[^"]*"/gi, "").replace(/\s*data-cell-date-format="[^"]*"/gi, "").replace(/\s*data-cell-formula="[^"]*"/gi, "").replace(/\s*data-cell-formula-escape="[^"]*"/gi, "").replace(/\s*data-rowspan="[^"]*"/gi, "").replace(/\s*data-covered="[^"]*"/gi, "").replace(/\s*data-has-rowspan(?:="[^"]*")?/gi, "");
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
export { TEMPLATE_AFTER_ATTR, TEMPLATE_BEFORE_ATTR, TEMPLATE_BLOCK_REF_ATTR, TEMPLATE_DATA_BINDING_ATTR, TEMPLATE_GROUP_ID_ATTR, TEMPLATE_GROUP_LABEL_ATTR, TEMPLATE_PIN_ATTR, TemplateController, createIntegrityExtension, createTemplateController, effectivePin, findGroupViolation, findOrderConstraintCyclicRefs, findPinSequenceViolation, getProtectedContentNode, getRegisteredTemplateBlockExtensions, getTemplateBlockRegistry, getTemplatePluginState, parseTemplateBlockMeta, readStructureCarriers, readTemplateBlockMeta, registerTemplateBlocks, registerTemplates, renderTemplateBlockMeta, stripTemplateDataAttributes };
|
|
1516
|
+
//# sourceMappingURL=chunk-EOUNN3NA.mjs.map
|
|
1517
|
+
//# sourceMappingURL=chunk-EOUNN3NA.mjs.map
|