@valbuild/react 0.12.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 (60) 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/jest.config.js +5 -0
  33. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.browser.esm.js +2 -1
  34. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.cjs.dev.js +2 -1
  35. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.cjs.prod.js +2 -1
  36. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.esm.js +2 -1
  37. package/jsx-dev-runtime/dist/valbuild-react-jsx-dev-runtime.worker.esm.js +2 -1
  38. package/jsx-runtime/dist/valbuild-react-jsx-runtime.browser.esm.js +2 -1
  39. package/jsx-runtime/dist/valbuild-react-jsx-runtime.cjs.dev.js +2 -1
  40. package/jsx-runtime/dist/valbuild-react-jsx-runtime.cjs.prod.js +2 -1
  41. package/jsx-runtime/dist/valbuild-react-jsx-runtime.esm.js +2 -1
  42. package/jsx-runtime/dist/valbuild-react-jsx-runtime.worker.esm.js +2 -1
  43. package/package.json +9 -6
  44. package/src/AuthStatus.tsx +13 -0
  45. package/src/ShadowRoot.tsx +32 -0
  46. package/src/ValApi.ts +65 -0
  47. package/src/ValProvider.tsx +51 -0
  48. package/src/ValProviderWrapper.tsx +15 -0
  49. package/src/ValRichText.tsx +141 -0
  50. package/src/ValStore.ts +62 -0
  51. package/src/ValUI.tsx +353 -0
  52. package/src/assets.ts +124 -0
  53. package/src/hooks/useVal.test.tsx +57 -0
  54. package/src/hooks/useVal.ts +35 -0
  55. package/src/index.ts +6 -0
  56. package/src/jsx-dev-runtime.js +47 -0
  57. package/src/jsx-namespace.d.ts +46 -0
  58. package/src/jsx-runtime.d.ts +1 -0
  59. package/src/jsx-runtime.dev.d.ts +1 -0
  60. package/src/jsx-runtime.js +52 -0
@@ -0,0 +1,398 @@
1
+ 'use strict';
2
+
3
+ var ValProvider = require('./ValProvider-7364ec46.cjs.dev.js');
4
+ var slicedToArray = require('./slicedToArray-b7cf26e0.cjs.dev.js');
5
+ var React = require('react');
6
+ var ui = require('@valbuild/ui');
7
+ var core = require('@valbuild/core');
8
+ var reactDom = require('react-dom');
9
+ var ReactJSXRuntime = require('react/jsx-runtime');
10
+ require('./unsupportedIterableToArray-9e97e24a.cjs.dev.js');
11
+
12
+ function ownKeys(object, enumerableOnly) {
13
+ var keys = Object.keys(object);
14
+ if (Object.getOwnPropertySymbols) {
15
+ var symbols = Object.getOwnPropertySymbols(object);
16
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
17
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
18
+ })), keys.push.apply(keys, symbols);
19
+ }
20
+ return keys;
21
+ }
22
+ function _objectSpread2(target) {
23
+ for (var i = 1; i < arguments.length; i++) {
24
+ var source = null != arguments[i] ? arguments[i] : {};
25
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
26
+ ValProvider._defineProperty(target, key, source[key]);
27
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
28
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
29
+ });
30
+ }
31
+ return target;
32
+ }
33
+
34
+ function ShadowContent(_ref) {
35
+ var root = _ref.root,
36
+ children = _ref.children;
37
+ return /*#__PURE__*/reactDom.createPortal(children, root);
38
+ }
39
+ var ShadowRoot = function ShadowRoot(_ref2) {
40
+ var children = _ref2.children;
41
+ var node = React.useRef(null);
42
+ var _useState = React.useState(null),
43
+ _useState2 = slicedToArray._slicedToArray(_useState, 2),
44
+ root = _useState2[0],
45
+ setRoot = _useState2[1];
46
+ React.useLayoutEffect(function () {
47
+ if (node.current) {
48
+ var _root = node.current.attachShadow({
49
+ mode: "closed"
50
+ });
51
+ setRoot(_root);
52
+ }
53
+ }, []);
54
+ return /*#__PURE__*/ReactJSXRuntime.jsx("div", {
55
+ ref: node,
56
+ children: root && /*#__PURE__*/ReactJSXRuntime.jsx(ShadowContent, {
57
+ root: root,
58
+ children: children
59
+ })
60
+ });
61
+ };
62
+
63
+ function ValUI(_ref) {
64
+ var _ref3;
65
+ var valApi = _ref.valApi,
66
+ valStore = _ref.valStore;
67
+ var _useState = React.useState([]),
68
+ _useState2 = slicedToArray._slicedToArray(_useState, 2),
69
+ selectedSources = _useState2[0],
70
+ setSelectedSources = _useState2[1];
71
+ var _useState3 = React.useState(false),
72
+ _useState4 = slicedToArray._slicedToArray(_useState3, 2),
73
+ editMode = _useState4[0],
74
+ setEditMode = _useState4[1];
75
+ var _useState5 = React.useState(null),
76
+ _useState6 = slicedToArray._slicedToArray(_useState5, 2),
77
+ editFormPosition = _useState6[0],
78
+ setEditFormPosition = _useState6[1];
79
+ var _useState7 = React.useState({
80
+ status: "not-asked"
81
+ }),
82
+ _useState8 = slicedToArray._slicedToArray(_useState7, 2),
83
+ authentication = _useState8[0],
84
+ setAuthentication = _useState8[1];
85
+ React.useEffect(function () {
86
+ if (editMode) {
87
+ valStore.updateAll();
88
+ }
89
+ }, [editMode]);
90
+ React.useEffect(function () {
91
+ var openValFormListener = undefined;
92
+ var styleElement = undefined;
93
+ var editButtonClickOptions = {
94
+ capture: true,
95
+ passive: true
96
+ };
97
+ if (editMode) {
98
+ // highlight val element by appending a new style
99
+ styleElement = document.createElement("style");
100
+ styleElement.id = "val-edit-highlight";
101
+ styleElement.innerHTML = "\n .val-edit-mode >* [data-val-path] {\n outline: #ffff00 solid 1px;\n cursor: pointer;\n }\n ";
102
+ document.body.appendChild(styleElement);
103
+
104
+ // capture event clicks on data-val-path elements
105
+ openValFormListener = function openValFormListener(e) {
106
+ if (e.target instanceof Element) {
107
+ var _parent;
108
+ var parent = e.target;
109
+ while (parent && parent !== document.body) {
110
+ if (parent.getAttribute("data-val-path")) {
111
+ break;
112
+ }
113
+ if (parent.parentElement) {
114
+ parent = parent.parentElement;
115
+ } else {
116
+ break;
117
+ }
118
+ }
119
+ var valSources = (_parent = parent) === null || _parent === void 0 ? void 0 : _parent.getAttribute("data-val-path");
120
+ if (valSources) {
121
+ e.stopPropagation();
122
+ setSelectedSources(valSources.split(",") /* TODO: just split on commas will not work if path contains , */);
123
+
124
+ setEditFormPosition({
125
+ left: e.pageX,
126
+ top: e.pageY
127
+ });
128
+ // } else if (!isValElement(e.target)) {
129
+ // console.log("click outside", e.target);
130
+ // setEditFormPosition(null);
131
+ // setSelectedSources([]);
132
+ }
133
+ }
134
+ };
135
+
136
+ document.addEventListener("click", openValFormListener, editButtonClickOptions);
137
+ }
138
+ return function () {
139
+ var _styleElement;
140
+ if (openValFormListener) {
141
+ document.removeEventListener("click", openValFormListener, editButtonClickOptions);
142
+ }
143
+ (_styleElement = styleElement) === null || _styleElement === void 0 ? void 0 : _styleElement.remove();
144
+ };
145
+ }, [editMode]);
146
+ React.useEffect(function () {
147
+ if (editMode) {
148
+ document.body.classList.add("val-edit-mode");
149
+ } else {
150
+ document.body.classList.remove("val-edit-mode");
151
+ }
152
+ if (editMode) {
153
+ if (authentication.status !== "authenticated") {
154
+ valApi.getSession().then( /*#__PURE__*/function () {
155
+ var _ref2 = ValProvider._asyncToGenerator( /*#__PURE__*/ValProvider._regeneratorRuntime().mark(function _callee(res) {
156
+ var data, message;
157
+ return ValProvider._regeneratorRuntime().wrap(function _callee$(_context) {
158
+ while (1) switch (_context.prev = _context.next) {
159
+ case 0:
160
+ if (!(res.status === 401)) {
161
+ _context.next = 4;
162
+ break;
163
+ }
164
+ setAuthentication({
165
+ status: "unauthenticated"
166
+ });
167
+ _context.next = 21;
168
+ break;
169
+ case 4:
170
+ if (!res.ok) {
171
+ _context.next = 11;
172
+ break;
173
+ }
174
+ _context.next = 7;
175
+ return res.json();
176
+ case 7:
177
+ data = _context.sent;
178
+ if (data.mode === "local") {
179
+ setAuthentication({
180
+ status: "local"
181
+ });
182
+ } else if (data.mode === "proxy") {
183
+ setAuthentication({
184
+ status: "authenticated"
185
+ });
186
+ } else {
187
+ setAuthentication({
188
+ status: "error",
189
+ message: "Unknown authentication mode"
190
+ });
191
+ }
192
+ _context.next = 21;
193
+ break;
194
+ case 11:
195
+ message = "Unknown error";
196
+ _context.prev = 12;
197
+ _context.next = 15;
198
+ return res.text();
199
+ case 15:
200
+ message = _context.sent;
201
+ _context.next = 20;
202
+ break;
203
+ case 18:
204
+ _context.prev = 18;
205
+ _context.t0 = _context["catch"](12);
206
+ case 20:
207
+ setAuthentication({
208
+ status: "error",
209
+ message: message
210
+ });
211
+ case 21:
212
+ case "end":
213
+ return _context.stop();
214
+ }
215
+ }, _callee, null, [[12, 18]]);
216
+ }));
217
+ return function (_x) {
218
+ return _ref2.apply(this, arguments);
219
+ };
220
+ }())["catch"](function (err) {
221
+ console.error("Failed to fetch session", err);
222
+ setAuthentication({
223
+ status: "error",
224
+ message: "Unknown authentication mode"
225
+ });
226
+ });
227
+ }
228
+ } else {
229
+ if (authentication.status === "error") {
230
+ setAuthentication({
231
+ status: "not-asked"
232
+ });
233
+ }
234
+ }
235
+ }, [editMode, authentication.status]);
236
+ var _useState9 = React.useState(false),
237
+ _useState10 = slicedToArray._slicedToArray(_useState9, 2),
238
+ showEditButton = _useState10[0],
239
+ setShowEditButton = _useState10[1];
240
+ React.useEffect(function () {
241
+ setShowEditButton(true);
242
+ }, []);
243
+ var _useState11 = React.useState({}),
244
+ _useState12 = slicedToArray._slicedToArray(_useState11, 2),
245
+ inputs = _useState12[0],
246
+ setInputs = _useState12[1];
247
+ React.useEffect(function () {
248
+ setInputs({});
249
+ var _iterator = ValProvider._createForOfIteratorHelper(selectedSources),
250
+ _step;
251
+ try {
252
+ var _loop = function _loop() {
253
+ var path = _step.value;
254
+ valApi.getModule(path).then(function (serializedModule) {
255
+ var input;
256
+ if (serializedModule.schema.type === "string" && typeof serializedModule.source === "string") {
257
+ input = {
258
+ status: "completed",
259
+ type: "text",
260
+ data: serializedModule.source
261
+ };
262
+ } else if (serializedModule.schema.type === "richtext" && slicedToArray._typeof(serializedModule.source) === "object") {
263
+ input = {
264
+ status: "completed",
265
+ type: "richtext",
266
+ data: serializedModule.source // TODO: validate
267
+ };
268
+ } else if (serializedModule.schema.type === "image" && serializedModule.source && slicedToArray._typeof(serializedModule.source) === "object" && core.FILE_REF_PROP in serializedModule.source && typeof serializedModule.source[core.FILE_REF_PROP] === "string" && core.VAL_EXTENSION in serializedModule.source && typeof serializedModule.source[core.VAL_EXTENSION] === "string") {
269
+ input = {
270
+ status: "completed",
271
+ type: "image",
272
+ data: core.Internal.convertImageSource(serializedModule.source)
273
+ };
274
+ }
275
+ console.log("input path", path);
276
+ console.log("serialized path", serializedModule.path);
277
+ if (!input) {
278
+ throw new Error("Unsupported module type: ".concat(serializedModule.schema.type));
279
+ }
280
+ setInputs(function (inputs) {
281
+ return _objectSpread2(_objectSpread2({}, inputs), {}, ValProvider._defineProperty({}, serializedModule.path, input));
282
+ });
283
+ });
284
+ };
285
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
286
+ _loop();
287
+ }
288
+ } catch (err) {
289
+ _iterator.e(err);
290
+ } finally {
291
+ _iterator.f();
292
+ }
293
+ }, [selectedSources.join(",")]);
294
+ if (!showEditButton) {
295
+ return null;
296
+ }
297
+ return /*#__PURE__*/ReactJSXRuntime.jsxs(ShadowRoot, {
298
+ children: [/*#__PURE__*/ReactJSXRuntime.jsx("link", {
299
+ rel: "preconnect",
300
+ href: "https://fonts.googleapis.com"
301
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("link", {
302
+ rel: "preconnect",
303
+ href: "https://fonts.gstatic.com",
304
+ crossOrigin: "anonymous"
305
+ }), /*#__PURE__*/ReactJSXRuntime.jsx("link", {
306
+ 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",
307
+ rel: "stylesheet"
308
+ }), /*#__PURE__*/ReactJSXRuntime.jsx(ui.Style, {}), /*#__PURE__*/ReactJSXRuntime.jsx("div", {
309
+ "data-mode": "dark",
310
+ children: /*#__PURE__*/ReactJSXRuntime.jsx(ui.ValOverlay, {
311
+ editMode: editMode,
312
+ setEditMode: setEditMode,
313
+ closeValWindow: function closeValWindow() {
314
+ setEditFormPosition(null);
315
+ setSelectedSources([]);
316
+ setInputs({});
317
+ },
318
+ valWindow: (_ref3 = editFormPosition && {
319
+ position: editFormPosition,
320
+ inputs: inputs,
321
+ onSubmit: function onSubmit(inputs) {
322
+ Promise.all(Object.entries(inputs).map(function (_ref4) {
323
+ var _ref5 = slicedToArray._slicedToArray(_ref4, 2),
324
+ path = _ref5[0],
325
+ input = _ref5[1];
326
+ if (input.status === "completed") {
327
+ var _Internal$splitModule = core.Internal.splitModuleIdAndModulePath(path),
328
+ _Internal$splitModule2 = slicedToArray._slicedToArray(_Internal$splitModule, 2),
329
+ moduleId = _Internal$splitModule2[0],
330
+ modulePath = _Internal$splitModule2[1];
331
+ if (input.type === "text") {
332
+ var patch = [{
333
+ value: input.data,
334
+ op: "replace",
335
+ path: "/".concat(modulePath.split(".").map(function (p) {
336
+ return JSON.parse(p);
337
+ }).join("/"))
338
+ }];
339
+ return valApi.patchModuleContent(moduleId, patch);
340
+ } else if (input.type === "image") {
341
+ var pathParts = modulePath.split(".").map(function (p) {
342
+ return JSON.parse(p);
343
+ });
344
+ if (!(input !== null && input !== void 0 && input.data) || !("src" in input.data)) {
345
+ // 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
346
+ console.warn("No .src on input provided - this might mean no changes was made");
347
+ return;
348
+ }
349
+ var _patch = [{
350
+ value: input.data.src,
351
+ op: "replace",
352
+ path: "/".concat(pathParts.slice(0, -1).join("/"), "/$").concat(pathParts[pathParts.length - 1])
353
+ }];
354
+ if (input.data.metadata) {
355
+ if (input.data.addMetadata) {
356
+ _patch.push({
357
+ value: input.data.metadata,
358
+ op: "add",
359
+ path: "/".concat(pathParts.join("/"), "/metadata")
360
+ });
361
+ } else {
362
+ _patch.push({
363
+ value: input.data.metadata,
364
+ op: "replace",
365
+ path: "/".concat(pathParts.join("/"), "/metadata")
366
+ });
367
+ }
368
+ }
369
+ console.log("patch", _patch);
370
+ return valApi.patchModuleContent(moduleId, _patch);
371
+ } else if (input.type === "richtext") {
372
+ var _patch2 = [{
373
+ value: input.data,
374
+ op: "replace",
375
+ path: "/".concat(modulePath.split(".").map(function (p) {
376
+ return JSON.parse(p);
377
+ }).join("/"))
378
+ }];
379
+ return valApi.patchModuleContent(moduleId, _patch2);
380
+ }
381
+ throw new Error("Unsupported input type: ".concat(input.type));
382
+ } else {
383
+ console.error("Submitted incomplete input, ignoring...");
384
+ return Promise.resolve();
385
+ }
386
+ })).then(function () {
387
+ setEditFormPosition(null);
388
+ setSelectedSources([]);
389
+ setInputs({});
390
+ });
391
+ }
392
+ }) !== null && _ref3 !== void 0 ? _ref3 : undefined
393
+ })
394
+ })]
395
+ });
396
+ }
397
+
398
+ exports["default"] = ValUI;