@uniformdev/canvas 19.36.1-alpha.0 → 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.d.mts +133 -121
- package/dist/index.d.ts +133 -121
- package/dist/index.esm.js +179 -74
- package/dist/index.js +181 -74
- package/dist/index.mjs +179 -74
- package/package.json +3 -3
package/dist/index.esm.js
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/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
|
@@ -576,6 +578,9 @@ var CanvasClient = class extends import_api.ApiClient {
|
|
576
578
|
getCompositionById(options) {
|
577
579
|
return this.getOneComposition(options);
|
578
580
|
}
|
581
|
+
getCompositionDefaults(options) {
|
582
|
+
return this.getOneComposition(options);
|
583
|
+
}
|
579
584
|
/** Fetches historical versions of a composition or pattern */
|
580
585
|
async unstable_getCompositionHistory(options) {
|
581
586
|
const edgeUrl = this.createUrl("/api/v1/canvas-history", {
|
@@ -1736,80 +1741,6 @@ var RouteClient = class extends import_api6.ApiClient {
|
|
1736
1741
|
}
|
1737
1742
|
};
|
1738
1743
|
|
1739
|
-
// src/utils/bindVariables.ts
|
1740
|
-
function bindVariables({
|
1741
|
-
variables,
|
1742
|
-
value,
|
1743
|
-
errorPrefix = "Variable",
|
1744
|
-
handleBinding
|
1745
|
-
}) {
|
1746
|
-
let boundCount = 0;
|
1747
|
-
const errors = [];
|
1748
|
-
const defaultHandleBinding = (variableName, variables2, errors2) => {
|
1749
|
-
const variableValue = variables2[variableName];
|
1750
|
-
if (variableValue === void 0) {
|
1751
|
-
errors2.push(`${errorPrefix} "${variableName}" is not defined`);
|
1752
|
-
return "";
|
1753
|
-
}
|
1754
|
-
return variableValue;
|
1755
|
-
};
|
1756
|
-
const result = value.replace(/(?<!\\)\${([^}]+)}/g, (_match, variableName) => {
|
1757
|
-
const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(variableName, variables, errors);
|
1758
|
-
boundCount++;
|
1759
|
-
return variableValue;
|
1760
|
-
});
|
1761
|
-
return { result, boundCount, errors: errors.length > 0 ? errors : void 0 };
|
1762
|
-
}
|
1763
|
-
|
1764
|
-
// src/utils/bindVariablesToObject.ts
|
1765
|
-
var import_immer = require("immer");
|
1766
|
-
function bindVariablesToObject(options) {
|
1767
|
-
return bindVariablesToObjectRecursive(options);
|
1768
|
-
}
|
1769
|
-
function bindVariablesToObjectRecursive({
|
1770
|
-
value,
|
1771
|
-
recursivePath,
|
1772
|
-
...bindVariablesOptions
|
1773
|
-
}) {
|
1774
|
-
let boundCount = 0;
|
1775
|
-
const errors = [];
|
1776
|
-
if (typeof value === "string") {
|
1777
|
-
return bindVariables({ ...bindVariablesOptions, value });
|
1778
|
-
}
|
1779
|
-
if (typeof value !== "object" || value === null) {
|
1780
|
-
return { boundCount: 0, result: value };
|
1781
|
-
}
|
1782
|
-
const result = (0, import_immer.produce)(value, (draft) => {
|
1783
|
-
Object.entries(draft).forEach(([property, oldValue]) => {
|
1784
|
-
const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
|
1785
|
-
if (typeof oldValue === "string") {
|
1786
|
-
const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
|
1787
|
-
if (oldValue !== bindResult.result || bindResult.errors) {
|
1788
|
-
boundCount += bindResult.boundCount;
|
1789
|
-
draft[property] = bindResult.result;
|
1790
|
-
if (bindResult.errors) {
|
1791
|
-
errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
|
1792
|
-
}
|
1793
|
-
}
|
1794
|
-
return;
|
1795
|
-
}
|
1796
|
-
const childBind = bindVariablesToObjectRecursive({
|
1797
|
-
...bindVariablesOptions,
|
1798
|
-
value: oldValue,
|
1799
|
-
recursivePath: currentObjectPath
|
1800
|
-
});
|
1801
|
-
if (childBind.boundCount || childBind.errors) {
|
1802
|
-
boundCount += childBind.boundCount;
|
1803
|
-
draft[property] = childBind.result;
|
1804
|
-
if (childBind.errors) {
|
1805
|
-
errors.push(...childBind.errors.map((e) => `${currentObjectPath}: ${e}`));
|
1806
|
-
}
|
1807
|
-
}
|
1808
|
-
});
|
1809
|
-
});
|
1810
|
-
return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
|
1811
|
-
}
|
1812
|
-
|
1813
1744
|
// src/utils/createApiEnhancer.ts
|
1814
1745
|
var createUniformApiEnhancer = ({ apiUrl }) => {
|
1815
1746
|
return async (message) => {
|
@@ -1899,6 +1830,180 @@ function mapSlotToTestVariations(slot) {
|
|
1899
1830
|
});
|
1900
1831
|
}
|
1901
1832
|
|
1833
|
+
// src/utils/variables/parseVariableExpression.ts
|
1834
|
+
var escapeCharacter = "\\";
|
1835
|
+
var variablePrefix = "${";
|
1836
|
+
var variableSuffix = "}";
|
1837
|
+
function parseVariableExpression(serialized, onToken) {
|
1838
|
+
let bufferStartIndex = 0;
|
1839
|
+
let bufferEndIndex = 0;
|
1840
|
+
let tokenCount = 0;
|
1841
|
+
const handleToken = (token, type) => {
|
1842
|
+
tokenCount++;
|
1843
|
+
return onToken == null ? void 0 : onToken(token, type);
|
1844
|
+
};
|
1845
|
+
let state = "text";
|
1846
|
+
for (let index = 0; index < serialized.length; index++) {
|
1847
|
+
const char = serialized[index];
|
1848
|
+
if (bufferStartIndex > bufferEndIndex) {
|
1849
|
+
bufferEndIndex = bufferStartIndex;
|
1850
|
+
}
|
1851
|
+
if (char === variablePrefix[0] && serialized[index + 1] === variablePrefix[1]) {
|
1852
|
+
if (serialized[index - 1] === escapeCharacter) {
|
1853
|
+
bufferEndIndex -= escapeCharacter.length;
|
1854
|
+
if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
|
1855
|
+
return tokenCount;
|
1856
|
+
}
|
1857
|
+
bufferStartIndex = index;
|
1858
|
+
bufferEndIndex = index + 1;
|
1859
|
+
continue;
|
1860
|
+
}
|
1861
|
+
state = "variable";
|
1862
|
+
if (bufferEndIndex > bufferStartIndex) {
|
1863
|
+
if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "text") === false) {
|
1864
|
+
return tokenCount;
|
1865
|
+
}
|
1866
|
+
bufferStartIndex = bufferEndIndex;
|
1867
|
+
}
|
1868
|
+
index += variablePrefix.length - 1;
|
1869
|
+
bufferStartIndex += variablePrefix.length;
|
1870
|
+
continue;
|
1871
|
+
}
|
1872
|
+
if (char === variableSuffix && state === "variable") {
|
1873
|
+
state = "text";
|
1874
|
+
if (bufferEndIndex > bufferStartIndex) {
|
1875
|
+
if (handleToken(serialized.substring(bufferStartIndex, bufferEndIndex), "variable") === false) {
|
1876
|
+
return tokenCount;
|
1877
|
+
}
|
1878
|
+
bufferStartIndex = bufferEndIndex + variableSuffix.length;
|
1879
|
+
}
|
1880
|
+
continue;
|
1881
|
+
}
|
1882
|
+
bufferEndIndex++;
|
1883
|
+
}
|
1884
|
+
if (bufferEndIndex > bufferStartIndex) {
|
1885
|
+
if (state === "variable") {
|
1886
|
+
state = "text";
|
1887
|
+
bufferStartIndex -= variablePrefix.length;
|
1888
|
+
}
|
1889
|
+
handleToken(serialized.substring(bufferStartIndex), state);
|
1890
|
+
}
|
1891
|
+
return tokenCount;
|
1892
|
+
}
|
1893
|
+
|
1894
|
+
// src/utils/variables/bindVariables.ts
|
1895
|
+
function bindVariables({
|
1896
|
+
variables,
|
1897
|
+
value,
|
1898
|
+
errorPrefix = "Variable",
|
1899
|
+
handleBinding
|
1900
|
+
}) {
|
1901
|
+
let boundCount = 0;
|
1902
|
+
let tokenCount = 0;
|
1903
|
+
const errors = [];
|
1904
|
+
const defaultHandleBinding = (variableName, variables2, errors2) => {
|
1905
|
+
const variableValue = variables2[variableName];
|
1906
|
+
if (variableValue === void 0) {
|
1907
|
+
errors2.push(`${errorPrefix} "${variableName}" is not defined`);
|
1908
|
+
return "";
|
1909
|
+
}
|
1910
|
+
return variableValue;
|
1911
|
+
};
|
1912
|
+
const result = [];
|
1913
|
+
parseVariableExpression(value, (token, tokenType) => {
|
1914
|
+
tokenCount++;
|
1915
|
+
if (tokenType === "text") {
|
1916
|
+
result.push(token);
|
1917
|
+
return;
|
1918
|
+
}
|
1919
|
+
const variableValue = (handleBinding != null ? handleBinding : defaultHandleBinding)(token, variables, errors);
|
1920
|
+
boundCount++;
|
1921
|
+
result.push(variableValue);
|
1922
|
+
});
|
1923
|
+
return {
|
1924
|
+
result: tokenCount === 1 ? result[0] : result.join(""),
|
1925
|
+
boundCount,
|
1926
|
+
errors: errors.length > 0 ? errors : void 0
|
1927
|
+
};
|
1928
|
+
}
|
1929
|
+
|
1930
|
+
// src/utils/variables/bindVariablesToObject.ts
|
1931
|
+
var import_immer = require("immer");
|
1932
|
+
|
1933
|
+
// src/utils/variables/createVariableReference.ts
|
1934
|
+
function createVariableReference(variableName) {
|
1935
|
+
return `\${${variableName}}`;
|
1936
|
+
}
|
1937
|
+
|
1938
|
+
// src/utils/variables/bindVariablesToObject.ts
|
1939
|
+
function bindVariablesToObject(options) {
|
1940
|
+
return bindVariablesToObjectRecursive(options);
|
1941
|
+
}
|
1942
|
+
function bindVariablesToObjectRecursive({
|
1943
|
+
value,
|
1944
|
+
recursivePath,
|
1945
|
+
...bindVariablesOptions
|
1946
|
+
}) {
|
1947
|
+
let boundCount = 0;
|
1948
|
+
const errors = [];
|
1949
|
+
if (typeof value === "string") {
|
1950
|
+
return bindVariables({ ...bindVariablesOptions, value });
|
1951
|
+
}
|
1952
|
+
if (typeof value !== "object" || value === null) {
|
1953
|
+
return { boundCount: 0, result: value };
|
1954
|
+
}
|
1955
|
+
const richTextNodeResult = handleRichTextNodeBinding(value, bindVariablesOptions);
|
1956
|
+
if (richTextNodeResult !== void 0) {
|
1957
|
+
return richTextNodeResult;
|
1958
|
+
}
|
1959
|
+
const result = (0, import_immer.produce)(value, (draft) => {
|
1960
|
+
Object.entries(draft).forEach(([property, oldValue]) => {
|
1961
|
+
const currentObjectPath = recursivePath ? `${recursivePath}.${property}` : property;
|
1962
|
+
if (typeof oldValue === "string") {
|
1963
|
+
const bindResult = bindVariables({ ...bindVariablesOptions, value: oldValue });
|
1964
|
+
if (oldValue !== bindResult.result || bindResult.errors) {
|
1965
|
+
boundCount += bindResult.boundCount;
|
1966
|
+
draft[property] = bindResult.result;
|
1967
|
+
if (bindResult.errors) {
|
1968
|
+
errors.push(...bindResult.errors.map((e) => `${currentObjectPath}: ${e}`));
|
1969
|
+
}
|
1970
|
+
}
|
1971
|
+
return;
|
1972
|
+
}
|
1973
|
+
const childBind = bindVariablesToObjectRecursive({
|
1974
|
+
...bindVariablesOptions,
|
1975
|
+
value: oldValue,
|
1976
|
+
recursivePath: currentObjectPath
|
1977
|
+
});
|
1978
|
+
if (childBind.boundCount || childBind.errors) {
|
1979
|
+
boundCount += childBind.boundCount;
|
1980
|
+
draft[property] = childBind.result;
|
1981
|
+
if (childBind.errors) {
|
1982
|
+
errors.push(...childBind.errors);
|
1983
|
+
}
|
1984
|
+
}
|
1985
|
+
});
|
1986
|
+
});
|
1987
|
+
return { boundCount, result, errors: errors.length > 0 ? errors : void 0 };
|
1988
|
+
}
|
1989
|
+
function handleRichTextNodeBinding(object, options) {
|
1990
|
+
if ("type" in object && object.type === "variable" && "reference" in object && typeof object.reference === "string" && "version" in object && object.version === 1) {
|
1991
|
+
const value = createVariableReference(object.reference);
|
1992
|
+
const bindResult = bindVariables({ ...options, value });
|
1993
|
+
const bindResultAsTextNode = {
|
1994
|
+
detail: 0,
|
1995
|
+
format: 0,
|
1996
|
+
mode: "normal",
|
1997
|
+
style: "",
|
1998
|
+
text: bindResult.result,
|
1999
|
+
type: "text",
|
2000
|
+
version: 1
|
2001
|
+
};
|
2002
|
+
return { ...bindResult, result: bindResultAsTextNode };
|
2003
|
+
}
|
2004
|
+
return void 0;
|
2005
|
+
}
|
2006
|
+
|
1902
2007
|
// src/index.ts
|
1903
2008
|
var import_api7 = require("@uniformdev/context/api");
|
1904
2009
|
var CanvasClientError = import_api7.ApiClientError;
|
@@ -1958,6 +2063,7 @@ var CanvasClientError = import_api7.ApiClientError;
|
|
1958
2063
|
createEventBus,
|
1959
2064
|
createLimitPolicy,
|
1960
2065
|
createUniformApiEnhancer,
|
2066
|
+
createVariableReference,
|
1961
2067
|
enhance,
|
1962
2068
|
extractLocales,
|
1963
2069
|
generateHash,
|
@@ -1983,6 +2089,7 @@ var CanvasClientError = import_api7.ApiClientError;
|
|
1983
2089
|
mapSlotToPersonalizedVariations,
|
1984
2090
|
mapSlotToTestVariations,
|
1985
2091
|
nullLimitPolicy,
|
2092
|
+
parseVariableExpression,
|
1986
2093
|
subscribeToComposition,
|
1987
2094
|
unstable_CompositionRelationshipClient,
|
1988
2095
|
walkComponentTree
|