@impro.social/impro-plugin 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.js +87 -0
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -310,6 +310,30 @@ export class Plugin {
|
|
|
310
310
|
});
|
|
311
311
|
}
|
|
312
312
|
|
|
313
|
+
// callback(tokens, context) receives the rich-text token stream for one
|
|
314
|
+
// post and returns a new token array (or the input unchanged). The host
|
|
315
|
+
// batches all posts of a render into one call per plugin.
|
|
316
|
+
registerRichTextTransform(callback = (tokens) => tokens) {
|
|
317
|
+
const handlerId = uuid.create();
|
|
318
|
+
callHandlers.set(handlerId, async (batch) => {
|
|
319
|
+
const results = [];
|
|
320
|
+
for (const { tokens, context } of batch) {
|
|
321
|
+
try {
|
|
322
|
+
const value = await callback(tokens, context);
|
|
323
|
+
results.push({ value: serializeTransformTokens(value) });
|
|
324
|
+
} catch (error) {
|
|
325
|
+
results.push({ error: error?.message ?? String(error) });
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return results;
|
|
329
|
+
});
|
|
330
|
+
self.postMessage({
|
|
331
|
+
type: "register",
|
|
332
|
+
target: "richTextTransform",
|
|
333
|
+
handlerId,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
313
337
|
registerSlot(name, callback = () => null) {
|
|
314
338
|
const handlerId = uuid.create();
|
|
315
339
|
callHandlers.set(handlerId, async (context) => {
|
|
@@ -354,6 +378,69 @@ export class Plugin {
|
|
|
354
378
|
}
|
|
355
379
|
}
|
|
356
380
|
|
|
381
|
+
function serializeTransformTokens(tokens) {
|
|
382
|
+
if (!Array.isArray(tokens)) return tokens;
|
|
383
|
+
return tokens.map((token) => {
|
|
384
|
+
if (
|
|
385
|
+
(token?.type === "inline" || token?.type === "block") &&
|
|
386
|
+
token.node instanceof VirtualEl
|
|
387
|
+
) {
|
|
388
|
+
return { ...token, node: token.node._serialize() };
|
|
389
|
+
}
|
|
390
|
+
return token;
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Concatenates the renderable text of a token stream into a string with a position map,
|
|
395
|
+
// so a transform can pattern-match across token boundaries and map matches back to tokens.
|
|
396
|
+
export class FlattenedTokens {
|
|
397
|
+
constructor(tokens) {
|
|
398
|
+
this._segments = [];
|
|
399
|
+
let text = "";
|
|
400
|
+
for (const token of tokens) {
|
|
401
|
+
let value = null;
|
|
402
|
+
if (token.type === "text") value = token.value;
|
|
403
|
+
else if (token.type === "facet") value = token.text;
|
|
404
|
+
const start = text.length;
|
|
405
|
+
if (value != null) text += value;
|
|
406
|
+
this._segments.push({ token, start, end: text.length });
|
|
407
|
+
}
|
|
408
|
+
this.text = text;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// emit an inert range (no facets)
|
|
412
|
+
textFor(start, end) {
|
|
413
|
+
return this.text.slice(start, end);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// emit a live range, demoting partial facet tokens to text tokens
|
|
417
|
+
tokensFor(start, end) {
|
|
418
|
+
const out = [];
|
|
419
|
+
for (const segment of this._segments) {
|
|
420
|
+
if (segment.start === segment.end) {
|
|
421
|
+
if (segment.start >= start && segment.start < end) {
|
|
422
|
+
out.push(segment.token);
|
|
423
|
+
}
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (segment.end <= start || segment.start >= end) continue;
|
|
427
|
+
const from = Math.max(start, segment.start);
|
|
428
|
+
const to = Math.min(end, segment.end);
|
|
429
|
+
const isWholeToken = from === segment.start && to === segment.end;
|
|
430
|
+
if (isWholeToken) {
|
|
431
|
+
out.push(segment.token);
|
|
432
|
+
} else {
|
|
433
|
+
out.push({ type: "text", value: this.text.slice(from, to) });
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return out;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function flattenForScan(tokens) {
|
|
441
|
+
return new FlattenedTokens(tokens);
|
|
442
|
+
}
|
|
443
|
+
|
|
357
444
|
const openModals = new Map();
|
|
358
445
|
|
|
359
446
|
export class Modal {
|