Skip to content

Commit

Permalink
Update useActions
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Sep 29, 2023
1 parent 13a8aa6 commit 00d97c0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ registerActions([
id: 'reset-zoom',
label: 'Reset Zoom',
icon: 'zoom_out_map',
input: 'esc',
perform() {
viewTransform.value = initialViewTransform.value
},
Expand Down Expand Up @@ -283,6 +284,7 @@ registerActions([
{
id: 'open-project',
label: 'Open Project',
icon: 'file_open',
async perform() {
const handles = await window.showOpenFilePicker(filePickerOptions)
Expand All @@ -296,6 +298,7 @@ registerActions([
{
id: 'save-project',
label: 'Save Project',
icon: 'save',
async perform() {
if (!fileHandle.value) {
fileHandle.value = await window.showSaveFilePicker(filePickerOptions)
Expand Down
9 changes: 4 additions & 5 deletions src/tweeq/CommandPalette/CommandPalette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import * as Bndr from 'bndr-js'
import {search} from 'fast-fuzzy'
import {computed, ref, watch} from 'vue'
import {type Action, useActions} from '../action'
import {type Action, useActions} from '../useActions'
const {actions} = useActions()
// console.log(toRaw(actions))
const $popover = ref<HTMLElement | null>(null)
const searchWord = ref('')
Expand All @@ -23,7 +21,7 @@ const selectedAction = ref<null | Action>(null)
watch(filteredActions, () => {
if (filteredActions.value.length > 0) {
const notFoundInFiltered = !filteredActions.value.includes(
selectedAction.value!
selectedAction.value as any
)
if (notFoundInFiltered) {
Expand Down Expand Up @@ -51,7 +49,7 @@ function onKeydown(e: KeyboardEvent) {
}
if (selectedAction.value && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
const index = filteredActions.value.indexOf(selectedAction.value)
const index = filteredActions.value.indexOf(selectedAction.value as any)
const length = filteredActions.value.length
const move = e.key === 'ArrowDown' ? 1 : -1
Expand Down Expand Up @@ -157,3 +155,4 @@ function perform(action: Action) {
width 20px
font-size 20px
</style>
../useAction
20 changes: 17 additions & 3 deletions src/tweeq/action.ts → src/tweeq/useActions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import * as Bndr from 'bndr-js'
import {inject, InjectionKey, onBeforeUnmount, provide, reactive} from 'vue'

export interface Action {
id: string
label: string
shortLabel?: string
icon?: string
perform(...args: any): any
input?: string
perform(): any
}

const Emitters = new Map<string, Bndr.Emitter>()

const ActionsKey: InjectionKey<Record<string, Action>> = Symbol('tqActions')

const keyboard = Bndr.keyboard()

export function provideActions() {
const allActions = reactive<Record<string, Action>>({})

Expand All @@ -22,22 +28,30 @@ export function provideActions() {
}

allActions[action.id] = action

if (action.input) {
const emitter = keyboard.keydown(action.input)
emitter?.on(() => action.perform())

Emitters.set(action.id, emitter)
}
}

onBeforeUnmount(() => {
for (const action of actions) {
delete allActions[action.id]
Emitters.get(action.id)?.dispose()
}
})
}

function performAction(id: string, ...args: any) {
function performAction(id: string) {
const action = allActions[id]
if (!action) {
throw new Error(`Action ${id} is not registered`)
}

action.perform(...args)
action.perform()
}

return {registerActions, performAction}
Expand Down
2 changes: 1 addition & 1 deletion src/tweeq/useTweeq.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {provideActions} from './action'
import {provideActions} from './useActions'
import {provideAppStorage} from './useAppStorage'
import {ColorMode, provideTheme} from './useTheme'

Expand Down

0 comments on commit 00d97c0

Please sign in to comment.