From 8ffb6723d1d30e0dbc255ae4064cfd3677020e79 Mon Sep 17 00:00:00 2001
From: sight <1453017105@qq.com>
Date: Tue, 28 Jun 2022 14:07:44 +0800
Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D:=20chore:=20=E8=A1=A5=E5=85=85?=
=?UTF-8?q?=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../document-layer/src/assets/css/index.css | 2 +-
.../src/components/Children2.vue | 2 +-
.../document-layer/src/components/LayCode.vue | 11 ++
.../src/composable/usePlayground.ts | 147 ++++++++++++++++++
.../document-layer/src/document/zh-CN/demo.md | 4 +-
package/layer/src/component/index.vue | 121 +++++++++-----
6 files changed, 242 insertions(+), 45 deletions(-)
create mode 100644 package/document-layer/src/composable/usePlayground.ts
diff --git a/package/document-layer/src/assets/css/index.css b/package/document-layer/src/assets/css/index.css
index 134350bc..ebe02b4e 100644
--- a/package/document-layer/src/assets/css/index.css
+++ b/package/document-layer/src/assets/css/index.css
@@ -6,7 +6,7 @@ button + button {
button {
height: 38px;
line-height: 36px;
- padding: 0 18px;
+ padding: 0 18px !important;
color: #fff;
font-size: 14px;
text-align: center;
diff --git a/package/document-layer/src/components/Children2.vue b/package/document-layer/src/components/Children2.vue
index c0c7d11a..e98c09da 100644
--- a/package/document-layer/src/components/Children2.vue
+++ b/package/document-layer/src/components/Children2.vue
@@ -5,7 +5,7 @@
`
+ );
+ } else {
+ code = `${decodeCode}
+ `;
+ }
+
+ // 去除 export default,保留其中的内容
+ const exportDefaultResult = code.match(exportDefaultRe);
+ if (exportDefaultResult) {
+ code = code.replace(
+ exportDefaultRe,
+ trimBr(exportDefaultResult[1] + ``).trim()
+ );
+ // console.log("export",code);
+ }
+ // 去除 setup 函数,保留其中的内容
+ const setupResult = code.match(setupRe);
+ if (setupResult) {
+ code = code.replace(setupRe, trimBr(setupResult[1]));
+ // console.log("setup",code);
+ }
+ }
+ // 替换 layer 引入语句
+ // playground 中使用最新版 layer 请从 @layui/layer-vue 引入
+ if (code.match(layerRe)) {
+ code = code.replace(layerRe, `import { layer } from "@layui/layer-vue"`);
+ // console.log("layer",code);
+ }
+
+ code = await formatCode(MAIN_FILE_NAME, code);
+ const originCode = {
+ [MAIN_FILE_NAME]: code,
+ "index.css": extraCss,
+ };
+
+ const encoded = utoa(JSON.stringify(originCode));
+ const link = `https://layui-vue.gitee.io/sandbox-vue/#${encoded}`;
+ return {
+ code,
+ encoded,
+ link,
+ };
+};
+
+/**
+ *
+ * @returns 格式化代码
+ */
+async function formatCode(filename: string, data: string) {
+ const [format, parserHtml, parserTypeScript, parserBabel, parserPostcss] =
+ await Promise.all([
+ import("prettier/standalone").then((r) => r.format),
+ import("prettier/parser-html").then((m) => m.default),
+ import("prettier/parser-typescript").then((m) => m.default),
+ import("prettier/parser-babel").then((m) => m.default),
+ import("prettier/parser-postcss").then((m) => m.default),
+ ]);
+ let code = data;
+ let parser: BuiltInParserName;
+ if (filename.endsWith(".vue")) {
+ parser = "vue";
+ } else if (filename.endsWith(".js")) {
+ parser = "babel";
+ } else if (filename.endsWith(".ts")) {
+ parser = "typescript";
+ } else if (filename.endsWith(".json")) {
+ parser = "json";
+ } else {
+ return;
+ }
+ code = format(code, {
+ parser,
+ plugins: [parserHtml, parserTypeScript, parserBabel, parserPostcss],
+ semi: false, // 语句末尾打印分号
+ singleQuote: true, // 使用单引号
+ vueIndentScriptAndStyle: false, // 是否缩进 Vue 文件中的 script 和 style 标签
+ });
+
+ return code;
+}
+
+/**
+ * 去除字符串两端的空白行
+ * @param str
+ * @returns
+ */
+function trimBr(str: string): string {
+ return str.replace(/(^[\r\n]*)|([\r\n]*$)/, "");
+}
+
+export function utoa(data: string): string {
+ const buffer = strToU8(data);
+ const zipped = zlibSync(buffer, { level: 9 });
+ const binary = strFromU8(zipped, true);
+ return btoa(binary);
+}
+
+export function atou(base64: string): string {
+ const binary = atob(base64);
+
+ // zlib header (x78), level 9 (xDA)
+ if (binary.startsWith("\x78\xDA")) {
+ const buffer = strToU8(binary, true);
+ const unzipped = unzlibSync(buffer);
+ return strFromU8(unzipped);
+ }
+
+ // old unicode hacks for backward compatibility
+ // https://base64.guru/developers/javascript/examples/unicode-strings
+ return decodeURIComponent(escape(binary));
+}
diff --git a/package/document-layer/src/document/zh-CN/demo.md b/package/document-layer/src/document/zh-CN/demo.md
index 864f6fe6..869b3d1a 100644
--- a/package/document-layer/src/document/zh-CN/demo.md
+++ b/package/document-layer/src/document/zh-CN/demo.md
@@ -358,7 +358,7 @@ const closeAll = function() {
-::: demo 👉 查看 [Children1.vue](https://gitee.com/layui-vue/layer-vue/blob/master/example/src/components/Children1.vue), 通过 h() 函数的第二个参数向 Children1.vue 传递响应式变量。
+::: demo 👉 查看 [Children1.vue](https://layui-vue.gitee.io/sandbox-vue/#eNqdUkGO00AQ/ErLFyeSY5OrSaJFK87LA3yZtTub2R3PjGba0UaWD5w4cUTiCDwA+AGfQZH4BT127HXEjZu7XNVTXd1t9Mba9NhglEcbwtoqQbgrNMDmviEyGm5KJcunbREZi/rW1NZo1LQuot3508/zx++bbCAOIqltQ0Ani6wgfKYiguOqNhUqBipBInVYC/fEeMaSTTY9CoXm2pdOWgIl9EPo4JnnkRrLXMlvO4IWHIqS5BETOCRceKOOODmDDvbO1BDzUPFMpMQJ3fTzhstGZj24umLeHqSqHOo7jRdumo3YOiTFVOi9lkZ7gjATbCdPizb8HmbMIf796+v5/Y84KXS3fNFcR8nqxRK2O+ilvaU0MIZWACRJIfc6f/nw59vn0Cug3IlYncNhMbOc8KS9o2554fmDqFi9F8pjDwUjXUh+iJqDjZJoGH5VC5s+eqP5GtriAvIK8rbrmHQVw78Hszeu7r/+4w5Ykl30s5N4uYfxBsYEsZbEuVW4lxrfcuEXcSmUwiqe5WydsX6ivQvVEGowkMPd/SOWNK5mFkh4l04KwZe8iIqR4G3YjxVVJfVDDutX9vn1GGVg76LuL+G7LGU=), 通过 h() 函数的第二个参数向 Children1.vue 传递响应式变量。
@@ -384,7 +384,7 @@ const openComponent1 = () => {
:::
-::: demo 👉 查看 [Children2.vue](https://gitee.com/layui-vue/layer-vue/blob/master/example/src/components/Children2.vue), 通过 h() 函数的第二个参数声明 onXxx() 形式的函数完成 Children2.vue 的事件监听。
+::: demo 👉 查看 [Children2.vue](https://layui-vue.gitee.io/sandbox-vue/#eNqNksFum0AQhl9lxAWsYmPnSG2UKOoTtLfSA4Yh3hZ2VzC4sRDnSO2h1x6jPkQP7esklfoWnQXWAaWHcGJmv5n9d+ZvnSutV8cGndDZEpa6SAijWAJs9w2RknCZFiL9tIsdpVFeq1IriZIuYid6+PX14ffPbTCAQ5GQuiGgk0auILyl2IHjslQZFpyQTbnnRMDsNpjcxmGdVkITFIm8MZU1YzVSo/lU8J0VQQsVJimJI/pw8DnIoYO8UiW4rN+dcEVywup8eMlhI4I+uZyR1wdRZBXKd5/VyK4Cm7swQ2E0lqmSNYGulIbdWYLXdgt7ZF7VH+XeZr1ec96ezEfGjLeAXQStGVWvZ2UIr48BSFCBIbh/7u/+/vju+kOWOxFXh3DwJnr9oYn5jLKRBVDyKsu8CuvFEwC9wtUxKRpkDZPgFWws1E1avG32L26xfN6iW/Q/ZkCdWfSwWl6k4zvD5JdlolcfayXZdW08JnnlYdt1DM128NyYuarK/u/JpKPhhoitczZtkmXs1Mcv9zObvqCwbtip0ePdt7m/g/HymX2tea1h7fqxFMTTyjAXEt9wUHvvXVbk+i63dz9MnMLZuT2gr/Z6fBikRbn0/6jpadHJ1I0+OhUIdcpuyzhj3jCUam4v5E0Im7W+fW33ZejI6f4B7opkgw==), 通过 h() 函数的第二个参数声明 onXxx() 形式的函数完成 Children2.vue 的事件监听。
diff --git a/package/layer/src/component/index.vue b/package/layer/src/component/index.vue
index ad19893f..eae12570 100644
--- a/package/layer/src/component/index.vue
+++ b/package/layer/src/component/index.vue
@@ -57,18 +57,18 @@ export interface LayModalProps {
move?: boolean | string;
resize?: boolean | string;
type?:
- | 0
- | 1
- | 2
- | 3
- | 4
- | 5
- | "dialog"
- | "page"
- | "iframe"
- | "loading"
- | "drawer"
- | "photos";
+ | 0
+ | 1
+ | 2
+ | 3
+ | 4
+ | 5
+ | "dialog"
+ | "page"
+ | "iframe"
+ | "loading"
+ | "drawer"
+ | "photos";
content?: string | Function | object | VNodeTypes;
isHtmlFragment?: boolean;
shade?: boolean | string;
@@ -112,9 +112,9 @@ const props = withDefaults(defineProps(), {
resize: false,
isHtmlFragment: false,
isOutAnim: true,
- destroy: () => { },
- success: () => { },
- end: () => { },
+ destroy: () => {},
+ success: () => {},
+ end: () => {},
yesText: "确定",
isFunction: false,
isMessage: false,
@@ -172,8 +172,8 @@ const firstOpenDelayCalculation = function () {
props
);
}
- if (props.isHtmlFragment && props.area === 'auto') {
- area.value = ['auto', 'auto'];
+ if (props.isHtmlFragment && props.area === "auto") {
+ area.value = ["auto", "auto"];
}
offset.value = calculateOffset(props.offset, area.value, props.type);
w.value = area.value[0];
@@ -404,28 +404,32 @@ const styles = computed(() => {
height: h.value,
zIndex: index.value,
};
- if (props.isHtmlFragment && props.area === 'auto') {
+ if (props.isHtmlFragment && props.area === "auto") {
// @ts-ignore
- style.maxWidth = 'calc(100% - 2px)';
+ style.maxWidth = "calc(100% - 2px)";
// @ts-ignore
- style.maxHeight = 'calc(100% - 51px)';
- style.top = '50%';
- style.left = '50%';
+ style.maxHeight = "calc(100% - 51px)";
+ style.top = "50%";
+ style.left = "50%";
if (Array.isArray(offset.value)) {
- if (offset.value[0].indexOf('px') > -1) {
+ if (offset.value[0].indexOf("px") > -1) {
style.top = offset.value[0];
}
- if (offset.value[1].indexOf('px') > -1) {
+ if (offset.value[1].indexOf("px") > -1) {
style.left = offset.value[1];
}
- if (offset.value[0].indexOf('%') > -1 || offset.value[1].indexOf('%') > -1) {
+ if (
+ offset.value[0].indexOf("%") > -1 ||
+ offset.value[1].indexOf("%") > -1
+ ) {
// @ts-ignore
- style.transform = `translate(-${style.left.indexOf('%')>-1?style.left:0},-${style.top.indexOf('%')>-1?style.top:0})`;
+ style.transform = `translate(-${
+ style.left.indexOf("%") > -1 ? style.left : 0
+ },-${style.top.indexOf("%") > -1 ? style.top : 0})`;
}
}
-
}
- return style
+ return style;
});
/**
@@ -592,15 +596,33 @@ defineExpose({ reset, open, close });
-
+
-
+
-
+
-
+
@@ -612,26 +634,43 @@ defineExpose({ reset, open, close });
-
+
-
+
-
+
-