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 传递响应式变量。