@impro.social/impro-plugin 0.0.7 → 0.0.9

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 (2) hide show
  1. package/main.js +175 -12
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -22,24 +22,50 @@ function hostCall(method, ...args) {
22
22
  }
23
23
 
24
24
  const eventListeners = new Map();
25
+ const registeredEvents = new Set();
26
+
27
+ async function invokeListeners(listeners, event, args) {
28
+ for (const listener of listeners) {
29
+ try {
30
+ await listener(...args);
31
+ } catch (error) {
32
+ console.error(`"${event}" listener threw:`, error);
33
+ }
34
+ }
35
+ }
36
+
37
+ async function dispatchEvent(event, args) {
38
+ const listeners = eventListeners.get(event) ?? new Set();
39
+ switch (event) {
40
+ case "post-context-menu":
41
+ case "profile-context-menu": {
42
+ const menu = new Menu();
43
+ await invokeListeners(listeners, event, [menu, ...args]);
44
+ return menu._serialize();
45
+ }
46
+ case "post-composer-open": {
47
+ const composer = new Composer();
48
+ await invokeListeners(listeners, event, [composer, ...args]);
49
+ return composer._serialize();
50
+ }
51
+ default:
52
+ console.warn(`No dispatch case for plugin event "${event}".`);
53
+ return null;
54
+ }
55
+ }
25
56
 
26
57
  function addEventListener(event, listener) {
27
58
  let listeners = eventListeners.get(event);
28
59
  if (!listeners) {
29
60
  listeners = new Set();
30
61
  eventListeners.set(event, listeners);
62
+ }
63
+ listeners.add(listener);
64
+ // Register handler
65
+ if (!registeredEvents.has(event)) {
66
+ registeredEvents.add(event);
31
67
  const handlerId = uuid.create();
32
- callHandlers.set(handlerId, async (...args) => {
33
- const menu = new Menu();
34
- for (const eventListener of listeners) {
35
- try {
36
- await eventListener(menu, ...args);
37
- } catch (error) {
38
- console.error(`"${event}" listener threw:`, error);
39
- }
40
- }
41
- return menu._serialize();
42
- });
68
+ callHandlers.set(handlerId, (...args) => dispatchEvent(event, args));
43
69
  self.postMessage({
44
70
  type: "register",
45
71
  target: "eventListener",
@@ -47,7 +73,6 @@ function addEventListener(event, listener) {
47
73
  handlerId,
48
74
  });
49
75
  }
50
- listeners.add(listener);
51
76
  }
52
77
 
53
78
  export class MenuItem {
@@ -89,6 +114,32 @@ export class Menu {
89
114
  }
90
115
  }
91
116
 
117
+ export class Composer {
118
+ constructor() {
119
+ this._ops = [];
120
+ this._cursor = null;
121
+ }
122
+ setText(text) {
123
+ this._ops.push({ op: "set", text: String(text) });
124
+ return this;
125
+ }
126
+ appendText(text) {
127
+ this._ops.push({ op: "append", text: String(text) });
128
+ return this;
129
+ }
130
+ prependText(text) {
131
+ this._ops.push({ op: "prepend", text: String(text) });
132
+ return this;
133
+ }
134
+ setCursor(index) {
135
+ this._cursor = index;
136
+ return this;
137
+ }
138
+ _serialize() {
139
+ return { ops: this._ops, cursor: this._cursor };
140
+ }
141
+ }
142
+
92
143
  class PluginData {
93
144
  getPost(uri) {
94
145
  return hostCall("getPost", { uri });
@@ -259,6 +310,30 @@ export class Plugin {
259
310
  });
260
311
  }
261
312
 
313
+ // callback(tokens, context) receives the rich-text token stream for one
314
+ // post and returns a new token array (or the input unchanged). The host
315
+ // batches all posts of a render into one call per plugin.
316
+ registerRichTextTransform(callback = (tokens) => tokens) {
317
+ const handlerId = uuid.create();
318
+ callHandlers.set(handlerId, async (batch) => {
319
+ const results = [];
320
+ for (const { tokens, context } of batch) {
321
+ try {
322
+ const value = await callback(tokens, context);
323
+ results.push({ value: serializeTransformTokens(value) });
324
+ } catch (error) {
325
+ results.push({ error: error?.message ?? String(error) });
326
+ }
327
+ }
328
+ return results;
329
+ });
330
+ self.postMessage({
331
+ type: "register",
332
+ target: "richTextTransform",
333
+ handlerId,
334
+ });
335
+ }
336
+
262
337
  registerSlot(name, callback = () => null) {
263
338
  const handlerId = uuid.create();
264
339
  callHandlers.set(handlerId, async (context) => {
@@ -303,6 +378,69 @@ export class Plugin {
303
378
  }
304
379
  }
305
380
 
381
+ function serializeTransformTokens(tokens) {
382
+ if (!Array.isArray(tokens)) return tokens;
383
+ return tokens.map((token) => {
384
+ if (
385
+ (token?.type === "inline" || token?.type === "block") &&
386
+ token.node instanceof VirtualEl
387
+ ) {
388
+ return { ...token, node: token.node._serialize() };
389
+ }
390
+ return token;
391
+ });
392
+ }
393
+
394
+ // Concatenates the renderable text of a token stream into a string with a position map,
395
+ // so a transform can pattern-match across token boundaries and map matches back to tokens.
396
+ export class FlattenedTokens {
397
+ constructor(tokens) {
398
+ this._segments = [];
399
+ let text = "";
400
+ for (const token of tokens) {
401
+ let value = null;
402
+ if (token.type === "text") value = token.value;
403
+ else if (token.type === "facet") value = token.text;
404
+ const start = text.length;
405
+ if (value != null) text += value;
406
+ this._segments.push({ token, start, end: text.length });
407
+ }
408
+ this.text = text;
409
+ }
410
+
411
+ // emit an inert range (no facets)
412
+ textFor(start, end) {
413
+ return this.text.slice(start, end);
414
+ }
415
+
416
+ // emit a live range, demoting partial facet tokens to text tokens
417
+ tokensFor(start, end) {
418
+ const out = [];
419
+ for (const segment of this._segments) {
420
+ if (segment.start === segment.end) {
421
+ if (segment.start >= start && segment.start < end) {
422
+ out.push(segment.token);
423
+ }
424
+ continue;
425
+ }
426
+ if (segment.end <= start || segment.start >= end) continue;
427
+ const from = Math.max(start, segment.start);
428
+ const to = Math.min(end, segment.end);
429
+ const isWholeToken = from === segment.start && to === segment.end;
430
+ if (isWholeToken) {
431
+ out.push(segment.token);
432
+ } else {
433
+ out.push({ type: "text", value: this.text.slice(from, to) });
434
+ }
435
+ }
436
+ return out;
437
+ }
438
+ }
439
+
440
+ export function flattenForScan(tokens) {
441
+ return new FlattenedTokens(tokens);
442
+ }
443
+
306
444
  const openModals = new Map();
307
445
 
308
446
  export class Modal {
@@ -382,6 +520,11 @@ export class Setting {
382
520
  callback(component);
383
521
  return this;
384
522
  }
523
+ addTextArea(callback) {
524
+ const component = new TextAreaComponent(this.controlEl);
525
+ callback(component);
526
+ return this;
527
+ }
385
528
  addToggle(callback) {
386
529
  const component = new ToggleComponent(this.controlEl);
387
530
  callback(component);
@@ -420,6 +563,26 @@ class TextComponent {
420
563
  }
421
564
  }
422
565
 
566
+ class TextAreaComponent {
567
+ constructor(containerEl) {
568
+ this.el = containerEl.createEl("textarea", {
569
+ cls: "setting-item-textarea",
570
+ });
571
+ }
572
+ setValue(value) {
573
+ this.el.setText(value == null ? "" : String(value));
574
+ return this;
575
+ }
576
+ setPlaceholder(value) {
577
+ this.el.setAttr("placeholder", value);
578
+ return this;
579
+ }
580
+ onChange(callback) {
581
+ this.el.onChange((event) => callback(event.target.value));
582
+ return this;
583
+ }
584
+ }
585
+
423
586
  class ToggleComponent {
424
587
  constructor(containerEl) {
425
588
  this.el = containerEl.createEl("toggle-switch", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@impro.social/impro-plugin",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
6
  "license": "0BSD",