@pixerate/editor 0.13.0 → 0.14.0

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/dist/index.cjs CHANGED
@@ -66,6 +66,8 @@ __export(src_exports, {
66
66
  SmilieReplacer: () => SmilieReplacer,
67
67
  SpreadsheetController: () => SpreadsheetController,
68
68
  StreamingLayoutManager: () => StreamingLayoutManager,
69
+ TEMPLATE_DEPTH_ERROR: () => TEMPLATE_DEPTH_ERROR,
70
+ TEMPLATE_LOOP_ERROR: () => TEMPLATE_LOOP_ERROR,
69
71
  TEMPLATE_REGEX: () => TEMPLATE_REGEX,
70
72
  TemplateSuggestions: () => TemplateSuggestions,
71
73
  VARIABLE_REGEX: () => VARIABLE_REGEX,
@@ -111,6 +113,7 @@ __export(src_exports, {
111
113
  getEditorText: () => getEditorText,
112
114
  getLatestVersion: () => getLatestVersion,
113
115
  getLayoutedNodes: () => getLayoutedNodes,
116
+ getTemplateBody: () => getTemplateBody,
114
117
  getViewportMetrics: () => getViewportMetrics,
115
118
  globalImageCache: () => globalImageCache,
116
119
  handleImageInsertion: () => handleImageInsertion,
@@ -280,6 +283,18 @@ function getLatestVersion(template) {
280
283
  }
281
284
  return template.versions[template.versions.length - 1];
282
285
  }
286
+ var TEMPLATE_LOOP_ERROR = "[Template loop detected]";
287
+ var TEMPLATE_DEPTH_ERROR = "[Template max depth exceeded]";
288
+ function getTemplateBody(template, bindings) {
289
+ const boundVersionId = bindings?.[template.name];
290
+ if (boundVersionId && template.versions) {
291
+ const boundVersion = template.versions.find(
292
+ (v) => v.id === boundVersionId || v.version === boundVersionId
293
+ );
294
+ return boundVersion?.body;
295
+ }
296
+ return getLatestVersion(template)?.body ?? template.body;
297
+ }
283
298
  function resolveTemplates(text, templates = [], options) {
284
299
  const templateMap = /* @__PURE__ */ new Map();
285
300
  for (const t of templates) {
@@ -287,50 +302,27 @@ function resolveTemplates(text, templates = [], options) {
287
302
  }
288
303
  const maxDepth = options?.maxDepth ?? 10;
289
304
  function resolveRecursive(currentText, visited, depth) {
290
- if (depth > maxDepth) {
291
- return "[Template max depth exceeded]";
292
- }
293
- let result = currentText;
294
305
  const regex = new RegExp(TEMPLATE_REGEX.source, TEMPLATE_REGEX.flags);
295
- let match;
296
- while ((match = regex.exec(result)) !== null) {
297
- const [fullMatch, rawTemplateName] = match;
306
+ return currentText.replace(regex, (fullMatch, rawTemplateName) => {
298
307
  const templateName = rawTemplateName.trim();
299
308
  if (visited.has(templateName)) {
300
- return "[Template loop detected]";
309
+ return TEMPLATE_LOOP_ERROR;
301
310
  }
302
311
  const template = templateMap.get(templateName);
303
312
  if (!template) {
304
- continue;
305
- }
306
- let versionBody;
307
- const boundVersionId = options?.bindings?.[templateName];
308
- if (boundVersionId && template.versions) {
309
- const boundVersion = template.versions.find(
310
- (v) => v.id === boundVersionId || v.version === boundVersionId
311
- );
312
- versionBody = boundVersion?.body;
313
- } else {
314
- const latest = getLatestVersion(template);
315
- versionBody = latest?.body ?? template.body;
313
+ return fullMatch;
316
314
  }
315
+ const versionBody = getTemplateBody(template, options?.bindings);
317
316
  if (versionBody === void 0) {
318
- continue;
317
+ return fullMatch;
318
+ }
319
+ if (depth + 1 > maxDepth) {
320
+ return TEMPLATE_DEPTH_ERROR;
319
321
  }
320
322
  const nextVisited = new Set(visited);
321
323
  nextVisited.add(templateName);
322
- const resolvedBody = resolveRecursive(
323
- versionBody,
324
- nextVisited,
325
- depth + 1
326
- );
327
- if (resolvedBody === "[Template loop detected]" || resolvedBody === "[Template max depth exceeded]") {
328
- return resolvedBody;
329
- }
330
- result = result.replace(fullMatch, resolvedBody);
331
- regex.lastIndex = 0;
332
- }
333
- return result;
324
+ return resolveRecursive(versionBody, nextVisited, depth + 1);
325
+ });
334
326
  }
335
327
  return resolveRecursive(text, /* @__PURE__ */ new Set(), 0);
336
328
  }
@@ -379,7 +371,7 @@ function extractInstructionNames(text) {
379
371
  }
380
372
 
381
373
  // src/grammar/sourcemap.ts
382
- function resolveTemplatesWithMapping(text, templates = [], templateBindings) {
374
+ function resolveTemplatesWithMapping(text, templates = [], templateBindings, options) {
383
375
  if (!templates || templates.length === 0 || !text.includes("{{")) {
384
376
  return {
385
377
  text,
@@ -395,7 +387,7 @@ function resolveTemplatesWithMapping(text, templates = [], templateBindings) {
395
387
  };
396
388
  }
397
389
  const templateMap = new Map(templates.map((t) => [t.name, t]));
398
- const regex = /{{\s*([^}]+?)\s*}}/g;
390
+ const regex = new RegExp(TEMPLATE_REGEX.source, TEMPLATE_REGEX.flags);
399
391
  let match;
400
392
  let lastIndex = 0;
401
393
  let resolvedText = "";
@@ -417,25 +409,13 @@ function resolveTemplatesWithMapping(text, templates = [], templateBindings) {
417
409
  }
418
410
  let resolvedBody = fullMatch;
419
411
  let isTemplate = false;
420
- if (templateMap.has(templateName)) {
421
- const template = templateMap.get(templateName);
422
- let versionBody;
423
- const boundVersionId = templateBindings?.[templateName];
424
- if (boundVersionId && template.versions) {
425
- const boundVersion = template.versions.find(
426
- (v) => v.id === boundVersionId || v.version === boundVersionId
427
- );
428
- versionBody = boundVersion?.body;
429
- } else {
430
- const latestVersion = getLatestVersion(template);
431
- versionBody = latestVersion?.body ?? template.body;
432
- }
433
- if (versionBody !== void 0) {
434
- resolvedBody = resolveTemplates(versionBody, templates, {
435
- bindings: templateBindings
436
- });
437
- isTemplate = true;
438
- }
412
+ const template = templateMap.get(templateName);
413
+ if (template && getTemplateBody(template, templateBindings) !== void 0) {
414
+ resolvedBody = resolveTemplates(fullMatch, templates, {
415
+ bindings: templateBindings,
416
+ maxDepth: options?.maxDepth
417
+ });
418
+ isTemplate = true;
439
419
  }
440
420
  if (isTemplate) {
441
421
  map.push({
@@ -480,9 +460,11 @@ function mapResolvedOffsetToRaw(resolvedOffset, map) {
480
460
  if (inRange) {
481
461
  if (seg.source === "raw") {
482
462
  return seg.rawStart + (resolvedOffset - seg.resolvedStart);
483
- } else {
484
- return seg.rawStart;
485
463
  }
464
+ if (resolvedOffset === seg.resolvedEnd) {
465
+ return seg.rawEnd;
466
+ }
467
+ return seg.rawStart;
486
468
  }
487
469
  }
488
470
  return resolvedOffset;
@@ -495,9 +477,11 @@ function mapRawOffsetToResolved(rawOffset, map) {
495
477
  if (inRange) {
496
478
  if (seg.source === "raw") {
497
479
  return seg.resolvedStart + (rawOffset - seg.rawStart);
498
- } else {
499
- return seg.resolvedStart;
500
480
  }
481
+ if (rawOffset === seg.rawEnd) {
482
+ return seg.resolvedEnd;
483
+ }
484
+ return seg.resolvedStart;
501
485
  }
502
486
  }
503
487
  return rawOffset;
@@ -7025,6 +7009,8 @@ function hitTestAnnotation(imgPt, ann, padding = 10) {
7025
7009
  SmilieReplacer,
7026
7010
  SpreadsheetController,
7027
7011
  StreamingLayoutManager,
7012
+ TEMPLATE_DEPTH_ERROR,
7013
+ TEMPLATE_LOOP_ERROR,
7028
7014
  TEMPLATE_REGEX,
7029
7015
  TemplateSuggestions,
7030
7016
  VARIABLE_REGEX,
@@ -7070,6 +7056,7 @@ function hitTestAnnotation(imgPt, ann, padding = 10) {
7070
7056
  getEditorText,
7071
7057
  getLatestVersion,
7072
7058
  getLayoutedNodes,
7059
+ getTemplateBody,
7073
7060
  getViewportMetrics,
7074
7061
  globalImageCache,
7075
7062
  handleImageInsertion,