@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,1510 @@
|
|
|
1
|
+
import { findAncestor, resolveCellControl } from './chunk-K7Z56Q4M.mjs';
|
|
2
|
+
import { createStyles } from './chunk-ZM3WULHX.mjs';
|
|
3
|
+
import { sanitizeHtml } from './chunk-4W6PPBHQ.mjs';
|
|
4
|
+
import { stripTemplateDataAttributes, getTemplatePluginState, getProtectedContentNode, readTemplateBlockMeta } from './chunk-EOUNN3NA.mjs';
|
|
5
|
+
import { tr } from './chunk-W627LLF4.mjs';
|
|
6
|
+
import { memo, useRef, useEffect, useMemo, useState, useCallback } from 'react';
|
|
7
|
+
import { keyframes, css, cx } from '@emotion/css';
|
|
8
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
9
|
+
import { Selection, NodeSelection } from '@tiptap/pm/state';
|
|
10
|
+
import { selectedRect, TableMap, CellSelection } from '@tiptap/pm/tables';
|
|
11
|
+
|
|
12
|
+
// src/editor/features/outline/extractHeadings.ts
|
|
13
|
+
function extractHeadings(editor, config) {
|
|
14
|
+
const minLevel = config?.minLevel ?? 1;
|
|
15
|
+
const maxLevel = config?.maxLevel ?? 6;
|
|
16
|
+
const items = [];
|
|
17
|
+
const doc = editor.state.doc;
|
|
18
|
+
doc.descendants((node, pos) => {
|
|
19
|
+
if (node.type.name === "heading") {
|
|
20
|
+
const level = node.attrs.level;
|
|
21
|
+
if (level >= minLevel && level <= maxLevel) {
|
|
22
|
+
const text = node.textContent.trim();
|
|
23
|
+
if (text) {
|
|
24
|
+
items.push({
|
|
25
|
+
id: `outline-${pos}`,
|
|
26
|
+
level,
|
|
27
|
+
text,
|
|
28
|
+
pos
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return items;
|
|
35
|
+
}
|
|
36
|
+
function areOutlineItemsEqual(prev, next) {
|
|
37
|
+
if (prev.length !== next.length) return false;
|
|
38
|
+
return prev.every((item, index) => {
|
|
39
|
+
const other = next[index];
|
|
40
|
+
return item.id === other.id && item.level === other.level && item.text === other.text && item.pos === other.pos;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
var useOutlineStyles = createStyles(({ css: css2 }) => ({
|
|
44
|
+
panel: css2`
|
|
45
|
+
display: flex;
|
|
46
|
+
flex-direction: column;
|
|
47
|
+
width: 220px;
|
|
48
|
+
min-width: 180px;
|
|
49
|
+
max-height: 100%;
|
|
50
|
+
overflow: hidden;
|
|
51
|
+
border-left: 1px solid var(--colorBorder, #e8e8e8);
|
|
52
|
+
background: var(--colorBgContainer, #fff);
|
|
53
|
+
font-size: 13px;
|
|
54
|
+
`,
|
|
55
|
+
/* 标题与右侧操作(如「钉住」开关)同一行两端对齐;
|
|
56
|
+
上/下内边距取 token 变量(--padding=12px、--paddingXXS=4px);16px 右侧留白无等值变量,保留字面量 */
|
|
57
|
+
header: css2`
|
|
58
|
+
flex-shrink: 0;
|
|
59
|
+
display: flex;
|
|
60
|
+
align-items: center;
|
|
61
|
+
justify-content: space-between;
|
|
62
|
+
gap: 8px;
|
|
63
|
+
padding: var(--padding, 12px) 16px var(--paddingXXS, 4px);
|
|
64
|
+
`,
|
|
65
|
+
/* 标题栏右侧插槽:自定义操作(钉住按钮等)不被标题挤压 */
|
|
66
|
+
headerExtra: css2`
|
|
67
|
+
display: flex;
|
|
68
|
+
align-items: center;
|
|
69
|
+
flex-shrink: 0;
|
|
70
|
+
`,
|
|
71
|
+
title: css2`
|
|
72
|
+
margin: 0;
|
|
73
|
+
font-size: var(--fontSizeSM, 12px);
|
|
74
|
+
font-weight: 500;
|
|
75
|
+
color: var(--colorTextTertiary, #8c8c8c);
|
|
76
|
+
`,
|
|
77
|
+
/* 左右留白让条目圆角底色不贴面板边缘(8px/10px 无等值 token,保留字面量) */
|
|
78
|
+
body: css2`
|
|
79
|
+
flex: 1;
|
|
80
|
+
overflow-y: auto;
|
|
81
|
+
padding: var(--paddingXXS, 4px) 8px 10px;
|
|
82
|
+
`,
|
|
83
|
+
list: css2`
|
|
84
|
+
list-style: none;
|
|
85
|
+
margin: 0;
|
|
86
|
+
padding: 0;
|
|
87
|
+
`,
|
|
88
|
+
item: css2`
|
|
89
|
+
position: relative;
|
|
90
|
+
border-radius: var(--borderRadius, 6px);
|
|
91
|
+
transition: background 0.15s;
|
|
92
|
+
|
|
93
|
+
&:hover {
|
|
94
|
+
background: var(--colorFillTertiary, #f5f5f5);
|
|
95
|
+
}
|
|
96
|
+
`,
|
|
97
|
+
/* &&& 提升优先级:激活底色须压过 item 的 :hover(Emotion 类无书写顺序保证) */
|
|
98
|
+
itemActive: css2`
|
|
99
|
+
&&&,
|
|
100
|
+
&&&:hover {
|
|
101
|
+
background: var(--colorPrimary-1, #e6f4ff);
|
|
102
|
+
}
|
|
103
|
+
`,
|
|
104
|
+
itemBtn: css2`
|
|
105
|
+
all: unset;
|
|
106
|
+
display: block;
|
|
107
|
+
box-sizing: border-box;
|
|
108
|
+
width: 100%;
|
|
109
|
+
cursor: pointer;
|
|
110
|
+
padding: 5px 8px;
|
|
111
|
+
line-height: 1.5;
|
|
112
|
+
`,
|
|
113
|
+
itemText: css2`
|
|
114
|
+
display: -webkit-box;
|
|
115
|
+
-webkit-line-clamp: 2;
|
|
116
|
+
-webkit-box-orient: vertical;
|
|
117
|
+
overflow: hidden;
|
|
118
|
+
text-overflow: ellipsis;
|
|
119
|
+
word-break: break-all;
|
|
120
|
+
color: var(--colorTextSecondary, #595959);
|
|
121
|
+
`,
|
|
122
|
+
/* 标题级别字重/字号梯度:H1 最突出,H2 中等,H3 默认,H4 及更深弱化 */
|
|
123
|
+
itemTextH1: css2`
|
|
124
|
+
font-weight: 600;
|
|
125
|
+
color: var(--colorText, #262626);
|
|
126
|
+
`,
|
|
127
|
+
itemTextH2: css2`
|
|
128
|
+
font-weight: 500;
|
|
129
|
+
`,
|
|
130
|
+
itemTextDeep: css2`
|
|
131
|
+
font-size: var(--fontSizeSM, 12px);
|
|
132
|
+
color: var(--colorTextTertiary, #8c8c8c);
|
|
133
|
+
`,
|
|
134
|
+
/* && 提升优先级:激活文字的色值/字重须压过级别梯度(同为单类时顺序不定) */
|
|
135
|
+
itemTextActive: css2`
|
|
136
|
+
&& {
|
|
137
|
+
color: var(--colorPrimary, #1677ff);
|
|
138
|
+
font-weight: 500;
|
|
139
|
+
}
|
|
140
|
+
`,
|
|
141
|
+
empty: css2`
|
|
142
|
+
display: flex;
|
|
143
|
+
align-items: center;
|
|
144
|
+
justify-content: center;
|
|
145
|
+
padding: 32px 16px;
|
|
146
|
+
`,
|
|
147
|
+
emptyText: css2`
|
|
148
|
+
color: var(--colorTextQuaternary, #bfbfbf);
|
|
149
|
+
font-size: 13px;
|
|
150
|
+
`
|
|
151
|
+
}));
|
|
152
|
+
var outlineFlash = keyframes`
|
|
153
|
+
0%, 100% { background-color: transparent; }
|
|
154
|
+
30% { background-color: var(--colorWarningBg, #fffbe6); }
|
|
155
|
+
`;
|
|
156
|
+
var outlineHighlightClass = css`
|
|
157
|
+
animation: ${outlineFlash} 1.5s ease;
|
|
158
|
+
border-radius: var(--borderRadiusSM, 4px);
|
|
159
|
+
`;
|
|
160
|
+
|
|
161
|
+
// src/editor/features/outline/useOutline.ts
|
|
162
|
+
function scrollToPos(editor, pos, highlight = true) {
|
|
163
|
+
try {
|
|
164
|
+
const { view } = editor;
|
|
165
|
+
if (!view || editor.isDestroyed) return null;
|
|
166
|
+
const docSize = editor.state.doc.content.size;
|
|
167
|
+
const safePos = Math.min(pos, docSize);
|
|
168
|
+
editor.chain().focus().setTextSelection(safePos + 1).run();
|
|
169
|
+
const domAtPos = view.domAtPos(safePos + 1);
|
|
170
|
+
const targetNode = domAtPos.node;
|
|
171
|
+
const element = targetNode.nodeType === Node.ELEMENT_NODE ? targetNode : targetNode.parentElement;
|
|
172
|
+
if (element) {
|
|
173
|
+
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
174
|
+
if (highlight) {
|
|
175
|
+
element.classList.add(outlineHighlightClass);
|
|
176
|
+
return setTimeout(() => element.classList.remove(outlineHighlightClass), 1500);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
function useOutline(editor, config) {
|
|
184
|
+
const [items, setItems] = useState([]);
|
|
185
|
+
const [activeId, setActiveId] = useState(null);
|
|
186
|
+
const configRef = useRef(config);
|
|
187
|
+
configRef.current = config;
|
|
188
|
+
const highlightTimerRef = useRef(null);
|
|
189
|
+
const refresh = useCallback(() => {
|
|
190
|
+
if (!editor || editor.isDestroyed) {
|
|
191
|
+
setItems((prev) => prev.length === 0 ? prev : []);
|
|
192
|
+
setActiveId(null);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const newItems = extractHeadings(editor, configRef.current);
|
|
196
|
+
setItems((prev) => areOutlineItemsEqual(prev, newItems) ? prev : newItems);
|
|
197
|
+
}, [editor]);
|
|
198
|
+
const updateActiveItem = useCallback(() => {
|
|
199
|
+
if (!editor || editor.isDestroyed || items.length === 0) {
|
|
200
|
+
setActiveId(null);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const { from } = editor.state.selection;
|
|
204
|
+
let closest = null;
|
|
205
|
+
for (const item of items) {
|
|
206
|
+
if (item.pos <= from) {
|
|
207
|
+
closest = item;
|
|
208
|
+
} else {
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
setActiveId(closest?.id ?? null);
|
|
213
|
+
}, [editor, items]);
|
|
214
|
+
const refreshRef = useRef(refresh);
|
|
215
|
+
refreshRef.current = refresh;
|
|
216
|
+
const updateActiveItemRef = useRef(updateActiveItem);
|
|
217
|
+
updateActiveItemRef.current = updateActiveItem;
|
|
218
|
+
useEffect(() => {
|
|
219
|
+
if (!editor || editor.isDestroyed) return;
|
|
220
|
+
refreshRef.current();
|
|
221
|
+
const handleUpdate = () => refreshRef.current();
|
|
222
|
+
const handleSelectionUpdate = () => updateActiveItemRef.current();
|
|
223
|
+
editor.on("update", handleUpdate);
|
|
224
|
+
editor.on("selectionUpdate", handleSelectionUpdate);
|
|
225
|
+
return () => {
|
|
226
|
+
editor.off("update", handleUpdate);
|
|
227
|
+
editor.off("selectionUpdate", handleSelectionUpdate);
|
|
228
|
+
};
|
|
229
|
+
}, [editor]);
|
|
230
|
+
useEffect(() => {
|
|
231
|
+
updateActiveItem();
|
|
232
|
+
}, [items, updateActiveItem]);
|
|
233
|
+
const scrollToItem = useCallback(
|
|
234
|
+
(id) => {
|
|
235
|
+
if (!editor || editor.isDestroyed) return;
|
|
236
|
+
const target = items.find((item) => item.id === id);
|
|
237
|
+
if (target) {
|
|
238
|
+
const highlight = config?.highlightOnClick !== false;
|
|
239
|
+
if (highlightTimerRef.current) clearTimeout(highlightTimerRef.current);
|
|
240
|
+
highlightTimerRef.current = scrollToPos(editor, target.pos, highlight);
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
[editor, items, config?.highlightOnClick]
|
|
244
|
+
);
|
|
245
|
+
useEffect(
|
|
246
|
+
() => () => {
|
|
247
|
+
if (highlightTimerRef.current) clearTimeout(highlightTimerRef.current);
|
|
248
|
+
},
|
|
249
|
+
[]
|
|
250
|
+
);
|
|
251
|
+
return { items, activeId, scrollToItem, refresh };
|
|
252
|
+
}
|
|
253
|
+
var OutlineEntry = memo(function OutlineEntry2({ item, isActive, onClick, styles }) {
|
|
254
|
+
const indent = 8 + (item.level - 1) * 12;
|
|
255
|
+
const levelText = item.level === 1 ? styles.itemTextH1 : item.level === 2 ? styles.itemTextH2 : item.level >= 4 ? styles.itemTextDeep : void 0;
|
|
256
|
+
return /* @__PURE__ */ jsx(
|
|
257
|
+
"li",
|
|
258
|
+
{
|
|
259
|
+
className: cx(
|
|
260
|
+
"outline-item",
|
|
261
|
+
`outline-item-h${item.level}`,
|
|
262
|
+
isActive && "outline-item-active",
|
|
263
|
+
styles.item,
|
|
264
|
+
isActive && styles.itemActive
|
|
265
|
+
),
|
|
266
|
+
role: "treeitem",
|
|
267
|
+
"aria-level": item.level,
|
|
268
|
+
"aria-current": isActive ? "location" : void 0,
|
|
269
|
+
children: /* @__PURE__ */ jsx(
|
|
270
|
+
"button",
|
|
271
|
+
{
|
|
272
|
+
className: cx("outline-item-btn", styles.itemBtn),
|
|
273
|
+
style: { paddingLeft: `${indent}px` },
|
|
274
|
+
onClick: () => onClick(item.id),
|
|
275
|
+
title: item.text,
|
|
276
|
+
type: "button",
|
|
277
|
+
children: /* @__PURE__ */ jsx("span", { className: cx("outline-item-text", styles.itemText, levelText, isActive && styles.itemTextActive), children: item.text })
|
|
278
|
+
}
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
var OutlinePanel = memo(function OutlinePanel2({
|
|
284
|
+
editor,
|
|
285
|
+
config,
|
|
286
|
+
headerExtra,
|
|
287
|
+
onItemsChange
|
|
288
|
+
}) {
|
|
289
|
+
const { items, activeId, scrollToItem } = useOutline(editor, config);
|
|
290
|
+
const { styles, cx: cx2 } = useOutlineStyles();
|
|
291
|
+
const title = config?.title ?? tr("\u76EE\u5F55");
|
|
292
|
+
const onItemsChangeRef = useRef(onItemsChange);
|
|
293
|
+
onItemsChangeRef.current = onItemsChange;
|
|
294
|
+
useEffect(() => {
|
|
295
|
+
onItemsChangeRef.current?.(items);
|
|
296
|
+
}, [items]);
|
|
297
|
+
const content = useMemo(() => {
|
|
298
|
+
if (items.length === 0) {
|
|
299
|
+
return /* @__PURE__ */ jsx("div", { className: cx2("outline-empty", styles.empty), children: /* @__PURE__ */ jsx("span", { className: cx2("outline-empty-text", styles.emptyText), children: tr("\u6682\u65E0\u6807\u9898") }) });
|
|
300
|
+
}
|
|
301
|
+
return /* @__PURE__ */ jsx("ul", { className: cx2("outline-list", styles.list), role: "tree", "aria-label": title, children: items.map((item) => /* @__PURE__ */ jsx(
|
|
302
|
+
OutlineEntry,
|
|
303
|
+
{
|
|
304
|
+
item,
|
|
305
|
+
isActive: item.id === activeId,
|
|
306
|
+
onClick: scrollToItem,
|
|
307
|
+
styles
|
|
308
|
+
},
|
|
309
|
+
item.id
|
|
310
|
+
)) });
|
|
311
|
+
}, [items, activeId, scrollToItem, title, styles, cx2]);
|
|
312
|
+
return /* @__PURE__ */ jsxs("nav", { className: cx2("outline-panel", styles.panel, config?.className), "aria-label": title, children: [
|
|
313
|
+
/* @__PURE__ */ jsxs("div", { className: cx2("outline-header", styles.header), children: [
|
|
314
|
+
/* @__PURE__ */ jsx("h3", { className: cx2("outline-title", styles.title), children: title }),
|
|
315
|
+
headerExtra ? /* @__PURE__ */ jsx("div", { className: cx2("outline-header-extra", styles.headerExtra), children: headerExtra }) : null
|
|
316
|
+
] }),
|
|
317
|
+
/* @__PURE__ */ jsx("div", { className: cx2("outline-body", styles.body), children: content })
|
|
318
|
+
] });
|
|
319
|
+
});
|
|
320
|
+
function locateByBlockId(editor, blockId) {
|
|
321
|
+
const entry = getTemplatePluginState(editor.state)?.index.entries[blockId];
|
|
322
|
+
if (!entry) return null;
|
|
323
|
+
const outer = editor.state.doc.nodeAt(entry.pos);
|
|
324
|
+
const inner = getProtectedContentNode(outer);
|
|
325
|
+
if (!inner) return null;
|
|
326
|
+
return { node: inner, pos: entry.pos + 1, blockId, blockLabel: entry.meta.label };
|
|
327
|
+
}
|
|
328
|
+
function locateBySelection(editor) {
|
|
329
|
+
const { selection } = editor.state;
|
|
330
|
+
if (selection instanceof NodeSelection) {
|
|
331
|
+
const meta = readTemplateBlockMeta(selection.node);
|
|
332
|
+
if (meta) {
|
|
333
|
+
const inner = selection.node.firstChild;
|
|
334
|
+
return inner ? { node: inner, pos: selection.from + 1, blockId: meta.blockId, blockLabel: meta.label } : null;
|
|
335
|
+
}
|
|
336
|
+
return { node: selection.node, pos: selection.from };
|
|
337
|
+
}
|
|
338
|
+
const { $from } = selection;
|
|
339
|
+
for (let depth = $from.depth; depth >= 1; depth -= 1) {
|
|
340
|
+
const node = $from.node(depth);
|
|
341
|
+
const meta = readTemplateBlockMeta(node);
|
|
342
|
+
if (meta) {
|
|
343
|
+
const inner = node.firstChild;
|
|
344
|
+
return inner ? { node: inner, pos: $from.before(depth) + 1, blockId: meta.blockId, blockLabel: meta.label } : null;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if ($from.depth >= 1) {
|
|
348
|
+
return { node: $from.node(1), pos: $from.before(1) };
|
|
349
|
+
}
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
function locateBlockNode(editor, blockId) {
|
|
353
|
+
if (blockId) return locateByBlockId(editor, blockId);
|
|
354
|
+
return locateBySelection(editor);
|
|
355
|
+
}
|
|
356
|
+
function attrValueEquals(a, b) {
|
|
357
|
+
if (a === b) return true;
|
|
358
|
+
try {
|
|
359
|
+
return JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
|
|
360
|
+
} catch {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function resolveInsertPosition(editor, position) {
|
|
365
|
+
if (position?.blockId) {
|
|
366
|
+
const entry = getTemplatePluginState(editor.state)?.index.entries[position.blockId];
|
|
367
|
+
if (!entry) {
|
|
368
|
+
return { ok: false, error: tr("\u672A\u627E\u5230 blockId=${0} \u7684\u6A21\u677F\u5757", [position.blockId]) };
|
|
369
|
+
}
|
|
370
|
+
const container = editor.state.doc.nodeAt(entry.pos);
|
|
371
|
+
if (!container) {
|
|
372
|
+
return { ok: false, error: tr("\u6A21\u677F\u5757 ${0} \u7684\u5B9A\u4F4D\u5DF2\u5931\u6548\uFF08\u6587\u6863\u521A\u88AB\u4FEE\u6539\uFF09\uFF0C\u8BF7\u91CD\u8BD5", [position.blockId]) };
|
|
373
|
+
}
|
|
374
|
+
return { ok: true, pos: position.placement === "before" ? entry.pos : entry.pos + container.nodeSize };
|
|
375
|
+
}
|
|
376
|
+
return { ok: true, pos: editor.state.doc.content.size };
|
|
377
|
+
}
|
|
378
|
+
function restoreSelection(editor, saved) {
|
|
379
|
+
try {
|
|
380
|
+
const doc = editor.state.doc;
|
|
381
|
+
const anchor = Math.max(0, Math.min(saved.from, doc.content.size));
|
|
382
|
+
editor.view.dispatch(editor.state.tr.setSelection(Selection.near(doc.resolve(anchor))));
|
|
383
|
+
} catch {
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/editor/core/refApi/template.ts
|
|
388
|
+
function createTemplateApi(getController) {
|
|
389
|
+
return {
|
|
390
|
+
applyTemplate: (request) => {
|
|
391
|
+
const controller = getController();
|
|
392
|
+
if (!controller) return { ok: false, code: "FEATURE_UNAVAILABLE", message: tr("\u6A21\u677F\u529F\u80FD\u4E0D\u53EF\u7528") };
|
|
393
|
+
return controller.apply(request);
|
|
394
|
+
},
|
|
395
|
+
validateTemplate: () => {
|
|
396
|
+
const controller = getController();
|
|
397
|
+
if (!controller) return { status: "not-ready", valid: false, issues: [] };
|
|
398
|
+
return controller.validate();
|
|
399
|
+
},
|
|
400
|
+
focusTemplateBlock: (blockId) => {
|
|
401
|
+
const controller = getController();
|
|
402
|
+
if (!controller) return { ok: false, code: "FEATURE_UNAVAILABLE", message: tr("\u6A21\u677F\u529F\u80FD\u4E0D\u53EF\u7528") };
|
|
403
|
+
return controller.focusBlock(blockId);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function createBlockAttrsApi(getEditor) {
|
|
408
|
+
return {
|
|
409
|
+
getBlockAttributes: (query) => {
|
|
410
|
+
const editor = getEditor();
|
|
411
|
+
if (!editor || editor.isDestroyed) return null;
|
|
412
|
+
const target = locateBlockNode(editor, query.blockId);
|
|
413
|
+
if (!target) return null;
|
|
414
|
+
return { nodeType: target.node.type.name, attrs: { ...target.node.attrs } };
|
|
415
|
+
},
|
|
416
|
+
updateBlockAttributes: (query, attrs) => {
|
|
417
|
+
const editor = getEditor();
|
|
418
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
419
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u4FEE\u6539\u5757\u5C5E\u6027") };
|
|
420
|
+
const entries = Object.entries(attrs ?? {});
|
|
421
|
+
if (entries.length === 0) return { ok: false, error: tr("\u6CA1\u6709\u8981\u4FEE\u6539\u7684\u5C5E\u6027") };
|
|
422
|
+
const target = locateBlockNode(editor, query.blockId);
|
|
423
|
+
if (!target) {
|
|
424
|
+
return {
|
|
425
|
+
ok: false,
|
|
426
|
+
error: query.blockId ? tr("\u672A\u627E\u5230 blockId=${0} \u7684\u6A21\u677F\u5757", [query.blockId]) : tr("\u672A\u6307\u5B9A blockId\uFF0C\u4E14\u5F53\u524D\u5149\u6807\u4E0D\u5728\u4EFB\u4F55\u53EF\u5B9A\u4F4D\u7684\u5757\u5185")
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
const docBefore = editor.state.doc;
|
|
430
|
+
try {
|
|
431
|
+
const transaction = editor.state.tr.setNodeMarkup(target.pos, void 0, {
|
|
432
|
+
...target.node.attrs,
|
|
433
|
+
...attrs
|
|
434
|
+
});
|
|
435
|
+
editor.view.dispatch(transaction);
|
|
436
|
+
} catch (err) {
|
|
437
|
+
return { ok: false, error: tr("\u5C5E\u6027\u5199\u5165\u4E8B\u52A1\u6784\u9020\u5931\u8D25\uFF1A${0}", [String(err)]) };
|
|
438
|
+
}
|
|
439
|
+
if (editor.state.doc === docBefore) {
|
|
440
|
+
return {
|
|
441
|
+
ok: false,
|
|
442
|
+
error: tr(
|
|
443
|
+
"\u4FEE\u6539\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u8BE5\u5C5E\u6027\u6539\u52A8\u88AB\u5224\u5B9A\u4E3A\u300C\u6539\u7ED3\u6784\u300D\u3002\u4EC5\u8868\u73B0\u7C7B\u5C5E\u6027\uFF08\u5BF9\u9F50/\u989C\u8272/\u884C\u9AD8/\u5BBD\u9AD8\u7B49\uFF09\u53EF\u7ECF\u672C\u901A\u9053\u4FEE\u6539\uFF0C\u7ED3\u6784\u5C5E\u6027\uFF08\u5408\u5E76\u5355\u5143\u683C\u3001\u53EF\u586B\u6807\u8BB0\u3001\u6A21\u677F\u914D\u7F6E\uFF09\u5C5E\u6A21\u677F\u8BBE\u8BA1\u671F\u914D\u7F6E"
|
|
444
|
+
)
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
const afterNode = editor.state.doc.nodeAt(target.pos);
|
|
448
|
+
const applied = entries.filter(([key, value]) => afterNode != null && attrValueEquals(afterNode.attrs[key], value)).map(([key]) => key);
|
|
449
|
+
if (applied.length === 0) {
|
|
450
|
+
return {
|
|
451
|
+
ok: false,
|
|
452
|
+
error: tr("\u5C5E\u6027\u672A\u751F\u6548\uFF1A${0}\uFF08\u53EF\u80FD\u4E0D\u662F ${1} \u8282\u70B9\u7684\u5408\u6CD5\u5C5E\u6027\uFF09", [
|
|
453
|
+
entries.map(([key]) => key).join("\u3001"),
|
|
454
|
+
target.node.type.name
|
|
455
|
+
])
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
return { ok: true, applied };
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/editor/core/refApi/blocks.ts
|
|
464
|
+
function topLevelTypeCounts(doc) {
|
|
465
|
+
const counts = /* @__PURE__ */ new Map();
|
|
466
|
+
doc.forEach((node) => counts.set(node.type.name, (counts.get(node.type.name) ?? 0) + 1));
|
|
467
|
+
return counts;
|
|
468
|
+
}
|
|
469
|
+
function diffTopLevelTypes(before, afterDoc) {
|
|
470
|
+
const remaining = new Map(before);
|
|
471
|
+
const inserted = [];
|
|
472
|
+
afterDoc.forEach((node) => {
|
|
473
|
+
const left = remaining.get(node.type.name) ?? 0;
|
|
474
|
+
if (left > 0) {
|
|
475
|
+
remaining.set(node.type.name, left - 1);
|
|
476
|
+
} else {
|
|
477
|
+
inserted.push(node.type.name);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return inserted;
|
|
481
|
+
}
|
|
482
|
+
function createBlockOpsApi(getEditor) {
|
|
483
|
+
return {
|
|
484
|
+
insertBlocksAt: (position, html) => {
|
|
485
|
+
const editor = getEditor();
|
|
486
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
487
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u63D2\u5165\u5757") };
|
|
488
|
+
const markup = stripTemplateDataAttributes(sanitizeHtml(html ?? ""));
|
|
489
|
+
if (!markup.trim()) {
|
|
490
|
+
return { ok: false, error: tr("\u8981\u63D2\u5165\u7684 HTML \u4E3A\u7A7A\uFF08\u6216\u6D88\u6BD2\u540E\u65E0\u6709\u6548\u5185\u5BB9\uFF09") };
|
|
491
|
+
}
|
|
492
|
+
const located = resolveInsertPosition(editor, position);
|
|
493
|
+
if (!located.ok) return located;
|
|
494
|
+
const pos = located.pos;
|
|
495
|
+
const docBefore = editor.state.doc;
|
|
496
|
+
const typesBefore = topLevelTypeCounts(docBefore);
|
|
497
|
+
try {
|
|
498
|
+
editor.commands.insertContentAt(pos, markup);
|
|
499
|
+
} catch (err) {
|
|
500
|
+
return { ok: false, error: tr("\u63D2\u5165\u4E8B\u52A1\u6267\u884C\u5931\u8D25\uFF1A${0}", [String(err)]) };
|
|
501
|
+
}
|
|
502
|
+
if (editor.state.doc === docBefore) {
|
|
503
|
+
return {
|
|
504
|
+
ok: false,
|
|
505
|
+
error: tr(
|
|
506
|
+
"\u63D2\u5165\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u76EE\u6807\u4F4D\u7F6E\u5904\u4E8E\u6A21\u677F\u9501\u5B9A\u533A\uFF08\u9AA8\u67B6\u56FA\u5B9A\u5185\u5BB9\uFF09\uFF0C\u7ED3\u6784\u88AB\u6A21\u677F\u9501\u5B9A\u4E0D\u53EF\u63D2\u5165\u3002\u5EFA\u8BAE\uFF1A\u6539\u4E3A\u5728\u6587\u6863\u672B\u5C3E\u8FFD\u52A0\uFF08position \u7F3A\u7701\uFF09\uFF0C\u6216\u9009\u62E9\u6A21\u677F\u81EA\u7531\u533A\u7684\u4F4D\u7F6E"
|
|
507
|
+
)
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
const inserted = diffTopLevelTypes(typesBefore, editor.state.doc);
|
|
511
|
+
if (inserted.length === 0) {
|
|
512
|
+
return { ok: false, error: tr("\u5185\u5BB9\u672A\u751F\u6210\u4EFB\u4F55\u9876\u5C42\u5757\uFF08HTML \u987B\u4E3A\u5757\u7EA7\u5185\u5BB9\uFF0C\u5982\u6BB5\u843D/\u6807\u9898/\u8868\u683C\uFF09") };
|
|
513
|
+
}
|
|
514
|
+
return { ok: true, inserted };
|
|
515
|
+
},
|
|
516
|
+
removeBlock: (blockId) => {
|
|
517
|
+
const editor = getEditor();
|
|
518
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
519
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u5220\u9664\u5757") };
|
|
520
|
+
let pos;
|
|
521
|
+
let businessType;
|
|
522
|
+
if (blockId) {
|
|
523
|
+
const entry = getTemplatePluginState(editor.state)?.index.entries[blockId];
|
|
524
|
+
if (!entry) {
|
|
525
|
+
return { ok: false, error: tr("\u672A\u627E\u5230 blockId=${0} \u7684\u6A21\u677F\u5757", [blockId]) };
|
|
526
|
+
}
|
|
527
|
+
pos = entry.pos;
|
|
528
|
+
businessType = entry.nodeType;
|
|
529
|
+
} else {
|
|
530
|
+
const located = locateBlockNode(editor);
|
|
531
|
+
if (!located) {
|
|
532
|
+
return { ok: false, error: tr("\u672A\u6307\u5B9A blockId\uFF0C\u4E14\u5F53\u524D\u5149\u6807\u4E0D\u5728\u4EFB\u4F55\u53EF\u5B9A\u4F4D\u7684\u5757\u5185") };
|
|
533
|
+
}
|
|
534
|
+
const resolved = editor.state.doc.resolve(located.pos);
|
|
535
|
+
pos = resolved.depth >= 1 ? resolved.before(1) : located.pos;
|
|
536
|
+
businessType = located.node.type.name;
|
|
537
|
+
}
|
|
538
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
539
|
+
if (!node) {
|
|
540
|
+
return { ok: false, error: tr("\u76EE\u6807\u5757\u5B9A\u4F4D\u5DF2\u5931\u6548\uFF08\u6587\u6863\u521A\u88AB\u4FEE\u6539\uFF09\uFF0C\u8BF7\u91CD\u8BD5") };
|
|
541
|
+
}
|
|
542
|
+
const docBefore = editor.state.doc;
|
|
543
|
+
const transaction = editor.state.tr.delete(pos, pos + node.nodeSize);
|
|
544
|
+
if (!transaction.docChanged) {
|
|
545
|
+
return { ok: false, error: tr("\u65E0\u6CD5\u6784\u9020\u5220\u9664\u4E8B\u52A1\uFF08\u6587\u6863\u4E0D\u53EF\u5220\u7A7A\uFF0C\u8BF7\u81F3\u5C11\u4FDD\u7559\u4E00\u4E2A\u5757\uFF09") };
|
|
546
|
+
}
|
|
547
|
+
editor.view.dispatch(transaction);
|
|
548
|
+
if (editor.state.doc === docBefore) {
|
|
549
|
+
return {
|
|
550
|
+
ok: false,
|
|
551
|
+
error: tr(
|
|
552
|
+
"\u5220\u9664\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u8BE5\u5757\u5C5E\u6A21\u677F\u9501\u5B9A\u9AA8\u67B6\uFF08\u9501\u5B9A\u5757\u4E0D\u53EF\u5220\uFF09\uFF0C\u4EC5\u6A21\u677F\u81EA\u7531\u533A\u5757\u53EF\u5220\u3002\u5EFA\u8BAE\uFF1A\u5982\u786E\u9700\u8C03\u6574\u6A21\u677F\u7ED3\u6784\uFF0C\u8BF7\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668"
|
|
553
|
+
)
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return { ok: true, removed: businessType };
|
|
557
|
+
},
|
|
558
|
+
insertDiagram: (spec, position) => {
|
|
559
|
+
const editor = getEditor();
|
|
560
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
561
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u63D2\u5165\u56FE\u5F62\u5757") };
|
|
562
|
+
const nodeName = spec.kind === "flowchart" ? "flowchartBlock" : "mindmapBlock";
|
|
563
|
+
let dataJson;
|
|
564
|
+
try {
|
|
565
|
+
dataJson = JSON.stringify(spec.data ?? {});
|
|
566
|
+
} catch (err) {
|
|
567
|
+
return { ok: false, error: tr("\u56FE\u6570\u636E\u5E8F\u5217\u5316\u5931\u8D25\uFF1A${0}", [String(err)]) };
|
|
568
|
+
}
|
|
569
|
+
const located = resolveInsertPosition(editor, position);
|
|
570
|
+
if (!located.ok) return located;
|
|
571
|
+
const docBefore = editor.state.doc;
|
|
572
|
+
try {
|
|
573
|
+
editor.commands.insertContentAt(located.pos, {
|
|
574
|
+
type: nodeName,
|
|
575
|
+
attrs: { data: dataJson, svg: spec.svg ?? "", width: null, height: null }
|
|
576
|
+
});
|
|
577
|
+
} catch (err) {
|
|
578
|
+
return {
|
|
579
|
+
ok: false,
|
|
580
|
+
error: tr("\u56FE\u5F62\u5757\u63D2\u5165\u4E8B\u52A1\u6267\u884C\u5931\u8D25\uFF1A${0}\uFF08\u53EF\u80FD\u539F\u56E0\uFF1A${1} \u63D2\u4EF6\u672A\u542F\u7528\uFF0C\u8282\u70B9\u7C7B\u578B ${2} \u672A\u6CE8\u518C\uFF09", [
|
|
581
|
+
String(err),
|
|
582
|
+
spec.kind === "flowchart" ? tr("\u6D41\u7A0B\u56FE") : tr("\u601D\u7EF4\u5BFC\u56FE"),
|
|
583
|
+
nodeName
|
|
584
|
+
])
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
if (editor.state.doc === docBefore) {
|
|
588
|
+
return {
|
|
589
|
+
ok: false,
|
|
590
|
+
error: tr(
|
|
591
|
+
"\u63D2\u5165\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u76EE\u6807\u4F4D\u7F6E\u5904\u4E8E\u6A21\u677F\u9501\u5B9A\u533A\uFF08\u9AA8\u67B6\u56FA\u5B9A\u5185\u5BB9\uFF09\uFF0C\u7ED3\u6784\u88AB\u6A21\u677F\u9501\u5B9A\u4E0D\u53EF\u63D2\u5165\u3002\u5EFA\u8BAE\uFF1A\u6539\u4E3A\u5728\u6587\u6863\u672B\u5C3E\u8FFD\u52A0\uFF08position \u7F3A\u7701\uFF09\uFF0C\u6216\u9009\u62E9\u6A21\u677F\u81EA\u7531\u533A\u7684\u4F4D\u7F6E"
|
|
592
|
+
)
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
return { ok: true, inserted: nodeName };
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/editor/core/refApi/marks.ts
|
|
601
|
+
function findAnchorRanges(block, anchorText) {
|
|
602
|
+
let text = "";
|
|
603
|
+
const starts = [];
|
|
604
|
+
const contentBase = block.pos + 1;
|
|
605
|
+
block.node.descendants((node, relPos) => {
|
|
606
|
+
if (node.isText && node.text) {
|
|
607
|
+
const base = contentBase + relPos;
|
|
608
|
+
for (let i = 0; i < node.text.length; i += 1) starts.push(base + i);
|
|
609
|
+
text += node.text;
|
|
610
|
+
}
|
|
611
|
+
return true;
|
|
612
|
+
});
|
|
613
|
+
const preview = text.slice(0, 100);
|
|
614
|
+
const ranges = [];
|
|
615
|
+
if (anchorText && anchorText.length <= text.length) {
|
|
616
|
+
let index = text.indexOf(anchorText);
|
|
617
|
+
while (index >= 0) {
|
|
618
|
+
ranges.push({
|
|
619
|
+
from: starts[index],
|
|
620
|
+
to: starts[index + anchorText.length - 1] + 1
|
|
621
|
+
});
|
|
622
|
+
index = text.indexOf(anchorText, index + anchorText.length);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return { ranges, preview };
|
|
626
|
+
}
|
|
627
|
+
function createMarksApi(getEditor, getAiCommands) {
|
|
628
|
+
return {
|
|
629
|
+
applyMarksToRange: (blockId, target, marks) => {
|
|
630
|
+
const editor = getEditor();
|
|
631
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
632
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u5E94\u7528\u884C\u5185\u6807\u8BB0") };
|
|
633
|
+
if (!Array.isArray(marks) || marks.length === 0) {
|
|
634
|
+
return { ok: false, error: tr("\u6CA1\u6709\u8981\u5E94\u7528\u7684\u6807\u8BB0\uFF08marks \u5FC5\u987B\u4E3A\u975E\u7A7A\u6570\u7EC4\uFF09") };
|
|
635
|
+
}
|
|
636
|
+
if (typeof target?.anchorText !== "string" || !target.anchorText) {
|
|
637
|
+
return {
|
|
638
|
+
ok: false,
|
|
639
|
+
error: tr(
|
|
640
|
+
"\u951A\u6587\u672C\u4E0D\u80FD\u4E3A\u7A7A\uFF08\u5F53\u524D\u503C\uFF1A\u672A\u63D0\u4F9B\uFF1B\u5408\u6CD5\u503C\uFF1A\u4E0E\u6587\u6863\u539F\u6587\u5B8C\u5168\u4E00\u81F4\u7684\u6587\u5B57\u3002\u5EFA\u8BAE\uFF1A\u5148\u8C03 idocs_doc_get_content \u8BFB\u539F\u6587\uFF09"
|
|
641
|
+
)
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
const markDefs = new Map(
|
|
645
|
+
getAiCommands().filter((command) => command.kind === "mark").map((command) => [command.id, command])
|
|
646
|
+
);
|
|
647
|
+
if (markDefs.size === 0) {
|
|
648
|
+
return { ok: false, error: tr("\u5F53\u524D\u7F16\u8F91\u5668\u6CA1\u6709\u5DF2\u542F\u7528\u7684\u884C\u5185\u6807\u8BB0\u80FD\u529B\uFF08\u6240\u6709\u6807\u8BB0\u7C7B\u63D2\u4EF6\u5747\u672A\u542F\u7528\uFF09") };
|
|
649
|
+
}
|
|
650
|
+
const unknownIds = marks.filter((patch) => !markDefs.has(patch.id)).map((patch) => patch.id);
|
|
651
|
+
if (unknownIds.length > 0) {
|
|
652
|
+
return {
|
|
653
|
+
ok: false,
|
|
654
|
+
error: tr(
|
|
655
|
+
"\u672A\u77E5\u7684\u6807\u8BB0 id\uFF1A${0}\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1A${1}\u3002\u5EFA\u8BAE\uFF1A\u5148\u8C03 idocs_doc_command_schema \u67E5\u8BE2 mark \u7C7B\u6761\u76EE\uFF09",
|
|
656
|
+
[unknownIds.join("\u3001"), [...markDefs.keys()].join("\u3001")]
|
|
657
|
+
)
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
const located = locateBlockNode(editor, blockId);
|
|
661
|
+
if (!located) {
|
|
662
|
+
return {
|
|
663
|
+
ok: false,
|
|
664
|
+
error: blockId ? tr("\u672A\u627E\u5230 blockId=${0} \u7684\u6A21\u677F\u5757", [blockId]) : tr("\u672A\u6307\u5B9A blockId\uFF0C\u4E14\u5F53\u524D\u5149\u6807\u4E0D\u5728\u4EFB\u4F55\u53EF\u5B9A\u4F4D\u7684\u5757\u5185")
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
const { ranges, preview } = findAnchorRanges(located, target.anchorText);
|
|
668
|
+
if (ranges.length === 0) {
|
|
669
|
+
return {
|
|
670
|
+
ok: false,
|
|
671
|
+
error: tr(
|
|
672
|
+
"\u5757\u5185\u672A\u627E\u5230\u951A\u6587\u672C\u300C${0}\u300D\uFF08\u5B9E\u9645\u5339\u914D 0 \u5904\u3002\u5757\u5185\u524D 100 \u5B57\uFF1A\u300C${1}\u300D\u3002\u5EFA\u8BAE\uFF1A\u951A\u6587\u672C\u987B\u4E0E\u6587\u6863\u6587\u5B57\u5B8C\u5168\u4E00\u81F4\uFF08\u5927\u5C0F\u5199\u654F\u611F\u3001\u542B\u6807\u70B9\uFF09\uFF0C\u53EF\u5148\u7528 idocs_doc_get_content \u8BFB\u539F\u6587\uFF09",
|
|
673
|
+
[target.anchorText, preview]
|
|
674
|
+
)
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
const occurrence = target.occurrence ?? "all";
|
|
678
|
+
let hits;
|
|
679
|
+
if (occurrence === "all") {
|
|
680
|
+
hits = ranges;
|
|
681
|
+
} else {
|
|
682
|
+
if (!Number.isInteger(occurrence) || occurrence < 1) {
|
|
683
|
+
return {
|
|
684
|
+
ok: false,
|
|
685
|
+
error: tr(
|
|
686
|
+
"occurrence \u975E\u6CD5\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1A\u6B63\u6574\u6570\uFF081 \u8D77\u7684\u7B2C N \u5904\uFF09\u6216\u7F3A\u7701=\u5168\u90E8\u547D\u4E2D\u3002\u5EFA\u8BAE\uFF1A\u7F3A\u7701\u6216\u4F20 1 \u5230 ${1} \u4E4B\u95F4\u7684\u6574\u6570\uFF09",
|
|
687
|
+
[String(occurrence), String(ranges.length)]
|
|
688
|
+
)
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
if (occurrence > ranges.length) {
|
|
692
|
+
return {
|
|
693
|
+
ok: false,
|
|
694
|
+
error: tr(
|
|
695
|
+
"\u951A\u6587\u672C\u7B2C ${0} \u5904\u4E0D\u5B58\u5728\uFF08\u5B9E\u9645\u5339\u914D ${1} \u5904\u3002\u5757\u5185\u524D 100 \u5B57\uFF1A\u300C${2}\u300D\u3002\u5EFA\u8BAE\uFF1A\u6838\u5BF9\u951A\u6587\u672C\u6216\u6539\u7528 occurrence \u7F3A\u7701\u547D\u4E2D\u5168\u90E8\uFF09",
|
|
696
|
+
[String(occurrence), String(ranges.length), preview]
|
|
697
|
+
)
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
hits = [ranges[occurrence - 1]];
|
|
701
|
+
}
|
|
702
|
+
const savedSelection = { from: editor.state.selection.from, to: editor.state.selection.to };
|
|
703
|
+
const applied = [];
|
|
704
|
+
let totalHits = 0;
|
|
705
|
+
for (const patch of marks) {
|
|
706
|
+
const def = markDefs.get(patch.id);
|
|
707
|
+
let hitCount = 0;
|
|
708
|
+
for (const range of hits) {
|
|
709
|
+
const docBefore = editor.state.doc;
|
|
710
|
+
let ran = false;
|
|
711
|
+
try {
|
|
712
|
+
ran = def.apply?.(editor, { from: range.from, to: range.to }, patch.params) === true;
|
|
713
|
+
} catch {
|
|
714
|
+
ran = false;
|
|
715
|
+
}
|
|
716
|
+
if (ran && editor.state.doc !== docBefore) hitCount += 1;
|
|
717
|
+
}
|
|
718
|
+
applied.push(`${patch.id}\xD7${hitCount}`);
|
|
719
|
+
totalHits += hitCount;
|
|
720
|
+
}
|
|
721
|
+
restoreSelection(editor, savedSelection);
|
|
722
|
+
if (totalHits === 0) {
|
|
723
|
+
return {
|
|
724
|
+
ok: false,
|
|
725
|
+
applied,
|
|
726
|
+
error: tr(
|
|
727
|
+
"\u6807\u8BB0\u672A\u751F\u6548\uFF1A\u76EE\u6807\u951A\u6587\u672C\u5904\u4E8E\u6A21\u677F\u9501\u5B9A\u533A\u56FA\u5B9A\u6587\u672C\u4E0A\uFF0C\u884C\u5185\u6807\u8BB0\u6539\u52A8\u88AB\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF08\u53EF\u7F16\u8F91\u69FD\u4F4D/\u53EF\u586B\u683C/\u81EA\u7531\u533A\u7684\u6587\u5B57\u624D\u53EF\u6807\u8BB0\u3002\u5EFA\u8BAE\uFF1A\u6539\u9009\u53EF\u7F16\u8F91\u533A\u6587\u5B57\uFF0C\u6216\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668\u8C03\u6574\u6A21\u677F\uFF09"
|
|
728
|
+
)
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
return { ok: true, applied };
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
function checkCellFillable(cell) {
|
|
736
|
+
if (cell.attrs.templateCellFixed === true || cell.attrs.templateCellFixedLocal === true) {
|
|
737
|
+
return tr(
|
|
738
|
+
"\u8BE5\u5355\u5143\u683C\u662F\u6A21\u677F\u9501\u5B9A\u683C\uFF08templateCellFixed\uFF09\uFF0C\u5185\u5BB9\u5C5E\u6A21\u677F\u56FA\u5B9A\u90E8\u5206\u4E0D\u53EF\u586B\u5199\uFF08\u5EFA\u8BAE\uFF1A\u6539\u586B\u540C\u8868 fillable \u53EF\u586B\u683C\uFF0C\u6216\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668\u8C03\u6574\uFF09"
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
if (cell.attrs.fillable !== true) {
|
|
742
|
+
return tr(
|
|
743
|
+
"\u8BE5\u5355\u5143\u683C\u4E0D\u662F\u53EF\u586B\u683C\uFF08fillable=false\uFF0C\u5F53\u524D\u503C\uFF1A\u56FA\u5B9A\u683C\uFF1B\u5408\u6CD5\u76EE\u6807\uFF1Afillable=true \u7684\u53EF\u586B\u683C\u3002\u5EFA\u8BAE\uFF1A\u5148\u7528 idocs_doc_validate \u67E5\u770B\u53EF\u586B\u9879\u6E05\u5355\uFF09"
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
if (typeof cell.attrs.cellFormula === "string" && cell.attrs.cellFormula.length > 0) {
|
|
747
|
+
return tr(
|
|
748
|
+
"\u8BE5\u5355\u5143\u683C\u662F\u516C\u5F0F\u683C\uFF08\u5185\u5BB9\u7531\u516C\u5F0F\u91CD\u7B97\u7EF4\u62A4\uFF0C\u5F53\u524D\u503C\uFF1AcellFormula=${0}\uFF1B\u4E0D\u53EF\u76F4\u63A5\u586B\u5199\u3002\u5EFA\u8BAE\uFF1A\u6539\u5199\u5B83\u5F15\u7528\u7684\u53EF\u586B\u6570\u636E\u683C\uFF09",
|
|
749
|
+
[cell.attrs.cellFormula]
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
var DATE_INPUT_PATTERNS = [
|
|
755
|
+
/^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})$/,
|
|
756
|
+
/^(\d{4})年(\d{1,2})月(\d{1,2})日?$/,
|
|
757
|
+
/^(\d{4})(\d{2})(\d{2})$/
|
|
758
|
+
];
|
|
759
|
+
function normalizeEditorDateValue(input, dateFormat) {
|
|
760
|
+
const template = dateFormat || "YYYY-MM-DD";
|
|
761
|
+
for (const pattern of DATE_INPUT_PATTERNS) {
|
|
762
|
+
const match = input.match(pattern);
|
|
763
|
+
if (!match) continue;
|
|
764
|
+
const year = Number(match[1]);
|
|
765
|
+
const month = Number(match[2]);
|
|
766
|
+
const day = Number(match[3]);
|
|
767
|
+
const probe = new Date(year, month - 1, day);
|
|
768
|
+
if (probe.getFullYear() !== year || probe.getMonth() !== month - 1 || probe.getDate() !== day) return null;
|
|
769
|
+
const tokens = {
|
|
770
|
+
YYYY: String(year),
|
|
771
|
+
MM: String(month).padStart(2, "0"),
|
|
772
|
+
DD: String(day).padStart(2, "0"),
|
|
773
|
+
M: String(month),
|
|
774
|
+
D: String(day)
|
|
775
|
+
};
|
|
776
|
+
return template.replace(/YYYY|MM|DD|M|D/g, (token) => tokens[token]);
|
|
777
|
+
}
|
|
778
|
+
return null;
|
|
779
|
+
}
|
|
780
|
+
function createSlotApi(getEditor) {
|
|
781
|
+
return {
|
|
782
|
+
fillSlot: (target, value) => {
|
|
783
|
+
const editor = getEditor();
|
|
784
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
785
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u586B\u5199\u69FD\u4F4D") };
|
|
786
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
787
|
+
return { ok: false, error: tr("\u586B\u5199\u7684\u503C\u4E0D\u80FD\u4E3A\u7A7A\uFF08value\uFF1B\u5408\u6CD5\u503C\uFF1A\u975E\u7A7A\u6587\u672C/\u56FE\u7247\u5730\u5740\uFF09") };
|
|
788
|
+
}
|
|
789
|
+
if (target?.slotKey) {
|
|
790
|
+
const hits = [];
|
|
791
|
+
editor.state.doc.descendants((node, pos) => {
|
|
792
|
+
if ((node.type.name === "templateInlineSlot" || node.type.name === "templateImage") && node.attrs.slotKey === target.slotKey) {
|
|
793
|
+
hits.push({ pos, node });
|
|
794
|
+
}
|
|
795
|
+
return true;
|
|
796
|
+
});
|
|
797
|
+
if (hits.length === 0) {
|
|
798
|
+
return {
|
|
799
|
+
ok: false,
|
|
800
|
+
error: tr(
|
|
801
|
+
"\u672A\u627E\u5230 slotKey=${0} \u7684\u69FD\u4F4D\uFF08\u5F53\u524D\u503C\uFF1A\u65E0\u547D\u4E2D\uFF1B\u5408\u6CD5\u503C\uFF1A\u6587\u6863\u4E2D\u771F\u5B9E\u5B58\u5728\u7684\u69FD\u4F4D key\u3002\u5EFA\u8BAE\uFF1A\u5148\u8C03 idocs_doc_validate \u67E5\u770B\u672A\u586B\u69FD\u4F4D\u6E05\u5355\uFF09",
|
|
802
|
+
[target.slotKey]
|
|
803
|
+
)
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
const savedSelection = { from: editor.state.selection.from, to: editor.state.selection.to };
|
|
807
|
+
const docBefore = editor.state.doc;
|
|
808
|
+
try {
|
|
809
|
+
const transaction = editor.state.tr;
|
|
810
|
+
for (const hit of hits) {
|
|
811
|
+
const attrKey = hit.node.type.name === "templateImage" ? "src" : "value";
|
|
812
|
+
transaction.setNodeMarkup(hit.pos, void 0, { ...hit.node.attrs, [attrKey]: value });
|
|
813
|
+
}
|
|
814
|
+
editor.view.dispatch(transaction);
|
|
815
|
+
} catch (err) {
|
|
816
|
+
return { ok: false, error: tr("\u69FD\u4F4D\u5199\u5165\u4E8B\u52A1\u6784\u9020\u5931\u8D25\uFF1A${0}", [String(err)]) };
|
|
817
|
+
}
|
|
818
|
+
restoreSelection(editor, savedSelection);
|
|
819
|
+
if (editor.state.doc === docBefore) {
|
|
820
|
+
return {
|
|
821
|
+
ok: false,
|
|
822
|
+
error: tr("\u586B\u5199\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u8BE5\u69FD\u4F4D\u6539\u52A8\u88AB\u5224\u5B9A\u4E3A\u300C\u6539\u7ED3\u6784\u300D\u3002\u5EFA\u8BAE\uFF1A\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668\u8C03\u6574\u69FD\u4F4D\u5B9A\u4E49")
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
const first = hits[0].node.attrs;
|
|
826
|
+
const label = typeof first.slotLabel === "string" && first.slotLabel || target.slotKey;
|
|
827
|
+
const kindLabel = hits[0].node.type.name === "templateImage" ? tr("\u56FE\u7247\u69FD") : tr("\u884C\u5185\u69FD");
|
|
828
|
+
return { ok: true, label: tr("${0}\u300C${1}\u300D", [kindLabel, label]), value };
|
|
829
|
+
}
|
|
830
|
+
if (target?.blockId != null || target?.row != null || target?.col != null) {
|
|
831
|
+
if (!target?.blockId || !Number.isInteger(target?.row) || !Number.isInteger(target?.col) || (target?.row ?? -1) < 0 || (target?.col ?? -1) < 0) {
|
|
832
|
+
return {
|
|
833
|
+
ok: false,
|
|
834
|
+
error: tr(
|
|
835
|
+
"\u683C\u586B\u5199\u5B9A\u4F4D\u4E0D\u5B8C\u6574\uFF08\u5F53\u524D\u503C\uFF1AblockId=${0}, row=${1}, col=${2}\uFF1B\u5408\u6CD5\u503C\uFF1A\u4E09\u8005\u90FD\u4F20\uFF0Crow/col \u4E3A 0 \u8D77\u6574\u6570\uFF08row 0=\u8868\u5934\u884C\uFF09\u3002\u5EFA\u8BAE\uFF1AblockId \u53D6\u8868\u683C\u5757 blockId\uFF0C\u5750\u6807\u5148\u770B idocs_doc_get_content \u7684\u8868\u683C\u5185\u5BB9\uFF09",
|
|
836
|
+
[target?.blockId ?? tr("\u672A\u63D0\u4F9B"), String(target?.row), String(target?.col)]
|
|
837
|
+
)
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
const blockId = target.blockId;
|
|
841
|
+
const row = target.row;
|
|
842
|
+
const col = target.col;
|
|
843
|
+
const located = locateByBlockId(editor, blockId);
|
|
844
|
+
if (!located) {
|
|
845
|
+
return { ok: false, error: tr("\u672A\u627E\u5230 blockId=${0} \u7684\u6A21\u677F\u5757", [blockId]) };
|
|
846
|
+
}
|
|
847
|
+
let cellPos = -1;
|
|
848
|
+
let cellNode = null;
|
|
849
|
+
if (located.node.type.name === "table") {
|
|
850
|
+
const map = TableMap.get(located.node);
|
|
851
|
+
if (row >= map.height || col >= map.width) {
|
|
852
|
+
return {
|
|
853
|
+
ok: false,
|
|
854
|
+
error: tr(
|
|
855
|
+
"\u5750\u6807\u8D8A\u754C\uFF08\u5F53\u524D\u503C\uFF1Arow=${0}, col=${1}\uFF1B\u5408\u6CD5\u503C\uFF1Arow 0-${2}\u3001col 0-${3}\u3002\u8BE5\u8868\u683C\u5171 ${4} \u884C \xD7 ${5} \u5217\uFF09",
|
|
856
|
+
[
|
|
857
|
+
String(row),
|
|
858
|
+
String(col),
|
|
859
|
+
String(map.height - 1),
|
|
860
|
+
String(map.width - 1),
|
|
861
|
+
String(map.height),
|
|
862
|
+
String(map.width)
|
|
863
|
+
]
|
|
864
|
+
)
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
const rel = map.map[row * map.width + col];
|
|
868
|
+
cellPos = located.pos + 1 + rel;
|
|
869
|
+
cellNode = located.node.nodeAt(rel);
|
|
870
|
+
} else if (located.node.type.name === "fixedTable") {
|
|
871
|
+
const rowNode = row < located.node.childCount ? located.node.child(row) : null;
|
|
872
|
+
const cellInRow = rowNode && col < rowNode.childCount ? rowNode.child(col) : null;
|
|
873
|
+
if (!rowNode || !cellInRow) {
|
|
874
|
+
return {
|
|
875
|
+
ok: false,
|
|
876
|
+
error: tr(
|
|
877
|
+
"\u5750\u6807\u8D8A\u754C\uFF08\u5F53\u524D\u503C\uFF1Arow=${0}, col=${1}\uFF1B\u5408\u6CD5\u503C\uFF1A\u8BE5\u56FA\u5B9A\u8868\u683C\u5B9E\u9645\u884C\u5217\u8303\u56F4\u5185\u3002\u5EFA\u8BAE\uFF1A\u5148\u7528 idocs_doc_get_content \u8BFB\u8868\u683C\u5185\u5BB9\u786E\u8BA4\u5750\u6807\uFF09",
|
|
878
|
+
[String(row), String(col)]
|
|
879
|
+
)
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
if (cellInRow.attrs.covered === true) {
|
|
883
|
+
return {
|
|
884
|
+
ok: false,
|
|
885
|
+
error: tr("\u8BE5\u4F4D\u7F6E\u88AB\u4E0A\u65B9\u5408\u5E76\u683C\u8986\u76D6\uFF08covered \u54E8\u5175\u683C\uFF0C\u975E\u72EC\u7ACB\u5355\u5143\u683C\uFF1B\u5EFA\u8BAE\uFF1A\u76EE\u6807\u6539\u6307\u5408\u5E76\u4E3B\u683C\u6240\u5728\u5750\u6807\uFF09")
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
let rowOffset = 0;
|
|
889
|
+
for (let i = 0; i < row; i += 1) rowOffset += located.node.child(i).nodeSize;
|
|
890
|
+
let cellOffset = 0;
|
|
891
|
+
for (let i = 0; i < col; i += 1) cellOffset += rowNode.child(i).nodeSize;
|
|
892
|
+
cellNode = cellInRow;
|
|
893
|
+
cellPos = located.pos + 1 + rowOffset + 1 + cellOffset;
|
|
894
|
+
} else {
|
|
895
|
+
return {
|
|
896
|
+
ok: false,
|
|
897
|
+
error: tr(
|
|
898
|
+
"\u76EE\u6807\u5757\u4E0D\u662F\u8868\u683C\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1Atable \u6570\u636E\u8868\u683C\u5757\u6216 fixedTable \u56FA\u5B9A\u8868\u683C\u5757\u3002\u5EFA\u8BAE\uFF1A\u6838\u5BF9 blockId \u6307\u5411\u8868\u683C\u6A21\u677F\u5757\uFF09",
|
|
899
|
+
[located.node.type.name]
|
|
900
|
+
)
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
if (!cellNode) return { ok: false, error: tr("\u76EE\u6807\u5355\u5143\u683C\u5B9A\u4F4D\u5931\u8D25\uFF08\u6587\u6863\u521A\u88AB\u4FEE\u6539\uFF09\uFF0C\u8BF7\u91CD\u8BD5") };
|
|
904
|
+
const cellError = checkCellFillable(cellNode);
|
|
905
|
+
if (cellError) return { ok: false, error: cellError };
|
|
906
|
+
const control = resolveCellControl(editor.state.doc, cellNode, cellPos);
|
|
907
|
+
let textValue = value.replace(/[\r\n]+/g, " ").trim();
|
|
908
|
+
if (control?.type === "select") {
|
|
909
|
+
if (!control.options.includes(textValue)) {
|
|
910
|
+
return {
|
|
911
|
+
ok: false,
|
|
912
|
+
error: tr(
|
|
913
|
+
"\u8BE5\u683C\u662F\u4E0B\u62C9\u9009\u62E9\u683C\uFF0Cvalue \u987B\u4E3A\u9009\u9879\u539F\u6587\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1A${1}\u3002\u5EFA\u8BAE\uFF1A\u4ECE\u9009\u9879\u6E05\u5355\u4E2D\u539F\u6837\u9009\u4E00\u4E2A\u4F20\u5165\uFF09",
|
|
914
|
+
[
|
|
915
|
+
textValue,
|
|
916
|
+
control.options.length > 0 ? control.options.join("\u3001") : tr("\uFF08\u6A21\u677F\u672A\u914D\u7F6E\u9009\u9879\uFF0C\u65E0\u6CD5\u586B\u5199\uFF09")
|
|
917
|
+
]
|
|
918
|
+
)
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
} else if (control?.type === "date") {
|
|
922
|
+
const normalized = normalizeEditorDateValue(textValue, control.dateFormat);
|
|
923
|
+
if (!normalized) {
|
|
924
|
+
return {
|
|
925
|
+
ok: false,
|
|
926
|
+
error: tr(
|
|
927
|
+
"\u8BE5\u683C\u662F\u65E5\u671F\u683C\uFF0Cvalue \u65E0\u6CD5\u89E3\u6790\u4E3A\u65E5\u671F\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1A2026-01-31 / 2026/1/31 / 2026.1.31 / 2026\u5E741\u670831\u65E5 / 20260131 \u7B49\u5B8C\u6574\u5E74\u6708\u65E5\u5199\u6CD5\uFF0C\u5199\u5165\u65F6\u6309\u6A21\u677F\u683C\u5F0F ${1} \u5F52\u4E00\u3002\u5EFA\u8BAE\uFF1A\u6362\u7528\u4E0A\u8FF0\u5199\u6CD5\u91CD\u4F20\uFF09",
|
|
928
|
+
[textValue, control.dateFormat || "YYYY-MM-DD"]
|
|
929
|
+
)
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
textValue = normalized;
|
|
933
|
+
}
|
|
934
|
+
const savedSelection = { from: editor.state.selection.from, to: editor.state.selection.to };
|
|
935
|
+
const docBefore = editor.state.doc;
|
|
936
|
+
try {
|
|
937
|
+
const schema = editor.state.schema;
|
|
938
|
+
const paragraph = schema.nodes.paragraph.create(null, textValue ? schema.text(textValue) : null);
|
|
939
|
+
const transaction = editor.state.tr.replaceWith(cellPos + 1, cellPos + cellNode.nodeSize - 1, paragraph);
|
|
940
|
+
if (control) transaction.setMeta("templateCellInput", true);
|
|
941
|
+
editor.view.dispatch(transaction);
|
|
942
|
+
} catch (err) {
|
|
943
|
+
return { ok: false, error: tr("\u5355\u5143\u683C\u5199\u5165\u4E8B\u52A1\u6784\u9020\u5931\u8D25\uFF1A${0}", [String(err)]) };
|
|
944
|
+
}
|
|
945
|
+
restoreSelection(editor, savedSelection);
|
|
946
|
+
if (editor.state.doc === docBefore) {
|
|
947
|
+
return {
|
|
948
|
+
ok: false,
|
|
949
|
+
error: tr(
|
|
950
|
+
"\u586B\u5199\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u8BE5\u5355\u5143\u683C\u5185\u5BB9\u5C5E\u6A21\u677F\u9501\u5B9A\u90E8\u5206\uFF08\u6539\u5185\u5BB9=\u6539\u9AA8\u67B6\u88AB\u62D2\u3002\u5EFA\u8BAE\uFF1A\u6539\u586B fillable \u53EF\u586B\u683C\uFF0C\u6216\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668\u8C03\u6574\uFF09"
|
|
951
|
+
)
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
return {
|
|
955
|
+
ok: true,
|
|
956
|
+
label: tr("\u5757 ${0} \u5355\u5143\u683C(${1},${2})", [blockId, String(row), String(col)]),
|
|
957
|
+
value: textValue
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
return {
|
|
961
|
+
ok: false,
|
|
962
|
+
error: tr(
|
|
963
|
+
"target \u5F62\u6001\u4E0D\u5408\u6CD5\uFF08\u5F53\u524D\u503C\uFF1A\u65E2\u65E0 slotKey \u4E5F\u65E0 blockId+row+col\uFF1B\u5408\u6CD5\u503C\uFF1A{ slotKey }\uFF08\u884C\u5185\u69FD/\u56FE\u7247\u69FD\uFF09\u6216 { blockId, row, col }\uFF08\u53EF\u586B\u5355\u5143\u683C\uFF09\u4E8C\u9009\u4E00\uFF09"
|
|
964
|
+
)
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
function isTemplateTableEl(table) {
|
|
970
|
+
return !!table.closest("[data-template-protected-block]");
|
|
971
|
+
}
|
|
972
|
+
function useTableActions({
|
|
973
|
+
editor,
|
|
974
|
+
hoveredTableRef,
|
|
975
|
+
scheduleOverlayRefresh,
|
|
976
|
+
onTableDeleted
|
|
977
|
+
}) {
|
|
978
|
+
const getCellPos = useCallback(
|
|
979
|
+
(table, rowIndex, colIndex) => {
|
|
980
|
+
const row = table.rows[rowIndex];
|
|
981
|
+
if (!row) return null;
|
|
982
|
+
const cell = row.cells[colIndex];
|
|
983
|
+
if (!cell) return null;
|
|
984
|
+
try {
|
|
985
|
+
return editor.view.posAtDOM(cell, 0);
|
|
986
|
+
} catch {
|
|
987
|
+
return null;
|
|
988
|
+
}
|
|
989
|
+
},
|
|
990
|
+
[editor]
|
|
991
|
+
);
|
|
992
|
+
const handleToggleHeaderRow = useCallback(() => {
|
|
993
|
+
const table = hoveredTableRef.current;
|
|
994
|
+
if (!table) return;
|
|
995
|
+
const pos = getCellPos(table, 0, 0);
|
|
996
|
+
if (pos == null) return;
|
|
997
|
+
editor.chain().focus().setTextSelection(pos).toggleHeaderRow().run();
|
|
998
|
+
scheduleOverlayRefresh();
|
|
999
|
+
}, [editor, hoveredTableRef, getCellPos, scheduleOverlayRefresh]);
|
|
1000
|
+
const handleDeleteTable = useCallback(() => {
|
|
1001
|
+
const table = hoveredTableRef.current;
|
|
1002
|
+
if (!table) return;
|
|
1003
|
+
if (isTemplateTableEl(table)) return;
|
|
1004
|
+
const cellPos = getCellPos(table, 0, 0);
|
|
1005
|
+
if (cellPos == null) return;
|
|
1006
|
+
const $pos = editor.view.state.doc.resolve(cellPos);
|
|
1007
|
+
let deleteStart = -1;
|
|
1008
|
+
let deleteSize = 0;
|
|
1009
|
+
for (let depth = $pos.depth; depth > 0; depth--) {
|
|
1010
|
+
const node = $pos.node(depth);
|
|
1011
|
+
if (node.type.name === "tableWrapper" || node.type.name === "table") {
|
|
1012
|
+
deleteStart = $pos.before(depth);
|
|
1013
|
+
deleteSize = node.nodeSize;
|
|
1014
|
+
if (node.type.name === "tableWrapper") break;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
if (deleteStart < 0) return;
|
|
1018
|
+
const { state, view } = editor;
|
|
1019
|
+
const tr2 = state.tr.delete(deleteStart, deleteStart + deleteSize);
|
|
1020
|
+
view.dispatch(tr2);
|
|
1021
|
+
view.focus();
|
|
1022
|
+
onTableDeleted();
|
|
1023
|
+
}, [editor, hoveredTableRef, getCellPos, onTableDeleted]);
|
|
1024
|
+
const setAllColumnWidths = useCallback(
|
|
1025
|
+
(table, width) => {
|
|
1026
|
+
const cellPos = getCellPos(table, 0, 0);
|
|
1027
|
+
if (cellPos == null) return;
|
|
1028
|
+
const { state, view } = editor;
|
|
1029
|
+
const found = findAncestor(state.doc, cellPos, ["table"]);
|
|
1030
|
+
if (!found) return;
|
|
1031
|
+
const tableNode = found.node;
|
|
1032
|
+
const tableStart = found.pos + 1;
|
|
1033
|
+
const tr2 = state.tr;
|
|
1034
|
+
tableNode.descendants((node, relPos) => {
|
|
1035
|
+
if (node.type.name === "tableCell" || node.type.name === "tableHeader") {
|
|
1036
|
+
const colspan = node.attrs.colspan ?? 1;
|
|
1037
|
+
tr2.setNodeAttribute(tableStart + relPos, "colwidth", width == null ? null : Array(colspan).fill(width));
|
|
1038
|
+
return false;
|
|
1039
|
+
}
|
|
1040
|
+
return true;
|
|
1041
|
+
});
|
|
1042
|
+
if (tr2.docChanged) view.dispatch(tr2);
|
|
1043
|
+
view.focus();
|
|
1044
|
+
scheduleOverlayRefresh();
|
|
1045
|
+
},
|
|
1046
|
+
[editor, getCellPos, scheduleOverlayRefresh]
|
|
1047
|
+
);
|
|
1048
|
+
const handleEqualColumnWidth = useCallback(() => {
|
|
1049
|
+
const table = hoveredTableRef.current;
|
|
1050
|
+
if (!table) return;
|
|
1051
|
+
const firstRow = table.rows[0];
|
|
1052
|
+
if (!firstRow || firstRow.cells.length === 0) return;
|
|
1053
|
+
const tableWidth = table.getBoundingClientRect().width;
|
|
1054
|
+
let colCount = 0;
|
|
1055
|
+
for (let i = 0; i < firstRow.cells.length; i += 1) {
|
|
1056
|
+
colCount += firstRow.cells[i].colSpan || 1;
|
|
1057
|
+
}
|
|
1058
|
+
if (colCount === 0) return;
|
|
1059
|
+
const equalWidth = Math.max(50, Math.floor(tableWidth / colCount));
|
|
1060
|
+
setAllColumnWidths(table, equalWidth);
|
|
1061
|
+
}, [hoveredTableRef, setAllColumnWidths]);
|
|
1062
|
+
const handleAutoFitWidth = useCallback(() => {
|
|
1063
|
+
const table = hoveredTableRef.current;
|
|
1064
|
+
if (!table) return;
|
|
1065
|
+
setAllColumnWidths(table, null);
|
|
1066
|
+
}, [hoveredTableRef, setAllColumnWidths]);
|
|
1067
|
+
return { handleToggleHeaderRow, handleDeleteTable, handleEqualColumnWidth, handleAutoFitWidth };
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
// src/editor/plugins/builtin/table/useTableRowColActions.ts
|
|
1071
|
+
function canDeleteDataRow(state) {
|
|
1072
|
+
try {
|
|
1073
|
+
const rect = selectedRect(state);
|
|
1074
|
+
if (rect.top <= 0) return false;
|
|
1075
|
+
for (let rowIndex = rect.top; rowIndex < rect.bottom; rowIndex += 1) {
|
|
1076
|
+
if (rect.table.child(rowIndex).attrs.templateRowFixed === true) return false;
|
|
1077
|
+
}
|
|
1078
|
+
return true;
|
|
1079
|
+
} catch {
|
|
1080
|
+
return false;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
function markDataRowsFillable(editor, anchorPos) {
|
|
1084
|
+
let chain = editor.chain().focus();
|
|
1085
|
+
if (anchorPos != null) chain = chain.setTextSelection(anchorPos);
|
|
1086
|
+
chain.addRowAfter().command(({ tr: tr2 }) => {
|
|
1087
|
+
const $from = tr2.doc.resolve(tr2.selection.from);
|
|
1088
|
+
for (let depth = $from.depth; depth >= 1; depth -= 1) {
|
|
1089
|
+
const table = $from.node(depth);
|
|
1090
|
+
if (table.type.name !== "table" || table.attrs.templateTableMode !== "data") continue;
|
|
1091
|
+
const tableStart = $from.start(depth);
|
|
1092
|
+
table.forEach((row, rowOffset, rowIndex) => {
|
|
1093
|
+
if (rowIndex === 0 || row.attrs.templateRowFixed === true) return;
|
|
1094
|
+
row.forEach((cell, cellOffset) => {
|
|
1095
|
+
if (cell.type.name !== "tableCell" && cell.type.name !== "tableHeader") return;
|
|
1096
|
+
if (cell.attrs.fillable === true && cell.attrs.required !== true && cell.attrs.templateCellFixed !== true && cell.attrs.templateCellFixedLocal !== true) {
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
tr2.setNodeMarkup(tableStart + rowOffset + 1 + cellOffset, void 0, {
|
|
1100
|
+
...cell.attrs,
|
|
1101
|
+
fillable: true,
|
|
1102
|
+
required: false,
|
|
1103
|
+
templateCellFixed: false,
|
|
1104
|
+
templateCellFixedLocal: false
|
|
1105
|
+
});
|
|
1106
|
+
});
|
|
1107
|
+
});
|
|
1108
|
+
return true;
|
|
1109
|
+
}
|
|
1110
|
+
return true;
|
|
1111
|
+
}).run();
|
|
1112
|
+
}
|
|
1113
|
+
function useTableRowColActions({
|
|
1114
|
+
editor,
|
|
1115
|
+
hoveredTableRef,
|
|
1116
|
+
hoverCellPos,
|
|
1117
|
+
scheduleOverlayRefresh
|
|
1118
|
+
}) {
|
|
1119
|
+
const state = editor.state;
|
|
1120
|
+
const tableEl = hoveredTableRef.current;
|
|
1121
|
+
let selectionInTable = false;
|
|
1122
|
+
let mode = null;
|
|
1123
|
+
if (tableEl) {
|
|
1124
|
+
try {
|
|
1125
|
+
const hoverTableInnerPos = editor.view.posAtDOM(tableEl, 0);
|
|
1126
|
+
const found = findAncestor(state.doc, hoverTableInnerPos, ["table"]);
|
|
1127
|
+
if (found) {
|
|
1128
|
+
const tableMode = found.node.attrs.templateTableMode;
|
|
1129
|
+
mode = tableMode === "data" || tableMode === "form" ? tableMode : null;
|
|
1130
|
+
selectionInTable = state.selection.from > found.pos && state.selection.to < found.pos + found.node.nodeSize;
|
|
1131
|
+
}
|
|
1132
|
+
} catch {
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
const inProtected = !!tableEl && isTemplateTableEl(tableEl);
|
|
1136
|
+
const hoverAnchored = !selectionInTable && hoverCellPos != null;
|
|
1137
|
+
const hasAnchor = selectionInTable || hoverAnchored;
|
|
1138
|
+
let guardState = state;
|
|
1139
|
+
if (hoverAnchored && hoverCellPos != null) {
|
|
1140
|
+
try {
|
|
1141
|
+
guardState = state.apply(state.tr.setSelection(Selection.near(state.doc.resolve(hoverCellPos), 1)));
|
|
1142
|
+
} catch {
|
|
1143
|
+
guardState = state;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
const context = { hasAnchor, selectionInTable, mode, inProtected, guardState };
|
|
1147
|
+
const can = mode === "data" ? {
|
|
1148
|
+
addRowAbove: false,
|
|
1149
|
+
// 数据表格新增数据行是核心诉求,但仍须有锚点(悬在表格边框等空白处时禁用)
|
|
1150
|
+
addRowBelow: hasAnchor,
|
|
1151
|
+
// 表头行/固定行不可删(hover 锚点时按 hover 单元格所在行求值)
|
|
1152
|
+
deleteRow: canDeleteDataRow(guardState),
|
|
1153
|
+
addColLeft: false,
|
|
1154
|
+
addColRight: false,
|
|
1155
|
+
deleteColumn: false,
|
|
1156
|
+
merge: false,
|
|
1157
|
+
split: false
|
|
1158
|
+
} : {
|
|
1159
|
+
// 普通表格增删行/列无门控,有锚点即可
|
|
1160
|
+
addRowAbove: hasAnchor,
|
|
1161
|
+
addRowBelow: hasAnchor,
|
|
1162
|
+
deleteRow: hasAnchor,
|
|
1163
|
+
addColLeft: hasAnchor,
|
|
1164
|
+
addColRight: hasAnchor,
|
|
1165
|
+
deleteColumn: hasAnchor,
|
|
1166
|
+
// 合并/拆分依赖真实选区(CellSelection 框选),hover 锚点不可用
|
|
1167
|
+
merge: selectionInTable && editor.can().mergeCells(),
|
|
1168
|
+
split: selectionInTable && editor.can().splitCell()
|
|
1169
|
+
};
|
|
1170
|
+
const runWithAnchor = useCallback(
|
|
1171
|
+
(attach) => {
|
|
1172
|
+
let chain = editor.chain().focus();
|
|
1173
|
+
if (!context.selectionInTable && hoverCellPos != null) {
|
|
1174
|
+
chain = chain.setTextSelection(hoverCellPos);
|
|
1175
|
+
}
|
|
1176
|
+
attach(chain).run();
|
|
1177
|
+
scheduleOverlayRefresh();
|
|
1178
|
+
},
|
|
1179
|
+
[editor, context, hoverCellPos, scheduleOverlayRefresh]
|
|
1180
|
+
);
|
|
1181
|
+
const handleAddRowBefore = useCallback(() => {
|
|
1182
|
+
if (!context.hasAnchor || context.mode === "data" || context.inProtected) return;
|
|
1183
|
+
runWithAnchor((chain) => chain.addRowBefore());
|
|
1184
|
+
}, [context, runWithAnchor]);
|
|
1185
|
+
const handleAddRowAfter = useCallback(() => {
|
|
1186
|
+
if (!context.hasAnchor) return;
|
|
1187
|
+
if (context.mode === "data") {
|
|
1188
|
+
const anchorPos = context.selectionInTable ? void 0 : hoverCellPos ?? void 0;
|
|
1189
|
+
markDataRowsFillable(editor, anchorPos);
|
|
1190
|
+
scheduleOverlayRefresh();
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
if (context.inProtected) return;
|
|
1194
|
+
runWithAnchor((chain) => chain.addRowAfter());
|
|
1195
|
+
}, [editor, context, hoverCellPos, runWithAnchor, scheduleOverlayRefresh]);
|
|
1196
|
+
const handleDeleteRow = useCallback(() => {
|
|
1197
|
+
if (!context.hasAnchor) return;
|
|
1198
|
+
if (context.mode === "data" && !canDeleteDataRow(context.guardState)) return;
|
|
1199
|
+
if (context.mode !== "data" && context.inProtected) return;
|
|
1200
|
+
runWithAnchor((chain) => chain.deleteRow());
|
|
1201
|
+
}, [context, runWithAnchor]);
|
|
1202
|
+
const handleAddColumnBefore = useCallback(() => {
|
|
1203
|
+
if (!context.hasAnchor || context.mode === "data" || context.inProtected) return;
|
|
1204
|
+
runWithAnchor((chain) => chain.addColumnBefore());
|
|
1205
|
+
}, [context, runWithAnchor]);
|
|
1206
|
+
const handleAddColumnAfter = useCallback(() => {
|
|
1207
|
+
if (!context.hasAnchor || context.mode === "data" || context.inProtected) return;
|
|
1208
|
+
runWithAnchor((chain) => chain.addColumnAfter());
|
|
1209
|
+
}, [context, runWithAnchor]);
|
|
1210
|
+
const handleDeleteColumn = useCallback(() => {
|
|
1211
|
+
if (!context.hasAnchor || context.mode === "data" || context.inProtected) return;
|
|
1212
|
+
runWithAnchor((chain) => chain.deleteColumn());
|
|
1213
|
+
}, [context, runWithAnchor]);
|
|
1214
|
+
const handleMergeCells = useCallback(() => {
|
|
1215
|
+
if (!context.selectionInTable) return;
|
|
1216
|
+
editor.chain().focus().mergeCells().run();
|
|
1217
|
+
scheduleOverlayRefresh();
|
|
1218
|
+
}, [editor, context, scheduleOverlayRefresh]);
|
|
1219
|
+
const handleSplitCells = useCallback(() => {
|
|
1220
|
+
if (!context.selectionInTable) return;
|
|
1221
|
+
editor.chain().focus().splitCell().run();
|
|
1222
|
+
scheduleOverlayRefresh();
|
|
1223
|
+
}, [editor, context, scheduleOverlayRefresh]);
|
|
1224
|
+
return {
|
|
1225
|
+
context,
|
|
1226
|
+
can,
|
|
1227
|
+
handleAddRowBefore,
|
|
1228
|
+
handleAddRowAfter,
|
|
1229
|
+
handleDeleteRow,
|
|
1230
|
+
handleAddColumnBefore,
|
|
1231
|
+
handleAddColumnAfter,
|
|
1232
|
+
handleDeleteColumn,
|
|
1233
|
+
handleMergeCells,
|
|
1234
|
+
handleSplitCells
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
// src/editor/core/refApi/tables.ts
|
|
1239
|
+
function createTableOpsApi(getEditor, getAiCommands) {
|
|
1240
|
+
return {
|
|
1241
|
+
tableOps: (blockId, op, coords) => {
|
|
1242
|
+
const editor = getEditor();
|
|
1243
|
+
if (!editor || editor.isDestroyed) return { ok: false, error: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
1244
|
+
if (!editor.isEditable) return { ok: false, error: tr("\u7F16\u8F91\u5668\u5F53\u524D\u4E3A\u53EA\u8BFB\uFF0C\u65E0\u6CD5\u64CD\u4F5C\u8868\u683C") };
|
|
1245
|
+
const tableOpPrefix = "table.";
|
|
1246
|
+
const legalOps = getAiCommands().filter((command) => command.kind === "op" && command.id.startsWith(tableOpPrefix)).map((command) => command.id.slice(tableOpPrefix.length));
|
|
1247
|
+
if (legalOps.length === 0) {
|
|
1248
|
+
return { ok: false, error: tr("table \u63D2\u4EF6\u672A\u542F\u7528\u6216\u672A\u767B\u8BB0\u7ED3\u6784\u64CD\u4F5C\u80FD\u529B\uFF0C\u65E0\u6CD5\u6267\u884C\u8868\u683C\u7ED3\u6784\u64CD\u4F5C") };
|
|
1249
|
+
}
|
|
1250
|
+
if (typeof op !== "string" || !legalOps.includes(op)) {
|
|
1251
|
+
return {
|
|
1252
|
+
ok: false,
|
|
1253
|
+
error: tr(
|
|
1254
|
+
"\u672A\u77E5\u7684\u8868\u683C\u64CD\u4F5C op\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1A${1}\u3002\u5EFA\u8BAE\uFF1A\u5148\u8C03 idocs_doc_command_schema \u67E5\u8BE2 table \u63D2\u4EF6\u6761\u76EE\uFF09",
|
|
1255
|
+
[String(op), legalOps.join("\u3001")]
|
|
1256
|
+
)
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
const located = locateBlockNode(editor, blockId);
|
|
1260
|
+
if (!located) {
|
|
1261
|
+
return {
|
|
1262
|
+
ok: false,
|
|
1263
|
+
error: blockId ? tr("\u672A\u627E\u5230 blockId=${0} \u7684\u6A21\u677F\u5757", [blockId]) : tr("\u672A\u6307\u5B9A blockId\uFF0C\u4E14\u5F53\u524D\u5149\u6807\u4E0D\u5728\u4EFB\u4F55\u8868\u683C\u5185\uFF08\u5EFA\u8BAE\uFF1A\u4F20\u8868\u683C\u5757 blockId\uFF0C\u6216\u8BA9\u7528\u6237\u628A\u5149\u6807\u79FB\u8FDB\u8868\u683C\uFF09")
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
const typeName = located.node.type.name;
|
|
1267
|
+
if (typeName === "fixedTable") {
|
|
1268
|
+
return {
|
|
1269
|
+
ok: false,
|
|
1270
|
+
error: tr(
|
|
1271
|
+
"\u56FA\u5B9A\u8868\u683C\u4E0D\u652F\u6301\u7ED3\u6784\u64CD\u4F5C\uFF08\u5F53\u524D\u503C\uFF1AfixedTable\uFF1B\u56FA\u5B9A\u8868\u683C\u7684\u884C\u5217/\u5408\u5E76\u5C5E\u6A21\u677F\u8BBE\u8BA1\u671F\u914D\u7F6E\uFF0C\u7F16\u8F91\u5668\u4FA7\u4E0D\u53EF\u589E\u5220\u884C\u5217\u3002\u5EFA\u8BAE\uFF1A\u53EF\u586B\u5185\u5BB9\u8D70 idocs_doc_slot_update\uFF0C\u5355\u5143\u683C\u8868\u73B0\u5C5E\u6027\u8D70 idocs_doc_block_update\uFF0C\u7ED3\u6784\u8C03\u6574\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668\uFF09"
|
|
1272
|
+
)
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
if (typeName !== "table") {
|
|
1276
|
+
return {
|
|
1277
|
+
ok: false,
|
|
1278
|
+
error: tr(
|
|
1279
|
+
"\u76EE\u6807\u5757\u4E0D\u662F\u8868\u683C\uFF08\u5F53\u524D\u503C\uFF1A${0}\uFF1B\u5408\u6CD5\u503C\uFF1Atable \u6570\u636E\u8868\u683C\u5757\u3002\u5EFA\u8BAE\uFF1AblockId \u4F20\u8868\u683C\u5757\u7684 blockId\uFF0C\u6216\u4E0D\u4F20\u8BA9\u5149\u6807\u843D\u5728\u8868\u683C\u5185\uFF09",
|
|
1280
|
+
[typeName]
|
|
1281
|
+
)
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
const tableNode = located.node;
|
|
1285
|
+
const map = TableMap.get(tableNode);
|
|
1286
|
+
const dataMode = tableNode.attrs.templateTableMode === "data";
|
|
1287
|
+
const dimsLabel = tr("${0} \u884C \xD7 ${1} \u5217", [String(map.height), String(map.width)]);
|
|
1288
|
+
const coordOrError = (value, name, max, fallback) => {
|
|
1289
|
+
if (value == null) return { value: fallback };
|
|
1290
|
+
if (!Number.isInteger(value) || value < 0 || value >= max) {
|
|
1291
|
+
return {
|
|
1292
|
+
error: tr(
|
|
1293
|
+
"\u5750\u6807 ${0} \u975E\u6CD5\uFF08\u5F53\u524D\u503C\uFF1A${1}\uFF1B\u5408\u6CD5\u503C\uFF1A0 \u5230 ${2} \u7684\u6574\u6570\uFF08\u8BE5\u8868\u683C\u5171 ${3}\uFF09\u3002\u5EFA\u8BAE\uFF1A\u5148\u7528 idocs_doc_get_content \u8BFB\u8868\u683C\u5185\u5BB9\u786E\u8BA4\u884C\u5217\uFF09",
|
|
1294
|
+
[name, String(value), String(max - 1), dimsLabel]
|
|
1295
|
+
)
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
return { value };
|
|
1299
|
+
};
|
|
1300
|
+
const requiredCoordError = (name) => coords?.[name] == null ? tr("\u7F3A\u5C11\u5750\u6807 ${0}\uFF08\u5F53\u524D\u503C\uFF1A\u672A\u63D0\u4F9B\uFF1B\u5408\u6CD5\u503C\uFF1A0 \u8D77\u6574\u6570\u3002\u5EFA\u8BAE\uFF1A${1} \u64CD\u4F5C\u5FC5\u987B\u663E\u5F0F\u7ED9\u51FA ${0}\uFF09", [name, op]) : null;
|
|
1301
|
+
const cellPosAt = (row, col) => located.pos + 1 + map.map[row * map.width + col];
|
|
1302
|
+
const anchorChain = (row, col) => cellPosAt(row, col) + 1;
|
|
1303
|
+
const savedSelection = { from: editor.state.selection.from, to: editor.state.selection.to };
|
|
1304
|
+
const docBefore = editor.state.doc;
|
|
1305
|
+
let ran = false;
|
|
1306
|
+
switch (op) {
|
|
1307
|
+
case "addRow": {
|
|
1308
|
+
const rowResult = coordOrError(coords?.row, "row", map.height, map.height - 1);
|
|
1309
|
+
if ("error" in rowResult) return { ok: false, error: rowResult.error };
|
|
1310
|
+
if (dataMode) {
|
|
1311
|
+
markDataRowsFillable(editor, anchorChain(rowResult.value, 0));
|
|
1312
|
+
ran = true;
|
|
1313
|
+
} else {
|
|
1314
|
+
ran = editor.chain().setTextSelection(anchorChain(rowResult.value, 0)).addRowAfter().run();
|
|
1315
|
+
}
|
|
1316
|
+
break;
|
|
1317
|
+
}
|
|
1318
|
+
case "addCol": {
|
|
1319
|
+
const colResult = coordOrError(coords?.col, "col", map.width, map.width - 1);
|
|
1320
|
+
if ("error" in colResult) return { ok: false, error: colResult.error };
|
|
1321
|
+
ran = editor.chain().setTextSelection(anchorChain(0, colResult.value)).addColumnAfter().run();
|
|
1322
|
+
break;
|
|
1323
|
+
}
|
|
1324
|
+
case "deleteRow": {
|
|
1325
|
+
const missing = requiredCoordError("row");
|
|
1326
|
+
if (missing) return { ok: false, error: missing };
|
|
1327
|
+
const rowResult = coordOrError(coords?.row, "row", map.height, -1);
|
|
1328
|
+
if ("error" in rowResult) return { ok: false, error: rowResult.error };
|
|
1329
|
+
if (dataMode && rowResult.value === 0) {
|
|
1330
|
+
return {
|
|
1331
|
+
ok: false,
|
|
1332
|
+
error: tr("\u8868\u5934\u884C\u4E0D\u53EF\u5220\uFF08\u5F53\u524D\u503C\uFF1Arow=0\uFF1B\u5408\u6CD5\u503C\uFF1A1 \u5230 ${0}\uFF08\u6570\u636E\u8868\u683C\u7684\u8868\u5934\u884C\u59CB\u7EC8\u4FDD\u7559\uFF09\uFF09", [
|
|
1333
|
+
String(map.height - 1)
|
|
1334
|
+
])
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
ran = editor.chain().setTextSelection(anchorChain(rowResult.value, 0)).deleteRow().run();
|
|
1338
|
+
break;
|
|
1339
|
+
}
|
|
1340
|
+
case "deleteCol": {
|
|
1341
|
+
const missing = requiredCoordError("col");
|
|
1342
|
+
if (missing) return { ok: false, error: missing };
|
|
1343
|
+
const colResult = coordOrError(coords?.col, "col", map.width, -1);
|
|
1344
|
+
if ("error" in colResult) return { ok: false, error: colResult.error };
|
|
1345
|
+
ran = editor.chain().setTextSelection(anchorChain(0, colResult.value)).deleteColumn().run();
|
|
1346
|
+
break;
|
|
1347
|
+
}
|
|
1348
|
+
case "mergeCells": {
|
|
1349
|
+
for (const name of ["row", "col", "toRow", "toCol"]) {
|
|
1350
|
+
const missing = requiredCoordError(name);
|
|
1351
|
+
if (missing) return { ok: false, error: missing };
|
|
1352
|
+
}
|
|
1353
|
+
const rowA = coordOrError(coords?.row, "row", map.height, -1);
|
|
1354
|
+
if ("error" in rowA) return { ok: false, error: rowA.error };
|
|
1355
|
+
const colA = coordOrError(coords?.col, "col", map.width, -1);
|
|
1356
|
+
if ("error" in colA) return { ok: false, error: colA.error };
|
|
1357
|
+
const rowB = coordOrError(coords?.toRow, "toRow", map.height, -1);
|
|
1358
|
+
if ("error" in rowB) return { ok: false, error: rowB.error };
|
|
1359
|
+
const colB = coordOrError(coords?.toCol, "toCol", map.width, -1);
|
|
1360
|
+
if ("error" in colB) return { ok: false, error: colB.error };
|
|
1361
|
+
const fromRow = Math.min(rowA.value, rowB.value);
|
|
1362
|
+
const toRow = Math.max(rowA.value, rowB.value);
|
|
1363
|
+
const fromCol = Math.min(colA.value, colB.value);
|
|
1364
|
+
const toCol = Math.max(colA.value, colB.value);
|
|
1365
|
+
try {
|
|
1366
|
+
editor.view.dispatch(
|
|
1367
|
+
editor.state.tr.setSelection(
|
|
1368
|
+
CellSelection.create(editor.state.doc, cellPosAt(fromRow, fromCol), cellPosAt(toRow, toCol))
|
|
1369
|
+
)
|
|
1370
|
+
);
|
|
1371
|
+
} catch {
|
|
1372
|
+
return {
|
|
1373
|
+
ok: false,
|
|
1374
|
+
error: tr("\u65E0\u6CD5\u6784\u9020\u5355\u5143\u683C\u9009\u533A\uFF08\u5750\u6807 (${0},${1})-(${2},${3}) \u4E0D\u6784\u6210\u53EF\u6846\u9009\u533A\u57DF\uFF09", [
|
|
1375
|
+
String(fromRow),
|
|
1376
|
+
String(fromCol),
|
|
1377
|
+
String(toRow),
|
|
1378
|
+
String(toCol)
|
|
1379
|
+
])
|
|
1380
|
+
};
|
|
1381
|
+
}
|
|
1382
|
+
ran = editor.commands.mergeCells();
|
|
1383
|
+
break;
|
|
1384
|
+
}
|
|
1385
|
+
case "splitCell": {
|
|
1386
|
+
for (const name of ["row", "col"]) {
|
|
1387
|
+
const missing = requiredCoordError(name);
|
|
1388
|
+
if (missing) return { ok: false, error: missing };
|
|
1389
|
+
}
|
|
1390
|
+
const rowResult = coordOrError(coords?.row, "row", map.height, -1);
|
|
1391
|
+
if ("error" in rowResult) return { ok: false, error: rowResult.error };
|
|
1392
|
+
const colResult = coordOrError(coords?.col, "col", map.width, -1);
|
|
1393
|
+
if ("error" in colResult) return { ok: false, error: colResult.error };
|
|
1394
|
+
try {
|
|
1395
|
+
editor.view.dispatch(
|
|
1396
|
+
editor.state.tr.setSelection(
|
|
1397
|
+
CellSelection.create(editor.state.doc, cellPosAt(rowResult.value, colResult.value))
|
|
1398
|
+
)
|
|
1399
|
+
);
|
|
1400
|
+
} catch {
|
|
1401
|
+
return {
|
|
1402
|
+
ok: false,
|
|
1403
|
+
error: tr("\u65E0\u6CD5\u6784\u9020\u5355\u5143\u683C\u9009\u533A\uFF08\u5750\u6807 (${0},${1}) \u4E0D\u662F\u53EF\u5B9A\u4F4D\u7684\u5355\u5143\u683C\uFF09", [
|
|
1404
|
+
String(rowResult.value),
|
|
1405
|
+
String(colResult.value)
|
|
1406
|
+
])
|
|
1407
|
+
};
|
|
1408
|
+
}
|
|
1409
|
+
ran = editor.commands.splitCell();
|
|
1410
|
+
break;
|
|
1411
|
+
}
|
|
1412
|
+
case "toggleHeaderRow": {
|
|
1413
|
+
ran = editor.chain().setTextSelection(anchorChain(0, 0)).toggleHeaderRow().run();
|
|
1414
|
+
break;
|
|
1415
|
+
}
|
|
1416
|
+
default:
|
|
1417
|
+
return { ok: false, error: tr("\u64CD\u4F5C ${0} \u5DF2\u5728\u80FD\u529B\u76EE\u5F55\u767B\u8BB0\uFF0C\u4F46\u7F16\u8F91\u5668\u5C1A\u672A\u63D0\u4F9B\u6267\u884C\u901A\u9053", [op]) };
|
|
1418
|
+
}
|
|
1419
|
+
restoreSelection(editor, savedSelection);
|
|
1420
|
+
if (!ran) {
|
|
1421
|
+
return {
|
|
1422
|
+
ok: false,
|
|
1423
|
+
error: tr(
|
|
1424
|
+
"\u8868\u683C\u64CD\u4F5C\u547D\u4EE4\u672A\u88AB\u63A5\u53D7\uFF1A${0} \u5728\u5F53\u524D\u8868\u683C\u72B6\u6001\u4E0D\u53EF\u6267\u884C\uFF08\u5982\u5408\u5E76\u8981\u6C42\u77E9\u5F62\u533A\u57DF\u3001\u76EE\u6807\u884C/\u5217\u4E0D\u5B58\u5728\u7B49\uFF09\u3002\u5EFA\u8BAE\uFF1A\u6838\u5BF9\u5750\u6807\u540E\u91CD\u8BD5",
|
|
1425
|
+
[op]
|
|
1426
|
+
)
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
if (editor.state.doc === docBefore) {
|
|
1430
|
+
return {
|
|
1431
|
+
ok: false,
|
|
1432
|
+
error: tr(
|
|
1433
|
+
"\u8868\u683C\u64CD\u4F5C\u88AB\u6A21\u677F\u9501\u5B9A\u95E8\u63A7\u62D2\u7EDD\uFF1A\u8BE5\u8868\u7ED3\u6784\u5C5E\u6A21\u677F\u9501\u5B9A\u533A\uFF08\u9501\u5B9A\u884C/\u5217\u6216\u9501\u5B9A\u8868\u683C\u7684\u7ED3\u6784\u6539\u52A8\u4F1A\u88AB\u62D2\u3002\u6570\u636E\u8868\u683C\u4EC5\u652F\u6301\u6570\u636E\u884C\u589E\u5220\uFF1B\u81EA\u7531\u533A\u666E\u901A\u8868\u683C\u5168\u91CF\u53EF\u7528\u3002\u5EFA\u8BAE\uFF1A\u786E\u9700\u8C03\u6574\u9501\u5B9A\u7ED3\u6784\u8BF7\u56DE\u5230\u6A21\u677F\u8BBE\u8BA1\u5668\uFF09"
|
|
1434
|
+
)
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
let detail;
|
|
1438
|
+
try {
|
|
1439
|
+
const doc = editor.state.doc;
|
|
1440
|
+
const $pos = doc.resolve(Math.max(0, Math.min(located.pos, doc.content.size)));
|
|
1441
|
+
for (let depth = $pos.depth; depth >= 1; depth -= 1) {
|
|
1442
|
+
const node = $pos.node(depth);
|
|
1443
|
+
if (node.type.name === "table") {
|
|
1444
|
+
const after = TableMap.get(node);
|
|
1445
|
+
detail = tr("${0} \u884C \xD7 ${1} \u5217", [String(after.height), String(after.width)]);
|
|
1446
|
+
break;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
} catch {
|
|
1450
|
+
detail = void 0;
|
|
1451
|
+
}
|
|
1452
|
+
return { ok: true, detail };
|
|
1453
|
+
}
|
|
1454
|
+
};
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
// src/editor/core/hooks/useEditorRefApi.ts
|
|
1458
|
+
function useEditorRefApi(options) {
|
|
1459
|
+
const { editor, ydoc, outline, registry } = options;
|
|
1460
|
+
const getController = useCallback(() => {
|
|
1461
|
+
if (!editor) return void 0;
|
|
1462
|
+
const storage = editor.storage.templateStorage;
|
|
1463
|
+
return storage?.controller;
|
|
1464
|
+
}, [editor]);
|
|
1465
|
+
const getAiCommands = useCallback(() => registry?.buildAiCommands() ?? [], [registry]);
|
|
1466
|
+
const runGuardedContentCommand = (exec, violationMessage) => {
|
|
1467
|
+
if (!editor) return { ok: false, code: "NOT_READY", message: tr("\u7F16\u8F91\u5668\u672A\u5C31\u7EEA") };
|
|
1468
|
+
const controller = getController();
|
|
1469
|
+
if (controller) {
|
|
1470
|
+
const guard = controller.canReplaceWithPlainContent();
|
|
1471
|
+
if (!guard.ok) return guard;
|
|
1472
|
+
}
|
|
1473
|
+
return exec(editor) ? { ok: true, value: void 0 } : { ok: false, code: "INTEGRITY_VIOLATION", message: violationMessage };
|
|
1474
|
+
};
|
|
1475
|
+
return useMemo(
|
|
1476
|
+
() => ({
|
|
1477
|
+
getHTML: () => editor?.getHTML() || "",
|
|
1478
|
+
setContent: (html) => runGuardedContentCommand(
|
|
1479
|
+
(ed) => ed.commands.setContent(stripTemplateDataAttributes(sanitizeHtml(html))),
|
|
1480
|
+
tr("\u5185\u5BB9\u66FF\u6362\u4E8B\u52A1\u672A\u88AB\u63A5\u53D7")
|
|
1481
|
+
),
|
|
1482
|
+
getJSON: () => editor?.getJSON() ?? { type: "doc", content: [] },
|
|
1483
|
+
getText: () => editor?.getText() || "",
|
|
1484
|
+
getEditorElement: () => editor?.view?.dom || null,
|
|
1485
|
+
editor,
|
|
1486
|
+
getYDoc: () => ydoc,
|
|
1487
|
+
focus: () => editor?.commands.focus(),
|
|
1488
|
+
setEditable: (value) => {
|
|
1489
|
+
editor?.setEditable(value);
|
|
1490
|
+
},
|
|
1491
|
+
clear: () => runGuardedContentCommand((ed) => ed.commands.clearContent(), tr("\u6E05\u7A7A\u4E8B\u52A1\u672A\u88AB\u63A5\u53D7")),
|
|
1492
|
+
...createTemplateApi(getController),
|
|
1493
|
+
...createBlockAttrsApi(() => editor),
|
|
1494
|
+
...createBlockOpsApi(() => editor),
|
|
1495
|
+
...createMarksApi(() => editor, getAiCommands),
|
|
1496
|
+
...createSlotApi(() => editor),
|
|
1497
|
+
...createTableOpsApi(() => editor, getAiCommands),
|
|
1498
|
+
getAiCommands: () => registry?.buildAiCommands() ?? [],
|
|
1499
|
+
getOutlineItems: () => {
|
|
1500
|
+
if (!editor) return [];
|
|
1501
|
+
return extractHeadings(editor, outline?.panel);
|
|
1502
|
+
}
|
|
1503
|
+
}),
|
|
1504
|
+
[editor, ydoc, getController, getAiCommands, outline?.panel, registry]
|
|
1505
|
+
);
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
export { OutlinePanel, extractHeadings, isTemplateTableEl, locateBlockNode, useEditorRefApi, useOutline, useTableActions, useTableRowColActions };
|
|
1509
|
+
//# sourceMappingURL=chunk-BBZ5ZBEK.mjs.map
|
|
1510
|
+
//# sourceMappingURL=chunk-BBZ5ZBEK.mjs.map
|