@valbuild/shared 0.123.0 → 0.123.2
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/CHANGELOG.md +29 -0
- package/client/dist/valbuild-shared-client.cjs.d.ts +2 -0
- package/client/dist/valbuild-shared-client.cjs.dev.js +22 -0
- package/client/dist/valbuild-shared-client.cjs.js +7 -0
- package/client/dist/valbuild-shared-client.cjs.prod.js +22 -0
- package/client/dist/valbuild-shared-client.esm.js +2 -0
- package/client/package.json +4 -0
- package/dist/declarations/src/client/index.d.ts +21 -0
- package/dist/getSitemapTree-77b1203a.cjs.prod.js +389 -0
- package/dist/getSitemapTree-a0c9a0e3.cjs.dev.js +389 -0
- package/dist/getSitemapTree-d3e98a2e.esm.js +369 -0
- package/internal/dist/valbuild-shared-internal.cjs.dev.js +89 -454
- package/internal/dist/valbuild-shared-internal.cjs.prod.js +89 -454
- package/internal/dist/valbuild-shared-internal.esm.js +4 -368
- package/package.json +9 -1
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { Internal } from '@valbuild/core';
|
|
2
|
+
|
|
3
|
+
var VAL_CONFIG_SESSION_STORAGE_KEY = "val-config";
|
|
4
|
+
var VAL_THEME_SESSION_STORAGE_KEY = "val-theme";
|
|
5
|
+
var VAL_AI_SESSION_STORAGE_KEY = "val-ai-session";
|
|
6
|
+
|
|
7
|
+
function _typeof(o) {
|
|
8
|
+
"@babel/helpers - typeof";
|
|
9
|
+
|
|
10
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
11
|
+
return typeof o;
|
|
12
|
+
} : function (o) {
|
|
13
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
14
|
+
}, _typeof(o);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The protocol between the studio's canvas and the page inside it.
|
|
19
|
+
*
|
|
20
|
+
* The canvas shows the running site in a frame. That frame is a different
|
|
21
|
+
* document — the customer's app, rendered by their components — so the studio
|
|
22
|
+
* cannot reach into it, and it cannot reach out. Everything they say to each
|
|
23
|
+
* other goes through `postMessage`, and this is the vocabulary.
|
|
24
|
+
*
|
|
25
|
+
* It lives in `shared` because both halves have to agree on it: the page half
|
|
26
|
+
* ships in `@valbuild/next`, the studio half in `@valbuild/ui`, and a message
|
|
27
|
+
* shape defined twice is a message shape that will differ once.
|
|
28
|
+
*
|
|
29
|
+
* ## Why a query parameter turns it on
|
|
30
|
+
*
|
|
31
|
+
* The frame has to render the page as Val sees it — draft content, with
|
|
32
|
+
* `data-val-path` on every element Val tracks — while *not* showing Val's own
|
|
33
|
+
* overlay, because the studio is already the UI. Nothing about the cookies can
|
|
34
|
+
* express that: they are per-origin, and the studio and the page share an
|
|
35
|
+
* origin, so a cookie would change the customer's own tabs too. A parameter on
|
|
36
|
+
* the frame's URL is scoped to exactly the one document that needs it.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/** Marks a page load as the studio's canvas frame. */
|
|
40
|
+
var VAL_CANVAS_PARAM = "val_canvas";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Every message is tagged with this, and the tag is checked on receipt.
|
|
44
|
+
*
|
|
45
|
+
* A window receives messages from anything that has a handle on it —
|
|
46
|
+
* extensions, embeds, other frames — and `event.data` is whatever they chose to
|
|
47
|
+
* send. Checking a discriminator before reading anything else is what keeps a
|
|
48
|
+
* stray message from being read as a canvas instruction.
|
|
49
|
+
*/
|
|
50
|
+
var VAL_CANVAS_MESSAGE = "val-canvas";
|
|
51
|
+
|
|
52
|
+
/** One element on the page that Val has a path for. */
|
|
53
|
+
|
|
54
|
+
/** What the page tells the studio. */
|
|
55
|
+
|
|
56
|
+
/** What the studio tells the page. */
|
|
57
|
+
|
|
58
|
+
/** Narrow an unknown `event.data` to a message from the page. */
|
|
59
|
+
function isValCanvasPageMessage(data) {
|
|
60
|
+
if (_typeof(data) !== "object" || data === null) return false;
|
|
61
|
+
var message = data;
|
|
62
|
+
if (message.val !== VAL_CANVAS_MESSAGE) return false;
|
|
63
|
+
return message.type === "ready" || message.type === "elements" || message.type === "clicked" || message.type === "pinch" || message.type === "zoom" || message.type === "refreshing";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Narrow an unknown `event.data` to a message from the studio. */
|
|
67
|
+
function isValCanvasStudioMessage(data) {
|
|
68
|
+
if (_typeof(data) !== "object" || data === null) return false;
|
|
69
|
+
var message = data;
|
|
70
|
+
if (message.val !== VAL_CANVAS_MESSAGE) return false;
|
|
71
|
+
return message.type === "rescan" || message.type === "highlight" || message.type === "setPicking" || message.type === "sourceUpdate" || message.type === "sourcesSynced";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Whether this document was loaded as the studio's canvas frame.
|
|
76
|
+
*
|
|
77
|
+
* Reads the location directly rather than taking it as an argument, because
|
|
78
|
+
* every caller wants the answer for the document it is running in, and passing
|
|
79
|
+
* it around is how one of them ends up asking about the wrong one.
|
|
80
|
+
*/
|
|
81
|
+
function isValCanvasFrame(search) {
|
|
82
|
+
try {
|
|
83
|
+
return new URLSearchParams(search).get(VAL_CANVAS_PARAM) !== null;
|
|
84
|
+
} catch (_unused) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Add the canvas marker to a page URL. */
|
|
90
|
+
function withValCanvasParam(url) {
|
|
91
|
+
var separator = url.includes("?") ? "&" : "?";
|
|
92
|
+
return "".concat(url).concat(separator).concat(VAL_CANVAS_PARAM, "=1");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function parseRoutePattern(pattern) {
|
|
96
|
+
if (pattern === "" || pattern === "/") {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
var p = pattern.startsWith("/") ? pattern.slice(1) : pattern;
|
|
100
|
+
return p.split("/").map(function (part) {
|
|
101
|
+
var isOptionalParam = part.startsWith("[[") && part.endsWith("]]");
|
|
102
|
+
var isNonOptionalParam = part.startsWith("[") && part.endsWith("]");
|
|
103
|
+
var nameStart = isOptionalParam ? 2 : isNonOptionalParam ? 1 : 0;
|
|
104
|
+
var nameEnd = part.indexOf("]", nameStart);
|
|
105
|
+
var isArrayParam = part.slice(nameStart, nameStart + 3) === "...";
|
|
106
|
+
var paramName = part.slice(nameStart + (isArrayParam ? 3 : 0), nameEnd);
|
|
107
|
+
if (isOptionalParam || isNonOptionalParam) {
|
|
108
|
+
if (isArrayParam) {
|
|
109
|
+
return {
|
|
110
|
+
type: "array-param",
|
|
111
|
+
paramName: paramName,
|
|
112
|
+
optional: isOptionalParam
|
|
113
|
+
};
|
|
114
|
+
} else {
|
|
115
|
+
return {
|
|
116
|
+
type: "string-param",
|
|
117
|
+
paramName: paramName,
|
|
118
|
+
optional: isOptionalParam
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
type: "literal",
|
|
124
|
+
name: part
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function getNextAppRouterSourceFolder(moduleFilePath) {
|
|
130
|
+
if (moduleFilePath.startsWith("/app")) {
|
|
131
|
+
return "/app";
|
|
132
|
+
} else if (moduleFilePath.startsWith("/src/app")) {
|
|
133
|
+
return "/src/app";
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function _toPrimitive(t, r) {
|
|
139
|
+
if ("object" != typeof t || !t) return t;
|
|
140
|
+
var e = t[Symbol.toPrimitive];
|
|
141
|
+
if (void 0 !== e) {
|
|
142
|
+
var i = e.call(t, r || "default");
|
|
143
|
+
if ("object" != typeof i) return i;
|
|
144
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
145
|
+
}
|
|
146
|
+
return ("string" === r ? String : Number)(t);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function _toPropertyKey(t) {
|
|
150
|
+
var i = _toPrimitive(t, "string");
|
|
151
|
+
return "symbol" == typeof i ? i : i + "";
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function _defineProperty(e, r, t) {
|
|
155
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
156
|
+
value: t,
|
|
157
|
+
enumerable: !0,
|
|
158
|
+
configurable: !0,
|
|
159
|
+
writable: !0
|
|
160
|
+
}) : e[r] = t, e;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function ownKeys(e, r) {
|
|
164
|
+
var t = Object.keys(e);
|
|
165
|
+
if (Object.getOwnPropertySymbols) {
|
|
166
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
167
|
+
r && (o = o.filter(function (r) {
|
|
168
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
169
|
+
})), t.push.apply(t, o);
|
|
170
|
+
}
|
|
171
|
+
return t;
|
|
172
|
+
}
|
|
173
|
+
function _objectSpread2(e) {
|
|
174
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
175
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
176
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
177
|
+
_defineProperty(e, r, t[r]);
|
|
178
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
179
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return e;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function _arrayLikeToArray(r, a) {
|
|
186
|
+
(null == a || a > r.length) && (a = r.length);
|
|
187
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
188
|
+
return n;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function _unsupportedIterableToArray(r, a) {
|
|
192
|
+
if (r) {
|
|
193
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
194
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
195
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function _createForOfIteratorHelper(r, e) {
|
|
200
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
201
|
+
if (!t) {
|
|
202
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
|
|
203
|
+
t && (r = t);
|
|
204
|
+
var n = 0,
|
|
205
|
+
F = function () {};
|
|
206
|
+
return {
|
|
207
|
+
s: F,
|
|
208
|
+
n: function () {
|
|
209
|
+
return n >= r.length ? {
|
|
210
|
+
done: !0
|
|
211
|
+
} : {
|
|
212
|
+
done: !1,
|
|
213
|
+
value: r[n++]
|
|
214
|
+
};
|
|
215
|
+
},
|
|
216
|
+
e: function (r) {
|
|
217
|
+
throw r;
|
|
218
|
+
},
|
|
219
|
+
f: F
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
223
|
+
}
|
|
224
|
+
var o,
|
|
225
|
+
a = !0,
|
|
226
|
+
u = !1;
|
|
227
|
+
return {
|
|
228
|
+
s: function () {
|
|
229
|
+
t = t.call(r);
|
|
230
|
+
},
|
|
231
|
+
n: function () {
|
|
232
|
+
var r = t.next();
|
|
233
|
+
return a = r.done, r;
|
|
234
|
+
},
|
|
235
|
+
e: function (r) {
|
|
236
|
+
u = !0, o = r;
|
|
237
|
+
},
|
|
238
|
+
f: function () {
|
|
239
|
+
try {
|
|
240
|
+
a || null == t.return || t.return();
|
|
241
|
+
} finally {
|
|
242
|
+
if (u) throw o;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Strictly speaking this should be in the next package but it's shared, because we want to use it in the ui package. We want to resolve that somehow
|
|
249
|
+
function getNextAppRouterSitemapTree(srcFolder, paths) {
|
|
250
|
+
var root = {
|
|
251
|
+
type: "node",
|
|
252
|
+
name: "/",
|
|
253
|
+
children: []
|
|
254
|
+
};
|
|
255
|
+
var _iterator = _createForOfIteratorHelper(paths),
|
|
256
|
+
_step;
|
|
257
|
+
try {
|
|
258
|
+
var _loop = function _loop() {
|
|
259
|
+
var path = _step.value;
|
|
260
|
+
var urlPath = path.urlPath,
|
|
261
|
+
moduleFilePathS = path.moduleFilePath;
|
|
262
|
+
var moduleFilePath = moduleFilePathS;
|
|
263
|
+
if (!urlPath.startsWith("/")) {
|
|
264
|
+
console.error("urlPath must start with /: ".concat(urlPath));
|
|
265
|
+
return 0; // continue
|
|
266
|
+
}
|
|
267
|
+
var pattern = getPatternFromModuleFilePath(moduleFilePath, srcFolder);
|
|
268
|
+
if (urlPath === "/") {
|
|
269
|
+
var fullPath = "/";
|
|
270
|
+
root.pattern = getPatternFromModuleFilePath(moduleFilePath, srcFolder);
|
|
271
|
+
root.page = {
|
|
272
|
+
fullPath: fullPath
|
|
273
|
+
};
|
|
274
|
+
root.sourcePath = Internal.joinModuleFilePathAndModulePath(moduleFilePath, "\"".concat(fullPath, "\""));
|
|
275
|
+
root.moduleFilePath = moduleFilePath;
|
|
276
|
+
return 0; // continue
|
|
277
|
+
}
|
|
278
|
+
var pathParts = urlPath.split("/").slice(1).filter(Boolean);
|
|
279
|
+
var currentNode = root;
|
|
280
|
+
pathParts.forEach(function (part, index) {
|
|
281
|
+
var hasChildren = index < pathParts.length - 1;
|
|
282
|
+
var isLast = index === pathParts.length - 1;
|
|
283
|
+
var fullPath = "/" + pathParts.slice(0, index + 1).join("/");
|
|
284
|
+
var sourcePath = Internal.joinModuleFilePathAndModulePath(moduleFilePath, "\"".concat(fullPath, "\""));
|
|
285
|
+
var node = hasChildren ? _objectSpread2(_objectSpread2({
|
|
286
|
+
type: "node",
|
|
287
|
+
name: part,
|
|
288
|
+
pattern: pattern,
|
|
289
|
+
moduleFilePath: moduleFilePath
|
|
290
|
+
}, isLast ? {
|
|
291
|
+
page: {
|
|
292
|
+
fullPath: fullPath
|
|
293
|
+
},
|
|
294
|
+
sourcePath: sourcePath
|
|
295
|
+
} : {}), {}, {
|
|
296
|
+
children: []
|
|
297
|
+
}) : {
|
|
298
|
+
type: "leaf",
|
|
299
|
+
name: part,
|
|
300
|
+
pattern: pattern,
|
|
301
|
+
fullPath: fullPath,
|
|
302
|
+
moduleFilePath: moduleFilePath,
|
|
303
|
+
sourcePath: sourcePath,
|
|
304
|
+
children: []
|
|
305
|
+
};
|
|
306
|
+
var existingNodeIndex = currentNode.children.findIndex(function (node) {
|
|
307
|
+
return node.name === part;
|
|
308
|
+
});
|
|
309
|
+
if (existingNodeIndex === -1) {
|
|
310
|
+
currentNode.children.push(node);
|
|
311
|
+
if (currentNode.type === "node") {
|
|
312
|
+
currentNode.isLinear = true;
|
|
313
|
+
if (currentNode.children.length > 1) {
|
|
314
|
+
delete currentNode.isLinear;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
currentNode = node;
|
|
318
|
+
} else {
|
|
319
|
+
var existingNode = currentNode.children[existingNodeIndex];
|
|
320
|
+
if (existingNode.type === "leaf" && hasChildren) {
|
|
321
|
+
var _sourcePath = existingNode.sourcePath;
|
|
322
|
+
// convert leaf to node
|
|
323
|
+
currentNode.children[existingNodeIndex] = {
|
|
324
|
+
type: "node",
|
|
325
|
+
name: part,
|
|
326
|
+
pattern: pattern,
|
|
327
|
+
page: {
|
|
328
|
+
fullPath: fullPath
|
|
329
|
+
},
|
|
330
|
+
sourcePath: _sourcePath,
|
|
331
|
+
children: existingNode.children,
|
|
332
|
+
moduleFilePath: moduleFilePath
|
|
333
|
+
};
|
|
334
|
+
currentNode = currentNode.children[existingNodeIndex];
|
|
335
|
+
} else if (existingNode.type === "node" && isLast) {
|
|
336
|
+
// add page, source path and module file path
|
|
337
|
+
existingNode.page = {
|
|
338
|
+
fullPath: fullPath
|
|
339
|
+
};
|
|
340
|
+
existingNode.sourcePath = sourcePath;
|
|
341
|
+
existingNode.moduleFilePath = moduleFilePath;
|
|
342
|
+
existingNode.pattern = pattern;
|
|
343
|
+
} else {
|
|
344
|
+
currentNode = currentNode.children[existingNodeIndex];
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
},
|
|
349
|
+
_ret;
|
|
350
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
351
|
+
_ret = _loop();
|
|
352
|
+
if (_ret === 0) continue;
|
|
353
|
+
}
|
|
354
|
+
} catch (err) {
|
|
355
|
+
_iterator.e(err);
|
|
356
|
+
} finally {
|
|
357
|
+
_iterator.f();
|
|
358
|
+
}
|
|
359
|
+
return root;
|
|
360
|
+
}
|
|
361
|
+
function getPatternFromModuleFilePath(moduleFilePath, srcFolder) {
|
|
362
|
+
return moduleFilePath.replace(srcFolder, "")
|
|
363
|
+
// TODO: hacky!
|
|
364
|
+
// remove route groups
|
|
365
|
+
.replace(/\/\([^/]+\)/g, "") // not sure we want to do this?
|
|
366
|
+
.replace("/page.val.ts", "").replace("/page.val.js", "");
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export { VAL_CONFIG_SESSION_STORAGE_KEY as V, _createForOfIteratorHelper as _, VAL_THEME_SESSION_STORAGE_KEY as a, VAL_AI_SESSION_STORAGE_KEY as b, VAL_CANVAS_PARAM as c, VAL_CANVAS_MESSAGE as d, isValCanvasStudioMessage as e, isValCanvasFrame as f, getNextAppRouterSourceFolder as g, getNextAppRouterSitemapTree as h, isValCanvasPageMessage as i, getPatternFromModuleFilePath as j, _objectSpread2 as k, _arrayLikeToArray as l, _unsupportedIterableToArray as m, _defineProperty as n, _typeof as o, parseRoutePattern as p, withValCanvasParam as w };
|