forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodemirrorEditor.vue
More file actions
345 lines (298 loc) · 10.7 KB
/
Copy pathCodemirrorEditor.vue
File metadata and controls
345 lines (298 loc) · 10.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
<script setup lang="ts">
import FormulaEditorDialog from '@/components/editor/dialogs/FormulaEditorDialog.vue'
import LocalImageUploadDialog from '@/components/editor/dialogs/LocalImageUploadDialog.vue'
import EditorPanel from '@/components/editor/EditorPanel.vue'
import FolderSourcePanel from '@/components/editor/folder-source-panel/index.vue'
import PreviewPanel from '@/components/editor/PreviewPanel.vue'
import WysiwygEditor from '@/components/editor/WysiwygEditor.vue'
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from '@/components/ui/resizable'
import { useCursorSync } from '@/composables/useCursorSync'
import { useScrollSync } from '@/composables/useScrollSync'
import { useUIStore } from '@/stores/ui'
const uiStore = useUIStore()
const {
isMobile,
isOpenPostSlider,
isOpenFolderPanel,
isOpenRightSlider,
viewMode,
editorType,
enableScrollSync,
} = storeToRefs(uiStore)
// --- 子组件引用 ---
const editorPanelCompRef = ref<InstanceType<typeof EditorPanel> | null>(null)
const previewPanelCompRef = ref<InstanceType<typeof PreviewPanel> | null>(null)
// 从子组件获取 codeMirrorView 和 previewRef (使用 getter 避免 DeepReadonly 类型问题)
const getEditorView = () => editorPanelCompRef.value?.codeMirrorView ?? null
const getPreviewContainer = () => previewPanelCompRef.value?.previewRef ?? null
// --- 点击预览内容跳转到编辑器对应位置 ---
const { handlePreviewContentClick } = useCursorSync(getEditorView)
// --- 滚动同步 ---
useScrollSync(getEditorView, getPreviewContainer, enableScrollSync)
// --- 复制状态 ---
const backLight = ref(false)
const isCoping = ref(false)
let copyEndTimer: ReturnType<typeof setTimeout> | null = null
function startCopy() {
backLight.value = true
isCoping.value = true
}
function endCopy() {
backLight.value = false
if (copyEndTimer) {
clearTimeout(copyEndTimer)
}
copyEndTimer = setTimeout(() => {
isCoping.value = false
copyEndTimer = null
}, 800)
}
onUnmounted(() => {
if (copyEndTimer) {
clearTimeout(copyEndTimer)
copyEndTimer = null
}
})
// --- 上传图片透传 ---
function handleUploadImage(file: File, cb?: any, applyUrl?: boolean) {
editorPanelCompRef.value?.uploadImage(file, cb, applyUrl)
}
// --- 面板尺寸配置 ---
const hasSidePanel = computed(() => !isMobile.value && (isOpenRightSlider.value || uiStore.isShowCssEditor))
const editorPanelConfig = computed(() => {
const mode = viewMode.value
if (mode === `preview`) {
return { min: 0, max: 0 }
}
if (mode === `edit`) {
return hasSidePanel.value ? { min: 30, max: 85 } : { min: 100, max: 100 }
}
if (isMobile.value)
return { min: 30, max: 70 }
return { min: 15, max: 85 }
})
const previewPanelConfig = computed(() => {
const mode = viewMode.value
if (mode === `edit`) {
return { min: 0, max: 0 }
}
if (mode === `preview`) {
return hasSidePanel.value ? { min: 20, max: 75 } : { min: 100, max: 100 }
}
if (isMobile.value)
return { min: 30, max: 70 }
return { min: 15, max: 85 }
})
const editorResizablePanelRef = ref<InstanceType<typeof ResizablePanel> | null>(null)
const previewResizablePanelRef = ref<InstanceType<typeof ResizablePanel> | null>(null)
const cssEditorPanelRef = ref<InstanceType<typeof ResizablePanel> | null>(null)
const rightSliderPanelRef = ref<InstanceType<typeof ResizablePanel> | null>(null)
const PANEL_RESIZE_MAX_ATTEMPTS = 4
let panelResizeFrame: number | null = null
let isPanelLayoutUnmounted = false
function redistributePanelSizes(): boolean {
const editorPanel = editorResizablePanelRef.value
const previewPanel = previewResizablePanelRef.value
const cssPanel = cssEditorPanelRef.value
const rightPanel = rightSliderPanelRef.value
if (!editorPanel || !previewPanel || !cssPanel || !rightPanel)
return false
const cssTarget = !isMobile.value && uiStore.isShowCssEditor ? 25 : 0
const rightTarget = !isMobile.value && isOpenRightSlider.value ? 30 : 0
const contentSpace = 100 - cssTarget - rightTarget
try {
const mode = viewMode.value
if (mode === `edit`) {
editorPanel.resize(contentSpace)
previewPanel.resize(0)
}
else if (mode === `preview`) {
editorPanel.resize(0)
previewPanel.resize(contentSpace)
}
else {
const half = contentSpace / 2
editorPanel.resize(half)
previewPanel.resize(half)
}
cssPanel.resize(cssTarget)
rightPanel.resize(rightTarget)
return true
}
catch (error) {
// Reka 在 HMR/快速重挂载期间会短暂出现“组件 ref 已存在、面板尚未
// 注册进新 group”的窗口。此时 imperative resize 会断言失败;下一帧
// 注册完成后重试即可,不能把该瞬态错误泄漏成未处理异常。
if (error instanceof Error && /Assertion failed|Panel size not found/.test(error.message))
return false
throw error
}
}
function schedulePanelRedistribution(attempt = 0) {
if (isPanelLayoutUnmounted)
return
if (panelResizeFrame !== null)
cancelAnimationFrame(panelResizeFrame)
void nextTick(() => {
panelResizeFrame = requestAnimationFrame(() => {
panelResizeFrame = null
if (!redistributePanelSizes() && attempt < PANEL_RESIZE_MAX_ATTEMPTS)
schedulePanelRedistribution(attempt + 1)
})
})
}
watch([viewMode, () => uiStore.isShowCssEditor, isOpenRightSlider, isMobile], () => {
schedulePanelRedistribution()
})
onMounted(schedulePanelRedistribution)
onUnmounted(() => {
isPanelLayoutUnmounted = true
if (panelResizeFrame !== null) {
cancelAnimationFrame(panelResizeFrame)
panelResizeFrame = null
}
})
// --- 进度条 ---
const progressValue = computed(() => editorPanelCompRef.value?.progressValue ?? 0)
</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-x border-b"
>
<ResizablePanelGroup direction="horizontal">
<ResizablePanel
class="post-slider-panel"
:default-size="isMobile ? 0 : 15"
:max-size="!isMobile && isOpenPostSlider ? 20 : 0"
:min-size="!isMobile && isOpenPostSlider ? 10 : 0"
>
<PostSlider />
</ResizablePanel>
<ResizableHandle class="hidden md:block" />
<ResizablePanel
class="folder-panel"
:default-size="!isMobile && isOpenFolderPanel ? 15 : 0"
:max-size="!isMobile && isOpenFolderPanel ? 25 : 0"
:min-size="!isMobile && isOpenFolderPanel ? 10 : 0"
>
<FolderSourcePanel />
</ResizablePanel>
<ResizableHandle v-if="!isMobile && isOpenFolderPanel" class="hidden md:block" />
<!-- 主内容区域 (嵌套灵动布局) -->
<ResizablePanel :min-size="30">
<ResizablePanelGroup direction="horizontal">
<!-- Markdown 编辑器 -->
<ResizablePanel
ref="editorResizablePanelRef"
:order="1"
:default-size="viewMode === 'preview' ? 0 : viewMode === 'edit' ? 100 : 50"
:min-size="editorPanelConfig.min"
:max-size="editorPanelConfig.max"
collapsible
:collapsed-size="0"
>
<EditorPanel v-if="editorType === 'source'" ref="editorPanelCompRef" />
<WysiwygEditor v-else />
</ResizablePanel>
<ResizableHandle v-show="viewMode === 'split'" />
<!-- 预览区 -->
<ResizablePanel
ref="previewResizablePanelRef"
:order="2"
:default-size="viewMode === 'edit' ? 0 : viewMode === 'preview' ? 100 : 50"
:min-size="previewPanelConfig.min"
:max-size="previewPanelConfig.max"
collapsible
:collapsed-size="0"
>
<PreviewPanel
ref="previewPanelCompRef"
:back-light="backLight"
:is-coping="isCoping"
:on-content-click="handlePreviewContentClick"
/>
</ResizablePanel>
<!-- CSS 编辑器面板 -->
<ResizableHandle v-show="!isMobile && uiStore.isShowCssEditor" class="hidden md:block" />
<ResizablePanel
ref="cssEditorPanelRef"
:order="3"
:default-size="0"
:min-size="!isMobile && uiStore.isShowCssEditor ? 10 : 0"
:max-size="!isMobile && uiStore.isShowCssEditor ? 60 : 0"
collapsible
:collapsed-size="0"
>
<CssEditor v-if="!isMobile" />
</ResizablePanel>
<!-- 样式面板 -->
<ResizableHandle v-show="!isMobile && isOpenRightSlider" class="hidden md:block right-slider-handle" />
<ResizablePanel
ref="rightSliderPanelRef"
class="right-slider-panel"
:order="4"
:default-size="0"
:min-size="!isMobile && isOpenRightSlider ? 25 : 0"
:max-size="!isMobile && isOpenRightSlider ? 60 : 0"
collapsible
:collapsed-size="0"
>
<RightSlider v-if="!isMobile" />
</ResizablePanel>
</ResizablePanelGroup>
<!-- 移动端:CssEditor 和 RightSlider 作为浮层 -->
<template v-if="isMobile">
<CssEditor />
<RightSlider />
</template>
</ResizablePanel>
</ResizablePanelGroup>
</div>
<UploadImgDialog @upload-image="handleUploadImage" />
<InsertFormDialog />
<ImportMarkdownDialog />
<LocalImageUploadDialog />
<FormulaEditorDialog />
<TemplateDialog />
<CustomComponentDialog />
</main>
<Footer />
</div>
</template>
<style lang="less" scoped>
@import url('../../assets/less/app.less');
</style>
<style lang="less" scoped>
.container {
height: 100%;
min-height: 0;
min-width: 100%;
padding: 0;
}
.container-main {
overflow: hidden;
}
.right-slider-panel {
transition: flex-grow 300ms cubic-bezier(0.16, 1, 0.3, 1);
}
.post-slider-panel {
transition: flex-grow 300ms cubic-bezier(0.16, 1, 0.3, 1);
}
.folder-panel {
transition: flex-grow 300ms cubic-bezier(0.16, 1, 0.3, 1);
}
.right-slider-handle {
transition: opacity 300ms cubic-bezier(0.16, 1, 0.3, 1);
}
</style>