@pdfme/schemas 6.0.6-dev.14 → 6.0.6-dev.17

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.js CHANGED
@@ -1,6 +1,6 @@
1
- import { C as DEFAULT_FONT_COLOR, O as VERTICAL_ALIGN_MIDDLE, a as getColumnStylesPropPanelSchema, c as createSingleTable, i as getCellPropPanelSchema, n as getBody, o as getDefaultCellStyles, p as getFontKitFont, r as getBodyWithRange, s as HEX_COLOR_PATTERN, t as getDynamicHeightsForTable, x as DEFAULT_ALIGNMENT } from "./dynamicTemplate-DxHU9waC.js";
1
+ import { B as VERTICAL_ALIGN_MIDDLE, C as DEFAULT_ALIGNMENT, F as SYNTHETIC_BOLD_CSS_TEXT_SHADOW, T as DEFAULT_FONT_COLOR, a as getColumnStylesPropPanelSchema, c as createSingleTable, i as getCellPropPanelSchema, n as getBody, o as getDefaultCellStyles, p as getFontKitFont, r as getBodyWithRange, s as HEX_COLOR_PATTERN, t as getDynamicHeightsForTable, x as CODE_BACKGROUND_COLOR } from "./dynamicTemplate-Dsrw11aL.js";
2
+ import { a as mapVerticalAlignToFlex, c as Formatter, d as isInlineMarkdownTextSchema, f as resolveFontVariant, i as makeElementPlainTextContentEditable, l as getExtraFormatterSchema, m as parseInlineMarkdown, n as textSchema, o as uiRender$3, p as escapeInlineMarkdown, r as buildStyledTextContainer, s as propPanel$2, t as builtInPlugins, u as pdfRender$3 } from "./builtins-C0BvXHWr.js";
2
3
  import { addAlphaToHex, convertForPdfLayoutProps, createErrorElm, createSvgStr, hex2PrintingColor, isEditable, readFile, rotatePoint } from "./utils.js";
3
- import { a as mapVerticalAlignToFlex, c as Formatter, i as makeElementPlainTextContentEditable, l as getExtraFormatterSchema, n as textSchema, o as uiRender$3, r as buildStyledTextContainer, s as propPanel$2, t as builtInPlugins, u as pdfRender$3 } from "./builtins-KUsYW25j.js";
4
4
  import "./tables.js";
5
5
  import { DEFAULT_FONT_NAME, ZOOM, b64toUint8Array, getDefaultFont, getFallbackFontName, mm2pt, px2mm } from "@pdfme/common";
6
6
  import { Buffer as Buffer$1 } from "buffer";
@@ -35,7 +35,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
35
35
  }) : target, mod));
36
36
  //#endregion
37
37
  //#region src/multiVariableText/helper.ts
38
- var substituteVariables = (text, variablesIn) => {
38
+ var substituteVariables = (text, variablesIn, valueMapper = (value) => value) => {
39
39
  if (!text) return "";
40
40
  let substitutedText = text;
41
41
  if (variablesIn) {
@@ -48,12 +48,13 @@ var substituteVariables = (text, variablesIn) => {
48
48
  Object.keys(variables).forEach((variableName) => {
49
49
  const variableForRegex = variableName.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&");
50
50
  const regex = new RegExp("\\{" + variableForRegex + "\\}", "g");
51
- substitutedText = substitutedText.replace(regex, variables[variableName]);
51
+ substitutedText = substitutedText.replace(regex, valueMapper(variables[variableName], variableName));
52
52
  });
53
53
  }
54
54
  substitutedText = substitutedText.replace(/{[^{}]+}/g, "");
55
55
  return substitutedText;
56
56
  };
57
+ var substituteVariablesAsInlineMarkdownLiterals = (text, variablesIn) => substituteVariables(text, variablesIn, escapeInlineMarkdown);
57
58
  var validateVariables = (value, schema) => {
58
59
  if (schema.variables.length === 0) return true;
59
60
  let values;
@@ -74,7 +75,7 @@ var pdfRender$2 = async (arg) => {
74
75
  const { value, schema, ...rest } = arg;
75
76
  if (!validateVariables(value, schema)) return;
76
77
  await pdfRender$3({
77
- value: substituteVariables(schema.text || "", value),
78
+ value: isInlineMarkdownTextSchema(schema) ? substituteVariablesAsInlineMarkdownLiterals(schema.text || "", value) : substituteVariables(schema.text || "", value),
78
79
  schema,
79
80
  ...rest
80
81
  });
@@ -100,13 +101,6 @@ var visitVariables = (content, visitor) => {
100
101
  }
101
102
  }
102
103
  };
103
- var getVariableIndices = (content) => {
104
- const indices = /* @__PURE__ */ new Map();
105
- visitVariables(content, ({ name, startIndex }) => {
106
- indices.set(startIndex, name);
107
- });
108
- return indices;
109
- };
110
104
  var countUniqueVariableNames = (content) => {
111
105
  const variableNames = /* @__PURE__ */ new Set();
112
106
  visitVariables(content, ({ name }) => {
@@ -193,14 +187,16 @@ var mapDynamicVariables = (props) => {
193
187
  var propPanel$1 = {
194
188
  schema: (propPanelProps) => {
195
189
  if (typeof propPanel$2.schema !== "function") throw new Error("Oops, is text schema no longer a function?");
190
+ const parentSchema = typeof propPanel$2.schema === "function" ? propPanel$2.schema(propPanelProps) : {};
191
+ const i18n = propPanelProps.i18n;
196
192
  return {
197
- ...typeof propPanel$2.schema === "function" ? propPanel$2.schema(propPanelProps) : {},
193
+ ...parentSchema,
198
194
  "-------": {
199
195
  type: "void",
200
196
  widget: "Divider"
201
197
  },
202
198
  dynamicVarContainer: {
203
- title: "Variables Sample Data",
199
+ title: i18n("schemas.mvt.variablesSampleData"),
204
200
  type: "string",
205
201
  widget: "Card",
206
202
  span: 24,
@@ -212,7 +208,7 @@ var propPanel$1 = {
212
208
  span: 24
213
209
  },
214
210
  placeholderDynamicVar: {
215
- title: "Placeholder Dynamic Variable",
211
+ title: i18n("schemas.mvt.placeholderDynamicVariable"),
216
212
  type: "string",
217
213
  format: "textarea",
218
214
  props: {
@@ -274,8 +270,9 @@ var uiRender$2 = async (arg) => {
274
270
  await formUiRender(arg);
275
271
  return;
276
272
  }
273
+ const renderValue = isInlineMarkdownTextSchema(schema) ? substituteVariablesAsInlineMarkdownLiterals(text, value) : substituteVariables(text, value);
277
274
  await uiRender$3({
278
- value: isEditable(mode, schema) ? text : substituteVariables(text, value),
275
+ value: isEditable(mode, schema) ? text : renderValue,
279
276
  schema,
280
277
  mode: mode === "form" ? "viewer" : mode,
281
278
  rootElement,
@@ -314,10 +311,27 @@ var formUiRender = async (arg) => {
314
311
  const parsed = JSON.parse(value);
315
312
  if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) variables = parsed;
316
313
  } catch {}
317
- const variableIndices = getVariableIndices(rawText);
318
314
  const substitutedText = substituteVariables(rawText, variables);
315
+ const inlineMarkdownRuns = isInlineMarkdownTextSchema(schema) ? parseInlineMarkdown(rawText) : void 0;
319
316
  const font = options?.font || getDefaultFont();
320
- const textBlock = buildStyledTextContainer(arg, await getFontKitFont(schema.fontName, font, _cache), substitutedText);
317
+ const textBlock = buildStyledTextContainer(arg, await getFontKitFont(schema.fontName, font, _cache), inlineMarkdownRuns ? getInlineMarkdownFormDisplayText(inlineMarkdownRuns, variables) : substitutedText);
318
+ if (inlineMarkdownRuns) {
319
+ renderInlineMarkdownVariableSpans({
320
+ runs: inlineMarkdownRuns,
321
+ variables,
322
+ textBlock,
323
+ schema,
324
+ font,
325
+ theme,
326
+ onChange,
327
+ stopEditing
328
+ });
329
+ return;
330
+ }
331
+ const variableIndices = /* @__PURE__ */ new Map();
332
+ visitVariables(rawText, ({ name, startIndex }) => {
333
+ variableIndices.set(startIndex, name);
334
+ });
321
335
  let inVarString = false;
322
336
  for (let i = 0; i < rawText.length; i++) {
323
337
  const variableName = variableIndices.get(i);
@@ -349,6 +363,97 @@ var formUiRender = async (arg) => {
349
363
  }
350
364
  }
351
365
  };
366
+ var getInlineMarkdownFormDisplayText = (runs, variables) => runs.map((run) => substituteVariables(run.text, variables)).join("");
367
+ var applyInlineMarkdownStyle = (arg) => {
368
+ const { element, run, schema, font } = arg;
369
+ const resolvedFont = resolveFontVariant(run, schema, font);
370
+ if (resolvedFont.fontName) element.style.fontFamily = `'${resolvedFont.fontName}'`;
371
+ if (resolvedFont.syntheticBold) {
372
+ element.style.fontWeight = "800";
373
+ element.style.textShadow = SYNTHETIC_BOLD_CSS_TEXT_SHADOW;
374
+ }
375
+ if (resolvedFont.syntheticItalic) element.style.fontStyle = "italic";
376
+ if (run.strikethrough) element.style.textDecoration = "line-through";
377
+ if (run.code) {
378
+ element.style.backgroundColor = CODE_BACKGROUND_COLOR;
379
+ element.style.borderRadius = "2px";
380
+ element.style.padding = "0 0.15em";
381
+ if (!schema.fontVariants?.code || !font[schema.fontVariants.code]) element.style.fontFamily = resolvedFont.fontName ? `'${resolvedFont.fontName}', monospace` : "monospace";
382
+ }
383
+ };
384
+ var appendTextSpan = (arg) => {
385
+ const { textBlock, text, run, schema, font } = arg;
386
+ if (!text) return;
387
+ const span = document.createElement("span");
388
+ span.textContent = text;
389
+ applyInlineMarkdownStyle({
390
+ element: span,
391
+ run,
392
+ schema,
393
+ font
394
+ });
395
+ textBlock.appendChild(span);
396
+ };
397
+ var appendVariableSpan = (arg) => {
398
+ const { textBlock, variableName, variables, run, schema, font, theme, onChange, stopEditing } = arg;
399
+ const span = document.createElement("span");
400
+ span.style.outline = `${theme.colorPrimary} dashed 1px`;
401
+ applyInlineMarkdownStyle({
402
+ element: span,
403
+ run,
404
+ schema,
405
+ font
406
+ });
407
+ makeElementPlainTextContentEditable(span);
408
+ span.textContent = variables[variableName] ?? "";
409
+ span.addEventListener("blur", (e) => {
410
+ const newValue = e.target.textContent || "";
411
+ if (newValue !== variables[variableName]) {
412
+ variables[variableName] = newValue;
413
+ if (onChange) onChange({
414
+ key: "content",
415
+ value: JSON.stringify(variables)
416
+ });
417
+ if (stopEditing) stopEditing();
418
+ }
419
+ });
420
+ textBlock.appendChild(span);
421
+ };
422
+ var renderInlineMarkdownVariableSpans = (arg) => {
423
+ const { runs, variables, textBlock, schema, font, theme, onChange, stopEditing } = arg;
424
+ textBlock.innerHTML = "";
425
+ runs.forEach((run) => {
426
+ let lastIndex = 0;
427
+ visitVariables(run.text, ({ name, startIndex, endIndex }) => {
428
+ appendTextSpan({
429
+ textBlock,
430
+ text: run.text.slice(lastIndex, startIndex),
431
+ run,
432
+ schema,
433
+ font
434
+ });
435
+ appendVariableSpan({
436
+ textBlock,
437
+ variableName: name,
438
+ variables,
439
+ run,
440
+ schema,
441
+ font,
442
+ theme,
443
+ onChange,
444
+ stopEditing
445
+ });
446
+ lastIndex = endIndex + 1;
447
+ });
448
+ appendTextSpan({
449
+ textBlock,
450
+ text: run.text.slice(lastIndex),
451
+ run,
452
+ schema,
453
+ font
454
+ });
455
+ });
456
+ };
352
457
  /**
353
458
  * An optimisation to try to minimise jank while typing.
354
459
  * Only check whether variables were modified based on certain key presses.