markloom 0.5.0 → 0.5.1

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 CHANGED
@@ -24,7 +24,9 @@ new Editor(el, doc, {
24
24
 
25
25
  That is enough: local edits upload as Steps; remote Steps apply through OT. No extension list.
26
26
 
27
- Optional: `color`, `seed` (empty room init; default = constructor `doc`).
27
+ **Presence (built-in, Feishu-style):** top-right avatar stack (self + peers) and colored remote carets with name labels. Colors auto-assign by `siteId` unless you set `color`. No extra config.
28
+
29
+ Optional: `color`, `name`, `seed` (empty room init; default = constructor `doc`).
28
30
 
29
31
  Programmatic: `ed.collab` → `CollabClient` (`peersList`, `close()`).
30
32
 
package/dist/ai/index.cjs CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- var _chunkSLRD6FMPcjs = require('../chunk-SLRD6FMP.cjs');
9
+ var _chunkYIFOGP2Dcjs = require('../chunk-YIFOGP2D.cjs');
10
10
  require('../chunk-4TMHVNL6.cjs');
11
11
  require('../chunk-GQ2HHRGI.cjs');
12
12
 
@@ -17,4 +17,4 @@ require('../chunk-GQ2HHRGI.cjs');
17
17
 
18
18
 
19
19
 
20
- exports.MarkloomAi = _chunkSLRD6FMPcjs.MarkloomAi; exports.aiInstructionFromQuery = _chunkSLRD6FMPcjs.aiInstructionFromQuery; exports.blockPlainText = _chunkSLRD6FMPcjs.blockPlainText; exports.createAiStream = _chunkSLRD6FMPcjs.createAiStream; exports.focusBlockId = _chunkSLRD6FMPcjs.focusBlockId; exports.precedingPlainText = _chunkSLRD6FMPcjs.precedingPlainText; exports.startAiWrite = _chunkSLRD6FMPcjs.startAiWrite;
20
+ exports.MarkloomAi = _chunkYIFOGP2Dcjs.MarkloomAi; exports.aiInstructionFromQuery = _chunkYIFOGP2Dcjs.aiInstructionFromQuery; exports.blockPlainText = _chunkYIFOGP2Dcjs.blockPlainText; exports.createAiStream = _chunkYIFOGP2Dcjs.createAiStream; exports.focusBlockId = _chunkYIFOGP2Dcjs.focusBlockId; exports.precedingPlainText = _chunkYIFOGP2Dcjs.precedingPlainText; exports.startAiWrite = _chunkYIFOGP2Dcjs.startAiWrite;
@@ -1,8 +1,8 @@
1
- export { A as AiConfig, a as AiPromptMode, b as AiProvider, c as AiStreamFn, d as AiStreamInput, e as AiWriteMode, f as AiWriteSession, M as MarkloomAi, g as MarkloomAiOpts, h as blockPlainText, i as createAiStream, j as focusBlockId, s as startAiWrite } from '../controller-CVIZI--A.cjs';
1
+ export { A as AiConfig, a as AiPromptMode, b as AiProvider, c as AiStreamFn, d as AiStreamInput, e as AiWriteMode, f as AiWriteSession, M as MarkloomAi, g as MarkloomAiOpts, h as blockPlainText, i as createAiStream, j as focusBlockId, s as startAiWrite } from '../controller-DQITsFhy.cjs';
2
2
  import { E as ElementNode } from '../step-BrvHIK7O.cjs';
3
3
  import 'preact';
4
4
  import '../comments-DlExS6f-.cjs';
5
- import '../client-BNams4wv.cjs';
5
+ import '../client-ZRZj2nkq.cjs';
6
6
 
7
7
  /** 顶层块 [0, blockIdx) 纯文本拼接(作 /ai 上下文) */
8
8
  declare function precedingPlainText(doc: ElementNode, blockIdx: number): string;
@@ -1,8 +1,8 @@
1
- export { A as AiConfig, a as AiPromptMode, b as AiProvider, c as AiStreamFn, d as AiStreamInput, e as AiWriteMode, f as AiWriteSession, M as MarkloomAi, g as MarkloomAiOpts, h as blockPlainText, i as createAiStream, j as focusBlockId, s as startAiWrite } from '../controller-BkY1VPzc.js';
1
+ export { A as AiConfig, a as AiPromptMode, b as AiProvider, c as AiStreamFn, d as AiStreamInput, e as AiWriteMode, f as AiWriteSession, M as MarkloomAi, g as MarkloomAiOpts, h as blockPlainText, i as createAiStream, j as focusBlockId, s as startAiWrite } from '../controller-BNuuUM3E.js';
2
2
  import { E as ElementNode } from '../step-BrvHIK7O.js';
3
3
  import 'preact';
4
4
  import '../comments-PhnR7Gkn.js';
5
- import '../client-A0ZQJnyR.js';
5
+ import '../client-6KiH4kGK.js';
6
6
 
7
7
  /** 顶层块 [0, blockIdx) 纯文本拼接(作 /ai 上下文) */
8
8
  declare function precedingPlainText(doc: ElementNode, blockIdx: number): string;
package/dist/ai/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  focusBlockId,
7
7
  precedingPlainText,
8
8
  startAiWrite
9
- } from "../chunk-JMOJU5EU.js";
9
+ } from "../chunk-FCH7LZFI.js";
10
10
  import "../chunk-MFYF2A6U.js";
11
11
  import "../chunk-LA5SBCGQ.js";
12
12
  export {
@@ -2,6 +2,34 @@ import {
2
2
  transformRemoteAgainstPending
3
3
  } from "./chunk-RLTHVQNQ.js";
4
4
 
5
+ // src/collab/colors.ts
6
+ var COLLAB_COLORS = [
7
+ "#3370ff",
8
+ "#f54a45",
9
+ "#ff8800",
10
+ "#00b578",
11
+ "#7b61ff",
12
+ "#e13edb",
13
+ "#14c0ff",
14
+ "#8f959e",
15
+ "#d83931",
16
+ "#2ea121"
17
+ ];
18
+ function collabColor(siteId, explicit) {
19
+ if (explicit)
20
+ return explicit;
21
+ const i = Math.abs((siteId || 1) - 1) % COLLAB_COLORS.length;
22
+ return COLLAB_COLORS[i];
23
+ }
24
+ function collabInitial(name, siteId) {
25
+ const n = (name || "").trim();
26
+ if (n) {
27
+ const ch = n[0];
28
+ return /[a-z]/i.test(ch) ? ch.toUpperCase() : ch;
29
+ }
30
+ return String(siteId ?? "?");
31
+ }
32
+
5
33
  // src/collab/client.ts
6
34
  function toWsUrl(url, room) {
7
35
  let u = url.replace(/\/+$/, "");
@@ -102,9 +130,21 @@ var CollabClient = class {
102
130
  this.version = msg.version;
103
131
  this.pending = [];
104
132
  this.joined = true;
133
+ if (!this.cfg.color)
134
+ this.cfg.color = collabColor(msg.siteId);
105
135
  this.peers.clear();
106
- for (const p of msg.peers)
107
- this.peers.set(p.siteId, p);
136
+ for (const p of msg.peers) {
137
+ this.peers.set(p.siteId, {
138
+ ...p,
139
+ color: collabColor(p.siteId, p.color)
140
+ });
141
+ }
142
+ this.handlers.onWelcome?.({
143
+ siteId: msg.siteId,
144
+ version: msg.version,
145
+ name: this.cfg.name,
146
+ color: this.cfg.color
147
+ });
108
148
  this.handlers.onDoc(msg.doc, msg.version);
109
149
  this.handlers.onPeers?.(this.peersList);
110
150
  break;
@@ -145,14 +185,18 @@ var CollabClient = class {
145
185
  this.peers.set(msg.siteId, {
146
186
  ...cur,
147
187
  name: msg.name ?? cur.name,
148
- color: msg.color ?? cur.color,
188
+ color: collabColor(msg.siteId, msg.color ?? cur.color),
149
189
  sel: msg.sel === void 0 ? cur.sel : msg.sel
150
190
  });
151
191
  this.handlers.onPeers?.(this.peersList);
152
192
  break;
153
193
  }
154
194
  case "peer-join": {
155
- this.peers.set(msg.siteId, { siteId: msg.siteId, name: msg.name, color: msg.color });
195
+ this.peers.set(msg.siteId, {
196
+ siteId: msg.siteId,
197
+ name: msg.name,
198
+ color: collabColor(msg.siteId, msg.color)
199
+ });
156
200
  this.handlers.onPeers?.(this.peersList);
157
201
  break;
158
202
  }
@@ -183,7 +227,7 @@ var CollabClient = class {
183
227
  this.send({
184
228
  type: "presence",
185
229
  name: this.cfg.name,
186
- color: this.cfg.color,
230
+ color: this.cfg.color ?? collabColor(this.siteId),
187
231
  sel
188
232
  });
189
233
  }
@@ -203,6 +247,9 @@ function connectCollab(cfg, handlers) {
203
247
  }
204
248
 
205
249
  export {
250
+ COLLAB_COLORS,
251
+ collabColor,
252
+ collabInitial,
206
253
  CollabClient,
207
254
  connectCollab
208
255
  };
@@ -2,6 +2,34 @@
2
2
 
3
3
  var _chunkJS527FDVcjs = require('./chunk-JS527FDV.cjs');
4
4
 
5
+ // src/collab/colors.ts
6
+ var COLLAB_COLORS = [
7
+ "#3370ff",
8
+ "#f54a45",
9
+ "#ff8800",
10
+ "#00b578",
11
+ "#7b61ff",
12
+ "#e13edb",
13
+ "#14c0ff",
14
+ "#8f959e",
15
+ "#d83931",
16
+ "#2ea121"
17
+ ];
18
+ function collabColor(siteId, explicit) {
19
+ if (explicit)
20
+ return explicit;
21
+ const i = Math.abs((siteId || 1) - 1) % COLLAB_COLORS.length;
22
+ return COLLAB_COLORS[i];
23
+ }
24
+ function collabInitial(name, siteId) {
25
+ const n = (name || "").trim();
26
+ if (n) {
27
+ const ch = n[0];
28
+ return /[a-z]/i.test(ch) ? ch.toUpperCase() : ch;
29
+ }
30
+ return String(_nullishCoalesce(siteId, () => ( "?")));
31
+ }
32
+
5
33
  // src/collab/client.ts
6
34
  function toWsUrl(url, room) {
7
35
  let u = url.replace(/\/+$/, "");
@@ -102,11 +130,23 @@ var CollabClient = (_class = class {
102
130
  this.version = msg.version;
103
131
  this.pending = [];
104
132
  this.joined = true;
133
+ if (!this.cfg.color)
134
+ this.cfg.color = collabColor(msg.siteId);
105
135
  this.peers.clear();
106
- for (const p of msg.peers)
107
- this.peers.set(p.siteId, p);
136
+ for (const p of msg.peers) {
137
+ this.peers.set(p.siteId, {
138
+ ...p,
139
+ color: collabColor(p.siteId, p.color)
140
+ });
141
+ }
142
+ _optionalChain([this, 'access', _15 => _15.handlers, 'access', _16 => _16.onWelcome, 'optionalCall', _17 => _17({
143
+ siteId: msg.siteId,
144
+ version: msg.version,
145
+ name: this.cfg.name,
146
+ color: this.cfg.color
147
+ })]);
108
148
  this.handlers.onDoc(msg.doc, msg.version);
109
- _optionalChain([this, 'access', _15 => _15.handlers, 'access', _16 => _16.onPeers, 'optionalCall', _17 => _17(this.peersList)]);
149
+ _optionalChain([this, 'access', _18 => _18.handlers, 'access', _19 => _19.onPeers, 'optionalCall', _20 => _20(this.peersList)]);
110
150
  break;
111
151
  }
112
152
  case "resync": {
@@ -133,7 +173,7 @@ var CollabClient = (_class = class {
133
173
  );
134
174
  this.pending = this.pending.map((p, i) => ({
135
175
  ...p,
136
- steps: _nullishCoalesce(_optionalChain([nextP, 'access', _18 => _18[i], 'optionalAccess', _19 => _19.steps]), () => ( p.steps))
176
+ steps: _nullishCoalesce(_optionalChain([nextP, 'access', _21 => _21[i], 'optionalAccess', _22 => _22.steps]), () => ( p.steps))
137
177
  }));
138
178
  this.version = msg.version;
139
179
  if (remote.length)
@@ -145,24 +185,28 @@ var CollabClient = (_class = class {
145
185
  this.peers.set(msg.siteId, {
146
186
  ...cur,
147
187
  name: _nullishCoalesce(msg.name, () => ( cur.name)),
148
- color: _nullishCoalesce(msg.color, () => ( cur.color)),
188
+ color: collabColor(msg.siteId, _nullishCoalesce(msg.color, () => ( cur.color))),
149
189
  sel: msg.sel === void 0 ? cur.sel : msg.sel
150
190
  });
151
- _optionalChain([this, 'access', _20 => _20.handlers, 'access', _21 => _21.onPeers, 'optionalCall', _22 => _22(this.peersList)]);
191
+ _optionalChain([this, 'access', _23 => _23.handlers, 'access', _24 => _24.onPeers, 'optionalCall', _25 => _25(this.peersList)]);
152
192
  break;
153
193
  }
154
194
  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)]);
195
+ this.peers.set(msg.siteId, {
196
+ siteId: msg.siteId,
197
+ name: msg.name,
198
+ color: collabColor(msg.siteId, msg.color)
199
+ });
200
+ _optionalChain([this, 'access', _26 => _26.handlers, 'access', _27 => _27.onPeers, 'optionalCall', _28 => _28(this.peersList)]);
157
201
  break;
158
202
  }
159
203
  case "peer-leave": {
160
204
  this.peers.delete(msg.siteId);
161
- _optionalChain([this, 'access', _26 => _26.handlers, 'access', _27 => _27.onPeers, 'optionalCall', _28 => _28(this.peersList)]);
205
+ _optionalChain([this, 'access', _29 => _29.handlers, 'access', _30 => _30.onPeers, 'optionalCall', _31 => _31(this.peersList)]);
162
206
  break;
163
207
  }
164
208
  case "error":
165
- _optionalChain([this, 'access', _29 => _29.handlers, 'access', _30 => _30.onError, 'optionalCall', _31 => _31(msg.message)]);
209
+ _optionalChain([this, 'access', _32 => _32.handlers, 'access', _33 => _33.onError, 'optionalCall', _34 => _34(msg.message)]);
166
210
  break;
167
211
  case "pong":
168
212
  break;
@@ -183,7 +227,7 @@ var CollabClient = (_class = class {
183
227
  this.send({
184
228
  type: "presence",
185
229
  name: this.cfg.name,
186
- color: this.cfg.color,
230
+ color: _nullishCoalesce(this.cfg.color, () => ( collabColor(this.siteId))),
187
231
  sel
188
232
  });
189
233
  }
@@ -192,7 +236,7 @@ var CollabClient = (_class = class {
192
236
  if (this.reconnectTimer)
193
237
  clearTimeout(this.reconnectTimer);
194
238
  try {
195
- _optionalChain([this, 'access', _32 => _32.ws, 'optionalAccess', _33 => _33.close, 'call', _34 => _34()]);
239
+ _optionalChain([this, 'access', _35 => _35.ws, 'optionalAccess', _36 => _36.close, 'call', _37 => _37()]);
196
240
  } catch (e4) {
197
241
  }
198
242
  this.ws = null;
@@ -205,4 +249,7 @@ function connectCollab(cfg, handlers) {
205
249
 
206
250
 
207
251
 
208
- exports.CollabClient = CollabClient; exports.connectCollab = connectCollab;
252
+
253
+
254
+
255
+ exports.COLLAB_COLORS = COLLAB_COLORS; exports.collabColor = collabColor; exports.collabInitial = collabInitial; exports.CollabClient = CollabClient; exports.connectCollab = connectCollab;
@@ -660,7 +660,7 @@ var MarkloomAi = class {
660
660
  this.txOpen = false;
661
661
  this.txBlockId = null;
662
662
  }
663
- /** 整笔撤回事务(= undo) */
663
+ /** 整笔撤回事务(= undo);务必不留 aiDraft 孤儿段 */
664
664
  discard(blockId) {
665
665
  if (this.streamingId === blockId) {
666
666
  this.abort?.abort();
@@ -672,16 +672,21 @@ var MarkloomAi = class {
672
672
  }
673
673
  if (this.txOpen && (this.txBlockId === blockId || !this.txBlockId)) {
674
674
  this.rollback();
675
+ this.purgeDrafts(blockId);
675
676
  return;
676
677
  }
678
+ this.purgeDrafts(blockId, true);
679
+ }
680
+ /**
681
+ * 清掉 aiFrom=blockId 的顶层 draft;shell=true 时把 ai 壳也换成空段。
682
+ * discard / 异常恢复共用。
683
+ */
684
+ purgeDrafts(blockId, shell = false) {
677
685
  const ed = this.getEditor();
678
686
  if (!ed)
679
687
  return;
680
- const found = this.findAi(ed, blockId);
681
- if (!found)
682
- return;
683
- const steps = [];
684
688
  const doc = ed.doc;
689
+ const steps = [];
685
690
  const idxs = draftIndices(doc, blockId);
686
691
  for (let i = idxs.length - 1; i >= 0; i--)
687
692
  steps.push({ kind: "spliceChildren", path: [], at: idxs[i], remove: 1, insert: [] });
@@ -692,19 +697,20 @@ var MarkloomAi = class {
692
697
  shift++;
693
698
  }
694
699
  aiIdx -= shift;
695
- if (aiIdx >= 0) {
700
+ if (shell && aiIdx >= 0) {
696
701
  steps.push({
697
702
  kind: "replaceNode",
698
703
  path: [aiIdx],
699
- node: { type: "paragraph", id: found.node.id, children: [{ text: "" }] }
700
- });
701
- }
702
- if (steps.length) {
703
- ed.dispatch(steps, {
704
- anchor: { path: [Math.max(0, aiIdx), 0, 0] },
705
- head: { path: [Math.max(0, aiIdx), 0, 0] }
704
+ node: { type: "paragraph", id: blockId, children: [{ text: "" }] }
706
705
  });
707
706
  }
707
+ if (!steps.length)
708
+ return;
709
+ const caretAt = Math.max(0, aiIdx >= 0 ? aiIdx : idxs[0] ?? 0);
710
+ ed.dispatch(steps, {
711
+ anchor: { path: [caretAt, 0, 0] },
712
+ head: { path: [caretAt, 0, 0] }
713
+ });
708
714
  }
709
715
  act(blockId, action) {
710
716
  if (action === "accept")