@impro.social/impro-plugin 0.0.19 → 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 +105 -57
  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 = () => {}) {
@@ -431,6 +435,39 @@ export class Plugin {
431
435
  });
432
436
  }
433
437
 
438
+ registerPage({ id, title = null, display = () => null }) {
439
+ const displayHandlerId = uuid.create();
440
+ callHandlers.set(displayHandlerId, async () => {
441
+ const result = await display();
442
+ if (result == null) return null;
443
+ if (!(result instanceof VirtualEl)) {
444
+ const description = result?.constructor?.name ?? typeof result;
445
+ throw new Error(
446
+ `Page "${id}" must return a VirtualEl or null, got ${description}`,
447
+ );
448
+ }
449
+ return result._serialize();
450
+ });
451
+ self.postMessage({
452
+ type: "register",
453
+ target: "page",
454
+ id,
455
+ title,
456
+ displayHandlerId,
457
+ });
458
+ }
459
+
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
+
434
471
  // Makes mounted <plugin-slot name=...> instances re-invoke this plugin's
435
472
  // registered callback for that slot, and drops any cached results. Useful
436
473
  // when a slot's content depends on plugin state that changed after render.
@@ -495,8 +532,8 @@ function serializeTransformTokens(tokens) {
495
532
  // Concatenates the renderable text of a token stream into a string with a position map,
496
533
  // so a transform can pattern-match across token boundaries and map matches back to tokens.
497
534
  export class FlattenedTokens {
535
+ #segments = [];
498
536
  constructor(tokens) {
499
- this._segments = [];
500
537
  let text = "";
501
538
  for (const token of tokens) {
502
539
  let value = null;
@@ -504,7 +541,7 @@ export class FlattenedTokens {
504
541
  else if (token.type === "facet") value = token.text;
505
542
  const start = text.length;
506
543
  if (value != null) text += value;
507
- this._segments.push({ token, start, end: text.length });
544
+ this.#segments.push({ token, start, end: text.length });
508
545
  }
509
546
  this.text = text;
510
547
  }
@@ -517,7 +554,7 @@ export class FlattenedTokens {
517
554
  // emit a live range, demoting partial facet tokens to text tokens
518
555
  tokensFor(start, end) {
519
556
  const out = [];
520
- for (const segment of this._segments) {
557
+ for (const segment of this.#segments) {
521
558
  if (segment.start === segment.end) {
522
559
  if (segment.start >= start && segment.start < end) {
523
560
  out.push(segment.token);
@@ -545,22 +582,22 @@ export function flattenForScan(tokens) {
545
582
  const openModals = new Map();
546
583
 
547
584
  export class Modal {
585
+ #modalId = uuid.create();
548
586
  constructor() {
549
- this._modalId = uuid.create();
550
587
  this.contentEl = new VirtualEl("div");
551
588
  this.titleEl = new VirtualEl("h2");
552
589
  }
553
590
 
554
591
  open() {
555
- if (openModals.has(this._modalId)) return;
556
- openModals.set(this._modalId, this);
592
+ if (openModals.has(this.#modalId)) return;
593
+ openModals.set(this.#modalId, this);
557
594
  this.onOpen();
558
595
  self.postMessage({
559
596
  type: "hostCall",
560
597
  method: "openModal",
561
598
  args: [
562
599
  {
563
- modalId: this._modalId,
600
+ modalId: this.#modalId,
564
601
  title: this.titleEl._serialize(),
565
602
  content: this.contentEl._serialize(),
566
603
  },
@@ -569,13 +606,13 @@ export class Modal {
569
606
  }
570
607
 
571
608
  update() {
572
- if (!openModals.has(this._modalId)) return;
609
+ if (!openModals.has(this.#modalId)) return;
573
610
  self.postMessage({
574
611
  type: "hostCall",
575
612
  method: "updateModal",
576
613
  args: [
577
614
  {
578
- modalId: this._modalId,
615
+ modalId: this.#modalId,
579
616
  title: this.titleEl._serialize(),
580
617
  content: this.contentEl._serialize(),
581
618
  },
@@ -584,12 +621,12 @@ export class Modal {
584
621
  }
585
622
 
586
623
  close() {
587
- if (!openModals.has(this._modalId)) return;
588
- openModals.delete(this._modalId);
624
+ if (!openModals.has(this.#modalId)) return;
625
+ openModals.delete(this.#modalId);
589
626
  self.postMessage({
590
627
  type: "hostCall",
591
628
  method: "closeModal",
592
- args: [{ modalId: this._modalId }],
629
+ args: [{ modalId: this.#modalId }],
593
630
  });
594
631
  this.onClose();
595
632
  }
@@ -658,7 +695,8 @@ export class Setting {
658
695
  }
659
696
  }
660
697
 
661
- class TextComponent {
698
+ export class TextComponent {
699
+ /** @internal */
662
700
  constructor(containerEl) {
663
701
  this.el = containerEl.createEl("input", {
664
702
  attr: { type: "text" },
@@ -679,7 +717,8 @@ class TextComponent {
679
717
  }
680
718
  }
681
719
 
682
- class TextAreaComponent {
720
+ export class TextAreaComponent {
721
+ /** @internal */
683
722
  constructor(containerEl) {
684
723
  this.el = containerEl.createEl("textarea", {
685
724
  cls: "setting-item-textarea",
@@ -699,7 +738,8 @@ class TextAreaComponent {
699
738
  }
700
739
  }
701
740
 
702
- class ToggleComponent {
741
+ export class ToggleComponent {
742
+ /** @internal */
703
743
  constructor(containerEl) {
704
744
  this.el = containerEl.createEl("toggle-switch", {
705
745
  cls: "setting-item-toggle",
@@ -716,7 +756,8 @@ class ToggleComponent {
716
756
  }
717
757
  }
718
758
 
719
- class DropdownComponent {
759
+ export class DropdownComponent {
760
+ /** @internal */
720
761
  constructor(containerEl) {
721
762
  this.el = containerEl.createEl("select", {
722
763
  cls: "setting-item-dropdown",
@@ -748,7 +789,8 @@ class DropdownComponent {
748
789
  }
749
790
  }
750
791
 
751
- class ButtonComponent {
792
+ export class ButtonComponent {
793
+ /** @internal */
752
794
  constructor(containerEl) {
753
795
  this.el = containerEl.createEl("button", {
754
796
  cls: "rounded-button",
@@ -768,7 +810,8 @@ class ButtonComponent {
768
810
  }
769
811
  }
770
812
 
771
- class IconComponent {
813
+ export class IconComponent {
814
+ /** @internal */
772
815
  constructor(containerEl) {
773
816
  this.el = containerEl.createEl("plugin-icon");
774
817
  }
@@ -778,7 +821,8 @@ class IconComponent {
778
821
  }
779
822
  }
780
823
 
781
- class BlobImageComponent {
824
+ export class BlobImageComponent {
825
+ /** @internal */
782
826
  constructor(containerEl) {
783
827
  this.el = containerEl.createEl("plugin-blob-image");
784
828
  }
@@ -800,7 +844,8 @@ class BlobImageComponent {
800
844
  }
801
845
  }
802
846
 
803
- class ProfilesListComponent {
847
+ export class ProfilesListComponent {
848
+ /** @internal */
804
849
  constructor(containerEl) {
805
850
  this.el = containerEl.createEl("plugin-profiles-list");
806
851
  }
@@ -815,7 +860,8 @@ class ProfilesListComponent {
815
860
  }
816
861
  }
817
862
 
818
- class PostsFeedComponent {
863
+ export class PostsFeedComponent {
864
+ /** @internal */
819
865
  constructor(containerEl) {
820
866
  this.el = containerEl.createEl("plugin-posts-feed");
821
867
  }
@@ -835,6 +881,7 @@ export class VirtualText {
835
881
  this.value = value == null ? "" : String(value);
836
882
  }
837
883
 
884
+ /** @internal */
838
885
  _serialize() {
839
886
  return { type: "text", value: this.value };
840
887
  }
@@ -963,6 +1010,7 @@ export class VirtualEl {
963
1010
  return component;
964
1011
  }
965
1012
 
1013
+ /** @internal */
966
1014
  _serialize() {
967
1015
  const serialized = {
968
1016
  type: "element",
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "@impro.social/impro-plugin",
3
- "version": "0.0.19",
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
  }