@uniformdev/canvas 19.37.1 → 19.38.3-alpha.70

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.esm.js CHANGED
@@ -1658,80 +1658,6 @@ var RouteClient = class extends ApiClient6 {
1658
1658
  }
1659
1659
  };
1660
1660
 
1661
- // src/utils/bindVariables.ts
1662
- function bindVariables({
1663
- variables,
1664
- value,
1665
- errorPrefix = "Variable",
1666
- handleBinding
1667
- }) {
1668
- let boundCount = 0;
1669
- const errors = [];
1670
- const defaultHandleBinding = (variableName, variables2, errors2) => {
1671
- const variableValue = variables2[variableName];
1672
- if (variableValue === void 0) {
1673
- errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1674
- return "";
1675
- }
1676
- return variableValue;
1677
- };
1678
- const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
1679
- const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
1680
- boundCount++;
1681
- return variableValue;
1682
- });
1683
- return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
1684
- }
1685
-
1686
- // src/utils/bindVariablesToObject.ts
1687
- import { produce } from "immer";
1688
- function bindVariablesToObject(options) {
1689
- return bindVariablesToObjectRecursive(options);
1690
- }
1691
- function bindVariablesToObjectRecursive({
1692
- value,
1693
- recursivePath,
1694
- ...bindVariablesOptions
1695
- }) {
1696
- let boundCount = 0;
1697
- const errors = [];
1698
- if (typeof value === "string") {
1699
- return bindVariables({ ...bindVariablesOptions, value });
1700
- }
1701
- if (typeof value !== "object" || value === null) {
1702
- return { boundCount: 0, result: value };
1703
- }
1704
- const result = produce(value, (draft) => {
1705
- Object.entries(draft).forEach(([property, oldValue]) => {
1706
- const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1707
- if (typeof oldValue === "string") {
1708
- const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1709
- if (oldValue !== bindResult.result || bindResult.errors) {
1710
- boundCount += bindResult.boundCount;
1711
- draft[property] = bindResult.result;
1712
- if (bindResult.errors) {
1713
- errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1714
- }
1715
- }
1716
- return;
1717
- }
1718
- const childBind = bindVariablesToObjectRecursive({
1719
- ...bindVariablesOptions,
1720
- value: oldValue,
1721
- recursivePath: currentObjectPath
1722
- });
1723
- if (childBind.boundCount || childBind.errors) {
1724
- boundCount += childBind.boundCount;
1725
- draft[property] = childBind.result;
1726
- if (childBind.errors) {
1727
- errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
1728
- }
1729
- }
1730
- });
1731
- });
1732
- return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1733
- }
1734
-
1735
1661
  // src/utils/createApiEnhancer.ts
1736
1662
  var createUniformApiEnhancer = ({ apiUrl }) => {
1737
1663
  return async (message) => {
@@ -1821,6 +1747,180 @@ function mapSlotToTestVariations(slot) {
1821
1747
  });
1822
1748
  }
1823
1749
 
1750
+ // src/utils/variables/parseVariableExpression.ts
1751
+ var escapeCharacter = "\\";
1752
+ var variablePrefix = "${";
1753
+ var variableSuffix = "}";
1754
+ function parseVariableExpression(serialized, onToken) {
1755
+ let bufferStartIndex = 0;
1756
+ let bufferEndIndex = 0;
1757
+ let tokenCount = 0;
1758
+ const handleToken = (token, type) => {
1759
+ tokenCount++;
1760
+ return onToken == null ? void 0 : onToken(token, type);
1761
+ };
1762
+ let state = "text";
1763
+ for (let index = 0; index < serialized.length; index++) {
1764
+ const char = serialized[index];
1765
+ if (bufferStartIndex > bufferEndIndex) {
1766
+ bufferEndIndex = bufferStartIndex;
1767
+ }
1768
+ if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
1769
+ if (serialized[index - 1] === escapeCharacter) {
1770
+ bufferEndIndex -= escapeCharacter.length;
1771
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1772
+ return tokenCount;
1773
+ }
1774
+ bufferStartIndex = index;
1775
+ bufferEndIndex = index + 1;
1776
+ continue;
1777
+ }
1778
+ state = "variable";
1779
+ if (bufferEndIndex > bufferStartIndex) {
1780
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1781
+ return tokenCount;
1782
+ }
1783
+ bufferStartIndex = bufferEndIndex;
1784
+ }
1785
+ index += variablePrefix.length - 1;
1786
+ bufferStartIndex += variablePrefix.length;
1787
+ continue;
1788
+ }
1789
+ if (char === variableSuffix && state === "variable") {
1790
+ state = "text";
1791
+ if (bufferEndIndex > bufferStartIndex) {
1792
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
1793
+ return tokenCount;
1794
+ }
1795
+ bufferStartIndex = bufferEndIndex + variableSuffix.length;
1796
+ }
1797
+ continue;
1798
+ }
1799
+ bufferEndIndex++;
1800
+ }
1801
+ if (bufferEndIndex > bufferStartIndex) {
1802
+ if (state === "variable") {
1803
+ state = "text";
1804
+ bufferStartIndex -= variablePrefix.length;
1805
+ }
1806
+ handleToken(serialized.substring(bufferStartIndex), state);
1807
+ }
1808
+ return tokenCount;
1809
+ }
1810
+
1811
+ // src/utils/variables/bindVariables.ts
1812
+ function bindVariables({
1813
+ variables,
1814
+ value,
1815
+ errorPrefix = "Variable",
1816
+ handleBinding
1817
+ }) {
1818
+ let boundCount = 0;
1819
+ let tokenCount = 0;
1820
+ const errors = [];
1821
+ const defaultHandleBinding = (variableName, variables2, errors2) => {
1822
+ const variableValue = variables2[variableName];
1823
+ if (variableValue === void 0) {
1824
+ errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1825
+ return "";
1826
+ }
1827
+ return variableValue;
1828
+ };
1829
+ const result = [];
1830
+ parseVariableExpression(value, (token, tokenType) => {
1831
+ tokenCount++;
1832
+ if (tokenType === "text") {
1833
+ result.push(token);
1834
+ return;
1835
+ }
1836
+ const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
1837
+ boundCount++;
1838
+ result.push(variableValue);
1839
+ });
1840
+ return {
1841
+ result: tokenCount === 1 ? result[0] : result.join(""),
1842
+ boundCount,
1843
+ errors: errors.length > 0 ? errors : void 0
1844
+ };
1845
+ }
1846
+
1847
+ // src/utils/variables/bindVariablesToObject.ts
1848
+ import { produce } from "immer";
1849
+
1850
+ // src/utils/variables/createVariableReference.ts
1851
+ function createVariableReference(variableName) {
1852
+ return `\${${variableName}}`;
1853
+ }
1854
+
1855
+ // src/utils/variables/bindVariablesToObject.ts
1856
+ function bindVariablesToObject(options) {
1857
+ return bindVariablesToObjectRecursive(options);
1858
+ }
1859
+ function bindVariablesToObjectRecursive({
1860
+ value,
1861
+ recursivePath,
1862
+ ...bindVariablesOptions
1863
+ }) {
1864
+ let boundCount = 0;
1865
+ const errors = [];
1866
+ if (typeof value === "string") {
1867
+ return bindVariables({ ...bindVariablesOptions, value });
1868
+ }
1869
+ if (typeof value !== "object" || value === null) {
1870
+ return { boundCount: 0, result: value };
1871
+ }
1872
+ const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
1873
+ if (richTextNodeResult !== void 0) {
1874
+ return richTextNodeResult;
1875
+ }
1876
+ const result = produce(value, (draft) => {
1877
+ Object.entries(draft).forEach(([property, oldValue]) => {
1878
+ const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1879
+ if (typeof oldValue === "string") {
1880
+ const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1881
+ if (oldValue !== bindResult.result || bindResult.errors) {
1882
+ boundCount += bindResult.boundCount;
1883
+ draft[property] = bindResult.result;
1884
+ if (bindResult.errors) {
1885
+ errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1886
+ }
1887
+ }
1888
+ return;
1889
+ }
1890
+ const childBind = bindVariablesToObjectRecursive({
1891
+ ...bindVariablesOptions,
1892
+ value: oldValue,
1893
+ recursivePath: currentObjectPath
1894
+ });
1895
+ if (childBind.boundCount || childBind.errors) {
1896
+ boundCount += childBind.boundCount;
1897
+ draft[property] = childBind.result;
1898
+ if (childBind.errors) {
1899
+ errors.push(...childBind.errors);
1900
+ }
1901
+ }
1902
+ });
1903
+ });
1904
+ return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1905
+ }
1906
+ function handleRichTextNodeBinding(object, options) {
1907
+ if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
1908
+ const value = createVariableReference(object.reference);
1909
+ const bindResult = bindVariables({ ...options, value });
1910
+ const bindResultAsTextNode = {
1911
+ detail: 0,
1912
+ format: 0,
1913
+ mode: "normal",
1914
+ style: "",
1915
+ text: bindResult.result,
1916
+ type: "text",
1917
+ version: 1
1918
+ };
1919
+ return { ...bindResult, result: bindResultAsTextNode };
1920
+ }
1921
+ return void 0;
1922
+ }
1923
+
1824
1924
  // src/index.ts
1825
1925
  import { ApiClientError as ApiClientError2 } from "@uniformdev/context/api";
1826
1926
  var CanvasClientError = ApiClientError2;
@@ -1879,6 +1979,7 @@ export {
1879
1979
  createEventBus,
1880
1980
  createLimitPolicy,
1881
1981
  createUniformApiEnhancer,
1982
+ createVariableReference,
1882
1983
  enhance,
1883
1984
  extractLocales,
1884
1985
  generateHash,
@@ -1904,6 +2005,7 @@ export {
1904
2005
  mapSlotToPersonalizedVariations,
1905
2006
  mapSlotToTestVariations,
1906
2007
  nullLimitPolicy,
2008
+ parseVariableExpression,
1907
2009
  subscribeToComposition,
1908
2010
  unstable_CompositionRelationshipClient,
1909
2011
  walkComponentTree
package/dist/index.js CHANGED
@@ -331,6 +331,7 @@ __export(src_exports, {
331
331
  createEventBus: () => createEventBus,
332
332
  createLimitPolicy: () => createLimitPolicy,
333
333
  createUniformApiEnhancer: () => createUniformApiEnhancer,
334
+ createVariableReference: () => createVariableReference,
334
335
  enhance: () => enhance,
335
336
  extractLocales: () => extractLocales,
336
337
  generateHash: () => generateHash,
@@ -356,6 +357,7 @@ __export(src_exports, {
356
357
  mapSlotToPersonalizedVariations: () => mapSlotToPersonalizedVariations,
357
358
  mapSlotToTestVariations: () => mapSlotToTestVariations,
358
359
  nullLimitPolicy: () => nullLimitPolicy,
360
+ parseVariableExpression: () => parseVariableExpression,
359
361
  subscribeToComposition: () => subscribeToComposition,
360
362
  unstable_CompositionRelationshipClient: () => unstable_CompositionRelationshipClient,
361
363
  walkComponentTree: () => walkComponentTree
@@ -1752,80 +1754,6 @@ var RouteClient = class extends import_api7.ApiClient {
1752
1754
  }
1753
1755
  };
1754
1756
 
1755
- // src/utils/bindVariables.ts
1756
- function bindVariables({
1757
- variables,
1758
- value,
1759
- errorPrefix = "Variable",
1760
- handleBinding
1761
- }) {
1762
- let boundCount = 0;
1763
- const errors = [];
1764
- const defaultHandleBinding = (variableName, variables2, errors2) => {
1765
- const variableValue = variables2[variableName];
1766
- if (variableValue === void 0) {
1767
- errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1768
- return "";
1769
- }
1770
- return variableValue;
1771
- };
1772
- const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
1773
- const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
1774
- boundCount++;
1775
- return variableValue;
1776
- });
1777
- return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
1778
- }
1779
-
1780
- // src/utils/bindVariablesToObject.ts
1781
- var import_immer = require("immer");
1782
- function bindVariablesToObject(options) {
1783
- return bindVariablesToObjectRecursive(options);
1784
- }
1785
- function bindVariablesToObjectRecursive({
1786
- value,
1787
- recursivePath,
1788
- ...bindVariablesOptions
1789
- }) {
1790
- let boundCount = 0;
1791
- const errors = [];
1792
- if (typeof value === "string") {
1793
- return bindVariables({ ...bindVariablesOptions, value });
1794
- }
1795
- if (typeof value !== "object" || value === null) {
1796
- return { boundCount: 0, result: value };
1797
- }
1798
- const result = (0, import_immer.produce)(value, (draft) => {
1799
- Object.entries(draft).forEach(([property, oldValue]) => {
1800
- const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1801
- if (typeof oldValue === "string") {
1802
- const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1803
- if (oldValue !== bindResult.result || bindResult.errors) {
1804
- boundCount += bindResult.boundCount;
1805
- draft[property] = bindResult.result;
1806
- if (bindResult.errors) {
1807
- errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1808
- }
1809
- }
1810
- return;
1811
- }
1812
- const childBind = bindVariablesToObjectRecursive({
1813
- ...bindVariablesOptions,
1814
- value: oldValue,
1815
- recursivePath: currentObjectPath
1816
- });
1817
- if (childBind.boundCount || childBind.errors) {
1818
- boundCount += childBind.boundCount;
1819
- draft[property] = childBind.result;
1820
- if (childBind.errors) {
1821
- errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
1822
- }
1823
- }
1824
- });
1825
- });
1826
- return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
1827
- }
1828
-
1829
1757
  // src/utils/createApiEnhancer.ts
1830
1758
  var createUniformApiEnhancer = ({ apiUrl }) => {
1831
1759
  return async (message) => {
@@ -1915,6 +1843,180 @@ function mapSlotToTestVariations(slot) {
1915
1843
  });
1916
1844
  }
1917
1845
 
1846
+ // src/utils/variables/parseVariableExpression.ts
1847
+ var escapeCharacter = "\\";
1848
+ var variablePrefix = "${";
1849
+ var variableSuffix = "}";
1850
+ function parseVariableExpression(serialized, onToken) {
1851
+ let bufferStartIndex = 0;
1852
+ let bufferEndIndex = 0;
1853
+ let tokenCount = 0;
1854
+ const handleToken = (token, type) => {
1855
+ tokenCount++;
1856
+ return onToken == null ? void 0 : onToken(token, type);
1857
+ };
1858
+ let state = "text";
1859
+ for (let index = 0; index < serialized.length; index++) {
1860
+ const char = serialized[index];
1861
+ if (bufferStartIndex > bufferEndIndex) {
1862
+ bufferEndIndex = bufferStartIndex;
1863
+ }
1864
+ if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
1865
+ if (serialized[index - 1] === escapeCharacter) {
1866
+ bufferEndIndex -= escapeCharacter.length;
1867
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1868
+ return tokenCount;
1869
+ }
1870
+ bufferStartIndex = index;
1871
+ bufferEndIndex = index + 1;
1872
+ continue;
1873
+ }
1874
+ state = "variable";
1875
+ if (bufferEndIndex > bufferStartIndex) {
1876
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
1877
+ return tokenCount;
1878
+ }
1879
+ bufferStartIndex = bufferEndIndex;
1880
+ }
1881
+ index += variablePrefix.length - 1;
1882
+ bufferStartIndex += variablePrefix.length;
1883
+ continue;
1884
+ }
1885
+ if (char === variableSuffix && state === "variable") {
1886
+ state = "text";
1887
+ if (bufferEndIndex > bufferStartIndex) {
1888
+ if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
1889
+ return tokenCount;
1890
+ }
1891
+ bufferStartIndex = bufferEndIndex + variableSuffix.length;
1892
+ }
1893
+ continue;
1894
+ }
1895
+ bufferEndIndex++;
1896
+ }
1897
+ if (bufferEndIndex > bufferStartIndex) {
1898
+ if (state === "variable") {
1899
+ state = "text";
1900
+ bufferStartIndex -= variablePrefix.length;
1901
+ }
1902
+ handleToken(serialized.substring(bufferStartIndex), state);
1903
+ }
1904
+ return tokenCount;
1905
+ }
1906
+
1907
+ // src/utils/variables/bindVariables.ts
1908
+ function bindVariables({
1909
+ variables,
1910
+ value,
1911
+ errorPrefix = "Variable",
1912
+ handleBinding
1913
+ }) {
1914
+ let boundCount = 0;
1915
+ let tokenCount = 0;
1916
+ const errors = [];
1917
+ const defaultHandleBinding = (variableName, variables2, errors2) => {
1918
+ const variableValue = variables2[variableName];
1919
+ if (variableValue === void 0) {
1920
+ errors2.push(`${errorPrefix} "${variableName}" is not defined`);
1921
+ return "";
1922
+ }
1923
+ return variableValue;
1924
+ };
1925
+ const result = [];
1926
+ parseVariableExpression(value, (token, tokenType) => {
1927
+ tokenCount++;
1928
+ if (tokenType === "text") {
1929
+ result.push(token);
1930
+ return;
1931
+ }
1932
+ const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
1933
+ boundCount++;
1934
+ result.push(variableValue);
1935
+ });
1936
+ return {
1937
+ result: tokenCount === 1 ? result[0] : result.join(""),
1938
+ boundCount,
1939
+ errors: errors.length > 0 ? errors : void 0
1940
+ };
1941
+ }
1942
+
1943
+ // src/utils/variables/bindVariablesToObject.ts
1944
+ var import_immer = require("immer");
1945
+
1946
+ // src/utils/variables/createVariableReference.ts
1947
+ function createVariableReference(variableName) {
1948
+ return `\${${variableName}}`;
1949
+ }
1950
+
1951
+ // src/utils/variables/bindVariablesToObject.ts
1952
+ function bindVariablesToObject(options) {
1953
+ return bindVariablesToObjectRecursive(options);
1954
+ }
1955
+ function bindVariablesToObjectRecursive({
1956
+ value,
1957
+ recursivePath,
1958
+ ...bindVariablesOptions
1959
+ }) {
1960
+ let boundCount = 0;
1961
+ const errors = [];
1962
+ if (typeof value === "string") {
1963
+ return bindVariables({ ...bindVariablesOptions, value });
1964
+ }
1965
+ if (typeof value !== "object" || value === null) {
1966
+ return { boundCount: 0, result: value };
1967
+ }
1968
+ const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
1969
+ if (richTextNodeResult !== void 0) {
1970
+ return richTextNodeResult;
1971
+ }
1972
+ const result = (0, import_immer.produce)(value, (draft) => {
1973
+ Object.entries(draft).forEach(([property, oldValue]) => {
1974
+ const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
1975
+ if (typeof oldValue === "string") {
1976
+ const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
1977
+ if (oldValue !== bindResult.result || bindResult.errors) {
1978
+ boundCount += bindResult.boundCount;
1979
+ draft[property] = bindResult.result;
1980
+ if (bindResult.errors) {
1981
+ errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
1982
+ }
1983
+ }
1984
+ return;
1985
+ }
1986
+ const childBind = bindVariablesToObjectRecursive({
1987
+ ...bindVariablesOptions,
1988
+ value: oldValue,
1989
+ recursivePath: currentObjectPath
1990
+ });
1991
+ if (childBind.boundCount || childBind.errors) {
1992
+ boundCount += childBind.boundCount;
1993
+ draft[property] = childBind.result;
1994
+ if (childBind.errors) {
1995
+ errors.push(...childBind.errors);
1996
+ }
1997
+ }
1998
+ });
1999
+ });
2000
+ return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
2001
+ }
2002
+ function handleRichTextNodeBinding(object, options) {
2003
+ if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
2004
+ const value = createVariableReference(object.reference);
2005
+ const bindResult = bindVariables({ ...options, value });
2006
+ const bindResultAsTextNode = {
2007
+ detail: 0,
2008
+ format: 0,
2009
+ mode: "normal",
2010
+ style: "",
2011
+ text: bindResult.result,
2012
+ type: "text",
2013
+ version: 1
2014
+ };
2015
+ return { ...bindResult, result: bindResultAsTextNode };
2016
+ }
2017
+ return void 0;
2018
+ }
2019
+
1918
2020
  // src/index.ts
1919
2021
  var import_api8 = require("@uniformdev/context/api");
1920
2022
  var CanvasClientError = import_api8.ApiClientError;
@@ -1974,6 +2076,7 @@ var CanvasClientError = import_api8.ApiClientError;
1974
2076
  createEventBus,
1975
2077
  createLimitPolicy,
1976
2078
  createUniformApiEnhancer,
2079
+ createVariableReference,
1977
2080
  enhance,
1978
2081
  extractLocales,
1979
2082
  generateHash,
@@ -1999,6 +2102,7 @@ var CanvasClientError = import_api8.ApiClientError;
1999
2102
  mapSlotToPersonalizedVariations,
2000
2103
  mapSlotToTestVariations,
2001
2104
  nullLimitPolicy,
2105
+ parseVariableExpression,
2002
2106
  subscribeToComposition,
2003
2107
  unstable_CompositionRelationshipClient,
2004
2108
  walkComponentTree