|
1 | 1 | import { useAbsoluteStore } from '@/store/absolute' |
| 2 | +import { useContextStore } from '@/store/context' |
| 3 | +import { useEditorStore } from '@/store/editor' |
2 | 4 | import { useEventListener } from '@vueuse/core' |
3 | | -import { ProjectObject } from 'better-write-types' |
| 5 | +import { EntityType, ProjectObject } from 'better-write-types' |
| 6 | +import { nextTick } from 'vue' |
4 | 7 | import { useI18n } from 'vue-i18n' |
| 8 | +import useEmitter from './emitter' |
| 9 | +import { useFactory } from './factory' |
5 | 10 | import { useProject } from './project' |
| 11 | +import { useStorage } from './storage/storage' |
6 | 12 | import { useUtils } from './utils' |
7 | 13 |
|
8 | 14 | export const useListener = () => { |
9 | 15 | const ABSOLUTE = useAbsoluteStore() |
| 16 | + const CONTEXT = useContextStore() |
| 17 | + const EDITOR = useEditorStore() |
10 | 18 |
|
11 | 19 | const project = useProject() |
| 20 | + const factory = useFactory() |
12 | 21 |
|
13 | 22 | const utils = useUtils() |
14 | 23 | const { t } = useI18n() |
| 24 | + const storage = useStorage() |
| 25 | + const emitter = useEmitter() |
15 | 26 |
|
16 | 27 | const keyboard = () => { |
17 | 28 | const start = () => { |
18 | 29 | useEventListener('keydown', cb) |
19 | 30 | } |
20 | 31 |
|
21 | | - const cb = (e: KeyboardEvent) => { |
| 32 | + const cb = async (e: KeyboardEvent) => { |
| 33 | + // finder |
22 | 34 | if (e.ctrlKey && (e.key === 'f' || e.key === 'F')) { |
23 | 35 | e.preventDefault() |
24 | 36 | e.stopPropagation() |
25 | 37 |
|
26 | 38 | ABSOLUTE.shortcuts.finder = !ABSOLUTE.shortcuts.finder |
| 39 | + |
| 40 | + return |
27 | 41 | } |
28 | 42 |
|
| 43 | + // switcher |
29 | 44 | if (e.ctrlKey && (e.key === 'h' || e.key === 'H')) { |
30 | 45 | e.preventDefault() |
31 | 46 | e.stopPropagation() |
32 | 47 |
|
33 | 48 | ABSOLUTE.shortcuts.switcher = !ABSOLUTE.shortcuts.switcher |
| 49 | + |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + // commands |
| 54 | + if (e.ctrlKey) { |
| 55 | + const index = EDITOR.actives.entity.index |
| 56 | + |
| 57 | + // text attached |
| 58 | + if ( |
| 59 | + e.key === '1' || |
| 60 | + e.key === '2' || |
| 61 | + e.key === '3' || |
| 62 | + e.key === '6' || |
| 63 | + e.key === '7' |
| 64 | + ) { |
| 65 | + e.preventDefault() |
| 66 | + e.stopPropagation() |
| 67 | + |
| 68 | + await storage.normalize() |
| 69 | + |
| 70 | + const value = index + 1 |
| 71 | + |
| 72 | + let type: EntityType = 'paragraph' |
| 73 | + |
| 74 | + switch (e.key) { |
| 75 | + case '1': |
| 76 | + type = 'paragraph' |
| 77 | + break |
| 78 | + case '2': |
| 79 | + type = 'heading-two' |
| 80 | + break |
| 81 | + case '3': |
| 82 | + type = 'heading-three' |
| 83 | + break |
| 84 | + case '6': |
| 85 | + type = 'checkbox' |
| 86 | + break |
| 87 | + case '7': |
| 88 | + type = 'list' |
| 89 | + break |
| 90 | + } |
| 91 | + |
| 92 | + CONTEXT.insert(factory.entity().create(type), value) |
| 93 | + |
| 94 | + await nextTick |
| 95 | + |
| 96 | + emitter.emit('entity-text-focus', { |
| 97 | + target: value, |
| 98 | + position: 'auto', |
| 99 | + }) |
| 100 | + } |
34 | 101 | } |
35 | 102 | } |
36 | 103 |
|
|
0 commit comments