@kyro-cms/studio 0.0.0-stage → 0.2.0
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/README.md +159 -2
- package/components/KyroStudioPage.astro +7 -0
- package/package.json +32 -3
- package/src/blocks.tsx +709 -0
- package/src/bridge.ts +55 -0
- package/src/fonts.ts +49 -0
- package/src/forms.tsx +194 -0
- package/src/image-framing.ts +33 -0
- package/src/index.ts +18 -0
- package/src/layout-styles.ts +93 -0
- package/src/picked-icon.tsx +26 -0
- package/src/primitives.tsx +144 -0
- package/src/react.tsx +306 -0
- package/src/references.ts +71 -0
- package/src/styles.ts +8 -0
- package/src/types.ts +36 -0
package/src/bridge.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { StudioBlock } from "./types";
|
|
2
|
+
|
|
3
|
+
export type StudioBridgeMessageType =
|
|
4
|
+
"KYRO_STUDIO_READY" | "KYRO_STUDIO_SELECT" | "KYRO_STUDIO_DRAFT_UPDATE" | "KYRO_STUDIO_SCROLL_INTO_VIEW";
|
|
5
|
+
|
|
6
|
+
export interface StudioBridgeMessage<T = any> {
|
|
7
|
+
type: StudioBridgeMessageType;
|
|
8
|
+
documentId?: string;
|
|
9
|
+
revision?: string;
|
|
10
|
+
nodePath?: string;
|
|
11
|
+
fieldName?: string;
|
|
12
|
+
blocks?: StudioBlock[];
|
|
13
|
+
origin?: string;
|
|
14
|
+
payload?: T;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isStudioBridgeMessage(msg: unknown): msg is StudioBridgeMessage {
|
|
18
|
+
if (!msg || typeof msg !== "object") return false;
|
|
19
|
+
const m = msg as Record<string, any>;
|
|
20
|
+
return (
|
|
21
|
+
typeof m.type === "string" &&
|
|
22
|
+
["KYRO_STUDIO_READY", "KYRO_STUDIO_SELECT", "KYRO_STUDIO_DRAFT_UPDATE", "KYRO_STUDIO_SCROLL_INTO_VIEW"].includes(
|
|
23
|
+
m.type,
|
|
24
|
+
)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createPreviewBridge(options: {
|
|
29
|
+
allowedOrigins?: string[];
|
|
30
|
+
onMessage?: (message: StudioBridgeMessage) => void;
|
|
31
|
+
}) {
|
|
32
|
+
const allowed = options.allowedOrigins ? new Set(options.allowedOrigins) : null;
|
|
33
|
+
|
|
34
|
+
function handleMessage(event: MessageEvent) {
|
|
35
|
+
if (allowed && !allowed.has(event.origin)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (isStudioBridgeMessage(event.data)) {
|
|
39
|
+
if (options.onMessage) {
|
|
40
|
+
options.onMessage(event.data);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
window.addEventListener("message", handleMessage);
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
send(target: Window, message: StudioBridgeMessage, targetOrigin = "*") {
|
|
49
|
+
target.postMessage(message, targetOrigin);
|
|
50
|
+
},
|
|
51
|
+
destroy() {
|
|
52
|
+
window.removeEventListener("message", handleMessage);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
package/src/fonts.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { StudioBlock } from "./types";
|
|
2
|
+
|
|
3
|
+
export const studioFonts = [
|
|
4
|
+
{ name: "Inter", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
5
|
+
{ name: "Roboto", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
6
|
+
{ name: "DM Sans", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
7
|
+
{ name: "Manrope", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
8
|
+
{ name: "Work Sans", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
9
|
+
{ name: "Source Sans 3", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
10
|
+
{ name: "Nunito", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
11
|
+
{ name: "Bricolage Grotesque", category: "sans-serif", weights: [300, 400, 500, 600, 700] },
|
|
12
|
+
{ name: "Lora", category: "serif", weights: [400, 500, 600, 700] },
|
|
13
|
+
{ name: "Playfair Display", category: "serif", weights: [400, 500, 600, 700] },
|
|
14
|
+
{ name: "Crimson Text", category: "serif", weights: [400, 600, 700] },
|
|
15
|
+
{ name: "Instrument Serif", category: "serif", weights: [400] },
|
|
16
|
+
{ name: "IBM Plex Mono", category: "monospace", weights: [300, 400, 500, 600, 700] },
|
|
17
|
+
{ name: "Space Mono", category: "monospace", weights: [400, 700] },
|
|
18
|
+
];
|
|
19
|
+
export function fontStack(name: unknown): string | undefined {
|
|
20
|
+
if (name === "system") return "system-ui, sans-serif";
|
|
21
|
+
const font = studioFonts.find((font) => font.name === name);
|
|
22
|
+
return font ? `"${font.name}", ${font.category}` : undefined;
|
|
23
|
+
}
|
|
24
|
+
export function fontStylesheet(families: string[]): string | undefined {
|
|
25
|
+
const fonts = studioFonts.filter((font) => families.includes(font.name)).sort((a, b) => a.name.localeCompare(b.name));
|
|
26
|
+
if (!fonts.length) return;
|
|
27
|
+
return (
|
|
28
|
+
"https://fonts.googleapis.com/css2?" +
|
|
29
|
+
fonts
|
|
30
|
+
.map((font) => "family=" + encodeURIComponent(font.name).replace(/%20/g, "+") + ":wght@" + font.weights.join(";"))
|
|
31
|
+
.join("&") +
|
|
32
|
+
"&display=swap"
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
export function pageFontStylesheet(blocks: StudioBlock[]): string | undefined {
|
|
36
|
+
const families = new Set<string>();
|
|
37
|
+
const visit = (value: unknown) => {
|
|
38
|
+
if (!value || typeof value !== "object") return;
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
value.forEach(visit);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const record = value as Record<string, unknown>;
|
|
44
|
+
if (typeof record.fontFamily === "string") families.add(record.fontFamily);
|
|
45
|
+
Object.values(record).forEach(visit);
|
|
46
|
+
};
|
|
47
|
+
visit(blocks);
|
|
48
|
+
return fontStylesheet([...families]);
|
|
49
|
+
}
|
package/src/forms.tsx
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import type { StudioRenderProps } from "./types";
|
|
3
|
+
import { text, list, safeStudioUrl } from "./primitives";
|
|
4
|
+
|
|
5
|
+
export interface FormSubmissionAdapterOptions {
|
|
6
|
+
apiBase?: string;
|
|
7
|
+
onSuccess?: (response: any) => void;
|
|
8
|
+
onError?: (error: any) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createFormSubmissionAdapter(options: FormSubmissionAdapterOptions = {}) {
|
|
12
|
+
const apiBase = (options.apiBase || "").replace(/\/$/, "");
|
|
13
|
+
|
|
14
|
+
return function renderStudioForm(form: Record<string, any>, props: StudioRenderProps): React.ReactNode {
|
|
15
|
+
const { editing } = props;
|
|
16
|
+
const formId = form.id || form.slug;
|
|
17
|
+
|
|
18
|
+
if (editing) {
|
|
19
|
+
// In editor preview: Display non-submitting disabled form fields
|
|
20
|
+
return (
|
|
21
|
+
<div className="kyro-form-preview" data-kyro-interactive>
|
|
22
|
+
{form.subtitle && <p className="kyro-form-subtitle">{text(form.subtitle)}</p>}
|
|
23
|
+
<div className="kyro-form-fields">
|
|
24
|
+
{list(form.fields).map((f: any, idx: number) => (
|
|
25
|
+
<label key={f.id || idx} className="kyro-form-field">
|
|
26
|
+
<span className="kyro-form-label">
|
|
27
|
+
{text(f.fieldName)}
|
|
28
|
+
{f.isRequired && (
|
|
29
|
+
<span className="kyro-required" aria-hidden="true">
|
|
30
|
+
{" "}
|
|
31
|
+
*
|
|
32
|
+
</span>
|
|
33
|
+
)}
|
|
34
|
+
</span>
|
|
35
|
+
{f.inputType === "textarea" ? (
|
|
36
|
+
<textarea disabled placeholder={text(f.placeholder)} />
|
|
37
|
+
) : (
|
|
38
|
+
<input
|
|
39
|
+
disabled
|
|
40
|
+
type={["text", "email", "tel", "checkbox"].includes(f.inputType) ? f.inputType : "text"}
|
|
41
|
+
placeholder={text(f.placeholder)}
|
|
42
|
+
/>
|
|
43
|
+
)}
|
|
44
|
+
</label>
|
|
45
|
+
))}
|
|
46
|
+
</div>
|
|
47
|
+
<button type="button" disabled className="kyro-btn kyro-btn-primary">
|
|
48
|
+
{text(form.submitButtonText) || "Submit"}
|
|
49
|
+
</button>
|
|
50
|
+
<small className="kyro-form-note">Preview mode · submissions disabled</small>
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Live site renderer: interactive submission component
|
|
56
|
+
return <InteractiveStudioForm form={form} formId={formId} apiBase={apiBase} options={options} />;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function InteractiveStudioForm({
|
|
61
|
+
form,
|
|
62
|
+
formId,
|
|
63
|
+
apiBase,
|
|
64
|
+
options,
|
|
65
|
+
}: {
|
|
66
|
+
form: Record<string, any>;
|
|
67
|
+
formId: string;
|
|
68
|
+
apiBase: string;
|
|
69
|
+
options: FormSubmissionAdapterOptions;
|
|
70
|
+
}) {
|
|
71
|
+
const [formData, setFormData] = useState<Record<string, any>>({});
|
|
72
|
+
const [submitting, setSubmitting] = useState(false);
|
|
73
|
+
const [submitted, setSubmitted] = useState(false);
|
|
74
|
+
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
75
|
+
const [fieldErrors, setFieldErrors] = useState<Record<string, string>>({});
|
|
76
|
+
|
|
77
|
+
const handleSubmit = async (e: React.FormEvent) => {
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
setSubmitting(true);
|
|
80
|
+
setErrorMessage(null);
|
|
81
|
+
setFieldErrors({});
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const res = await fetch(`${apiBase}/api/forms/${encodeURIComponent(formId)}/submit`, {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers: { "Content-Type": "application/json" },
|
|
87
|
+
body: JSON.stringify(formData),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const data = await res.json().catch(() => ({}));
|
|
91
|
+
|
|
92
|
+
if (!res.ok || !data.success) {
|
|
93
|
+
if (data.fieldErrors) {
|
|
94
|
+
setFieldErrors(data.fieldErrors);
|
|
95
|
+
}
|
|
96
|
+
throw new Error(data.error || data.message || "Failed to submit form");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
setSubmitted(true);
|
|
100
|
+
if (options.onSuccess) options.onSuccess(data);
|
|
101
|
+
|
|
102
|
+
if (data.redirectUrl) {
|
|
103
|
+
const safeUrl = safeStudioUrl(data.redirectUrl);
|
|
104
|
+
if (safeUrl) {
|
|
105
|
+
window.location.href = safeUrl;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} catch (err: any) {
|
|
109
|
+
const msg = err.message || "Submission failed";
|
|
110
|
+
setErrorMessage(msg);
|
|
111
|
+
if (options.onError) options.onError(err);
|
|
112
|
+
} finally {
|
|
113
|
+
setSubmitting(false);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (submitted) {
|
|
118
|
+
return (
|
|
119
|
+
<div className="kyro-form-success" role="status">
|
|
120
|
+
<h3>Thank you!</h3>
|
|
121
|
+
<p>Your submission has been received.</p>
|
|
122
|
+
</div>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<form className="kyro-live-form" onSubmit={handleSubmit} noValidate>
|
|
128
|
+
{form.subtitle && <p className="kyro-form-subtitle">{text(form.subtitle)}</p>}
|
|
129
|
+
{errorMessage && (
|
|
130
|
+
<div className="kyro-form-alert error" role="alert">
|
|
131
|
+
{errorMessage}
|
|
132
|
+
</div>
|
|
133
|
+
)}
|
|
134
|
+
<div className="kyro-form-fields">
|
|
135
|
+
{/* Anti-spam honeypot */}
|
|
136
|
+
<input
|
|
137
|
+
type="text"
|
|
138
|
+
name="_gotcha"
|
|
139
|
+
style={{ display: "none" }}
|
|
140
|
+
tabIndex={-1}
|
|
141
|
+
autoComplete="off"
|
|
142
|
+
onChange={(e) => setFormData((prev) => ({ ...prev, _gotcha: e.target.value }))}
|
|
143
|
+
/>
|
|
144
|
+
{list(form.fields).map((f: any, idx: number) => {
|
|
145
|
+
const fieldName = f.fieldName || `field_${idx}`;
|
|
146
|
+
const error = fieldErrors[fieldName];
|
|
147
|
+
return (
|
|
148
|
+
<label key={f.id || idx} className={`kyro-form-field ${error ? "has-error" : ""}`}>
|
|
149
|
+
<span className="kyro-form-label">
|
|
150
|
+
{text(f.fieldName)}
|
|
151
|
+
{f.isRequired && (
|
|
152
|
+
<span className="kyro-required" aria-hidden="true">
|
|
153
|
+
{" "}
|
|
154
|
+
*
|
|
155
|
+
</span>
|
|
156
|
+
)}
|
|
157
|
+
</span>
|
|
158
|
+
{f.inputType === "textarea" ? (
|
|
159
|
+
<textarea
|
|
160
|
+
required={f.isRequired}
|
|
161
|
+
placeholder={text(f.placeholder)}
|
|
162
|
+
value={formData[fieldName] || ""}
|
|
163
|
+
onChange={(e) => setFormData((prev) => ({ ...prev, [fieldName]: e.target.value }))}
|
|
164
|
+
/>
|
|
165
|
+
) : (
|
|
166
|
+
<input
|
|
167
|
+
type={["text", "email", "tel", "checkbox"].includes(f.inputType) ? f.inputType : "text"}
|
|
168
|
+
required={f.isRequired}
|
|
169
|
+
placeholder={text(f.placeholder)}
|
|
170
|
+
checked={f.inputType === "checkbox" ? Boolean(formData[fieldName]) : undefined}
|
|
171
|
+
value={f.inputType !== "checkbox" ? formData[fieldName] || "" : undefined}
|
|
172
|
+
onChange={(e) =>
|
|
173
|
+
setFormData((prev) => ({
|
|
174
|
+
...prev,
|
|
175
|
+
[fieldName]: f.inputType === "checkbox" ? e.target.checked : e.target.value,
|
|
176
|
+
}))
|
|
177
|
+
}
|
|
178
|
+
/>
|
|
179
|
+
)}
|
|
180
|
+
{error && (
|
|
181
|
+
<span className="kyro-field-error" role="alert">
|
|
182
|
+
{error}
|
|
183
|
+
</span>
|
|
184
|
+
)}
|
|
185
|
+
</label>
|
|
186
|
+
);
|
|
187
|
+
})}
|
|
188
|
+
</div>
|
|
189
|
+
<button type="submit" disabled={submitting} className="kyro-btn kyro-btn-primary">
|
|
190
|
+
{submitting ? "Submitting…" : text(form.submitButtonText) || "Submit"}
|
|
191
|
+
</button>
|
|
192
|
+
</form>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface ImageRegion { x: number; y: number; width: number; height: number }
|
|
2
|
+
|
|
3
|
+
export interface MediaPlacementMetadata {
|
|
4
|
+
crop?: ImageRegion | null;
|
|
5
|
+
hotspot?: ImageRegion | null;
|
|
6
|
+
alt?: string | null;
|
|
7
|
+
caption?: string | null;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface MediaPlacement {
|
|
12
|
+
id: string;
|
|
13
|
+
relationTo?: string;
|
|
14
|
+
metadata?: MediaPlacementMetadata;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function imageFrame(width: number, height: number, crop?: ImageRegion | null, hotspot?: ImageRegion | null, aspect?: number): ImageRegion {
|
|
18
|
+
const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value));
|
|
19
|
+
const valid = (rect?: ImageRegion | null) => rect && [rect.x, rect.y, rect.width, rect.height].every(Number.isFinite) && rect.width > 0 && rect.height > 0;
|
|
20
|
+
const source = valid(crop) ? crop! : { x: 0, y: 0, width: 100, height: 100 };
|
|
21
|
+
const x = clamp(source.x, 0, 99.99), y = clamp(source.y, 0, 99.99);
|
|
22
|
+
const frame = { x, y, width: clamp(source.width, .01, 100 - x), height: clamp(source.height, .01, 100 - y) };
|
|
23
|
+
if (!(width > 0 && height > 0 && aspect && aspect > 0)) return frame;
|
|
24
|
+
const pixelRatio = aspect * height / width;
|
|
25
|
+
const fw = Math.min(frame.width, frame.height * pixelRatio);
|
|
26
|
+
const fh = fw / pixelRatio;
|
|
27
|
+
const focus = valid(hotspot) ? hotspot! : frame;
|
|
28
|
+
return {
|
|
29
|
+
x: clamp(focus.x + focus.width / 2 - fw / 2, frame.x, frame.x + frame.width - fw),
|
|
30
|
+
y: clamp(focus.y + focus.height / 2 - fh / 2, frame.y, frame.y + frame.height - fh),
|
|
31
|
+
width: fw, height: fh,
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from "./react";
|
|
2
|
+
export { collectStudioReferences, resolveStudioReferences, getStudioReferenceIssues } from "./references";
|
|
3
|
+
export type { StudioReference } from "./references";
|
|
4
|
+
export { createFormSubmissionAdapter, type FormSubmissionAdapterOptions } from "./forms";
|
|
5
|
+
export {
|
|
6
|
+
isStudioBridgeMessage,
|
|
7
|
+
createPreviewBridge,
|
|
8
|
+
type StudioBridgeMessage,
|
|
9
|
+
type StudioBridgeMessageType,
|
|
10
|
+
} from "./bridge";
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
imageFrame,
|
|
14
|
+
type ImageRegion,
|
|
15
|
+
type MediaPlacement,
|
|
16
|
+
type MediaPlacementMetadata,
|
|
17
|
+
} from "./image-framing";
|
|
18
|
+
export { studioFonts, fontStack, fontStylesheet, pageFontStylesheet } from './fonts';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { fontStack } from "./fonts";
|
|
2
|
+
import type { CSSProperties } from "react";
|
|
3
|
+
|
|
4
|
+
/** A declaration-only editor cannot inject selectors or additional style rules. */
|
|
5
|
+
export function parseLayoutCss(source: unknown): CSSProperties {
|
|
6
|
+
if (typeof source !== "string" || /[{}@<>\\]/.test(source)) return {};
|
|
7
|
+
const result: Record<string, string> = {};
|
|
8
|
+
for (const declaration of source.split(";")) {
|
|
9
|
+
const separator = declaration.indexOf(":");
|
|
10
|
+
if (separator < 1) continue;
|
|
11
|
+
const property = declaration.slice(0, separator).trim();
|
|
12
|
+
const value = declaration.slice(separator + 1).trim();
|
|
13
|
+
if (
|
|
14
|
+
!/^(--[a-zA-Z0-9-]+|[a-z][a-z-]*)$/.test(property) ||
|
|
15
|
+
!value ||
|
|
16
|
+
/!important|expression\s*\(|javascript:/i.test(value)
|
|
17
|
+
)
|
|
18
|
+
continue;
|
|
19
|
+
const key = property.startsWith("--")
|
|
20
|
+
? property
|
|
21
|
+
: property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
22
|
+
result[key] = value;
|
|
23
|
+
}
|
|
24
|
+
return result as CSSProperties;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function extendedLayoutStyle(settings: Record<string, any>): CSSProperties {
|
|
28
|
+
const style: Record<string, any> = {};
|
|
29
|
+
const setNumber = (name: string, min = 0, max = 2400, target = name) => {
|
|
30
|
+
if (typeof settings[name] === "number" && Number.isFinite(settings[name]))
|
|
31
|
+
style[target] = Math.max(min, Math.min(max, settings[name]));
|
|
32
|
+
};
|
|
33
|
+
for (const name of ["maxWidth", "height", "maxHeight", "rowGap", "columnGap"]) setNumber(name);
|
|
34
|
+
for (const side of ["Top", "Right", "Bottom", "Left"]) {
|
|
35
|
+
setNumber(`padding${side}`);
|
|
36
|
+
setNumber(`margin${side}`, -1000);
|
|
37
|
+
setNumber(`inset${side}`, -2400, 2400, side.toLowerCase());
|
|
38
|
+
}
|
|
39
|
+
if (fontStack(settings.fontFamily)) style.fontFamily = fontStack(settings.fontFamily);
|
|
40
|
+
setNumber("fontSize", 8, 200);
|
|
41
|
+
setNumber("lineHeight", 0.5, 4);
|
|
42
|
+
setNumber("letterSpacing", -10, 30);
|
|
43
|
+
if (["300", "400", "500", "600", "700"].includes(settings.fontWeight)) style.fontWeight = Number(settings.fontWeight);
|
|
44
|
+
if (["start", "center", "end", "justify"].includes(settings.textAlign)) style.textAlign = settings.textAlign;
|
|
45
|
+
setNumber("borderWidth", 0, 30);
|
|
46
|
+
setNumber("zIndex", -100, 100);
|
|
47
|
+
if (typeof settings.opacity === "number") style.opacity = Math.max(0, Math.min(100, settings.opacity)) / 100;
|
|
48
|
+
if (["static", "relative", "absolute", "sticky", "fixed"].includes(settings.position))
|
|
49
|
+
style.position = settings.position;
|
|
50
|
+
if (["solid", "dashed", "dotted", "double", "none"].includes(settings.borderStyle))
|
|
51
|
+
style.borderStyle = settings.borderStyle;
|
|
52
|
+
else if (settings.borderWidth) style.borderStyle = "solid";
|
|
53
|
+
if (settings.borderColor) style.borderColor = settings.borderColor;
|
|
54
|
+
if (settings.textColor) {
|
|
55
|
+
style.color = settings.textColor;
|
|
56
|
+
style["--studio-ink"] = settings.textColor;
|
|
57
|
+
}
|
|
58
|
+
if (settings.backgroundType === "color") style.background = settings.backgroundColor || "transparent";
|
|
59
|
+
if (settings.backgroundType === "gradient")
|
|
60
|
+
style.background = `linear-gradient(${Number(settings.gradientAngle) || 0}deg, ${settings.backgroundColor || "transparent"}, ${settings.gradientColor || "transparent"})`;
|
|
61
|
+
for (const device of ["tablet", "mobile"]) {
|
|
62
|
+
for (const property of ["Padding", "Gap", "Columns"]) {
|
|
63
|
+
const value = settings[device + property];
|
|
64
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
65
|
+
style[`--layout-${device}-${property.toLowerCase()}`] =
|
|
66
|
+
property === "Columns"
|
|
67
|
+
? Math.max(1, Math.min(4, Math.round(value)))
|
|
68
|
+
: `${Math.max(0, Math.min(2400, value))}px`;
|
|
69
|
+
}
|
|
70
|
+
if (["row", "column"].includes(settings[device + "Direction"]))
|
|
71
|
+
style[`--layout-${device}-direction`] = settings[device + "Direction"];
|
|
72
|
+
}
|
|
73
|
+
return { ...style, ...parseLayoutCss(settings.customCss) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function layoutItemStyle(settings: Record<string, any> = {}): CSSProperties {
|
|
77
|
+
const style: Record<string, any> = {};
|
|
78
|
+
for (const [name, min, max] of [
|
|
79
|
+
["flexBasis", 0, 2400],
|
|
80
|
+
["flexGrow", 0, 10],
|
|
81
|
+
["flexShrink", 0, 10],
|
|
82
|
+
["order", -100, 100],
|
|
83
|
+
] as const) {
|
|
84
|
+
if (typeof settings[name] === "number" && Number.isFinite(settings[name]))
|
|
85
|
+
style[name] = Math.max(min, Math.min(max, settings[name]));
|
|
86
|
+
}
|
|
87
|
+
if (["auto", "start", "center", "end", "stretch"].includes(settings.alignSelf)) style.alignSelf = settings.alignSelf;
|
|
88
|
+
if (Number.isFinite(settings.columnSpan))
|
|
89
|
+
style.gridColumn = `span ${Math.max(1, Math.min(4, Math.round(settings.columnSpan)))}`;
|
|
90
|
+
if (Number.isFinite(settings.rowSpan))
|
|
91
|
+
style.gridRow = `span ${Math.max(1, Math.min(24, Math.round(settings.rowSpan)))}`;
|
|
92
|
+
return style;
|
|
93
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React, { lazy, Suspense } from "react";
|
|
2
|
+
|
|
3
|
+
const cache = new Map<string, React.LazyExoticComponent<React.ComponentType<any>>>();
|
|
4
|
+
export function PickedIcon({ name, size, label }: { name: string; size: number; label: string }) {
|
|
5
|
+
if (!cache.has(name))
|
|
6
|
+
cache.set(
|
|
7
|
+
name,
|
|
8
|
+
lazy(async () => {
|
|
9
|
+
const [library, raw] = name.includes(":") ? name.split(":", 2) : ["lucide", name];
|
|
10
|
+
const pascal = raw.replace(/(^|[-_])(\w)/g, (_, _separator, letter) => letter.toUpperCase());
|
|
11
|
+
const icons: Record<string, any> =
|
|
12
|
+
library === "hero-solid"
|
|
13
|
+
? await import("@heroicons/react/24/solid")
|
|
14
|
+
: library === "hero" || library === "hero-outline"
|
|
15
|
+
? await import("@heroicons/react/24/outline")
|
|
16
|
+
: await import("lucide-react");
|
|
17
|
+
return { default: icons[raw] || icons[pascal] || icons[pascal + "Icon"] || (() => null) };
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
const Icon = cache.get(name)!;
|
|
21
|
+
return (
|
|
22
|
+
<Suspense fallback={<span style={{ display: "inline-block", width: size, height: size }} />}>
|
|
23
|
+
<Icon width={size} height={size} role="img" aria-label={label} strokeWidth={1.5} />
|
|
24
|
+
</Suspense>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { imageFrame } from "./image-framing";
|
|
3
|
+
import type { StudioRenderProps } from "./types";
|
|
4
|
+
export const text = (value: unknown) => (typeof value === "string" || typeof value === "number" ? String(value) : "");
|
|
5
|
+
export const list = (value: unknown): any[] => (Array.isArray(value) ? value : []);
|
|
6
|
+
export function safeStudioUrl(input: unknown): string | undefined {
|
|
7
|
+
const url = text(input).trim();
|
|
8
|
+
if (!url || /[\u0000-\u001f\u007f\\]/.test(url)) return;
|
|
9
|
+
if (/^(https?:\/\/|mailto:|tel:|\/|#)/i.test(url) && !url.startsWith("//")) return url;
|
|
10
|
+
}
|
|
11
|
+
export const attr = (props: StudioRenderProps, path: string) =>
|
|
12
|
+
props.editing ? { "data-kyro-field": props.pathPrefix ? `${props.pathPrefix}.${path}` : path } : {};
|
|
13
|
+
export function reference(value: any, collection: string, props: StudioRenderProps): any {
|
|
14
|
+
if (!value) return;
|
|
15
|
+
if (typeof value === "object" && value.value) return reference(value.value, value.relationTo || collection, props);
|
|
16
|
+
if (typeof value === "object" && (value.url || value.title || value.name || value.fields || value.content))
|
|
17
|
+
return value;
|
|
18
|
+
if (collection === "media" && typeof value === "string" && safeStudioUrl(value)) return { url: value };
|
|
19
|
+
const id = typeof value === "object" ? value.id : value;
|
|
20
|
+
return props.context?.references?.get(`${collection}/${id}`);
|
|
21
|
+
}
|
|
22
|
+
export function Media({
|
|
23
|
+
value,
|
|
24
|
+
path,
|
|
25
|
+
props,
|
|
26
|
+
alt,
|
|
27
|
+
className,
|
|
28
|
+
}: {
|
|
29
|
+
value: any;
|
|
30
|
+
path: string;
|
|
31
|
+
props: StudioRenderProps;
|
|
32
|
+
alt?: string;
|
|
33
|
+
className?: string;
|
|
34
|
+
}) {
|
|
35
|
+
const asset = reference(value, "media", props);
|
|
36
|
+
const settings = value?.metadata || asset?.metadata;
|
|
37
|
+
const hotspot = settings?.hotspot;
|
|
38
|
+
const crop = settings?.crop;
|
|
39
|
+
const url = safeStudioUrl(asset?.url);
|
|
40
|
+
const frame = imageFrame(1, 1, crop);
|
|
41
|
+
const resizeParams = new URLSearchParams(url ? { url } : {});
|
|
42
|
+
if (crop) {
|
|
43
|
+
resizeParams.set("cx", String(frame.x));
|
|
44
|
+
resizeParams.set("cy", String(frame.y));
|
|
45
|
+
resizeParams.set("cw", String(frame.width));
|
|
46
|
+
resizeParams.set("ch", String(frame.height));
|
|
47
|
+
}
|
|
48
|
+
if (hotspot) {
|
|
49
|
+
resizeParams.set("hx", String(hotspot.x));
|
|
50
|
+
resizeParams.set("hy", String(hotspot.y));
|
|
51
|
+
resizeParams.set("hw", String(hotspot.width));
|
|
52
|
+
resizeParams.set("hh", String(hotspot.height));
|
|
53
|
+
}
|
|
54
|
+
const transformedUrl = url && (crop || hotspot) ? `/api/media/resize?${resizeParams.toString()}` : url;
|
|
55
|
+
return url ? (
|
|
56
|
+
<img
|
|
57
|
+
{...attr(props, path)}
|
|
58
|
+
className={className}
|
|
59
|
+
src={transformedUrl}
|
|
60
|
+
style={hotspot ? { objectPosition: `${Math.max(0, Math.min(100, (hotspot.x + hotspot.width / 2 - frame.x) / frame.width * 100))}% ${Math.max(0, Math.min(100, (hotspot.y + hotspot.height / 2 - frame.y) / frame.height * 100))}%` } : undefined}
|
|
61
|
+
alt={alt || asset?.altText || asset?.alt || ""}
|
|
62
|
+
loading="lazy"
|
|
63
|
+
/>
|
|
64
|
+
) : props.editing ? (
|
|
65
|
+
<div {...attr(props, path)} className="kyro-media-placeholder">
|
|
66
|
+
{value ? "Resolving image…" : "Choose an image"}
|
|
67
|
+
</div>
|
|
68
|
+
) : null;
|
|
69
|
+
}
|
|
70
|
+
export function Heading({
|
|
71
|
+
props,
|
|
72
|
+
path = "title",
|
|
73
|
+
level = 2,
|
|
74
|
+
}: {
|
|
75
|
+
props: StudioRenderProps;
|
|
76
|
+
path?: string;
|
|
77
|
+
level?: number;
|
|
78
|
+
}) {
|
|
79
|
+
const Tag = `h${Math.min(6, Math.max(1, level))}` as "h2";
|
|
80
|
+
return <Tag {...attr(props, path)}>{text(props.block[path])}</Tag>;
|
|
81
|
+
}
|
|
82
|
+
export function Intro({ props }: { props: StudioRenderProps }) {
|
|
83
|
+
return (
|
|
84
|
+
<header className="kyro-section-intro">
|
|
85
|
+
<Heading props={props} />
|
|
86
|
+
{props.block.subtitle && <p {...attr(props, "subtitle")}>{text(props.block.subtitle)}</p>}
|
|
87
|
+
{props.block.description && <p {...attr(props, "description")}>{text(props.block.description)}</p>}
|
|
88
|
+
</header>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
export function Buttons({
|
|
92
|
+
items,
|
|
93
|
+
path = "buttons",
|
|
94
|
+
props,
|
|
95
|
+
}: {
|
|
96
|
+
items: unknown;
|
|
97
|
+
path?: string;
|
|
98
|
+
props: StudioRenderProps;
|
|
99
|
+
}) {
|
|
100
|
+
return (
|
|
101
|
+
<div className="kyro-actions">
|
|
102
|
+
{list(items).map((item, index) => (
|
|
103
|
+
<a
|
|
104
|
+
key={item.id || index}
|
|
105
|
+
{...attr(props, `${path}.${index}.text`)}
|
|
106
|
+
href={safeStudioUrl(item.url)}
|
|
107
|
+
className="kyro-action"
|
|
108
|
+
>
|
|
109
|
+
{text(item.text)}
|
|
110
|
+
</a>
|
|
111
|
+
))}
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function IconGlyph({ name }: { name: unknown }) {
|
|
117
|
+
const key = text(name)
|
|
118
|
+
.replace(/^.*:/, "")
|
|
119
|
+
.replace(/[^a-z]/gi, "")
|
|
120
|
+
.toLowerCase();
|
|
121
|
+
const paths: Record<string, string> = {
|
|
122
|
+
check: "m5 12 4 4L19 6",
|
|
123
|
+
checkcircle: "m7 12 3 3 7-7M21 12a9 9 0 1 1-9-9",
|
|
124
|
+
star: "m12 3 2.8 5.7 6.2.9-4.5 4.4 1.1 6.2-5.6-3-5.6 3 1.1-6.2L3 9.6l6.2-.9Z",
|
|
125
|
+
heart: "M20 5c-3-3-6-1-8 1-2-2-5-4-8-1-4 4 2 10 8 14 6-4 12-10 8-14",
|
|
126
|
+
arrowright: "M4 12h16m-6-6 6 6-6 6",
|
|
127
|
+
zap: "m13 2-9 12h7l-1 8 10-12h-7Z",
|
|
128
|
+
shield: "m12 3 8 3v6c0 4-8 9-8 9s-8-5-8-9V6Z",
|
|
129
|
+
clock: "M12 8v5l3 2M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0",
|
|
130
|
+
};
|
|
131
|
+
return (
|
|
132
|
+
<svg
|
|
133
|
+
width="24"
|
|
134
|
+
height="24"
|
|
135
|
+
viewBox="0 0 24 24"
|
|
136
|
+
fill="none"
|
|
137
|
+
stroke="currentColor"
|
|
138
|
+
strokeWidth="1.5"
|
|
139
|
+
aria-hidden="true"
|
|
140
|
+
>
|
|
141
|
+
<path d={paths[key] || "M12 3a9 9 0 1 0 0 18 9 9 0 0 0 0-18M12 9v6m-3-3h6"} />
|
|
142
|
+
</svg>
|
|
143
|
+
);
|
|
144
|
+
}
|