@valbuild/react 0.13.0 → 0.13.3

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.
Files changed (48) hide show
  1. package/dist/ValProvider-52f2fa13.esm.js +731 -0
  2. package/dist/ValProvider-7364ec46.cjs.dev.js +746 -0
  3. package/dist/ValProvider-a45a47b9.worker.esm.js +726 -0
  4. package/dist/ValProvider-c7a8476b.browser.esm.js +731 -0
  5. package/dist/ValProvider-e1d5ffbe.cjs.js +7 -0
  6. package/dist/ValProvider-e1d5ffbe.cjs.prod.js +746 -0
  7. package/dist/ValUI-0fbdafd4.cjs.prod.js +398 -0
  8. package/dist/ValUI-371e9bf4.cjs.dev.js +398 -0
  9. package/dist/ValUI-51404232.browser.esm.js +396 -0
  10. package/dist/ValUI-9a3eb570.esm.js +396 -0
  11. package/dist/declarations/src/AuthStatus.d.ts +6 -0
  12. package/dist/declarations/src/ShadowRoot.d.ts +4 -0
  13. package/dist/declarations/src/ValProvider.d.ts +1 -1
  14. package/dist/declarations/src/ValProviderWrapper.d.ts +3 -0
  15. package/dist/declarations/src/ValUI.d.ts +8 -0
  16. package/dist/declarations/src/index.d.ts +1 -1
  17. package/dist/{slicedToArray-0ead6329.cjs.dev.js → slicedToArray-0eb0bcdb.cjs.prod.js} +3 -17
  18. package/dist/{slicedToArray-bf9b195a.worker.esm.js → slicedToArray-1a246338.browser.esm.js} +3 -16
  19. package/dist/{slicedToArray-236143cd.browser.esm.js → slicedToArray-9e7d1407.worker.esm.js} +3 -16
  20. package/dist/{slicedToArray-57b117df.cjs.prod.js → slicedToArray-b7cf26e0.cjs.dev.js} +3 -17
  21. package/dist/{slicedToArray-390fde8c.esm.js → slicedToArray-d846e1d2.esm.js} +3 -16
  22. package/dist/unsupportedIterableToArray-51bb61c2.esm.js +16 -0
  23. package/dist/unsupportedIterableToArray-738344ef.worker.esm.js +16 -0
  24. package/dist/unsupportedIterableToArray-9e97e24a.cjs.dev.js +18 -0
  25. package/dist/unsupportedIterableToArray-afbea1dd.cjs.prod.js +18 -0
  26. package/dist/unsupportedIterableToArray-d3087ed5.browser.esm.js +16 -0
  27. package/dist/valbuild-react.browser.esm.js +9 -1083
  28. package/dist/valbuild-react.cjs.dev.js +24 -1074
  29. package/dist/valbuild-react.cjs.prod.js +24 -1074
  30. package/dist/valbuild-react.esm.js +13 -1079
  31. package/dist/valbuild-react.worker.esm.js +13 -1079
  32. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.browser.esm.js +2 -1
  33. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.cjs.dev.js +2 -1
  34. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.cjs.prod.js +2 -1
  35. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.esm.js +2 -1
  36. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.worker.esm.js +2 -1
  37. package/jsx-runtime/dist/valbuild-react-jsx-runtime.browser.esm.js +2 -1
  38. package/jsx-runtime/dist/valbuild-react-jsx-runtime.cjs.dev.js +2 -1
  39. package/jsx-runtime/dist/valbuild-react-jsx-runtime.cjs.prod.js +2 -1
  40. package/jsx-runtime/dist/valbuild-react-jsx-runtime.esm.js +2 -1
  41. package/jsx-runtime/dist/valbuild-react-jsx-runtime.worker.esm.js +2 -1
  42. package/package.json +9 -6
  43. package/src/AuthStatus.tsx +13 -0
  44. package/src/ShadowRoot.tsx +32 -0
  45. package/src/ValProvider.tsx +10 -383
  46. package/src/ValProviderWrapper.tsx +15 -0
  47. package/src/ValUI.tsx +353 -0
  48. package/src/index.ts +1 -1
@@ -1,7 +1,8 @@
1
- import { a as _slicedToArray, b as _typeof } from '../../dist/slicedToArray-236143cd.browser.esm.js';
1
+ import { _ as _slicedToArray, a as _typeof } from '../../dist/slicedToArray-1a246338.browser.esm.js';
2
2
  import { Internal } from '@valbuild/core';
3
3
  import * as ReactJSXRuntimeDev from 'react/jsx-dev-runtime';
4
4
  export * from 'react/jsx-dev-runtime';
5
+ import '../../dist/unsupportedIterableToArray-d3087ed5.browser.esm.js';
5
6
 
6
7
  var isIntrinsicElement = function isIntrinsicElement(type) {
7
8
  // TODO: think this is not correct, but good enough for now?
@@ -2,9 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var slicedToArray = require('../../dist/slicedToArray-0ead6329.cjs.dev.js');
5
+ var slicedToArray = require('../../dist/slicedToArray-b7cf26e0.cjs.dev.js');
6
6
  var core = require('@valbuild/core');
7
7
  var ReactJSXRuntimeDev = require('react/jsx-dev-runtime');
8
+ require('../../dist/unsupportedIterableToArray-9e97e24a.cjs.dev.js');
8
9
 
9
10
  function _interopNamespace(e) {
10
11
  if (e && e.__esModule) return e;
@@ -2,9 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var slicedToArray = require('../../dist/slicedToArray-57b117df.cjs.prod.js');
5
+ var slicedToArray = require('../../dist/slicedToArray-0eb0bcdb.cjs.prod.js');
6
6
  var core = require('@valbuild/core');
7
7
  var ReactJSXRuntimeDev = require('react/jsx-dev-runtime');
8
+ require('../../dist/unsupportedIterableToArray-afbea1dd.cjs.prod.js');
8
9
 
9
10
  function _interopNamespace(e) {
10
11
  if (e && e.__esModule) return e;
@@ -1,7 +1,8 @@
1
- import { a as _slicedToArray, b as _typeof } from '../../dist/slicedToArray-390fde8c.esm.js';
1
+ import { _ as _slicedToArray, a as _typeof } from '../../dist/slicedToArray-d846e1d2.esm.js';
2
2
  import { Internal } from '@valbuild/core';
3
3
  import * as ReactJSXRuntimeDev from 'react/jsx-dev-runtime';
4
4
  export * from 'react/jsx-dev-runtime';
5
+ import '../../dist/unsupportedIterableToArray-51bb61c2.esm.js';
5
6
 
6
7
  var isIntrinsicElement = function isIntrinsicElement(type) {
7
8
  // TODO: think this is not correct, but good enough for now?
@@ -1,7 +1,8 @@
1
- import { a as _slicedToArray, b as _typeof } from '../../dist/slicedToArray-bf9b195a.worker.esm.js';
1
+ import { _ as _slicedToArray, a as _typeof } from '../../dist/slicedToArray-9e7d1407.worker.esm.js';
2
2
  import { Internal } from '@valbuild/core';
3
3
  import * as ReactJSXRuntimeDev from 'react/jsx-dev-runtime';
4
4
  export * from 'react/jsx-dev-runtime';
5
+ import '../../dist/unsupportedIterableToArray-738344ef.worker.esm.js';
5
6
 
6
7
  var isIntrinsicElement = function isIntrinsicElement(type) {
7
8
  // TODO: think this is not correct, but good enough for now?
@@ -1,7 +1,8 @@
1
- import { a as _slicedToArray, b as _typeof } from '../../dist/slicedToArray-236143cd.browser.esm.js';
1
+ import { _ as _slicedToArray, a as _typeof } from '../../dist/slicedToArray-1a246338.browser.esm.js';
2
2
  import { Internal } from '@valbuild/core';
3
3
  import * as ReactJSXRuntime from 'react/jsx-runtime';
4
4
  export * from 'react/jsx-runtime';
5
+ import '../../dist/unsupportedIterableToArray-d3087ed5.browser.esm.js';
5
6
 
6
7
  var isIntrinsicElement = function isIntrinsicElement(type) {
7
8
  // TODO: think this is not correct, but good enough for now?
@@ -2,9 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var slicedToArray = require('../../dist/slicedToArray-0ead6329.cjs.dev.js');
5
+ var slicedToArray = require('../../dist/slicedToArray-b7cf26e0.cjs.dev.js');
6
6
  var core = require('@valbuild/core');
7
7
  var ReactJSXRuntime = require('react/jsx-runtime');
8
+ require('../../dist/unsupportedIterableToArray-9e97e24a.cjs.dev.js');
8
9
 
9
10
  function _interopNamespace(e) {
10
11
  if (e && e.__esModule) return e;
@@ -2,9 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var slicedToArray = require('../../dist/slicedToArray-57b117df.cjs.prod.js');
5
+ var slicedToArray = require('../../dist/slicedToArray-0eb0bcdb.cjs.prod.js');
6
6
  var core = require('@valbuild/core');
7
7
  var ReactJSXRuntime = require('react/jsx-runtime');
8
+ require('../../dist/unsupportedIterableToArray-afbea1dd.cjs.prod.js');
8
9
 
9
10
  function _interopNamespace(e) {
10
11
  if (e && e.__esModule) return e;
@@ -1,7 +1,8 @@
1
- import { a as _slicedToArray, b as _typeof } from '../../dist/slicedToArray-390fde8c.esm.js';
1
+ import { _ as _slicedToArray, a as _typeof } from '../../dist/slicedToArray-d846e1d2.esm.js';
2
2
  import { Internal } from '@valbuild/core';
3
3
  import * as ReactJSXRuntime from 'react/jsx-runtime';
4
4
  export * from 'react/jsx-runtime';
5
+ import '../../dist/unsupportedIterableToArray-51bb61c2.esm.js';
5
6
 
6
7
  var isIntrinsicElement = function isIntrinsicElement(type) {
7
8
  // TODO: think this is not correct, but good enough for now?
@@ -1,7 +1,8 @@
1
- import { a as _slicedToArray, b as _typeof } from '../../dist/slicedToArray-bf9b195a.worker.esm.js';
1
+ import { _ as _slicedToArray, a as _typeof } from '../../dist/slicedToArray-9e7d1407.worker.esm.js';
2
2
  import { Internal } from '@valbuild/core';
3
3
  import * as ReactJSXRuntime from 'react/jsx-runtime';
4
4
  export * from 'react/jsx-runtime';
5
+ import '../../dist/unsupportedIterableToArray-738344ef.worker.esm.js';
5
6
 
6
7
  var isIntrinsicElement = function isIntrinsicElement(type) {
7
8
  // TODO: think this is not correct, but good enough for now?
package/package.json CHANGED
@@ -1,26 +1,29 @@
1
1
  {
2
2
  "name": "@valbuild/react",
3
- "version": "0.13.0",
3
+ "version": "0.13.3",
4
+ "private": false,
5
+ "description": "Val - React",
4
6
  "sideEffects": false,
5
7
  "scripts": {
6
8
  "typecheck": "tsc --noEmit",
7
9
  "test": "jest"
8
10
  },
9
11
  "dependencies": {
10
- "@valbuild/core": "~0.13.0",
11
- "@valbuild/ui": "~0.13.0",
12
+ "@valbuild/core": "~0.13.3",
13
+ "@valbuild/ui": "~0.13.3",
12
14
  "base64-arraybuffer": "^1.0.2",
13
- "react-shadow": "^20.0.0",
14
15
  "style-to-object": "^0.4.1"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@testing-library/react": "^14.0.0",
18
19
  "@types/react": "^18.0.26",
19
20
  "jest-environment-jsdom": "^29.5.0",
20
- "react": "^18.2.0"
21
+ "react": "^18.2.0",
22
+ "react-dom": "^18.2.0"
21
23
  },
22
24
  "peerDependencies": {
23
- "react": ">=16.8.0"
25
+ "react": ">=16.8.0",
26
+ "react-dom": ">=16.8.0"
24
27
  },
25
28
  "peerDependenciesMeta": {
26
29
  "@types/react": {
@@ -0,0 +1,13 @@
1
+ export type AuthStatus =
2
+ | {
3
+ status:
4
+ | "not-asked"
5
+ | "authenticated"
6
+ | "unauthenticated"
7
+ | "loading"
8
+ | "local";
9
+ }
10
+ | {
11
+ status: "error";
12
+ message: string;
13
+ };
@@ -0,0 +1,32 @@
1
+ import { useState, useLayoutEffect, useRef } from "react";
2
+ import { createPortal } from "react-dom";
3
+
4
+ function ShadowContent({
5
+ root,
6
+ children,
7
+ }: {
8
+ children: React.ReactNode;
9
+ root: Element | DocumentFragment;
10
+ }) {
11
+ return createPortal(children, root);
12
+ }
13
+
14
+ export const ShadowRoot = ({ children }: { children: React.ReactNode }) => {
15
+ const node = useRef<HTMLDivElement>(null);
16
+ const [root, setRoot] = useState<ShadowRoot | null>(null);
17
+
18
+ useLayoutEffect(() => {
19
+ if (node.current) {
20
+ const root = node.current.attachShadow({
21
+ mode: "closed",
22
+ });
23
+ setRoot(root);
24
+ }
25
+ }, []);
26
+
27
+ return (
28
+ <div ref={node}>
29
+ {root && <ShadowContent root={root}>{children}</ShadowContent>}
30
+ </div>
31
+ );
32
+ };
@@ -1,19 +1,8 @@
1
+ "use client";
1
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import React, { useContext, useEffect, useMemo, useState } from "react";
3
+ import React, { lazy, useContext, useMemo } from "react";
3
4
  import { ValApi } from "./ValApi";
4
5
  import { ValStore } from "./ValStore";
5
- import { Inputs, Style, ValOverlay } from "@valbuild/ui";
6
- import root from "react-shadow"; // TODO: remove dependency on react-shadow here?
7
- import {
8
- FileSource,
9
- FILE_REF_PROP,
10
- Internal,
11
- RichText,
12
- SourcePath,
13
- VAL_EXTENSION,
14
- } from "@valbuild/core";
15
- import { PatchJSON } from "@valbuild/core/patch";
16
- import { ImageMetadata } from "@valbuild/core/src/schema/image";
17
6
 
18
7
  export function useValStore() {
19
8
  return useContext(ValContext).valStore;
@@ -44,381 +33,19 @@ export type ValProviderProps = {
44
33
  host?: string;
45
34
  children?: React.ReactNode;
46
35
  };
36
+ const ValUI =
37
+ typeof window !== "undefined" ? lazy(() => import("./ValUI")) : null;
47
38
 
48
- type AuthStatus =
49
- | {
50
- status:
51
- | "not-asked"
52
- | "authenticated"
53
- | "unauthenticated"
54
- | "loading"
55
- | "local";
56
- }
57
- | {
58
- status: "error";
59
- message: string;
60
- };
61
-
62
- export function ValProvider({ host = "/api/val", children }: ValProviderProps) {
63
- const [selectedSources, setSelectedSources] = useState<string[]>([]);
64
- const [editMode, setEditMode] = useState(false);
65
- const [editFormPosition, setEditFormPosition] = useState<{
66
- left: number;
67
- top: number;
68
- } | null>(null);
69
-
70
- const [authentication, setAuthentication] = useState<AuthStatus>({
71
- status: "not-asked",
72
- });
39
+ export default function ValProvider({
40
+ host = "/api/val",
41
+ children,
42
+ }: ValProviderProps) {
73
43
  const valApi = useMemo(() => new ValApi(host), [host]);
74
44
  const valStore = useMemo(() => new ValStore(valApi), [valApi]);
75
-
76
- useEffect(() => {
77
- if (editMode) {
78
- valStore.updateAll();
79
- }
80
- }, [editMode]);
81
- useEffect(() => {
82
- let openValFormListener: ((e: MouseEvent) => void) | undefined = undefined;
83
- let styleElement: HTMLStyleElement | undefined = undefined;
84
- const editButtonClickOptions = {
85
- capture: true,
86
- passive: true,
87
- };
88
- if (editMode) {
89
- // highlight val element by appending a new style
90
- styleElement = document.createElement("style");
91
- styleElement.id = "val-edit-highlight";
92
- styleElement.innerHTML = `
93
- .val-edit-mode >* [data-val-path] {
94
- outline: black solid 2px;
95
- outline-offset: 4px;
96
- cursor: pointer;
97
- }
98
- `;
99
- document.body.appendChild(styleElement);
100
-
101
- // capture event clicks on data-val-path elements
102
- openValFormListener = (e: MouseEvent) => {
103
- if (e.target instanceof Element) {
104
- let parent = e.target;
105
- while (parent && parent !== document.body) {
106
- if (parent.getAttribute("data-val-path")) {
107
- break;
108
- }
109
- if (parent.parentElement) {
110
- parent = parent.parentElement;
111
- } else {
112
- break;
113
- }
114
- }
115
- const valSources = parent?.getAttribute("data-val-path");
116
- if (valSources) {
117
- e.stopPropagation();
118
- setSelectedSources(
119
- valSources.split(
120
- ","
121
- ) /* TODO: just split on commas will not work if path contains , */
122
- );
123
- setEditFormPosition({
124
- left: e.pageX,
125
- top: e.pageY,
126
- });
127
- // } else if (!isValElement(e.target)) {
128
- // console.log("click outside", e.target);
129
- // setEditFormPosition(null);
130
- // setSelectedSources([]);
131
- }
132
- }
133
- };
134
- document.addEventListener(
135
- "click",
136
- openValFormListener,
137
- editButtonClickOptions
138
- );
139
- }
140
- return () => {
141
- if (openValFormListener) {
142
- document.removeEventListener(
143
- "click",
144
- openValFormListener,
145
- editButtonClickOptions
146
- );
147
- }
148
- styleElement?.remove();
149
- };
150
- }, [editMode]);
151
-
152
- // useEffect(() => {
153
- // const requestAuth = !(
154
- // authentication.status === "authenticated" ||
155
- // authentication.status === "local"
156
- // );
157
- // if (requestAuth) {
158
- // setSelectedSources([]);
159
- // console.log("request auth");
160
- // setEditFormPosition(null);
161
- // }
162
- // if (!editMode) {
163
- // // reset state when disabled
164
- // setSelectedSources([]);
165
- // console.log("reset state");
166
- // setEditFormPosition(null);
167
- // }
168
- // }, [editMode, selectedSources.length, authentication.status]);
169
-
170
- useEffect(() => {
171
- if (editMode) {
172
- document.body.classList.add("val-edit-mode");
173
- } else {
174
- document.body.classList.remove("val-edit-mode");
175
- }
176
-
177
- if (editMode) {
178
- if (authentication.status !== "authenticated") {
179
- valApi
180
- .getSession()
181
- .then(async (res) => {
182
- if (res.status === 401) {
183
- setAuthentication({
184
- status: "unauthenticated",
185
- });
186
- } else if (res.ok) {
187
- const data = await res.json();
188
- if (data.mode === "local") {
189
- setAuthentication({ status: "local" });
190
- } else if (data.mode === "proxy") {
191
- setAuthentication({
192
- status: "authenticated",
193
- });
194
- } else {
195
- setAuthentication({
196
- status: "error",
197
- message: "Unknown authentication mode",
198
- });
199
- }
200
- } else {
201
- let message = "Unknown error";
202
- try {
203
- message = await res.text();
204
- } catch {
205
- // ignore
206
- }
207
- setAuthentication({
208
- status: "error",
209
- message,
210
- });
211
- }
212
- })
213
- .catch((err) => {
214
- console.error("Failed to fetch session", err);
215
- setAuthentication({
216
- status: "error",
217
- message: "Unknown authentication mode",
218
- });
219
- });
220
- }
221
- } else {
222
- if (authentication.status === "error") {
223
- setAuthentication({
224
- status: "not-asked",
225
- });
226
- }
227
- }
228
- }, [editMode, authentication.status]);
229
-
230
- const [showEditButton, setShowEditButton] = useState(false);
231
- useEffect(() => {
232
- setShowEditButton(true);
233
- }, []);
234
-
235
- const [inputs, setInputs] = useState<Inputs>({});
236
-
237
- useEffect(() => {
238
- setInputs({});
239
- for (const path of selectedSources) {
240
- valApi.getModule(path).then((serializedModule) => {
241
- let input: Inputs[string] | undefined;
242
- if (
243
- serializedModule.schema.type === "string" &&
244
- typeof serializedModule.source === "string"
245
- ) {
246
- input = {
247
- status: "completed",
248
- type: "text",
249
- data: serializedModule.source,
250
- };
251
- } else if (
252
- serializedModule.schema.type === "richtext" &&
253
- typeof serializedModule.source === "object"
254
- ) {
255
- input = {
256
- status: "completed",
257
- type: "richtext",
258
- data: serializedModule.source as RichText, // TODO: validate
259
- };
260
- } else if (
261
- serializedModule.schema.type === "image" &&
262
- serializedModule.source &&
263
- typeof serializedModule.source === "object" &&
264
- FILE_REF_PROP in serializedModule.source &&
265
- typeof serializedModule.source[FILE_REF_PROP] === "string" &&
266
- VAL_EXTENSION in serializedModule.source &&
267
- typeof serializedModule.source[VAL_EXTENSION] === "string"
268
- ) {
269
- input = {
270
- status: "completed",
271
- type: "image",
272
- data: Internal.convertImageSource(
273
- serializedModule.source as FileSource<ImageMetadata>
274
- ),
275
- };
276
- }
277
- console.log("input path", path);
278
- console.log("serialized path", serializedModule.path);
279
- if (!input) {
280
- throw new Error(
281
- `Unsupported module type: ${serializedModule.schema.type}`
282
- );
283
- }
284
- setInputs((inputs) => {
285
- return {
286
- ...inputs,
287
- [serializedModule.path]: input,
288
- } as Inputs;
289
- });
290
- });
291
- }
292
- }, [selectedSources.join(",")]);
293
45
  return (
294
- <ValContext.Provider
295
- value={{
296
- valApi,
297
- valStore,
298
- }}
299
- >
46
+ <ValContext.Provider value={{ valApi, valStore }}>
300
47
  {children}
301
- {showEditButton && (
302
- <root.div>
303
- {/* TODO: */}
304
- <link rel="preconnect" href="https://fonts.googleapis.com" />
305
- <link
306
- rel="preconnect"
307
- href="https://fonts.gstatic.com"
308
- crossOrigin="anonymous"
309
- />
310
- <link
311
- href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;1,400&family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,400;1,700&display=swap"
312
- rel="stylesheet"
313
- />
314
- <Style />
315
- <div data-mode="dark">
316
- <ValOverlay
317
- editMode={editMode}
318
- setEditMode={setEditMode}
319
- closeValWindow={() => {
320
- setEditFormPosition(null);
321
- setSelectedSources([]);
322
- setInputs({});
323
- }}
324
- valWindow={
325
- (editFormPosition && {
326
- position: editFormPosition,
327
- inputs,
328
- onSubmit: (inputs) => {
329
- Promise.all(
330
- Object.entries(inputs).map(([path, input]) => {
331
- if (input.status === "completed") {
332
- const [moduleId, modulePath] =
333
- Internal.splitModuleIdAndModulePath(
334
- path as SourcePath
335
- );
336
- if (input.type === "text") {
337
- const patch: PatchJSON = [
338
- {
339
- value: input.data,
340
- op: "replace",
341
- path: `/${modulePath
342
- .split(".")
343
- .map((p) => JSON.parse(p))
344
- .join("/")}`,
345
- },
346
- ];
347
- return valApi.patchModuleContent(moduleId, patch);
348
- } else if (input.type === "image") {
349
- const pathParts = modulePath
350
- .split(".")
351
- .map((p) => JSON.parse(p));
352
-
353
- if (!input?.data || !("src" in input.data)) {
354
- // TODO: We probably need to have an Output type that is different from the Input: we have a union of both cases in Input right now, and we believe we do not want that
355
- console.warn(
356
- "No .src on input provided - this might mean no changes was made"
357
- );
358
- return;
359
- }
360
- const patch: PatchJSON = [
361
- {
362
- value: input.data.src,
363
- op: "replace",
364
- path: `/${pathParts.slice(0, -1).join("/")}/$${
365
- pathParts[pathParts.length - 1]
366
- }`,
367
- },
368
- ];
369
- if (input.data.metadata) {
370
- if (input.data.addMetadata) {
371
- patch.push({
372
- value: input.data.metadata,
373
- op: "add",
374
- path: `/${pathParts.join("/")}/metadata`,
375
- });
376
- } else {
377
- patch.push({
378
- value: input.data.metadata,
379
- op: "replace",
380
- path: `/${pathParts.join("/")}/metadata`,
381
- });
382
- }
383
- }
384
- console.log("patch", patch);
385
- return valApi.patchModuleContent(moduleId, patch);
386
- } else if (input.type === "richtext") {
387
- const patch: PatchJSON = [
388
- {
389
- value: input.data,
390
- op: "replace",
391
- path: `/${modulePath
392
- .split(".")
393
- .map((p) => JSON.parse(p))
394
- .join("/")}`,
395
- },
396
- ];
397
- return valApi.patchModuleContent(moduleId, patch);
398
- }
399
- throw new Error(
400
- `Unsupported input type: ${(input as any).type}`
401
- );
402
- } else {
403
- console.error(
404
- "Submitted incomplete input, ignoring..."
405
- );
406
- return Promise.resolve();
407
- }
408
- })
409
- ).then(() => {
410
- setEditFormPosition(null);
411
- setSelectedSources([]);
412
- setInputs({});
413
- });
414
- },
415
- }) ??
416
- undefined
417
- }
418
- />
419
- </div>
420
- </root.div>
421
- )}
48
+ {ValUI && <ValUI valApi={valApi} valStore={valStore} />}
422
49
  </ValContext.Provider>
423
50
  );
424
51
  }
@@ -0,0 +1,15 @@
1
+ import { lazy } from "react";
2
+ import { ValProviderProps } from "./ValProvider";
3
+
4
+ const ValProvider =
5
+ typeof window === "undefined" && lazy(() => import("./ValProvider"));
6
+
7
+ export function ValProviderWrapper({
8
+ host = "/api/val",
9
+ children,
10
+ }: ValProviderProps) {
11
+ if (!ValProvider) {
12
+ return null;
13
+ }
14
+ return <ValProvider host={host}>{children}</ValProvider>;
15
+ }