markloom 0.6.0 → 0.7.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/collab.md +25 -13
- package/dist/ai/index.cjs +5 -4
- package/dist/ai/index.d.cts +5 -5
- package/dist/ai/index.d.ts +5 -5
- package/dist/ai/index.js +4 -3
- package/dist/{chunk-KYZJIOOL.js → chunk-4Q7PAR2T.js} +3671 -904
- package/dist/chunk-75EJ4ARB.cjs +642 -0
- package/dist/{chunk-U3JZDFL4.cjs → chunk-BMPCVT3B.cjs} +2 -34
- package/dist/{chunk-VSBIMMI7.js → chunk-EFYT2223.js} +3 -35
- package/dist/chunk-HXABDVLZ.cjs +297 -0
- package/dist/{chunk-I5VK4U7J.cjs → chunk-I2LDEVLA.cjs} +434 -80
- package/dist/{chunk-YBAT343I.js → chunk-MQLRZQZL.js} +263 -119
- package/dist/chunk-OVHASRID.cjs +4279 -0
- package/dist/chunk-PCSVBPOR.cjs +40 -0
- package/dist/{chunk-TB2OY3D4.js → chunk-PSAXWBNM.js} +413 -59
- package/dist/{chunk-QEVME6ZU.cjs → chunk-PZRWTLLB.cjs} +260 -161
- package/dist/chunk-QO7JEZDB.js +4279 -0
- package/dist/{chunk-EJSFZHGT.js → chunk-WSJUXPAG.js} +18 -2
- package/dist/{chunk-ACFYBYZJ.cjs → chunk-Y4IXQFWL.cjs} +4212 -1445
- package/dist/{chunk-CGYCJORO.js → chunk-YLVSMDIO.js} +220 -121
- package/dist/chunk-ZL6WT6OY.js +297 -0
- package/dist/{client-CLYq17SW.d.ts → client-BvvyHjhh.d.ts} +57 -7
- package/dist/{client-DAth8pPq.d.cts → client-Ciy7Fa_k.d.cts} +57 -7
- package/dist/collab/index.cjs +5 -4
- package/dist/collab/index.d.cts +13 -15
- package/dist/collab/index.d.ts +13 -15
- package/dist/collab/index.js +4 -3
- package/dist/{controller-BIOO_XKJ.d.cts → controller-CHznEhAR.d.cts} +186 -27
- package/dist/{controller-6AJ36G52.d.ts → controller-DLqI8_jm.d.ts} +186 -27
- package/dist/index.cjs +69 -7
- package/dist/index.d.cts +127 -8
- package/dist/index.d.ts +127 -8
- package/dist/index.js +80 -18
- package/dist/io/index.cjs +58 -59
- package/dist/io/index.d.cts +1 -1
- package/dist/io/index.d.ts +1 -1
- package/dist/io/index.js +15 -16
- package/dist/react/index.cjs +9 -8
- package/dist/react/index.d.cts +5 -5
- package/dist/react/index.d.ts +5 -5
- package/dist/react/index.js +8 -7
- package/dist/{rebase-batch-DlerAcFN.d.ts → rebase-batch-CSnv8FKp.d.ts} +1 -1
- package/dist/{rebase-batch-PjENQ742.d.cts → rebase-batch-DO1ygkx9.d.cts} +1 -1
- package/dist/{types-COUweEav.d.cts → types-BkHlmUFL.d.cts} +5 -1
- package/dist/{types-COUweEav.d.ts → types-BkHlmUFL.d.ts} +5 -1
- package/dist/{versions-9mG1epfB.d.cts → versions-DNFf8zVz.d.cts} +1 -1
- package/dist/{versions-Bubyo4pt.d.ts → versions-DOkOyh2J.d.ts} +1 -1
- package/dist/view/index.cjs +215 -17
- package/dist/view/index.d.cts +82 -9
- package/dist/view/index.d.ts +82 -9
- package/dist/view/index.js +212 -14
- package/dist/view/theme.css +349 -75
- package/dist/{suggest-Bk4zLzuv.d.ts → word-count-BPw9G2Bg.d.ts} +15 -5
- package/dist/{suggest-BkGGiPKA.d.cts → word-count-DNLn1h_t.d.cts} +15 -5
- package/package.json +11 -6
- package/dist/chunk-EADZKEU4.cjs +0 -498
- package/dist/chunk-SNFF6X45.js +0 -2347
- package/dist/chunk-TWD5T4YC.cjs +0 -2347
- package/dist/chunk-ZKQSQSSM.cjs +0 -24
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { E as ElementNode, S as Selection, P as Path, M as Mark } from './types-
|
|
2
|
-
import { S as Step } from './versions-
|
|
1
|
+
import { E as ElementNode, S as Selection, P as Path, M as Mark } from './types-BkHlmUFL.cjs';
|
|
2
|
+
import { S as Step } from './versions-DNFf8zVz.cjs';
|
|
3
3
|
|
|
4
4
|
interface Commit {
|
|
5
5
|
steps: Step[];
|
|
@@ -88,9 +88,7 @@ declare function isSuggestMark(m: Mark, id?: string): boolean;
|
|
|
88
88
|
declare function suggestIdsInDoc(doc: ElementNode): Set<string>;
|
|
89
89
|
/** 文档序枚举建议(同一 id 合并 quote)。 */
|
|
90
90
|
declare function listSuggestions(doc: ElementNode): SuggestHit[];
|
|
91
|
-
/** 全树对某 id 剥 suggest mark(保留正文)。 */
|
|
92
91
|
declare function stripSuggestMarks(doc: ElementNode, id: string): Step[];
|
|
93
|
-
/** 全树删掉带某 suggest id 的文本叶段(拒绝插入 / 接受删除)。 */
|
|
94
92
|
declare function dropSuggestText(doc: ElementNode, id: string): Step[];
|
|
95
93
|
/**
|
|
96
94
|
* 接受建议:
|
|
@@ -117,4 +115,16 @@ declare function rangeIsSuggestInsert(doc: ElementNode, sel: Selection): boolean
|
|
|
117
115
|
*/
|
|
118
116
|
declare function markSuggestRange(doc: ElementNode, sel: Selection, kind: SuggestKind, id: string, author?: SuggestAuthor): Step[];
|
|
119
117
|
|
|
120
|
-
|
|
118
|
+
interface WordCount {
|
|
119
|
+
/** 词数:CJK 一字一词,其余按词。 */
|
|
120
|
+
words: number;
|
|
121
|
+
/** 字符数,含空格,不含换行(码点计,emoji 算 1)。 */
|
|
122
|
+
chars: number;
|
|
123
|
+
/** 字符数,不含任何空白。 */
|
|
124
|
+
charsNoSpace: number;
|
|
125
|
+
/** 段落数:有字的 paragraph / heading 块(嵌在列表、引用里的也算)。 */
|
|
126
|
+
paragraphs: number;
|
|
127
|
+
}
|
|
128
|
+
declare function wordCount(doc: ElementNode): WordCount;
|
|
129
|
+
|
|
130
|
+
export { stripSuggestMarks as A, suggestIdsInDoc as B, type CommentAuthor as C, suggestMark as D, undo as E, wordCount as F, type History as H, type SearchOpts as S, type TextMatch as T, type WordCount as W, type CommentMessage as a, type CommentRenderState as b, type CommentThread as c, type Commit as d, type SuggestAuthor as e, type SuggestHit as f, type SuggestKind as g, acceptSuggestion as h, amend as i, applyForeign as j, applyReplaceAll as k, commentIdsInDoc as l, commit as m, dropSuggestText as n, findText as o, init as p, isSuggestMark as q, listSuggestions as r, markSuggestRange as s, rangeForDelete as t, rangeIsSuggestInsert as u, redo as v, rejectSuggestion as w, replaceAll as x, replaceMatch as y, stripCommentMarks as z };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "markloom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "The loom for Markdown — rich text with MD isomorphism, reversible Steps, AI write, collab, search/versions/suggest, DOCX I/O",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -59,7 +59,11 @@
|
|
|
59
59
|
"test": "vitest run",
|
|
60
60
|
"test:watch": "vitest",
|
|
61
61
|
"site:dev": "bunx --bun serve site -l 4178",
|
|
62
|
-
"deploy:site": "wrangler pages deploy site --project-name=markloom-www --branch=main --commit-dirty=true"
|
|
62
|
+
"deploy:site": "wrangler pages deploy site --project-name=markloom-www --branch=main --commit-dirty=true",
|
|
63
|
+
"test:domain": "vitest run __tests__/domain/",
|
|
64
|
+
"test:domain:audit": "vitest run __tests__/domain/spaces-audit.meta.test.ts __tests__/domain/axis-sync.meta.test.ts __tests__/domain/registry.meta.test.ts __tests__/domain/catalog.meta.test.ts __tests__/domain/shell-surfaces.meta.test.ts __tests__/domain/classic-coverage.meta.test.ts",
|
|
65
|
+
"test:shell": "vitest run __tests__/editor-behavior-contracts.test.ts __tests__/compound-contracts.test.ts __tests__/editor-delete.test.ts __tests__/editor-cut.test.ts __tests__/tiptap-list-parity.test.ts __tests__/editor-shift-enter.test.ts __tests__/editor-undo-selection.test.ts __tests__/undo-coalesce.test.ts __tests__/block-start-backspace.test.ts __tests__/table-tab-append.test.ts __tests__/code-exit-down.test.ts __tests__/typography-edge.test.ts __tests__/editor-word-line-delete.test.ts __tests__/editor-stray-node.test.ts __tests__/open-async.test.ts __tests__/editor-preact-surface.test.ts __tests__/atom-neighbor-select.test.ts __tests__/gap.test.ts __tests__/hard-break.test.ts __tests__/tasklist.test.ts __tests__/align.test.ts __tests__/typora-keys.test.ts __tests__/keymap.test.ts __tests__/ime-composition-contracts.test.ts __tests__/slash-menu.test.ts __tests__/mention-menu.test.ts __tests__/mention-popovers.test.ts __tests__/bubble-menu.test.ts __tests__/drag-handle.test.ts __tests__/floating-menu.test.ts __tests__/prompt-dialog.test.ts __tests__/toc-panel.test.ts __tests__/ai-write.test.ts __tests__/ai-ui.test.ts __tests__/ai-bubble.test.ts __tests__/ai-discard.test.ts __tests__/ai-discard-draft-orphan.test.ts __tests__/table-controls.test.ts __tests__/image-controls.test.ts __tests__/code-controls.test.ts __tests__/editor-table-delete.test.ts __tests__/editor-image-delete.test.ts __tests__/editor-code-select-all.test.ts __tests__/editor-cell-autolist.test.ts __tests__/editor-paste-image.test.ts __tests__/image-caption-align.test.ts __tests__/math-embed.test.ts __tests__/view.test.ts __tests__/selection.test.ts __tests__/render-parity.test.ts __tests__/heading-fold.test.ts __tests__/invisible-characters.test.ts __tests__/readonly.test.ts __tests__/placeholder.test.ts __tests__/i18n.test.ts __tests__/html-paste.test.ts __tests__/extension.test.ts __tests__/react.test.tsx __tests__/collab-state.test.ts",
|
|
66
|
+
"test:model": "bun run test:domain:audit && bun run test:shell"
|
|
63
67
|
},
|
|
64
68
|
"devDependencies": {
|
|
65
69
|
"@types/mdast": "^4.0.4",
|
|
@@ -67,9 +71,9 @@
|
|
|
67
71
|
"@types/react-dom": "^19.2.3",
|
|
68
72
|
"@vitejs/plugin-react": "4",
|
|
69
73
|
"aihand": "0.1.17",
|
|
70
|
-
"teactjs": "file:../../teactjs",
|
|
71
74
|
"autoprefixer": "^10.5.4",
|
|
72
75
|
"github-slugger": "^2.0.0",
|
|
76
|
+
"jsdom": "^26.1.0",
|
|
73
77
|
"postcss": "^8.5.24",
|
|
74
78
|
"react": "^19.2.7",
|
|
75
79
|
"react-dom": "^19.2.7",
|
|
@@ -99,14 +103,15 @@
|
|
|
99
103
|
},
|
|
100
104
|
"dependencies": {
|
|
101
105
|
"@floating-ui/dom": "^1.8.0",
|
|
106
|
+
"@xmldom/xmldom": "^0.9.10",
|
|
107
|
+
"fflate": "^0.8.3",
|
|
102
108
|
"mdast-util-from-markdown": "^2",
|
|
103
109
|
"mdast-util-gfm": "^3",
|
|
104
110
|
"mdast-util-to-markdown": "^2",
|
|
111
|
+
"micromark-extension-cjk-friendly": "^2.0.2",
|
|
105
112
|
"micromark-extension-gfm": "^3",
|
|
106
113
|
"photoswipe": "^5.4.4",
|
|
107
114
|
"preact": "^10.29.2",
|
|
108
|
-
"preact-render-to-string": "^6.7.0"
|
|
109
|
-
"@xmldom/xmldom": "^0.9.10",
|
|
110
|
-
"fflate": "^0.8.3"
|
|
115
|
+
"preact-render-to-string": "^6.7.0"
|
|
111
116
|
}
|
|
112
117
|
}
|
package/dist/chunk-EADZKEU4.cjs
DELETED
|
@@ -1,498 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var _chunkU3JZDFL4cjs = require('./chunk-U3JZDFL4.cjs');
|
|
5
|
-
|
|
6
|
-
// src/collab/colors.ts
|
|
7
|
-
var COLLAB_COLORS = [
|
|
8
|
-
"#ffd6d6",
|
|
9
|
-
// 浅粉
|
|
10
|
-
"#ffe8b8",
|
|
11
|
-
// 浅杏
|
|
12
|
-
"#d4f0c8",
|
|
13
|
-
// 薄荷绿
|
|
14
|
-
"#c8e4ff",
|
|
15
|
-
// 天蓝
|
|
16
|
-
"#e0d4f5",
|
|
17
|
-
// 薰衣草
|
|
18
|
-
"#d0d0d0",
|
|
19
|
-
// 浅灰
|
|
20
|
-
"#6b6b6b",
|
|
21
|
-
// 深灰
|
|
22
|
-
"#e85d4c",
|
|
23
|
-
// 红
|
|
24
|
-
"#f5a623",
|
|
25
|
-
// 橙
|
|
26
|
-
"#f0c040",
|
|
27
|
-
// 黄
|
|
28
|
-
"#34b33a",
|
|
29
|
-
// 绿
|
|
30
|
-
"#3b6ff5",
|
|
31
|
-
// 蓝
|
|
32
|
-
"#7c5cff"
|
|
33
|
-
// 紫
|
|
34
|
-
];
|
|
35
|
-
var COLLAB_CARET_COLORS = [
|
|
36
|
-
"#f06a6a",
|
|
37
|
-
// 粉
|
|
38
|
-
"#e09a20",
|
|
39
|
-
// 杏
|
|
40
|
-
"#3db84a",
|
|
41
|
-
// 绿
|
|
42
|
-
"#3d8cf0",
|
|
43
|
-
// 天蓝
|
|
44
|
-
"#8b6ef0",
|
|
45
|
-
// 薰衣草
|
|
46
|
-
"#8a8a8a",
|
|
47
|
-
// 灰
|
|
48
|
-
"#4a4a4a",
|
|
49
|
-
// 深灰
|
|
50
|
-
"#e03d30",
|
|
51
|
-
// 红
|
|
52
|
-
"#f09000",
|
|
53
|
-
// 橙
|
|
54
|
-
"#e0a800",
|
|
55
|
-
// 黄
|
|
56
|
-
"#1f9e28",
|
|
57
|
-
// 绿
|
|
58
|
-
"#2f5ff0",
|
|
59
|
-
// 蓝
|
|
60
|
-
"#6a48f0"
|
|
61
|
-
// 紫
|
|
62
|
-
];
|
|
63
|
-
function collabColor(siteId, explicit) {
|
|
64
|
-
if (explicit)
|
|
65
|
-
return explicit;
|
|
66
|
-
const i = Math.abs((siteId || 1) - 1) % COLLAB_COLORS.length;
|
|
67
|
-
return COLLAB_COLORS[i];
|
|
68
|
-
}
|
|
69
|
-
function collabCaretColor(siteId, explicit) {
|
|
70
|
-
if (explicit)
|
|
71
|
-
return solidify(explicit);
|
|
72
|
-
const i = Math.abs((siteId || 1) - 1) % COLLAB_CARET_COLORS.length;
|
|
73
|
-
return COLLAB_CARET_COLORS[i];
|
|
74
|
-
}
|
|
75
|
-
function solidify(hex) {
|
|
76
|
-
const rgb = parseHex(hex);
|
|
77
|
-
if (!rgb)
|
|
78
|
-
return hex;
|
|
79
|
-
const [r, g, b] = rgb;
|
|
80
|
-
const luma = 0.2126 * (r / 255) + 0.7152 * (g / 255) + 0.0722 * (b / 255);
|
|
81
|
-
if (luma <= 0.48)
|
|
82
|
-
return hex;
|
|
83
|
-
const target = 0.4;
|
|
84
|
-
const scale = target / Math.max(luma, 0.01);
|
|
85
|
-
const mid = (r + g + b) / 3;
|
|
86
|
-
const sat = 1.25;
|
|
87
|
-
const punch = (c) => {
|
|
88
|
-
const away = mid + (c - mid) * sat;
|
|
89
|
-
return Math.max(0, Math.min(255, Math.round(away * scale)));
|
|
90
|
-
};
|
|
91
|
-
return toHex(punch(r), punch(g), punch(b));
|
|
92
|
-
}
|
|
93
|
-
function parseHex(hex) {
|
|
94
|
-
const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
95
|
-
if (!m)
|
|
96
|
-
return null;
|
|
97
|
-
const n = Number.parseInt(m[1], 16);
|
|
98
|
-
return [n >> 16 & 255, n >> 8 & 255, n & 255];
|
|
99
|
-
}
|
|
100
|
-
function toHex(r, g, b) {
|
|
101
|
-
return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
|
|
102
|
-
}
|
|
103
|
-
function collabLuma(hex) {
|
|
104
|
-
const rgb = parseHex(hex);
|
|
105
|
-
if (!rgb)
|
|
106
|
-
return 0.5;
|
|
107
|
-
const [r, g, b] = rgb;
|
|
108
|
-
return 0.2126 * (r / 255) + 0.7152 * (g / 255) + 0.0722 * (b / 255);
|
|
109
|
-
}
|
|
110
|
-
function collabFg(bg) {
|
|
111
|
-
return collabLuma(bg) > 0.62 ? "rgba(0,0,0,0.78)" : "#fff";
|
|
112
|
-
}
|
|
113
|
-
function collabInitial(name, siteId) {
|
|
114
|
-
const n = (name || "").trim();
|
|
115
|
-
if (n) {
|
|
116
|
-
const ch = n[0];
|
|
117
|
-
return /[a-z]/i.test(ch) ? ch.toUpperCase() : ch;
|
|
118
|
-
}
|
|
119
|
-
return String(_nullishCoalesce(siteId, () => ( "?")));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// src/collab/client.ts
|
|
123
|
-
function wireToSel(w) {
|
|
124
|
-
const toPos = (p) => ({
|
|
125
|
-
path: p.path.concat(p.offset)
|
|
126
|
-
});
|
|
127
|
-
return { anchor: toPos(w.anchor), head: toPos(w.head) };
|
|
128
|
-
}
|
|
129
|
-
function selToWire(sel) {
|
|
130
|
-
const split = (p) => {
|
|
131
|
-
const path = p.path;
|
|
132
|
-
if (path.length < 1)
|
|
133
|
-
return { path: [], offset: 0 };
|
|
134
|
-
return { path: path.slice(0, -1), offset: path[path.length - 1] };
|
|
135
|
-
};
|
|
136
|
-
return { anchor: split(sel.anchor), head: split(sel.head) };
|
|
137
|
-
}
|
|
138
|
-
function toWsUrl(url, room) {
|
|
139
|
-
let u = url.replace(/\/+$/, "");
|
|
140
|
-
if (u.startsWith("https://"))
|
|
141
|
-
u = `wss://${u.slice(8)}`;
|
|
142
|
-
else if (u.startsWith("http://"))
|
|
143
|
-
u = `ws://${u.slice(7)}`;
|
|
144
|
-
return `${u}/room/${encodeURIComponent(room)}`;
|
|
145
|
-
}
|
|
146
|
-
function retryDelay(attempt) {
|
|
147
|
-
const base = Math.min(1e3 * 2 ** Math.min(attempt, 5), 3e4);
|
|
148
|
-
const jitter = base * 0.5 * Math.random();
|
|
149
|
-
return base * 0.75 + jitter;
|
|
150
|
-
}
|
|
151
|
-
var CollabClient = (_class = class {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
__init() {this.ws = null}
|
|
155
|
-
__init2() {this.closed = false}
|
|
156
|
-
__init3() {this.retry = 0}
|
|
157
|
-
__init4() {this.reconnectTimer = null}
|
|
158
|
-
__init5() {this.pingTimer = null}
|
|
159
|
-
__init6() {this.lastPong = 0}
|
|
160
|
-
__init7() {this.authFailed = false}
|
|
161
|
-
__init8() {this.siteId = 0}
|
|
162
|
-
__init9() {this.version = 0}
|
|
163
|
-
__init10() {this.clientSeq = 0}
|
|
164
|
-
__init11() {this.pending = []}
|
|
165
|
-
__init12() {this.peers = /* @__PURE__ */ new Map()}
|
|
166
|
-
__init13() {this.joined = false}
|
|
167
|
-
constructor(cfg, handlers) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);_class.prototype.__init7.call(this);_class.prototype.__init8.call(this);_class.prototype.__init9.call(this);_class.prototype.__init10.call(this);_class.prototype.__init11.call(this);_class.prototype.__init12.call(this);_class.prototype.__init13.call(this);
|
|
168
|
-
this.cfg = cfg;
|
|
169
|
-
this.handlers = handlers;
|
|
170
|
-
this.connect();
|
|
171
|
-
}
|
|
172
|
-
get peersList() {
|
|
173
|
-
return [...this.peers.values()];
|
|
174
|
-
}
|
|
175
|
-
get status() {
|
|
176
|
-
if (this.authFailed)
|
|
177
|
-
return "auth-error";
|
|
178
|
-
if (this.closed)
|
|
179
|
-
return "closed";
|
|
180
|
-
if (!this.joined)
|
|
181
|
-
return _optionalChain([this, 'access', _ => _.ws, 'optionalAccess', _2 => _2.readyState]) === WebSocket.OPEN ? "connecting" : "connecting";
|
|
182
|
-
return "open";
|
|
183
|
-
}
|
|
184
|
-
connect() {
|
|
185
|
-
if (this.closed || this.authFailed)
|
|
186
|
-
return;
|
|
187
|
-
_optionalChain([this, 'access', _3 => _3.handlers, 'access', _4 => _4.onStatus, 'optionalCall', _5 => _5("connecting")]);
|
|
188
|
-
const url = toWsUrl(this.cfg.url, this.cfg.room);
|
|
189
|
-
let ws;
|
|
190
|
-
try {
|
|
191
|
-
ws = new WebSocket(url);
|
|
192
|
-
} catch (e) {
|
|
193
|
-
_optionalChain([this, 'access', _6 => _6.handlers, 'access', _7 => _7.onError, 'optionalCall', _8 => _8(e instanceof Error ? e.message : String(e))]);
|
|
194
|
-
this.scheduleReconnect();
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
this.ws = ws;
|
|
198
|
-
ws.onopen = () => {
|
|
199
|
-
this.retry = 0;
|
|
200
|
-
this.lastPong = Date.now();
|
|
201
|
-
this.startHeartbeat();
|
|
202
|
-
void this.sendJoin();
|
|
203
|
-
};
|
|
204
|
-
ws.onclose = () => {
|
|
205
|
-
this.joined = false;
|
|
206
|
-
this.stopHeartbeat();
|
|
207
|
-
if (this.authFailed) {
|
|
208
|
-
_optionalChain([this, 'access', _9 => _9.handlers, 'access', _10 => _10.onStatus, 'optionalCall', _11 => _11("auth-error")]);
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
_optionalChain([this, 'access', _12 => _12.handlers, 'access', _13 => _13.onStatus, 'optionalCall', _14 => _14("closed")]);
|
|
212
|
-
if (!this.closed)
|
|
213
|
-
this.scheduleReconnect();
|
|
214
|
-
};
|
|
215
|
-
ws.onerror = () => {
|
|
216
|
-
try {
|
|
217
|
-
ws.close();
|
|
218
|
-
} catch (e2) {
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
ws.onmessage = (ev) => {
|
|
222
|
-
try {
|
|
223
|
-
const msg = JSON.parse(String(ev.data));
|
|
224
|
-
this.onServer(msg);
|
|
225
|
-
} catch (e3) {
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
async sendJoin() {
|
|
230
|
-
let token;
|
|
231
|
-
try {
|
|
232
|
-
const t = this.cfg.token;
|
|
233
|
-
if (typeof t === "function")
|
|
234
|
-
token = await t();
|
|
235
|
-
else if (typeof t === "string")
|
|
236
|
-
token = t;
|
|
237
|
-
} catch (e) {
|
|
238
|
-
_optionalChain([this, 'access', _15 => _15.handlers, 'access', _16 => _16.onError, 'optionalCall', _17 => _17(e instanceof Error ? e.message : String(e))]);
|
|
239
|
-
this.authFailed = true;
|
|
240
|
-
_optionalChain([this, 'access', _18 => _18.handlers, 'access', _19 => _19.onStatus, 'optionalCall', _20 => _20("auth-error")]);
|
|
241
|
-
try {
|
|
242
|
-
_optionalChain([this, 'access', _21 => _21.ws, 'optionalAccess', _22 => _22.close, 'call', _23 => _23()]);
|
|
243
|
-
} catch (e4) {
|
|
244
|
-
}
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
this.send({
|
|
248
|
-
type: "join",
|
|
249
|
-
room: this.cfg.room,
|
|
250
|
-
name: this.cfg.name,
|
|
251
|
-
color: this.cfg.color,
|
|
252
|
-
seed: this.cfg.seed,
|
|
253
|
-
...token ? { token } : {}
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
startHeartbeat() {
|
|
257
|
-
this.stopHeartbeat();
|
|
258
|
-
this.pingTimer = setInterval(() => {
|
|
259
|
-
if (_optionalChain([this, 'access', _24 => _24.ws, 'optionalAccess', _25 => _25.readyState]) !== WebSocket.OPEN)
|
|
260
|
-
return;
|
|
261
|
-
if (this.lastPong && Date.now() - this.lastPong > 45e3) {
|
|
262
|
-
try {
|
|
263
|
-
this.ws.close();
|
|
264
|
-
} catch (e5) {
|
|
265
|
-
}
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
this.send({ type: "ping" });
|
|
269
|
-
}, 2e4);
|
|
270
|
-
}
|
|
271
|
-
stopHeartbeat() {
|
|
272
|
-
if (this.pingTimer) {
|
|
273
|
-
clearInterval(this.pingTimer);
|
|
274
|
-
this.pingTimer = null;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
scheduleReconnect() {
|
|
278
|
-
if (this.reconnectTimer || this.closed || this.authFailed)
|
|
279
|
-
return;
|
|
280
|
-
const delay = retryDelay(this.retry++);
|
|
281
|
-
this.reconnectTimer = setTimeout(() => {
|
|
282
|
-
this.reconnectTimer = null;
|
|
283
|
-
this.connect();
|
|
284
|
-
}, delay);
|
|
285
|
-
}
|
|
286
|
-
send(msg) {
|
|
287
|
-
if (_optionalChain([this, 'access', _26 => _26.ws, 'optionalAccess', _27 => _27.readyState]) === WebSocket.OPEN)
|
|
288
|
-
this.ws.send(JSON.stringify(msg));
|
|
289
|
-
}
|
|
290
|
-
onServer(msg) {
|
|
291
|
-
switch (msg.type) {
|
|
292
|
-
case "welcome": {
|
|
293
|
-
this.siteId = msg.siteId;
|
|
294
|
-
this.version = msg.version;
|
|
295
|
-
this.pending = [];
|
|
296
|
-
this.joined = true;
|
|
297
|
-
_optionalChain([this, 'access', _28 => _28.handlers, 'access', _29 => _29.onStatus, 'optionalCall', _30 => _30("open")]);
|
|
298
|
-
if (!this.cfg.color)
|
|
299
|
-
this.cfg.color = collabColor(msg.siteId);
|
|
300
|
-
this.peers.clear();
|
|
301
|
-
for (const p of msg.peers) {
|
|
302
|
-
this.peers.set(p.siteId, {
|
|
303
|
-
...p,
|
|
304
|
-
color: collabColor(p.siteId, p.color)
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
_optionalChain([this, 'access', _31 => _31.handlers, 'access', _32 => _32.onWelcome, 'optionalCall', _33 => _33({
|
|
308
|
-
siteId: msg.siteId,
|
|
309
|
-
version: msg.version,
|
|
310
|
-
name: this.cfg.name,
|
|
311
|
-
color: this.cfg.color
|
|
312
|
-
})]);
|
|
313
|
-
this.handlers.onDoc(msg.doc, msg.version);
|
|
314
|
-
_optionalChain([this, 'access', _34 => _34.handlers, 'access', _35 => _35.onPeers, 'optionalCall', _36 => _36(this.peersList)]);
|
|
315
|
-
if (msg.versions)
|
|
316
|
-
_optionalChain([this, 'access', _37 => _37.handlers, 'access', _38 => _38.onVersions, 'optionalCall', _39 => _39(msg.versions)]);
|
|
317
|
-
break;
|
|
318
|
-
}
|
|
319
|
-
case "versions": {
|
|
320
|
-
_optionalChain([this, 'access', _40 => _40.handlers, 'access', _41 => _41.onVersions, 'optionalCall', _42 => _42(msg.versions)]);
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
case "resync": {
|
|
324
|
-
this.version = msg.version;
|
|
325
|
-
this.pending = [];
|
|
326
|
-
_optionalChain([this, 'access', _43 => _43.handlers, 'access', _44 => _44.onStatus, 'optionalCall', _45 => _45("resyncing")]);
|
|
327
|
-
_optionalChain([this, 'access', _46 => _46.handlers, 'access', _47 => _47.onResync, 'optionalCall', _48 => _48(msg.version)]);
|
|
328
|
-
this.handlers.onDoc(msg.doc, msg.version);
|
|
329
|
-
_optionalChain([this, 'access', _49 => _49.handlers, 'access', _50 => _50.onStatus, 'optionalCall', _51 => _51("open")]);
|
|
330
|
-
break;
|
|
331
|
-
}
|
|
332
|
-
case "op": {
|
|
333
|
-
if (!this.joined)
|
|
334
|
-
return;
|
|
335
|
-
if (msg.siteId === this.siteId) {
|
|
336
|
-
if (msg.clientSeq != null)
|
|
337
|
-
this.pending = this.pending.filter((p) => p.clientSeq > msg.clientSeq);
|
|
338
|
-
this.version = msg.version;
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
const pendingAs = this.pending.map((p) => ({ siteId: this.siteId, steps: p.steps }));
|
|
342
|
-
const { remote, pending: nextP } = _chunkU3JZDFL4cjs.transformRemoteAgainstPending.call(void 0,
|
|
343
|
-
msg.steps,
|
|
344
|
-
msg.siteId,
|
|
345
|
-
pendingAs,
|
|
346
|
-
this.siteId
|
|
347
|
-
);
|
|
348
|
-
this.pending = this.pending.map((p, i) => ({
|
|
349
|
-
...p,
|
|
350
|
-
steps: _nullishCoalesce(_optionalChain([nextP, 'access', _52 => _52[i], 'optionalAccess', _53 => _53.steps]), () => ( p.steps))
|
|
351
|
-
}));
|
|
352
|
-
this.version = msg.version;
|
|
353
|
-
if (remote.length) {
|
|
354
|
-
if (!this.peers.has(msg.siteId)) {
|
|
355
|
-
this.peers.set(msg.siteId, {
|
|
356
|
-
siteId: msg.siteId,
|
|
357
|
-
color: collabColor(msg.siteId)
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
this.mapPeerSelsThrough(remote);
|
|
361
|
-
this.ensureAuthorCaret(msg.siteId, remote);
|
|
362
|
-
this.handlers.onRemote(remote, msg.siteId);
|
|
363
|
-
_optionalChain([this, 'access', _54 => _54.handlers, 'access', _55 => _55.onPeers, 'optionalCall', _56 => _56(this.peersList)]);
|
|
364
|
-
}
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
case "presence": {
|
|
368
|
-
const cur = _nullishCoalesce(this.peers.get(msg.siteId), () => ( { siteId: msg.siteId }));
|
|
369
|
-
this.peers.set(msg.siteId, {
|
|
370
|
-
...cur,
|
|
371
|
-
name: _nullishCoalesce(msg.name, () => ( cur.name)),
|
|
372
|
-
color: collabColor(msg.siteId, _nullishCoalesce(msg.color, () => ( cur.color))),
|
|
373
|
-
sel: msg.sel === void 0 ? cur.sel : msg.sel
|
|
374
|
-
});
|
|
375
|
-
_optionalChain([this, 'access', _57 => _57.handlers, 'access', _58 => _58.onPeers, 'optionalCall', _59 => _59(this.peersList)]);
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
case "peer-join": {
|
|
379
|
-
this.peers.set(msg.siteId, {
|
|
380
|
-
siteId: msg.siteId,
|
|
381
|
-
name: msg.name,
|
|
382
|
-
color: collabColor(msg.siteId, msg.color)
|
|
383
|
-
});
|
|
384
|
-
_optionalChain([this, 'access', _60 => _60.handlers, 'access', _61 => _61.onPeers, 'optionalCall', _62 => _62(this.peersList)]);
|
|
385
|
-
break;
|
|
386
|
-
}
|
|
387
|
-
case "peer-leave": {
|
|
388
|
-
this.peers.delete(msg.siteId);
|
|
389
|
-
_optionalChain([this, 'access', _63 => _63.handlers, 'access', _64 => _64.onPeers, 'optionalCall', _65 => _65(this.peersList)]);
|
|
390
|
-
break;
|
|
391
|
-
}
|
|
392
|
-
case "error": {
|
|
393
|
-
_optionalChain([this, 'access', _66 => _66.handlers, 'access', _67 => _67.onError, 'optionalCall', _68 => _68(msg.message)]);
|
|
394
|
-
if (/auth|token|unauthorized/i.test(msg.message)) {
|
|
395
|
-
this.authFailed = true;
|
|
396
|
-
_optionalChain([this, 'access', _69 => _69.handlers, 'access', _70 => _70.onStatus, 'optionalCall', _71 => _71("auth-error")]);
|
|
397
|
-
try {
|
|
398
|
-
_optionalChain([this, 'access', _72 => _72.ws, 'optionalAccess', _73 => _73.close, 'call', _74 => _74()]);
|
|
399
|
-
} catch (e6) {
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
break;
|
|
403
|
-
}
|
|
404
|
-
case "pong":
|
|
405
|
-
this.lastPong = Date.now();
|
|
406
|
-
break;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
/** 本地已 apply 的 steps 推上服务器 */
|
|
410
|
-
push(steps) {
|
|
411
|
-
if (!steps.length || !this.joined || this.siteId <= 0)
|
|
412
|
-
return;
|
|
413
|
-
const clientSeq = ++this.clientSeq;
|
|
414
|
-
const baseVersion = this.version;
|
|
415
|
-
this.pending.push({ clientSeq, baseVersion, steps });
|
|
416
|
-
this.send({ type: "op", baseVersion, clientSeq, steps });
|
|
417
|
-
}
|
|
418
|
-
/** 命名版本上行(侧信道,不进 OT) */
|
|
419
|
-
pushVersion(v) {
|
|
420
|
-
if (!this.joined || this.siteId <= 0)
|
|
421
|
-
return;
|
|
422
|
-
this.send({ type: "version-save", version: v });
|
|
423
|
-
}
|
|
424
|
-
removeVersion(id) {
|
|
425
|
-
if (!this.joined || this.siteId <= 0)
|
|
426
|
-
return;
|
|
427
|
-
this.send({ type: "version-remove", id });
|
|
428
|
-
}
|
|
429
|
-
/** 用一批 steps 搬所有 peer 的 wire 选区(作者打字时彩光标贴字) */
|
|
430
|
-
mapPeerSelsThrough(steps) {
|
|
431
|
-
for (const [id, p] of this.peers) {
|
|
432
|
-
if (!p.sel)
|
|
433
|
-
continue;
|
|
434
|
-
let sel = wireToSel(p.sel);
|
|
435
|
-
for (const s of steps)
|
|
436
|
-
sel = _chunkU3JZDFL4cjs.mapSelection.call(void 0, s, sel);
|
|
437
|
-
this.peers.set(id, { ...p, sel: selToWire(sel) });
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* 作者无历史 presence 时,从本批最后一次 insertText 推光标到插入末尾。
|
|
442
|
-
* 有 sel 时 mapPeerSelsThrough 已搬过,不再覆盖。
|
|
443
|
-
*/
|
|
444
|
-
ensureAuthorCaret(siteId, steps) {
|
|
445
|
-
const p = this.peers.get(siteId);
|
|
446
|
-
if (!p || p.sel)
|
|
447
|
-
return;
|
|
448
|
-
for (let i = steps.length - 1; i >= 0; i--) {
|
|
449
|
-
const s = steps[i];
|
|
450
|
-
if (s.kind !== "insertText")
|
|
451
|
-
continue;
|
|
452
|
-
const offset = s.offset + s.text.length;
|
|
453
|
-
p.sel = {
|
|
454
|
-
anchor: { path: s.path.slice(), offset },
|
|
455
|
-
head: { path: s.path.slice(), offset }
|
|
456
|
-
};
|
|
457
|
-
this.peers.set(siteId, p);
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
presence(sel) {
|
|
462
|
-
if (!this.joined)
|
|
463
|
-
return;
|
|
464
|
-
this.send({
|
|
465
|
-
type: "presence",
|
|
466
|
-
name: this.cfg.name,
|
|
467
|
-
color: _nullishCoalesce(this.cfg.color, () => ( collabColor(this.siteId))),
|
|
468
|
-
sel
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
close() {
|
|
472
|
-
this.closed = true;
|
|
473
|
-
this.stopHeartbeat();
|
|
474
|
-
if (this.reconnectTimer)
|
|
475
|
-
clearTimeout(this.reconnectTimer);
|
|
476
|
-
try {
|
|
477
|
-
_optionalChain([this, 'access', _75 => _75.ws, 'optionalAccess', _76 => _76.close, 'call', _77 => _77()]);
|
|
478
|
-
} catch (e7) {
|
|
479
|
-
}
|
|
480
|
-
this.ws = null;
|
|
481
|
-
}
|
|
482
|
-
}, _class);
|
|
483
|
-
function connectCollab(cfg, handlers) {
|
|
484
|
-
return new CollabClient(cfg, handlers);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
exports.COLLAB_COLORS = COLLAB_COLORS; exports.COLLAB_CARET_COLORS = COLLAB_CARET_COLORS; exports.collabColor = collabColor; exports.collabCaretColor = collabCaretColor; exports.solidify = solidify; exports.collabLuma = collabLuma; exports.collabFg = collabFg; exports.collabInitial = collabInitial; exports.CollabClient = CollabClient; exports.connectCollab = connectCollab;
|