forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickCommands.ts
More file actions
86 lines (72 loc) · 2.54 KB
/
Copy pathquickCommands.ts
File metadata and controls
86 lines (72 loc) · 2.54 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { v4 as uuidv4 } from 'uuid'
import { ref, watch } from 'vue'
import { store } from '@/utils/storage'
export interface QuickCommandPersisted {
id: string
label: string
template: string // 用 {{sel}} 占位
}
export interface QuickCommandRuntime extends QuickCommandPersisted {
buildPrompt: (sel?: string) => string
}
const STORAGE_KEY = `quick_commands`
// 把持久化的对象转换为可执行的 buildPrompt
function hydrate(cmd: QuickCommandPersisted): QuickCommandRuntime {
return {
...cmd,
buildPrompt: (sel = ``) =>
cmd.template.replace(/\{\{\s*sel\s*\}\}/gi, sel),
}
}
// 4 条默认指令
const DEFAULT_COMMANDS: QuickCommandPersisted[] = [
{ id: `polish`, label: `润色`, template: `请润色以下内容:\n\n{{sel}}` },
{ id: `to-en`, label: `翻译成英文`, template: `请将以下内容翻译为英文:\n\n{{sel}}` },
{ id: `to-zh`, label: `翻译成中文`, template: `Please translate the following content into Chinese:\n\n{{sel}}` },
{ id: `summary`, label: `总结`, template: `请对以下内容进行总结:\n\n{{sel}}` },
]
export const useQuickCommandsStore = defineStore(`quickCommands`, () => {
// ---------- state ----------
const commands = ref<QuickCommandRuntime[]>([])
// ---------- helpers ----------
async function save() {
const toSave: QuickCommandPersisted[] = commands.value.map(
({ id, label, template }) => ({ id, label, template }),
)
await store.setJSON(STORAGE_KEY, toSave)
}
async function load() {
const parsed = await store.getJSON<QuickCommandPersisted[]>(STORAGE_KEY)
if (parsed && Array.isArray(parsed)) {
try {
commands.value = parsed.map(hydrate)
}
catch (e) {
console.warn(`解析快捷指令失败,已恢复默认值`, e)
commands.value = DEFAULT_COMMANDS.map(hydrate)
await save()
}
}
else {
commands.value = DEFAULT_COMMANDS.map(hydrate)
await save()
}
}
// ---------- CRUD ----------
function add(label: string, template: string) {
const id = uuidv4()
commands.value.push(hydrate({ id, label, template }))
}
function update(id: string, label: string, template: string) {
const idx = commands.value.findIndex(c => c.id === id)
if (idx !== -1)
commands.value[idx] = hydrate({ id, label, template })
}
function remove(id: string) {
commands.value = commands.value.filter(c => c.id !== id)
}
// ---------- init ----------
load()
watch(commands, save, { deep: true })
return { commands, add, update, remove }
})