@xosen/site-sdk 0.0.7 → 0.0.8
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.js +65 -63
- package/package.json +1 -1
- package/src/composables/useLivePreview.ts +2 -0
- package/src/index.ts +2 -1
- package/src/types/blocks.ts +1 -1
- package/src/types/config.ts +15 -0
- package/src/worker/types.ts +6 -0
package/dist/index.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { ref as y, onMounted as T, watch as
|
|
2
|
-
import { useI18n as
|
|
3
|
-
function
|
|
4
|
-
const t = y(null), { locale: e } =
|
|
1
|
+
import { ref as y, onMounted as T, watch as $, computed as u, onUnmounted as b } from "vue";
|
|
2
|
+
import { useI18n as C } from "vue-i18n";
|
|
3
|
+
function R(n, o) {
|
|
4
|
+
const t = y(null), { locale: e } = C(), a = window.__SITE_DATA__, s = a?.locale;
|
|
5
5
|
function c() {
|
|
6
|
-
return a && a[
|
|
6
|
+
return a && a[n] && e.value === s ? (t.value = a[n], !0) : !1;
|
|
7
7
|
}
|
|
8
8
|
async function h() {
|
|
9
9
|
try {
|
|
10
|
-
const r = await fetch(`/api/content/${
|
|
10
|
+
const r = await fetch(`/api/content/${o}:${e.value}`);
|
|
11
11
|
r.ok && (t.value = await r.json());
|
|
12
12
|
} catch {
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
return T(() => {
|
|
16
16
|
c() || h();
|
|
17
|
-
}),
|
|
17
|
+
}), $(e, () => {
|
|
18
18
|
h();
|
|
19
19
|
}), t;
|
|
20
20
|
}
|
|
21
21
|
function O() {
|
|
22
|
-
const
|
|
23
|
-
function
|
|
24
|
-
const
|
|
25
|
-
return typeof
|
|
22
|
+
const n = window.__SITE_DATA__, o = u(() => n?.config || null), t = u(() => n?.components || {}), e = u(() => o.value?.navigation || []), a = u(() => o.value?.locales || []), s = u(() => o.value?.defaultLocale || "en"), c = u(() => o.value?.branding || {}), h = u(() => o.value?.footer || {}), r = u(() => o.value?.features || {});
|
|
23
|
+
function f(g) {
|
|
24
|
+
const p = r.value[g];
|
|
25
|
+
return typeof p == "boolean" ? p : typeof p == "object";
|
|
26
26
|
}
|
|
27
27
|
return {
|
|
28
|
-
config:
|
|
28
|
+
config: o,
|
|
29
29
|
components: t,
|
|
30
30
|
navigation: e,
|
|
31
31
|
locales: a,
|
|
@@ -33,37 +33,37 @@ function O() {
|
|
|
33
33
|
branding: c,
|
|
34
34
|
footer: h,
|
|
35
35
|
features: r,
|
|
36
|
-
hasFeature:
|
|
36
|
+
hasFeature: f
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
|
-
function j(
|
|
40
|
-
function
|
|
39
|
+
function j(n) {
|
|
40
|
+
function o(t) {
|
|
41
41
|
const e = document.documentElement;
|
|
42
42
|
if (t.colors)
|
|
43
43
|
for (const [a, s] of Object.entries(t.colors))
|
|
44
|
-
s && e.style.setProperty(`--x-color-${
|
|
44
|
+
s && e.style.setProperty(`--x-color-${x(a)}`, s);
|
|
45
45
|
t.typography?.fontFamily && e.style.setProperty("--x-font-family", t.typography.fontFamily), t.typography?.headingFont && e.style.setProperty("--x-font-heading", t.typography.headingFont), t.typography?.baseFontSize && e.style.setProperty("--x-font-size", t.typography.baseFontSize), t.shape?.borderRadius && e.style.setProperty("--x-border-radius", t.shape.borderRadius), t.shape?.maxWidth && e.style.setProperty("--x-max-width", t.shape.maxWidth);
|
|
46
46
|
}
|
|
47
47
|
T(() => {
|
|
48
|
-
if (
|
|
49
|
-
n
|
|
48
|
+
if (n) {
|
|
49
|
+
o(n);
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
const e = window.__SITE_DATA__?.skin;
|
|
53
|
-
e && (e.colors || e.typography || e.shape) &&
|
|
53
|
+
e && (e.colors || e.typography || e.shape) && o({
|
|
54
54
|
colors: e.colors || {},
|
|
55
55
|
typography: e.typography || {},
|
|
56
56
|
shape: e.shape || {}
|
|
57
57
|
});
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
|
-
function
|
|
61
|
-
return
|
|
60
|
+
function x(n) {
|
|
61
|
+
return n.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
62
62
|
}
|
|
63
63
|
function F() {
|
|
64
|
-
const
|
|
64
|
+
const o = window.__SITE_DATA__?.apiBase || "";
|
|
65
65
|
async function t(s) {
|
|
66
|
-
const c = await fetch(`${
|
|
66
|
+
const c = await fetch(`${o}${s}`);
|
|
67
67
|
if (!c.ok)
|
|
68
68
|
throw new Error(`API error: ${c.status}`);
|
|
69
69
|
return c.json();
|
|
@@ -87,43 +87,43 @@ function S() {
|
|
|
87
87
|
return !0;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
function D(
|
|
91
|
-
S() &&
|
|
90
|
+
function D(n) {
|
|
91
|
+
S() && n.beforeEach((o, t) => !t.name);
|
|
92
92
|
}
|
|
93
93
|
function U() {
|
|
94
|
-
const
|
|
94
|
+
const n = y(null), o = y(S());
|
|
95
95
|
function t(e) {
|
|
96
96
|
const a = e.data;
|
|
97
|
-
a?.type === "xosen-preview-update" && a.page && (
|
|
97
|
+
a?.type === "xosen-preview-update" && a.page && (n.value = a.page, o.value = !0);
|
|
98
98
|
}
|
|
99
99
|
return T(() => {
|
|
100
100
|
window.addEventListener("message", t);
|
|
101
101
|
}), b(() => {
|
|
102
102
|
window.removeEventListener("message", t);
|
|
103
103
|
}), {
|
|
104
|
-
previewPage:
|
|
105
|
-
isPreviewMode:
|
|
104
|
+
previewPage: n,
|
|
105
|
+
isPreviewMode: o
|
|
106
106
|
};
|
|
107
107
|
}
|
|
108
|
-
const
|
|
109
|
-
function N(
|
|
110
|
-
const e = new URL(
|
|
111
|
-
if (e &&
|
|
112
|
-
if (
|
|
113
|
-
const a =
|
|
114
|
-
for (const s of
|
|
108
|
+
const A = /\.(js|css|png|jpg|jpeg|gif|svg|ico|woff2?|ttf|eot|webp|avif|map|json|txt|xml|webmanifest)$/;
|
|
109
|
+
function N(n, o) {
|
|
110
|
+
const e = new URL(n.url).searchParams.get("lang");
|
|
111
|
+
if (e && o.supportedLocales?.includes(e)) return e;
|
|
112
|
+
if (o.supportedLocales?.length) {
|
|
113
|
+
const a = n.headers.get("Accept-Language") || "";
|
|
114
|
+
for (const s of o.supportedLocales)
|
|
115
115
|
if (a.includes(s)) return s;
|
|
116
116
|
}
|
|
117
|
-
return
|
|
117
|
+
return o.defaultLocale;
|
|
118
118
|
}
|
|
119
|
-
function I(
|
|
120
|
-
if (!
|
|
121
|
-
const e = decodeURIComponent(
|
|
119
|
+
function I(n, o, t) {
|
|
120
|
+
if (!n.pathname.startsWith("/api/content/")) return null;
|
|
121
|
+
const e = decodeURIComponent(n.pathname.replace("/api/content/", ""));
|
|
122
122
|
return e ? (async () => {
|
|
123
|
-
let a = await
|
|
123
|
+
let a = await o.SITE_CONTENT.get(e);
|
|
124
124
|
if (!a) {
|
|
125
125
|
const s = e.lastIndexOf(":");
|
|
126
|
-
s > 0 && e.slice(s + 1) !== t && (a = await
|
|
126
|
+
s > 0 && e.slice(s + 1) !== t && (a = await o.SITE_CONTENT.get(e.slice(0, s + 1) + t));
|
|
127
127
|
}
|
|
128
128
|
return a ? new Response(a, {
|
|
129
129
|
headers: {
|
|
@@ -134,30 +134,30 @@ function I(o, n, t) {
|
|
|
134
134
|
}) : Response.json({ error: "Not found" }, { status: 404 });
|
|
135
135
|
})() : Promise.resolve(Response.json({ error: "Key is required" }, { status: 400 }));
|
|
136
136
|
}
|
|
137
|
-
function _(
|
|
138
|
-
let e =
|
|
139
|
-
if (
|
|
137
|
+
function _(n, o, t) {
|
|
138
|
+
let e = n;
|
|
139
|
+
if (o.title) {
|
|
140
140
|
const a = t ? ` — ${t}` : "";
|
|
141
|
-
e = e.replace(/<title>[^<]*<\/title>/, `<title>${
|
|
141
|
+
e = e.replace(/<title>[^<]*<\/title>/, `<title>${o.title}${a}</title>`);
|
|
142
142
|
}
|
|
143
|
-
return
|
|
143
|
+
return o.meta?.description && (e = e.replace(/(<meta\s+name="description"\s+content=")[^"]*(")/, `$1${o.meta.description}$2`)), o.meta?.ogTitle && (e = e.replace(/(<meta\s+property="og:title"\s+content=")[^"]*(")/, `$1${o.meta.ogTitle}$2`)), o.html && (e = e.replace('<div id="app"></div>', `<div id="app">${o.html}</div>`)), e;
|
|
144
144
|
}
|
|
145
|
-
function J(
|
|
145
|
+
function J(n) {
|
|
146
146
|
return {
|
|
147
|
-
async fetch(
|
|
148
|
-
const e = new URL(
|
|
147
|
+
async fetch(o, t) {
|
|
148
|
+
const e = new URL(o.url), a = I(e, t, n.defaultLocale);
|
|
149
149
|
if (a) return a;
|
|
150
|
-
if (
|
|
151
|
-
return t.ASSETS.fetch(
|
|
152
|
-
const s = N(
|
|
150
|
+
if (A.test(e.pathname))
|
|
151
|
+
return t.ASSETS.fetch(o);
|
|
152
|
+
const s = N(o, n), c = new URL("/index.html", o.url);
|
|
153
153
|
let r = await (await t.ASSETS.fetch(new Request(c))).text();
|
|
154
|
-
const
|
|
154
|
+
const f = n.defaultLocale, [g, p] = await Promise.all([
|
|
155
155
|
t.SITE_CONTENT.get("config"),
|
|
156
156
|
t.SITE_CONTENT.get(`content:${s}`)
|
|
157
157
|
]), v = g ? JSON.parse(g) : {};
|
|
158
|
-
let d =
|
|
159
|
-
if (!
|
|
160
|
-
const i = await t.SITE_CONTENT.get(`content:${
|
|
158
|
+
let d = p ? JSON.parse(p) : {};
|
|
159
|
+
if (!p && s !== f) {
|
|
160
|
+
const i = await t.SITE_CONTENT.get(`content:${f}`);
|
|
161
161
|
i && (d = JSON.parse(i));
|
|
162
162
|
}
|
|
163
163
|
const m = e.pathname.match(/^\/p\/(.+)$/);
|
|
@@ -165,7 +165,7 @@ function J(o) {
|
|
|
165
165
|
const i = m[1];
|
|
166
166
|
if (!d[i]) {
|
|
167
167
|
let l = await t.SITE_CONTENT.get(`page:${i}:${s}`);
|
|
168
|
-
!l && s !==
|
|
168
|
+
!l && s !== f && (l = await t.SITE_CONTENT.get(`page:${i}:${f}`)), l && (d[i] = JSON.parse(l));
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
const w = {
|
|
@@ -176,11 +176,11 @@ function J(o) {
|
|
|
176
176
|
w[`page:${i}`] = l;
|
|
177
177
|
if (m) {
|
|
178
178
|
const i = m[1], l = w[`page:${i}`];
|
|
179
|
-
l && (r = _(r, l,
|
|
179
|
+
l && (r = _(r, l, n.siteName));
|
|
180
180
|
}
|
|
181
|
-
e.pathname === "/" && d.home && (r = _(r, d.home,
|
|
182
|
-
const
|
|
183
|
-
return r = r.replace("</head>", `${
|
|
181
|
+
e.pathname === "/" && d.home && (r = _(r, d.home, n.siteName));
|
|
182
|
+
const E = `<script>window.__SITE_DATA__ = ${JSON.stringify(w)};<\/script>`;
|
|
183
|
+
return r = r.replace("</head>", `${E}
|
|
184
184
|
</head>`), new Response(r, {
|
|
185
185
|
headers: {
|
|
186
186
|
"Content-Type": "text/html;charset=utf-8",
|
|
@@ -190,13 +190,15 @@ function J(o) {
|
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
192
|
}
|
|
193
|
+
const M = /* @__PURE__ */ Symbol("pageContext");
|
|
193
194
|
export {
|
|
195
|
+
M as PAGE_CONTEXT_KEY,
|
|
194
196
|
J as createSiteWorker,
|
|
195
197
|
S as isInPreview,
|
|
196
198
|
D as setupPreviewRouter,
|
|
197
199
|
U as useLivePreview,
|
|
198
200
|
F as useSiteApi,
|
|
199
201
|
O as useSiteConfig,
|
|
200
|
-
|
|
202
|
+
R as useSiteData,
|
|
201
203
|
j as useSkin
|
|
202
204
|
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -25,5 +25,6 @@ export type {
|
|
|
25
25
|
GalleryBlockData,
|
|
26
26
|
MapBlockData,
|
|
27
27
|
} from './types/blocks.js';
|
|
28
|
-
export type { SiteConfig, NavItem, PageConfig, SiteData } from './types/config.js';
|
|
28
|
+
export type { SiteConfig, NavItem, PageConfig, SiteData, PageContext } from './types/config.js';
|
|
29
|
+
export { PAGE_CONTEXT_KEY } from './types/config.js';
|
|
29
30
|
export type { Skin } from './types/skin.js';
|
package/src/types/blocks.ts
CHANGED
|
@@ -82,7 +82,7 @@ export interface ContactsBlockData {
|
|
|
82
82
|
|
|
83
83
|
export interface GalleryBlockData {
|
|
84
84
|
title?: string;
|
|
85
|
-
images: Array<{
|
|
85
|
+
images: Array<{ url: string; alt?: string; caption?: string }>;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
export interface MapBlockData {
|
package/src/types/config.ts
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
import type { InjectionKey, Ref } from 'vue';
|
|
1
2
|
import type { Block } from './blocks.js';
|
|
2
3
|
|
|
4
|
+
export interface PageContext {
|
|
5
|
+
title?: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
author?: string;
|
|
8
|
+
featuredImage?: string;
|
|
9
|
+
excerpt?: string;
|
|
10
|
+
isGated?: boolean;
|
|
11
|
+
termIds?: string[];
|
|
12
|
+
layout?: string;
|
|
13
|
+
publishedAt?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const PAGE_CONTEXT_KEY: InjectionKey<Ref<PageContext>> = Symbol('pageContext');
|
|
17
|
+
|
|
3
18
|
export interface SiteConfig {
|
|
4
19
|
template: string;
|
|
5
20
|
version: string;
|