@uniformdev/canvas-next-rsc-client 19.55.2-alpha.14
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/LICENSE.txt +2 -0
- package/README.md +5 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.esm.js +158 -0
- package/dist/index.js +189 -0
- package/dist/index.mjs +158 -0
- package/package.json +50 -0
package/LICENSE.txt
ADDED
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AppDirectoryContextState, ComponentProps, PersonalizeProps, TestProps, AppDirectoryContext } from '@uniformdev/canvas-next-rsc-shared';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
declare const ContextUpdateTransferClient: ({ update, ts, }: {
|
|
6
|
+
update: Partial<AppDirectoryContextState>;
|
|
7
|
+
ts: number;
|
|
8
|
+
}) => null;
|
|
9
|
+
|
|
10
|
+
declare const PersonalizeClient: (props: ComponentProps<PersonalizeProps>) => react.FunctionComponentElement<{
|
|
11
|
+
children?: react.ReactNode;
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
declare const TestClient: (props: ComponentProps<TestProps>) => react.FunctionComponentElement<{
|
|
15
|
+
children?: react.ReactNode;
|
|
16
|
+
}> | null;
|
|
17
|
+
|
|
18
|
+
declare const UniformScript: () => react_jsx_runtime.JSX.Element;
|
|
19
|
+
|
|
20
|
+
declare const useInitUniformContext: (callback: () => AppDirectoryContext) => void;
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
interface Window {
|
|
24
|
+
__UNIFORM_CONTEXT__: AppDirectoryContext | undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
declare const useUniformContext: () => {
|
|
28
|
+
context: AppDirectoryContext | undefined;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { ContextUpdateTransferClient, PersonalizeClient, TestClient, UniformScript, useInitUniformContext, useUniformContext };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AppDirectoryContextState, ComponentProps, PersonalizeProps, TestProps, AppDirectoryContext } from '@uniformdev/canvas-next-rsc-shared';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
declare const ContextUpdateTransferClient: ({ update, ts, }: {
|
|
6
|
+
update: Partial<AppDirectoryContextState>;
|
|
7
|
+
ts: number;
|
|
8
|
+
}) => null;
|
|
9
|
+
|
|
10
|
+
declare const PersonalizeClient: (props: ComponentProps<PersonalizeProps>) => react.FunctionComponentElement<{
|
|
11
|
+
children?: react.ReactNode;
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
declare const TestClient: (props: ComponentProps<TestProps>) => react.FunctionComponentElement<{
|
|
15
|
+
children?: react.ReactNode;
|
|
16
|
+
}> | null;
|
|
17
|
+
|
|
18
|
+
declare const UniformScript: () => react_jsx_runtime.JSX.Element;
|
|
19
|
+
|
|
20
|
+
declare const useInitUniformContext: (callback: () => AppDirectoryContext) => void;
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
interface Window {
|
|
24
|
+
__UNIFORM_CONTEXT__: AppDirectoryContext | undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
declare const useUniformContext: () => {
|
|
28
|
+
context: AppDirectoryContext | undefined;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { ContextUpdateTransferClient, PersonalizeClient, TestClient, UniformScript, useInitUniformContext, useUniformContext };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/ContextUpdateTransferClient.tsx
|
|
4
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
5
|
+
|
|
6
|
+
// src/hooks/useUniformContext.ts
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
var useUniformContext = () => {
|
|
9
|
+
const [context, setContext] = useState(
|
|
10
|
+
typeof window !== "undefined" ? window.__UNIFORM_CONTEXT__ : void 0
|
|
11
|
+
);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const listener = () => {
|
|
14
|
+
setContext(window.__UNIFORM_CONTEXT__);
|
|
15
|
+
};
|
|
16
|
+
window.addEventListener("uniform.context.loaded", listener);
|
|
17
|
+
return () => {
|
|
18
|
+
window.removeEventListener("uniform.context.loaded", listener);
|
|
19
|
+
};
|
|
20
|
+
}, []);
|
|
21
|
+
return {
|
|
22
|
+
context
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/components/ContextUpdateTransferClient.tsx
|
|
27
|
+
var ContextUpdateTransferClient = ({
|
|
28
|
+
update,
|
|
29
|
+
ts
|
|
30
|
+
}) => {
|
|
31
|
+
const { context } = useUniformContext();
|
|
32
|
+
const [lastLogged, setLastLogged] = useState2(void 0);
|
|
33
|
+
useEffect2(() => {
|
|
34
|
+
const shouldBeLogged = (!ts || lastLogged !== ts) && Object.keys(update).length > 0;
|
|
35
|
+
const hasContext = typeof context !== "undefined";
|
|
36
|
+
if (shouldBeLogged && hasContext) {
|
|
37
|
+
setLastLogged(ts);
|
|
38
|
+
context.update(update);
|
|
39
|
+
}
|
|
40
|
+
}, [context, update, lastLogged, ts]);
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/components/PersonalizeClient.ts
|
|
45
|
+
import { CANVAS_PERSONALIZE_SLOT } from "@uniformdev/canvas";
|
|
46
|
+
import { runPersonalization } from "@uniformdev/canvas-next-rsc-shared";
|
|
47
|
+
import { createElement, Fragment, useMemo } from "react";
|
|
48
|
+
var PersonalizeClient = (props) => {
|
|
49
|
+
const { slots } = props;
|
|
50
|
+
const { context } = useUniformContext();
|
|
51
|
+
const indexes = useMemo(() => {
|
|
52
|
+
const result = runPersonalization({
|
|
53
|
+
...props,
|
|
54
|
+
contextInstance: context
|
|
55
|
+
});
|
|
56
|
+
return result.indexes;
|
|
57
|
+
}, [props, context]);
|
|
58
|
+
const slotsToShow = indexes.map((key) => {
|
|
59
|
+
var _a;
|
|
60
|
+
return (_a = slots[CANVAS_PERSONALIZE_SLOT]) == null ? void 0 : _a.items[key];
|
|
61
|
+
});
|
|
62
|
+
return createElement(Fragment, void 0, slotsToShow);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/components/TestClient.ts
|
|
66
|
+
import { CANVAS_TEST_SLOT } from "@uniformdev/canvas";
|
|
67
|
+
import { runTest } from "@uniformdev/canvas-next-rsc-shared";
|
|
68
|
+
import { createElement as createElement2, Fragment as Fragment2, useMemo as useMemo2 } from "react";
|
|
69
|
+
var TestClient = (props) => {
|
|
70
|
+
var _a;
|
|
71
|
+
const { slots } = props;
|
|
72
|
+
const { context } = useUniformContext();
|
|
73
|
+
const index = useMemo2(() => {
|
|
74
|
+
const result = runTest({
|
|
75
|
+
...props,
|
|
76
|
+
contextInstance: context
|
|
77
|
+
});
|
|
78
|
+
return result.index;
|
|
79
|
+
}, [props, context]);
|
|
80
|
+
if (typeof index !== "number") {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return createElement2(Fragment2, void 0, (_a = slots == null ? void 0 : slots[CANVAS_TEST_SLOT]) == null ? void 0 : _a.items[index]);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/components/UniformScriptClient.tsx
|
|
87
|
+
import { createCanvasChannel, IN_CONTEXT_EDITOR_QUERY_STRING_PARAM } from "@uniformdev/canvas";
|
|
88
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
89
|
+
import { useEffect as useEffect3, useMemo as useMemo3 } from "react";
|
|
90
|
+
import { Fragment as Fragment3, jsx } from "react/jsx-runtime";
|
|
91
|
+
var UniformScript = () => {
|
|
92
|
+
const router = useRouter();
|
|
93
|
+
const params = useSearchParams();
|
|
94
|
+
const enabled = params.get(IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === "true";
|
|
95
|
+
const channel = useMemo3(() => {
|
|
96
|
+
var _a;
|
|
97
|
+
if (typeof window === "undefined") {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const instance = createCanvasChannel({
|
|
101
|
+
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
102
|
+
listenTo: [window]
|
|
103
|
+
});
|
|
104
|
+
return instance;
|
|
105
|
+
}, []);
|
|
106
|
+
useEffect3(() => {
|
|
107
|
+
if (!channel) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const unsubscribeFromEditorUpdates = channel.on("editor-state-updated", () => {
|
|
111
|
+
router.refresh();
|
|
112
|
+
});
|
|
113
|
+
return () => {
|
|
114
|
+
unsubscribeFromEditorUpdates();
|
|
115
|
+
};
|
|
116
|
+
}, [channel, router]);
|
|
117
|
+
useEffect3(() => {
|
|
118
|
+
if (typeof window === "undefined") {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const existing = document.getElementById("uniform-script");
|
|
122
|
+
if (enabled) {
|
|
123
|
+
if (!existing) {
|
|
124
|
+
const script = document.createElement("script");
|
|
125
|
+
script.id = "uniform-script";
|
|
126
|
+
script.src = `https://uniform.app/files/canvas-in-context-embed/index.js`;
|
|
127
|
+
script.async = true;
|
|
128
|
+
document.head.appendChild(script);
|
|
129
|
+
}
|
|
130
|
+
} else if (existing) {
|
|
131
|
+
existing.remove();
|
|
132
|
+
}
|
|
133
|
+
}, [enabled]);
|
|
134
|
+
return /* @__PURE__ */ jsx(Fragment3, {});
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/hooks/useInitUniformContext.ts
|
|
138
|
+
import { useEffect as useEffect4, useState as useState3 } from "react";
|
|
139
|
+
var useInitUniformContext = (callback) => {
|
|
140
|
+
const [called, setCalled] = useState3(false);
|
|
141
|
+
useEffect4(() => {
|
|
142
|
+
if (typeof window === "undefined" || called || typeof window.__UNIFORM_CONTEXT__ !== "undefined") {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
window.__UNIFORM_CONTEXT__ = callback();
|
|
146
|
+
setCalled(true);
|
|
147
|
+
const event = new Event("uniform.context.loaded");
|
|
148
|
+
window.dispatchEvent(event);
|
|
149
|
+
}, [called, callback]);
|
|
150
|
+
};
|
|
151
|
+
export {
|
|
152
|
+
ContextUpdateTransferClient,
|
|
153
|
+
PersonalizeClient,
|
|
154
|
+
TestClient,
|
|
155
|
+
UniformScript,
|
|
156
|
+
useInitUniformContext,
|
|
157
|
+
useUniformContext
|
|
158
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
ContextUpdateTransferClient: () => ContextUpdateTransferClient,
|
|
25
|
+
PersonalizeClient: () => PersonalizeClient,
|
|
26
|
+
TestClient: () => TestClient,
|
|
27
|
+
UniformScript: () => UniformScript,
|
|
28
|
+
useInitUniformContext: () => useInitUniformContext,
|
|
29
|
+
useUniformContext: () => useUniformContext
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
32
|
+
|
|
33
|
+
// src/components/ContextUpdateTransferClient.tsx
|
|
34
|
+
var import_react2 = require("react");
|
|
35
|
+
|
|
36
|
+
// src/hooks/useUniformContext.ts
|
|
37
|
+
var import_react = require("react");
|
|
38
|
+
var useUniformContext = () => {
|
|
39
|
+
const [context, setContext] = (0, import_react.useState)(
|
|
40
|
+
typeof window !== "undefined" ? window.__UNIFORM_CONTEXT__ : void 0
|
|
41
|
+
);
|
|
42
|
+
(0, import_react.useEffect)(() => {
|
|
43
|
+
const listener = () => {
|
|
44
|
+
setContext(window.__UNIFORM_CONTEXT__);
|
|
45
|
+
};
|
|
46
|
+
window.addEventListener("uniform.context.loaded", listener);
|
|
47
|
+
return () => {
|
|
48
|
+
window.removeEventListener("uniform.context.loaded", listener);
|
|
49
|
+
};
|
|
50
|
+
}, []);
|
|
51
|
+
return {
|
|
52
|
+
context
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/components/ContextUpdateTransferClient.tsx
|
|
57
|
+
var ContextUpdateTransferClient = ({
|
|
58
|
+
update,
|
|
59
|
+
ts
|
|
60
|
+
}) => {
|
|
61
|
+
const { context } = useUniformContext();
|
|
62
|
+
const [lastLogged, setLastLogged] = (0, import_react2.useState)(void 0);
|
|
63
|
+
(0, import_react2.useEffect)(() => {
|
|
64
|
+
const shouldBeLogged = (!ts || lastLogged !== ts) && Object.keys(update).length > 0;
|
|
65
|
+
const hasContext = typeof context !== "undefined";
|
|
66
|
+
if (shouldBeLogged && hasContext) {
|
|
67
|
+
setLastLogged(ts);
|
|
68
|
+
context.update(update);
|
|
69
|
+
}
|
|
70
|
+
}, [context, update, lastLogged, ts]);
|
|
71
|
+
return null;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/components/PersonalizeClient.ts
|
|
75
|
+
var import_canvas = require("@uniformdev/canvas");
|
|
76
|
+
var import_canvas_next_rsc_shared = require("@uniformdev/canvas-next-rsc-shared");
|
|
77
|
+
var import_react3 = require("react");
|
|
78
|
+
var PersonalizeClient = (props) => {
|
|
79
|
+
const { slots } = props;
|
|
80
|
+
const { context } = useUniformContext();
|
|
81
|
+
const indexes = (0, import_react3.useMemo)(() => {
|
|
82
|
+
const result = (0, import_canvas_next_rsc_shared.runPersonalization)({
|
|
83
|
+
...props,
|
|
84
|
+
contextInstance: context
|
|
85
|
+
});
|
|
86
|
+
return result.indexes;
|
|
87
|
+
}, [props, context]);
|
|
88
|
+
const slotsToShow = indexes.map((key) => {
|
|
89
|
+
var _a;
|
|
90
|
+
return (_a = slots[import_canvas.CANVAS_PERSONALIZE_SLOT]) == null ? void 0 : _a.items[key];
|
|
91
|
+
});
|
|
92
|
+
return (0, import_react3.createElement)(import_react3.Fragment, void 0, slotsToShow);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/components/TestClient.ts
|
|
96
|
+
var import_canvas2 = require("@uniformdev/canvas");
|
|
97
|
+
var import_canvas_next_rsc_shared2 = require("@uniformdev/canvas-next-rsc-shared");
|
|
98
|
+
var import_react4 = require("react");
|
|
99
|
+
var TestClient = (props) => {
|
|
100
|
+
var _a;
|
|
101
|
+
const { slots } = props;
|
|
102
|
+
const { context } = useUniformContext();
|
|
103
|
+
const index = (0, import_react4.useMemo)(() => {
|
|
104
|
+
const result = (0, import_canvas_next_rsc_shared2.runTest)({
|
|
105
|
+
...props,
|
|
106
|
+
contextInstance: context
|
|
107
|
+
});
|
|
108
|
+
return result.index;
|
|
109
|
+
}, [props, context]);
|
|
110
|
+
if (typeof index !== "number") {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return (0, import_react4.createElement)(import_react4.Fragment, void 0, (_a = slots == null ? void 0 : slots[import_canvas2.CANVAS_TEST_SLOT]) == null ? void 0 : _a.items[index]);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// src/components/UniformScriptClient.tsx
|
|
117
|
+
var import_canvas3 = require("@uniformdev/canvas");
|
|
118
|
+
var import_navigation = require("next/navigation");
|
|
119
|
+
var import_react5 = require("react");
|
|
120
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
121
|
+
var UniformScript = () => {
|
|
122
|
+
const router = (0, import_navigation.useRouter)();
|
|
123
|
+
const params = (0, import_navigation.useSearchParams)();
|
|
124
|
+
const enabled = params.get(import_canvas3.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === "true";
|
|
125
|
+
const channel = (0, import_react5.useMemo)(() => {
|
|
126
|
+
var _a;
|
|
127
|
+
if (typeof window === "undefined") {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const instance = (0, import_canvas3.createCanvasChannel)({
|
|
131
|
+
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
132
|
+
listenTo: [window]
|
|
133
|
+
});
|
|
134
|
+
return instance;
|
|
135
|
+
}, []);
|
|
136
|
+
(0, import_react5.useEffect)(() => {
|
|
137
|
+
if (!channel) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const unsubscribeFromEditorUpdates = channel.on("editor-state-updated", () => {
|
|
141
|
+
router.refresh();
|
|
142
|
+
});
|
|
143
|
+
return () => {
|
|
144
|
+
unsubscribeFromEditorUpdates();
|
|
145
|
+
};
|
|
146
|
+
}, [channel, router]);
|
|
147
|
+
(0, import_react5.useEffect)(() => {
|
|
148
|
+
if (typeof window === "undefined") {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const existing = document.getElementById("uniform-script");
|
|
152
|
+
if (enabled) {
|
|
153
|
+
if (!existing) {
|
|
154
|
+
const script = document.createElement("script");
|
|
155
|
+
script.id = "uniform-script";
|
|
156
|
+
script.src = `https://uniform.app/files/canvas-in-context-embed/index.js`;
|
|
157
|
+
script.async = true;
|
|
158
|
+
document.head.appendChild(script);
|
|
159
|
+
}
|
|
160
|
+
} else if (existing) {
|
|
161
|
+
existing.remove();
|
|
162
|
+
}
|
|
163
|
+
}, [enabled]);
|
|
164
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {});
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// src/hooks/useInitUniformContext.ts
|
|
168
|
+
var import_react6 = require("react");
|
|
169
|
+
var useInitUniformContext = (callback) => {
|
|
170
|
+
const [called, setCalled] = (0, import_react6.useState)(false);
|
|
171
|
+
(0, import_react6.useEffect)(() => {
|
|
172
|
+
if (typeof window === "undefined" || called || typeof window.__UNIFORM_CONTEXT__ !== "undefined") {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
window.__UNIFORM_CONTEXT__ = callback();
|
|
176
|
+
setCalled(true);
|
|
177
|
+
const event = new Event("uniform.context.loaded");
|
|
178
|
+
window.dispatchEvent(event);
|
|
179
|
+
}, [called, callback]);
|
|
180
|
+
};
|
|
181
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
182
|
+
0 && (module.exports = {
|
|
183
|
+
ContextUpdateTransferClient,
|
|
184
|
+
PersonalizeClient,
|
|
185
|
+
TestClient,
|
|
186
|
+
UniformScript,
|
|
187
|
+
useInitUniformContext,
|
|
188
|
+
useUniformContext
|
|
189
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/ContextUpdateTransferClient.tsx
|
|
4
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
5
|
+
|
|
6
|
+
// src/hooks/useUniformContext.ts
|
|
7
|
+
import { useEffect, useState } from "react";
|
|
8
|
+
var useUniformContext = () => {
|
|
9
|
+
const [context, setContext] = useState(
|
|
10
|
+
typeof window !== "undefined" ? window.__UNIFORM_CONTEXT__ : void 0
|
|
11
|
+
);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const listener = () => {
|
|
14
|
+
setContext(window.__UNIFORM_CONTEXT__);
|
|
15
|
+
};
|
|
16
|
+
window.addEventListener("uniform.context.loaded", listener);
|
|
17
|
+
return () => {
|
|
18
|
+
window.removeEventListener("uniform.context.loaded", listener);
|
|
19
|
+
};
|
|
20
|
+
}, []);
|
|
21
|
+
return {
|
|
22
|
+
context
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/components/ContextUpdateTransferClient.tsx
|
|
27
|
+
var ContextUpdateTransferClient = ({
|
|
28
|
+
update,
|
|
29
|
+
ts
|
|
30
|
+
}) => {
|
|
31
|
+
const { context } = useUniformContext();
|
|
32
|
+
const [lastLogged, setLastLogged] = useState2(void 0);
|
|
33
|
+
useEffect2(() => {
|
|
34
|
+
const shouldBeLogged = (!ts || lastLogged !== ts) && Object.keys(update).length > 0;
|
|
35
|
+
const hasContext = typeof context !== "undefined";
|
|
36
|
+
if (shouldBeLogged && hasContext) {
|
|
37
|
+
setLastLogged(ts);
|
|
38
|
+
context.update(update);
|
|
39
|
+
}
|
|
40
|
+
}, [context, update, lastLogged, ts]);
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/components/PersonalizeClient.ts
|
|
45
|
+
import { CANVAS_PERSONALIZE_SLOT } from "@uniformdev/canvas";
|
|
46
|
+
import { runPersonalization } from "@uniformdev/canvas-next-rsc-shared";
|
|
47
|
+
import { createElement, Fragment, useMemo } from "react";
|
|
48
|
+
var PersonalizeClient = (props) => {
|
|
49
|
+
const { slots } = props;
|
|
50
|
+
const { context } = useUniformContext();
|
|
51
|
+
const indexes = useMemo(() => {
|
|
52
|
+
const result = runPersonalization({
|
|
53
|
+
...props,
|
|
54
|
+
contextInstance: context
|
|
55
|
+
});
|
|
56
|
+
return result.indexes;
|
|
57
|
+
}, [props, context]);
|
|
58
|
+
const slotsToShow = indexes.map((key) => {
|
|
59
|
+
var _a;
|
|
60
|
+
return (_a = slots[CANVAS_PERSONALIZE_SLOT]) == null ? void 0 : _a.items[key];
|
|
61
|
+
});
|
|
62
|
+
return createElement(Fragment, void 0, slotsToShow);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/components/TestClient.ts
|
|
66
|
+
import { CANVAS_TEST_SLOT } from "@uniformdev/canvas";
|
|
67
|
+
import { runTest } from "@uniformdev/canvas-next-rsc-shared";
|
|
68
|
+
import { createElement as createElement2, Fragment as Fragment2, useMemo as useMemo2 } from "react";
|
|
69
|
+
var TestClient = (props) => {
|
|
70
|
+
var _a;
|
|
71
|
+
const { slots } = props;
|
|
72
|
+
const { context } = useUniformContext();
|
|
73
|
+
const index = useMemo2(() => {
|
|
74
|
+
const result = runTest({
|
|
75
|
+
...props,
|
|
76
|
+
contextInstance: context
|
|
77
|
+
});
|
|
78
|
+
return result.index;
|
|
79
|
+
}, [props, context]);
|
|
80
|
+
if (typeof index !== "number") {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return createElement2(Fragment2, void 0, (_a = slots == null ? void 0 : slots[CANVAS_TEST_SLOT]) == null ? void 0 : _a.items[index]);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/components/UniformScriptClient.tsx
|
|
87
|
+
import { createCanvasChannel, IN_CONTEXT_EDITOR_QUERY_STRING_PARAM } from "@uniformdev/canvas";
|
|
88
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
89
|
+
import { useEffect as useEffect3, useMemo as useMemo3 } from "react";
|
|
90
|
+
import { Fragment as Fragment3, jsx } from "react/jsx-runtime";
|
|
91
|
+
var UniformScript = () => {
|
|
92
|
+
const router = useRouter();
|
|
93
|
+
const params = useSearchParams();
|
|
94
|
+
const enabled = params.get(IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === "true";
|
|
95
|
+
const channel = useMemo3(() => {
|
|
96
|
+
var _a;
|
|
97
|
+
if (typeof window === "undefined") {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const instance = createCanvasChannel({
|
|
101
|
+
broadcastTo: [(_a = window.opener) != null ? _a : window.top],
|
|
102
|
+
listenTo: [window]
|
|
103
|
+
});
|
|
104
|
+
return instance;
|
|
105
|
+
}, []);
|
|
106
|
+
useEffect3(() => {
|
|
107
|
+
if (!channel) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const unsubscribeFromEditorUpdates = channel.on("editor-state-updated", () => {
|
|
111
|
+
router.refresh();
|
|
112
|
+
});
|
|
113
|
+
return () => {
|
|
114
|
+
unsubscribeFromEditorUpdates();
|
|
115
|
+
};
|
|
116
|
+
}, [channel, router]);
|
|
117
|
+
useEffect3(() => {
|
|
118
|
+
if (typeof window === "undefined") {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const existing = document.getElementById("uniform-script");
|
|
122
|
+
if (enabled) {
|
|
123
|
+
if (!existing) {
|
|
124
|
+
const script = document.createElement("script");
|
|
125
|
+
script.id = "uniform-script";
|
|
126
|
+
script.src = `https://uniform.app/files/canvas-in-context-embed/index.js`;
|
|
127
|
+
script.async = true;
|
|
128
|
+
document.head.appendChild(script);
|
|
129
|
+
}
|
|
130
|
+
} else if (existing) {
|
|
131
|
+
existing.remove();
|
|
132
|
+
}
|
|
133
|
+
}, [enabled]);
|
|
134
|
+
return /* @__PURE__ */ jsx(Fragment3, {});
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/hooks/useInitUniformContext.ts
|
|
138
|
+
import { useEffect as useEffect4, useState as useState3 } from "react";
|
|
139
|
+
var useInitUniformContext = (callback) => {
|
|
140
|
+
const [called, setCalled] = useState3(false);
|
|
141
|
+
useEffect4(() => {
|
|
142
|
+
if (typeof window === "undefined" || called || typeof window.__UNIFORM_CONTEXT__ !== "undefined") {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
window.__UNIFORM_CONTEXT__ = callback();
|
|
146
|
+
setCalled(true);
|
|
147
|
+
const event = new Event("uniform.context.loaded");
|
|
148
|
+
window.dispatchEvent(event);
|
|
149
|
+
}, [called, callback]);
|
|
150
|
+
};
|
|
151
|
+
export {
|
|
152
|
+
ContextUpdateTransferClient,
|
|
153
|
+
PersonalizeClient,
|
|
154
|
+
TestClient,
|
|
155
|
+
UniformScript,
|
|
156
|
+
useInitUniformContext,
|
|
157
|
+
useUniformContext
|
|
158
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@uniformdev/canvas-next-rsc-client",
|
|
3
|
+
"version": "19.55.2-alpha.14+9b75d5322",
|
|
4
|
+
"license": "SEE LICENSE IN LICENSE.txt",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsup",
|
|
7
|
+
"dev": "tsup --watch",
|
|
8
|
+
"lint": "eslint \"**/*.{ts,tsx}\" --fix",
|
|
9
|
+
"test": "jest --maxWorkers=1 --passWithNoTests"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"module": "./dist/index.esm.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
"import": {
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"node": "./dist/index.mjs",
|
|
18
|
+
"default": "./dist/index.esm.js"
|
|
19
|
+
},
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"/dist"
|
|
25
|
+
],
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^18.0.0",
|
|
28
|
+
"@types/react": "^18.2.20",
|
|
29
|
+
"eslint": "^8.11.0",
|
|
30
|
+
"next": "^13.4.12",
|
|
31
|
+
"react": "18.2.0",
|
|
32
|
+
"react-dom": "18.2.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@uniformdev/canvas": "19.55.2-alpha.14+9b75d5322",
|
|
36
|
+
"@uniformdev/canvas-next-rsc-shared": "^19.55.2-alpha.14+9b75d5322"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=16.14.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"next": ">=13.4.7",
|
|
43
|
+
"react": ">=18.2",
|
|
44
|
+
"react-dom": ">=18.2"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"gitHead": "9b75d5322988b2a8c59e7e1b641fb0c8cb7ad301"
|
|
50
|
+
}
|