From 8f96aba9d82d5b3fc1763db9bf4bffd6935180e6 Mon Sep 17 00:00:00 2001 From: pataar Date: Mon, 7 Mar 2022 18:44:30 +0100 Subject: [PATCH] feat: add responsive component (#704) --- package-lock.json | 53 +++++++++++++++++++++++ package.json | 3 ++ src/components/mixins/responsive.ts | 48 ++++++++++++++++++++ src/components/panels/GcodefilesPanel.vue | 2 + src/components/ui/Responsive.vue | 26 +++++++++++ src/{plugins => directives}/longpress.ts | 0 src/directives/responsive-class.ts | 16 +++++++ src/main.ts | 6 ++- tsconfig.json | 1 + 9 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/components/mixins/responsive.ts create mode 100644 src/components/ui/Responsive.vue rename src/{plugins => directives}/longpress.ts (100%) create mode 100644 src/directives/responsive-class.ts diff --git a/package-lock.json b/package-lock.json index 67ac5b31..f5dcf3f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,9 +26,11 @@ "echarts": "^5.2.2", "echarts-gl": "^2.0.8", "js-sha256": "^0.9.0", + "lodash.throttle": "^4.1.1", "overlayscrollbars": "^1.13.1", "overlayscrollbars-vue": "^0.2.2", "regenerator-runtime": "^0.13.9", + "resize-observer-polyfill": "^1.5.1", "semver": "^7.3.5", "uuid": "^8.3.2", "vue": "^2.6.14", @@ -47,6 +49,7 @@ "devDependencies": { "@intlify/vite-plugin-vue-i18n": "^2.5.0", "@mdi/js": "^6.5.95", + "@types/lodash.throttle": "^4.1.6", "@types/semver": "^7.3.8", "@types/uuid": "^8.3.1", "@typescript-eslint/eslint-plugin": "^5.11.0", @@ -2724,6 +2727,21 @@ "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, + "node_modules/@types/lodash": { + "version": "4.14.179", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.179.tgz", + "integrity": "sha512-uwc1x90yCKqGcIOAT6DwOSuxnrAbpkdPsUOZtwrXb4D/6wZs+6qG7QnIawDuZWg0sWpxl+ltIKCaLoMlna678w==", + "dev": true + }, + "node_modules/@types/lodash.throttle": { + "version": "4.1.6", + "resolved": "https://registry.npmjs.org/@types/lodash.throttle/-/lodash.throttle-4.1.6.tgz", + "integrity": "sha512-/UIH96i/sIRYGC60NoY72jGkCJtFN5KVPhEMMMTjol65effe1gPn0tycJqV5tlSwMTzX8FqzB5yAj0rfGHTPNg==", + "dev": true, + "dependencies": { + "@types/lodash": "*" + } + }, "node_modules/@types/node": { "version": "16.11.26", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.11.26.tgz", @@ -6917,6 +6935,11 @@ "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=", "dev": true }, + "node_modules/lodash.throttle": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.throttle/-/lodash.throttle-4.1.1.tgz", + "integrity": "sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ=" + }, "node_modules/log-symbols": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", @@ -7637,6 +7660,11 @@ "resolved": "https://registry.npmjs.org/resize-detector/-/resize-detector-0.3.0.tgz", "integrity": "sha512-R/tCuvuOHQ8o2boRP6vgx8hXCCy87H1eY9V5imBYeVNyNVpuL9ciReSccLj2gDcax9+2weXy3bc8Vv+NRXeEvQ==" }, + "node_modules/resize-observer-polyfill": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==" + }, "node_modules/resolve": { "version": "1.22.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.0.tgz", @@ -11435,6 +11463,21 @@ "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, + "@types/lodash": { + "version": "4.14.179", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.179.tgz", + "integrity": "sha512-uwc1x90yCKqGcIOAT6DwOSuxnrAbpkdPsUOZtwrXb4D/6wZs+6qG7QnIawDuZWg0sWpxl+ltIKCaLoMlna678w==", + "dev": true + }, + "@types/lodash.throttle": { + "version": "4.1.6", + "resolved": "https://registry.npmjs.org/@types/lodash.throttle/-/lodash.throttle-4.1.6.tgz", + "integrity": "sha512-/UIH96i/sIRYGC60NoY72jGkCJtFN5KVPhEMMMTjol65effe1gPn0tycJqV5tlSwMTzX8FqzB5yAj0rfGHTPNg==", + "dev": true, + "requires": { + "@types/lodash": "*" + } + }, "@types/node": { "version": "16.11.26", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.11.26.tgz", @@ -14416,6 +14459,11 @@ "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=", "dev": true }, + "lodash.throttle": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.throttle/-/lodash.throttle-4.1.1.tgz", + "integrity": "sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ=" + }, "log-symbols": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", @@ -14953,6 +15001,11 @@ "resolved": "https://registry.npmjs.org/resize-detector/-/resize-detector-0.3.0.tgz", "integrity": "sha512-R/tCuvuOHQ8o2boRP6vgx8hXCCy87H1eY9V5imBYeVNyNVpuL9ciReSccLj2gDcax9+2weXy3bc8Vv+NRXeEvQ==" }, + "resize-observer-polyfill": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==" + }, "resolve": { "version": "1.22.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.0.tgz", diff --git a/package.json b/package.json index 5304642f..ca120097 100644 --- a/package.json +++ b/package.json @@ -40,9 +40,11 @@ "echarts": "^5.2.2", "echarts-gl": "^2.0.8", "js-sha256": "^0.9.0", + "lodash.throttle": "^4.1.1", "overlayscrollbars": "^1.13.1", "overlayscrollbars-vue": "^0.2.2", "regenerator-runtime": "^0.13.9", + "resize-observer-polyfill": "^1.5.1", "semver": "^7.3.5", "uuid": "^8.3.2", "vue": "^2.6.14", @@ -61,6 +63,7 @@ "devDependencies": { "@intlify/vite-plugin-vue-i18n": "^2.5.0", "@mdi/js": "^6.5.95", + "@types/lodash.throttle": "^4.1.6", "@types/semver": "^7.3.8", "@types/uuid": "^8.3.1", "@typescript-eslint/eslint-plugin": "^5.11.0", diff --git a/src/components/mixins/responsive.ts b/src/components/mixins/responsive.ts new file mode 100644 index 00000000..008063aa --- /dev/null +++ b/src/components/mixins/responsive.ts @@ -0,0 +1,48 @@ +import Component from 'vue-class-component' +import BaseMixin from './base' +import { Prop } from 'vue-property-decorator' +import throttle from 'lodash.throttle' + +export type ResponsiveElement = { + is: { + [key: string]: boolean + } +} + +@Component +export default class ResponsiveMixin extends BaseMixin { + @Prop() protected declare breakpoints: { + [key: string]: (el: DOMRect) => boolean + } + + observer?: ResizeObserver + + el: ResponsiveElement = { + is: {}, + } + + mounted() { + if (this.breakpoints) { + this.$nextTick(() => { + this.observer = new ResizeObserver(throttle(this.onResize, 50)) + if (this.$el instanceof Element) { + this.observer.observe(this.$el) + } + }) + } + } + + beforeDestroy() { + if (this.$el instanceof Element) { + this.observer?.unobserve(this.$el) + } + } + + private onResize(entries: ResizeObserverEntry[]) { + const cr = entries[0].contentRect + const conds = this.breakpoints + for (const breakpoint in conds) { + this.$set(this.el.is, breakpoint, conds[breakpoint](cr)) + } + } +} diff --git a/src/components/panels/GcodefilesPanel.vue b/src/components/panels/GcodefilesPanel.vue index 83c41a1c..ff8d22cc 100644 --- a/src/components/panels/GcodefilesPanel.vue +++ b/src/components/panels/GcodefilesPanel.vue @@ -561,6 +561,7 @@ import { mdiPencil, mdiDelete, mdiCloseThick, + mdiClose, } from '@mdi/js' interface draggingFile { @@ -609,6 +610,7 @@ export default class GcodefilesPanel extends Mixins(BaseMixin) { mdiPlay = mdiPlay mdiPlaylistPlus = mdiPlaylistPlus mdiFire = mdiFire + mdiClose = mdiClose mdiVideo3d = mdiVideo3d mdiCloudDownload = mdiCloudDownload mdiRenameBox = mdiRenameBox diff --git a/src/components/ui/Responsive.vue b/src/components/ui/Responsive.vue new file mode 100644 index 00000000..489d0d82 --- /dev/null +++ b/src/components/ui/Responsive.vue @@ -0,0 +1,26 @@ + + + diff --git a/src/plugins/longpress.ts b/src/directives/longpress.ts similarity index 100% rename from src/plugins/longpress.ts rename to src/directives/longpress.ts diff --git a/src/directives/responsive-class.ts b/src/directives/responsive-class.ts new file mode 100644 index 00000000..21f03eb4 --- /dev/null +++ b/src/directives/responsive-class.ts @@ -0,0 +1,16 @@ +import throttle from 'lodash.throttle' +import Vue from 'vue' + +Vue.directive('responsive-class', { + inserted(el, conds) { + const handleResize = throttle((entries: ResizeObserverEntry[]) => { + const cr = entries[0].contentRect + for (const breakpoint in conds.value) { + el.classList.toggle(breakpoint, conds.value[breakpoint](cr)) + } + }, 50) + + const observer = new ResizeObserver(handleResize) + observer.observe(el) + }, +}) diff --git a/src/main.ts b/src/main.ts index 5c38fd14..38054ca2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,10 @@ import 'regenerator-runtime' // async polyfill used by the gcodeviewer +import 'resize-observer-polyfill' // polyfill needed by the responsive class detection import Vue from 'vue' import App from '@/App.vue' import vuetify from '@/plugins/vuetify' import i18n from '@/plugins/i18n' -import './plugins/longpress' import store from '@/store' import router from '@/plugins/router' @@ -50,6 +50,10 @@ Vue.use(OverlayScrollbarsPlugin, { }, }) +// Directives +import './directives/longpress' +import './directives/responsive-class' + // Echarts import ECharts from 'vue-echarts' import { use } from 'echarts/core' diff --git a/tsconfig.json b/tsconfig.json index 420ce6c7..6d5e5f9a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ "allowSyntheticDefaultImports": true, "experimentalDecorators": true, "sourceMap": true, + "skipLibCheck": true, "baseUrl": ".", "types": [ "cypress",