markloom 0.4.0 → 0.5.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.
Files changed (50) hide show
  1. package/README.md +29 -16
  2. package/ai.md +160 -0
  3. package/collab.md +68 -0
  4. package/dist/ai/index.cjs +4 -3
  5. package/dist/ai/index.d.cts +4 -2
  6. package/dist/ai/index.d.ts +4 -2
  7. package/dist/ai/index.js +3 -2
  8. package/dist/{chunk-VVFIQLVR.cjs → chunk-4TMHVNL6.cjs} +71 -79
  9. package/dist/chunk-6DCI7AL3.js +208 -0
  10. package/dist/chunk-AC5E42NH.cjs +208 -0
  11. package/dist/chunk-GQ2HHRGI.cjs +16 -0
  12. package/dist/{chunk-QXJHFQLJ.js → chunk-JMOJU5EU.js} +4 -2
  13. package/dist/chunk-JS527FDV.cjs +294 -0
  14. package/dist/chunk-LA5SBCGQ.js +16 -0
  15. package/dist/{chunk-ITVAR2UM.js → chunk-LEYAQNJA.js} +5 -3
  16. package/dist/{chunk-J3QWW6CN.js → chunk-MFYF2A6U.js} +5 -13
  17. package/dist/chunk-RLTHVQNQ.js +294 -0
  18. package/dist/{chunk-LNSKDJMF.cjs → chunk-SLRD6FMP.cjs} +27 -25
  19. package/dist/{chunk-VMUJM52A.js → chunk-WUHTCCAE.js} +65 -6
  20. package/dist/{chunk-VSTOMEU2.cjs → chunk-YJAWPRJW.cjs} +400 -341
  21. package/dist/{chunk-HA6XHJUD.cjs → chunk-YXQDCW4Y.cjs} +22 -20
  22. package/dist/client-A0ZQJnyR.d.ts +137 -0
  23. package/dist/client-BNams4wv.d.cts +137 -0
  24. package/dist/collab/index.cjs +19 -0
  25. package/dist/collab/index.d.cts +3 -0
  26. package/dist/collab/index.d.ts +3 -0
  27. package/dist/collab/index.js +19 -0
  28. package/dist/comments-DlExS6f-.d.cts +43 -0
  29. package/dist/comments-PhnR7Gkn.d.ts +43 -0
  30. package/dist/{controller-B_wG2Wh-.d.ts → controller-BkY1VPzc.d.ts} +17 -1
  31. package/dist/{controller-Cd1Dprrk.d.cts → controller-CVIZI--A.d.cts} +17 -1
  32. package/dist/index.cjs +21 -243
  33. package/dist/index.d.cts +5 -7
  34. package/dist/index.d.ts +5 -7
  35. package/dist/index.js +22 -244
  36. package/dist/react/index.cjs +8 -5
  37. package/dist/react/index.d.cts +4 -2
  38. package/dist/react/index.d.ts +4 -2
  39. package/dist/react/index.js +7 -4
  40. package/dist/rebase-batch-8rL_aXEE.d.ts +38 -0
  41. package/dist/rebase-batch-bYnBJpRO.d.cts +38 -0
  42. package/dist/step-BrvHIK7O.d.cts +65 -0
  43. package/dist/step-BrvHIK7O.d.ts +65 -0
  44. package/dist/view/index.cjs +11 -8
  45. package/dist/view/index.d.cts +5 -3
  46. package/dist/view/index.d.ts +5 -3
  47. package/dist/view/index.js +7 -4
  48. package/package.json +10 -3
  49. package/dist/comments-USjT1wYN.d.cts +0 -105
  50. package/dist/comments-USjT1wYN.d.ts +0 -105
@@ -0,0 +1,208 @@
1
+ import {
2
+ transformRemoteAgainstPending
3
+ } from "./chunk-RLTHVQNQ.js";
4
+
5
+ // src/collab/client.ts
6
+ function toWsUrl(url, room) {
7
+ let u = url.replace(/\/+$/, "");
8
+ if (u.startsWith("https://"))
9
+ u = `wss://${u.slice(8)}`;
10
+ else if (u.startsWith("http://"))
11
+ u = `ws://${u.slice(7)}`;
12
+ return `${u}/room/${encodeURIComponent(room)}`;
13
+ }
14
+ function retryDelay(attempt) {
15
+ const base = Math.min(1e3 * 2 ** Math.min(attempt, 5), 3e4);
16
+ const jitter = base * 0.5 * Math.random();
17
+ return base * 0.75 + jitter;
18
+ }
19
+ var CollabClient = class {
20
+ cfg;
21
+ handlers;
22
+ ws = null;
23
+ closed = false;
24
+ retry = 0;
25
+ reconnectTimer = null;
26
+ siteId = 0;
27
+ version = 0;
28
+ clientSeq = 0;
29
+ pending = [];
30
+ peers = /* @__PURE__ */ new Map();
31
+ joined = false;
32
+ constructor(cfg, handlers) {
33
+ this.cfg = cfg;
34
+ this.handlers = handlers;
35
+ this.connect();
36
+ }
37
+ get peersList() {
38
+ return [...this.peers.values()];
39
+ }
40
+ connect() {
41
+ if (this.closed)
42
+ return;
43
+ this.handlers.onStatus?.("connecting");
44
+ const url = toWsUrl(this.cfg.url, this.cfg.room);
45
+ let ws;
46
+ try {
47
+ ws = new WebSocket(url);
48
+ } catch (e) {
49
+ this.handlers.onError?.(e instanceof Error ? e.message : String(e));
50
+ this.scheduleReconnect();
51
+ return;
52
+ }
53
+ this.ws = ws;
54
+ ws.onopen = () => {
55
+ this.retry = 0;
56
+ this.handlers.onStatus?.("open");
57
+ this.send({
58
+ type: "join",
59
+ room: this.cfg.room,
60
+ name: this.cfg.name,
61
+ color: this.cfg.color,
62
+ seed: this.cfg.seed
63
+ });
64
+ };
65
+ ws.onclose = () => {
66
+ this.joined = false;
67
+ this.handlers.onStatus?.("closed");
68
+ if (!this.closed)
69
+ this.scheduleReconnect();
70
+ };
71
+ ws.onerror = () => {
72
+ try {
73
+ ws.close();
74
+ } catch {
75
+ }
76
+ };
77
+ ws.onmessage = (ev) => {
78
+ try {
79
+ const msg = JSON.parse(String(ev.data));
80
+ this.onServer(msg);
81
+ } catch {
82
+ }
83
+ };
84
+ }
85
+ scheduleReconnect() {
86
+ if (this.reconnectTimer || this.closed)
87
+ return;
88
+ const delay = retryDelay(this.retry++);
89
+ this.reconnectTimer = setTimeout(() => {
90
+ this.reconnectTimer = null;
91
+ this.connect();
92
+ }, delay);
93
+ }
94
+ send(msg) {
95
+ if (this.ws?.readyState === WebSocket.OPEN)
96
+ this.ws.send(JSON.stringify(msg));
97
+ }
98
+ onServer(msg) {
99
+ switch (msg.type) {
100
+ case "welcome": {
101
+ this.siteId = msg.siteId;
102
+ this.version = msg.version;
103
+ this.pending = [];
104
+ this.joined = true;
105
+ this.peers.clear();
106
+ for (const p of msg.peers)
107
+ this.peers.set(p.siteId, p);
108
+ this.handlers.onDoc(msg.doc, msg.version);
109
+ this.handlers.onPeers?.(this.peersList);
110
+ break;
111
+ }
112
+ case "resync": {
113
+ this.version = msg.version;
114
+ this.pending = [];
115
+ this.handlers.onDoc(msg.doc, msg.version);
116
+ break;
117
+ }
118
+ case "op": {
119
+ if (!this.joined)
120
+ return;
121
+ if (msg.siteId === this.siteId) {
122
+ if (msg.clientSeq != null)
123
+ this.pending = this.pending.filter((p) => p.clientSeq > msg.clientSeq);
124
+ this.version = msg.version;
125
+ return;
126
+ }
127
+ const pendingAs = this.pending.map((p) => ({ siteId: this.siteId, steps: p.steps }));
128
+ const { remote, pending: nextP } = transformRemoteAgainstPending(
129
+ msg.steps,
130
+ msg.siteId,
131
+ pendingAs,
132
+ this.siteId
133
+ );
134
+ this.pending = this.pending.map((p, i) => ({
135
+ ...p,
136
+ steps: nextP[i]?.steps ?? p.steps
137
+ }));
138
+ this.version = msg.version;
139
+ if (remote.length)
140
+ this.handlers.onRemote(remote);
141
+ break;
142
+ }
143
+ case "presence": {
144
+ const cur = this.peers.get(msg.siteId) ?? { siteId: msg.siteId };
145
+ this.peers.set(msg.siteId, {
146
+ ...cur,
147
+ name: msg.name ?? cur.name,
148
+ color: msg.color ?? cur.color,
149
+ sel: msg.sel === void 0 ? cur.sel : msg.sel
150
+ });
151
+ this.handlers.onPeers?.(this.peersList);
152
+ break;
153
+ }
154
+ case "peer-join": {
155
+ this.peers.set(msg.siteId, { siteId: msg.siteId, name: msg.name, color: msg.color });
156
+ this.handlers.onPeers?.(this.peersList);
157
+ break;
158
+ }
159
+ case "peer-leave": {
160
+ this.peers.delete(msg.siteId);
161
+ this.handlers.onPeers?.(this.peersList);
162
+ break;
163
+ }
164
+ case "error":
165
+ this.handlers.onError?.(msg.message);
166
+ break;
167
+ case "pong":
168
+ break;
169
+ }
170
+ }
171
+ /** 本地已 apply 的 steps 推上服务器 */
172
+ push(steps) {
173
+ if (!steps.length || !this.joined || this.siteId <= 0)
174
+ return;
175
+ const clientSeq = ++this.clientSeq;
176
+ const baseVersion = this.version;
177
+ this.pending.push({ clientSeq, baseVersion, steps });
178
+ this.send({ type: "op", baseVersion, clientSeq, steps });
179
+ }
180
+ presence(sel) {
181
+ if (!this.joined)
182
+ return;
183
+ this.send({
184
+ type: "presence",
185
+ name: this.cfg.name,
186
+ color: this.cfg.color,
187
+ sel
188
+ });
189
+ }
190
+ close() {
191
+ this.closed = true;
192
+ if (this.reconnectTimer)
193
+ clearTimeout(this.reconnectTimer);
194
+ try {
195
+ this.ws?.close();
196
+ } catch {
197
+ }
198
+ this.ws = null;
199
+ }
200
+ };
201
+ function connectCollab(cfg, handlers) {
202
+ return new CollabClient(cfg, handlers);
203
+ }
204
+
205
+ export {
206
+ CollabClient,
207
+ connectCollab
208
+ };
@@ -0,0 +1,208 @@
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
+ var _chunkJS527FDVcjs = require('./chunk-JS527FDV.cjs');
4
+
5
+ // src/collab/client.ts
6
+ function toWsUrl(url, room) {
7
+ let u = url.replace(/\/+$/, "");
8
+ if (u.startsWith("https://"))
9
+ u = `wss://${u.slice(8)}`;
10
+ else if (u.startsWith("http://"))
11
+ u = `ws://${u.slice(7)}`;
12
+ return `${u}/room/${encodeURIComponent(room)}`;
13
+ }
14
+ function retryDelay(attempt) {
15
+ const base = Math.min(1e3 * 2 ** Math.min(attempt, 5), 3e4);
16
+ const jitter = base * 0.5 * Math.random();
17
+ return base * 0.75 + jitter;
18
+ }
19
+ var CollabClient = (_class = class {
20
+
21
+
22
+ __init() {this.ws = null}
23
+ __init2() {this.closed = false}
24
+ __init3() {this.retry = 0}
25
+ __init4() {this.reconnectTimer = null}
26
+ __init5() {this.siteId = 0}
27
+ __init6() {this.version = 0}
28
+ __init7() {this.clientSeq = 0}
29
+ __init8() {this.pending = []}
30
+ __init9() {this.peers = /* @__PURE__ */ new Map()}
31
+ __init10() {this.joined = false}
32
+ 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);
33
+ this.cfg = cfg;
34
+ this.handlers = handlers;
35
+ this.connect();
36
+ }
37
+ get peersList() {
38
+ return [...this.peers.values()];
39
+ }
40
+ connect() {
41
+ if (this.closed)
42
+ return;
43
+ _optionalChain([this, 'access', _ => _.handlers, 'access', _2 => _2.onStatus, 'optionalCall', _3 => _3("connecting")]);
44
+ const url = toWsUrl(this.cfg.url, this.cfg.room);
45
+ let ws;
46
+ try {
47
+ ws = new WebSocket(url);
48
+ } catch (e) {
49
+ _optionalChain([this, 'access', _4 => _4.handlers, 'access', _5 => _5.onError, 'optionalCall', _6 => _6(e instanceof Error ? e.message : String(e))]);
50
+ this.scheduleReconnect();
51
+ return;
52
+ }
53
+ this.ws = ws;
54
+ ws.onopen = () => {
55
+ this.retry = 0;
56
+ _optionalChain([this, 'access', _7 => _7.handlers, 'access', _8 => _8.onStatus, 'optionalCall', _9 => _9("open")]);
57
+ this.send({
58
+ type: "join",
59
+ room: this.cfg.room,
60
+ name: this.cfg.name,
61
+ color: this.cfg.color,
62
+ seed: this.cfg.seed
63
+ });
64
+ };
65
+ ws.onclose = () => {
66
+ this.joined = false;
67
+ _optionalChain([this, 'access', _10 => _10.handlers, 'access', _11 => _11.onStatus, 'optionalCall', _12 => _12("closed")]);
68
+ if (!this.closed)
69
+ this.scheduleReconnect();
70
+ };
71
+ ws.onerror = () => {
72
+ try {
73
+ ws.close();
74
+ } catch (e2) {
75
+ }
76
+ };
77
+ ws.onmessage = (ev) => {
78
+ try {
79
+ const msg = JSON.parse(String(ev.data));
80
+ this.onServer(msg);
81
+ } catch (e3) {
82
+ }
83
+ };
84
+ }
85
+ scheduleReconnect() {
86
+ if (this.reconnectTimer || this.closed)
87
+ return;
88
+ const delay = retryDelay(this.retry++);
89
+ this.reconnectTimer = setTimeout(() => {
90
+ this.reconnectTimer = null;
91
+ this.connect();
92
+ }, delay);
93
+ }
94
+ send(msg) {
95
+ if (_optionalChain([this, 'access', _13 => _13.ws, 'optionalAccess', _14 => _14.readyState]) === WebSocket.OPEN)
96
+ this.ws.send(JSON.stringify(msg));
97
+ }
98
+ onServer(msg) {
99
+ switch (msg.type) {
100
+ case "welcome": {
101
+ this.siteId = msg.siteId;
102
+ this.version = msg.version;
103
+ this.pending = [];
104
+ this.joined = true;
105
+ this.peers.clear();
106
+ for (const p of msg.peers)
107
+ this.peers.set(p.siteId, p);
108
+ this.handlers.onDoc(msg.doc, msg.version);
109
+ _optionalChain([this, 'access', _15 => _15.handlers, 'access', _16 => _16.onPeers, 'optionalCall', _17 => _17(this.peersList)]);
110
+ break;
111
+ }
112
+ case "resync": {
113
+ this.version = msg.version;
114
+ this.pending = [];
115
+ this.handlers.onDoc(msg.doc, msg.version);
116
+ break;
117
+ }
118
+ case "op": {
119
+ if (!this.joined)
120
+ return;
121
+ if (msg.siteId === this.siteId) {
122
+ if (msg.clientSeq != null)
123
+ this.pending = this.pending.filter((p) => p.clientSeq > msg.clientSeq);
124
+ this.version = msg.version;
125
+ return;
126
+ }
127
+ const pendingAs = this.pending.map((p) => ({ siteId: this.siteId, steps: p.steps }));
128
+ const { remote, pending: nextP } = _chunkJS527FDVcjs.transformRemoteAgainstPending.call(void 0,
129
+ msg.steps,
130
+ msg.siteId,
131
+ pendingAs,
132
+ this.siteId
133
+ );
134
+ this.pending = this.pending.map((p, i) => ({
135
+ ...p,
136
+ steps: _nullishCoalesce(_optionalChain([nextP, 'access', _18 => _18[i], 'optionalAccess', _19 => _19.steps]), () => ( p.steps))
137
+ }));
138
+ this.version = msg.version;
139
+ if (remote.length)
140
+ this.handlers.onRemote(remote);
141
+ break;
142
+ }
143
+ case "presence": {
144
+ const cur = _nullishCoalesce(this.peers.get(msg.siteId), () => ( { siteId: msg.siteId }));
145
+ this.peers.set(msg.siteId, {
146
+ ...cur,
147
+ name: _nullishCoalesce(msg.name, () => ( cur.name)),
148
+ color: _nullishCoalesce(msg.color, () => ( cur.color)),
149
+ sel: msg.sel === void 0 ? cur.sel : msg.sel
150
+ });
151
+ _optionalChain([this, 'access', _20 => _20.handlers, 'access', _21 => _21.onPeers, 'optionalCall', _22 => _22(this.peersList)]);
152
+ break;
153
+ }
154
+ case "peer-join": {
155
+ this.peers.set(msg.siteId, { siteId: msg.siteId, name: msg.name, color: msg.color });
156
+ _optionalChain([this, 'access', _23 => _23.handlers, 'access', _24 => _24.onPeers, 'optionalCall', _25 => _25(this.peersList)]);
157
+ break;
158
+ }
159
+ case "peer-leave": {
160
+ this.peers.delete(msg.siteId);
161
+ _optionalChain([this, 'access', _26 => _26.handlers, 'access', _27 => _27.onPeers, 'optionalCall', _28 => _28(this.peersList)]);
162
+ break;
163
+ }
164
+ case "error":
165
+ _optionalChain([this, 'access', _29 => _29.handlers, 'access', _30 => _30.onError, 'optionalCall', _31 => _31(msg.message)]);
166
+ break;
167
+ case "pong":
168
+ break;
169
+ }
170
+ }
171
+ /** 本地已 apply 的 steps 推上服务器 */
172
+ push(steps) {
173
+ if (!steps.length || !this.joined || this.siteId <= 0)
174
+ return;
175
+ const clientSeq = ++this.clientSeq;
176
+ const baseVersion = this.version;
177
+ this.pending.push({ clientSeq, baseVersion, steps });
178
+ this.send({ type: "op", baseVersion, clientSeq, steps });
179
+ }
180
+ presence(sel) {
181
+ if (!this.joined)
182
+ return;
183
+ this.send({
184
+ type: "presence",
185
+ name: this.cfg.name,
186
+ color: this.cfg.color,
187
+ sel
188
+ });
189
+ }
190
+ close() {
191
+ this.closed = true;
192
+ if (this.reconnectTimer)
193
+ clearTimeout(this.reconnectTimer);
194
+ try {
195
+ _optionalChain([this, 'access', _32 => _32.ws, 'optionalAccess', _33 => _33.close, 'call', _34 => _34()]);
196
+ } catch (e4) {
197
+ }
198
+ this.ws = null;
199
+ }
200
+ }, _class);
201
+ function connectCollab(cfg, handlers) {
202
+ return new CollabClient(cfg, handlers);
203
+ }
204
+
205
+
206
+
207
+
208
+ exports.CollabClient = CollabClient; exports.connectCollab = connectCollab;
@@ -0,0 +1,16 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/tree/types.ts
2
+ function isText(n) {
3
+ return "text" in n;
4
+ }
5
+ function isElement(n) {
6
+ return "children" in n;
7
+ }
8
+ function isInlineAtom(n) {
9
+ return isElement(n) && (n.type === "mention" || n.type === "hard_break" || n.type === "math");
10
+ }
11
+
12
+
13
+
14
+
15
+
16
+ exports.isText = isText; exports.isElement = isElement; exports.isInlineAtom = isInlineAtom;
@@ -1,12 +1,14 @@
1
1
  import {
2
2
  appendText,
3
3
  insertAfter,
4
- isText,
5
4
  mdToDoc,
6
5
  normalizeToLeaf,
7
6
  pathOfId,
8
7
  rewriteNode
9
- } from "./chunk-J3QWW6CN.js";
8
+ } from "./chunk-MFYF2A6U.js";
9
+ import {
10
+ isText
11
+ } from "./chunk-LA5SBCGQ.js";
10
12
 
11
13
  // src/view/selection.ts
12
14
  function rootEl(root) {