@kuzuma/colwork 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +186 -2
- package/dist/colwork.css +1 -0
- package/dist/colwork.js +1341 -0
- package/dist/colwork.umd.cjs +8 -0
- package/dist/index.d.ts +243 -0
- package/package.json +79 -4
package/dist/colwork.js
ADDED
|
@@ -0,0 +1,1341 @@
|
|
|
1
|
+
var ct = Object.defineProperty;
|
|
2
|
+
var ht = (a, t, e) => t in a ? ct(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
|
|
3
|
+
var h = (a, t, e) => ht(a, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import * as K from "yjs";
|
|
5
|
+
import { Awareness as dt } from "y-protocols/awareness";
|
|
6
|
+
import { WebrtcProvider as ut } from "y-webrtc";
|
|
7
|
+
import { WebsocketProvider as tt } from "y-websocket";
|
|
8
|
+
const Y = 6e5, G = (a) => Array.from(a, (t) => t.toString(16).padStart(2, "0")).join("");
|
|
9
|
+
async function it(a, t) {
|
|
10
|
+
var s;
|
|
11
|
+
if (!((s = globalThis.crypto) != null && s.subtle)) throw new Error("密码锁定需要 HTTPS 或 localhost 环境");
|
|
12
|
+
const e = await crypto.subtle.importKey("raw", new TextEncoder().encode(a), "PBKDF2", !1, ["deriveBits"]);
|
|
13
|
+
return new Uint8Array(await crypto.subtle.deriveBits({ name: "PBKDF2", hash: "SHA-256", salt: t, iterations: Y }, e, 256));
|
|
14
|
+
}
|
|
15
|
+
async function mt(a) {
|
|
16
|
+
var s;
|
|
17
|
+
if (!a.length) throw new Error("密码不能为空");
|
|
18
|
+
if (!((s = globalThis.crypto) != null && s.subtle)) throw new Error("密码锁定需要 HTTPS 或 localhost 环境");
|
|
19
|
+
const t = crypto.getRandomValues(new Uint8Array(16)), e = {
|
|
20
|
+
version: 1,
|
|
21
|
+
algorithm: "PBKDF2-SHA-256",
|
|
22
|
+
iterations: Y,
|
|
23
|
+
salt: G(t),
|
|
24
|
+
verifier: G(await it(a, t))
|
|
25
|
+
};
|
|
26
|
+
return JSON.stringify(e);
|
|
27
|
+
}
|
|
28
|
+
async function gt(a, t) {
|
|
29
|
+
let e;
|
|
30
|
+
try {
|
|
31
|
+
e = JSON.parse(a);
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error("锁定数据无效,无法解锁");
|
|
34
|
+
}
|
|
35
|
+
if (!e || e.version !== 1 || e.algorithm !== "PBKDF2-SHA-256" || e.iterations !== Y || typeof e.salt != "string" || !/^[0-9a-f]{32}$/.test(e.salt) || typeof e.verifier != "string" || !/^[0-9a-f]{64}$/.test(e.verifier))
|
|
36
|
+
throw new Error("锁定数据无效或版本不受支持,无法解锁");
|
|
37
|
+
const s = Uint8Array.from(e.salt.match(/../g), (n) => parseInt(n, 16)), o = G(await it(t, s));
|
|
38
|
+
let r = 0;
|
|
39
|
+
for (let n = 0; n < o.length; n += 1) r |= o.charCodeAt(n) ^ e.verifier.charCodeAt(n);
|
|
40
|
+
return r === 0;
|
|
41
|
+
}
|
|
42
|
+
function ft(a, t) {
|
|
43
|
+
const e = a.trim().replace("#", "");
|
|
44
|
+
if (/^[\da-f]{3}$/i.test(e)) {
|
|
45
|
+
const [o, r, n] = e.split("").map((i) => Number.parseInt(i + i, 16));
|
|
46
|
+
return `rgba(${o}, ${r}, ${n}, ${t})`;
|
|
47
|
+
}
|
|
48
|
+
if (/^[\da-f]{6}$/i.test(e)) {
|
|
49
|
+
const o = Number.parseInt(e, 16);
|
|
50
|
+
return `rgba(${o >> 16}, ${o >> 8 & 255}, ${o & 255}, ${t})`;
|
|
51
|
+
}
|
|
52
|
+
const s = a.match(/^rgba?\(\s*([\d.]+)[, ]+\s*([\d.]+)[, ]+\s*([\d.]+)/i);
|
|
53
|
+
return s ? `rgba(${s[1]}, ${s[2]}, ${s[3]}, ${t})` : `color-mix(in srgb, ${a} 12%, transparent)`;
|
|
54
|
+
}
|
|
55
|
+
function pt(a, t) {
|
|
56
|
+
return {
|
|
57
|
+
start: {
|
|
58
|
+
row: Math.min(a.row, t.row),
|
|
59
|
+
column: Math.min(a.column, t.column)
|
|
60
|
+
},
|
|
61
|
+
end: {
|
|
62
|
+
row: Math.max(a.row, t.row),
|
|
63
|
+
column: Math.max(a.column, t.column)
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function T(a, t) {
|
|
68
|
+
return t.row >= a.start.row && t.row <= a.end.row && t.column >= a.start.column && t.column <= a.end.column;
|
|
69
|
+
}
|
|
70
|
+
function Dt(a) {
|
|
71
|
+
return {
|
|
72
|
+
rows: a.end.row - a.start.row + 1,
|
|
73
|
+
columns: a.end.column - a.start.column + 1
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function W(a) {
|
|
77
|
+
return `${a.row}:${a.column}`;
|
|
78
|
+
}
|
|
79
|
+
function $(a, t) {
|
|
80
|
+
const e = a ?? t ?? { row: 0, column: 0 };
|
|
81
|
+
return pt(e, t ?? e);
|
|
82
|
+
}
|
|
83
|
+
function at(a, t, e, s) {
|
|
84
|
+
return {
|
|
85
|
+
row: a === "column" || a === "all" ? e - 1 : t.row,
|
|
86
|
+
column: a === "row" || a === "all" ? s - 1 : t.column
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function wt(a, t, e) {
|
|
90
|
+
if (!(!a.anchor || !a.focus))
|
|
91
|
+
return $(a.anchor, at(a.mode, a.focus, t, e));
|
|
92
|
+
}
|
|
93
|
+
const yt = 54, bt = 42, et = 12, St = 56, Ct = 150, vt = 28, kt = 64, Et = 0.25, Mt = 2;
|
|
94
|
+
function P(a, t) {
|
|
95
|
+
let e = 0;
|
|
96
|
+
for (let s = 0; s < a; s += 1) e += t(s);
|
|
97
|
+
return e;
|
|
98
|
+
}
|
|
99
|
+
function J(a, t) {
|
|
100
|
+
return P(a, t);
|
|
101
|
+
}
|
|
102
|
+
function zt(a) {
|
|
103
|
+
const t = (l, d, c, m) => {
|
|
104
|
+
let g = 0, p = 0;
|
|
105
|
+
for (; p < c && g + m(p) <= l; )
|
|
106
|
+
g += m(p), p += 1;
|
|
107
|
+
const M = Math.max(0, p - a.overscan);
|
|
108
|
+
let E = p, R = g;
|
|
109
|
+
for (; E < c && R < l + d; )
|
|
110
|
+
R += m(E), E += 1;
|
|
111
|
+
return { start: M, end: Math.min(c - 1, E + a.overscan - 1) };
|
|
112
|
+
}, e = t(a.scrollTop, a.clientHeight, a.rowCount, a.rowSize), s = t(a.scrollLeft, a.clientWidth, a.columnCount, a.columnSize), o = e.start, r = e.end, n = s.start, i = s.end;
|
|
113
|
+
return { top: o, bottom: r, left: n, right: i };
|
|
114
|
+
}
|
|
115
|
+
function I(a) {
|
|
116
|
+
let t = "";
|
|
117
|
+
for (let s = 0; s < a.length; s += 32768)
|
|
118
|
+
t += String.fromCharCode(...a.subarray(s, s + 32768));
|
|
119
|
+
return btoa(t);
|
|
120
|
+
}
|
|
121
|
+
function st(a) {
|
|
122
|
+
const t = atob(a);
|
|
123
|
+
return Uint8Array.from(t, (e) => e.charCodeAt(0));
|
|
124
|
+
}
|
|
125
|
+
const ot = "application/x-colwork-cells", Lt = ["任务", "负责人", "状态", "优先级", "开始日期", "备注"], xt = [
|
|
126
|
+
{ urls: "stun:stun.qq.com:3478" },
|
|
127
|
+
{ urls: "stun:stun.miwifi.com:3478" },
|
|
128
|
+
{ urls: "stun:stun.chat.bilibili.com:3478" },
|
|
129
|
+
{ urls: "stun:stun.l.google.com:19302" },
|
|
130
|
+
{ urls: "stun:stun1.l.google.com:19302" }
|
|
131
|
+
], j = [
|
|
132
|
+
["设计协作表格", "小林", "进行中", "高", "2026-08-07", "欢迎测试"],
|
|
133
|
+
["整理组件 API", "阿南", "已完成", "中", "2026-08-08", "已补充示例"],
|
|
134
|
+
["补充单元测试", "小周", "未开始", "高", "2026-08-09", "覆盖边界输入"],
|
|
135
|
+
["实现虚拟滚动", "小林", "进行中", "高", "2026-08-10", "先支持行虚拟化"],
|
|
136
|
+
["接入 WebSocket", "阿南", "已完成", "中", "2026-08-11", "等待联调"],
|
|
137
|
+
["增加复制粘贴", "小周", "进行中", "中", "2026-08-12", "支持 TSV 格式"],
|
|
138
|
+
["检查 Safari 兼容性", "小雨", "阻塞", "高", "2026-08-13", "需要补充 ICE 日志"],
|
|
139
|
+
["编写使用文档", "阿南", "未开始", "低", "2026-08-14", "准备 API 清单"],
|
|
140
|
+
["优化移动端布局", "小雨", "未开始", "中", "2026-08-15", "检查横向滚动"],
|
|
141
|
+
["发布 v0.1.0", "小林", "未开始", "低", "2026-08-18", "等待最终验收"]
|
|
142
|
+
];
|
|
143
|
+
function At(a) {
|
|
144
|
+
let t = a + 1, e = "";
|
|
145
|
+
for (; t > 0; )
|
|
146
|
+
t -= 1, e = String.fromCharCode(65 + t % 26) + e, t = Math.floor(t / 26);
|
|
147
|
+
return e;
|
|
148
|
+
}
|
|
149
|
+
class Ft {
|
|
150
|
+
constructor(t, e) {
|
|
151
|
+
h(this, "doc");
|
|
152
|
+
h(this, "cells");
|
|
153
|
+
h(this, "styles");
|
|
154
|
+
h(this, "rowHeights");
|
|
155
|
+
h(this, "columnWidths");
|
|
156
|
+
h(this, "locks");
|
|
157
|
+
h(this, "messageDialog");
|
|
158
|
+
h(this, "passwordDialog");
|
|
159
|
+
h(this, "destroyed", !1);
|
|
160
|
+
h(this, "frozenRows", 0);
|
|
161
|
+
h(this, "frozenColumns", 0);
|
|
162
|
+
h(this, "merges");
|
|
163
|
+
h(this, "undoManager");
|
|
164
|
+
h(this, "provider");
|
|
165
|
+
h(this, "awareness");
|
|
166
|
+
h(this, "root");
|
|
167
|
+
h(this, "options");
|
|
168
|
+
h(this, "table");
|
|
169
|
+
h(this, "viewportResizeObserver");
|
|
170
|
+
h(this, "viewport");
|
|
171
|
+
h(this, "frozenBoundaryLayer");
|
|
172
|
+
h(this, "protectionMenuButton");
|
|
173
|
+
h(this, "contextMenu");
|
|
174
|
+
h(this, "pendingCellContext");
|
|
175
|
+
h(this, "rightButtonReleased", !1);
|
|
176
|
+
h(this, "rightMouseUpPoint");
|
|
177
|
+
h(this, "rightDrag");
|
|
178
|
+
h(this, "undoButton");
|
|
179
|
+
h(this, "redoButton");
|
|
180
|
+
h(this, "rowCountValue");
|
|
181
|
+
h(this, "columnCountValue");
|
|
182
|
+
h(this, "zoomRatio", 1);
|
|
183
|
+
h(this, "showGridLines", !0);
|
|
184
|
+
h(this, "bandedRows", !1);
|
|
185
|
+
h(this, "readOnly");
|
|
186
|
+
h(this, "documentRevision", 0);
|
|
187
|
+
h(this, "updateSequence", 0);
|
|
188
|
+
h(this, "updateLog", []);
|
|
189
|
+
h(this, "resizing");
|
|
190
|
+
h(this, "onCellsChange", () => this.refreshValues());
|
|
191
|
+
h(this, "onStylesChange", () => this.refreshStyles());
|
|
192
|
+
h(this, "onSizesChange", () => this.render());
|
|
193
|
+
h(this, "onMergesChange", () => this.render());
|
|
194
|
+
h(this, "onLocksChange", () => {
|
|
195
|
+
this.undoManager.clear(), this.editingKey && this.isCellLocked(this.editingKey) && this.finishEdit(!1), this.resizing && this.isAxisLocked(this.resizing.axis, this.resizing.index) && (this.resizing = void 0, this.viewport.classList.remove("is-resizing")), this.render();
|
|
196
|
+
});
|
|
197
|
+
h(this, "onAwarenessChange", () => {
|
|
198
|
+
this.table && (this.expandForRemoteSelections() ? this.render() : this.refreshCursors());
|
|
199
|
+
});
|
|
200
|
+
h(this, "renderFrame");
|
|
201
|
+
h(this, "onViewportScroll", () => {
|
|
202
|
+
this.renderFrame === void 0 && (this.renderFrame = requestAnimationFrame(() => {
|
|
203
|
+
this.renderFrame = void 0, this.expandLogicalSpace(), this.render();
|
|
204
|
+
}));
|
|
205
|
+
});
|
|
206
|
+
h(this, "onViewportWheel", (t) => {
|
|
207
|
+
if (!t.ctrlKey) return;
|
|
208
|
+
t.preventDefault();
|
|
209
|
+
const e = t.deltaY < 0 ? 1.1 : 0.9;
|
|
210
|
+
this.zoomRatio = Math.max(Et, Math.min(Mt, this.zoomRatio * e)), this.expandLogicalSpace(), this.render();
|
|
211
|
+
});
|
|
212
|
+
h(this, "onViewportKeyDown", (t) => {
|
|
213
|
+
if (this.readOnly || this.editor) return;
|
|
214
|
+
const e = t.ctrlKey || t.metaKey;
|
|
215
|
+
if (e && t.key.toLowerCase() === "z") {
|
|
216
|
+
t.preventDefault(), t.shiftKey ? this.redo() : this.undo();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (e && t.key.toLowerCase() === "y") {
|
|
220
|
+
t.preventDefault(), this.redo();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (t.key !== " " || !this.selectionFocus) return;
|
|
224
|
+
const s = `${this.selectionFocus.row}:${this.selectionFocus.column}`, o = this.table.querySelector(`td[data-key="${s}"]`);
|
|
225
|
+
o && (t.preventDefault(), this.startEdit(o, s));
|
|
226
|
+
});
|
|
227
|
+
h(this, "onTableMouseDown", (t) => {
|
|
228
|
+
t.target.closest("th") && t.preventDefault();
|
|
229
|
+
});
|
|
230
|
+
h(this, "onDocumentMouseDown", (t) => {
|
|
231
|
+
var e;
|
|
232
|
+
this.contextMenu && !this.contextMenu.contains(t.target) && !((e = this.protectionMenuButton) != null && e.contains(t.target)) && this.hideContextMenu();
|
|
233
|
+
});
|
|
234
|
+
h(this, "onWindowMouseUp", (t) => {
|
|
235
|
+
if (this.resizing) {
|
|
236
|
+
this.resizing.axis === "row" ? this.rowHeights.set(String(this.resizing.index), this.resizing.currentSize / this.zoomRatio) : this.columnWidths.set(String(this.resizing.index), this.resizing.currentSize / this.zoomRatio), this.resizing = void 0, this.viewport.classList.remove("is-resizing");
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (this.selecting = !1, this.headerSelecting = void 0, t.button === 2 && (this.rightButtonReleased = !0, this.rightMouseUpPoint = { x: t.clientX, y: t.clientY }, this.pendingCellContext)) {
|
|
240
|
+
const e = this.rightMouseUpPoint;
|
|
241
|
+
this.pendingCellContext = void 0, this.showCellContextMenu(e.x, e.y);
|
|
242
|
+
}
|
|
243
|
+
this.pendingAwarenessCursor && this.sendAwareness(this.pendingAwarenessCursor);
|
|
244
|
+
});
|
|
245
|
+
h(this, "onWindowBlur", () => {
|
|
246
|
+
this.headerSelecting && (this.selecting = !1, this.headerSelecting = void 0, this.pendingAwarenessCursor && this.sendAwareness(this.pendingAwarenessCursor));
|
|
247
|
+
});
|
|
248
|
+
h(this, "onWindowMouseMove", (t) => {
|
|
249
|
+
if (this.headerSelecting && !(t.buttons & 1) && this.onWindowBlur(), !this.resizing) return;
|
|
250
|
+
const e = this.resizing.axis === "row" ? t.clientY - this.resizing.start : t.clientX - this.resizing.start, s = (this.resizing.axis === "row" ? vt : kt) * this.zoomRatio, o = Math.max(s, this.resizing.size + e);
|
|
251
|
+
this.resizing.currentSize = o, this.render();
|
|
252
|
+
});
|
|
253
|
+
h(this, "onHistoryChange", () => this.refreshHistoryButtons());
|
|
254
|
+
h(this, "editor");
|
|
255
|
+
h(this, "editingKey");
|
|
256
|
+
h(this, "editingOriginal", "");
|
|
257
|
+
h(this, "selectedKey");
|
|
258
|
+
h(this, "selectionAnchor");
|
|
259
|
+
h(this, "selectionFocus");
|
|
260
|
+
h(this, "selectionMode");
|
|
261
|
+
h(this, "selecting", !1);
|
|
262
|
+
h(this, "headerSelecting");
|
|
263
|
+
h(this, "pendingAwarenessCursor");
|
|
264
|
+
h(this, "loggedPeers", /* @__PURE__ */ new Set());
|
|
265
|
+
h(this, "loggedSignals", /* @__PURE__ */ new Set());
|
|
266
|
+
h(this, "onDocUpdate", (t, e) => {
|
|
267
|
+
this.documentRevision += 1;
|
|
268
|
+
const s = this.provider, o = { seq: ++this.updateSequence, update: I(t), timestamp: Date.now() };
|
|
269
|
+
this.updateLog.push(o), this.logChunk(`yjs:${e === (s == null ? void 0 : s.room) ? "recv" : "send"}`, t), this.log(`yjs:state ${JSON.stringify({ cells: Array.from(this.cells.entries()), rowHeights: Array.from(this.rowHeights.entries()), columnWidths: Array.from(this.columnWidths.entries()), merges: Array.from(this.merges.entries()) })}`);
|
|
270
|
+
});
|
|
271
|
+
h(this, "onAwarenessUpdate", ({ added: t, updated: e, removed: s }, o) => {
|
|
272
|
+
const r = this.provider;
|
|
273
|
+
this.log(`awareness:${o === (r == null ? void 0 : r.room) ? "recv" : "send"} added=${t.join(",")} updated=${e.join(",")} removed=${s.join(",")}`), this.log(`awareness:state ${JSON.stringify(Array.from(this.awareness.getStates().entries()))}`);
|
|
274
|
+
});
|
|
275
|
+
h(this, "onCopy", (t) => {
|
|
276
|
+
const e = this.selectionToClipboard();
|
|
277
|
+
if (!e || !t.clipboardData) return;
|
|
278
|
+
const s = e.values.map((o) => o.join(" ")).join(`
|
|
279
|
+
`);
|
|
280
|
+
t.clipboardData.setData("text/plain", s), t.clipboardData.setData(ot, JSON.stringify(e)), t.preventDefault();
|
|
281
|
+
});
|
|
282
|
+
h(this, "onPaste", (t) => {
|
|
283
|
+
var l, d, c;
|
|
284
|
+
if (t.preventDefault(), t.stopPropagation(), this.readOnly) return;
|
|
285
|
+
const e = (d = (l = t.clipboardData) == null ? void 0 : l.getData("text/plain")) == null ? void 0 : d.replace(/^\uFEFF/, "");
|
|
286
|
+
if (!e || !this.selectionFocus) return;
|
|
287
|
+
this.editor && this.finishEdit(!1);
|
|
288
|
+
let s = e.replace(/\r\n/g, `
|
|
289
|
+
`).replace(/\r/g, `
|
|
290
|
+
`).split(`
|
|
291
|
+
`).filter((m, g, p) => !(g === p.length - 1 && m === "")).map((m) => m.split(" ")), o;
|
|
292
|
+
const r = (c = t.clipboardData) == null ? void 0 : c.getData(ot);
|
|
293
|
+
if (r)
|
|
294
|
+
try {
|
|
295
|
+
const m = JSON.parse(r);
|
|
296
|
+
Array.isArray(m.values) && Array.isArray(m.styles) && (s = m.values, o = m.styles);
|
|
297
|
+
} catch {
|
|
298
|
+
o = void 0;
|
|
299
|
+
}
|
|
300
|
+
const n = this.selectionFocus, i = Math.max(...s.map((m) => m.length));
|
|
301
|
+
s.some((m, g) => m.some((p, M) => this.isCellLocked(W({ row: n.row + g, column: n.column + M })))) || (this.doc.transact(() => s.forEach((m, g) => m.forEach((p, M) => {
|
|
302
|
+
var q;
|
|
303
|
+
const E = n.row + g, R = n.column + M;
|
|
304
|
+
if (E >= this.rowCount || R >= this.columnCount) return;
|
|
305
|
+
const H = W({ row: E, column: R });
|
|
306
|
+
if (this.cells.set(H, p), o) {
|
|
307
|
+
const O = (q = o[g]) == null ? void 0 : q[M];
|
|
308
|
+
O ? this.styles.set(H, JSON.stringify(O)) : this.styles.delete(H);
|
|
309
|
+
}
|
|
310
|
+
}))), this.selectionAnchor = n, this.selectionFocus = { row: Math.min(this.rowCount - 1, n.row + s.length - 1), column: Math.min(this.columnCount - 1, n.column + i - 1) }, this.render());
|
|
311
|
+
});
|
|
312
|
+
var A, X, Z, Q;
|
|
313
|
+
this.root = t, this.options = e, this.readOnly = e.readOnly === !0, this.doc = new K.Doc(), this.cells = this.doc.getMap("cells"), this.styles = this.doc.getMap("styles"), this.rowHeights = this.doc.getMap("rowHeights"), this.columnWidths = this.doc.getMap("columnWidths"), this.merges = this.doc.getMap("merges"), this.locks = this.doc.getMap("locks"), this.rowCountValue = Math.max(e.rowCount ?? 100, ((A = e.rows) == null ? void 0 : A.length) ?? j.length), this.columnCountValue = Math.max(e.columnCount ?? 10, Lt.length);
|
|
314
|
+
const s = e.transport === "webrtc", o = new URL(e.websocketUrl ?? "ws://127.0.0.1:1234"), r = Object.fromEntries(o.searchParams);
|
|
315
|
+
o.search = "", this.readOnly ? this.awareness = new dt(this.doc) : (this.provider = s ? new ut(e.room, this.doc, {
|
|
316
|
+
signaling: [e.signalingUrl ?? "ws://127.0.0.1:4444"],
|
|
317
|
+
filterBcConns: !1,
|
|
318
|
+
peerOpts: e.peerOpts ?? {
|
|
319
|
+
config: {
|
|
320
|
+
iceServers: xt
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}) : new tt(o.toString().replace(/\/$/, ""), encodeURIComponent(e.room), this.doc, { params: r, connect: !e.initializeAfterSync }), this.awareness = this.provider.awareness), e.snapshot && K.applyUpdate(this.doc, e.snapshot), (X = e.updates) == null || X.forEach((f) => K.applyUpdate(this.doc, f)), this.log(`room:${e.room}`), this.doc.on("update", this.onDocUpdate), this.awareness.on("update", this.onAwarenessUpdate), this.awareness.on("change", this.onAwarenessChange), this.awareness.setLocalStateField("user", { name: e.user, color: e.userColor }), s && this.provider && this.inspectConnections();
|
|
324
|
+
const n = document.createElement("section");
|
|
325
|
+
n.className = "colwork-table";
|
|
326
|
+
const i = document.createElement("div");
|
|
327
|
+
i.className = "colwork-table__toolbar", this.readOnly && (i.hidden = !0), i.setAttribute("aria-label", "表格工具栏");
|
|
328
|
+
const l = document.createElement("div");
|
|
329
|
+
l.className = "colwork-table__toolbar-row";
|
|
330
|
+
const d = document.createElement("div");
|
|
331
|
+
d.className = "colwork-table__toolbar-row", i.append(l, d);
|
|
332
|
+
const c = (f, S) => {
|
|
333
|
+
const w = document.createElement("div");
|
|
334
|
+
w.className = "colwork-table__toolbar-group", w.setAttribute("role", "group"), w.setAttribute("aria-label", S);
|
|
335
|
+
const k = document.createElement("span");
|
|
336
|
+
return k.className = "colwork-table__group-label", k.textContent = S, w.append(k), f.append(w), w;
|
|
337
|
+
}, m = c(l, "历史"), g = c(l, "单元格"), p = c(l, "保护"), M = c(l, "显示"), E = c(d, "文字"), R = c(d, "对齐"), H = c(d, "格式");
|
|
338
|
+
[
|
|
339
|
+
["撤销", "↶", () => this.undo()],
|
|
340
|
+
["重做", "↷", () => this.redo()]
|
|
341
|
+
].forEach(([f, S, w], k) => {
|
|
342
|
+
const _ = document.createElement("button");
|
|
343
|
+
_.type = "button", _.className = `colwork-table__history colwork-table__history--${k === 0 ? "undo" : "redo"}`, _.title = f, _.textContent = S, _.addEventListener("mousedown", (lt) => lt.preventDefault()), _.addEventListener("click", w), k === 0 ? this.undoButton = _ : this.redoButton = _, m.append(_);
|
|
344
|
+
}), [
|
|
345
|
+
["合并单元格", "合并", () => this.mergeSelection()],
|
|
346
|
+
["拆分单元格", "拆分", () => this.unmergeSelection()]
|
|
347
|
+
].forEach(([f, S, w]) => {
|
|
348
|
+
const k = document.createElement("button");
|
|
349
|
+
k.type = "button", k.className = "colwork-table__format colwork-table__grid-action", k.title = f, k.textContent = S, k.addEventListener("mousedown", (_) => _.preventDefault()), k.addEventListener("click", w), g.append(k);
|
|
350
|
+
}), (Z = p.querySelector(".colwork-table__group-label")) == null || Z.remove();
|
|
351
|
+
const z = document.createElement("button");
|
|
352
|
+
this.protectionMenuButton = z, z.type = "button", z.className = "colwork-table__format colwork-table__dropdown-trigger", z.textContent = "保护", z.title = "锁定、密码锁定与解锁", z.setAttribute("aria-label", "保护"), z.setAttribute("aria-expanded", "false"), z.addEventListener("mousedown", (f) => f.preventDefault()), z.addEventListener("click", () => {
|
|
353
|
+
if (z.getAttribute("aria-expanded") === "true") {
|
|
354
|
+
this.hideContextMenu();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const f = z.getBoundingClientRect();
|
|
358
|
+
this.showContextMenu(f.left, f.bottom + 4, [{ label: "锁定与解锁", actions: this.protectionActions() }]), z.setAttribute("aria-expanded", "true");
|
|
359
|
+
}), z.addEventListener("keydown", (f) => {
|
|
360
|
+
f.key === "ArrowDown" && (f.preventDefault(), z.click());
|
|
361
|
+
}), p.append(z);
|
|
362
|
+
const U = document.createElement("div");
|
|
363
|
+
U.className = "colwork-table__view-control";
|
|
364
|
+
const D = document.createElement("button");
|
|
365
|
+
D.type = "button", D.className = "colwork-table__format", D.textContent = "视图", D.setAttribute("aria-label", "视图"), D.classList.add("colwork-table__dropdown-trigger"), D.title = "本地视图选项";
|
|
366
|
+
const V = document.createElement("div");
|
|
367
|
+
V.className = "colwork-table__view-menu", [
|
|
368
|
+
["隔行异色", () => this.bandedRows, (f) => {
|
|
369
|
+
this.bandedRows = f;
|
|
370
|
+
}],
|
|
371
|
+
["显示网格线", () => this.showGridLines, (f) => {
|
|
372
|
+
this.showGridLines = f;
|
|
373
|
+
}]
|
|
374
|
+
].forEach(([f, S, w]) => {
|
|
375
|
+
const k = document.createElement("label"), _ = document.createElement("input");
|
|
376
|
+
_.type = "checkbox", _.checked = S(), _.addEventListener("change", () => {
|
|
377
|
+
w(_.checked), this.render();
|
|
378
|
+
}), k.append(_, document.createTextNode(f)), V.append(k);
|
|
379
|
+
}), [
|
|
380
|
+
["固定至选中行", () => this.freezeSelection("row")],
|
|
381
|
+
["固定至选中列", () => this.freezeSelection("column")],
|
|
382
|
+
["取消固定", () => this.setFrozenPanes(0, 0)]
|
|
383
|
+
].forEach(([f, S]) => {
|
|
384
|
+
const w = document.createElement("button");
|
|
385
|
+
w.type = "button", w.textContent = f, w.addEventListener("mousedown", (k) => k.preventDefault()), w.addEventListener("click", S), V.append(w);
|
|
386
|
+
}), U.append(D, V), M.append(U), [
|
|
387
|
+
["bold", "B", "加粗"],
|
|
388
|
+
["italic", "I", "斜体"],
|
|
389
|
+
["underline", "U", "下划线"],
|
|
390
|
+
["strike", "S", "删除线"],
|
|
391
|
+
["wrap", "换行", "自动换行"],
|
|
392
|
+
["align-left", "左", "左对齐"],
|
|
393
|
+
["align-center", "中", "居中"],
|
|
394
|
+
["align-right", "右", "右对齐"]
|
|
395
|
+
].forEach(([f, S, w]) => {
|
|
396
|
+
const k = document.createElement("button");
|
|
397
|
+
k.type = "button", k.className = `colwork-table__format colwork-table__format--${f}`, k.textContent = S, k.title = w, k.addEventListener("mousedown", (_) => _.preventDefault()), k.addEventListener("click", () => f.startsWith("align-") ? this.setStyle("align", f.slice(6)) : this.toggleFormat(f)), (f.startsWith("align-") || f === "wrap" ? R : E).append(k);
|
|
398
|
+
});
|
|
399
|
+
const b = document.createElement("select");
|
|
400
|
+
b.className = "colwork-table__select", b.title = "字体大小", [10, 12, 14, 16, 18, 24].forEach((f) => {
|
|
401
|
+
const S = document.createElement("option");
|
|
402
|
+
S.value = String(f), S.textContent = `${f}px`, b.append(S);
|
|
403
|
+
}), b.addEventListener("change", () => this.setStyle("fontSize", Number(b.value))), E.append(b);
|
|
404
|
+
const L = document.createElement("select");
|
|
405
|
+
L.className = "colwork-table__select", L.title = "单元格格式", [
|
|
406
|
+
["general", "常规"],
|
|
407
|
+
["number", "数字"],
|
|
408
|
+
["date", "日期"],
|
|
409
|
+
["percent", "百分比"]
|
|
410
|
+
].forEach(([f, S]) => {
|
|
411
|
+
const w = document.createElement("option");
|
|
412
|
+
w.value = f, w.textContent = S, L.append(w);
|
|
413
|
+
}), L.addEventListener("change", () => this.setStyle("format", L.value)), H.append(L);
|
|
414
|
+
const C = document.createElement("button");
|
|
415
|
+
C.type = "button", C.className = "colwork-table__format", C.title = "清除格式", C.textContent = "清除", C.addEventListener("mousedown", (f) => f.preventDefault()), C.addEventListener("click", () => this.clearFormatting()), H.append(C);
|
|
416
|
+
const F = document.createElement("div");
|
|
417
|
+
F.className = "colwork-table__border-control";
|
|
418
|
+
const v = document.createElement("button");
|
|
419
|
+
v.type = "button", v.className = "colwork-table__format", v.title = "边框选项", v.textContent = "边框", v.setAttribute("aria-label", "边框"), v.classList.add("colwork-table__dropdown-trigger");
|
|
420
|
+
const x = document.createElement("div");
|
|
421
|
+
x.className = "colwork-table__border-menu", [
|
|
422
|
+
["外围", "outer"],
|
|
423
|
+
["中间", "inner"],
|
|
424
|
+
["横线", "horizontal"],
|
|
425
|
+
["竖线", "vertical"]
|
|
426
|
+
].forEach(([f, S]) => {
|
|
427
|
+
const w = document.createElement("button");
|
|
428
|
+
w.type = "button", w.textContent = f, w.addEventListener("click", () => this.toggleBorder(S)), x.append(w);
|
|
429
|
+
}), F.append(v, x), H.insertBefore(F, C), [
|
|
430
|
+
["color", "文字颜色"],
|
|
431
|
+
["background", "背景颜色"]
|
|
432
|
+
].forEach(([f, S]) => {
|
|
433
|
+
const w = document.createElement("input");
|
|
434
|
+
w.type = "color", w.className = `colwork-table__color colwork-table__color--${f}`, w.title = S, w.addEventListener("input", () => this.setStyle(f, w.value)), (f === "color" ? E : H).append(w);
|
|
435
|
+
}), this.viewport = document.createElement("div"), this.viewport.className = "colwork-table__viewport", this.viewport.addEventListener("scroll", this.onViewportScroll), this.viewport.addEventListener("wheel", this.onViewportWheel, { passive: !1 }), this.viewport.tabIndex = 0, this.viewport.addEventListener("keydown", this.onViewportKeyDown), this.viewport.addEventListener("copy", this.onCopy, !0), this.viewport.addEventListener("paste", this.onPaste, !0), window.addEventListener("mouseup", this.onWindowMouseUp), window.addEventListener("mousemove", this.onWindowMouseMove), window.addEventListener("blur", this.onWindowBlur), document.addEventListener("mousedown", this.onDocumentMouseDown), this.table = document.createElement("table"), this.table.addEventListener("mousedown", this.onTableMouseDown), this.viewport.append(this.table), this.frozenBoundaryLayer = document.createElement("div"), this.frozenBoundaryLayer.className = "colwork-table__frozen-boundaries", this.frozenBoundaryLayer.setAttribute("aria-hidden", "true"), n.append(i, this.viewport, this.frozenBoundaryLayer), t.replaceChildren(n);
|
|
436
|
+
const N = !this.readOnly && !e.snapshot && !((Q = e.updates) != null && Q.length), B = N && this.cells.size === 0 && this.locks.size === 0;
|
|
437
|
+
if (N && !e.initializeAfterSync && this.seed(e.rows ?? j), B && !e.initializeAfterSync && this.seedDemoFormatting(), this.expandLogicalSizeFromDocument(), this.undoManager = new K.UndoManager([this.cells, this.styles, this.rowHeights, this.columnWidths, this.merges], { captureTimeout: 500 }), this.undoManager.on("stack-item-added", this.onHistoryChange), this.undoManager.on("stack-item-popped", this.onHistoryChange), this.undoManager.on("stack-cleared", this.onHistoryChange), this.render(), this.refreshHistoryButtons(), this.cells.observe(this.onCellsChange), this.styles.observe(this.onStylesChange), this.rowHeights.observe(this.onSizesChange), this.columnWidths.observe(this.onSizesChange), this.merges.observe(this.onMergesChange), this.locks.observe(this.onLocksChange), this.viewportResizeObserver = new ResizeObserver(() => this.refreshFrozenBoundaries()), this.viewportResizeObserver.observe(this.viewport), this.provider) {
|
|
438
|
+
const f = this.provider;
|
|
439
|
+
f.on("status", ({ connected: S, status: w }) => {
|
|
440
|
+
this.log(S === !0 || w === "connected" ? "signaling:connected" : "signaling:disconnected"), s && this.inspectConnections();
|
|
441
|
+
}), s ? f.on("synced", ({ synced: S }) => this.log(S ? "synced" : "syncing")) : f.on("sync", (S) => {
|
|
442
|
+
if (this.log(S ? "synced" : "syncing"), !S || !e.initializeAfterSync) return;
|
|
443
|
+
const w = this.doc.getMap("colwork-metadata");
|
|
444
|
+
w.get("initialized") || (this.doc.transact(() => {
|
|
445
|
+
N && this.cells.size === 0 && this.locks.size === 0 && (this.seed(e.rows ?? j), this.seedDemoFormatting()), w.set("initialized", !0);
|
|
446
|
+
}), this.undoManager.clear()), this.expandLogicalSizeFromDocument(), this.render();
|
|
447
|
+
}), e.initializeAfterSync && this.provider instanceof tt && this.provider.connect(), s && f.on("peers", ({ added: S, removed: w, webrtcPeers: k }) => {
|
|
448
|
+
S.length && this.log(`peer:+${S.length}`), w.length && this.log(`peer:-${w.length}`), this.log(`webrtc:${k.length}`), this.inspectConnections();
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
log(t) {
|
|
453
|
+
var e, s;
|
|
454
|
+
(s = (e = this.options).onLog) == null || s.call(e, `${(/* @__PURE__ */ new Date()).toLocaleTimeString()} ${t}`);
|
|
455
|
+
}
|
|
456
|
+
undo() {
|
|
457
|
+
this.readOnly || !this.undoManager.canUndo() || (this.editor && this.finishEdit(!0), this.undoManager.undo());
|
|
458
|
+
}
|
|
459
|
+
redo() {
|
|
460
|
+
this.readOnly || !this.undoManager.canRedo() || (this.editor && this.finishEdit(!0), this.undoManager.redo());
|
|
461
|
+
}
|
|
462
|
+
refreshHistoryButtons() {
|
|
463
|
+
this.undoButton && (this.undoButton.disabled = this.readOnly || !this.undoManager.canUndo()), this.redoButton && (this.redoButton.disabled = this.readOnly || !this.undoManager.canRedo());
|
|
464
|
+
}
|
|
465
|
+
getYjsSnapshot() {
|
|
466
|
+
return {
|
|
467
|
+
format: "colwork.snapshot",
|
|
468
|
+
version: 1,
|
|
469
|
+
room: this.options.room,
|
|
470
|
+
sequence: this.updateSequence,
|
|
471
|
+
snapshot: I(K.encodeStateAsUpdate(this.doc)),
|
|
472
|
+
stateVector: I(K.encodeStateVector(this.doc))
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
getYjsUpdateLog() {
|
|
476
|
+
return {
|
|
477
|
+
format: "colwork.update-log",
|
|
478
|
+
version: 1,
|
|
479
|
+
room: this.options.room,
|
|
480
|
+
updates: this.updateLog.map((t) => ({ ...t }))
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
/** Freeze a leading prefix of rows/columns in this client only. */
|
|
484
|
+
setFrozenPanes(t, e) {
|
|
485
|
+
if (![t, e].every((r) => Number.isInteger(r) && r >= 0))
|
|
486
|
+
throw new RangeError("Frozen row and column counts must be non-negative integers");
|
|
487
|
+
const s = Math.min(t, this.rowCount - 1), o = Math.min(e, this.columnCount - 1);
|
|
488
|
+
return this.hasPartialMerge("row", 0, s - 1) || this.hasPartialMerge("column", 0, o - 1) ? (this.showLockMessage("当前选择包含不完整的合并块,无法固定,请选择完整的合并块"), !1) : (this.finishEdit(!0), this.frozenRows = s, this.frozenColumns = o, this.closeLockMessage(), this.render(), !0);
|
|
489
|
+
}
|
|
490
|
+
hasPartialMerge(t, e, s) {
|
|
491
|
+
return e > s ? !1 : this.getMergeRanges().some((o) => {
|
|
492
|
+
const r = o.start[t], n = o.end[t];
|
|
493
|
+
return r <= s && n >= e && (r < e || n > s);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
crossesFrozenBoundary(t) {
|
|
497
|
+
return t.start.row < this.frozenRows && t.end.row >= this.frozenRows || t.start.column < this.frozenColumns && t.end.column >= this.frozenColumns;
|
|
498
|
+
}
|
|
499
|
+
freezeSelection(t) {
|
|
500
|
+
if (!this.selectionFocus) return;
|
|
501
|
+
const e = $(this.selectionAnchor, this.selectionFocus);
|
|
502
|
+
if (this.hasPartialMerge(t, e.start[t], e.end[t])) {
|
|
503
|
+
this.showLockMessage("当前选择包含不完整的合并块,无法固定,请选择完整的合并块");
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
this.setFrozenPanes(
|
|
507
|
+
t === "row" ? e.end.row + 1 : this.frozenRows,
|
|
508
|
+
t === "column" ? e.end.column + 1 : this.frozenColumns
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
rangeHasLocks(t) {
|
|
512
|
+
return Array.from(this.locks.keys()).some((e) => {
|
|
513
|
+
const [s, o] = e.split(":").map(Number);
|
|
514
|
+
return T(t, { row: s, column: o });
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
isCellLocked(t) {
|
|
518
|
+
if (this.locks.has(t)) return !0;
|
|
519
|
+
const [e, s] = t.split(":").map(Number), o = this.getMergeRanges().find((r) => T(r, { row: e, column: s }));
|
|
520
|
+
return !!o && this.rangeHasLocks(o);
|
|
521
|
+
}
|
|
522
|
+
isCellPasswordLocked(t) {
|
|
523
|
+
const e = (n) => this.locks.has(n) && this.locks.get(n) !== "1";
|
|
524
|
+
if (e(t)) return !0;
|
|
525
|
+
const [s, o] = t.split(":").map(Number), r = this.getMergeRanges().find((n) => T(n, { row: s, column: o }));
|
|
526
|
+
return !!r && Array.from(this.locks.keys()).some((n) => {
|
|
527
|
+
const [i, l] = n.split(":").map(Number);
|
|
528
|
+
return e(n) && T(r, { row: i, column: l });
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
selectionHasLocks() {
|
|
532
|
+
return this.rangeHasLocks($(this.selectionAnchor, this.selectionFocus));
|
|
533
|
+
}
|
|
534
|
+
isAxisLocked(t, e, s = !1) {
|
|
535
|
+
return Array.from(this.locks.keys()).some((o) => {
|
|
536
|
+
const r = Number(o.split(":")[t === "row" ? 0 : 1]);
|
|
537
|
+
return s ? r >= e : r === e;
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
/** Passwords are optional for legacy/plain locks; protected locks require verification. */
|
|
541
|
+
async setSelectionLocked(t, e) {
|
|
542
|
+
if (this.readOnly || this.destroyed || !this.selectionFocus) return;
|
|
543
|
+
this.finishEdit(!0);
|
|
544
|
+
const s = this.expandSelectionToMerges(
|
|
545
|
+
this.selectionAnchor ?? this.selectionFocus,
|
|
546
|
+
this.selectionFocus
|
|
547
|
+
), o = $(s.anchor, s.focus), r = [];
|
|
548
|
+
for (let d = o.start.row; d <= o.end.row; d += 1)
|
|
549
|
+
for (let c = o.start.column; c <= o.end.column; c += 1) r.push(W({ row: d, column: c }));
|
|
550
|
+
const n = [...new Set(r.map((d) => this.locks.get(d)).filter((d) => d !== void 0 && d !== "1"))];
|
|
551
|
+
if (t && n.length) throw new Error("选区包含密码锁定单元格,请先用原密码解锁");
|
|
552
|
+
const i = this.documentRevision;
|
|
553
|
+
let l = "1";
|
|
554
|
+
if (t && e !== void 0 && (l = await mt(e)), !t && n.length) {
|
|
555
|
+
if (!e) throw new Error("请输入解锁密码");
|
|
556
|
+
for (const d of n)
|
|
557
|
+
if (!await gt(d, e)) throw new Error("密码不正确,选区未解锁");
|
|
558
|
+
}
|
|
559
|
+
if (!this.destroyed) {
|
|
560
|
+
if (i !== this.documentRevision) throw new Error("表格内容或锁定状态已变化,请重试");
|
|
561
|
+
this.doc.transact(() => {
|
|
562
|
+
r.forEach((d) => {
|
|
563
|
+
t ? this.locks.set(d, l) : this.locks.delete(d);
|
|
564
|
+
});
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
requestSelectionLock(t, e = !1) {
|
|
569
|
+
if (this.readOnly || this.destroyed || !this.selectionFocus || this.passwordDialog) return;
|
|
570
|
+
this.closeLockMessage();
|
|
571
|
+
const s = $(this.selectionAnchor, this.selectionFocus);
|
|
572
|
+
if (!(e || !t && Array.from(this.locks.entries()).some(([E, R]) => {
|
|
573
|
+
const [H, q] = E.split(":").map(Number);
|
|
574
|
+
return R !== "1" && T(s, { row: H, column: q });
|
|
575
|
+
}))) {
|
|
576
|
+
this.setSelectionLocked(t).catch((E) => this.showLockMessage(E instanceof Error ? E.message : "操作失败"));
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
const r = document.createElement("dialog");
|
|
580
|
+
this.passwordDialog = r, r.className = "colwork-table__password-dialog", r.setAttribute("aria-label", t ? "密码锁定" : "密码解锁");
|
|
581
|
+
const n = document.createElement("form"), i = document.createElement("h3");
|
|
582
|
+
i.textContent = t ? "设置锁定密码" : "输入解锁密码";
|
|
583
|
+
const l = document.createElement("input");
|
|
584
|
+
l.type = "password", l.required = !0, l.placeholder = t ? "设置密码" : "输入密码", l.autocomplete = t ? "new-password" : "current-password", l.setAttribute("aria-label", "密码");
|
|
585
|
+
const d = document.createElement("input");
|
|
586
|
+
d.type = "password", d.required = t, d.placeholder = "再次输入密码", d.autocomplete = "new-password", d.setAttribute("aria-label", "确认密码");
|
|
587
|
+
const c = document.createElement("p");
|
|
588
|
+
c.setAttribute("role", "alert");
|
|
589
|
+
const m = document.createElement("button");
|
|
590
|
+
m.type = "submit", m.textContent = t ? "确认锁定" : "确认解锁";
|
|
591
|
+
const g = document.createElement("button");
|
|
592
|
+
g.type = "button", g.textContent = "取消", g.addEventListener("click", () => r.close()), r.addEventListener("close", () => {
|
|
593
|
+
l.value = "", d.value = "", r.remove(), this.passwordDialog === r && (this.passwordDialog = void 0);
|
|
594
|
+
});
|
|
595
|
+
const p = this.selectionAnchor && { ...this.selectionAnchor }, M = { ...this.selectionFocus };
|
|
596
|
+
n.addEventListener("submit", async (E) => {
|
|
597
|
+
if (E.preventDefault(), !m.disabled) {
|
|
598
|
+
if (t && l.value !== d.value) {
|
|
599
|
+
c.textContent = "两次输入的密码不一致";
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
m.disabled = !0, g.disabled = !0, r.oncancel = (R) => R.preventDefault(), c.textContent = "正在处理…";
|
|
603
|
+
try {
|
|
604
|
+
this.selectionAnchor = p, this.selectionFocus = M, await this.setSelectionLocked(t, l.value), r.close();
|
|
605
|
+
} catch (R) {
|
|
606
|
+
c.textContent = R instanceof Error ? R.message : "操作失败,请重试", l.value = "", d.value = "", l.focus();
|
|
607
|
+
} finally {
|
|
608
|
+
m.disabled = !1, g.disabled = !1, r.oncancel = null;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}), n.append(i, l), t && n.append(d), n.append(c, m, g), r.append(n), this.root.append(r), r.showModal(), l.focus();
|
|
612
|
+
}
|
|
613
|
+
closeLockMessage() {
|
|
614
|
+
const t = this.messageDialog;
|
|
615
|
+
this.messageDialog = void 0, t == null || t.close(), t == null || t.remove();
|
|
616
|
+
}
|
|
617
|
+
showLockMessage(t) {
|
|
618
|
+
if (this.destroyed) return;
|
|
619
|
+
this.closeLockMessage(), this.hideContextMenu();
|
|
620
|
+
const e = document.createElement("dialog");
|
|
621
|
+
this.messageDialog = e, e.className = "colwork-table__message-dialog", e.setAttribute("role", "alertdialog"), e.setAttribute("aria-label", "操作提示"), e.setAttribute("aria-description", t);
|
|
622
|
+
const s = document.createElement("h3");
|
|
623
|
+
s.textContent = "操作提示";
|
|
624
|
+
const o = document.createElement("p");
|
|
625
|
+
o.className = "colwork-table__lock-status", o.setAttribute("role", "status"), o.textContent = t;
|
|
626
|
+
const r = document.createElement("button");
|
|
627
|
+
r.type = "button", r.textContent = "确定", r.addEventListener("click", () => e.close()), e.addEventListener("close", () => {
|
|
628
|
+
e.remove(), this.messageDialog === e && (this.messageDialog = void 0);
|
|
629
|
+
}), e.append(s, o, r), this.root.append(e), e.showModal(), r.focus();
|
|
630
|
+
}
|
|
631
|
+
get rowCount() {
|
|
632
|
+
return this.rowCountValue;
|
|
633
|
+
}
|
|
634
|
+
get columnCount() {
|
|
635
|
+
return this.columnCountValue;
|
|
636
|
+
}
|
|
637
|
+
get rowHeaderWidth() {
|
|
638
|
+
return yt * this.zoomRatio;
|
|
639
|
+
}
|
|
640
|
+
get columnHeaderHeight() {
|
|
641
|
+
return bt * this.zoomRatio;
|
|
642
|
+
}
|
|
643
|
+
getRowHeight(t) {
|
|
644
|
+
var e;
|
|
645
|
+
return ((e = this.resizing) == null ? void 0 : e.axis) === "row" && this.resizing.index === t ? this.resizing.currentSize : (this.rowHeights.get(String(t)) ?? St) * this.zoomRatio;
|
|
646
|
+
}
|
|
647
|
+
getColumnWidth(t) {
|
|
648
|
+
var e;
|
|
649
|
+
return ((e = this.resizing) == null ? void 0 : e.axis) === "column" && this.resizing.index === t ? this.resizing.currentSize : (this.columnWidths.get(String(t)) ?? Ct) * this.zoomRatio;
|
|
650
|
+
}
|
|
651
|
+
beginResize(t, e, s) {
|
|
652
|
+
s.preventDefault(), s.stopPropagation(), !(s.button !== 0 || this.readOnly || this.isAxisLocked(t, e)) && (this.finishEdit(!0), this.selecting = !1, this.headerSelecting = void 0, this.resizing = {
|
|
653
|
+
axis: t,
|
|
654
|
+
index: e,
|
|
655
|
+
start: t === "row" ? s.clientY : s.clientX,
|
|
656
|
+
size: t === "row" ? this.getRowHeight(e) : this.getColumnWidth(e),
|
|
657
|
+
currentSize: t === "row" ? this.getRowHeight(e) : this.getColumnWidth(e)
|
|
658
|
+
}, this.viewport.classList.add("is-resizing"));
|
|
659
|
+
}
|
|
660
|
+
addResizeHandle(t, e, s) {
|
|
661
|
+
const o = document.createElement("span");
|
|
662
|
+
o.className = `colwork-table__resize-handle colwork-table__resize-handle--${e}`, o.addEventListener("mousedown", (r) => this.beginResize(e, s, r)), t.append(o);
|
|
663
|
+
}
|
|
664
|
+
selectRange(t, e, s) {
|
|
665
|
+
this.selectionAnchor = t, this.selectionFocus = e, this.selectionMode = s, this.selectedKey = W(t), this.pendingAwarenessCursor = e, this.refreshSelection();
|
|
666
|
+
}
|
|
667
|
+
hideContextMenu() {
|
|
668
|
+
var t, e;
|
|
669
|
+
(t = this.protectionMenuButton) == null || t.setAttribute("aria-expanded", "false"), (e = this.contextMenu) == null || e.remove(), this.contextMenu = void 0;
|
|
670
|
+
}
|
|
671
|
+
showContextMenu(t, e, s) {
|
|
672
|
+
var n;
|
|
673
|
+
this.hideContextMenu();
|
|
674
|
+
const o = document.createElement("div");
|
|
675
|
+
o.className = "colwork-table__context-menu", o.setAttribute("aria-label", "表格操作"), s.forEach(({ label: i, actions: l }) => {
|
|
676
|
+
const d = document.createElement("div");
|
|
677
|
+
d.className = "colwork-table__context-group", d.setAttribute("role", "group"), d.setAttribute("aria-label", i);
|
|
678
|
+
const c = document.createElement("div");
|
|
679
|
+
c.className = "colwork-table__context-label", c.textContent = i, d.append(c), l.forEach(([m, g]) => {
|
|
680
|
+
const p = document.createElement("button");
|
|
681
|
+
p.type = "button", p.textContent = m, (m.startsWith("删除") || m === "清空内容") && (p.className = "is-destructive"), p.addEventListener("click", () => {
|
|
682
|
+
this.hideContextMenu(), g();
|
|
683
|
+
}), d.append(p);
|
|
684
|
+
}), o.append(d);
|
|
685
|
+
}), o.addEventListener("keydown", (i) => {
|
|
686
|
+
var m;
|
|
687
|
+
if (i.key === "Escape") {
|
|
688
|
+
i.preventDefault(), this.hideContextMenu(), this.viewport.focus({ preventScroll: !0 });
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
const l = Array.from(o.querySelectorAll("button")), d = l.indexOf(document.activeElement);
|
|
692
|
+
let c;
|
|
693
|
+
if (i.key === "ArrowDown") c = (d + 1) % l.length;
|
|
694
|
+
else if (i.key === "ArrowUp") c = (d - 1 + l.length) % l.length;
|
|
695
|
+
else if (i.key === "Home") c = 0;
|
|
696
|
+
else if (i.key === "End") c = l.length - 1;
|
|
697
|
+
else return;
|
|
698
|
+
i.preventDefault(), (m = l[c]) == null || m.focus();
|
|
699
|
+
}), o.style.left = `${t}px`, o.style.top = `${e}px`, document.body.append(o);
|
|
700
|
+
const r = o.getBoundingClientRect();
|
|
701
|
+
o.style.left = `${Math.max(8, Math.min(t, window.innerWidth - r.width - 8))}px`, o.style.top = `${Math.max(8, Math.min(e, window.innerHeight - r.height - 8))}px`, this.contextMenu = o, (n = o.querySelector("button")) == null || n.focus({ preventScroll: !0 });
|
|
702
|
+
}
|
|
703
|
+
protectionActions() {
|
|
704
|
+
return [
|
|
705
|
+
["锁定选区", () => this.requestSelectionLock(!0)],
|
|
706
|
+
["密码锁定选区", () => this.requestSelectionLock(!0, !0)],
|
|
707
|
+
["解锁选区", () => this.requestSelectionLock(!1)]
|
|
708
|
+
];
|
|
709
|
+
}
|
|
710
|
+
hasHeaderSelection(t, e) {
|
|
711
|
+
const s = $(this.selectionAnchor, this.selectionFocus);
|
|
712
|
+
return this.selectionMode === t && (t === "row" ? e >= s.start.row && e <= s.end.row : e >= s.start.column && e <= s.end.column);
|
|
713
|
+
}
|
|
714
|
+
openRowContextMenu(t, e) {
|
|
715
|
+
if (this.readOnly) return;
|
|
716
|
+
t.preventDefault(), t.stopPropagation(), this.hasHeaderSelection("row", e) || this.selectHeaderRange("row", e, !1);
|
|
717
|
+
const s = $(this.selectionAnchor, this.selectionFocus), o = s.end.row - s.start.row + 1;
|
|
718
|
+
this.showContextMenu(t.clientX, t.clientY, [
|
|
719
|
+
{ label: "行操作", actions: [
|
|
720
|
+
[`在上方插入 ${o} 行`, () => this.modifyAxis("row", s.start.row, o)],
|
|
721
|
+
[`在下方插入 ${o} 行`, () => this.modifyAxis("row", s.end.row + 1, o)],
|
|
722
|
+
[`删除 ${o} 行`, () => this.deleteRows()]
|
|
723
|
+
] },
|
|
724
|
+
{ label: "固定", actions: [
|
|
725
|
+
["固定至此行", () => this.freezeSelection("row")],
|
|
726
|
+
["取消固定", () => this.setFrozenPanes(0, 0)]
|
|
727
|
+
] },
|
|
728
|
+
{ label: "保护", actions: this.protectionActions() }
|
|
729
|
+
]);
|
|
730
|
+
}
|
|
731
|
+
openColumnContextMenu(t, e) {
|
|
732
|
+
if (this.readOnly) return;
|
|
733
|
+
t.preventDefault(), t.stopPropagation(), this.hasHeaderSelection("column", e) || this.selectHeaderRange("column", e, !1);
|
|
734
|
+
const s = $(this.selectionAnchor, this.selectionFocus), o = s.end.column - s.start.column + 1;
|
|
735
|
+
this.showContextMenu(t.clientX, t.clientY, [
|
|
736
|
+
{ label: "列操作", actions: [
|
|
737
|
+
[`在左侧插入 ${o} 列`, () => this.modifyAxis("column", s.start.column, o)],
|
|
738
|
+
[`在右侧插入 ${o} 列`, () => this.modifyAxis("column", s.end.column + 1, o)],
|
|
739
|
+
[`删除 ${o} 列`, () => this.deleteColumns()]
|
|
740
|
+
] },
|
|
741
|
+
{ label: "固定", actions: [
|
|
742
|
+
["固定至此列", () => this.freezeSelection("column")],
|
|
743
|
+
["取消固定", () => this.setFrozenPanes(0, 0)]
|
|
744
|
+
] },
|
|
745
|
+
{ label: "保护", actions: this.protectionActions() }
|
|
746
|
+
]);
|
|
747
|
+
}
|
|
748
|
+
mergeKey(t) {
|
|
749
|
+
return `${t.start.row}:${t.start.column}-${t.end.row}:${t.end.column}`;
|
|
750
|
+
}
|
|
751
|
+
parseMergeKey(t) {
|
|
752
|
+
const e = t.match(/^(\d+):(\d+)-(\d+):(\d+)$/);
|
|
753
|
+
if (e)
|
|
754
|
+
return {
|
|
755
|
+
start: { row: Number(e[1]), column: Number(e[2]) },
|
|
756
|
+
end: { row: Number(e[3]), column: Number(e[4]) }
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
getMergeRanges() {
|
|
760
|
+
return Array.from(this.merges.keys()).flatMap((t) => {
|
|
761
|
+
const e = this.parseMergeKey(t);
|
|
762
|
+
return e ? [e] : [];
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
expandSelectionToMerges(t, e) {
|
|
766
|
+
let s = $(t, e);
|
|
767
|
+
for (const n of this.getMergeRanges())
|
|
768
|
+
n.start.row <= s.end.row && n.end.row >= s.start.row && n.start.column <= s.end.column && n.end.column >= s.start.column && (s = {
|
|
769
|
+
start: { row: Math.min(s.start.row, n.start.row), column: Math.min(s.start.column, n.start.column) },
|
|
770
|
+
end: { row: Math.max(s.end.row, n.end.row), column: Math.max(s.end.column, n.end.column) }
|
|
771
|
+
});
|
|
772
|
+
const o = e.row >= t.row, r = e.column >= t.column;
|
|
773
|
+
return {
|
|
774
|
+
anchor: {
|
|
775
|
+
row: o ? s.start.row : s.end.row,
|
|
776
|
+
column: r ? s.start.column : s.end.column
|
|
777
|
+
},
|
|
778
|
+
focus: {
|
|
779
|
+
row: o ? s.end.row : s.start.row,
|
|
780
|
+
column: r ? s.end.column : s.start.column
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
getMergeStartingAt(t, e) {
|
|
785
|
+
return this.getMergeRanges().find((s) => s.start.row === t && s.start.column === e);
|
|
786
|
+
}
|
|
787
|
+
isMergeCovered(t, e) {
|
|
788
|
+
return this.getMergeRanges().some((s) => T(s, { row: t, column: e }) && (s.start.row !== t || s.start.column !== e));
|
|
789
|
+
}
|
|
790
|
+
mergeSelection() {
|
|
791
|
+
if (this.readOnly || this.selectionHasLocks()) return;
|
|
792
|
+
const t = $(this.selectionAnchor, this.selectionFocus);
|
|
793
|
+
if (!(t.start.row === t.end.row && t.start.column === t.end.column)) {
|
|
794
|
+
if (this.crossesFrozenBoundary(t)) {
|
|
795
|
+
this.showLockMessage("合并范围同时包含固定和非固定区域,无法合并,请调整选区或取消固定");
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
this.closeLockMessage(), this.doc.transact(() => {
|
|
799
|
+
this.getMergeRanges().forEach((e) => {
|
|
800
|
+
(T(t, e.start) || T(e, t.start)) && this.merges.delete(this.mergeKey(e));
|
|
801
|
+
}), this.merges.set(this.mergeKey(t), "1");
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
unmergeSelection() {
|
|
806
|
+
if (this.readOnly || this.selectionHasLocks()) return;
|
|
807
|
+
const t = $(this.selectionAnchor, this.selectionFocus);
|
|
808
|
+
this.doc.transact(() => this.getMergeRanges().forEach((e) => {
|
|
809
|
+
(T(t, e.start) || T(e, t.start)) && this.merges.delete(this.mergeKey(e));
|
|
810
|
+
}));
|
|
811
|
+
}
|
|
812
|
+
modifyAxis(t, e, s, o = 0) {
|
|
813
|
+
if (this.readOnly || s === 0 || this.isAxisLocked(t, e, !0)) return;
|
|
814
|
+
const r = [this.cells, this.styles];
|
|
815
|
+
this.doc.transact(() => {
|
|
816
|
+
r.forEach((i) => {
|
|
817
|
+
const l = Array.from(i.entries());
|
|
818
|
+
i.clear(), l.forEach(([d, c]) => {
|
|
819
|
+
const [m, g] = d.split(":").map(Number), p = t === "row" ? m : g;
|
|
820
|
+
if (o && p >= e && p < e + o) return;
|
|
821
|
+
const M = p >= e + o ? p + s : p, E = W(t === "row" ? { row: M, column: g } : { row: m, column: M });
|
|
822
|
+
i.set(E, c);
|
|
823
|
+
});
|
|
824
|
+
});
|
|
825
|
+
const n = this.getMergeRanges();
|
|
826
|
+
this.merges.clear(), n.forEach((i) => {
|
|
827
|
+
const l = t === "row" ? i.start.row : i.start.column, d = t === "row" ? i.end.row : i.end.column;
|
|
828
|
+
if (o && l < e + o && d >= e) return;
|
|
829
|
+
const c = l >= e + o ? s : 0, m = t === "row" ? { start: { row: i.start.row + c, column: i.start.column }, end: { row: i.end.row + c, column: i.end.column } } : { start: { row: i.start.row, column: i.start.column + c }, end: { row: i.end.row, column: i.end.column + c } };
|
|
830
|
+
this.merges.set(this.mergeKey(m), "1");
|
|
831
|
+
});
|
|
832
|
+
}), t === "row" ? this.rowCountValue = Math.max(1, this.rowCountValue + s) : this.columnCountValue = Math.max(1, this.columnCountValue + s), this.render();
|
|
833
|
+
}
|
|
834
|
+
deleteRows() {
|
|
835
|
+
const t = $(this.selectionAnchor, this.selectionFocus), e = Math.min(t.end.row - t.start.row + 1, this.rowCount - 1);
|
|
836
|
+
this.modifyAxis("row", t.start.row, -e, e);
|
|
837
|
+
}
|
|
838
|
+
deleteColumns() {
|
|
839
|
+
const t = $(this.selectionAnchor, this.selectionFocus), e = Math.min(t.end.column - t.start.column + 1, this.columnCount - 1);
|
|
840
|
+
this.modifyAxis("column", t.start.column, -e, e);
|
|
841
|
+
}
|
|
842
|
+
selectHeaderRange(t, e, s) {
|
|
843
|
+
var r, n;
|
|
844
|
+
let o = e;
|
|
845
|
+
s && this.selectionMode === t && (o = t === "row" ? ((r = this.selectionAnchor) == null ? void 0 : r.row) ?? e : ((n = this.selectionAnchor) == null ? void 0 : n.column) ?? e), this.selectHeaderSpan(t, o, e);
|
|
846
|
+
}
|
|
847
|
+
selectHeaderSpan(t, e, s) {
|
|
848
|
+
t === "row" ? this.selectRange({ row: e, column: 0 }, { row: s, column: this.columnCount - 1 }, "row") : this.selectRange({ row: 0, column: e }, { row: this.rowCount - 1, column: s }, "column");
|
|
849
|
+
}
|
|
850
|
+
addHeaderSelection(t, e, s) {
|
|
851
|
+
t.dataset.selectionAxis = e, t.dataset.selectionIndex = String(s), t.addEventListener("mousedown", (o) => {
|
|
852
|
+
o.button === 0 && (o.preventDefault(), o.stopPropagation(), this.finishEdit(!0), this.viewport.focus({ preventScroll: !0 }), this.hideContextMenu(), this.selectHeaderRange(e, s, o.shiftKey), this.headerSelecting = { axis: e, anchor: this.selectionAnchor[e] }, this.selecting = !0);
|
|
853
|
+
}), t.addEventListener("mouseenter", (o) => {
|
|
854
|
+
if (!(!this.selecting || !this.headerSelecting || this.headerSelecting.axis !== e)) {
|
|
855
|
+
if (!(o.buttons & 1)) {
|
|
856
|
+
this.onWindowBlur();
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
this.selectHeaderSpan(e, this.headerSelecting.anchor, s);
|
|
860
|
+
}
|
|
861
|
+
}), t.addEventListener("contextmenu", (o) => e === "row" ? this.openRowContextMenu(o, s) : this.openColumnContextMenu(o, s));
|
|
862
|
+
}
|
|
863
|
+
expandLogicalSpace() {
|
|
864
|
+
const t = this.viewport.scrollTop + this.viewport.clientHeight >= this.viewport.scrollHeight - 280, e = this.viewport.scrollLeft + this.viewport.clientWidth >= this.viewport.scrollWidth - 280, s = t, o = e;
|
|
865
|
+
if (t && (this.rowCountValue += 100), e && (this.columnCountValue += 5), this.selectionMode && (s || o)) {
|
|
866
|
+
const r = this.selectionFocus ?? { row: 0, column: 0 };
|
|
867
|
+
this.selectionFocus = at(this.selectionMode, r, this.rowCount, this.columnCount), this.publishSelection();
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
publishSelection() {
|
|
871
|
+
this.updateAwareness();
|
|
872
|
+
}
|
|
873
|
+
updateAwareness(t = {}) {
|
|
874
|
+
const e = this.awareness.getLocalState() ?? {};
|
|
875
|
+
this.awareness.setLocalState({
|
|
876
|
+
...e,
|
|
877
|
+
...t,
|
|
878
|
+
selection: { anchor: this.selectionAnchor, focus: this.selectionFocus, mode: this.selectionMode }
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
expandForRemoteSelections() {
|
|
882
|
+
let t = this.rowCount, e = this.columnCount;
|
|
883
|
+
this.awareness.getStates().forEach((o, r) => {
|
|
884
|
+
if (r === this.awareness.clientID) return;
|
|
885
|
+
const n = o.selection;
|
|
886
|
+
!(n != null && n.anchor) || !n.focus || (t = Math.max(t, n.anchor.row + 1, n.focus.row + 1), e = Math.max(e, n.anchor.column + 1, n.focus.column + 1));
|
|
887
|
+
});
|
|
888
|
+
const s = t !== this.rowCount || e !== this.columnCount;
|
|
889
|
+
return this.rowCountValue = t, this.columnCountValue = e, s;
|
|
890
|
+
}
|
|
891
|
+
getSelectionBounds() {
|
|
892
|
+
const t = this.selectionAnchor ?? this.selectionFocus ?? { row: 0, column: 0 }, e = this.selectionFocus ?? t, s = $(t, e);
|
|
893
|
+
return { top: s.start.row, bottom: s.end.row, left: s.start.column, right: s.end.column };
|
|
894
|
+
}
|
|
895
|
+
selectCell(t, e, s = !1) {
|
|
896
|
+
const o = { row: t, column: e };
|
|
897
|
+
this.selectionMode = void 0, (!s || !this.selectionAnchor) && (this.selectionAnchor = o);
|
|
898
|
+
const r = this.expandSelectionToMerges(this.selectionAnchor, o);
|
|
899
|
+
this.selectionAnchor = r.anchor, this.selectionFocus = r.focus, this.selectedKey = `${t}:${e}`, this.pendingAwarenessCursor = o;
|
|
900
|
+
}
|
|
901
|
+
isPositionSelected(t) {
|
|
902
|
+
return T($(this.selectionAnchor, this.selectionFocus), t);
|
|
903
|
+
}
|
|
904
|
+
selectionToClipboard() {
|
|
905
|
+
if (!this.selectionFocus) return;
|
|
906
|
+
const t = $(this.selectionAnchor, this.selectionFocus), e = [], s = [];
|
|
907
|
+
for (let o = t.start.row; o <= t.end.row; o += 1) {
|
|
908
|
+
const r = [], n = [];
|
|
909
|
+
for (let i = t.start.column; i <= t.end.column; i += 1) {
|
|
910
|
+
const l = W({ row: o, column: i });
|
|
911
|
+
r.push(this.cells.get(l) ?? "");
|
|
912
|
+
const d = this.readStyle(l);
|
|
913
|
+
n.push(Object.keys(d).length ? d : null);
|
|
914
|
+
}
|
|
915
|
+
e.push(r), s.push(n);
|
|
916
|
+
}
|
|
917
|
+
return { values: e, styles: s };
|
|
918
|
+
}
|
|
919
|
+
selectionToText() {
|
|
920
|
+
var t;
|
|
921
|
+
return ((t = this.selectionToClipboard()) == null ? void 0 : t.values.map((e) => e.join(" ")).join(`
|
|
922
|
+
`)) ?? "";
|
|
923
|
+
}
|
|
924
|
+
copySelection() {
|
|
925
|
+
var e;
|
|
926
|
+
const t = this.selectionToText();
|
|
927
|
+
t && ((e = navigator.clipboard) == null || e.writeText(t));
|
|
928
|
+
}
|
|
929
|
+
clearSelectionContents() {
|
|
930
|
+
if (this.readOnly || this.selectionHasLocks()) return;
|
|
931
|
+
const t = $(this.selectionAnchor, this.selectionFocus);
|
|
932
|
+
this.doc.transact(() => {
|
|
933
|
+
for (let e = t.start.row; e <= t.end.row; e += 1)
|
|
934
|
+
for (let s = t.start.column; s <= t.end.column; s += 1) this.cells.delete(W({ row: e, column: s }));
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
showCellContextMenu(t, e) {
|
|
938
|
+
this.readOnly || this.showContextMenu(t, e, [
|
|
939
|
+
{ label: "编辑", actions: [
|
|
940
|
+
["复制", () => this.copySelection()],
|
|
941
|
+
["清空内容", () => this.clearSelectionContents()]
|
|
942
|
+
] },
|
|
943
|
+
{ label: "单元格", actions: [
|
|
944
|
+
["合并单元格", () => this.mergeSelection()],
|
|
945
|
+
["取消合并单元格", () => this.unmergeSelection()]
|
|
946
|
+
] },
|
|
947
|
+
{ label: "固定", actions: [
|
|
948
|
+
["固定至选中行", () => this.freezeSelection("row")],
|
|
949
|
+
["固定至选中列", () => this.freezeSelection("column")],
|
|
950
|
+
["取消固定", () => this.setFrozenPanes(0, 0)]
|
|
951
|
+
] },
|
|
952
|
+
{ label: "保护", actions: this.protectionActions() }
|
|
953
|
+
]);
|
|
954
|
+
}
|
|
955
|
+
openCellContextMenu(t) {
|
|
956
|
+
if (!this.readOnly && (t.preventDefault(), t.stopPropagation(), this.pendingCellContext = { x: t.clientX, y: t.clientY }, this.rightButtonReleased)) {
|
|
957
|
+
this.pendingCellContext = void 0;
|
|
958
|
+
const e = this.rightMouseUpPoint ?? { x: t.clientX, y: t.clientY };
|
|
959
|
+
this.showCellContextMenu(e.x, e.y);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
sendAwareness(t) {
|
|
963
|
+
this.pendingAwarenessCursor = void 0, this.updateAwareness({ cursor: t, editing: this.editingKey ?? null });
|
|
964
|
+
}
|
|
965
|
+
logChunk(t, e) {
|
|
966
|
+
const s = Array.from(e, (o) => o.toString(16).padStart(2, "0")).join(" ");
|
|
967
|
+
this.log(`${t} bytes=${e.byteLength} hex=${s}`);
|
|
968
|
+
}
|
|
969
|
+
inspectConnections() {
|
|
970
|
+
var e, s;
|
|
971
|
+
const t = this.provider;
|
|
972
|
+
(e = t.signalingConns) == null || e.forEach((o) => {
|
|
973
|
+
if (this.loggedSignals.has(o)) return;
|
|
974
|
+
this.loggedSignals.add(o);
|
|
975
|
+
const r = o.send.bind(o);
|
|
976
|
+
o.send = (n) => {
|
|
977
|
+
this.log(`signal:send ${JSON.stringify(n)}`), r(n);
|
|
978
|
+
}, o.on("message", (n) => this.log(`signal:recv ${JSON.stringify(n)}`)), o.on("connect", () => this.log("signal:connect")), o.on("disconnect", () => this.log("signal:disconnect")), o.on("error", (n) => this.log(`signal:error ${String(n)}`));
|
|
979
|
+
}), (s = t.room) == null || s.webrtcConns.forEach((o, r) => {
|
|
980
|
+
const n = o.peer;
|
|
981
|
+
if (this.loggedPeers.has(n)) return;
|
|
982
|
+
this.loggedPeers.add(n);
|
|
983
|
+
const i = n.send.bind(n);
|
|
984
|
+
n.send = (l) => {
|
|
985
|
+
this.logChunk(`webrtc:send peer=${r}`, l), i(l);
|
|
986
|
+
}, n.on("data", (l) => this.logChunk(`webrtc:recv peer=${r}`, l)), n.on("connect", () => this.log(`webrtc:connect peer=${r}`)), n.on("close", () => this.log(`webrtc:close peer=${r}`)), n.on("error", (l) => this.log(`webrtc:error peer=${r} ${String(l)}`));
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
seed(t) {
|
|
990
|
+
t.forEach((e, s) => e.forEach((o, r) => {
|
|
991
|
+
const n = `${s}:${r}`;
|
|
992
|
+
!this.cells.has(n) && !this.isCellLocked(n) && this.cells.set(n, o);
|
|
993
|
+
}));
|
|
994
|
+
}
|
|
995
|
+
seedDemoFormatting() {
|
|
996
|
+
this.doc.transact(() => {
|
|
997
|
+
this.cells.set("0:0", `设计协作表格
|
|
998
|
+
多人协作工作区`), this.cells.delete("0:1"), this.cells.delete("0:2"), this.merges.set("0:0-0:2", "1"), this.styles.set("0:0", JSON.stringify({ bold: !0, fontSize: 16, color: "#0f172a", background: "#bae6fd", align: "center", wrap: !0, border: { top: !0, right: !0, bottom: !0, left: !0 } })), this.styles.set("1:0", JSON.stringify({ bold: !0, underline: !0, background: "#fef3c7", border: { bottom: !0 } })), this.styles.set("1:1", JSON.stringify({ italic: !0, color: "#7c3aed" })), this.styles.set("1:2", JSON.stringify({ strike: !0, color: "#b91c1c" })), this.cells.set("2:0", "125000"), this.styles.set("2:0", JSON.stringify({ format: "number", fontSize: 14, border: { top: !0, bottom: !0 } })), this.cells.set("2:1", "0.72"), this.styles.set("2:1", JSON.stringify({ format: "percent", color: "#047857", align: "right" })), this.cells.set("2:2", "2026-08-09"), this.styles.set("2:2", JSON.stringify({ format: "date", background: "#dcfce7" })), this.cells.set("3:0", `这是一段较长的演示内容
|
|
999
|
+
可使用自动换行工具查看`), this.styles.set("3:0", JSON.stringify({ wrap: !0, background: "#f1f5f9", border: { top: !0, right: !0, bottom: !0, left: !0 } })), this.cells.set("4:0", "删除线示例"), this.styles.set("4:0", JSON.stringify({ strike: !0, color: "#64748b" })), this.cells.set("5:0", "合并区域示例"), this.cells.delete("5:1"), this.cells.delete("5:2"), this.merges.set("5:0-5:2", "1"), this.styles.set("5:0", JSON.stringify({ bold: !0, align: "center", background: "#ede9fe", border: { top: !0, right: !0, bottom: !0, left: !0 } })), this.rowHeights.set("0", 72), this.rowHeights.set("3", 72), this.rowHeights.set("5", 44), this.columnWidths.set("0", 220), this.columnWidths.set("1", 130), this.columnWidths.set("2", 130);
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
expandLogicalSizeFromDocument() {
|
|
1003
|
+
const t = [...this.cells.keys(), ...this.styles.keys(), ...this.locks.keys(), ...this.rowHeights.keys(), ...this.columnWidths.keys()].map((e) => e.split(":").map(Number)).filter(([e, s]) => Number.isInteger(e) && Number.isInteger(s));
|
|
1004
|
+
t.length && (this.rowCountValue = Math.max(this.rowCount, ...t.map(([e]) => e + 1)), this.columnCountValue = Math.max(this.columnCount, ...t.map(([, e]) => e + 1)));
|
|
1005
|
+
}
|
|
1006
|
+
render() {
|
|
1007
|
+
this.editor && this.finishEdit(!0);
|
|
1008
|
+
const t = this.columnCount, e = this.rowCount;
|
|
1009
|
+
this.frozenRows = Math.min(this.frozenRows, e - 1), this.frozenColumns = Math.min(this.frozenColumns, t - 1);
|
|
1010
|
+
const s = this.hasPartialMerge("row", 0, this.frozenRows - 1), o = this.hasPartialMerge("column", 0, this.frozenColumns - 1);
|
|
1011
|
+
(s || o) && (s && (this.frozenRows = 0), o && (this.frozenColumns = 0), this.showLockMessage("合并区域已变化,已取消与合并块冲突的固定行列"));
|
|
1012
|
+
const r = (u) => this.getRowHeight(u), n = (u) => this.getColumnWidth(u), i = zt({
|
|
1013
|
+
scrollTop: this.viewport.scrollTop,
|
|
1014
|
+
scrollLeft: this.viewport.scrollLeft,
|
|
1015
|
+
clientHeight: this.viewport.clientHeight || 560,
|
|
1016
|
+
clientWidth: this.viewport.clientWidth || 900,
|
|
1017
|
+
rowCount: e,
|
|
1018
|
+
columnCount: t,
|
|
1019
|
+
rowSize: r,
|
|
1020
|
+
columnSize: n,
|
|
1021
|
+
overscan: 4
|
|
1022
|
+
});
|
|
1023
|
+
let l = !0;
|
|
1024
|
+
const d = this.getMergeRanges();
|
|
1025
|
+
for (; l; ) {
|
|
1026
|
+
l = !1;
|
|
1027
|
+
for (const u of d) {
|
|
1028
|
+
const y = u.start.row <= i.bottom && (u.end.row >= i.top || u.start.row < this.frozenRows), b = u.start.column <= i.right && (u.end.column >= i.left || u.start.column < this.frozenColumns);
|
|
1029
|
+
!y || !b || (u.start.row >= this.frozenRows && u.start.row < i.top && (i.top = u.start.row, l = !0), u.start.column >= this.frozenColumns && u.start.column < i.left && (i.left = u.start.column, l = !0), u.end.row > i.bottom && (i.bottom = u.end.row, l = !0), u.end.column > i.right && (i.right = u.end.column, l = !0));
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
i.bottom = Math.max(i.bottom, this.frozenRows - 1), i.right = Math.max(i.right, this.frozenColumns - 1);
|
|
1033
|
+
const c = Math.max(this.frozenRows, i.top), m = Math.max(this.frozenColumns, i.left), g = (u, y, b) => [
|
|
1034
|
+
...Array.from({ length: u }, (L, C) => C),
|
|
1035
|
+
-1,
|
|
1036
|
+
// spacer between the frozen prefix and the virtual window
|
|
1037
|
+
...Array.from({ length: Math.max(0, b - y + 1) }, (L, C) => y + C)
|
|
1038
|
+
], p = g(this.frozenRows, c, i.bottom), M = g(this.frozenColumns, m, i.right), E = P(m, n) - P(this.frozenColumns, n), R = P(c, r) - P(this.frozenRows, r), H = (u, y, b) => {
|
|
1039
|
+
const L = y >= 0 && y < this.frozenRows, C = b >= 0 && b < this.frozenColumns;
|
|
1040
|
+
!L && !C || (u.classList.add("is-frozen"), u.style.position = "sticky", L && (u.style.top = `${this.columnHeaderHeight + P(y, r)}px`), C && (u.style.left = `${this.rowHeaderWidth + P(b, n)}px`), u.style.zIndex = String(y < 0 ? 10 : b < 0 ? 8 : L && C ? 5 : L ? 4 : 3));
|
|
1041
|
+
}, q = document.createElement("thead"), O = document.createElement("tr"), z = document.createElement("th");
|
|
1042
|
+
z.style.width = `${this.rowHeaderWidth}px`, z.style.height = `${this.columnHeaderHeight}px`, z.addEventListener("mousedown", (u) => {
|
|
1043
|
+
u.preventDefault(), u.stopPropagation(), this.selectRange({ row: 0, column: 0 }, { row: e - 1, column: t - 1 }, "all");
|
|
1044
|
+
}), O.append(z);
|
|
1045
|
+
const U = (u) => {
|
|
1046
|
+
const y = document.createElement("th");
|
|
1047
|
+
return y.style.width = `${u}px`, y.style.padding = "0", y.style.border = "0", y;
|
|
1048
|
+
};
|
|
1049
|
+
for (const u of M) {
|
|
1050
|
+
if (u === -1) {
|
|
1051
|
+
O.append(U(E));
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
const y = document.createElement("th");
|
|
1055
|
+
y.style.width = `${n(u)}px`, y.style.height = `${this.columnHeaderHeight}px`, y.style.fontSize = `${et * this.zoomRatio}px`;
|
|
1056
|
+
const b = document.createElement("b");
|
|
1057
|
+
b.className = "colwork-table__column-letter", b.textContent = At(u), y.append(b), this.addResizeHandle(y, "column", u), this.addHeaderSelection(y, "column", u), H(y, -1, u), O.append(y);
|
|
1058
|
+
}
|
|
1059
|
+
O.append(U(J(t, n) - P(i.right + 1, n))), q.append(O);
|
|
1060
|
+
const D = document.createElement("tbody"), V = (u) => {
|
|
1061
|
+
const y = document.createElement("tr");
|
|
1062
|
+
y.className = "colwork-table__spacer", y.style.height = `${u}px`;
|
|
1063
|
+
const b = document.createElement("td");
|
|
1064
|
+
return b.colSpan = M.length + 2, b.style.height = `${u}px`, y.append(b), y;
|
|
1065
|
+
};
|
|
1066
|
+
for (const u of p) {
|
|
1067
|
+
if (u === -1) {
|
|
1068
|
+
D.append(V(R));
|
|
1069
|
+
continue;
|
|
1070
|
+
}
|
|
1071
|
+
const y = document.createElement("tr");
|
|
1072
|
+
this.bandedRows && u % 2 === 1 && (y.className = "is-banded-row"), y.style.height = `${r(u)}px`;
|
|
1073
|
+
const b = document.createElement("th");
|
|
1074
|
+
b.style.width = `${this.rowHeaderWidth}px`, b.style.height = `${r(u)}px`, b.style.fontSize = `${et * this.zoomRatio}px`, b.textContent = String(u + 1), this.addResizeHandle(b, "row", u), this.addHeaderSelection(b, "row", u), y.append(b), H(b, u, -1);
|
|
1075
|
+
for (const C of M) {
|
|
1076
|
+
if (C === -1) {
|
|
1077
|
+
const A = document.createElement("td");
|
|
1078
|
+
A.style.width = `${E}px`, A.style.padding = "0", A.style.border = "0", y.append(A);
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
if (this.isMergeCovered(u, C)) continue;
|
|
1082
|
+
const F = W({ row: u, column: C }), v = document.createElement("td");
|
|
1083
|
+
v.style.width = `${n(C)}px`, v.style.height = `${r(u)}px`, v.dataset.key = F;
|
|
1084
|
+
const x = this.getMergeStartingAt(u, C);
|
|
1085
|
+
x && (v.rowSpan = x.end.row - x.start.row + 1, v.colSpan = x.end.column - x.start.column + 1);
|
|
1086
|
+
const N = document.createElement("span");
|
|
1087
|
+
N.className = "colwork-table__cell-value", N.style.height = `${Math.max(0, r(u) - 4)}px`, N.style.maxHeight = `${Math.max(0, r(u) - 4)}px`, N.textContent = this.displayValue(F), this.applyStyle(N, F), v.append(N), this.applyStyle(v, F), H(v, u, C);
|
|
1088
|
+
const B = this.isCellLocked(F);
|
|
1089
|
+
v.classList.toggle("is-locked", B), v.classList.toggle("is-password-locked", this.isCellPasswordLocked(F)), v.setAttribute("aria-readonly", String(this.readOnly || B)), B && (v.title = this.isCellPasswordLocked(F) ? "密码锁定:验证密码后可编辑" : "已锁定:解锁后可编辑"), v.addEventListener("mousedown", (A) => {
|
|
1090
|
+
A.target instanceof HTMLTextAreaElement || A.button !== 0 && A.button !== 2 || (A.button === 2 ? (this.rightButtonReleased = !1, this.rightDrag = { start: { row: u, column: C }, moved: !1 }) : this.rightDrag = void 0, A.preventDefault(), this.viewport.focus({ preventScroll: !0 }), this.selecting = !0, (A.button === 0 || !this.isPositionSelected({ row: u, column: C })) && this.selectCell(u, C, A.shiftKey), this.refreshSelection());
|
|
1091
|
+
}), v.addEventListener("mouseenter", () => {
|
|
1092
|
+
if (!(!this.selecting || this.headerSelecting)) {
|
|
1093
|
+
if (this.rightDrag) {
|
|
1094
|
+
if (this.rightDrag.start.row === u && this.rightDrag.start.column === C) return;
|
|
1095
|
+
this.rightDrag.moved || (this.rightDrag.moved = !0, this.selectionMode = void 0, this.selectionAnchor = this.rightDrag.start, this.selectedKey = W(this.rightDrag.start));
|
|
1096
|
+
}
|
|
1097
|
+
this.selectCell(u, C, !0), this.refreshSelection();
|
|
1098
|
+
}
|
|
1099
|
+
}), this.readOnly || v.addEventListener("dblclick", () => this.startEdit(v, F, !1)), v.addEventListener("contextmenu", (A) => this.openCellContextMenu(A)), y.append(v);
|
|
1100
|
+
}
|
|
1101
|
+
const L = document.createElement("td");
|
|
1102
|
+
L.style.width = `${J(t, n) - P(i.right + 1, n)}px`, L.style.padding = "0", L.style.border = "0", y.append(L), D.append(y);
|
|
1103
|
+
}
|
|
1104
|
+
D.append(V(J(e, r) - P(i.bottom + 1, r))), this.table.style.setProperty("--colwork-zoom", String(this.zoomRatio)), this.table.style.width = `${this.rowHeaderWidth + J(t, n)}px`, this.table.classList.toggle("is-grid-hidden", !this.showGridLines), this.table.classList.toggle("is-banded-rows", this.bandedRows), this.table.style.fontSize = `${this.zoomRatio}em`, this.table.replaceChildren(q, D), this.refreshFrozenBoundaries(), this.refreshSelection(), this.refreshCursors();
|
|
1105
|
+
}
|
|
1106
|
+
refreshFrozenBoundaries() {
|
|
1107
|
+
this.frozenBoundaryLayer.replaceChildren(), this.frozenBoundaryLayer.style.width = `${this.viewport.clientWidth}px`, this.frozenBoundaryLayer.style.height = `${this.viewport.clientHeight}px`;
|
|
1108
|
+
const t = (e, s, o, r) => {
|
|
1109
|
+
if (!s || o >= r) return;
|
|
1110
|
+
const n = document.createElement("span");
|
|
1111
|
+
n.className = `colwork-table__frozen-boundary colwork-table__frozen-boundary--${e}`, n.setAttribute("aria-hidden", "true"), n.style.left = `${e === "column" ? o - 1 : 0}px`, n.style.top = `${e === "row" ? o - 1 : 0}px`, e === "column" ? n.style.height = `${this.viewport.clientHeight}px` : n.style.width = `${this.viewport.clientWidth}px`, this.frozenBoundaryLayer.append(n);
|
|
1112
|
+
};
|
|
1113
|
+
t("row", this.frozenRows, this.columnHeaderHeight + P(this.frozenRows, (e) => this.getRowHeight(e)), this.viewport.clientHeight), t("column", this.frozenColumns, this.rowHeaderWidth + P(this.frozenColumns, (e) => this.getColumnWidth(e)), this.viewport.clientWidth);
|
|
1114
|
+
}
|
|
1115
|
+
refreshValues() {
|
|
1116
|
+
const t = document.activeElement;
|
|
1117
|
+
this.table.querySelectorAll("td[data-key]").forEach((e) => {
|
|
1118
|
+
const s = e.dataset.key;
|
|
1119
|
+
if (s && e !== t && s !== this.editingKey) {
|
|
1120
|
+
const o = e.querySelector(".colwork-table__cell-value");
|
|
1121
|
+
o ? o.textContent = this.displayValue(s) : e.textContent = this.displayValue(s);
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
refreshSelection() {
|
|
1126
|
+
if (this.viewport.querySelectorAll(".colwork-table__selection").forEach((e) => e.remove()), !this.selectionFocus) return;
|
|
1127
|
+
const t = this.getSelectionBounds();
|
|
1128
|
+
this.table.querySelectorAll("th[data-selection-axis]").forEach((e) => {
|
|
1129
|
+
const s = e.dataset.selectionAxis, o = Number(e.dataset.selectionIndex), r = this.selectionMode === "all" || this.selectionMode === s && (s === "row" ? o >= t.top && o <= t.bottom : o >= t.left && o <= t.right);
|
|
1130
|
+
e.classList.toggle("is-selected-header", r);
|
|
1131
|
+
}), this.addSelectionOverlay($({ row: t.top, column: t.left }, { row: t.bottom, column: t.right }), "colwork-table__selection", this.options.userColor ?? "#3b82f6");
|
|
1132
|
+
}
|
|
1133
|
+
refreshCursors() {
|
|
1134
|
+
this.table && (this.table.querySelectorAll("td[data-key]").forEach((t) => {
|
|
1135
|
+
t.querySelectorAll(".colwork-table__remote-cursor").forEach((e) => e.remove());
|
|
1136
|
+
}), this.viewport.querySelectorAll(".colwork-table__remote-selection").forEach((t) => t.remove()), this.awareness.getStates().forEach((t, e) => {
|
|
1137
|
+
var o;
|
|
1138
|
+
if (e === this.awareness.clientID) return;
|
|
1139
|
+
const s = t.selection;
|
|
1140
|
+
if (s != null && s.anchor && s.focus) {
|
|
1141
|
+
const r = wt(s, this.rowCount, this.columnCount);
|
|
1142
|
+
r && this.addSelectionOverlay(r, "colwork-table__remote-selection", ((o = t.user) == null ? void 0 : o.color) ?? "#94a3b8");
|
|
1143
|
+
const n = Math.min(s.anchor.row, s.focus.row), i = Math.min(s.anchor.column, s.focus.column), l = this.table.querySelector(`td[data-key="${n}:${i}"]`);
|
|
1144
|
+
l && this.addCursorLabel(l, t, e);
|
|
1145
|
+
} else {
|
|
1146
|
+
const r = t.cursor, n = r && this.table.querySelector(`td[data-key="${r.row}:${r.column}"]`);
|
|
1147
|
+
n && this.addCursorLabel(n, t, e);
|
|
1148
|
+
}
|
|
1149
|
+
}));
|
|
1150
|
+
}
|
|
1151
|
+
addSelectionOverlay(t, e, s) {
|
|
1152
|
+
this.table.querySelectorAll("td[data-key]").forEach((o) => {
|
|
1153
|
+
const [r, n] = o.dataset.key.split(":").map(Number), i = r + o.rowSpan - 1, l = n + o.colSpan - 1;
|
|
1154
|
+
if (r > t.end.row || i < t.start.row || n > t.end.column || l < t.start.column) return;
|
|
1155
|
+
const d = getComputedStyle(o), c = parseFloat(d.borderTopWidth), m = parseFloat(d.borderRightWidth), g = parseFloat(d.borderBottomWidth), p = parseFloat(d.borderLeftWidth), M = (z, U, D, V, u, y, b) => {
|
|
1156
|
+
if (z >= D && U <= V) return [`${-y}px`, `${-b}px`];
|
|
1157
|
+
let L = 0, C = 0, F = 0;
|
|
1158
|
+
for (let x = z; x <= U; x++) {
|
|
1159
|
+
const N = this.table.querySelector(`th[data-selection-axis="${u}"][data-selection-index="${x}"]`), B = N == null ? void 0 : N.getBoundingClientRect(), A = u === "row" ? (B == null ? void 0 : B.height) ?? this.getRowHeight(x) : (B == null ? void 0 : B.width) ?? this.getColumnWidth(x);
|
|
1160
|
+
L += A, x < D && (C += A), x > V && (F += A);
|
|
1161
|
+
}
|
|
1162
|
+
const v = (x, N) => x === 0 ? `${-N}px` : `calc(${x / L * 100}% + ${(y + b) * x / L - N}px)`;
|
|
1163
|
+
return [v(C, y), v(F, b)];
|
|
1164
|
+
}, [E, R] = M(r, i, t.start.row, t.end.row, "row", c, g), [H, q] = M(n, l, t.start.column, t.end.column, "column", p, m), O = document.createElement("span");
|
|
1165
|
+
O.className = e, O.setAttribute("aria-hidden", "true"), Object.assign(O.style, {
|
|
1166
|
+
top: E,
|
|
1167
|
+
right: q,
|
|
1168
|
+
bottom: R,
|
|
1169
|
+
left: H,
|
|
1170
|
+
borderTopWidth: r <= t.start.row ? "2px" : "0",
|
|
1171
|
+
borderRightWidth: l >= t.end.column ? "2px" : "0",
|
|
1172
|
+
borderBottomWidth: i >= t.end.row ? "2px" : "0",
|
|
1173
|
+
borderLeftWidth: n <= t.start.column ? "2px" : "0",
|
|
1174
|
+
borderColor: s,
|
|
1175
|
+
backgroundColor: ft(s, 0.1)
|
|
1176
|
+
}), o.append(O);
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
addCursorLabel(t, e, s) {
|
|
1180
|
+
var r, n;
|
|
1181
|
+
const o = document.createElement("span");
|
|
1182
|
+
o.className = "colwork-table__remote-cursor", o.style.backgroundColor = ((r = e.user) == null ? void 0 : r.color) ?? "#64748b", o.textContent = ((n = e.user) == null ? void 0 : n.name) ?? String(s).slice(0, 6), t.append(o);
|
|
1183
|
+
}
|
|
1184
|
+
startEdit(t, e, s = !0) {
|
|
1185
|
+
if (this.readOnly || this.isCellLocked(e) || this.editingKey === e) return;
|
|
1186
|
+
this.finishEdit(!0);
|
|
1187
|
+
const o = Array.from(this.table.querySelectorAll("td[data-key]")).find((n) => n.dataset.key === e);
|
|
1188
|
+
o && (t = o), this.editingKey = e, this.editingOriginal = this.cells.get(e) ?? "";
|
|
1189
|
+
const r = document.createElement("textarea");
|
|
1190
|
+
r.className = "colwork-table__editor", r.value = this.editingOriginal, this.applyStyle(r, e), this.editor = r, t.classList.add("is-editing"), t.append(r), s && this.sendAwareness({ ...this.selectionFocus ?? { row: 0, column: 0 }, offset: 0 }), r.addEventListener("blur", () => this.finishEdit(!0), { once: !0 }), r.addEventListener("keydown", (n) => {
|
|
1191
|
+
n.key === "Enter" && !n.ctrlKey && !n.shiftKey && r.blur(), n.key === "Escape" && this.finishEdit(!1);
|
|
1192
|
+
}), r.focus(), r.select();
|
|
1193
|
+
}
|
|
1194
|
+
readStyle(t) {
|
|
1195
|
+
try {
|
|
1196
|
+
return JSON.parse(this.styles.get(t) ?? "{}");
|
|
1197
|
+
} catch {
|
|
1198
|
+
return {};
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
applyStyle(t, e) {
|
|
1202
|
+
var o, r, n, i;
|
|
1203
|
+
const s = this.readStyle(e);
|
|
1204
|
+
t.style.fontWeight = s.bold ? "700" : "", t.style.fontStyle = s.italic ? "oblique" : "", t.style.textDecoration = [s.underline ? "underline" : "", s.strike ? "line-through" : ""].filter(Boolean).join(" "), t.style.transform = t.classList.contains("colwork-table__cell-value") && s.italic ? "skewX(-8deg)" : "", t.style.fontSize = s.fontSize ? `${s.fontSize * this.zoomRatio}px` : "", t.style.color = s.color ?? "", t.style.backgroundColor = s.background ?? "", t.style.textAlign = s.align ?? "", t.classList.contains("colwork-table__cell-value") && (t.style.whiteSpace = s.wrap ? "pre-wrap" : "pre"), !t.classList.contains("colwork-table__cell-value") && !t.classList.contains("colwork-table__editor") && (t.style.borderTop = (o = s.border) != null && o.top ? "2px solid #64748b" : "", t.style.borderRight = (r = s.border) != null && r.right ? "2px solid #64748b" : "", t.style.borderBottom = (n = s.border) != null && n.bottom ? "2px solid #64748b" : "", t.style.borderLeft = (i = s.border) != null && i.left ? "2px solid #64748b" : "");
|
|
1205
|
+
}
|
|
1206
|
+
displayValue(t) {
|
|
1207
|
+
const e = this.cells.get(t) ?? "", s = this.readStyle(t).format ?? "general";
|
|
1208
|
+
if (!e || s === "general") return e;
|
|
1209
|
+
if (s === "number") {
|
|
1210
|
+
const r = Number(e);
|
|
1211
|
+
return Number.isFinite(r) ? r.toLocaleString() : e;
|
|
1212
|
+
}
|
|
1213
|
+
if (s === "percent") {
|
|
1214
|
+
const r = Number(e.replace(/%$/, ""));
|
|
1215
|
+
return Number.isFinite(r) ? `${e.endsWith("%") ? r : r * 100}%` : e;
|
|
1216
|
+
}
|
|
1217
|
+
const o = new Date(e);
|
|
1218
|
+
return Number.isNaN(o.getTime()) ? e : o.toISOString().slice(0, 10);
|
|
1219
|
+
}
|
|
1220
|
+
toggleFormat(t) {
|
|
1221
|
+
if (this.readOnly || !this.selectedKey || this.selectionHasLocks()) return;
|
|
1222
|
+
const { top: e, bottom: s, left: o, right: r } = this.getSelectionBounds(), n = this.readStyle(this.selectedKey)[t];
|
|
1223
|
+
this.doc.transact(() => {
|
|
1224
|
+
for (let i = e; i <= s; i += 1)
|
|
1225
|
+
for (let l = o; l <= r; l += 1) {
|
|
1226
|
+
const d = `${i}:${l}`, c = this.readStyle(d);
|
|
1227
|
+
c[t] = !n, this.styles.set(d, JSON.stringify(c));
|
|
1228
|
+
}
|
|
1229
|
+
}), this.render();
|
|
1230
|
+
}
|
|
1231
|
+
clearFormatting() {
|
|
1232
|
+
if (this.readOnly || !this.selectedKey || this.selectionHasLocks()) return;
|
|
1233
|
+
const { top: t, bottom: e, left: s, right: o } = this.getSelectionBounds();
|
|
1234
|
+
this.doc.transact(() => {
|
|
1235
|
+
for (let r = t; r <= e; r += 1)
|
|
1236
|
+
for (let n = s; n <= o; n += 1) this.styles.delete(W({ row: r, column: n }));
|
|
1237
|
+
}), this.render();
|
|
1238
|
+
}
|
|
1239
|
+
toggleBorder(t) {
|
|
1240
|
+
if (this.readOnly || !this.selectedKey || this.selectionHasLocks()) return;
|
|
1241
|
+
const { top: e, bottom: s, left: o, right: r } = this.getSelectionBounds(), n = (c, m) => {
|
|
1242
|
+
const g = [];
|
|
1243
|
+
return t === "outer" && (c === e && g.push("top"), c === s && g.push("bottom"), m === o && g.push("left"), m === r && g.push("right")), (t === "inner" || t === "horizontal") && c < s && g.push("bottom"), (t === "inner" || t === "vertical") && m < r && g.push("right"), g;
|
|
1244
|
+
}, i = Array.from({ length: s - e + 1 }, (c, m) => Array.from({ length: r - o + 1 }, (g, p) => ({
|
|
1245
|
+
row: e + m,
|
|
1246
|
+
column: o + p
|
|
1247
|
+
}))).flat();
|
|
1248
|
+
if (!i.flatMap(({ row: c, column: m }) => n(c, m)).length) return;
|
|
1249
|
+
const d = !i.every(({ row: c, column: m }) => n(c, m).every((g) => {
|
|
1250
|
+
var p;
|
|
1251
|
+
return (p = this.readStyle(W({ row: c, column: m })).border) == null ? void 0 : p[g];
|
|
1252
|
+
}));
|
|
1253
|
+
this.doc.transact(() => {
|
|
1254
|
+
for (let c = e; c <= s; c += 1)
|
|
1255
|
+
for (let m = o; m <= r; m += 1) {
|
|
1256
|
+
const g = W({ row: c, column: m }), p = this.readStyle(g), M = { ...p.border ?? {} };
|
|
1257
|
+
n(c, m).forEach((E) => {
|
|
1258
|
+
d ? M[E] = !0 : delete M[E];
|
|
1259
|
+
}), Object.keys(M).length ? p.border = M : delete p.border, this.styles.set(g, JSON.stringify(p));
|
|
1260
|
+
}
|
|
1261
|
+
}), this.render();
|
|
1262
|
+
}
|
|
1263
|
+
setStyle(t, e) {
|
|
1264
|
+
if (this.readOnly || !this.selectedKey || this.selectionHasLocks()) return;
|
|
1265
|
+
const { top: s, bottom: o, left: r, right: n } = this.getSelectionBounds();
|
|
1266
|
+
this.doc.transact(() => {
|
|
1267
|
+
for (let i = s; i <= o; i += 1)
|
|
1268
|
+
for (let l = r; l <= n; l += 1) {
|
|
1269
|
+
const d = `${i}:${l}`, c = this.readStyle(d);
|
|
1270
|
+
t === "align" ? c.align = e : t === "color" ? c.color = e : t === "background" ? c.background = e : t === "fontSize" ? c.fontSize = e : c.format = e, this.styles.set(d, JSON.stringify(c));
|
|
1271
|
+
}
|
|
1272
|
+
}), this.render();
|
|
1273
|
+
}
|
|
1274
|
+
refreshStyles() {
|
|
1275
|
+
this.table.querySelectorAll("td[data-key]").forEach((t) => {
|
|
1276
|
+
if (t.dataset.key) {
|
|
1277
|
+
this.applyStyle(t, t.dataset.key);
|
|
1278
|
+
const e = t.querySelector(".colwork-table__cell-value");
|
|
1279
|
+
e && (this.applyStyle(e, t.dataset.key), e.textContent = this.displayValue(t.dataset.key));
|
|
1280
|
+
}
|
|
1281
|
+
}), this.refreshSelection(), this.refreshCursors();
|
|
1282
|
+
}
|
|
1283
|
+
finishEdit(t) {
|
|
1284
|
+
if (!this.editor || !this.editingKey) return;
|
|
1285
|
+
const e = this.editingKey, s = this.editor, o = t ? s.value : this.editingOriginal;
|
|
1286
|
+
this.editor = void 0, this.editingKey = void 0, this.editingOriginal = "", !this.isCellLocked(e) && o !== this.cells.get(e) && this.cells.set(e, o);
|
|
1287
|
+
const r = this.awareness.getLocalState() ?? {};
|
|
1288
|
+
this.awareness.setLocalState({ ...r, editing: null }), s.remove();
|
|
1289
|
+
const n = this.table.querySelector(`td[data-key="${e}"]`);
|
|
1290
|
+
if (!n) {
|
|
1291
|
+
this.render();
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
this.table.querySelectorAll("td.is-editing").forEach((l) => l.classList.remove("is-editing"));
|
|
1295
|
+
const i = n.querySelector(".colwork-table__cell-value") ?? document.createElement("span");
|
|
1296
|
+
i.className = "colwork-table__cell-value", i.textContent = this.displayValue(e), i.parentElement || n.append(i), Array.from(n.childNodes).forEach((l) => {
|
|
1297
|
+
l !== i && l.nodeType === Node.TEXT_NODE && l.remove();
|
|
1298
|
+
}), this.applyStyle(n, e), this.applyStyle(i, e), this.refreshSelection(), this.refreshCursors();
|
|
1299
|
+
}
|
|
1300
|
+
destroy() {
|
|
1301
|
+
var t, e;
|
|
1302
|
+
this.destroyed = !0, this.viewportResizeObserver.disconnect(), this.closeLockMessage(), (t = this.passwordDialog) == null || t.close(), this.finishEdit(!0), this.doc.off("update", this.onDocUpdate), this.awareness.off("update", this.onAwarenessUpdate), this.awareness.off("change", this.onAwarenessChange), this.cells.unobserve(this.onCellsChange), this.styles.unobserve(this.onStylesChange), this.rowHeights.unobserve(this.onSizesChange), this.columnWidths.unobserve(this.onSizesChange), this.merges.unobserve(this.onMergesChange), this.locks.unobserve(this.onLocksChange), this.undoManager.off("stack-item-added", this.onHistoryChange), this.undoManager.off("stack-item-popped", this.onHistoryChange), this.undoManager.off("stack-cleared", this.onHistoryChange), this.viewport.removeEventListener("scroll", this.onViewportScroll), this.viewport.removeEventListener("wheel", this.onViewportWheel), this.viewport.removeEventListener("keydown", this.onViewportKeyDown), this.viewport.removeEventListener("copy", this.onCopy, !0), this.viewport.removeEventListener("paste", this.onPaste, !0), this.table.removeEventListener("mousedown", this.onTableMouseDown), window.removeEventListener("mouseup", this.onWindowMouseUp), window.removeEventListener("mousemove", this.onWindowMouseMove), window.removeEventListener("blur", this.onWindowBlur), document.removeEventListener("mousedown", this.onDocumentMouseDown), this.hideContextMenu(), this.renderFrame !== void 0 && cancelAnimationFrame(this.renderFrame), (e = this.provider) == null || e.destroy(), this.undoManager.destroy(), this.doc.destroy(), this.root.replaceChildren();
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
function nt(a) {
|
|
1306
|
+
if (a != null && a.trim())
|
|
1307
|
+
return JSON.parse(a);
|
|
1308
|
+
}
|
|
1309
|
+
function _t(a) {
|
|
1310
|
+
if (!(!a || a.format === "colwork.update-log"))
|
|
1311
|
+
return a.format === "colwork.archive" ? a.snapshot : a;
|
|
1312
|
+
}
|
|
1313
|
+
function rt(a) {
|
|
1314
|
+
return !a || a.format === "colwork.snapshot" ? [] : a.updates ?? [];
|
|
1315
|
+
}
|
|
1316
|
+
function Ot(a, t) {
|
|
1317
|
+
const e = nt(a), s = nt(t), o = _t(e), r = [...rt(e), ...rt(s)], n = new K.Doc();
|
|
1318
|
+
o != null && o.snapshot && K.applyUpdate(n, st(o.snapshot));
|
|
1319
|
+
const i = Number((o == null ? void 0 : o.sequence) ?? 0), l = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set();
|
|
1320
|
+
let c = i;
|
|
1321
|
+
for (const m of r.sort((g, p) => Number(g.seq) - Number(p.seq))) {
|
|
1322
|
+
const g = Number(m.seq);
|
|
1323
|
+
!Number.isInteger(g) || typeof m.update != "string" || g <= i || l.has(g) || d.has(m.update) || (K.applyUpdate(n, st(m.update)), l.add(g), d.add(m.update), c = Math.max(c, g));
|
|
1324
|
+
}
|
|
1325
|
+
return {
|
|
1326
|
+
format: "colwork.snapshot",
|
|
1327
|
+
version: 1,
|
|
1328
|
+
room: o == null ? void 0 : o.room,
|
|
1329
|
+
sequence: c,
|
|
1330
|
+
snapshot: I(K.encodeStateAsUpdate(n)),
|
|
1331
|
+
stateVector: I(K.encodeStateVector(n))
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
export {
|
|
1335
|
+
Ft as ColworkTable,
|
|
1336
|
+
W as cellKey,
|
|
1337
|
+
T as containsPosition,
|
|
1338
|
+
pt as normalizeRange,
|
|
1339
|
+
Dt as rangeSize,
|
|
1340
|
+
Ot as rebuildYjsSnapshot
|
|
1341
|
+
};
|