diff --git a/package.json b/package.json index 5554c339..2063cf1a 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "dependencies": { "@iconify/iconify": "^2.0.0-rc.2", - "@vueuse/core": "4.0.0-rc.3", + "@vueuse/core": "^4.0.0-rc.3", "ant-design-vue": "2.0.0-beta.15", "apexcharts": "3.22.0", "axios": "^0.21.0", @@ -33,7 +33,7 @@ "nprogress": "^0.2.0", "path-to-regexp": "^6.2.0", "qrcode": "^1.4.4", - "vditor": "^3.6.2", + "vditor": "^3.6.3", "vue": "^3.0.2", "vue-i18n": "^9.0.0-beta.8", "vue-router": "^4.0.0-rc.3", @@ -45,7 +45,7 @@ "devDependencies": { "@commitlint/cli": "^11.0.0", "@commitlint/config-conventional": "^11.0.0", - "@iconify/json": "^1.1.260", + "@iconify/json": "^1.1.261", "@ls-lint/ls-lint": "^1.9.2", "@purge-icons/generated": "^0.4.1", "@types/echarts": "^4.9.1", @@ -69,7 +69,7 @@ "conventional-changelog-cli": "^2.1.1", "conventional-changelog-custom-config": "^0.3.1", "cross-env": "^7.0.2", - "dot-prop": "^6.0.0", + "dot-prop": "^6.0.1", "dotenv": "^8.2.0", "eslint": "^7.13.0", "eslint-config-prettier": "^6.15.0", @@ -93,7 +93,7 @@ "stylelint-order": "^4.1.0", "tasksfile": "^5.1.1", "ts-node": "^9.0.0", - "typescript": "^4.0.5", + "typescript": "^4.1.2", "vite": "^1.0.0-rc.9", "vite-plugin-html": "^1.0.0-beta.2", "vite-plugin-mock": "^1.0.6", diff --git a/src/router/index.ts b/src/router/index.ts index 54246eca..851f398a 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -3,28 +3,17 @@ import type { App } from 'vue'; import { createRouter, createWebHashHistory } from 'vue-router'; -import { scrollWaiter } from './scrollWaiter'; - import { createGuard } from './guard/'; import { basicRoutes } from './routes/'; +import { scrollBehavior } from './scrollBehaviour'; // app router const router = createRouter({ history: createWebHashHistory(), routes: basicRoutes as RouteRecordRaw[], strict: true, - scrollBehavior: async (to, from, savedPosition) => { - await scrollWaiter.wait(); - if (savedPosition) { - return savedPosition; - } else { - if (to.matched.every((record, i) => from.matched[i] !== record)) { - return { left: 0, top: 0 }; - } - return false; - } - }, + scrollBehavior: scrollBehavior, }); // reset router diff --git a/src/router/scrollBehaviour.ts b/src/router/scrollBehaviour.ts new file mode 100644 index 00000000..57c93526 --- /dev/null +++ b/src/router/scrollBehaviour.ts @@ -0,0 +1,58 @@ +/** + * Handles the scroll behavior on route navigation + * + * @param {object} to Route object of next page + * @param {object} from Route object of previous page + * @param {object} savedPosition Used by popstate navigations + * @returns {(object|boolean)} Scroll position or `false` + */ +// @ts-ignore +export async function scrollBehavior(to, from, savedPosition) { + await scrollWaiter.wait(); + // Use predefined scroll behavior if defined, defaults to no scroll behavior + const behavior = document.documentElement.style.scrollBehavior || 'auto'; + + // Returning the `savedPosition` (if available) will result in a native-like + // behavior when navigating with back/forward buttons + if (savedPosition) { + return { ...savedPosition, behavior }; + } + + // Scroll to anchor by returning the selector + if (to.hash) { + return { el: decodeURI(to.hash), behavior }; + } + + // Check if any matched route config has meta that discourages scrolling to top + if (to.matched.some((m: any) => m.meta.scrollToTop === false)) { + // Leave scroll as it is + return false; + } + + // Always scroll to top + return { left: 0, top: 0, behavior }; +} + +// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts +class ScrollQueue { + private resolve: (() => void) | null = null; + private promise: Promise | null = null; + + add() { + this.promise = new Promise((resolve) => { + this.resolve = resolve; + }); + } + + flush() { + this.resolve && this.resolve(); + this.resolve = null; + this.promise = null; + } + + async wait() { + await this.promise; + } +} + +export const scrollWaiter = new ScrollQueue(); diff --git a/src/router/scrollWaiter.ts b/src/router/scrollWaiter.ts deleted file mode 100644 index df234a8a..00000000 --- a/src/router/scrollWaiter.ts +++ /dev/null @@ -1,23 +0,0 @@ -// see https://github.com/vuejs/vue-router-next/blob/master/playground/scrollWaiter.ts -class ScrollQueue { - private resolve: (() => void) | null = null; - private promise: Promise | null = null; - - add() { - this.promise = new Promise((resolve) => { - this.resolve = resolve; - }); - } - - flush() { - this.resolve && this.resolve(); - this.resolve = null; - this.promise = null; - } - - async wait() { - await this.promise; - } -} - -export const scrollWaiter = new ScrollQueue(); diff --git a/src/utils/eventHub.ts b/src/utils/eventHub.ts deleted file mode 100644 index 690ca111..00000000 --- a/src/utils/eventHub.ts +++ /dev/null @@ -1,33 +0,0 @@ -class EventHub { - private cache: { [key: string]: Array<(data: any) => void> } = {}; - on(eventName: string, fn: (data: any) => void) { - this.cache[eventName] = this.cache[eventName] || []; - this.cache[eventName].push(fn); - } - - once(eventName: string, fn: (data: any) => void) { - const decor = (...args: any[]) => { - fn && fn.apply(this, args); - this.off(eventName, decor); - }; - this.on(eventName, decor); - return this; - } - - emit(eventName: string, data?: any) { - if (this.cache[eventName] === undefined) return; - this.cache[eventName].forEach((fn) => fn(data)); - } - off(eventName: string, fn: (data: any) => void) { - if (this.cache[eventName] === undefined || this.cache[eventName].length === 0) return; - const i = this.cache[eventName].indexOf(fn); - if (i === -1) return; - this.cache[eventName].splice(i, 1); - } - - clear() { - this.cache = {}; - } -} - -export default EventHub; diff --git a/src/utils/mitt.ts b/src/utils/mitt.ts new file mode 100644 index 00000000..bcc3b65c --- /dev/null +++ b/src/utils/mitt.ts @@ -0,0 +1,73 @@ +/** + * Mitt: Tiny functional event emitter / pubsub + * + * @name mitt + * @param {Array} [all] Optional array of event names to registered handler functions + * @returns {Function} The function's instance + */ +export default class Mitt { + private cache: Map void>>; + constructor(all = []) { + // A Map of event names to registered handler functions. + this.cache = new Map(all); + } + + once(type: string, handler: Fn) { + const decor = (...args: any[]) => { + handler && handler.apply(this, args); + this.off(type, decor); + }; + this.on(type, decor); + return this; + } + + /** + * Register an event handler for the given type. + * + * @param {string|symbol} type Type of event to listen for, or `"*"` for all events + * @param {Function} handler Function to call in response to given event + */ + on(type: string, handler: Fn) { + const handlers = this.cache.get(type); + const added = handlers && handlers.push(handler); + if (!added) { + this.cache.set(type, [handler]); + } + } + + /** + * Remove an event handler for the given type. + * + * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"` + * @param {Function} handler Handler function to remove + */ + off(type: string, handler: Fn) { + const handlers = this.cache.get(type); + if (handlers) { + handlers.splice(handlers.indexOf(handler) >>> 0, 1); + } + } + + /** + * Invoke all handlers for the given type. + * If present, `"*"` handlers are invoked after type-matched handlers. + * + * Note: Manually firing "*" handlers is not supported. + * + * @param {string|symbol} type The event type to invoke + * @param {*} [evt] Any value (object is recommended and powerful), passed to each handler + */ + emit(type: string, evt: any) { + for (const handler of (this.cache.get(type) || []).slice()) handler(evt); + for (const handler of (this.cache.get('*') || []).slice()) handler(type, evt); + } + + /** + * Remove all event handlers. + * + * Note: This will also remove event handlers passed via `mitt(all: EventHandlerMap)`. + */ + clear() { + this.cache.clear(); + } +} diff --git a/yarn.lock b/yarn.lock index 2f5cebce..8b29ccb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1050,10 +1050,10 @@ resolved "https://registry.npmjs.org/@iconify/iconify/-/iconify-2.0.0-rc.2.tgz#c4a95ddc06ca9b9496df03604e66fdefb39f4c4b" integrity sha512-BybEHU5/I9EQ0CcwKAqmreZ2bMnAXrqLCTptAc6vPetHMbrXdZfejP5mt57e/8PNSt/qE7BHniU5PCYA+PGIHw== -"@iconify/json@^1.1.260": - version "1.1.260" - resolved "https://registry.npmjs.org/@iconify/json/-/json-1.1.260.tgz#75bfcdcaf01f1a0092bb26f4ce7aebf357da431a" - integrity sha512-gpRn0o55mvBTCcZRb8jBtqxV/5Av01BnnVn7/FyboBNdGkEQ8EMTqJL10SDUf9TLM8s63KKSg/ZeCJj870THtA== +"@iconify/json@^1.1.261": + version "1.1.261" + resolved "https://registry.npmjs.org/@iconify/json/-/json-1.1.261.tgz#9a6986b6b36d77ca147c4be149db9a43280a8fb2" + integrity sha512-lnRk1OBqNxZ593oZyOXEMp/O+cr+lF54xaW6+F3krWdWhzxQgi0W1ffzvdiLySdbTEorQ2NvVU4e0+Af27rXPA== "@koa/cors@^3.1.0": version "3.1.0" @@ -1737,7 +1737,7 @@ vscode-languageserver-textdocument "^1.0.1" vscode-uri "^2.1.2" -"@vueuse/core@4.0.0-rc.3": +"@vueuse/core@^4.0.0-rc.3": version "4.0.0-rc.3" resolved "https://registry.npmjs.org/@vueuse/core/-/core-4.0.0-rc.3.tgz#5381ca657e10df596cd7027fc5c96b2d4b3a090c" integrity sha512-dQ/FZgo0z7kBFOvDWxuzaUrmuO8X1AlQk17e3PU1TVtG2Uu+mCvjPNbuvI2fjhTjl5rzPJawwoU2WZFj+nlFvw== @@ -3227,10 +3227,10 @@ dot-prop@^5.1.0: dependencies: is-obj "^2.0.0" -dot-prop@^6.0.0: - version "6.0.0" - resolved "https://registry.npmjs.org/dot-prop/-/dot-prop-6.0.0.tgz#bd579fd704d970981c4b05de591db648959f2ebb" - integrity sha512-xCbB8IN3IT+tdgoEPOnJmYTNJDrygGFOmiQEiVa5eAD+JEB1vTgMNhVGRnN5Eex/6amck7cdcrixb1qN9Go+GQ== +dot-prop@^6.0.1: + version "6.0.1" + resolved "https://registry.npmjs.org/dot-prop/-/dot-prop-6.0.1.tgz#fc26b3cf142b9e59b74dbd39ed66ce620c681083" + integrity sha512-tE7ztYzXHIeyvc7N+hR3oi7FIbf/NIjVP9hmAt3yMXzrQ072/fpjGLx2GxNxGxUl5V73MEqYzioOMoVhGMJ5cA== dependencies: is-obj "^2.0.0" @@ -7869,10 +7869,10 @@ typescript@^3.9.7: resolved "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa" integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw== -typescript@^4.0.5: - version "4.0.5" - resolved "https://registry.npmjs.org/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389" - integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ== +typescript@^4.1.2: + version "4.1.2" + resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.2.tgz#6369ef22516fe5e10304aae5a5c4862db55380e9" + integrity sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ== uglify-js@^3.1.4: version "3.11.6" @@ -8033,10 +8033,10 @@ vary@^1.1.2: resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= -vditor@^3.6.2: - version "3.6.2" - resolved "https://registry.npmjs.org/vditor/-/vditor-3.6.2.tgz#ee6011efa3ec563c6356ed82efbf2e00ba2e35c6" - integrity sha512-HPHHun5+IXmYGMKDWcUD83VfP1Qfncz7DmaIKoWpluJgE8ve7s+4RbFBcaEpYPXYzIuL2UTHoMnIjmTPbenOCA== +vditor@^3.6.3: + version "3.6.3" + resolved "https://registry.npmjs.org/vditor/-/vditor-3.6.3.tgz#7d006f273208869b268268453b688ad4a0de8995" + integrity sha512-skLJQrVBdeBFLdYVckfovCxNnzOR1vlgEesUFAc0WJdbfj/m4FHB7SY/LgV7KtoiK+crDPFrIdw3jkEQmhkFGA== dependencies: diff-match-patch "^1.0.5"