40 lines
1.6 KiB
JavaScript
40 lines
1.6 KiB
JavaScript
// 模板 CSS 使用 Taro 的 750 设计宽度,转换到预览容器单位。
|
|
export function templatePreviewCss() {
|
|
return {
|
|
postcssPlugin: "template-preview-css",
|
|
Once(root) {
|
|
const file = root.source?.input.file || "";
|
|
const templateStyle =
|
|
file.includes("/packages/template-") && file.endsWith("/taro/style.css");
|
|
const sharedFormStyle = file.endsWith("/apps/taro/src/components/forms.css");
|
|
if (!templateStyle && !sharedFormStyle) return;
|
|
root.walkRules((rule) => {
|
|
rule.selector = rule.selector
|
|
.split(",")
|
|
.map(
|
|
(selector) =>
|
|
".component-template-preview__canvas " +
|
|
selector
|
|
.trim()
|
|
.replace(
|
|
/(^|[ >+~])(image|text|view)(?=[ .#:[>+~)]|$)/g,
|
|
(_, prefix, tag) => prefix + { image: "img", text: "span", view: "div" }[tag],
|
|
),
|
|
)
|
|
.join(", ");
|
|
});
|
|
root.walkDecls((declaration) => {
|
|
// 组件预览中的固定按钮必须以页面画布为定位基准。
|
|
if (declaration.prop === "position" && declaration.value === "fixed")
|
|
declaration.value = "absolute";
|
|
// 模板页面的视口高度按手机画布宽度计算,避免引用 Admin 浏览器高度。
|
|
declaration.value = declaration.value.replace(/100vh/g, "178cqw");
|
|
declaration.value = declaration.value.replace(
|
|
/(-?(?:[0-9]*[.])?[0-9]+)px/g,
|
|
(_, value) => Number((Number(value) / 7.5).toFixed(8)) + "cqw",
|
|
);
|
|
});
|
|
},
|
|
};
|
|
}
|