@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.mjs CHANGED
@@ -557,10 +557,53 @@ var UncachedCanvasClient = class extends CanvasClient {
557
557
  }
558
558
  };
559
559
 
560
- // src/CompositionRelationshipClient.ts
560
+ // src/CategoryClient.ts
561
561
  import { ApiClient as ApiClient2 } from "@uniformdev/context/api";
562
+ var CATEGORIES_URL = "/api/v1/categories";
563
+ var CategoryClient = class extends ApiClient2 {
564
+ constructor(options) {
565
+ if (!options.limitPolicy) {
566
+ options.limitPolicy = createLimitPolicy({});
567
+ }
568
+ super(options);
569
+ }
570
+ /** Fetches all categories created in given project */
571
+ async getCategories(options) {
572
+ const { projectId } = this.options;
573
+ const fetchUri = this.createUrl("/api/v1/categories", { ...options, projectId });
574
+ return await this.apiClient(fetchUri);
575
+ }
576
+ /** Updates or creates a category, also used to re-order them */
577
+ async upsertCategories(categories) {
578
+ const { projectId } = this.options;
579
+ const fetchUri = this.createUrl(CATEGORIES_URL);
580
+ return await this.apiClient(fetchUri, {
581
+ method: "PUT",
582
+ body: JSON.stringify({ categories, projectId }),
583
+ expectNoContent: true
584
+ });
585
+ }
586
+ /** Deletes a category */
587
+ async removeCategory(options) {
588
+ const { projectId } = this.options;
589
+ const fetchUri = this.createUrl(CATEGORIES_URL);
590
+ return await this.apiClient(fetchUri, {
591
+ method: "DELETE",
592
+ body: JSON.stringify({ projectId, categoryId: options.categoryId }),
593
+ expectNoContent: true
594
+ });
595
+ }
596
+ };
597
+ var UncachedCategoryClient = class extends CategoryClient {
598
+ constructor(options) {
599
+ super({ ...options, bypassCache: true });
600
+ }
601
+ };
602
+
603
+ // src/CompositionRelationshipClient.ts
604
+ import { ApiClient as ApiClient3 } from "@uniformdev/context/api";
562
605
  var COMPOSITION_RELATIONSHIP_URL = "/api/v1/composition-relationships";
563
- var unstable_CompositionRelationshipClient = class extends ApiClient2 {
606
+ var unstable_CompositionRelationshipClient = class extends ApiClient3 {
564
607
  constructor(options) {
565
608
  super(options);
566
609
  this.getDefinitionsRelationships = async ({
@@ -621,10 +664,10 @@ var unstable_CompositionRelationshipClient = class extends ApiClient2 {
621
664
  };
622
665
 
623
666
  // src/DataSourceClient.ts
624
- import { ApiClient as ApiClient3 } from "@uniformdev/context/api";
667
+ import { ApiClient as ApiClient4 } from "@uniformdev/context/api";
625
668
  var dataSourceUrl = "/api/v1/data-source";
626
669
  var dataSourcesUrl = "/api/v1/data-sources";
627
- var DataSourceClient = class extends ApiClient3 {
670
+ var DataSourceClient = class extends ApiClient4 {
628
671
  constructor(options) {
629
672
  super(options);
630
673
  }
@@ -661,9 +704,9 @@ var DataSourceClient = class extends ApiClient3 {
661
704
  };
662
705
 
663
706
  // src/DataTypeClient.ts
664
- import { ApiClient as ApiClient4 } from "@uniformdev/context/api";
707
+ import { ApiClient as ApiClient5 } from "@uniformdev/context/api";
665
708
  var _url;
666
- var _DataTypeClient = class _DataTypeClient extends ApiClient4 {
709
+ var _DataTypeClient = class _DataTypeClient extends ApiClient5 {
667
710
  constructor(options) {
668
711
  super(options);
669
712
  }
@@ -1580,9 +1623,9 @@ function subscribeToComposition({
1580
1623
  }
1581
1624
 
1582
1625
  // src/RouteClient.ts
1583
- import { ApiClient as ApiClient5 } from "@uniformdev/context/api";
1626
+ import { ApiClient as ApiClient6 } from "@uniformdev/context/api";
1584
1627
  var ROUTE_URL = "/api/v1/route";
1585
- var RouteClient = class extends ApiClient5 {
1628
+ var RouteClient = class extends ApiClient6 {
1586
1629
  constructor(options) {
1587
1630
  var _a;
1588
1631
  if (!options.limitPolicy) {
@@ -1599,7 +1642,157 @@ var RouteClient = class extends ApiClient5 {
1599
1642
  }
1600
1643
  };
1601
1644
 
1602
- // src/utils/bindVariables.ts
1645
+ // src/utils/createApiEnhancer.ts
1646
+ var createUniformApiEnhancer = ({ apiUrl }) => {
1647
+ return async (message) => {
1648
+ const response = await fetch(apiUrl, {
1649
+ method: "post",
1650
+ body: JSON.stringify({
1651
+ composition: message.composition,
1652
+ hash: message.hash
1653
+ }),
1654
+ headers: {
1655
+ "Content-Type": "application/json"
1656
+ }
1657
+ });
1658
+ const json = await response.json();
1659
+ if (!response.ok) {
1660
+ throw new Error("Error reading enhanced composition");
1661
+ }
1662
+ const body = json;
1663
+ return body.composition;
1664
+ };
1665
+ };
1666
+
1667
+ // src/utils/getParameterAttributes.ts
1668
+ var ATTRIBUTE_COMPONENT_ID = "data-uniform-component-id";
1669
+ var ATTRIBUTE_PARAMETER_ID = "data-uniform-parameter-id";
1670
+ var ATTRIBUTE_PARAMETER_VALUE = "data-uniform-parameter-value";
1671
+ var ATTRIBUTE_PARAMETER_TYPE = "data-uniform-parameter-type";
1672
+ var ATTRIBUTE_PLACEHOLDER = "data-uniform-placeholder";
1673
+ var ATTRIBUTE_MULTILINE = "data-uniform-is-multiline";
1674
+ var getParameterAttributes = ({
1675
+ id,
1676
+ component,
1677
+ placeholder,
1678
+ isMultiline = false
1679
+ }) => {
1680
+ var _a, _b, _c;
1681
+ const componentId = component == null ? void 0 : component._id;
1682
+ const parameter = (_a = component == null ? void 0 : component.parameters) == null ? void 0 : _a[id];
1683
+ const value = parameter == null ? void 0 : parameter.value;
1684
+ const type = parameter == null ? void 0 : parameter.type;
1685
+ const isEditable = (_c = (_b = parameter == null ? void 0 : parameter._contextualEditing) == null ? void 0 : _b.isEditable) != null ? _c : false;
1686
+ return {
1687
+ [ATTRIBUTE_COMPONENT_ID]: componentId,
1688
+ [ATTRIBUTE_PARAMETER_ID]: id,
1689
+ [ATTRIBUTE_PARAMETER_VALUE]: String(value != null ? value : ""),
1690
+ [ATTRIBUTE_PARAMETER_TYPE]: type,
1691
+ [ATTRIBUTE_PLACEHOLDER]: placeholder,
1692
+ [ATTRIBUTE_MULTILINE]: type === "text" ? isMultiline : void 0,
1693
+ contentEditable: type === "text" ? isEditable : void 0
1694
+ };
1695
+ };
1696
+
1697
+ // src/utils/isSystemComponentDefinition.ts
1698
+ var isSystemComponentDefinition = (componentType) => {
1699
+ return componentType.startsWith("$");
1700
+ };
1701
+
1702
+ // src/utils/mapSlotToPersonalizedVariations.ts
1703
+ function mapSlotToPersonalizedVariations(slot) {
1704
+ if (!slot)
1705
+ return [];
1706
+ return slot.map((v, i) => {
1707
+ var _a, _b;
1708
+ const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_PERSONALIZATION_PARAM]) == null ? void 0 : _b.value;
1709
+ const id = (contextTag == null ? void 0 : contextTag.name) || `pz-${i}-${v.type}`;
1710
+ return {
1711
+ ...v,
1712
+ id,
1713
+ pz: contextTag
1714
+ };
1715
+ });
1716
+ }
1717
+
1718
+ // src/utils/mapSlotToTestVariations.ts
1719
+ function mapSlotToTestVariations(slot) {
1720
+ if (!slot)
1721
+ return [];
1722
+ return slot.map((v, i) => {
1723
+ var _a, _b, _c;
1724
+ const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_TEST_VARIANT_PARAM]) == null ? void 0 : _b.value;
1725
+ const id = (_c = contextTag == null ? void 0 : contextTag.id) != null ? _c : "testId" in v ? v.testId : `ab-${i}-${v.type}`;
1726
+ return {
1727
+ ...v,
1728
+ id
1729
+ //testDistribution: contextTag?.testDistribution,
1730
+ };
1731
+ });
1732
+ }
1733
+
1734
+ // src/utils/variables/parseVariableExpression.ts
1735
+ var escapeCharacter = "\\";
1736
+ var variablePrefix = "${";
1737
+ var variableSuffix = "}";
1738
+ function parseVariableExpression(serialized, onToken) {
1739
+ let bufferStartIndex = 0;
1740
+ let bufferEndIndex = 0;
1741
+ let tokenCount = 0;
1742
+ const handleToken = (token, type) => {
1743
+ tokenCount++;
1744
+ return onToken == null ? void 0 : onToken(token, type);
1745
+ };
1746
+ let state = "text";
1747
+ for (let index = 0; index < serialized.length; index++) {
1748
+ const char = serialized[index];
1749
+ if (bufferStartIndex > bufferEndIndex) {
1750
+ bufferEndIndex = bufferStartIndex;
1751
+ }
1752
+ if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
1753
+ if (serialized[index - 1] === escapeCharacter) {
1754
+ bufferEndIndex -= escapeCharacter.length;
1755
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1756
+ return tokenCount;
1757
+ }
1758
+ bufferStartIndex = index;
1759
+ bufferEndIndex = index + 1;
1760
+ continue;
1761
+ }
1762
+ state = "variable";
1763
+ if (bufferEndIndex > bufferStartIndex) {
1764
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1765
+ return tokenCount;
1766
+ }
1767
+ bufferStartIndex = bufferEndIndex;
1768
+ }
1769
+ index += variablePrefix.length - 1;
1770
+ bufferStartIndex += variablePrefix.length;
1771
+ continue;
1772
+ }
1773
+ if (char === variableSuffix && state === "variable") {
1774
+ state = "text";
1775
+ if (bufferEndIndex > bufferStartIndex) {
1776
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
1777
+ return tokenCount;
1778
+ }
1779
+ bufferStartIndex = bufferEndIndex + variableSuffix.length;
1780
+ }
1781
+ continue;
1782
+ }
1783
+ bufferEndIndex++;
1784
+ }
1785
+ if (bufferEndIndex > bufferStartIndex) {
1786
+ if (state === "variable") {
1787
+ state = "text";
1788
+ bufferStartIndex -= variablePrefix.length;
1789
+ }
1790
+ handleToken(serialized.substring(bufferStartIndex), state);
1791
+ }
1792
+ return tokenCount;
1793
+ }
1794
+
1795
+ // src/utils/variables/bindVariables.ts
1603
1796
  function bindVariables({
1604
1797
  variables,
1605
1798
  value,
@@ -1607,6 +1800,7 @@ function bindVariables({
1607
1800
  handleBinding
1608
1801
  }) {
1609
1802
  let boundCount = 0;
1803
+ let tokenCount = 0;
1610
1804
  const errors = [];
1611
1805
  const defaultHandleBinding = (variableName, variables2, errors2) => {
1612
1806
  const variableValue = variables2[variableName];
@@ -1616,16 +1810,33 @@ function bindVariables({
1616
1810
  }
1617
1811
  return variableValue;
1618
1812
  };
1619
- const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
1620
- const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
1813
+ const result = [];
1814
+ parseVariableExpression(value, (token, tokenType) => {
1815
+ tokenCount++;
1816
+ if (tokenType === "text") {
1817
+ result.push(token);
1818
+ return;
1819
+ }
1820
+ const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
1621
1821
  boundCount++;
1622
- return variableValue;
1822
+ result.push(variableValue);
1623
1823
  });
1624
- return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
1824
+ return {
1825
+ result: tokenCount === 1 ? result[0] : result.join(""),
1826
+ boundCount,
1827
+ errors: errors.length > 0 ? errors : void 0
1828
+ };
1625
1829
  }
1626
1830
 
1627
- // src/utils/bindVariablesToObject.ts
1831
+ // src/utils/variables/bindVariablesToObject.ts
1628
1832
  import { produce } from "immer";
1833
+
1834
+ // src/utils/variables/createVariableReference.ts
1835
+ function createVariableReference(variableName) {
1836
+ return `\${${variableName}}`;
1837
+ }
1838
+
1839
+ // src/utils/variables/bindVariablesToObject.ts
1629
1840
  function bindVariablesToObject(options) {
1630
1841
  return bindVariablesToObjectRecursive(options);
1631
1842
  }
@@ -1642,6 +1853,10 @@ function bindVariablesToObjectRecursive({
1642
1853
  if (typeof value !== "object" || value === null) {
1643
1854
  return { boundCount: 0, result: value };
1644
1855
  }
1856
+ const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
1857
+ if (richTextNodeResult !== void 0) {
1858
+ return richTextNodeResult;
1859
+ }
1645
1860
  const result = produce(value, (draft) => {
1646
1861
  Object.entries(draft).forEach(([property, oldValue]) => {
1647
1862
  const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
@@ -1665,77 +1880,41 @@ function bindVariablesToObjectRecursive({
1665
1880
  boundCount += childBind.boundCount;
1666
1881
  draft[property] = childBind.result;
1667
1882
  if (childBind.errors) {
1668
- errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
1883
+ errors.push(...childBind.errors);
1669
1884
  }
1670
1885
  }
1671
1886
  });
1672
1887
  });
1673
1888
  return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1674
1889
  }
1675
-
1676
- // src/utils/createApiEnhancer.ts
1677
- var createUniformApiEnhancer = ({ apiUrl }) => {
1678
- return async (message) => {
1679
- const response = await fetch(apiUrl, {
1680
- method: "post",
1681
- body: JSON.stringify({
1682
- composition: message.composition,
1683
- hash: message.hash
1684
- }),
1685
- headers: {
1686
- "Content-Type": "application/json"
1687
- }
1688
- });
1689
- const json = await response.json();
1690
- if (!response.ok) {
1691
- throw new Error("Error reading enhanced composition");
1692
- }
1693
- const body = json;
1694
- return body.composition;
1695
- };
1696
- };
1697
-
1698
- // src/utils/isSystemComponentDefinition.ts
1699
- var isSystemComponentDefinition = (componentType) => {
1700
- return componentType.startsWith("$");
1701
- };
1702
-
1703
- // src/utils/mapSlotToPersonalizedVariations.ts
1704
- function mapSlotToPersonalizedVariations(slot) {
1705
- if (!slot)
1706
- return [];
1707
- return slot.map((v, i) => {
1708
- var _a, _b;
1709
- const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_PERSONALIZATION_PARAM]) == null ? void 0 : _b.value;
1710
- const id = (contextTag == null ? void 0 : contextTag.name) || `pz-${i}-${v.type}`;
1711
- return {
1712
- ...v,
1713
- id,
1714
- pz: contextTag
1715
- };
1716
- });
1717
- }
1718
-
1719
- // src/utils/mapSlotToTestVariations.ts
1720
- function mapSlotToTestVariations(slot) {
1721
- if (!slot)
1722
- return [];
1723
- return slot.map((v, i) => {
1724
- var _a, _b, _c;
1725
- const contextTag = (_b = (_a = v.parameters) == null ? void 0 : _a[CANVAS_TEST_VARIANT_PARAM]) == null ? void 0 : _b.value;
1726
- const id = (_c = contextTag == null ? void 0 : contextTag.id) != null ? _c : "testId" in v ? v.testId : `ab-${i}-${v.type}`;
1727
- return {
1728
- ...v,
1729
- id
1730
- //testDistribution: contextTag?.testDistribution,
1890
+ function handleRichTextNodeBinding(object, options) {
1891
+ if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
1892
+ const value = createVariableReference(object.reference);
1893
+ const bindResult = bindVariables({ ...options, value });
1894
+ const bindResultAsTextNode = {
1895
+ detail: 0,
1896
+ format: 0,
1897
+ mode: "normal",
1898
+ style: "",
1899
+ text: bindResult.result,
1900
+ type: "text",
1901
+ version: 1
1731
1902
  };
1732
- });
1903
+ return { ...bindResult, result: bindResultAsTextNode };
1904
+ }
1905
+ return void 0;
1733
1906
  }
1734
1907
 
1735
1908
  // src/index.ts
1736
1909
  import { ApiClientError } from "@uniformdev/context/api";
1737
1910
  var CanvasClientError = ApiClientError;
1738
1911
  export {
1912
+ ATTRIBUTE_COMPONENT_ID,
1913
+ ATTRIBUTE_MULTILINE,
1914
+ ATTRIBUTE_PARAMETER_ID,
1915
+ ATTRIBUTE_PARAMETER_TYPE,
1916
+ ATTRIBUTE_PARAMETER_VALUE,
1917
+ ATTRIBUTE_PLACEHOLDER,
1739
1918
  ApiClientError,
1740
1919
  BatchEntry,
1741
1920
  CANVAS_DRAFT_STATE,
@@ -1753,6 +1932,7 @@ export {
1753
1932
  CANVAS_TEST_VARIANT_PARAM,
1754
1933
  CanvasClient,
1755
1934
  CanvasClientError,
1935
+ CategoryClient,
1756
1936
  ChildEnhancerBuilder,
1757
1937
  DataSourceClient,
1758
1938
  DataTypeClient,
@@ -1773,6 +1953,7 @@ export {
1773
1953
  PLACEHOLDER_ID,
1774
1954
  RouteClient,
1775
1955
  UncachedCanvasClient,
1956
+ UncachedCategoryClient,
1776
1957
  UniqueBatchEntries,
1777
1958
  bindVariables,
1778
1959
  bindVariablesToObject,
@@ -1782,12 +1963,14 @@ export {
1782
1963
  createEventBus,
1783
1964
  createLimitPolicy,
1784
1965
  createUniformApiEnhancer,
1966
+ createVariableReference,
1785
1967
  enhance,
1786
1968
  extractLocales,
1787
1969
  generateHash,
1788
1970
  getChannelName,
1789
1971
  getComponentJsonPointer,
1790
1972
  getComponentPath,
1973
+ getParameterAttributes,
1791
1974
  isAddComponentMessage,
1792
1975
  isDismissPlaceholderMessage,
1793
1976
  isMovingComponentMessage,
@@ -1806,6 +1989,7 @@ export {
1806
1989
  mapSlotToPersonalizedVariations,
1807
1990
  mapSlotToTestVariations,
1808
1991
  nullLimitPolicy,
1992
+ parseVariableExpression,
1809
1993
  subscribeToComposition,
1810
1994
  unstable_CompositionRelationshipClient,
1811
1995
  walkComponentTree
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/canvas",
3
- "version": "19.35.1",
3
+ "version": "19.35.3-alpha.82+4bc341093",
4
4
  "description": "Common functionality and types for Uniform Canvas",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -38,7 +38,7 @@
38
38
  "pusher-js": "8.2.0"
39
39
  },
40
40
  "dependencies": {
41
- "@uniformdev/context": "19.35.1",
41
+ "@uniformdev/context": "19.35.3-alpha.82+4bc341093",
42
42
  "immer": "9.0.21"
43
43
  },
44
44
  "files": [
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "a85462ec75d379c5447dc625135f828e9d4d7dd6"
50
+ "gitHead": "4bc341093bc946900df2646fe53eca4bcddc693c"
51
51
  }