@impro.social/impro-plugin 0.0.20 → 0.0.21
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/main.js +798 -59
- package/package.json +5 -3
package/main.js
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Record<string, unknown>} PostView
|
|
3
|
+
* Hydrated `app.bsky.feed.defs#postView` shape.
|
|
4
|
+
* @typedef {Record<string, unknown>} ProfileView
|
|
5
|
+
* Basic `app.bsky.actor.defs#profileView` shape.
|
|
6
|
+
* @typedef {Record<string, unknown>} DetailedProfileView
|
|
7
|
+
* Detailed profile view with viewer relationship fields.
|
|
8
|
+
* @typedef {Record<string, unknown>} KnownFollowersResponse
|
|
9
|
+
* Paginated response from `getKnownFollowers`: `{ followers, cursor }`.
|
|
10
|
+
* @typedef {Record<string, unknown>} RepoRecord
|
|
11
|
+
* A raw repo record: `{ uri, cid, value }`.
|
|
12
|
+
* @typedef {Record<string, unknown>} FeedItem
|
|
13
|
+
* A `app.bsky.feed.defs#feedViewPost` (post + reply/repost context).
|
|
14
|
+
* @typedef {Record<string, unknown>} RichTextToken
|
|
15
|
+
* One token in a rich-text stream — `text`, `facet`, `inline`, or `block`.
|
|
16
|
+
* @typedef {VirtualEl | null | undefined} RenderResult
|
|
17
|
+
* What a render callback may return: a tree to render, or nothing.
|
|
18
|
+
*/
|
|
19
|
+
|
|
1
20
|
export class SimpleUUID {
|
|
2
21
|
#id = 0;
|
|
3
22
|
create() {
|
|
@@ -73,6 +92,11 @@ function addEventListener(event, listener) {
|
|
|
73
92
|
}
|
|
74
93
|
}
|
|
75
94
|
|
|
95
|
+
/**
|
|
96
|
+
* A single item in a {@link Menu}. Configure with the chained setters and
|
|
97
|
+
* an `onClick` handler. Not constructed directly — obtained via
|
|
98
|
+
* `Menu.addItem(builder)`.
|
|
99
|
+
*/
|
|
76
100
|
export class MenuItem {
|
|
77
101
|
constructor() {
|
|
78
102
|
this.title = "";
|
|
@@ -80,24 +104,51 @@ export class MenuItem {
|
|
|
80
104
|
/** @internal */
|
|
81
105
|
this._callback = () => {};
|
|
82
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Set the menu item's label.
|
|
109
|
+
* @param {string} title
|
|
110
|
+
* @returns {this}
|
|
111
|
+
*/
|
|
83
112
|
setTitle(title) {
|
|
84
113
|
this.title = title;
|
|
85
114
|
return this;
|
|
86
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Set the leading icon. Either a named icon (string) or a {@link VirtualEl}
|
|
118
|
+
* to render as the icon.
|
|
119
|
+
* @param {string | VirtualEl} icon
|
|
120
|
+
* @returns {this}
|
|
121
|
+
*/
|
|
87
122
|
setIcon(icon) {
|
|
88
123
|
this.icon = icon;
|
|
89
124
|
return this;
|
|
90
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Called when the user activates the item.
|
|
128
|
+
* @param {() => void} callback
|
|
129
|
+
* @returns {this}
|
|
130
|
+
*/
|
|
91
131
|
onClick(callback) {
|
|
92
132
|
this._callback = callback;
|
|
93
133
|
return this;
|
|
94
134
|
}
|
|
95
135
|
}
|
|
96
136
|
|
|
137
|
+
/**
|
|
138
|
+
* A context-menu builder passed as the first argument to
|
|
139
|
+
* `post-context-menu` and `profile-context-menu` event listeners. Call
|
|
140
|
+
* `addItem` to append menu entries.
|
|
141
|
+
*/
|
|
97
142
|
export class Menu {
|
|
98
143
|
constructor() {
|
|
144
|
+
/** @type {MenuItem[]} */
|
|
99
145
|
this.items = [];
|
|
100
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Append a menu item. The builder receives a {@link MenuItem} to configure.
|
|
149
|
+
* @param {(item: MenuItem) => void} builder
|
|
150
|
+
* @returns {this}
|
|
151
|
+
*/
|
|
101
152
|
addItem(builder) {
|
|
102
153
|
const item = new MenuItem();
|
|
103
154
|
builder(item);
|
|
@@ -116,21 +167,47 @@ export class Menu {
|
|
|
116
167
|
}
|
|
117
168
|
}
|
|
118
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Builder passed as the first argument to `post-composer-open` event
|
|
172
|
+
* listeners. Queues text operations that the host applies to the composer once
|
|
173
|
+
* all listeners have run. operations from multiple plugins are applied in order.
|
|
174
|
+
*/
|
|
119
175
|
export class Composer {
|
|
120
176
|
#ops = [];
|
|
177
|
+
/** @type {number | null} */
|
|
121
178
|
#cursor = null;
|
|
179
|
+
/**
|
|
180
|
+
* Replace the composer's current text.
|
|
181
|
+
* @param {string} text
|
|
182
|
+
* @returns {this}
|
|
183
|
+
*/
|
|
122
184
|
setText(text) {
|
|
123
185
|
this.#ops.push({ op: "set", text: String(text) });
|
|
124
186
|
return this;
|
|
125
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Append text to the end of the composer.
|
|
190
|
+
* @param {string} text
|
|
191
|
+
* @returns {this}
|
|
192
|
+
*/
|
|
126
193
|
appendText(text) {
|
|
127
194
|
this.#ops.push({ op: "append", text: String(text) });
|
|
128
195
|
return this;
|
|
129
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* Prepend text to the start of the composer.
|
|
199
|
+
* @param {string} text
|
|
200
|
+
* @returns {this}
|
|
201
|
+
*/
|
|
130
202
|
prependText(text) {
|
|
131
203
|
this.#ops.push({ op: "prepend", text: String(text) });
|
|
132
204
|
return this;
|
|
133
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Move the caret to the given character index in the final text.
|
|
208
|
+
* @param {number} index
|
|
209
|
+
* @returns {this}
|
|
210
|
+
*/
|
|
134
211
|
setCursor(index) {
|
|
135
212
|
this.#cursor = index;
|
|
136
213
|
return this;
|
|
@@ -141,70 +218,173 @@ export class Composer {
|
|
|
141
218
|
}
|
|
142
219
|
}
|
|
143
220
|
|
|
221
|
+
/**
|
|
222
|
+
* Read-only accessors for appview data with the current user as the viewer.
|
|
223
|
+
* Reached via {@link App.data} on the plugin's {@link App} instance.
|
|
224
|
+
*/
|
|
144
225
|
export class PluginData {
|
|
226
|
+
/**
|
|
227
|
+
* Fetch a hydrated post view by AT-URI, as seen by the current user.
|
|
228
|
+
* @param {string} uri
|
|
229
|
+
* @returns {Promise<PostView>}
|
|
230
|
+
*/
|
|
145
231
|
getPost(uri) {
|
|
146
232
|
return hostCall("getPost", { uri });
|
|
147
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Fetch the basic profile view for a DID.
|
|
236
|
+
* @param {string} did
|
|
237
|
+
* @returns {Promise<ProfileView>}
|
|
238
|
+
*/
|
|
148
239
|
getProfile(did) {
|
|
149
240
|
return hostCall("getProfile", { did });
|
|
150
241
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Like {@link PluginData.getProfile}, but includes viewer relationship
|
|
244
|
+
* details not present on the basic profile view: `viewer.following`,
|
|
245
|
+
* `viewer.followedBy`, and `viewer.knownFollowers` (a summary of mutual
|
|
246
|
+
* followers).
|
|
247
|
+
* @param {string} did
|
|
248
|
+
* @returns {Promise<DetailedProfileView>}
|
|
249
|
+
*/
|
|
154
250
|
getDetailedProfile(did) {
|
|
155
251
|
return hostCall("getDetailedProfile", { did });
|
|
156
252
|
}
|
|
157
|
-
|
|
158
|
-
|
|
253
|
+
/**
|
|
254
|
+
* The full known-followers list for `did`. The summary on
|
|
255
|
+
* {@link PluginData.getDetailedProfile}'s `viewer.knownFollowers` is
|
|
256
|
+
* capped to a handful; use this to paginate the complete list.
|
|
257
|
+
* @param {string} did
|
|
258
|
+
* @returns {Promise<KnownFollowersResponse>}
|
|
259
|
+
*/
|
|
159
260
|
getKnownFollowers(did) {
|
|
160
261
|
return hostCall("getKnownFollowers", { did });
|
|
161
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Fetch a raw repo record by `(repo, collection, rkey)`.
|
|
265
|
+
* @param {string} repo
|
|
266
|
+
* @param {string} collection
|
|
267
|
+
* @param {string} rkey
|
|
268
|
+
* @returns {Promise<RepoRecord>}
|
|
269
|
+
*/
|
|
162
270
|
getRecord(repo, collection, rkey) {
|
|
163
271
|
return hostCall("getRecord", { repo, collection, rkey });
|
|
164
272
|
}
|
|
165
273
|
}
|
|
166
274
|
|
|
275
|
+
/**
|
|
276
|
+
* The plugin's handle to the running impro app. Exposed as `this.app` on a
|
|
277
|
+
* {@link Plugin} instance. Owns event subscriptions, data accessors
|
|
278
|
+
* ({@link App.data}), and user-scoped actions.
|
|
279
|
+
*/
|
|
167
280
|
export class App {
|
|
168
281
|
/** @internal */
|
|
169
282
|
constructor() {
|
|
283
|
+
/**
|
|
284
|
+
* The signed-in user's basic profile, populated before `onload()` runs.
|
|
285
|
+
* Null when no session is active.
|
|
286
|
+
* @type {ProfileView | null}
|
|
287
|
+
*/
|
|
170
288
|
this.currentUser = null;
|
|
289
|
+
/** Read-only appview accessors — see {@link PluginData}. */
|
|
171
290
|
this.data = new PluginData();
|
|
172
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Register an event listener. Supported events:
|
|
294
|
+
*
|
|
295
|
+
* - `"post-context-menu"` — `(menu: Menu, post) => void`, called when the
|
|
296
|
+
* user opens a post's context menu.
|
|
297
|
+
* - `"profile-context-menu"` — `(menu: Menu, profile) => void`, called
|
|
298
|
+
* when the user opens a profile's context menu.
|
|
299
|
+
* - `"post-composer-open"` — `(composer: Composer, context: { kind, replyTo, replyRoot, quotedPost }) => void`,
|
|
300
|
+
* called when the post composer opens; use `composer` to seed text.
|
|
301
|
+
*
|
|
302
|
+
* The `listener` signature varies per event (see above).
|
|
303
|
+
* @param {string} event
|
|
304
|
+
* @param {Function} listener
|
|
305
|
+
*/
|
|
173
306
|
on(event, listener) {
|
|
174
307
|
addEventListener(event, listener);
|
|
175
308
|
}
|
|
176
309
|
|
|
310
|
+
/**
|
|
311
|
+
* Re-run registered feed filters. Pass a `feedURI` to limit the refresh
|
|
312
|
+
* to one feed, or omit/pass `null` to refresh every feed.
|
|
313
|
+
* @param {string | null} [feedURI]
|
|
314
|
+
* @returns {Promise<void>}
|
|
315
|
+
*/
|
|
177
316
|
refreshFeedFilters(feedURI = null) {
|
|
178
317
|
return hostCall("refreshFeedFilters", feedURI);
|
|
179
318
|
}
|
|
180
319
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
320
|
+
/**
|
|
321
|
+
* Mute an actor on behalf of the signed-in user. Requires the `"mute"`
|
|
322
|
+
* scope in the plugin manifest's `permissions.actions`.
|
|
323
|
+
* @param {string} did
|
|
324
|
+
* @returns {Promise<void>}
|
|
325
|
+
*/
|
|
185
326
|
muteActor(did) {
|
|
186
327
|
return hostCall("muteActor", { did, mute: true });
|
|
187
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* Unmute an actor. Requires the `"mute"` scope.
|
|
331
|
+
* @param {string} did
|
|
332
|
+
* @returns {Promise<void>}
|
|
333
|
+
*/
|
|
188
334
|
unmuteActor(did) {
|
|
189
335
|
return hostCall("muteActor", { did, mute: false });
|
|
190
336
|
}
|
|
337
|
+
/**
|
|
338
|
+
* Block an actor on behalf of the signed-in user. Requires the `"block"`
|
|
339
|
+
* scope in the plugin manifest's `permissions.actions`.
|
|
340
|
+
* @param {string} did
|
|
341
|
+
* @returns {Promise<void>}
|
|
342
|
+
*/
|
|
191
343
|
blockActor(did) {
|
|
192
344
|
return hostCall("blockActor", { did, block: true });
|
|
193
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Unblock an actor. Requires the `"block"` scope.
|
|
348
|
+
* @param {string} did
|
|
349
|
+
* @returns {Promise<void>}
|
|
350
|
+
*/
|
|
194
351
|
unblockActor(did) {
|
|
195
352
|
return hostCall("blockActor", { did, block: false });
|
|
196
353
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
354
|
+
/**
|
|
355
|
+
* Acts like the user clicking "Show less like this": sends the
|
|
356
|
+
* `requestLess` feedback signal to `feedUri` and collapses the post
|
|
357
|
+
* behind a feedback message in feeds. Requires the `"feedFeedback"`
|
|
358
|
+
* scope.
|
|
359
|
+
* @param {string} postUri
|
|
360
|
+
* @param {string} feedUri
|
|
361
|
+
* @returns {Promise<void>}
|
|
362
|
+
*/
|
|
200
363
|
showLessLikeThis(postUri, feedUri) {
|
|
201
364
|
return hostCall("showLessLikeThis", { postUri, feedUri });
|
|
202
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Sends the `requestMore` feedback signal to `feedUri` for `postUri`.
|
|
368
|
+
* Requires the `"feedFeedback"` scope.
|
|
369
|
+
* @param {string} postUri
|
|
370
|
+
* @param {string} feedUri
|
|
371
|
+
* @returns {Promise<void>}
|
|
372
|
+
*/
|
|
203
373
|
showMoreLikeThis(postUri, feedUri) {
|
|
204
374
|
return hostCall("showMoreLikeThis", { postUri, feedUri });
|
|
205
375
|
}
|
|
206
376
|
}
|
|
207
377
|
|
|
378
|
+
/**
|
|
379
|
+
* Proxied fetch through the host. Requires the `"networkRequest"` permission
|
|
380
|
+
* scope and the target URL must be covered by the plugin's manifest allowlist.
|
|
381
|
+
* `init` accepts `method`, `headers` (plain object, `Headers`, `Map`, or
|
|
382
|
+
* `[name, value]` iterable), and a string `body`. Resolves to a
|
|
383
|
+
* {@link PluginResponse}.
|
|
384
|
+
* @param {string} url
|
|
385
|
+
* @param {{ method?: string, headers?: Record<string, string> | Headers | Map<string, string> | Iterable<[string, string], void, undefined>, body?: string }} [init]
|
|
386
|
+
* @returns {Promise<PluginResponse>}
|
|
387
|
+
*/
|
|
208
388
|
export async function fetch(url, init = {}) {
|
|
209
389
|
const result = await hostCall("fetch", {
|
|
210
390
|
url,
|
|
@@ -234,29 +414,60 @@ function serializeFetchInit(init) {
|
|
|
234
414
|
return serialized;
|
|
235
415
|
}
|
|
236
416
|
|
|
417
|
+
/**
|
|
418
|
+
* Response returned from {@link fetch}. Body is buffered by the host and
|
|
419
|
+
* exposed as text or parsed JSON. `status`, `ok`, and `headers` (a `Map`)
|
|
420
|
+
* mirror the underlying HTTP response.
|
|
421
|
+
*/
|
|
237
422
|
export class PluginResponse {
|
|
238
423
|
#body;
|
|
239
424
|
/** @internal */
|
|
240
425
|
constructor({ status, ok, headers, body }) {
|
|
426
|
+
/** @type {number} */
|
|
241
427
|
this.status = status;
|
|
428
|
+
/** @type {boolean} */
|
|
242
429
|
this.ok = ok;
|
|
430
|
+
/** @type {Map<string, string>} */
|
|
243
431
|
this.headers = new Map(Object.entries(headers ?? {}));
|
|
244
432
|
this.#body = body;
|
|
245
433
|
}
|
|
434
|
+
/**
|
|
435
|
+
* Resolves with the response body as a string.
|
|
436
|
+
* @returns {Promise<string>}
|
|
437
|
+
*/
|
|
246
438
|
async text() {
|
|
247
439
|
return this.#body;
|
|
248
440
|
}
|
|
441
|
+
/**
|
|
442
|
+
* Resolves with the response body parsed as JSON.
|
|
443
|
+
* @returns {Promise<unknown>}
|
|
444
|
+
*/
|
|
249
445
|
async json() {
|
|
250
446
|
return JSON.parse(this.#body);
|
|
251
447
|
}
|
|
252
448
|
}
|
|
253
449
|
|
|
450
|
+
/**
|
|
451
|
+
* Shows a toast notification. `noticeEl` is a {@link VirtualEl} that can be
|
|
452
|
+
* mutated (e.g. `addClass`, appending children) synchronously after
|
|
453
|
+
* construction — the host serializes and renders the toast on a microtask, so
|
|
454
|
+
* any mutations made in the same task apply to the initial render. Mutations
|
|
455
|
+
* or `setMessage` calls made after that point do NOT propagate to the mounted
|
|
456
|
+
* toast; call {@link Notice.hide} and create a new `Notice` to change it.
|
|
457
|
+
*/
|
|
254
458
|
export class Notice {
|
|
255
459
|
#toastId = uuid.create();
|
|
256
460
|
#timeout;
|
|
257
461
|
#hidden = false;
|
|
462
|
+
/**
|
|
463
|
+
* `timeout` is in milliseconds; `0` (the default) keeps the toast up until
|
|
464
|
+
* {@link Notice.hide} is called.
|
|
465
|
+
* @param {string} message
|
|
466
|
+
* @param {number} [timeout=0]
|
|
467
|
+
*/
|
|
258
468
|
constructor(message, timeout = 0) {
|
|
259
469
|
this.#timeout = timeout;
|
|
470
|
+
/** @type {VirtualEl} */
|
|
260
471
|
this.noticeEl = new VirtualEl("div");
|
|
261
472
|
this.noticeEl.addClass("toast");
|
|
262
473
|
this.noticeEl.setText(message);
|
|
@@ -269,10 +480,20 @@ export class Notice {
|
|
|
269
480
|
});
|
|
270
481
|
});
|
|
271
482
|
}
|
|
483
|
+
/**
|
|
484
|
+
* Updates `noticeEl`'s text. Only takes effect if called synchronously
|
|
485
|
+
* before the microtask that snapshots the toast for the initial render.
|
|
486
|
+
* @param {string} message
|
|
487
|
+
* @returns {this}
|
|
488
|
+
*/
|
|
272
489
|
setMessage(message) {
|
|
273
490
|
this.noticeEl.setText(message);
|
|
274
491
|
return this;
|
|
275
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Dismisses the toast. No-op if already hidden.
|
|
495
|
+
* @returns {void}
|
|
496
|
+
*/
|
|
276
497
|
hide() {
|
|
277
498
|
if (this.#hidden) return;
|
|
278
499
|
this.#hidden = true;
|
|
@@ -280,10 +501,22 @@ export class Notice {
|
|
|
280
501
|
}
|
|
281
502
|
}
|
|
282
503
|
|
|
504
|
+
/**
|
|
505
|
+
* Injects a CSS block scoped to the plugin. The stylesheet is mounted by the
|
|
506
|
+
* host on the next microtask; snippets are automatically removed when the
|
|
507
|
+
* plugin unloads.
|
|
508
|
+
*/
|
|
283
509
|
export class StyleSnippet {
|
|
284
510
|
#snippetId = uuid.create();
|
|
285
511
|
#removed = false;
|
|
512
|
+
/**
|
|
513
|
+
* @param {string} cssText
|
|
514
|
+
*/
|
|
286
515
|
constructor(cssText) {
|
|
516
|
+
/**
|
|
517
|
+
* Resolves once the snippet has been applied by the host.
|
|
518
|
+
* @type {Promise<void>}
|
|
519
|
+
*/
|
|
287
520
|
this.ready = new Promise((resolve, reject) => {
|
|
288
521
|
queueMicrotask(() => {
|
|
289
522
|
if (this.#removed) return resolve();
|
|
@@ -294,6 +527,10 @@ export class StyleSnippet {
|
|
|
294
527
|
});
|
|
295
528
|
});
|
|
296
529
|
}
|
|
530
|
+
/**
|
|
531
|
+
* Removes the snippet from the document. No-op if already removed.
|
|
532
|
+
* @returns {void}
|
|
533
|
+
*/
|
|
297
534
|
remove() {
|
|
298
535
|
if (this.#removed) return;
|
|
299
536
|
this.#removed = true;
|
|
@@ -303,12 +540,29 @@ export class StyleSnippet {
|
|
|
303
540
|
|
|
304
541
|
let registered = false;
|
|
305
542
|
|
|
543
|
+
/**
|
|
544
|
+
* Base class for impro plugins. Subclass this and override {@link Plugin.onload}
|
|
545
|
+
* (and optionally {@link Plugin.onunload}) to wire up your plugin's extension
|
|
546
|
+
* points. `this.app` is the shared {@link App} instance for host actions and
|
|
547
|
+
* data. Call `MyPlugin.register()` at the top of your plugin's main.js to boot.
|
|
548
|
+
*/
|
|
306
549
|
export class Plugin {
|
|
550
|
+
/** @type {PluginSettingTab | null} */
|
|
307
551
|
#settingTab = null;
|
|
552
|
+
/** @internal */
|
|
308
553
|
constructor() {
|
|
554
|
+
/** @type {App} */
|
|
309
555
|
this.app = new App();
|
|
310
556
|
}
|
|
311
557
|
|
|
558
|
+
/**
|
|
559
|
+
* Adds an item to the impro sidebar. `icon` is a {@link VirtualEl} (typically
|
|
560
|
+
* an SVG) or a string; `callback` runs when the item is clicked.
|
|
561
|
+
* @param {string | VirtualEl} icon
|
|
562
|
+
* @param {string} title
|
|
563
|
+
* @param {() => void} [callback]
|
|
564
|
+
* @returns {void}
|
|
565
|
+
*/
|
|
312
566
|
addSidebarItem(icon, title, callback = () => {}) {
|
|
313
567
|
const handlerId = uuid.create();
|
|
314
568
|
callHandlers.set(handlerId, callback);
|
|
@@ -321,26 +575,50 @@ export class Plugin {
|
|
|
321
575
|
});
|
|
322
576
|
}
|
|
323
577
|
|
|
578
|
+
/**
|
|
579
|
+
* Loads this plugin's account-synced JSON blob. Follows the user across
|
|
580
|
+
* devices via account preferences. Returns whatever was last saved, or null.
|
|
581
|
+
* @returns {Promise<unknown>}
|
|
582
|
+
*/
|
|
324
583
|
async loadData() {
|
|
325
584
|
return hostCall("loadData");
|
|
326
585
|
}
|
|
327
586
|
|
|
587
|
+
/**
|
|
588
|
+
* Persists `data` as this plugin's account-synced JSON blob.
|
|
589
|
+
* @param {unknown} data
|
|
590
|
+
* @returns {Promise<void>}
|
|
591
|
+
*/
|
|
328
592
|
async saveData(data) {
|
|
329
593
|
await hostCall("saveData", { data });
|
|
330
594
|
}
|
|
331
595
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
596
|
+
/**
|
|
597
|
+
* Device-local counterpart to {@link Plugin.loadData}: never synced through
|
|
598
|
+
* the user's account preferences, so it's the right place for anything that
|
|
599
|
+
* shouldn't silently follow the plugin to another device (e.g. a locally
|
|
600
|
+
* held secret key). Cleared on uninstall, same as loadData/saveData.
|
|
601
|
+
* @returns {Promise<unknown>}
|
|
602
|
+
*/
|
|
336
603
|
async loadLocalData() {
|
|
337
604
|
return hostCall("loadLocalData");
|
|
338
605
|
}
|
|
339
606
|
|
|
607
|
+
/**
|
|
608
|
+
* Device-local counterpart to {@link Plugin.saveData}.
|
|
609
|
+
* @param {unknown} data
|
|
610
|
+
* @returns {Promise<void>}
|
|
611
|
+
*/
|
|
340
612
|
async saveLocalData(data) {
|
|
341
613
|
await hostCall("saveLocalData", { data });
|
|
342
614
|
}
|
|
343
615
|
|
|
616
|
+
/**
|
|
617
|
+
* Registers a {@link PluginSettingTab} shown under this plugin's entry in
|
|
618
|
+
* the app's settings.
|
|
619
|
+
* @param {PluginSettingTab} tab
|
|
620
|
+
* @returns {void}
|
|
621
|
+
*/
|
|
344
622
|
addSettingTab(tab) {
|
|
345
623
|
tab.plugin = this;
|
|
346
624
|
const displayHandlerId = uuid.create();
|
|
@@ -358,7 +636,14 @@ export class Plugin {
|
|
|
358
636
|
this.#settingTab = tab;
|
|
359
637
|
}
|
|
360
638
|
|
|
361
|
-
|
|
639
|
+
/**
|
|
640
|
+
* Registers a feed filter. `callback(feedUri, feedItems)` returns an object
|
|
641
|
+
* `{ [postUri]: false }` for posts to hide from the feed. Only `false` hides;
|
|
642
|
+
* any other value is ignored, so one plugin can't un-hide what another hid.
|
|
643
|
+
* @param {(feedUri: string, feedItems: FeedItem[]) => Record<string, boolean> | Promise<Record<string, boolean>>} callback
|
|
644
|
+
* @returns {void}
|
|
645
|
+
*/
|
|
646
|
+
addFeedFilter(callback = () => ({})) {
|
|
362
647
|
const handlerId = uuid.create();
|
|
363
648
|
callHandlers.set(handlerId, callback);
|
|
364
649
|
self.postMessage({
|
|
@@ -368,12 +653,21 @@ export class Plugin {
|
|
|
368
653
|
});
|
|
369
654
|
}
|
|
370
655
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
656
|
+
/**
|
|
657
|
+
* Registers a rich-text transform. `callback(tokens, context)` receives the
|
|
658
|
+
* token stream for one post and returns a new token array (or the input
|
|
659
|
+
* unchanged). Tokens are one of: `text` (plain string run), `facet` (linked
|
|
660
|
+
* text with an atproto facet feature), `inline` (a plugin-produced inline
|
|
661
|
+
* {@link VirtualEl}), or `block` (a plugin-produced block VirtualEl). See
|
|
662
|
+
* {@link FlattenedTokens} for pattern-matching across token boundaries.
|
|
663
|
+
*
|
|
664
|
+
* `options.handlesFacetTypes` is an array of facet feature `$type` strings
|
|
665
|
+
* this transform owns, so the host can suppress fallback rendering flash
|
|
666
|
+
* while the transform runs.
|
|
667
|
+
* @param {(tokens: RichTextToken[], context: { uri: string, surface: string, source: { text: string } }) => RichTextToken[] | Promise<RichTextToken[]>} callback
|
|
668
|
+
* @param {{ handlesFacetTypes?: string[] }} [options]
|
|
669
|
+
* @returns {void}
|
|
670
|
+
*/
|
|
377
671
|
registerRichTextTransform(callback = (tokens) => tokens, options = {}) {
|
|
378
672
|
const handlerId = uuid.create();
|
|
379
673
|
callHandlers.set(handlerId, async (batch) => {
|
|
@@ -399,14 +693,25 @@ export class Plugin {
|
|
|
399
693
|
});
|
|
400
694
|
}
|
|
401
695
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
696
|
+
/**
|
|
697
|
+
* Registers a slot renderer. `<plugin-slot name="...">` elements in the host
|
|
698
|
+
* UI (or other plugins' output) invoke `callback(context)`, where `context`
|
|
699
|
+
* is a flat string map of the slot element's attributes. The callback should
|
|
700
|
+
* return a {@link VirtualEl} or `null`.
|
|
701
|
+
*
|
|
702
|
+
* `options.cacheKey` is an array of context field names. If provided, the
|
|
703
|
+
* host treats the slot content as a pure function of these fields — omitting
|
|
704
|
+
* other fields from the callback's context and caching return values until
|
|
705
|
+
* invalidated by {@link Plugin.refreshSlot}. An empty array declares that
|
|
706
|
+
* the content depends on no context at all, so one cached result serves
|
|
707
|
+
* every instance.
|
|
708
|
+
*
|
|
709
|
+
* The host batches all pending contexts of a render into one call.
|
|
710
|
+
* @param {string} name
|
|
711
|
+
* @param {(context: Record<string, string>) => RenderResult | Promise<RenderResult>} callback
|
|
712
|
+
* @param {{ cacheKey?: string[] }} [options]
|
|
713
|
+
* @returns {void}
|
|
714
|
+
*/
|
|
410
715
|
registerSlot(name, callback = () => null, options = {}) {
|
|
411
716
|
const handlerId = uuid.create();
|
|
412
717
|
callHandlers.set(handlerId, async (batch) => {
|
|
@@ -435,15 +740,21 @@ export class Plugin {
|
|
|
435
740
|
});
|
|
436
741
|
}
|
|
437
742
|
|
|
743
|
+
/**
|
|
744
|
+
* Registers a full-page view reachable via {@link Plugin.openPage}. `display()`
|
|
745
|
+
* is called on navigation and must return a {@link VirtualEl}, or nothing to
|
|
746
|
+
* render an empty page.
|
|
747
|
+
* @param {{ id: string, title?: string | null, display?: () => RenderResult | Promise<RenderResult> }} options
|
|
748
|
+
* @returns {void}
|
|
749
|
+
*/
|
|
438
750
|
registerPage({ id, title = null, display = () => null }) {
|
|
439
751
|
const displayHandlerId = uuid.create();
|
|
440
752
|
callHandlers.set(displayHandlerId, async () => {
|
|
441
|
-
const result = await display();
|
|
753
|
+
const result = /** @type {unknown} */ (await display());
|
|
442
754
|
if (result == null) return null;
|
|
443
755
|
if (!(result instanceof VirtualEl)) {
|
|
444
|
-
const description = result?.constructor?.name ?? typeof result;
|
|
445
756
|
throw new Error(
|
|
446
|
-
`Page "${id}" must return a VirtualEl or null, got ${
|
|
757
|
+
`Page "${id}" must return a VirtualEl or null, got ${describeValue(result)}`,
|
|
447
758
|
);
|
|
448
759
|
}
|
|
449
760
|
return result._serialize();
|
|
@@ -457,33 +768,61 @@ export class Plugin {
|
|
|
457
768
|
});
|
|
458
769
|
}
|
|
459
770
|
|
|
460
|
-
|
|
771
|
+
/**
|
|
772
|
+
* Navigates the user to one of this plugin's registered pages.
|
|
773
|
+
* @param {string} pageId
|
|
774
|
+
* @returns {Promise<void>}
|
|
775
|
+
*/
|
|
461
776
|
openPage(pageId) {
|
|
462
777
|
return hostCall("openPage", { pageId });
|
|
463
778
|
}
|
|
464
779
|
|
|
465
|
-
|
|
466
|
-
|
|
780
|
+
/**
|
|
781
|
+
* Re-invokes a registered page's display callback if the page is open.
|
|
782
|
+
* `options.reset` also discards the rendered tree instead of patching it.
|
|
783
|
+
* @param {string} pageId
|
|
784
|
+
* @param {{ reset?: boolean }} [options]
|
|
785
|
+
* @returns {Promise<void>}
|
|
786
|
+
*/
|
|
467
787
|
refreshPage(pageId, { reset = false } = {}) {
|
|
468
788
|
return hostCall("refreshPage", { pageId, reset });
|
|
469
789
|
}
|
|
470
790
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
791
|
+
/**
|
|
792
|
+
* Makes mounted `<plugin-slot name=...>` instances re-invoke this plugin's
|
|
793
|
+
* registered callback for that slot, and drops any cached results. Useful
|
|
794
|
+
* when a slot's content depends on plugin state that changed after render.
|
|
795
|
+
*
|
|
796
|
+
* `options.keys` is an array of matcher objects OR'd together,
|
|
797
|
+
* e.g. `[{ did: "..." }]` — any matching slots are invalidated and refreshed.
|
|
798
|
+
* Omit to refresh every instance. A slot registered with a `cacheKey` can
|
|
799
|
+
* only be matched on those declared fields, since its output depends on
|
|
800
|
+
* nothing else.
|
|
801
|
+
* @param {string} name
|
|
802
|
+
* @param {{ keys?: Record<string, string>[] }} [options]
|
|
803
|
+
* @returns {Promise<void>}
|
|
804
|
+
*/
|
|
480
805
|
refreshSlot(name, options = {}) {
|
|
481
806
|
return hostCall("refreshSlot", { name, keys: options.keys ?? null });
|
|
482
807
|
}
|
|
483
808
|
|
|
809
|
+
/**
|
|
810
|
+
* Override in your subclass. Runs after the plugin is loaded and the current user is resolved.
|
|
811
|
+
* @returns {void | Promise<void>}
|
|
812
|
+
*/
|
|
484
813
|
onload() {}
|
|
814
|
+
/**
|
|
815
|
+
* Override in your subclass. Runs when the plugin is being torn down (uninstall/reload).
|
|
816
|
+
* @returns {void | Promise<void>}
|
|
817
|
+
*/
|
|
485
818
|
onunload() {}
|
|
486
819
|
|
|
820
|
+
/**
|
|
821
|
+
* Boots the plugin. Call as `MyPlugin.register()` at the top of your plugin's
|
|
822
|
+
* main.js — instantiates the subclass, resolves the current user onto
|
|
823
|
+
* `app.currentUser`, then invokes {@link Plugin.onload}. Idempotent.
|
|
824
|
+
* @returns {void}
|
|
825
|
+
*/
|
|
487
826
|
static register() {
|
|
488
827
|
if (registered) return;
|
|
489
828
|
registered = true;
|
|
@@ -504,13 +843,23 @@ export class Plugin {
|
|
|
504
843
|
}
|
|
505
844
|
}
|
|
506
845
|
|
|
846
|
+
/**
|
|
847
|
+
* Names the type of an arbitrary value, for use in an error message.
|
|
848
|
+
* @param {unknown} value
|
|
849
|
+
* @returns {string}
|
|
850
|
+
*/
|
|
851
|
+
function describeValue(value) {
|
|
852
|
+
if (value === null) return "null";
|
|
853
|
+
if (typeof value !== "object") return typeof value;
|
|
854
|
+
return value.constructor?.name ?? "object";
|
|
855
|
+
}
|
|
856
|
+
|
|
507
857
|
async function getSlotContent(name, callback, context) {
|
|
508
|
-
const result = await callback(context);
|
|
858
|
+
const result = /** @type {unknown} */ (await callback(context));
|
|
509
859
|
if (result == null) return null;
|
|
510
860
|
if (!(result instanceof VirtualEl)) {
|
|
511
|
-
const description = result?.constructor?.name ?? typeof result;
|
|
512
861
|
throw new Error(
|
|
513
|
-
`Slot "${name}" must return a VirtualEl or null, got ${
|
|
862
|
+
`Slot "${name}" must return a VirtualEl or null, got ${describeValue(result)}`,
|
|
514
863
|
);
|
|
515
864
|
}
|
|
516
865
|
return result._serialize();
|
|
@@ -529,10 +878,17 @@ function serializeTransformTokens(tokens) {
|
|
|
529
878
|
});
|
|
530
879
|
}
|
|
531
880
|
|
|
532
|
-
|
|
533
|
-
|
|
881
|
+
/**
|
|
882
|
+
* Flattens a rich-text token stream into a plain string with a position map,
|
|
883
|
+
* so a {@link Plugin.registerRichTextTransform | rich-text transform} can
|
|
884
|
+
* pattern-match (e.g. with a regex) across token boundaries and map matches
|
|
885
|
+
* back to the original tokens.
|
|
886
|
+
*/
|
|
534
887
|
export class FlattenedTokens {
|
|
535
888
|
#segments = [];
|
|
889
|
+
/**
|
|
890
|
+
* @param {RichTextToken[]} tokens
|
|
891
|
+
*/
|
|
536
892
|
constructor(tokens) {
|
|
537
893
|
let text = "";
|
|
538
894
|
for (const token of tokens) {
|
|
@@ -543,15 +899,28 @@ export class FlattenedTokens {
|
|
|
543
899
|
if (value != null) text += value;
|
|
544
900
|
this.#segments.push({ token, start, end: text.length });
|
|
545
901
|
}
|
|
902
|
+
/** @type {string} */
|
|
546
903
|
this.text = text;
|
|
547
904
|
}
|
|
548
905
|
|
|
549
|
-
|
|
906
|
+
/**
|
|
907
|
+
* Return the plain-text slice for `[start, end)` — inert, with no facets.
|
|
908
|
+
* @param {number} start
|
|
909
|
+
* @param {number} end
|
|
910
|
+
* @returns {string}
|
|
911
|
+
*/
|
|
550
912
|
textFor(start, end) {
|
|
551
913
|
return this.text.slice(start, end);
|
|
552
914
|
}
|
|
553
915
|
|
|
554
|
-
|
|
916
|
+
/**
|
|
917
|
+
* Return the tokens covering `[start, end)`. Facet tokens partially
|
|
918
|
+
* overlapping the range are demoted to plain text tokens so the emitted
|
|
919
|
+
* slice never carries a truncated facet.
|
|
920
|
+
* @param {number} start
|
|
921
|
+
* @param {number} end
|
|
922
|
+
* @returns {RichTextToken[]}
|
|
923
|
+
*/
|
|
555
924
|
tokensFor(start, end) {
|
|
556
925
|
const out = [];
|
|
557
926
|
for (const segment of this.#segments) {
|
|
@@ -575,19 +944,35 @@ export class FlattenedTokens {
|
|
|
575
944
|
}
|
|
576
945
|
}
|
|
577
946
|
|
|
947
|
+
/**
|
|
948
|
+
* Convenience wrapper that returns a new {@link FlattenedTokens} for `tokens`.
|
|
949
|
+
* @param {RichTextToken[]} tokens
|
|
950
|
+
* @returns {FlattenedTokens}
|
|
951
|
+
*/
|
|
578
952
|
export function flattenForScan(tokens) {
|
|
579
953
|
return new FlattenedTokens(tokens);
|
|
580
954
|
}
|
|
581
955
|
|
|
582
956
|
const openModals = new Map();
|
|
583
957
|
|
|
958
|
+
/**
|
|
959
|
+
* A dismissable modal dialog. Subclass and populate `contentEl` and
|
|
960
|
+
* `titleEl` (both {@link VirtualEl}), typically inside {@link Modal.onOpen}.
|
|
961
|
+
* Call `open()` to show and `close()` to hide.
|
|
962
|
+
*/
|
|
584
963
|
export class Modal {
|
|
585
964
|
#modalId = uuid.create();
|
|
586
965
|
constructor() {
|
|
966
|
+
/** @type {VirtualEl} */
|
|
587
967
|
this.contentEl = new VirtualEl("div");
|
|
968
|
+
/** @type {VirtualEl} */
|
|
588
969
|
this.titleEl = new VirtualEl("h2");
|
|
589
970
|
}
|
|
590
971
|
|
|
972
|
+
/**
|
|
973
|
+
* Show the modal. Runs {@link Modal.onOpen} first, then mounts the current el state.
|
|
974
|
+
* @returns {void}
|
|
975
|
+
*/
|
|
591
976
|
open() {
|
|
592
977
|
if (openModals.has(this.#modalId)) return;
|
|
593
978
|
openModals.set(this.#modalId, this);
|
|
@@ -605,6 +990,10 @@ export class Modal {
|
|
|
605
990
|
});
|
|
606
991
|
}
|
|
607
992
|
|
|
993
|
+
/**
|
|
994
|
+
* Re-render the open modal from the current `titleEl` / `contentEl` state. No-op if closed.
|
|
995
|
+
* @returns {void}
|
|
996
|
+
*/
|
|
608
997
|
update() {
|
|
609
998
|
if (!openModals.has(this.#modalId)) return;
|
|
610
999
|
self.postMessage({
|
|
@@ -620,6 +1009,10 @@ export class Modal {
|
|
|
620
1009
|
});
|
|
621
1010
|
}
|
|
622
1011
|
|
|
1012
|
+
/**
|
|
1013
|
+
* Hide the modal and fire {@link Modal.onClose}.
|
|
1014
|
+
* @returns {void}
|
|
1015
|
+
*/
|
|
623
1016
|
close() {
|
|
624
1017
|
if (!openModals.has(this.#modalId)) return;
|
|
625
1018
|
openModals.delete(this.#modalId);
|
|
@@ -631,63 +1024,144 @@ export class Modal {
|
|
|
631
1024
|
this.onClose();
|
|
632
1025
|
}
|
|
633
1026
|
|
|
1027
|
+
/**
|
|
1028
|
+
* Lifecycle hook — override to populate `titleEl` / `contentEl` before the modal mounts.
|
|
1029
|
+
* @returns {void}
|
|
1030
|
+
*/
|
|
634
1031
|
onOpen() {}
|
|
1032
|
+
/**
|
|
1033
|
+
* Lifecycle hook — override to react to the modal being dismissed (by `close()` or the user).
|
|
1034
|
+
* @returns {void}
|
|
1035
|
+
*/
|
|
635
1036
|
onClose() {}
|
|
636
1037
|
}
|
|
637
1038
|
|
|
1039
|
+
/**
|
|
1040
|
+
* A tab in the plugin's settings UI. Subclass and override {@link PluginSettingTab.display}
|
|
1041
|
+
* to render into `this.containerEl`. Register with `plugin.addSettingTab(tab)`.
|
|
1042
|
+
*/
|
|
638
1043
|
export class PluginSettingTab {
|
|
1044
|
+
/**
|
|
1045
|
+
* The owning plugin. Set by the host in {@link Plugin.addSettingTab}.
|
|
1046
|
+
* @type {Plugin}
|
|
1047
|
+
*/
|
|
1048
|
+
plugin;
|
|
639
1049
|
constructor() {
|
|
1050
|
+
/** @type {VirtualEl} */
|
|
640
1051
|
this.containerEl = new VirtualEl("div");
|
|
1052
|
+
/** @type {string | null} */
|
|
641
1053
|
this.name = null;
|
|
642
1054
|
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Set the tab's label.
|
|
1057
|
+
* @param {string} name
|
|
1058
|
+
* @returns {this}
|
|
1059
|
+
*/
|
|
643
1060
|
setName(name) {
|
|
644
1061
|
this.name = name;
|
|
645
1062
|
return this;
|
|
646
1063
|
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Override to render the tab's contents into `this.containerEl`.
|
|
1066
|
+
* @returns {void}
|
|
1067
|
+
*/
|
|
647
1068
|
display() {}
|
|
1069
|
+
/**
|
|
1070
|
+
* Re-invoke {@link PluginSettingTab.display}. `reset: true` also discards the rendered tree.
|
|
1071
|
+
* @param {{ reset?: boolean }} [options]
|
|
1072
|
+
* @returns {Promise<void>}
|
|
1073
|
+
*/
|
|
648
1074
|
refresh({ reset = false } = {}) {
|
|
649
1075
|
return hostCall("refreshSettingTab", { reset });
|
|
650
1076
|
}
|
|
651
1077
|
}
|
|
652
1078
|
|
|
1079
|
+
/**
|
|
1080
|
+
* Fluent builder for a single labeled settings row. `new Setting(containerEl)`
|
|
1081
|
+
* appends a row (name + description + control area) to `containerEl` and
|
|
1082
|
+
* returns a builder for populating it.
|
|
1083
|
+
*/
|
|
653
1084
|
export class Setting {
|
|
1085
|
+
/**
|
|
1086
|
+
* @param {VirtualEl} containerEl
|
|
1087
|
+
*/
|
|
654
1088
|
constructor(containerEl) {
|
|
1089
|
+
/** @type {VirtualEl} */
|
|
655
1090
|
this.settingEl = containerEl.createDiv({ cls: "setting-item" });
|
|
1091
|
+
/** @type {VirtualEl} */
|
|
656
1092
|
this.infoEl = this.settingEl.createDiv({ cls: "setting-item-info" });
|
|
1093
|
+
/** @type {VirtualEl} */
|
|
657
1094
|
this.nameEl = this.infoEl.createEl("h2", { cls: "setting-item-name" });
|
|
1095
|
+
/** @type {VirtualEl} */
|
|
658
1096
|
this.descEl = this.infoEl.createEl("p", { cls: "setting-item-desc" });
|
|
1097
|
+
/** @type {VirtualEl} */
|
|
659
1098
|
this.controlEl = this.settingEl.createDiv({
|
|
660
1099
|
cls: "setting-item-control",
|
|
661
1100
|
});
|
|
662
1101
|
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Set the row's name/label.
|
|
1104
|
+
* @param {string} text
|
|
1105
|
+
* @returns {this}
|
|
1106
|
+
*/
|
|
663
1107
|
setName(text) {
|
|
664
1108
|
this.nameEl.setText(text);
|
|
665
1109
|
return this;
|
|
666
1110
|
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Set the row's description text below the name.
|
|
1113
|
+
* @param {string} text
|
|
1114
|
+
* @returns {this}
|
|
1115
|
+
*/
|
|
667
1116
|
setDesc(text) {
|
|
668
1117
|
this.descEl.setText(text);
|
|
669
1118
|
return this;
|
|
670
1119
|
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Add a text input; the callback receives a {@link TextComponent}.
|
|
1122
|
+
* @param {(component: TextComponent) => void} callback
|
|
1123
|
+
* @returns {this}
|
|
1124
|
+
*/
|
|
671
1125
|
addText(callback) {
|
|
672
1126
|
const component = new TextComponent(this.controlEl);
|
|
673
1127
|
callback(component);
|
|
674
1128
|
return this;
|
|
675
1129
|
}
|
|
1130
|
+
/**
|
|
1131
|
+
* Add a multi-line text input; the callback receives a {@link TextAreaComponent}.
|
|
1132
|
+
* @param {(component: TextAreaComponent) => void} callback
|
|
1133
|
+
* @returns {this}
|
|
1134
|
+
*/
|
|
676
1135
|
addTextArea(callback) {
|
|
677
1136
|
const component = new TextAreaComponent(this.controlEl);
|
|
678
1137
|
callback(component);
|
|
679
1138
|
return this;
|
|
680
1139
|
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Add a toggle switch; the callback receives a {@link ToggleComponent}.
|
|
1142
|
+
* @param {(component: ToggleComponent) => void} callback
|
|
1143
|
+
* @returns {this}
|
|
1144
|
+
*/
|
|
681
1145
|
addToggle(callback) {
|
|
682
1146
|
const component = new ToggleComponent(this.controlEl);
|
|
683
1147
|
callback(component);
|
|
684
1148
|
return this;
|
|
685
1149
|
}
|
|
1150
|
+
/**
|
|
1151
|
+
* Add a dropdown; the callback receives a {@link DropdownComponent}.
|
|
1152
|
+
* @param {(component: DropdownComponent) => void} callback
|
|
1153
|
+
* @returns {this}
|
|
1154
|
+
*/
|
|
686
1155
|
addDropdown(callback) {
|
|
687
1156
|
const component = new DropdownComponent(this.controlEl);
|
|
688
1157
|
callback(component);
|
|
689
1158
|
return this;
|
|
690
1159
|
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Add a button; the callback receives a {@link ButtonComponent}.
|
|
1162
|
+
* @param {(component: ButtonComponent) => void} callback
|
|
1163
|
+
* @returns {this}
|
|
1164
|
+
*/
|
|
691
1165
|
addButton(callback) {
|
|
692
1166
|
const component = new ButtonComponent(this.controlEl);
|
|
693
1167
|
callback(component);
|
|
@@ -695,189 +1169,344 @@ export class Setting {
|
|
|
695
1169
|
}
|
|
696
1170
|
}
|
|
697
1171
|
|
|
1172
|
+
/** Single-line text input, built via {@link Setting.addText}. */
|
|
698
1173
|
export class TextComponent {
|
|
699
1174
|
/** @internal */
|
|
700
1175
|
constructor(containerEl) {
|
|
1176
|
+
/** @type {VirtualEl} */
|
|
701
1177
|
this.el = containerEl.createEl("input", {
|
|
702
1178
|
attr: { type: "text" },
|
|
703
1179
|
cls: "setting-item-text-input",
|
|
704
1180
|
});
|
|
705
1181
|
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Set the current value.
|
|
1184
|
+
* @param {string | null} value
|
|
1185
|
+
* @returns {this}
|
|
1186
|
+
*/
|
|
706
1187
|
setValue(value) {
|
|
707
1188
|
this.el.setAttr("value", value == null ? "" : String(value));
|
|
708
1189
|
return this;
|
|
709
1190
|
}
|
|
1191
|
+
/**
|
|
1192
|
+
* Set the placeholder text shown when the input is empty.
|
|
1193
|
+
* @param {string} value
|
|
1194
|
+
* @returns {this}
|
|
1195
|
+
*/
|
|
710
1196
|
setPlaceholder(value) {
|
|
711
1197
|
this.el.setAttr("placeholder", value);
|
|
712
1198
|
return this;
|
|
713
1199
|
}
|
|
1200
|
+
/**
|
|
1201
|
+
* Fires with the new string value on every change.
|
|
1202
|
+
* @param {(value: string) => void} callback
|
|
1203
|
+
* @returns {this}
|
|
1204
|
+
*/
|
|
714
1205
|
onChange(callback) {
|
|
715
|
-
this.el.onChange((event) => callback(event.target.value));
|
|
1206
|
+
this.el.onChange((event) => callback(event.target.value ?? ""));
|
|
716
1207
|
return this;
|
|
717
1208
|
}
|
|
718
1209
|
}
|
|
719
1210
|
|
|
1211
|
+
/** Multi-line text input, built via {@link Setting.addTextArea}. */
|
|
720
1212
|
export class TextAreaComponent {
|
|
721
1213
|
/** @internal */
|
|
722
1214
|
constructor(containerEl) {
|
|
1215
|
+
/** @type {VirtualEl} */
|
|
723
1216
|
this.el = containerEl.createEl("textarea", {
|
|
724
1217
|
cls: "setting-item-textarea",
|
|
725
1218
|
});
|
|
726
1219
|
}
|
|
1220
|
+
/**
|
|
1221
|
+
* Set the current value.
|
|
1222
|
+
* @param {string | null} value
|
|
1223
|
+
* @returns {this}
|
|
1224
|
+
*/
|
|
727
1225
|
setValue(value) {
|
|
728
1226
|
this.el.setText(value == null ? "" : String(value));
|
|
729
1227
|
return this;
|
|
730
1228
|
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Set the placeholder text shown when the input is empty.
|
|
1231
|
+
* @param {string} value
|
|
1232
|
+
* @returns {this}
|
|
1233
|
+
*/
|
|
731
1234
|
setPlaceholder(value) {
|
|
732
1235
|
this.el.setAttr("placeholder", value);
|
|
733
1236
|
return this;
|
|
734
1237
|
}
|
|
1238
|
+
/**
|
|
1239
|
+
* Fires with the new string value on every change.
|
|
1240
|
+
* @param {(value: string) => void} callback
|
|
1241
|
+
* @returns {this}
|
|
1242
|
+
*/
|
|
735
1243
|
onChange(callback) {
|
|
736
|
-
this.el.onChange((event) => callback(event.target.value));
|
|
1244
|
+
this.el.onChange((event) => callback(event.target.value ?? ""));
|
|
737
1245
|
return this;
|
|
738
1246
|
}
|
|
739
1247
|
}
|
|
740
1248
|
|
|
1249
|
+
/** On/off toggle switch, built via {@link Setting.addToggle}. */
|
|
741
1250
|
export class ToggleComponent {
|
|
742
1251
|
/** @internal */
|
|
743
1252
|
constructor(containerEl) {
|
|
1253
|
+
/** @type {VirtualEl} */
|
|
744
1254
|
this.el = containerEl.createEl("toggle-switch", {
|
|
745
1255
|
cls: "setting-item-toggle",
|
|
746
1256
|
});
|
|
747
1257
|
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Set the checked state.
|
|
1260
|
+
* @param {boolean} value
|
|
1261
|
+
* @returns {this}
|
|
1262
|
+
*/
|
|
748
1263
|
setValue(value) {
|
|
749
1264
|
if (value) this.el.setAttr("checked", "");
|
|
750
1265
|
else delete this.el.attrs.checked;
|
|
751
1266
|
return this;
|
|
752
1267
|
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Fires with the new boolean value on every change.
|
|
1270
|
+
* @param {(checked: boolean) => void} callback
|
|
1271
|
+
* @returns {this}
|
|
1272
|
+
*/
|
|
753
1273
|
onChange(callback) {
|
|
754
|
-
this.el.onChange((event) => callback(event.target.checked));
|
|
1274
|
+
this.el.onChange((event) => callback(event.target.checked ?? false));
|
|
755
1275
|
return this;
|
|
756
1276
|
}
|
|
757
1277
|
}
|
|
758
1278
|
|
|
1279
|
+
/** Single-select dropdown, built via {@link Setting.addDropdown}. */
|
|
759
1280
|
export class DropdownComponent {
|
|
760
1281
|
/** @internal */
|
|
761
1282
|
constructor(containerEl) {
|
|
1283
|
+
/** @type {VirtualEl} */
|
|
762
1284
|
this.el = containerEl.createEl("select", {
|
|
763
1285
|
cls: "setting-item-dropdown",
|
|
764
1286
|
});
|
|
765
1287
|
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Append one option.
|
|
1290
|
+
* @param {string} value
|
|
1291
|
+
* @param {string} label
|
|
1292
|
+
* @returns {this}
|
|
1293
|
+
*/
|
|
766
1294
|
addOption(value, label) {
|
|
767
1295
|
this.el.createEl("option", { text: label, attr: { value } });
|
|
768
1296
|
return this;
|
|
769
1297
|
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Append every `{ value: label }` entry as an option.
|
|
1300
|
+
* @param {Record<string, string>} map
|
|
1301
|
+
* @returns {this}
|
|
1302
|
+
*/
|
|
770
1303
|
addOptions(map) {
|
|
771
1304
|
for (const [value, label] of Object.entries(map)) {
|
|
772
1305
|
this.addOption(value, label);
|
|
773
1306
|
}
|
|
774
1307
|
return this;
|
|
775
1308
|
}
|
|
1309
|
+
/**
|
|
1310
|
+
* Select the option whose value matches.
|
|
1311
|
+
* @param {string} value
|
|
1312
|
+
* @returns {this}
|
|
1313
|
+
*/
|
|
776
1314
|
setValue(value) {
|
|
777
1315
|
for (const child of this.el.children) {
|
|
778
|
-
if (child
|
|
1316
|
+
if (!(child instanceof VirtualEl)) continue;
|
|
1317
|
+
if (child.attrs.value === value) {
|
|
779
1318
|
child.attrs.selected = "";
|
|
780
|
-
} else
|
|
1319
|
+
} else {
|
|
781
1320
|
delete child.attrs.selected;
|
|
782
1321
|
}
|
|
783
1322
|
}
|
|
784
1323
|
return this;
|
|
785
1324
|
}
|
|
1325
|
+
/**
|
|
1326
|
+
* Fires with the newly selected value on every change.
|
|
1327
|
+
* @param {(value: string) => void} callback
|
|
1328
|
+
* @returns {this}
|
|
1329
|
+
*/
|
|
786
1330
|
onChange(callback) {
|
|
787
|
-
this.el.onChange((event) => callback(event.target.value));
|
|
1331
|
+
this.el.onChange((event) => callback(event.target.value ?? ""));
|
|
788
1332
|
return this;
|
|
789
1333
|
}
|
|
790
1334
|
}
|
|
791
1335
|
|
|
1336
|
+
/** Clickable button, built via {@link Setting.addButton}. */
|
|
792
1337
|
export class ButtonComponent {
|
|
793
1338
|
/** @internal */
|
|
794
1339
|
constructor(containerEl) {
|
|
1340
|
+
/** @type {VirtualEl} */
|
|
795
1341
|
this.el = containerEl.createEl("button", {
|
|
796
1342
|
cls: "rounded-button",
|
|
797
1343
|
});
|
|
798
1344
|
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Set the button's label.
|
|
1347
|
+
* @param {string} text
|
|
1348
|
+
* @returns {this}
|
|
1349
|
+
*/
|
|
799
1350
|
setButtonText(text) {
|
|
800
1351
|
this.el.setText(text);
|
|
801
1352
|
return this;
|
|
802
1353
|
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Style the button as the row's primary call-to-action.
|
|
1356
|
+
* @returns {this}
|
|
1357
|
+
*/
|
|
803
1358
|
setCta() {
|
|
804
1359
|
this.el.addClass("rounded-button-primary");
|
|
805
1360
|
return this;
|
|
806
1361
|
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Register a click handler.
|
|
1364
|
+
* @param {() => void} callback
|
|
1365
|
+
* @returns {this}
|
|
1366
|
+
*/
|
|
807
1367
|
onClick(callback) {
|
|
808
1368
|
this.el.onClick(callback);
|
|
809
1369
|
return this;
|
|
810
1370
|
}
|
|
811
1371
|
}
|
|
812
1372
|
|
|
1373
|
+
/** Renders a host-provided icon by name. Built via {@link VirtualEl.createIcon}. */
|
|
813
1374
|
export class IconComponent {
|
|
814
1375
|
/** @internal */
|
|
815
1376
|
constructor(containerEl) {
|
|
1377
|
+
/** @type {VirtualEl} */
|
|
816
1378
|
this.el = containerEl.createEl("plugin-icon");
|
|
817
1379
|
}
|
|
1380
|
+
/**
|
|
1381
|
+
* Set the icon name.
|
|
1382
|
+
* @param {string} name
|
|
1383
|
+
* @returns {this}
|
|
1384
|
+
*/
|
|
818
1385
|
setIcon(name) {
|
|
819
1386
|
this.el.setAttr("icon", name);
|
|
820
1387
|
return this;
|
|
821
1388
|
}
|
|
822
1389
|
}
|
|
823
1390
|
|
|
1391
|
+
/**
|
|
1392
|
+
* Renders a repo blob (by owning DID + blob CID) as an image. Built via
|
|
1393
|
+
* {@link VirtualEl.createBlobImage}.
|
|
1394
|
+
*/
|
|
824
1395
|
export class BlobImageComponent {
|
|
825
1396
|
/** @internal */
|
|
826
1397
|
constructor(containerEl) {
|
|
1398
|
+
/** @type {VirtualEl} */
|
|
827
1399
|
this.el = containerEl.createEl("plugin-blob-image");
|
|
828
1400
|
}
|
|
1401
|
+
/**
|
|
1402
|
+
* DID of the repo the blob belongs to.
|
|
1403
|
+
* @param {string} did
|
|
1404
|
+
* @returns {this}
|
|
1405
|
+
*/
|
|
829
1406
|
setDid(did) {
|
|
830
1407
|
this.el.setAttr("did", did);
|
|
831
1408
|
return this;
|
|
832
1409
|
}
|
|
1410
|
+
/**
|
|
1411
|
+
* CID of the blob to render.
|
|
1412
|
+
* @param {string} cid
|
|
1413
|
+
* @returns {this}
|
|
1414
|
+
*/
|
|
833
1415
|
setCid(cid) {
|
|
834
1416
|
this.el.setAttr("cid", cid);
|
|
835
1417
|
return this;
|
|
836
1418
|
}
|
|
1419
|
+
/**
|
|
1420
|
+
* Set the image's alt text.
|
|
1421
|
+
* @param {string} alt
|
|
1422
|
+
* @returns {this}
|
|
1423
|
+
*/
|
|
837
1424
|
setAlt(alt) {
|
|
838
1425
|
this.el.setAttr("alt", alt);
|
|
839
1426
|
return this;
|
|
840
1427
|
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Override the CDN URL prefix used to fetch the blob.
|
|
1430
|
+
* @param {string} prefix
|
|
1431
|
+
* @returns {this}
|
|
1432
|
+
*/
|
|
841
1433
|
setCdnPrefix(prefix) {
|
|
842
1434
|
this.el.setAttr("cdn-prefix", prefix);
|
|
843
1435
|
return this;
|
|
844
1436
|
}
|
|
845
1437
|
}
|
|
846
1438
|
|
|
1439
|
+
/**
|
|
1440
|
+
* Renders a list of profiles from an array of DIDs, hydrated by the host.
|
|
1441
|
+
* Built via {@link VirtualEl.createProfilesList}.
|
|
1442
|
+
*/
|
|
847
1443
|
export class ProfilesListComponent {
|
|
848
1444
|
/** @internal */
|
|
849
1445
|
constructor(containerEl) {
|
|
1446
|
+
/** @type {VirtualEl} */
|
|
850
1447
|
this.el = containerEl.createEl("plugin-profiles-list");
|
|
851
1448
|
}
|
|
1449
|
+
/**
|
|
1450
|
+
* Set the DIDs to render (array or comma-separated string).
|
|
1451
|
+
* @param {string[] | string} dids
|
|
1452
|
+
* @returns {this}
|
|
1453
|
+
*/
|
|
852
1454
|
setDids(dids) {
|
|
853
1455
|
const value = Array.isArray(dids) ? dids.join(",") : String(dids ?? "");
|
|
854
1456
|
this.el.setAttr("dids", value);
|
|
855
1457
|
return this;
|
|
856
1458
|
}
|
|
1459
|
+
/**
|
|
1460
|
+
* Message shown when the list is empty.
|
|
1461
|
+
* @param {string} message
|
|
1462
|
+
* @returns {this}
|
|
1463
|
+
*/
|
|
857
1464
|
setEmptyMessage(message) {
|
|
858
1465
|
this.el.setAttr("empty-message", message);
|
|
859
1466
|
return this;
|
|
860
1467
|
}
|
|
861
1468
|
}
|
|
862
1469
|
|
|
1470
|
+
/**
|
|
1471
|
+
* Renders a feed of posts from an array of post URIs, hydrated by the host.
|
|
1472
|
+
* Built via {@link VirtualEl.createPostsFeed}.
|
|
1473
|
+
*/
|
|
863
1474
|
export class PostsFeedComponent {
|
|
864
1475
|
/** @internal */
|
|
865
1476
|
constructor(containerEl) {
|
|
1477
|
+
/** @type {VirtualEl} */
|
|
866
1478
|
this.el = containerEl.createEl("plugin-posts-feed");
|
|
867
1479
|
}
|
|
1480
|
+
/**
|
|
1481
|
+
* Set the post URIs to render (array or comma-separated string).
|
|
1482
|
+
* @param {string[] | string} uris
|
|
1483
|
+
* @returns {this}
|
|
1484
|
+
*/
|
|
868
1485
|
setUris(uris) {
|
|
869
1486
|
const value = Array.isArray(uris) ? uris.join(",") : String(uris ?? "");
|
|
870
1487
|
this.el.setAttr("uris", value);
|
|
871
1488
|
return this;
|
|
872
1489
|
}
|
|
1490
|
+
/**
|
|
1491
|
+
* Message shown when the feed is empty.
|
|
1492
|
+
* @param {string} message
|
|
1493
|
+
* @returns {this}
|
|
1494
|
+
*/
|
|
873
1495
|
setEmptyMessage(message) {
|
|
874
1496
|
this.el.setAttr("empty-message", message);
|
|
875
1497
|
return this;
|
|
876
1498
|
}
|
|
877
1499
|
}
|
|
878
1500
|
|
|
1501
|
+
/**
|
|
1502
|
+
* A text node in a {@link VirtualEl} tree. Null/undefined coerce to `""`.
|
|
1503
|
+
*/
|
|
879
1504
|
export class VirtualText {
|
|
1505
|
+
/**
|
|
1506
|
+
* @param {string | null | undefined} value
|
|
1507
|
+
*/
|
|
880
1508
|
constructor(value) {
|
|
1509
|
+
/** @type {string} */
|
|
881
1510
|
this.value = value == null ? "" : String(value);
|
|
882
1511
|
}
|
|
883
1512
|
|
|
@@ -887,20 +1516,44 @@ export class VirtualText {
|
|
|
887
1516
|
}
|
|
888
1517
|
}
|
|
889
1518
|
|
|
1519
|
+
/**
|
|
1520
|
+
* Minimal DOM-builder node. Plugins run in a sandbox and cannot touch the real
|
|
1521
|
+
* DOM directly; instead they build a serializable {@link VirtualEl} tree that
|
|
1522
|
+
* the host renders and reconciles.
|
|
1523
|
+
*/
|
|
890
1524
|
export class VirtualEl {
|
|
1525
|
+
/**
|
|
1526
|
+
* @param {string} tag
|
|
1527
|
+
*/
|
|
891
1528
|
constructor(tag) {
|
|
1529
|
+
/** @type {string} */
|
|
892
1530
|
this.tag = tag;
|
|
1531
|
+
/** @type {Record<string, string>} */
|
|
893
1532
|
this.attrs = {};
|
|
1533
|
+
/** @type {Record<string, string>} */
|
|
894
1534
|
this.styles = {};
|
|
1535
|
+
/** @type {(VirtualEl | VirtualText)[]} */
|
|
895
1536
|
this.children = [];
|
|
1537
|
+
/** @type {Record<string, number>} */
|
|
896
1538
|
this.events = {};
|
|
897
1539
|
}
|
|
898
1540
|
|
|
1541
|
+
/**
|
|
1542
|
+
* Set an inline style.
|
|
1543
|
+
* @param {string} name
|
|
1544
|
+
* @param {string | null} value
|
|
1545
|
+
* @returns {this}
|
|
1546
|
+
*/
|
|
899
1547
|
setStyle(name, value) {
|
|
900
1548
|
this.styles[String(name)] = value == null ? "" : String(value);
|
|
901
1549
|
return this;
|
|
902
1550
|
}
|
|
903
1551
|
|
|
1552
|
+
/**
|
|
1553
|
+
* Register a click handler.
|
|
1554
|
+
* @param {(event?: object) => void} fn
|
|
1555
|
+
* @returns {this}
|
|
1556
|
+
*/
|
|
904
1557
|
onClick(fn) {
|
|
905
1558
|
const handlerId = uuid.create();
|
|
906
1559
|
callHandlers.set(handlerId, fn);
|
|
@@ -908,6 +1561,11 @@ export class VirtualEl {
|
|
|
908
1561
|
return this;
|
|
909
1562
|
}
|
|
910
1563
|
|
|
1564
|
+
/**
|
|
1565
|
+
* Register a change handler (form controls).
|
|
1566
|
+
* @param {(event: { target: { value?: string, checked?: boolean } }) => void} fn
|
|
1567
|
+
* @returns {this}
|
|
1568
|
+
*/
|
|
911
1569
|
onChange(fn) {
|
|
912
1570
|
const handlerId = uuid.create();
|
|
913
1571
|
callHandlers.set(handlerId, fn);
|
|
@@ -915,6 +1573,11 @@ export class VirtualEl {
|
|
|
915
1573
|
return this;
|
|
916
1574
|
}
|
|
917
1575
|
|
|
1576
|
+
/**
|
|
1577
|
+
* Register an input handler (text inputs).
|
|
1578
|
+
* @param {(event: { target: { value?: string } }) => void} fn
|
|
1579
|
+
* @returns {this}
|
|
1580
|
+
*/
|
|
918
1581
|
onInput(fn) {
|
|
919
1582
|
const handlerId = uuid.create();
|
|
920
1583
|
callHandlers.set(handlerId, fn);
|
|
@@ -922,17 +1585,31 @@ export class VirtualEl {
|
|
|
922
1585
|
return this;
|
|
923
1586
|
}
|
|
924
1587
|
|
|
1588
|
+
/**
|
|
1589
|
+
* Replace all children with a single {@link VirtualText} node.
|
|
1590
|
+
* @param {string | null} text
|
|
1591
|
+
* @returns {this}
|
|
1592
|
+
*/
|
|
925
1593
|
setText(text) {
|
|
926
1594
|
this.children = [];
|
|
927
1595
|
if (text != null && text !== "") this.children.push(new VirtualText(text));
|
|
928
1596
|
return this;
|
|
929
1597
|
}
|
|
930
1598
|
|
|
1599
|
+
/**
|
|
1600
|
+
* Remove all children.
|
|
1601
|
+
* @returns {this}
|
|
1602
|
+
*/
|
|
931
1603
|
empty() {
|
|
932
1604
|
this.children = [];
|
|
933
1605
|
return this;
|
|
934
1606
|
}
|
|
935
1607
|
|
|
1608
|
+
/**
|
|
1609
|
+
* Append a {@link VirtualEl} or {@link VirtualText}; throws on anything else.
|
|
1610
|
+
* @param {VirtualEl | VirtualText} child
|
|
1611
|
+
* @returns {this}
|
|
1612
|
+
*/
|
|
936
1613
|
appendChild(child) {
|
|
937
1614
|
if (!(child instanceof VirtualEl) && !(child instanceof VirtualText)) {
|
|
938
1615
|
throw new TypeError(
|
|
@@ -943,27 +1620,57 @@ export class VirtualEl {
|
|
|
943
1620
|
return this;
|
|
944
1621
|
}
|
|
945
1622
|
|
|
1623
|
+
/**
|
|
1624
|
+
* Append a {@link VirtualText} child.
|
|
1625
|
+
* @param {string} value
|
|
1626
|
+
* @returns {this}
|
|
1627
|
+
*/
|
|
946
1628
|
appendText(value) {
|
|
947
1629
|
this.children.push(new VirtualText(value));
|
|
948
1630
|
return this;
|
|
949
1631
|
}
|
|
950
1632
|
|
|
1633
|
+
/**
|
|
1634
|
+
* Append and return a {@link VirtualText} child.
|
|
1635
|
+
* @param {string} value
|
|
1636
|
+
* @returns {VirtualText}
|
|
1637
|
+
*/
|
|
951
1638
|
createText(value) {
|
|
952
1639
|
const node = new VirtualText(value);
|
|
953
1640
|
this.children.push(node);
|
|
954
1641
|
return node;
|
|
955
1642
|
}
|
|
956
1643
|
|
|
1644
|
+
/**
|
|
1645
|
+
* Add a CSS class, whitespace-joined with any existing classes.
|
|
1646
|
+
* @param {string} cls
|
|
1647
|
+
* @returns {this}
|
|
1648
|
+
*/
|
|
957
1649
|
addClass(cls) {
|
|
958
1650
|
this.attrs.class = this.attrs.class ? `${this.attrs.class} ${cls}` : cls;
|
|
959
1651
|
return this;
|
|
960
1652
|
}
|
|
961
1653
|
|
|
1654
|
+
/**
|
|
1655
|
+
* Set an attribute; `undefined` coerces to `""`.
|
|
1656
|
+
* @param {string} name
|
|
1657
|
+
* @param {string | undefined} value
|
|
1658
|
+
* @returns {this}
|
|
1659
|
+
*/
|
|
962
1660
|
setAttr(name, value) {
|
|
963
1661
|
this.attrs[name] = value === undefined ? "" : value;
|
|
964
1662
|
return this;
|
|
965
1663
|
}
|
|
966
1664
|
|
|
1665
|
+
/**
|
|
1666
|
+
* Append a child element and return it. `options` accepts `text`,
|
|
1667
|
+
* `cls` (string or string[]), and `attr` (object). The optional callback
|
|
1668
|
+
* receives the new child for further building.
|
|
1669
|
+
* @param {string} tag
|
|
1670
|
+
* @param {{ text?: string, cls?: string | string[], attr?: Record<string, string> }} [options]
|
|
1671
|
+
* @param {(child: VirtualEl) => void} [callback]
|
|
1672
|
+
* @returns {VirtualEl}
|
|
1673
|
+
*/
|
|
967
1674
|
createEl(tag, options = {}, callback) {
|
|
968
1675
|
const child = new VirtualEl(tag);
|
|
969
1676
|
if (options.text != null) child.setText(options.text);
|
|
@@ -978,32 +1685,64 @@ export class VirtualEl {
|
|
|
978
1685
|
return child;
|
|
979
1686
|
}
|
|
980
1687
|
|
|
1688
|
+
/**
|
|
1689
|
+
* Shorthand for `createEl("div", ...)`.
|
|
1690
|
+
* @param {{ text?: string, cls?: string | string[], attr?: Record<string, string> }} [options]
|
|
1691
|
+
* @param {(child: VirtualEl) => void} [callback]
|
|
1692
|
+
* @returns {VirtualEl}
|
|
1693
|
+
*/
|
|
981
1694
|
createDiv(options = {}, callback) {
|
|
982
1695
|
return this.createEl("div", options, callback);
|
|
983
1696
|
}
|
|
984
1697
|
|
|
1698
|
+
/**
|
|
1699
|
+
* Shorthand for `createEl("span", ...)`.
|
|
1700
|
+
* @param {{ text?: string, cls?: string | string[], attr?: Record<string, string> }} [options]
|
|
1701
|
+
* @param {(child: VirtualEl) => void} [callback]
|
|
1702
|
+
* @returns {VirtualEl}
|
|
1703
|
+
*/
|
|
985
1704
|
createSpan(options = {}, callback) {
|
|
986
1705
|
return this.createEl("span", options, callback);
|
|
987
1706
|
}
|
|
988
1707
|
|
|
1708
|
+
/**
|
|
1709
|
+
* Append a profiles-list custom component and return its builder.
|
|
1710
|
+
* @param {(c: ProfilesListComponent) => void} [callback]
|
|
1711
|
+
* @returns {ProfilesListComponent}
|
|
1712
|
+
*/
|
|
989
1713
|
createProfilesList(callback) {
|
|
990
1714
|
const component = new ProfilesListComponent(this);
|
|
991
1715
|
if (typeof callback === "function") callback(component);
|
|
992
1716
|
return component;
|
|
993
1717
|
}
|
|
994
1718
|
|
|
1719
|
+
/**
|
|
1720
|
+
* Append a posts-feed custom component and return its builder.
|
|
1721
|
+
* @param {(c: PostsFeedComponent) => void} [callback]
|
|
1722
|
+
* @returns {PostsFeedComponent}
|
|
1723
|
+
*/
|
|
995
1724
|
createPostsFeed(callback) {
|
|
996
1725
|
const component = new PostsFeedComponent(this);
|
|
997
1726
|
if (typeof callback === "function") callback(component);
|
|
998
1727
|
return component;
|
|
999
1728
|
}
|
|
1000
1729
|
|
|
1730
|
+
/**
|
|
1731
|
+
* Append an icon custom component and return its builder.
|
|
1732
|
+
* @param {(c: IconComponent) => void} [callback]
|
|
1733
|
+
* @returns {IconComponent}
|
|
1734
|
+
*/
|
|
1001
1735
|
createIcon(callback) {
|
|
1002
1736
|
const component = new IconComponent(this);
|
|
1003
1737
|
if (typeof callback === "function") callback(component);
|
|
1004
1738
|
return component;
|
|
1005
1739
|
}
|
|
1006
1740
|
|
|
1741
|
+
/**
|
|
1742
|
+
* Append a blob-image custom component and return its builder.
|
|
1743
|
+
* @param {(c: BlobImageComponent) => void} [callback]
|
|
1744
|
+
* @returns {BlobImageComponent}
|
|
1745
|
+
*/
|
|
1007
1746
|
createBlobImage(callback) {
|
|
1008
1747
|
const component = new BlobImageComponent(this);
|
|
1009
1748
|
if (typeof callback === "function") callback(component);
|