@uniformdev/canvas 19.35.1 → 19.35.3-alpha.82

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
@@ -277,7 +277,13 @@ var require_retry2 = __commonJS({
277
277
  // src/index.ts
278
278
  var src_exports = {};
279
279
  __export(src_exports, {
280
- ApiClientError: () => import_api6.ApiClientError,
280
+ ATTRIBUTE_COMPONENT_ID: () => ATTRIBUTE_COMPONENT_ID,
281
+ ATTRIBUTE_MULTILINE: () => ATTRIBUTE_MULTILINE,
282
+ ATTRIBUTE_PARAMETER_ID: () => ATTRIBUTE_PARAMETER_ID,
283
+ ATTRIBUTE_PARAMETER_TYPE: () => ATTRIBUTE_PARAMETER_TYPE,
284
+ ATTRIBUTE_PARAMETER_VALUE: () => ATTRIBUTE_PARAMETER_VALUE,
285
+ ATTRIBUTE_PLACEHOLDER: () => ATTRIBUTE_PLACEHOLDER,
286
+ ApiClientError: () => import_api7.ApiClientError,
281
287
  BatchEntry: () => BatchEntry,
282
288
  CANVAS_DRAFT_STATE: () => CANVAS_DRAFT_STATE,
283
289
  CANVAS_ENRICHMENT_TAG_PARAM: () => CANVAS_ENRICHMENT_TAG_PARAM,
@@ -294,6 +300,7 @@ __export(src_exports, {
294
300
  CANVAS_TEST_VARIANT_PARAM: () => CANVAS_TEST_VARIANT_PARAM,
295
301
  CanvasClient: () => CanvasClient,
296
302
  CanvasClientError: () => CanvasClientError,
303
+ CategoryClient: () => CategoryClient,
297
304
  ChildEnhancerBuilder: () => ChildEnhancerBuilder,
298
305
  DataSourceClient: () => DataSourceClient,
299
306
  DataTypeClient: () => DataTypeClient,
@@ -314,6 +321,7 @@ __export(src_exports, {
314
321
  PLACEHOLDER_ID: () => PLACEHOLDER_ID,
315
322
  RouteClient: () => RouteClient,
316
323
  UncachedCanvasClient: () => UncachedCanvasClient,
324
+ UncachedCategoryClient: () => UncachedCategoryClient,
317
325
  UniqueBatchEntries: () => UniqueBatchEntries,
318
326
  bindVariables: () => bindVariables,
319
327
  bindVariablesToObject: () => bindVariablesToObject,
@@ -323,12 +331,14 @@ __export(src_exports, {
323
331
  createEventBus: () => createEventBus,
324
332
  createLimitPolicy: () => createLimitPolicy,
325
333
  createUniformApiEnhancer: () => createUniformApiEnhancer,
334
+ createVariableReference: () => createVariableReference,
326
335
  enhance: () => enhance,
327
336
  extractLocales: () => extractLocales,
328
337
  generateHash: () => generateHash,
329
338
  getChannelName: () => getChannelName,
330
339
  getComponentJsonPointer: () => getComponentJsonPointer,
331
340
  getComponentPath: () => getComponentPath,
341
+ getParameterAttributes: () => getParameterAttributes,
332
342
  isAddComponentMessage: () => isAddComponentMessage,
333
343
  isDismissPlaceholderMessage: () => isDismissPlaceholderMessage,
334
344
  isMovingComponentMessage: () => isMovingComponentMessage,
@@ -347,6 +357,7 @@ __export(src_exports, {
347
357
  mapSlotToPersonalizedVariations: () => mapSlotToPersonalizedVariations,
348
358
  mapSlotToTestVariations: () => mapSlotToTestVariations,
349
359
  nullLimitPolicy: () => nullLimitPolicy,
360
+ parseVariableExpression: () => parseVariableExpression,
350
361
  subscribeToComposition: () => subscribeToComposition,
351
362
  unstable_CompositionRelationshipClient: () => unstable_CompositionRelationshipClient,
352
363
  walkComponentTree: () => walkComponentTree
@@ -642,10 +653,53 @@ var UncachedCanvasClient = class extends CanvasClient {
642
653
  }
643
654
  };
644
655
 
645
- // src/CompositionRelationshipClient.ts
656
+ // src/CategoryClient.ts
646
657
  var import_api2 = require("@uniformdev/context/api");
658
+ var CATEGORIES_URL = "/api/v1/categories";
659
+ var CategoryClient = class extends import_api2.ApiClient {
660
+ constructor(options) {
661
+ if (!options.limitPolicy) {
662
+ options.limitPolicy = createLimitPolicy({});
663
+ }
664
+ super(options);
665
+ }
666
+ /** Fetches all categories created in given project */
667
+ async getCategories(options) {
668
+ const { projectId } = this.options;
669
+ const fetchUri = this.createUrl("/api/v1/categories", { ...options, projectId });
670
+ return await this.apiClient(fetchUri);
671
+ }
672
+ /** Updates or creates a category, also used to re-order them */
673
+ async upsertCategories(categories) {
674
+ const { projectId } = this.options;
675
+ const fetchUri = this.createUrl(CATEGORIES_URL);
676
+ return await this.apiClient(fetchUri, {
677
+ method: "PUT",
678
+ body: JSON.stringify({ categories, projectId }),
679
+ expectNoContent: true
680
+ });
681
+ }
682
+ /** Deletes a category */
683
+ async removeCategory(options) {
684
+ const { projectId } = this.options;
685
+ const fetchUri = this.createUrl(CATEGORIES_URL);
686
+ return await this.apiClient(fetchUri, {
687
+ method: "DELETE",
688
+ body: JSON.stringify({ projectId, categoryId: options.categoryId }),
689
+ expectNoContent: true
690
+ });
691
+ }
692
+ };
693
+ var UncachedCategoryClient = class extends CategoryClient {
694
+ constructor(options) {
695
+ super({ ...options, bypassCache: true });
696
+ }
697
+ };
698
+
699
+ // src/CompositionRelationshipClient.ts
700
+ var import_api3 = require("@uniformdev/context/api");
647
701
  var COMPOSITION_RELATIONSHIP_URL = "/api/v1/composition-relationships";
648
- var unstable_CompositionRelationshipClient = class extends import_api2.ApiClient {
702
+ var unstable_CompositionRelationshipClient = class extends import_api3.ApiClient {
649
703
  constructor(options) {
650
704
  super(options);
651
705
  this.getDefinitionsRelationships = async ({
@@ -706,10 +760,10 @@ var unstable_CompositionRelationshipClient = class extends import_api2.ApiClient
706
760
  };
707
761
 
708
762
  // src/DataSourceClient.ts
709
- var import_api3 = require("@uniformdev/context/api");
763
+ var import_api4 = require("@uniformdev/context/api");
710
764
  var dataSourceUrl = "/api/v1/data-source";
711
765
  var dataSourcesUrl = "/api/v1/data-sources";
712
- var DataSourceClient = class extends import_api3.ApiClient {
766
+ var DataSourceClient = class extends import_api4.ApiClient {
713
767
  constructor(options) {
714
768
  super(options);
715
769
  }
@@ -746,9 +800,9 @@ var DataSourceClient = class extends import_api3.ApiClient {
746
800
  };
747
801
 
748
802
  // src/DataTypeClient.ts
749
- var import_api4 = require("@uniformdev/context/api");
803
+ var import_api5 = require("@uniformdev/context/api");
750
804
  var _url;
751
- var _DataTypeClient = class _DataTypeClient extends import_api4.ApiClient {
805
+ var _DataTypeClient = class _DataTypeClient extends import_api5.ApiClient {
752
806
  constructor(options) {
753
807
  super(options);
754
808
  }
@@ -1665,9 +1719,9 @@ function subscribeToComposition({
1665
1719
  }
1666
1720
 
1667
1721
  // src/RouteClient.ts
1668
- var import_api5 = require("@uniformdev/context/api");
1722
+ var import_api6 = require("@uniformdev/context/api");
1669
1723
  var ROUTE_URL = "/api/v1/route";
1670
- var RouteClient = class extends import_api5.ApiClient {
1724
+ var RouteClient = class extends import_api6.ApiClient {
1671
1725
  constructor(options) {
1672
1726
  var _a;
1673
1727
  if (!options.limitPolicy) {
@@ -1684,7 +1738,157 @@ var RouteClient = class extends import_api5.ApiClient {
1684
1738
  }
1685
1739
  };
1686
1740
 
1687
- // src/utils/bindVariables.ts
1741
+ // src/utils/createApiEnhancer.ts
1742
+ var createUniformApiEnhancer = ({ apiUrl }) => {
1743
+ return async (message) => {
1744
+ const response = await fetch(apiUrl, {
1745
+ method: "post",
1746
+ body: JSON.stringify({
1747
+ composition: message.composition,
1748
+ hash: message.hash
1749
+ }),
1750
+ headers: {
1751
+ "Content-Type": "application/json"
1752
+ }
1753
+ });
1754
+ const json = await response.json();
1755
+ if (!response.ok) {
1756
+ throw new Error("Error reading enhanced composition");
1757
+ }
1758
+ const body = json;
1759
+ return body.composition;
1760
+ };
1761
+ };
1762
+
1763
+ // src/utils/getParameterAttributes.ts
1764
+ var ATTRIBUTE_COMPONENT_ID = "data-uniform-component-id";
1765
+ var ATTRIBUTE_PARAMETER_ID = "data-uniform-parameter-id";
1766
+ var ATTRIBUTE_PARAMETER_VALUE = "data-uniform-parameter-value";
1767
+ var ATTRIBUTE_PARAMETER_TYPE = "data-uniform-parameter-type";
1768
+ var ATTRIBUTE_PLACEHOLDER = "data-uniform-placeholder";
1769
+ var ATTRIBUTE_MULTILINE = "data-uniform-is-multiline";
1770
+ var getParameterAttributes = ({
1771
+ id,
1772
+ component,
1773
+ placeholder,
1774
+ isMultiline = false
1775
+ }) => {
1776
+ var _a, _b, _c;
1777
+ const componentId = component == null ? void 0 : component._id;
1778
+ const parameter = (_a = component == null ? void 0 : component.parameters) == null ? void 0 : _a[id];
1779
+ const value = parameter == null ? void 0 : parameter.value;
1780
+ const type = parameter == null ? void 0 : parameter.type;
1781
+ const isEditable = (_c = (_b = parameter == null ? void 0 : parameter._contextualEditing) == null ? void 0 : _b.isEditable) != null ? _c : false;
1782
+ return {
1783
+ [ATTRIBUTE_COMPONENT_ID]: componentId,
1784
+ [ATTRIBUTE_PARAMETER_ID]: id,
1785
+ [ATTRIBUTE_PARAMETER_VALUE]: String(value != null ? value : ""),
1786
+ [ATTRIBUTE_PARAMETER_TYPE]: type,
1787
+ [ATTRIBUTE_PLACEHOLDER]: placeholder,
1788
+ [ATTRIBUTE_MULTILINE]: type === "text" ? isMultiline : void 0,
1789
+ contentEditable: type === "text" ? isEditable : void 0
1790
+ };
1791
+ };
1792
+
1793
+ // src/utils/isSystemComponentDefinition.ts
1794
+ var isSystemComponentDefinition = (componentType) => {
1795
+ return componentType.startsWith("$");
1796
+ };
1797
+
1798
+ // src/utils/mapSlotToPersonalizedVariations.ts
1799
+ function mapSlotToPersonalizedVariations(slot) {
1800
+ if (!slot)
1801
+ return [];
1802
+ return slot.map((v, i) => {
1803
+ var _a, _b;
1804
+ const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_PERSONALIZATION_PARAM]) == null ? void 0 : _b.value;
1805
+ const id = (contextTag == null ? void 0 : contextTag.name) || `pz-${i}-${v.type}`;
1806
+ return {
1807
+ ...v,
1808
+ id,
1809
+ pz: contextTag
1810
+ };
1811
+ });
1812
+ }
1813
+
1814
+ // src/utils/mapSlotToTestVariations.ts
1815
+ function mapSlotToTestVariations(slot) {
1816
+ if (!slot)
1817
+ return [];
1818
+ return slot.map((v, i) => {
1819
+ var _a, _b, _c;
1820
+ const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_TEST_VARIANT_PARAM]) == null ? void 0 : _b.value;
1821
+ const id = (_c = contextTag == null ? void 0 : contextTag.id) != null ? _c : "testId" in v ? v.testId : `ab-${i}-${v.type}`;
1822
+ return {
1823
+ ...v,
1824
+ id
1825
+ //testDistribution: contextTag?.testDistribution,
1826
+ };
1827
+ });
1828
+ }
1829
+
1830
+ // src/utils/variables/parseVariableExpression.ts
1831
+ var escapeCharacter = "\\";
1832
+ var variablePrefix = "${";
1833
+ var variableSuffix = "}";
1834
+ function parseVariableExpression(serialized, onToken) {
1835
+ let bufferStartIndex = 0;
1836
+ let bufferEndIndex = 0;
1837
+ let tokenCount = 0;
1838
+ const handleToken = (token, type) => {
1839
+ tokenCount++;
1840
+ return onToken == null ? void 0 : onToken(token, type);
1841
+ };
1842
+ let state = "text";
1843
+ for (let index = 0; index < serialized.length; index++) {
1844
+ const char = serialized[index];
1845
+ if (bufferStartIndex > bufferEndIndex) {
1846
+ bufferEndIndex = bufferStartIndex;
1847
+ }
1848
+ if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
1849
+ if (serialized[index - 1] === escapeCharacter) {
1850
+ bufferEndIndex -= escapeCharacter.length;
1851
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1852
+ return tokenCount;
1853
+ }
1854
+ bufferStartIndex = index;
1855
+ bufferEndIndex = index + 1;
1856
+ continue;
1857
+ }
1858
+ state = "variable";
1859
+ if (bufferEndIndex > bufferStartIndex) {
1860
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1861
+ return tokenCount;
1862
+ }
1863
+ bufferStartIndex = bufferEndIndex;
1864
+ }
1865
+ index += variablePrefix.length - 1;
1866
+ bufferStartIndex += variablePrefix.length;
1867
+ continue;
1868
+ }
1869
+ if (char === variableSuffix && state === "variable") {
1870
+ state = "text";
1871
+ if (bufferEndIndex > bufferStartIndex) {
1872
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
1873
+ return tokenCount;
1874
+ }
1875
+ bufferStartIndex = bufferEndIndex + variableSuffix.length;
1876
+ }
1877
+ continue;
1878
+ }
1879
+ bufferEndIndex++;
1880
+ }
1881
+ if (bufferEndIndex > bufferStartIndex) {
1882
+ if (state === "variable") {
1883
+ state = "text";
1884
+ bufferStartIndex -= variablePrefix.length;
1885
+ }
1886
+ handleToken(serialized.substring(bufferStartIndex), state);
1887
+ }
1888
+ return tokenCount;
1889
+ }
1890
+
1891
+ // src/utils/variables/bindVariables.ts
1688
1892
  function bindVariables({
1689
1893
  variables,
1690
1894
  value,
@@ -1692,6 +1896,7 @@ function bindVariables({
1692
1896
  handleBinding
1693
1897
  }) {
1694
1898
  let boundCount = 0;
1899
+ let tokenCount = 0;
1695
1900
  const errors = [];
1696
1901
  const defaultHandleBinding = (variableName, variables2, errors2) => {
1697
1902
  const variableValue = variables2[variableName];
@@ -1701,16 +1906,33 @@ function bindVariables({
1701
1906
  }
1702
1907
  return variableValue;
1703
1908
  };
1704
- const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
1705
- const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
1909
+ const result = [];
1910
+ parseVariableExpression(value, (token, tokenType) => {
1911
+ tokenCount++;
1912
+ if (tokenType === "text") {
1913
+ result.push(token);
1914
+ return;
1915
+ }
1916
+ const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
1706
1917
  boundCount++;
1707
- return variableValue;
1918
+ result.push(variableValue);
1708
1919
  });
1709
- return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
1920
+ return {
1921
+ result: tokenCount === 1 ? result[0] : result.join(""),
1922
+ boundCount,
1923
+ errors: errors.length > 0 ? errors : void 0
1924
+ };
1710
1925
  }
1711
1926
 
1712
- // src/utils/bindVariablesToObject.ts
1927
+ // src/utils/variables/bindVariablesToObject.ts
1713
1928
  var import_immer = require("immer");
1929
+
1930
+ // src/utils/variables/createVariableReference.ts
1931
+ function createVariableReference(variableName) {
1932
+ return `\${${variableName}}`;
1933
+ }
1934
+
1935
+ // src/utils/variables/bindVariablesToObject.ts
1714
1936
  function bindVariablesToObject(options) {
1715
1937
  return bindVariablesToObjectRecursive(options);
1716
1938
  }
@@ -1727,6 +1949,10 @@ function bindVariablesToObjectRecursive({
1727
1949
  if (typeof value !== "object" || value === null) {
1728
1950
  return { boundCount: 0, result: value };
1729
1951
  }
1952
+ const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
1953
+ if (richTextNodeResult !== void 0) {
1954
+ return richTextNodeResult;
1955
+ }
1730
1956
  const result = (0, import_immer.produce)(value, (draft) => {
1731
1957
  Object.entries(draft).forEach(([property, oldValue]) => {
1732
1958
  const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
@@ -1750,78 +1976,42 @@ function bindVariablesToObjectRecursive({
1750
1976
  boundCount += childBind.boundCount;
1751
1977
  draft[property] = childBind.result;
1752
1978
  if (childBind.errors) {
1753
- errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
1979
+ errors.push(...childBind.errors);
1754
1980
  }
1755
1981
  }
1756
1982
  });
1757
1983
  });
1758
1984
  return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1759
1985
  }
1760
-
1761
- // src/utils/createApiEnhancer.ts
1762
- var createUniformApiEnhancer = ({ apiUrl }) => {
1763
- return async (message) => {
1764
- const response = await fetch(apiUrl, {
1765
- method: "post",
1766
- body: JSON.stringify({
1767
- composition: message.composition,
1768
- hash: message.hash
1769
- }),
1770
- headers: {
1771
- "Content-Type": "application/json"
1772
- }
1773
- });
1774
- const json = await response.json();
1775
- if (!response.ok) {
1776
- throw new Error("Error reading enhanced composition");
1777
- }
1778
- const body = json;
1779
- return body.composition;
1780
- };
1781
- };
1782
-
1783
- // src/utils/isSystemComponentDefinition.ts
1784
- var isSystemComponentDefinition = (componentType) => {
1785
- return componentType.startsWith("$");
1786
- };
1787
-
1788
- // src/utils/mapSlotToPersonalizedVariations.ts
1789
- function mapSlotToPersonalizedVariations(slot) {
1790
- if (!slot)
1791
- return [];
1792
- return slot.map((v, i) => {
1793
- var _a, _b;
1794
- const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_PERSONALIZATION_PARAM]) == null ? void 0 : _b.value;
1795
- const id = (contextTag == null ? void 0 : contextTag.name) || `pz-${i}-${v.type}`;
1796
- return {
1797
- ...v,
1798
- id,
1799
- pz: contextTag
1800
- };
1801
- });
1802
- }
1803
-
1804
- // src/utils/mapSlotToTestVariations.ts
1805
- function mapSlotToTestVariations(slot) {
1806
- if (!slot)
1807
- return [];
1808
- return slot.map((v, i) => {
1809
- var _a, _b, _c;
1810
- const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_TEST_VARIANT_PARAM]) == null ? void 0 : _b.value;
1811
- const id = (_c = contextTag == null ? void 0 : contextTag.id) != null ? _c : "testId" in v ? v.testId : `ab-${i}-${v.type}`;
1812
- return {
1813
- ...v,
1814
- id
1815
- //testDistribution: contextTag?.testDistribution,
1986
+ function handleRichTextNodeBinding(object, options) {
1987
+ if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
1988
+ const value = createVariableReference(object.reference);
1989
+ const bindResult = bindVariables({ ...options, value });
1990
+ const bindResultAsTextNode = {
1991
+ detail: 0,
1992
+ format: 0,
1993
+ mode: "normal",
1994
+ style: "",
1995
+ text: bindResult.result,
1996
+ type: "text",
1997
+ version: 1
1816
1998
  };
1817
- });
1999
+ return { ...bindResult, result: bindResultAsTextNode };
2000
+ }
2001
+ return void 0;
1818
2002
  }
1819
2003
 
1820
2004
  // src/index.ts
1821
- var import_api6 = require("@uniformdev/context/api");
1822
- var CanvasClientError = import_api6.ApiClientError;
2005
+ var import_api7 = require("@uniformdev/context/api");
2006
+ var CanvasClientError = import_api7.ApiClientError;
1823
2007
  // Annotate the CommonJS export names for ESM import in node:
1824
2008
  0 && (module.exports = {
2009
+ ATTRIBUTE_COMPONENT_ID,
2010
+ ATTRIBUTE_MULTILINE,
2011
+ ATTRIBUTE_PARAMETER_ID,
2012
+ ATTRIBUTE_PARAMETER_TYPE,
2013
+ ATTRIBUTE_PARAMETER_VALUE,
2014
+ ATTRIBUTE_PLACEHOLDER,
1825
2015
  ApiClientError,
1826
2016
  BatchEntry,
1827
2017
  CANVAS_DRAFT_STATE,
@@ -1839,6 +2029,7 @@ var CanvasClientError = import_api6.ApiClientError;
1839
2029
  CANVAS_TEST_VARIANT_PARAM,
1840
2030
  CanvasClient,
1841
2031
  CanvasClientError,
2032
+ CategoryClient,
1842
2033
  ChildEnhancerBuilder,
1843
2034
  DataSourceClient,
1844
2035
  DataTypeClient,
@@ -1859,6 +2050,7 @@ var CanvasClientError = import_api6.ApiClientError;
1859
2050
  PLACEHOLDER_ID,
1860
2051
  RouteClient,
1861
2052
  UncachedCanvasClient,
2053
+ UncachedCategoryClient,
1862
2054
  UniqueBatchEntries,
1863
2055
  bindVariables,
1864
2056
  bindVariablesToObject,
@@ -1868,12 +2060,14 @@ var CanvasClientError = import_api6.ApiClientError;
1868
2060
  createEventBus,
1869
2061
  createLimitPolicy,
1870
2062
  createUniformApiEnhancer,
2063
+ createVariableReference,
1871
2064
  enhance,
1872
2065
  extractLocales,
1873
2066
  generateHash,
1874
2067
  getChannelName,
1875
2068
  getComponentJsonPointer,
1876
2069
  getComponentPath,
2070
+ getParameterAttributes,
1877
2071
  isAddComponentMessage,
1878
2072
  isDismissPlaceholderMessage,
1879
2073
  isMovingComponentMessage,
@@ -1892,6 +2086,7 @@ var CanvasClientError = import_api6.ApiClientError;
1892
2086
  mapSlotToPersonalizedVariations,
1893
2087
  mapSlotToTestVariations,
1894
2088
  nullLimitPolicy,
2089
+ parseVariableExpression,
1895
2090
  subscribeToComposition,
1896
2091
  unstable_CompositionRelationshipClient,
1897
2092
  walkComponentTree