diff --git a/package.json b/package.json index e5fcfb14d8b6ec018c92a4e6731cfd539a0a5058..aaf6089ce8cafb8196d63ac3080cdb0afb150b65 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "postinstall": "patch-package" }, "dependencies": { - "@layui/layui-vue": "1.1.3", + "@layui/layui-vue": "1.1.5", "@vue/repl": "1.2.4", "@vueuse/core": "8.5.0", "compare-versions": "4.1.3", @@ -32,12 +32,13 @@ "@sxzz/eslint-config-prettier": "1.6.1", "@sxzz/eslint-config-vue": "1.6.4", "@types/node": "*", + "@types/prettier": "^2.6.3", "@vitejs/plugin-vue": "2.3.3", "eslint": "8.7.0", "eslint-define-config": "1.2.3", "local-pkg": "0.4.1", "patch-package": "^6.4.7", - "prettier": "2.5.1", + "prettier": "2.6.1", "standard-version": "9.3.2", "typescript": "4.7.2", "unocss": "0.34.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c1ffdcc9ad03649cdddf525543a428503aa734b5..e97f91cd40076582fc9e6c4517203bb0abff0ae7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,10 +1,11 @@ lockfileVersion: 5.3 specifiers: - '@layui/layui-vue': 1.1.3 + '@layui/layui-vue': 1.1.5 '@sxzz/eslint-config-prettier': 1.6.1 '@sxzz/eslint-config-vue': 1.6.4 '@types/node': '*' + '@types/prettier': ^2.6.3 '@vitejs/plugin-vue': 2.3.3 '@vue/repl': 1.2.4 '@vueuse/core': 8.5.0 @@ -15,7 +16,7 @@ specifiers: jszip: ^3.10.0 local-pkg: 0.4.1 patch-package: ^6.4.7 - prettier: 2.5.1 + prettier: 2.6.1 standard-version: 9.3.2 typescript: 4.7.2 unocss: 0.34.1 @@ -28,7 +29,7 @@ specifiers: vue-tsc: 0.35.0 dependencies: - '@layui/layui-vue': 1.1.3_vue@3.2.36 + '@layui/layui-vue': 1.1.5_vue@3.2.36 '@vue/repl': 1.2.4_vue@3.2.36 '@vueuse/core': 8.5.0_vue@3.2.36 compare-versions: registry.npmmirror.com/compare-versions/4.1.3 @@ -41,12 +42,13 @@ devDependencies: '@sxzz/eslint-config-prettier': registry.npmmirror.com/@sxzz/eslint-config-prettier/1.6.1_eslint@8.7.0 '@sxzz/eslint-config-vue': registry.npmmirror.com/@sxzz/eslint-config-vue/1.6.4_eslint@8.7.0+typescript@4.7.2 '@types/node': registry.npmmirror.com/@types/node/17.0.15 + '@types/prettier': 2.6.3 '@vitejs/plugin-vue': 2.3.3_vite@2.9.9+vue@3.2.36 eslint: registry.npmmirror.com/eslint/8.7.0 eslint-define-config: registry.npmmirror.com/eslint-define-config/1.2.3 local-pkg: registry.npmmirror.com/local-pkg/0.4.1 patch-package: registry.npmmirror.com/patch-package/6.4.7 - prettier: registry.npmmirror.com/prettier/2.5.1 + prettier: 2.6.1 standard-version: registry.npmmirror.com/standard-version/9.3.2 typescript: 4.7.2 unocss: 0.34.1_vite@2.9.9 @@ -151,15 +153,15 @@ packages: resolution: {integrity: sha512-GZ69m1/rG1Rb+VSVYFqLzy4Hzx1unOOvOCMfk3VAZ8bulHdi/Qab7woTwUdMTyQW+ghi9iqlpq30tly9qIUrzQ==} dev: false - /@layui/layer-vue/1.3.14: - resolution: {integrity: sha512-IgYaB7gY4MKaqSqrj0PO4XLJhKwCoJQubaNQNxHZhkXz2zQbVasvNOQh1M0vBe8yRb/ZGHgCu2VdjQlXYPejyA==} + /@layui/layer-vue/1.3.16: + resolution: {integrity: sha512-i47tlv13Kedey3xOTOTfvL1I1cpHJi6hwST8ODZL33w0ps0jJKKS91xwKlrrdc7S5Fjy78ofmWW1dYYs5VoveQ==} dev: false - /@layui/layui-vue/1.1.3_vue@3.2.36: - resolution: {integrity: sha512-5QcxXRu8g0GVi7RD5Lt7pmABOX/udfkNFi7XwKKtEQUA9YglC/9dB4ZLHUb22P3pexEulDR2siNvtum5wXAlOA==} + /@layui/layui-vue/1.1.5_vue@3.2.36: + resolution: {integrity: sha512-nuV9j9icjevG16c53lPIh3TUsRVtSpf/2WiU6/fh/q/in1HkwwmDDMwOQ0vglCWw7GdN074Gsnb3K5jJgKqmzA==} dependencies: '@layui/icons-vue': 1.0.9 - '@layui/layer-vue': 1.3.14 + '@layui/layer-vue': 1.3.16 '@vueuse/core': 8.5.0_vue@3.2.36 animate.css: 4.1.1 async-validator: 4.1.1 @@ -314,6 +316,10 @@ packages: picomatch: 2.3.1 dev: true + /@types/prettier/2.6.3: + resolution: {integrity: sha512-ymZk3LEC/fsut+/Q5qejp6R9O1rMxz3XaRHDV6kX8MrGAhOSPqVARbDi+EZvInBpw+BnCX3TD240byVkOfQsHg==} + dev: true + /@unocss/cli/0.34.1: resolution: {integrity: sha512-RHZyo70xOO/Loq17wVQoL+9Hs0QY0RbfAVfap6vk1uQMcDoZ3FVPjOvaDmpfqwwFeA6rExBplEF0jKlmsPfbuQ==} engines: {node: '>=14'} @@ -1337,6 +1343,12 @@ packages: picocolors: 1.0.0 source-map-js: 1.0.2 + /prettier/2.6.1: + resolution: {integrity: sha512-8UVbTBYGwN37Bs9LERmxCPjdvPxlEowx2urIL6urHzdb3SDq4B/Z6xLFCblrSnE4iKWcS6ziJ3aOYrc1kz/E2A==} + engines: {node: '>=10.13.0'} + hasBin: true + dev: true + /process-nextick-args/2.0.1: resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==} @@ -2163,9 +2175,9 @@ packages: eslint: registry.npmmirror.com/eslint/8.7.0 eslint-config-prettier: registry.npmmirror.com/eslint-config-prettier/8.3.0_eslint@8.7.0 eslint-define-config: registry.npmmirror.com/eslint-define-config/1.2.3 - eslint-plugin-prettier: registry.npmmirror.com/eslint-plugin-prettier/4.0.0_4660519532e4c3b0a9e5bb6623cfedf6 + eslint-plugin-prettier: registry.npmmirror.com/eslint-plugin-prettier/4.0.0_10a801f47698c0deafd43a8652d5fc0f eslint-plugin-yml: registry.npmmirror.com/eslint-plugin-yml/0.10.1_eslint@8.7.0 - prettier: registry.npmmirror.com/prettier/2.5.1 + prettier: 2.6.1 transitivePeerDependencies: - supports-color dev: true @@ -3306,7 +3318,7 @@ packages: natural-compare: registry.npmmirror.com/natural-compare/1.4.0 dev: true - registry.npmmirror.com/eslint-plugin-prettier/4.0.0_4660519532e4c3b0a9e5bb6623cfedf6: + registry.npmmirror.com/eslint-plugin-prettier/4.0.0_10a801f47698c0deafd43a8652d5fc0f: resolution: {integrity: sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.0.0.tgz} id: registry.npmmirror.com/eslint-plugin-prettier/4.0.0 name: eslint-plugin-prettier @@ -3322,7 +3334,7 @@ packages: dependencies: eslint: registry.npmmirror.com/eslint/8.7.0 eslint-config-prettier: registry.npmmirror.com/eslint-config-prettier/8.3.0_eslint@8.7.0 - prettier: registry.npmmirror.com/prettier/2.5.1 + prettier: 2.6.1 prettier-linter-helpers: registry.npmmirror.com/prettier-linter-helpers/1.0.0 dev: true @@ -4970,14 +4982,6 @@ packages: fast-diff: registry.npmmirror.com/fast-diff/1.2.0 dev: true - registry.npmmirror.com/prettier/2.5.1: - resolution: {integrity: sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/prettier/-/prettier-2.5.1.tgz} - name: prettier - version: 2.5.1 - engines: {node: '>=10.13.0'} - hasBin: true - dev: true - registry.npmmirror.com/punycode/2.1.1: resolution: {integrity: sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/punycode/-/punycode-2.1.1.tgz} name: punycode diff --git a/src/App.vue b/src/App.vue index ed3888f4b82d19ddde3f57f16456941b54844dd1..3e3547ebf56faeb3a2e063b3981e4b0649769db9 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,11 +2,12 @@ import { Repl } from '@vue/repl' import { ReplStore, preferSFC } from './store' import Header from './components/Header.vue' +import type { BuiltInParserName } from 'prettier' import type { SFCOptions } from '@vue/repl' const loading = ref(true) -const layerLoadingId = layer.load(2, {}, () => { }) +const layerLoadingId = layer.load(2, {}, () => {}) const repl = ref(null) @@ -17,12 +18,56 @@ const sfcOptions: SFCOptions = { }, } +const handleKeydown = (evt: KeyboardEvent) => { + if ((evt.ctrlKey || evt.metaKey) && evt.code === 'KeyS') { + evt.preventDefault() + return + } + if ((evt.altKey || evt.ctrlKey) && evt.shiftKey && evt.code === 'KeyF') { + evt.preventDefault() + formatCode() + return + } +} + +const formatCode = async () => { + 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), + ]) + const file = store.state.activeFile + let parser: BuiltInParserName + if (file.filename.endsWith('.vue')) { + parser = 'vue' + } else if (file.filename.endsWith('.js')) { + parser = 'babel' + } else if (file.filename.endsWith('.ts')) { + parser = 'typescript' + } else if (file.filename.endsWith('.json')) { + parser = 'json' + } else if (file.filename.endsWith('.html')) { + parser = 'html' + } else { + return + } + file.code = format(file.code, { + parser, + plugins: [parserHtml, parserTypeScript, parserBabel, parserPostcss], + semi: false, + singleQuote: true, + }) +} + const store = new ReplStore({ serializedState: location.hash.slice(1), }) store.init().then(() => { loading.value = false - layer.close(layerLoadingId); + layer.close(layerLoadingId) }) // persist state @@ -35,13 +80,12 @@ watchEffect(() => history.replaceState({}, '', store.serialize())) @@ -49,8 +93,8 @@ watchEffect(() => history.replaceState({}, '', store.serialize()))