@impro.social/impro-plugin 0.0.18 → 0.0.20

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 +150 -69
  2. package/package.json +8 -2
package/main.js CHANGED
@@ -1,9 +1,7 @@
1
1
  export class SimpleUUID {
2
- constructor() {
3
- this._id = 0;
4
- }
2
+ #id = 0;
5
3
  create() {
6
- return this._id++;
4
+ return this.#id++;
7
5
  }
8
6
  }
9
7
 
@@ -79,6 +77,7 @@ export class MenuItem {
79
77
  constructor() {
80
78
  this.title = "";
81
79
  this.icon = null;
80
+ /** @internal */
82
81
  this._callback = () => {};
83
82
  }
84
83
  setTitle(title) {
@@ -105,6 +104,7 @@ export class Menu {
105
104
  this.items.push(item);
106
105
  return this;
107
106
  }
107
+ /** @internal */
108
108
  _serialize() {
109
109
  return this.items.map((item) => {
110
110
  const handlerId = uuid.create();
@@ -117,32 +117,31 @@ export class Menu {
117
117
  }
118
118
 
119
119
  export class Composer {
120
- constructor() {
121
- this._ops = [];
122
- this._cursor = null;
123
- }
120
+ #ops = [];
121
+ #cursor = null;
124
122
  setText(text) {
125
- this._ops.push({ op: "set", text: String(text) });
123
+ this.#ops.push({ op: "set", text: String(text) });
126
124
  return this;
127
125
  }
128
126
  appendText(text) {
129
- this._ops.push({ op: "append", text: String(text) });
127
+ this.#ops.push({ op: "append", text: String(text) });
130
128
  return this;
131
129
  }
132
130
  prependText(text) {
133
- this._ops.push({ op: "prepend", text: String(text) });
131
+ this.#ops.push({ op: "prepend", text: String(text) });
134
132
  return this;
135
133
  }
136
134
  setCursor(index) {
137
- this._cursor = index;
135
+ this.#cursor = index;
138
136
  return this;
139
137
  }
138
+ /** @internal */
140
139
  _serialize() {
141
- return { ops: this._ops, cursor: this._cursor };
140
+ return { ops: this.#ops, cursor: this.#cursor };
142
141
  }
143
142
  }
144
143
 
145
- class PluginData {
144
+ export class PluginData {
146
145
  getPost(uri) {
147
146
  return hostCall("getPost", { uri });
148
147
  }
@@ -165,7 +164,8 @@ class PluginData {
165
164
  }
166
165
  }
167
166
 
168
- class App {
167
+ export class App {
168
+ /** @internal */
169
169
  constructor() {
170
170
  this.currentUser = null;
171
171
  this.data = new PluginData();
@@ -234,35 +234,38 @@ function serializeFetchInit(init) {
234
234
  return serialized;
235
235
  }
236
236
 
237
- class PluginResponse {
237
+ export class PluginResponse {
238
+ #body;
239
+ /** @internal */
238
240
  constructor({ status, ok, headers, body }) {
239
241
  this.status = status;
240
242
  this.ok = ok;
241
243
  this.headers = new Map(Object.entries(headers ?? {}));
242
- this._body = body;
244
+ this.#body = body;
243
245
  }
244
246
  async text() {
245
- return this._body;
247
+ return this.#body;
246
248
  }
247
249
  async json() {
248
- return JSON.parse(this._body);
250
+ return JSON.parse(this.#body);
249
251
  }
250
252
  }
251
253
 
252
254
  export class Notice {
255
+ #toastId = uuid.create();
256
+ #timeout;
257
+ #hidden = false;
253
258
  constructor(message, timeout = 0) {
254
- this._toastId = uuid.create();
255
- this._timeout = timeout;
256
- this._hidden = false;
259
+ this.#timeout = timeout;
257
260
  this.noticeEl = new VirtualEl("div");
258
261
  this.noticeEl.addClass("toast");
259
262
  this.noticeEl.setText(message);
260
263
  queueMicrotask(() => {
261
- if (this._hidden) return;
264
+ if (this.#hidden) return;
262
265
  hostCall("showToast", {
263
- toastId: this._toastId,
266
+ toastId: this.#toastId,
264
267
  element: this.noticeEl._serialize(),
265
- timeout: this._timeout,
268
+ timeout: this.#timeout,
266
269
  });
267
270
  });
268
271
  }
@@ -271,36 +274,37 @@ export class Notice {
271
274
  return this;
272
275
  }
273
276
  hide() {
274
- if (this._hidden) return;
275
- this._hidden = true;
276
- hostCall("hideToast", { toastId: this._toastId });
277
+ if (this.#hidden) return;
278
+ this.#hidden = true;
279
+ hostCall("hideToast", { toastId: this.#toastId });
277
280
  }
278
281
  }
279
282
 
280
283
  export class StyleSnippet {
284
+ #snippetId = uuid.create();
285
+ #removed = false;
281
286
  constructor(cssText) {
282
- this._snippetId = uuid.create();
283
- this._removed = false;
284
287
  this.ready = new Promise((resolve, reject) => {
285
288
  queueMicrotask(() => {
286
- if (this._removed) return resolve();
289
+ if (this.#removed) return resolve();
287
290
  hostCall("applyStyleSnippet", {
288
- snippetId: this._snippetId,
291
+ snippetId: this.#snippetId,
289
292
  cssText,
290
293
  }).then(resolve, reject);
291
294
  });
292
295
  });
293
296
  }
294
297
  remove() {
295
- if (this._removed) return;
296
- this._removed = true;
297
- hostCall("removeStyleSnippet", { snippetId: this._snippetId });
298
+ if (this.#removed) return;
299
+ this.#removed = true;
300
+ hostCall("removeStyleSnippet", { snippetId: this.#snippetId });
298
301
  }
299
302
  }
300
303
 
301
304
  let registered = false;
302
305
 
303
306
  export class Plugin {
307
+ #settingTab = null;
304
308
  constructor() {
305
309
  this.app = new App();
306
310
  }
@@ -351,7 +355,7 @@ export class Plugin {
351
355
  name: tab.name ?? null,
352
356
  displayHandlerId,
353
357
  });
354
- this._settingTab = tab;
358
+ this.#settingTab = tab;
355
359
  }
356
360
 
357
361
  addFeedFilter(callback = () => {}) {
@@ -395,32 +399,86 @@ export class Plugin {
395
399
  });
396
400
  }
397
401
 
398
- registerSlot(name, callback = () => null) {
402
+ // options.cacheKey: array of context fields. If provided, the host
403
+ // will treat the slot content as a pure function of these fields
404
+ // - omitting other fields in the callback and caching return values
405
+ // until they're invalidated by refreshSlot(). An empty array declares
406
+ // that the content depends on no context at all, so one cached result
407
+ // serves every instance.
408
+ //
409
+ // The host batches all pending contexts of a render into one call.
410
+ registerSlot(name, callback = () => null, options = {}) {
399
411
  const handlerId = uuid.create();
400
- callHandlers.set(handlerId, async (context) => {
401
- const result = await callback(context);
412
+ callHandlers.set(handlerId, async (batch) => {
413
+ const results = [];
414
+ for (const context of batch) {
415
+ try {
416
+ results.push({
417
+ value: await getSlotContent(name, callback, context),
418
+ });
419
+ } catch (error) {
420
+ results.push({ error: error?.message ?? String(error) });
421
+ }
422
+ }
423
+ return results;
424
+ });
425
+ const cacheKey = Array.isArray(options.cacheKey)
426
+ ? options.cacheKey.filter((field) => typeof field === "string")
427
+ : null;
428
+ self.postMessage({
429
+ type: "register",
430
+ target: "slot",
431
+ name,
432
+ handlerId,
433
+ cacheKey,
434
+ batch: true,
435
+ });
436
+ }
437
+
438
+ registerPage({ id, title = null, display = () => null }) {
439
+ const displayHandlerId = uuid.create();
440
+ callHandlers.set(displayHandlerId, async () => {
441
+ const result = await display();
402
442
  if (result == null) return null;
403
443
  if (!(result instanceof VirtualEl)) {
404
444
  const description = result?.constructor?.name ?? typeof result;
405
445
  throw new Error(
406
- `Slot "${name}" must return a VirtualEl (or null), got ${description}`,
446
+ `Page "${id}" must return a VirtualEl or null, got ${description}`,
407
447
  );
408
448
  }
409
449
  return result._serialize();
410
450
  });
411
451
  self.postMessage({
412
452
  type: "register",
413
- target: "slot",
414
- name,
415
- handlerId,
453
+ target: "page",
454
+ id,
455
+ title,
456
+ displayHandlerId,
416
457
  });
417
458
  }
418
459
 
419
- // Makes every mounted <plugin-slot name=...> re-invoke this plugin's
420
- // registered callbacks for that slot. Useful when a slot's content depends
421
- // on data that resolves asynchronously after the initial render.
422
- refreshSlot(name) {
423
- return hostCall("refreshSlot", { name });
460
+ // Navigates the user to one of this plugin's registered pages.
461
+ openPage(pageId) {
462
+ return hostCall("openPage", { pageId });
463
+ }
464
+
465
+ // Re-invokes a registered page's display callback if the page is open.
466
+ // options.reset also discards the rendered tree instead of patching
467
+ refreshPage(pageId, { reset = false } = {}) {
468
+ return hostCall("refreshPage", { pageId, reset });
469
+ }
470
+
471
+ // Makes mounted <plugin-slot name=...> instances re-invoke this plugin's
472
+ // registered callback for that slot, and drops any cached results. Useful
473
+ // when a slot's content depends on plugin state that changed after render.
474
+ //
475
+ // options.keys: array of matcher objects to be OR'd together,
476
+ // e.g. [{ did: "..." }] - any matching slots will be invalidated / refreshed.
477
+ // Omit to refresh every instance. A slot registered with a cacheKey can only
478
+ // be matched on those declared fields, since its output depends on nothing
479
+ // else.
480
+ refreshSlot(name, options = {}) {
481
+ return hostCall("refreshSlot", { name, keys: options.keys ?? null });
424
482
  }
425
483
 
426
484
  onload() {}
@@ -446,6 +504,18 @@ export class Plugin {
446
504
  }
447
505
  }
448
506
 
507
+ async function getSlotContent(name, callback, context) {
508
+ const result = await callback(context);
509
+ if (result == null) return null;
510
+ if (!(result instanceof VirtualEl)) {
511
+ const description = result?.constructor?.name ?? typeof result;
512
+ throw new Error(
513
+ `Slot "${name}" must return a VirtualEl or null, got ${description}`,
514
+ );
515
+ }
516
+ return result._serialize();
517
+ }
518
+
449
519
  function serializeTransformTokens(tokens) {
450
520
  if (!Array.isArray(tokens)) return tokens;
451
521
  return tokens.map((token) => {
@@ -462,8 +532,8 @@ function serializeTransformTokens(tokens) {
462
532
  // Concatenates the renderable text of a token stream into a string with a position map,
463
533
  // so a transform can pattern-match across token boundaries and map matches back to tokens.
464
534
  export class FlattenedTokens {
535
+ #segments = [];
465
536
  constructor(tokens) {
466
- this._segments = [];
467
537
  let text = "";
468
538
  for (const token of tokens) {
469
539
  let value = null;
@@ -471,7 +541,7 @@ export class FlattenedTokens {
471
541
  else if (token.type === "facet") value = token.text;
472
542
  const start = text.length;
473
543
  if (value != null) text += value;
474
- this._segments.push({ token, start, end: text.length });
544
+ this.#segments.push({ token, start, end: text.length });
475
545
  }
476
546
  this.text = text;
477
547
  }
@@ -484,7 +554,7 @@ export class FlattenedTokens {
484
554
  // emit a live range, demoting partial facet tokens to text tokens
485
555
  tokensFor(start, end) {
486
556
  const out = [];
487
- for (const segment of this._segments) {
557
+ for (const segment of this.#segments) {
488
558
  if (segment.start === segment.end) {
489
559
  if (segment.start >= start && segment.start < end) {
490
560
  out.push(segment.token);
@@ -512,22 +582,22 @@ export function flattenForScan(tokens) {
512
582
  const openModals = new Map();
513
583
 
514
584
  export class Modal {
585
+ #modalId = uuid.create();
515
586
  constructor() {
516
- this._modalId = uuid.create();
517
587
  this.contentEl = new VirtualEl("div");
518
588
  this.titleEl = new VirtualEl("h2");
519
589
  }
520
590
 
521
591
  open() {
522
- if (openModals.has(this._modalId)) return;
523
- openModals.set(this._modalId, this);
592
+ if (openModals.has(this.#modalId)) return;
593
+ openModals.set(this.#modalId, this);
524
594
  this.onOpen();
525
595
  self.postMessage({
526
596
  type: "hostCall",
527
597
  method: "openModal",
528
598
  args: [
529
599
  {
530
- modalId: this._modalId,
600
+ modalId: this.#modalId,
531
601
  title: this.titleEl._serialize(),
532
602
  content: this.contentEl._serialize(),
533
603
  },
@@ -536,13 +606,13 @@ export class Modal {
536
606
  }
537
607
 
538
608
  update() {
539
- if (!openModals.has(this._modalId)) return;
609
+ if (!openModals.has(this.#modalId)) return;
540
610
  self.postMessage({
541
611
  type: "hostCall",
542
612
  method: "updateModal",
543
613
  args: [
544
614
  {
545
- modalId: this._modalId,
615
+ modalId: this.#modalId,
546
616
  title: this.titleEl._serialize(),
547
617
  content: this.contentEl._serialize(),
548
618
  },
@@ -551,12 +621,12 @@ export class Modal {
551
621
  }
552
622
 
553
623
  close() {
554
- if (!openModals.has(this._modalId)) return;
555
- openModals.delete(this._modalId);
624
+ if (!openModals.has(this.#modalId)) return;
625
+ openModals.delete(this.#modalId);
556
626
  self.postMessage({
557
627
  type: "hostCall",
558
628
  method: "closeModal",
559
- args: [{ modalId: this._modalId }],
629
+ args: [{ modalId: this.#modalId }],
560
630
  });
561
631
  this.onClose();
562
632
  }
@@ -625,7 +695,8 @@ export class Setting {
625
695
  }
626
696
  }
627
697
 
628
- class TextComponent {
698
+ export class TextComponent {
699
+ /** @internal */
629
700
  constructor(containerEl) {
630
701
  this.el = containerEl.createEl("input", {
631
702
  attr: { type: "text" },
@@ -646,7 +717,8 @@ class TextComponent {
646
717
  }
647
718
  }
648
719
 
649
- class TextAreaComponent {
720
+ export class TextAreaComponent {
721
+ /** @internal */
650
722
  constructor(containerEl) {
651
723
  this.el = containerEl.createEl("textarea", {
652
724
  cls: "setting-item-textarea",
@@ -666,7 +738,8 @@ class TextAreaComponent {
666
738
  }
667
739
  }
668
740
 
669
- class ToggleComponent {
741
+ export class ToggleComponent {
742
+ /** @internal */
670
743
  constructor(containerEl) {
671
744
  this.el = containerEl.createEl("toggle-switch", {
672
745
  cls: "setting-item-toggle",
@@ -683,7 +756,8 @@ class ToggleComponent {
683
756
  }
684
757
  }
685
758
 
686
- class DropdownComponent {
759
+ export class DropdownComponent {
760
+ /** @internal */
687
761
  constructor(containerEl) {
688
762
  this.el = containerEl.createEl("select", {
689
763
  cls: "setting-item-dropdown",
@@ -715,7 +789,8 @@ class DropdownComponent {
715
789
  }
716
790
  }
717
791
 
718
- class ButtonComponent {
792
+ export class ButtonComponent {
793
+ /** @internal */
719
794
  constructor(containerEl) {
720
795
  this.el = containerEl.createEl("button", {
721
796
  cls: "rounded-button",
@@ -735,7 +810,8 @@ class ButtonComponent {
735
810
  }
736
811
  }
737
812
 
738
- class IconComponent {
813
+ export class IconComponent {
814
+ /** @internal */
739
815
  constructor(containerEl) {
740
816
  this.el = containerEl.createEl("plugin-icon");
741
817
  }
@@ -745,7 +821,8 @@ class IconComponent {
745
821
  }
746
822
  }
747
823
 
748
- class BlobImageComponent {
824
+ export class BlobImageComponent {
825
+ /** @internal */
749
826
  constructor(containerEl) {
750
827
  this.el = containerEl.createEl("plugin-blob-image");
751
828
  }
@@ -767,7 +844,8 @@ class BlobImageComponent {
767
844
  }
768
845
  }
769
846
 
770
- class ProfilesListComponent {
847
+ export class ProfilesListComponent {
848
+ /** @internal */
771
849
  constructor(containerEl) {
772
850
  this.el = containerEl.createEl("plugin-profiles-list");
773
851
  }
@@ -782,7 +860,8 @@ class ProfilesListComponent {
782
860
  }
783
861
  }
784
862
 
785
- class PostsFeedComponent {
863
+ export class PostsFeedComponent {
864
+ /** @internal */
786
865
  constructor(containerEl) {
787
866
  this.el = containerEl.createEl("plugin-posts-feed");
788
867
  }
@@ -802,6 +881,7 @@ export class VirtualText {
802
881
  this.value = value == null ? "" : String(value);
803
882
  }
804
883
 
884
+ /** @internal */
805
885
  _serialize() {
806
886
  return { type: "text", value: this.value };
807
887
  }
@@ -930,6 +1010,7 @@ export class VirtualEl {
930
1010
  return component;
931
1011
  }
932
1012
 
1013
+ /** @internal */
933
1014
  _serialize() {
934
1015
  const serialized = {
935
1016
  type: "element",
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "@impro.social/impro-plugin",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
+ "scripts": {
7
+ "docs": "typedoc --options typedoc.json"
8
+ },
6
9
  "license": "0BSD",
7
10
  "exports": {
8
11
  ".": "./main.js"
9
12
  },
10
13
  "files": [
11
14
  "main.js"
12
- ]
15
+ ],
16
+ "devDependencies": {
17
+ "typedoc": "^0.28.20"
18
+ }
13
19
  }