mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-20 23:11:43 +01:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56230ea915 | ||
|
|
126b5fcfd4 | ||
|
|
d3536d8768 | ||
|
|
59f62d322f | ||
|
|
b85a8e7203 |
@@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||||
|
|
||||||
|
### [1.2.11](https://github.com/nuxtlabs/ui/compare/v1.2.10...v1.2.11) (2023-05-04)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **defineShortcuts:** use `useEventListener` ([#150](https://github.com/nuxtlabs/ui/issues/150)) ([59f62d3](https://github.com/nuxtlabs/ui/commit/59f62d322f07919d16a8d35340c3aa038cd09520))
|
||||||
|
|
||||||
### [1.2.10](https://github.com/nuxtlabs/ui/compare/v1.2.9...v1.2.10) (2023-04-07)
|
### [1.2.10](https://github.com/nuxtlabs/ui/compare/v1.2.9...v1.2.10) (2023-04-07)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nuxthq/ui",
|
"name": "@nuxthq/ui",
|
||||||
"version": "1.2.10",
|
"version": "1.2.11",
|
||||||
"repository": "https://github.com/nuxtlabs/ui",
|
"repository": "https://github.com/nuxtlabs/ui",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import type { Ref, ComputedRef } from 'vue'
|
import type { ComputedRef, WatchSource } from 'vue'
|
||||||
import { logicAnd, logicNot } from '@vueuse/math'
|
import { logicAnd, logicNot } from '@vueuse/math'
|
||||||
import { computed, onMounted, onBeforeUnmount } from 'vue'
|
import { useEventListener } from '@vueuse/core'
|
||||||
|
import { computed } from 'vue'
|
||||||
import { useShortcuts } from './useShortcuts'
|
import { useShortcuts } from './useShortcuts'
|
||||||
|
|
||||||
export interface ShortcutConfig {
|
export interface ShortcutConfig {
|
||||||
handler: Function
|
handler: Function
|
||||||
usingInput?: string | boolean
|
usingInput?: string | boolean
|
||||||
whenever?: Ref<Boolean>[]
|
whenever?: WatchSource<Boolean>[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ShortcutsConfig {
|
export interface ShortcutsConfig {
|
||||||
@@ -52,58 +53,51 @@ export const defineShortcuts = (config: ShortcutsConfig) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
// Map config to full detailled shortcuts
|
||||||
// Map config to full detailled shortcuts
|
shortcuts = Object.entries(config).map(([key, shortcutConfig]) => {
|
||||||
shortcuts = Object.entries(config).map(([key, shortcutConfig]) => {
|
if (!shortcutConfig) {
|
||||||
if (!shortcutConfig) {
|
return null
|
||||||
return null
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Parse key and modifiers
|
// Parse key and modifiers
|
||||||
const keySplit = key.toLowerCase().split('_').map(k => k)
|
const keySplit = key.toLowerCase().split('_').map(k => k)
|
||||||
let shortcut: Partial<Shortcut> = {
|
let shortcut: Partial<Shortcut> = {
|
||||||
key: keySplit.filter(k => !['meta', 'ctrl', 'shift', 'alt'].includes(k)).join('_'),
|
key: keySplit.filter(k => !['meta', 'ctrl', 'shift', 'alt'].includes(k)).join('_'),
|
||||||
metaKey: keySplit.includes('meta'),
|
metaKey: keySplit.includes('meta'),
|
||||||
ctrlKey: keySplit.includes('ctrl'),
|
ctrlKey: keySplit.includes('ctrl'),
|
||||||
shiftKey: keySplit.includes('shift'),
|
shiftKey: keySplit.includes('shift'),
|
||||||
altKey: keySplit.includes('alt')
|
altKey: keySplit.includes('alt')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert Meta to Ctrl for non-MacOS
|
// Convert Meta to Ctrl for non-MacOS
|
||||||
if (!macOS.value && shortcut.metaKey && !shortcut.ctrlKey) {
|
if (!macOS.value && shortcut.metaKey && !shortcut.ctrlKey) {
|
||||||
shortcut.metaKey = false
|
shortcut.metaKey = false
|
||||||
shortcut.ctrlKey = true
|
shortcut.ctrlKey = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve handler function
|
// Retrieve handler function
|
||||||
if (typeof shortcutConfig === 'function') {
|
if (typeof shortcutConfig === 'function') {
|
||||||
shortcut.handler = shortcutConfig
|
shortcut.handler = shortcutConfig
|
||||||
} else if (typeof shortcutConfig === 'object') {
|
} else if (typeof shortcutConfig === 'object') {
|
||||||
shortcut = { ...shortcut, handler: shortcutConfig.handler }
|
shortcut = { ...shortcut, handler: shortcutConfig.handler }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!shortcut.handler) {
|
if (!shortcut.handler) {
|
||||||
// eslint-disable-next-line no-console
|
console.trace('[Shortcut] Invalid value')
|
||||||
console.trace('[Shortcut] Invalid value')
|
return null
|
||||||
return null
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Create shortcut computed
|
// Create shortcut computed
|
||||||
const conditions = []
|
const conditions: ComputedRef<Boolean>[] = []
|
||||||
if (!(shortcutConfig as ShortcutConfig).usingInput) {
|
if (!(shortcutConfig as ShortcutConfig).usingInput) {
|
||||||
conditions.push(logicNot(usingInput))
|
conditions.push(logicNot(usingInput))
|
||||||
} else if (typeof (shortcutConfig as ShortcutConfig).usingInput === 'string') {
|
} else if (typeof (shortcutConfig as ShortcutConfig).usingInput === 'string') {
|
||||||
conditions.push(computed(() => usingInput.value === (shortcutConfig as ShortcutConfig).usingInput))
|
conditions.push(computed(() => usingInput.value === (shortcutConfig as ShortcutConfig).usingInput))
|
||||||
}
|
}
|
||||||
shortcut.condition = logicAnd(...conditions, ...((shortcutConfig as ShortcutConfig).whenever || []))
|
shortcut.condition = logicAnd(...conditions, ...((shortcutConfig as ShortcutConfig).whenever || []))
|
||||||
|
|
||||||
return shortcut as Shortcut
|
return shortcut as Shortcut
|
||||||
}).filter(Boolean) as Shortcut[]
|
}).filter(Boolean) as Shortcut[]
|
||||||
|
|
||||||
document.addEventListener('keydown', onKeyDown)
|
useEventListener('keydown', onKeyDown)
|
||||||
})
|
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
|
||||||
document.removeEventListener('keydown', onKeyDown)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user