forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorContextMenu.vue
More file actions
351 lines (325 loc) · 10.9 KB
/
Copy pathEditorContextMenu.vue
File metadata and controls
351 lines (325 loc) · 10.9 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<script setup lang='ts'>
import {
Blocks,
Bold,
ClipboardPaste,
Copy,
FileCode,
FileDown,
FileImage,
FileText,
FileUp,
Heading1,
Heading2,
Heading3,
Heading4,
Heading5,
Heading6,
Image,
Import,
Italic,
Link,
List,
ListOrdered,
RefreshCw,
RotateCcw,
Strikethrough,
Table,
Trash2,
Wand2,
} from '@lucide/vue'
import { altSign, headingLevels as baseHeadingLevels, ctrlKey, ctrlSign, shiftSign } from '@md/shared/configs'
import DEFAULT_CONTENT from '@/assets/example/markdown.md?raw'
import { useEditorFormat } from '@/composables/useEditorFormat'
import { useConfirmStore } from '@/stores/confirm'
import { useCssEditorStore } from '@/stores/cssEditor'
import { useEditorStore } from '@/stores/editor'
import { useExportStore } from '@/stores/export'
import { usePostStore } from '@/stores/post'
import { useRenderStore } from '@/stores/render'
import { useThemeStore } from '@/stores/theme'
import { useUIStore } from '@/stores/ui'
import { copyPlain } from '@/utils/clipboard'
import { normalizeFormulaInput } from '@/utils/formula'
const confirmStore = useConfirmStore()
const cssEditorStore = useCssEditorStore()
const editorStore = useEditorStore()
const exportStore = useExportStore()
const postStore = usePostStore()
const renderStore = useRenderStore()
const themeStore = useThemeStore()
const uiStore = useUIStore()
const {
toggleShowInsertFormDialog,
toggleShowUploadImgDialog,
toggleShowImportMdDialog,
toggleShowComponentDialog,
} = uiStore
const { editor } = storeToRefs(editorStore)
const { addFormat } = useEditorFormat(editor)
const headingIcons = [Heading1, Heading2, Heading3, Heading4, Heading5, Heading6]
const headingLevels = baseHeadingLevels.map((item, index) => ({
...item,
icon: headingIcons[index],
}))
// 格式化文档
async function formatContent() {
const doc = await editorStore.formatContent()
if (doc && postStore.currentPost) {
postStore.updatePostContent(postStore.currentPostId, doc)
}
}
// 导入默认内容
function importDefaultContent() {
editorStore.importContent(DEFAULT_CONTENT)
toast.success(`文档已重置`)
}
// 清空内容
function clearContent() {
editorStore.clearContent()
}
function openFormulaEditor() {
const selection = normalizeFormulaInput(editorStore.getSelection())
uiStore.openFormulaEditor({
value: selection.latex,
displayMode: selection.displayMode,
})
}
// 复制到剪贴板
async function copyToClipboard() {
const selectedText = editorStore.getSelection()
copyPlain(selectedText)
}
// 从剪贴板粘贴
async function pasteFromClipboard() {
try {
const text = await navigator.clipboard.readText()
editorStore.replaceSelection(text)
}
catch {
// 静默失败
}
}
// 重置样式确认
function resetStyleConfirm() {
confirmStore.confirm({
title: '提示',
description: '此操作将丢失本地自定义样式,是否继续?',
onConfirm: () => {
themeStore.resetStyle()
cssEditorStore.resetCssConfig()
themeStore.applyCurrentTheme()
themeStore.updateCodeTheme()
const raw = editorStore.getContent()
renderStore.render(raw)
toast.success(`样式已重置`)
},
})
}
// 导出函数
function exportEditorContent2HTML() {
exportStore.exportEditorContent2HTML()
}
function exportEditorContent2PDF() {
exportStore.exportEditorContent2PDF()
}
function exportEditorContent2MD() {
exportStore.exportEditorContent2MD(editorStore.getContent())
}
function downloadAsCardImage() {
exportStore.downloadAsCardImage()
}
</script>
<template>
<ContextMenu>
<ContextMenuTrigger>
<slot />
</ContextMenuTrigger>
<ContextMenuContent class="w-64 max-h-[80vh] overflow-y-auto">
<!-- 插入子菜单 -->
<ContextMenuSub>
<ContextMenuSubTrigger>
<Import class="mr-2 h-4 w-4" />
插入
</ContextMenuSubTrigger>
<ContextMenuSubContent class="w-48">
<ContextMenuItem @click="toggleShowUploadImgDialog()">
<Image class="mr-2 h-4 w-4" />
图片
</ContextMenuItem>
<ContextMenuItem @click="openFormulaEditor()">
<span class="mr-2 inline-flex h-4 w-4 items-center justify-center text-xs font-semibold">ƒ</span>
公式
</ContextMenuItem>
<ContextMenuItem @click="toggleShowInsertFormDialog()">
<Table class="mr-2 h-4 w-4" />
表格
</ContextMenuItem>
<ContextMenuItem @click="toggleShowComponentDialog()">
<Blocks class="mr-2 h-4 w-4" />
组件
</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<!-- 格式化子菜单 -->
<ContextMenuSub>
<ContextMenuSubTrigger>
<Wand2 class="mr-2 h-4 w-4" />
文本格式
</ContextMenuSubTrigger>
<ContextMenuSubContent class="w-48">
<ContextMenuItem @click="addFormat(`${ctrlKey}-B`)">
<Bold class="mr-2 h-4 w-4" />
加粗
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">B</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem @click="addFormat(`${ctrlKey}-I`)">
<Italic class="mr-2 h-4 w-4" />
斜体
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">I</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem @click="addFormat(`${ctrlKey}-D`)">
<Strikethrough class="mr-2 h-4 w-4" />
删除线
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">D</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem @click="addFormat(`${ctrlKey}-K`)">
<Link class="mr-2 h-4 w-4" />
超链接
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">K</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem @click="addFormat(`${ctrlKey}-E`)">
<FileCode class="mr-2 h-4 w-4" />
行内代码
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">E</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<!-- 标题子菜单 -->
<ContextMenuSub>
<ContextMenuSubTrigger>
<Heading1 class="mr-2 h-4 w-4" />
标题
</ContextMenuSubTrigger>
<ContextMenuSubContent class="w-48">
<ContextMenuItem
v-for="{ level, icon, label } in headingLevels"
:key="level"
@click="addFormat(`${ctrlKey}-${level}`)"
>
<component :is="icon" class="mr-2 h-4 w-4" />
{{ label }}
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ level }}</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<!-- 列表 -->
<ContextMenuItem @click="addFormat(`${ctrlKey}-U`)">
<List class="mr-2 h-4 w-4" />
无序列表
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">U</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem @click="addFormat(`${ctrlKey}-O`)">
<ListOrdered class="mr-2 h-4 w-4" />
有序列表
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">O</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSeparator />
<!-- 导入导出操作 -->
<ContextMenuItem @click="toggleShowImportMdDialog(true)">
<FileUp class="mr-2 h-4 w-4" />
导入 .md 文档
</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger>
<FileDown class="mr-2 h-4 w-4" />
导出
</ContextMenuSubTrigger>
<ContextMenuSubContent class="w-48">
<ContextMenuItem @click="exportEditorContent2MD()">
<FileDown class="mr-2 h-4 w-4" />
导出 .md 文档
</ContextMenuItem>
<ContextMenuItem @click="exportEditorContent2HTML()">
<FileCode class="mr-2 h-4 w-4" />
导出 .html
</ContextMenuItem>
<ContextMenuItem @click="exportEditorContent2PDF()">
<FileText class="mr-2 h-4 w-4" />
导出 .pdf
</ContextMenuItem>
<ContextMenuItem @click="downloadAsCardImage()">
<FileImage class="mr-2 h-4 w-4" />
导出 .png
</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<ContextMenuSeparator />
<!-- 文档操作 -->
<ContextMenuItem @click="formatContent()">
<Wand2 class="mr-2 h-4 w-4" />
格式化
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ altSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ shiftSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">F</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem @click="importDefaultContent()">
<RefreshCw class="mr-2 h-4 w-4" />
重置文档
</ContextMenuItem>
<ContextMenuItem @click="resetStyleConfirm()">
<RotateCcw class="mr-2 h-4 w-4" />
重置样式
</ContextMenuItem>
<ContextMenuItem @click="clearContent()">
<Trash2 class="mr-2 h-4 w-4" />
清空内容
</ContextMenuItem>
<ContextMenuSeparator />
<!-- 编辑操作 -->
<ContextMenuItem @click="copyToClipboard()">
<Copy class="mr-2 h-4 w-4" />
复制
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">C</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem @click="pasteFromClipboard">
<ClipboardPaste class="mr-2 h-4 w-4" />
粘贴
<ContextMenuShortcut>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">{{ ctrlSign }}</kbd>
<kbd class="mx-1 bg-gray-2 dark:bg-stone-9">V</kbd>
</ContextMenuShortcut>
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
</template>