@uniformdev/canvas 19.36.1-alpha.4 → 19.36.1-alpha.61

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
@@ -482,6 +482,9 @@ var CanvasClient = class extends ApiClient {
482
482
  getCompositionById(options) {
483
483
  return this.getOneComposition(options);
484
484
  }
485
+ getCompositionDefaults(options) {
486
+ return this.getOneComposition(options);
487
+ }
485
488
  /** Fetches historical versions of a composition or pattern */
486
489
  async unstable_getCompositionHistory(options) {
487
490
  const edgeUrl = this.createUrl("/api/v1/canvas-history", {
@@ -1642,80 +1645,6 @@ var RouteClient = class extends ApiClient6 {
1642
1645
  }
1643
1646
  };
1644
1647
 
1645
- // src/utils/bindVariables.ts
1646
- function bindVariables({
1647
- variables,
1648
- value,
1649
- errorPrefix = "Variable",
1650
- handleBinding
1651
- }) {
1652
- let boundCount = 0;
1653
- const errors = [];
1654
- const defaultHandleBinding = (variableName, variables2, errors2) => {
1655
- const variableValue = variables2[variableName];
1656
- if (variableValue === void 0) {
1657
- errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1658
- return "";
1659
- }
1660
- return variableValue;
1661
- };
1662
- const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
1663
- const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
1664
- boundCount++;
1665
- return variableValue;
1666
- });
1667
- return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
1668
- }
1669
-
1670
- // src/utils/bindVariablesToObject.ts
1671
- import { produce } from "immer";
1672
- function bindVariablesToObject(options) {
1673
- return bindVariablesToObjectRecursive(options);
1674
- }
1675
- function bindVariablesToObjectRecursive({
1676
- value,
1677
- recursivePath,
1678
- ...bindVariablesOptions
1679
- }) {
1680
- let boundCount = 0;
1681
- const errors = [];
1682
- if (typeof value === "string") {
1683
- return bindVariables({ ...bindVariablesOptions, value });
1684
- }
1685
- if (typeof value !== "object" || value === null) {
1686
- return { boundCount: 0, result: value };
1687
- }
1688
- const result = produce(value, (draft) => {
1689
- Object.entries(draft).forEach(([property, oldValue]) => {
1690
- const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1691
- if (typeof oldValue === "string") {
1692
- const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1693
- if (oldValue !== bindResult.result || bindResult.errors) {
1694
- boundCount += bindResult.boundCount;
1695
- draft[property] = bindResult.result;
1696
- if (bindResult.errors) {
1697
- errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1698
- }
1699
- }
1700
- return;
1701
- }
1702
- const childBind = bindVariablesToObjectRecursive({
1703
- ...bindVariablesOptions,
1704
- value: oldValue,
1705
- recursivePath: currentObjectPath
1706
- });
1707
- if (childBind.boundCount || childBind.errors) {
1708
- boundCount += childBind.boundCount;
1709
- draft[property] = childBind.result;
1710
- if (childBind.errors) {
1711
- errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
1712
- }
1713
- }
1714
- });
1715
- });
1716
- return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1717
- }
1718
-
1719
1648
  // src/utils/createApiEnhancer.ts
1720
1649
  var createUniformApiEnhancer = ({ apiUrl }) => {
1721
1650
  return async (message) => {
@@ -1805,6 +1734,180 @@ function mapSlotToTestVariations(slot) {
1805
1734
  });
1806
1735
  }
1807
1736
 
1737
+ // src/utils/variables/parseVariableExpression.ts
1738
+ var escapeCharacter = "\\";
1739
+ var variablePrefix = "${";
1740
+ var variableSuffix = "}";
1741
+ function parseVariableExpression(serialized, onToken) {
1742
+ let bufferStartIndex = 0;
1743
+ let bufferEndIndex = 0;
1744
+ let tokenCount = 0;
1745
+ const handleToken = (token, type) => {
1746
+ tokenCount++;
1747
+ return onToken == null ? void 0 : onToken(token, type);
1748
+ };
1749
+ let state = "text";
1750
+ for (let index = 0; index < serialized.length; index++) {
1751
+ const char = serialized[index];
1752
+ if (bufferStartIndex > bufferEndIndex) {
1753
+ bufferEndIndex = bufferStartIndex;
1754
+ }
1755
+ if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
1756
+ if (serialized[index - 1] === escapeCharacter) {
1757
+ bufferEndIndex -= escapeCharacter.length;
1758
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1759
+ return tokenCount;
1760
+ }
1761
+ bufferStartIndex = index;
1762
+ bufferEndIndex = index + 1;
1763
+ continue;
1764
+ }
1765
+ state = "variable";
1766
+ if (bufferEndIndex > bufferStartIndex) {
1767
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1768
+ return tokenCount;
1769
+ }
1770
+ bufferStartIndex = bufferEndIndex;
1771
+ }
1772
+ index += variablePrefix.length - 1;
1773
+ bufferStartIndex += variablePrefix.length;
1774
+ continue;
1775
+ }
1776
+ if (char === variableSuffix && state === "variable") {
1777
+ state = "text";
1778
+ if (bufferEndIndex > bufferStartIndex) {
1779
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
1780
+ return tokenCount;
1781
+ }
1782
+ bufferStartIndex = bufferEndIndex + variableSuffix.length;
1783
+ }
1784
+ continue;
1785
+ }
1786
+ bufferEndIndex++;
1787
+ }
1788
+ if (bufferEndIndex > bufferStartIndex) {
1789
+ if (state === "variable") {
1790
+ state = "text";
1791
+ bufferStartIndex -= variablePrefix.length;
1792
+ }
1793
+ handleToken(serialized.substring(bufferStartIndex), state);
1794
+ }
1795
+ return tokenCount;
1796
+ }
1797
+
1798
+ // src/utils/variables/bindVariables.ts
1799
+ function bindVariables({
1800
+ variables,
1801
+ value,
1802
+ errorPrefix = "Variable",
1803
+ handleBinding
1804
+ }) {
1805
+ let boundCount = 0;
1806
+ let tokenCount = 0;
1807
+ const errors = [];
1808
+ const defaultHandleBinding = (variableName, variables2, errors2) => {
1809
+ const variableValue = variables2[variableName];
1810
+ if (variableValue === void 0) {
1811
+ errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1812
+ return "";
1813
+ }
1814
+ return variableValue;
1815
+ };
1816
+ const result = [];
1817
+ parseVariableExpression(value, (token, tokenType) => {
1818
+ tokenCount++;
1819
+ if (tokenType === "text") {
1820
+ result.push(token);
1821
+ return;
1822
+ }
1823
+ const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
1824
+ boundCount++;
1825
+ result.push(variableValue);
1826
+ });
1827
+ return {
1828
+ result: tokenCount === 1 ? result[0] : result.join(""),
1829
+ boundCount,
1830
+ errors: errors.length > 0 ? errors : void 0
1831
+ };
1832
+ }
1833
+
1834
+ // src/utils/variables/bindVariablesToObject.ts
1835
+ import { produce } from "immer";
1836
+
1837
+ // src/utils/variables/createVariableReference.ts
1838
+ function createVariableReference(variableName) {
1839
+ return `\${${variableName}}`;
1840
+ }
1841
+
1842
+ // src/utils/variables/bindVariablesToObject.ts
1843
+ function bindVariablesToObject(options) {
1844
+ return bindVariablesToObjectRecursive(options);
1845
+ }
1846
+ function bindVariablesToObjectRecursive({
1847
+ value,
1848
+ recursivePath,
1849
+ ...bindVariablesOptions
1850
+ }) {
1851
+ let boundCount = 0;
1852
+ const errors = [];
1853
+ if (typeof value === "string") {
1854
+ return bindVariables({ ...bindVariablesOptions, value });
1855
+ }
1856
+ if (typeof value !== "object" || value === null) {
1857
+ return { boundCount: 0, result: value };
1858
+ }
1859
+ const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
1860
+ if (richTextNodeResult !== void 0) {
1861
+ return richTextNodeResult;
1862
+ }
1863
+ const result = produce(value, (draft) => {
1864
+ Object.entries(draft).forEach(([property, oldValue]) => {
1865
+ const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1866
+ if (typeof oldValue === "string") {
1867
+ const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1868
+ if (oldValue !== bindResult.result || bindResult.errors) {
1869
+ boundCount += bindResult.boundCount;
1870
+ draft[property] = bindResult.result;
1871
+ if (bindResult.errors) {
1872
+ errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1873
+ }
1874
+ }
1875
+ return;
1876
+ }
1877
+ const childBind = bindVariablesToObjectRecursive({
1878
+ ...bindVariablesOptions,
1879
+ value: oldValue,
1880
+ recursivePath: currentObjectPath
1881
+ });
1882
+ if (childBind.boundCount || childBind.errors) {
1883
+ boundCount += childBind.boundCount;
1884
+ draft[property] = childBind.result;
1885
+ if (childBind.errors) {
1886
+ errors.push(...childBind.errors);
1887
+ }
1888
+ }
1889
+ });
1890
+ });
1891
+ return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1892
+ }
1893
+ function handleRichTextNodeBinding(object, options) {
1894
+ if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
1895
+ const value = createVariableReference(object.reference);
1896
+ const bindResult = bindVariables({ ...options, value });
1897
+ const bindResultAsTextNode = {
1898
+ detail: 0,
1899
+ format: 0,
1900
+ mode: "normal",
1901
+ style: "",
1902
+ text: bindResult.result,
1903
+ type: "text",
1904
+ version: 1
1905
+ };
1906
+ return { ...bindResult, result: bindResultAsTextNode };
1907
+ }
1908
+ return void 0;
1909
+ }
1910
+
1808
1911
  // src/index.ts
1809
1912
  import { ApiClientError } from "@uniformdev/context/api";
1810
1913
  var CanvasClientError = ApiClientError;
@@ -1863,6 +1966,7 @@ export {
1863
1966
  createEventBus,
1864
1967
  createLimitPolicy,
1865
1968
  createUniformApiEnhancer,
1969
+ createVariableReference,
1866
1970
  enhance,
1867
1971
  extractLocales,
1868
1972
  generateHash,
@@ -1888,6 +1992,7 @@ export {
1888
1992
  mapSlotToPersonalizedVariations,
1889
1993
  mapSlotToTestVariations,
1890
1994
  nullLimitPolicy,
1995
+ parseVariableExpression,
1891
1996
  subscribeToComposition,
1892
1997
  unstable_CompositionRelationshipClient,
1893
1998
  walkComponentTree
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/canvas",
3
- "version": "19.36.1-alpha.4+99b59c404",
3
+ "version": "19.36.1-alpha.61+12e4e2a6e",
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.36.1-alpha.4+99b59c404",
41
+ "@uniformdev/context": "19.36.1-alpha.61+12e4e2a6e",
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": "99b59c4046213b258e6610258c603878c490a0b9"
50
+ "gitHead": "12e4e2a6e17788b5337c4c0c90c094735d256c60"
51
51
  }