@impro.social/impro-plugin 0.0.8 → 0.0.10

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 +90 -0
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -147,6 +147,9 @@ class PluginData {
147
147
  getProfile(did) {
148
148
  return hostCall("getProfile", { did });
149
149
  }
150
+ getRecord(repo, collection, rkey) {
151
+ return hostCall("getRecord", { repo, collection, rkey });
152
+ }
150
153
  }
151
154
 
152
155
  class App {
@@ -310,6 +313,30 @@ export class Plugin {
310
313
  });
311
314
  }
312
315
 
316
+ // callback(tokens, context) receives the rich-text token stream for one
317
+ // post and returns a new token array (or the input unchanged). The host
318
+ // batches all posts of a render into one call per plugin.
319
+ registerRichTextTransform(callback = (tokens) => tokens) {
320
+ const handlerId = uuid.create();
321
+ callHandlers.set(handlerId, async (batch) => {
322
+ const results = [];
323
+ for (const { tokens, context } of batch) {
324
+ try {
325
+ const value = await callback(tokens, context);
326
+ results.push({ value: serializeTransformTokens(value) });
327
+ } catch (error) {
328
+ results.push({ error: error?.message ?? String(error) });
329
+ }
330
+ }
331
+ return results;
332
+ });
333
+ self.postMessage({
334
+ type: "register",
335
+ target: "richTextTransform",
336
+ handlerId,
337
+ });
338
+ }
339
+
313
340
  registerSlot(name, callback = () => null) {
314
341
  const handlerId = uuid.create();
315
342
  callHandlers.set(handlerId, async (context) => {
@@ -354,6 +381,69 @@ export class Plugin {
354
381
  }
355
382
  }
356
383
 
384
+ function serializeTransformTokens(tokens) {
385
+ if (!Array.isArray(tokens)) return tokens;
386
+ return tokens.map((token) => {
387
+ if (
388
+ (token?.type === "inline" || token?.type === "block") &&
389
+ token.node instanceof VirtualEl
390
+ ) {
391
+ return { ...token, node: token.node._serialize() };
392
+ }
393
+ return token;
394
+ });
395
+ }
396
+
397
+ // Concatenates the renderable text of a token stream into a string with a position map,
398
+ // so a transform can pattern-match across token boundaries and map matches back to tokens.
399
+ export class FlattenedTokens {
400
+ constructor(tokens) {
401
+ this._segments = [];
402
+ let text = "";
403
+ for (const token of tokens) {
404
+ let value = null;
405
+ if (token.type === "text") value = token.value;
406
+ else if (token.type === "facet") value = token.text;
407
+ const start = text.length;
408
+ if (value != null) text += value;
409
+ this._segments.push({ token, start, end: text.length });
410
+ }
411
+ this.text = text;
412
+ }
413
+
414
+ // emit an inert range (no facets)
415
+ textFor(start, end) {
416
+ return this.text.slice(start, end);
417
+ }
418
+
419
+ // emit a live range, demoting partial facet tokens to text tokens
420
+ tokensFor(start, end) {
421
+ const out = [];
422
+ for (const segment of this._segments) {
423
+ if (segment.start === segment.end) {
424
+ if (segment.start >= start && segment.start < end) {
425
+ out.push(segment.token);
426
+ }
427
+ continue;
428
+ }
429
+ if (segment.end <= start || segment.start >= end) continue;
430
+ const from = Math.max(start, segment.start);
431
+ const to = Math.min(end, segment.end);
432
+ const isWholeToken = from === segment.start && to === segment.end;
433
+ if (isWholeToken) {
434
+ out.push(segment.token);
435
+ } else {
436
+ out.push({ type: "text", value: this.text.slice(from, to) });
437
+ }
438
+ }
439
+ return out;
440
+ }
441
+ }
442
+
443
+ export function flattenForScan(tokens) {
444
+ return new FlattenedTokens(tokens);
445
+ }
446
+
357
447
  const openModals = new Map();
358
448
 
359
449
  export class Modal {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@impro.social/impro-plugin",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
6
  "license": "0BSD",