forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseEditorFormat.ts
More file actions
71 lines (68 loc) · 1.64 KB
/
Copy pathuseEditorFormat.ts
File metadata and controls
71 lines (68 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { EditorView } from '@codemirror/view'
import { ctrlKey } from '@md/shared/configs'
import {
applyHeading,
formatBold,
formatCode,
formatItalic,
formatLink,
formatOrderedList,
formatStrikethrough,
formatUnorderedList,
} from '@md/shared/editor'
import { unref } from 'vue'
/**
* 编辑器格式化操作 composable
* 使用泛型和 unref 来支持任意类型的 ref(包括 readonly ref)
*/
export function useEditorFormat<T extends { value: any }>(editor: T) {
function addFormat(cmd: string) {
const editorView = unref(editor) as EditorView
if (!unref(editor))
return
switch (cmd) {
case `${ctrlKey}-B`:
formatBold(editorView)
break
case `${ctrlKey}-I`:
formatItalic(editorView)
break
case `${ctrlKey}-D`:
formatStrikethrough(editorView)
break
case `${ctrlKey}-K`:
formatLink(editorView)
break
case `${ctrlKey}-E`:
formatCode(editorView)
break
case `${ctrlKey}-1`:
applyHeading(editorView, 1)
break
case `${ctrlKey}-2`:
applyHeading(editorView, 2)
break
case `${ctrlKey}-3`:
applyHeading(editorView, 3)
break
case `${ctrlKey}-4`:
applyHeading(editorView, 4)
break
case `${ctrlKey}-5`:
applyHeading(editorView, 5)
break
case `${ctrlKey}-6`:
applyHeading(editorView, 6)
break
case `${ctrlKey}-U`:
formatUnorderedList(editorView)
break
case `${ctrlKey}-O`:
formatOrderedList(editorView)
break
}
}
return {
addFormat,
}
}