-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathCodemirrorEditor.vue
More file actions
731 lines (644 loc) · 19.7 KB
/
CodemirrorEditor.vue
File metadata and controls
731 lines (644 loc) · 19.7 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
<script setup lang="ts">
import type { ComponentPublicInstance } from 'vue'
import { Compartment, EditorState } from '@codemirror/state'
import { EditorView } from '@codemirror/view'
import { markdownSetup, theme } from '@md/shared/editor'
import imageCompression from 'browser-image-compression'
import { Eye, Pen } from 'lucide-vue-next'
import { SidebarAIToolbar } from '@/components/ai'
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from '@/components/ui/resizable'
import { SearchTab } from '@/components/ui/search-tab'
import { checkImage, toBase64 } from '@/utils'
import { fileUpload } from '@/utils/file'
const store = useStore()
const displayStore = useDisplayStore()
const { isDark, output, editor } = storeToRefs(store)
const { editorRefresh } = store
const { toggleShowUploadImgDialog } = displayStore
const backLight = ref(false)
const isCoping = ref(false)
// 辅助函数:查找 CodeMirror 滚动容器
function findCodeMirrorScroller(): HTMLElement | null {
return document.querySelector<HTMLElement>(`.cm-scroller`)
|| document.querySelector<HTMLElement>(`.CodeMirror-scroll`)
}
function startCopy() {
backLight.value = true
isCoping.value = true
}
// 拷贝结束
function endCopy() {
backLight.value = false
setTimeout(() => {
isCoping.value = false
}, 800)
}
const showEditor = ref(true)
// 切换编辑/预览视图(仅限移动端)
function toggleView() {
showEditor.value = !showEditor.value
}
// AI 工具箱已移到侧边栏
const previewRef = useTemplateRef<HTMLDivElement>(`previewRef`)
const timeout = ref<NodeJS.Timeout>()
const codeMirrorView = ref<EditorView | null>(null)
const themeCompartment = new Compartment()
// 使浏览区与编辑区滚动条建立同步联系
function leftAndRightScroll() {
const scrollCB = (text: string) => {
// AIPolishBtnRef.value?.close()
let source: HTMLElement | null
let target: HTMLElement | null
clearTimeout(timeout.value)
if (text === `preview`) {
source = previewRef.value!
target = findCodeMirrorScroller()
if (!target) {
console.warn(`Cannot find CodeMirror scroll container`)
return
}
// CodeMirror v6 使用 DOM 事件
const scrollEl = findCodeMirrorScroller()
if (scrollEl) {
scrollEl.removeEventListener(`scroll`, editorScrollCB)
timeout.value = setTimeout(() => {
scrollEl.addEventListener(`scroll`, editorScrollCB)
}, 300)
}
}
else {
source = findCodeMirrorScroller()
target = previewRef.value!
if (!source) {
console.warn(`Cannot find CodeMirror scroll container`)
return
}
target.removeEventListener(`scroll`, previewScrollCB, false)
timeout.value = setTimeout(() => {
target!.addEventListener(`scroll`, previewScrollCB, false)
}, 300)
}
if (!source || !target) {
return
}
const sourceHeight = source.scrollHeight - source.offsetHeight
const targetHeight = target.scrollHeight - target.offsetHeight
if (sourceHeight <= 0 || targetHeight <= 0) {
return
}
const percentage = source.scrollTop / sourceHeight
const height = percentage * targetHeight
target.scrollTo(0, height)
}
function editorScrollCB() {
scrollCB(`editor`)
}
function previewScrollCB() {
scrollCB(`preview`)
}
if (previewRef.value) {
previewRef.value.addEventListener(`scroll`, previewScrollCB, false)
}
const scrollEl = findCodeMirrorScroller()
if (scrollEl) {
scrollEl.addEventListener(`scroll`, editorScrollCB)
}
}
onMounted(() => {
setTimeout(() => {
leftAndRightScroll()
}, 300)
})
const searchTabRef
= useTemplateRef<InstanceType<typeof SearchTab>>(`searchTabRef`)
// 用于存储待处理的搜索请求
const pendingSearchRequest = ref<{ selected: string } | null>(null)
function openSearchWithSelection(view: EditorView) {
const selection = view.state.selection.main
const selected = view.state.doc.sliceString(selection.from, selection.to).trim()
if (searchTabRef.value) {
// SearchTab 已准备好,直接使用
if (selected) {
searchTabRef.value.setSearchWord(selected)
}
else {
searchTabRef.value.showSearchTab = true
}
}
else {
// SearchTab 还没准备好,保存请求
pendingSearchRequest.value = { selected }
}
}
// 监听 searchTabRef 的变化,处理待处理的请求
watch(searchTabRef, (newRef) => {
if (newRef && pendingSearchRequest.value) {
const { selected } = pendingSearchRequest.value
if (selected) {
newRef.setSearchWord(selected)
}
else {
newRef.showSearchTab = true
}
pendingSearchRequest.value = null
}
})
function handleGlobalKeydown(e: KeyboardEvent) {
// 处理 ESC 键关闭搜索
const editorView = codeMirrorView.value
if (e.key === `Escape` && searchTabRef.value?.showSearchTab) {
searchTabRef.value.showSearchTab = false
e.preventDefault()
editorView?.focus()
}
}
onMounted(() => {
// 使用较低优先级确保 CodeMirror 键盘事件先处理
document.addEventListener(`keydown`, handleGlobalKeydown, { passive: false, capture: false })
})
function beforeUpload(file: File) {
// validate image
const checkResult = checkImage(file)
if (!checkResult.ok) {
toast.error(checkResult.msg)
return false
}
// check image host
const imgHost = localStorage.getItem(`imgHost`) || `default`
localStorage.setItem(`imgHost`, imgHost)
const config = localStorage.getItem(`${imgHost}Config`)
const isValidHost = imgHost === `default` || config
if (!isValidHost) {
toast.error(`请先配置 ${imgHost} 图床参数`)
return false
}
return true
}
// 图片上传结束
function uploaded(imageUrl: string) {
if (!imageUrl) {
toast.error(`上传图片未知异常`)
return
}
setTimeout(() => {
toggleShowUploadImgDialog(false)
}, 1000)
// 上传成功,插入图片
const markdownImage = ``
// 将 Markdown 形式的 URL 插入编辑框光标所在位置
if (codeMirrorView.value) {
codeMirrorView.value.dispatch(codeMirrorView.value.state.replaceSelection(`\n${markdownImage}\n`))
}
toast.success(`图片上传成功`)
}
const isImgLoading = ref(false)
async function compressImage(file: File) {
const options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true,
}
const compressedFile = await imageCompression(file, options)
return compressedFile
}
async function uploadImage(
file: File,
cb?: { (url: any, data: string): void, (arg0: unknown): void } | undefined,
applyUrl?: boolean,
) {
try {
isImgLoading.value = true
// compress image if useCompression is true
const useCompression = localStorage.getItem(`useCompression`) === `true`
if (useCompression) {
file = await compressImage(file)
}
const base64Content = await toBase64(file)
const url = await fileUpload(base64Content, file)
if (cb) {
cb(url, base64Content)
}
else {
uploaded(url)
}
if (applyUrl) {
return uploaded(url)
}
}
catch (err) {
toast.error((err as any).message)
}
finally {
isImgLoading.value = false
}
}
// 从文件列表中查找一个 md 文件并解析
async function getMd({ list }: { list: { path: string, file: File }[] }) {
return new Promise<{ str: string, file: File, path: string }>((resolve) => {
const { path, file } = list.find(item => item.path.match(/\.md$/))!
const reader = new FileReader()
reader.readAsText(file!, `UTF-8`)
reader.onload = (evt) => {
resolve({
str: evt.target!.result as string,
file,
path,
})
}
})
}
// 转换文件系统句柄中的文件为文件列表
async function showFileStructure(root: any) {
const result = []
let cwd = ``
try {
const dirs = [root]
for (const dir of dirs) {
cwd += `${dir.name}/`
for await (const [, handle] of dir) {
if (handle.kind === `file`) {
result.push({
path: cwd + handle.name,
file: await handle.getFile(),
})
}
else {
result.push({
path: `${cwd + handle.name}/`,
})
dirs.push(handle)
}
}
}
}
catch (err) {
console.error(err)
}
return result
}
// 上传 md 中的图片
async function uploadMdImg({
md,
list,
}: {
md: { str: string, path: string, file: File }
list: { path: string, file: File }[]
}) {
// 获取所有相对地址的图片
const mdImgList = [...(md.str.matchAll(/!\[(.*?)\]\((.*?)\)/g) || [])].filter(item => item)
const root = md.path.match(/.+?\//)![0]
const resList = await Promise.all<{ matchStr: string, url: string }>(
mdImgList.map((item) => {
return new Promise((resolve) => {
let [, , matchStr] = item
matchStr = matchStr.replace(/^.\//, ``) // 处理 ./img/ 为 img/ 统一相对路径风格
const { file }
= list.find(f => f.path === `${root}${matchStr}`) || {}
uploadImage(file!, url => resolve({ matchStr, url }))
})
}),
)
resList.forEach((item) => {
md.str = md.str
.replace(`](./${item.matchStr})`, `](${item.url})`)
.replace(`](${item.matchStr})`, `](${item.url})`)
})
if (codeMirrorView.value) {
codeMirrorView.value.dispatch({
changes: { from: 0, to: codeMirrorView.value.state.doc.length, insert: md.str },
})
}
}
const codeMirrorWrapper = useTemplateRef<ComponentPublicInstance<HTMLDivElement>>(`codeMirrorWrapper`)
// 转换 markdown 中的本地图片为线上图片
// todo 处理事件覆盖
function mdLocalToRemote() {
const dom = codeMirrorWrapper.value!
dom.ondragover = evt => evt.preventDefault()
dom.ondrop = async (evt) => {
evt.preventDefault()
if (evt.dataTransfer == null || !Array.isArray(evt.dataTransfer.items)) {
return
}
for (const item of evt.dataTransfer.items.filter(item => item.kind === `file`)) {
item
.getAsFileSystemHandle()
.then(async (handle: { kind: string, getFile: () => any }) => {
if (handle.kind === `directory`) {
const list = (await showFileStructure(handle)) as {
path: string
file: File
}[]
const md = await getMd({ list })
uploadMdImg({ md, list })
}
else {
const file = await handle.getFile()
console.log(`file`, file)
beforeUpload(file) && uploadImage(file)
}
})
}
}
}
const changeTimer = ref<NodeJS.Timeout>()
const editorRef = useTemplateRef<HTMLDivElement>(`editorRef`)
const progressValue = ref(0)
function createFormTextArea(dom: HTMLDivElement) {
// 创建编辑器状态
const state = EditorState.create({
doc: store.posts[store.currentPostIndex].content,
extensions: [
markdownSetup({
onSearch: openSearchWithSelection,
}),
themeCompartment.of(theme(isDark.value)),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
const value = update.state.doc.toString()
clearTimeout(changeTimer.value)
changeTimer.value = setTimeout(() => {
editorRefresh()
const currentPost = store.posts[store.currentPostIndex]
if (value === currentPost.content) {
return
}
currentPost.updateDatetime = new Date()
currentPost.content = value
store.putPost(currentPost.id, currentPost)
}, 300)
}
}),
],
})
// 创建编辑器视图
const view = new EditorView({
state,
parent: dom,
})
codeMirrorView.value = view
// 添加粘贴事件监听
view.dom.addEventListener(`paste`, async (event: ClipboardEvent) => {
if (!(event.clipboardData?.items) || isImgLoading.value) {
return
}
const items = [...event.clipboardData.items].map(item => item.getAsFile()).filter(item => item != null && beforeUpload(item)) as File[]
// 即使return了,粘贴的文本内容也会被插入
if (items.length === 0) {
return
}
// start progress
const intervalId = setInterval(() => {
const newProgress = progressValue.value + 1
if (newProgress >= 100) {
return
}
progressValue.value = newProgress
}, 100)
for (const item of items) {
event.preventDefault()
await uploadImage(item)
}
const cleanup = () => {
clearInterval(intervalId)
progressValue.value = 100 // 设置完成状态
// 可选:延迟一段时间后重置进度
setTimeout(() => {
progressValue.value = 0
}, 1000)
}
cleanup()
})
// 返回编辑器 view
return view
}
// 初始化编辑器
onMounted(() => {
const editorDom = editorRef.value
if (editorDom == null) {
return
}
nextTick(() => {
const editorView = createFormTextArea(editorDom)
editor.value = editorView
// AI 工具箱已移到侧边栏,不再需要初始化编辑器事件
editorRefresh()
mdLocalToRemote()
})
})
// 监听暗色模式变化并更新编辑器主题
watch(isDark, () => {
if (codeMirrorView.value) {
codeMirrorView.value.dispatch({
effects: themeCompartment.reconfigure(theme(isDark.value)),
})
}
})
// 历史记录的定时器
const historyTimer = ref<NodeJS.Timeout>()
onMounted(() => {
// 定时,30 秒记录一次文章的历史记录
historyTimer.value = setInterval(() => {
const currentPost = store.posts[store.currentPostIndex]
// 与最后一篇记录对比
const pre = (currentPost.history || [])[0]?.content
if (pre === currentPost.content) {
return
}
currentPost.history ??= []
currentPost.history.unshift({
content: currentPost.content,
datetime: new Date().toLocaleString(`zh-CN`),
})
window.storex.post(`history`, {
postsId: currentPost.id,
content: currentPost.content,
})
currentPost.history.length = Math.min(currentPost.history.length, 10)
}, 30 * 1000)
})
// 销毁时清理定时器和全局事件监听器
onUnmounted(() => {
// 清理定时器 - 防止回调访问已销毁的DOM
clearTimeout(historyTimer.value)
clearTimeout(timeout.value)
clearTimeout(changeTimer.value)
// 清理全局事件监听器 - 防止全局事件触发已销毁的组件
document.removeEventListener(`keydown`, handleGlobalKeydown)
})
</script>
<template>
<div class="container flex flex-col">
<Progress v-model="progressValue" class="absolute left-0 right-0 rounded-none" style="height: 2px;" />
<EditorHeader
@start-copy="startCopy"
@end-copy="endCopy"
/>
<main class="container-main flex flex-1 flex-col">
<div
class="container-main-section border-radius-10 relative flex flex-1 overflow-hidden border"
>
<ResizablePanelGroup direction="horizontal">
<ResizablePanel
:default-size="15"
:max-size="store.isOpenPostSlider ? 30 : 0"
:min-size="store.isOpenPostSlider ? 10 : 0"
>
<PostSlider />
</ResizablePanel>
<ResizableHandle class="hidden md:block" />
<ResizablePanel class="flex">
<div
v-show="!store.isMobile || (store.isMobile && showEditor)"
ref="codeMirrorWrapper"
class="codeMirror-wrapper relative flex-1"
:class="{
'order-1 border-l': !store.isEditOnLeft,
'border-r': store.isEditOnLeft,
}"
>
<SearchTab v-if="codeMirrorView" ref="searchTabRef" :editor-view="codeMirrorView as any" />
<SidebarAIToolbar
:is-mobile="store.isMobile"
:show-editor="showEditor"
/>
<EditorContextMenu>
<div
id="editor"
ref="editorRef"
class="codemirror-container"
/>
</EditorContextMenu>
</div>
<div
v-show="!store.isMobile || (store.isMobile && !showEditor)"
class="relative flex-1 overflow-x-hidden transition-width"
:class="[store.isOpenRightSlider ? 'w-0' : 'w-100']"
>
<div
id="preview"
ref="previewRef"
class="preview-wrapper w-full p-5"
>
<div
id="output-wrapper"
class="w-full"
:class="{ output_night: !backLight }"
>
<div
class="preview border-x shadow-xl"
:class="[store.isMobile ? 'w-[100%]' : store.previewWidth]"
>
<section id="output" class="w-full" v-html="output" />
<div v-if="isCoping" class="loading-mask">
<div class="loading-mask-box">
<div class="loading__img" />
<span>正在生成</span>
</div>
</div>
</div>
</div>
<BackTop
target="preview"
:right="store.isMobile ? 24 : 20"
:bottom="store.isMobile ? 90 : 20"
/>
</div>
<FloatingToc />
</div>
<CssEditor />
<RightSlider />
</ResizablePanel>
</ResizablePanelGroup>
</div>
<!-- 移动端浮动按钮组 -->
<div v-if="store.isMobile" class="fixed bottom-16 right-6 z-50 flex flex-col gap-2">
<!-- 切换编辑/预览按钮 -->
<button
class="bg-primary flex items-center justify-center rounded-full p-3 text-white shadow-lg transition active:scale-95 hover:scale-105 dark:bg-gray-700 dark:text-white dark:ring-2 dark:ring-white/30"
aria-label="切换编辑/预览"
@click="toggleView"
>
<component :is="showEditor ? Eye : Pen" class="h-5 w-5" />
</button>
</div>
<!-- AI工具箱已移到侧边栏,这里不再显示 -->
<UploadImgDialog @upload-image="uploadImage" />
<InsertFormDialog />
<InsertMpCardDialog />
<AlertDialog v-model:open="store.isOpenConfirmDialog">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>提示</AlertDialogTitle>
<AlertDialogDescription>
此操作将丢失本地自定义样式,是否继续?
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
<AlertDialogAction @click="store.resetStyle()">
确认
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</main>
<Footer />
</div>
</template>
<style lang="less" scoped>
@import url('../assets/less/app.less');
</style>
<style lang="less" scoped>
.container {
height: 100vh;
min-width: 100%;
padding: 0;
}
.container-main {
overflow: hidden;
}
#output-wrapper {
position: relative;
user-select: text;
height: 100%;
}
.loading-mask {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
text-align: center;
color: hsl(var(--foreground));
background-color: hsl(var(--background));
.loading-mask-box {
position: sticky;
top: 50%;
transform: translateY(-50%);
.loading__img {
width: 75px;
height: 75px;
background: url('../assets/images/favicon.png') no-repeat;
margin: 1em auto;
background-size: cover;
}
}
}
:deep(.preview-table) {
border-spacing: 0;
}
.codeMirror-wrapper,
.preview-wrapper {
height: 100%;
}
.codeMirror-wrapper {
overflow-x: hidden;
height: 100%;
position: relative;
}
</style>