133 lines
4.4 KiB
React
133 lines
4.4 KiB
React
import React from "react";
|
|
import { TemplatePreviewAssetsContext } from "./preview-assets.js";
|
|
|
|
export const TemplateAssetContext = React.createContext("");
|
|
const templateAssets = import.meta.glob("../../../../../packages/template-*/miniapp/assets/**/*", {
|
|
eager: true,
|
|
query: "?url",
|
|
import: "default",
|
|
});
|
|
|
|
function resolveImageSource(src, templateKey) {
|
|
if (typeof src !== "string" || !src.startsWith("/assets/")) return src;
|
|
const relative = src.slice("/assets/".length);
|
|
for (const [file, url] of Object.entries(templateAssets)) {
|
|
const parts = file.split("/packages/template-")[1]?.split("/miniapp/assets/");
|
|
if (!parts) continue;
|
|
const [key, asset] = parts;
|
|
if (relative === key + "/" + asset || (key === templateKey && relative === asset)) return url;
|
|
}
|
|
return src;
|
|
}
|
|
|
|
const element = (tag) =>
|
|
React.forwardRef(({ children, className, style, onClick, ...props }, ref) =>
|
|
React.createElement(tag, { ...props, ref, className, style, onClick }, children),
|
|
);
|
|
|
|
export const View = element("div");
|
|
export const Text = element("span");
|
|
export const Button = React.forwardRef(
|
|
({ children, className, style, onClick, loading, ...props }, ref) =>
|
|
React.createElement(
|
|
"button",
|
|
{
|
|
...props,
|
|
ref,
|
|
className,
|
|
style,
|
|
onClick,
|
|
...(loading ? { "aria-busy": "true" } : {}),
|
|
},
|
|
children,
|
|
),
|
|
);
|
|
export const Input = React.forwardRef(({ onInput, ...props }, ref) =>
|
|
React.createElement("input", {
|
|
...props,
|
|
ref,
|
|
onInput: (event) => onInput?.({ detail: { value: event.currentTarget.value } }),
|
|
}),
|
|
);
|
|
export const Textarea = React.forwardRef(({ onInput, maxlength, ...props }, ref) =>
|
|
React.createElement("textarea", {
|
|
...props,
|
|
ref,
|
|
maxLength: maxlength,
|
|
onInput: (event) => onInput?.({ detail: { value: event.currentTarget.value } }),
|
|
}),
|
|
);
|
|
export function Picker({ children, mode, range = [], value, onChange }) {
|
|
return (
|
|
<div style={{ position: "relative" }}>
|
|
{children}
|
|
{mode === "date" ? (
|
|
<input
|
|
aria-label="选择日期"
|
|
type="date"
|
|
value={value || ""}
|
|
onChange={(event) => onChange?.({ detail: { value: event.currentTarget.value } })}
|
|
style={{ position: "absolute", inset: 0, width: "100%", opacity: 0, cursor: "pointer" }}
|
|
/>
|
|
) : (
|
|
<select
|
|
aria-label="选择选项"
|
|
value={value ?? 0}
|
|
onChange={(event) => onChange?.({ detail: { value: event.currentTarget.value } })}
|
|
style={{ position: "absolute", inset: 0, width: "100%", opacity: 0, cursor: "pointer" }}
|
|
>
|
|
{range.map((label, index) => (
|
|
<option key={index} value={index}>
|
|
{label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
const CheckboxContext = React.createContext(null);
|
|
export function CheckboxGroup({ children, onChange }) {
|
|
const [selected, setSelected] = React.useState([]);
|
|
const update = (value, checked) => {
|
|
const next = checked ? [...selected, value] : selected.filter((item) => item !== value);
|
|
setSelected(next);
|
|
onChange?.({ detail: { value: next } });
|
|
};
|
|
return (
|
|
<CheckboxContext.Provider value={update}>
|
|
<div>{children}</div>
|
|
</CheckboxContext.Provider>
|
|
);
|
|
}
|
|
export function Checkbox({ value, checked }) {
|
|
const update = React.useContext(CheckboxContext);
|
|
return (
|
|
<input
|
|
type="checkbox"
|
|
value={value}
|
|
checked={Boolean(checked)}
|
|
onChange={(event) => update?.(value, event.currentTarget.checked)}
|
|
/>
|
|
);
|
|
}
|
|
export const Label = element("label");
|
|
export const Image = React.forwardRef(({ mode, src, style, ...props }, ref) => {
|
|
const key = React.useContext(TemplateAssetContext);
|
|
const assets = React.useContext(TemplatePreviewAssetsContext);
|
|
const normalizedSrc = typeof src === "string" ? src.trim() : "";
|
|
if (!normalizedSrc) return null;
|
|
return React.createElement("img", {
|
|
...props,
|
|
ref,
|
|
src:
|
|
assets && normalizedSrc.startsWith("/assets/")
|
|
? // 导入包资源以 /assets 开头;本地多模板预览的 renderer 可能额外携带包 key 前缀。
|
|
(assets[normalizedSrc]?.dataUrl ??
|
|
assets[normalizedSrc.replace(`/assets/${key}/`, "/assets/")]?.dataUrl ??
|
|
normalizedSrc)
|
|
: resolveImageSource(normalizedSrc, key),
|
|
style: { objectFit: mode === "aspectFit" ? "contain" : "cover", ...style },
|
|
});
|
|
});
|