forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.vue
More file actions
352 lines (303 loc) · 8.49 KB
/
Copy pathApp.vue
File metadata and controls
352 lines (303 loc) · 8.49 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
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import ConfirmDialog from '@/components/confirm-dialog/ConfirmDialog.vue'
import DesktopSidebar from '@/components/desktop/DesktopSidebar.vue'
import CodemirrorEditor from '@/components/editor/CodemirrorEditor.vue'
import { Toaster } from '@/components/ui/sonner'
import WelcomeGuideDialog from '@/components/WelcomeGuideDialog.vue'
import { isAccountUiEnabled } from '@/services/account/config'
import { isSyncUiEnabled } from '@/services/sync/client'
import { useAccountSettingsStore } from '@/stores/accountSettings'
import { useAuthStore } from '@/stores/auth'
import { useDesktopStore } from '@/stores/desktop'
import { useDocumentWorkspaceStore } from '@/stores/documentWorkspace'
import { useEditorStore } from '@/stores/editor'
import { useExportStore } from '@/stores/export'
import { useSyncStore } from '@/stores/sync'
import { useUIStore } from '@/stores/ui'
const uiStore = useUIStore()
const { isDark } = storeToRefs(uiStore)
const desktopStore = useDesktopStore()
const editorStore = useEditorStore()
const exportStore = useExportStore()
const authStore = useAuthStore()
const accountSettingsStore = useAccountSettingsStore()
const documentWorkspaceStore = useDocumentWorkspaceStore()
const syncStore = useSyncStore()
const isUtools = ref(false)
let unlistenDesktopMenu: (() => void) | null = null
/** 初始化账户与云同步 */
async function bootstrapApp() {
if (isAccountUiEnabled()) {
await authStore.bootstrap()
}
// guest 与每个账号都先打开各自的本机草稿库;远端拉取由 Store 在后台继续。
await documentWorkspaceStore.bootstrap()
if (isAccountUiEnabled() && authStore.isLoggedIn)
await accountSettingsStore.bootstrap()
if (!isSyncUiEnabled() || !authStore.isLoggedIn)
return
syncStore.startAutoSyncWatcher()
await syncStore.sync()
}
function isPrimaryShortcut(event: KeyboardEvent) {
return event.metaKey || event.ctrlKey
}
function cycleViewMode() {
const modes: Array<'edit' | 'split' | 'preview'> = uiStore.isMobile ? [`edit`, `preview`] : [`edit`, `split`, `preview`]
const index = modes.indexOf(uiStore.viewMode)
uiStore.setViewMode(modes[(index + 1) % modes.length])
}
function runDesktopCommand(command: string) {
if (!desktopStore.isTauri)
return
switch (command) {
case `new-draft`:
void desktopStore.newDraft()
break
case `open-file`:
void desktopStore.openFileDialog()
break
case `open-folder`:
void desktopStore.openFolderDialog()
break
case `save`:
void desktopStore.saveBack()
break
case `save-as`:
void desktopStore.saveAs()
break
case `copy-to-wechat`:
window.dispatchEvent(new CustomEvent(`mdlook:copy-to-wechat`))
break
case `cycle-view`:
cycleViewMode()
break
case `toggle-style-panel`:
uiStore.isOpenRightSlider = !uiStore.isOpenRightSlider
break
case `open-history`:
uiStore.isOpenPostSlider = true
window.dispatchEvent(new CustomEvent(`mdlook:open-current-history`))
break
case `export-md`:
exportStore.exportEditorContent2MD(editorStore.getContent())
break
case `export-html`:
void exportStore.exportEditorContent2HTML()
break
case `export-pure-html`:
exportStore.exportEditorContent2PureHTML(editorStore.getContent())
break
case `export-pdf`:
void exportStore.exportEditorContent2PDF()
break
case `export-png`:
void exportStore.downloadAsCardImage()
break
}
}
function handleDesktopShortcut(event: KeyboardEvent) {
if (!desktopStore.isTauri || event.defaultPrevented || !isPrimaryShortcut(event) || event.altKey)
return
const key = event.key.toLowerCase()
if (key === `n` && !event.shiftKey) {
event.preventDefault()
runDesktopCommand(`new-draft`)
return
}
if (key === `o` && event.shiftKey) {
event.preventDefault()
runDesktopCommand(`open-folder`)
return
}
if (key === `o`) {
event.preventDefault()
runDesktopCommand(`open-file`)
return
}
if (key === `s` && event.shiftKey) {
event.preventDefault()
runDesktopCommand(`save-as`)
return
}
if (key === `s`) {
event.preventDefault()
runDesktopCommand(`save`)
return
}
if (key === `c` && event.shiftKey) {
event.preventDefault()
runDesktopCommand(`copy-to-wechat`)
return
}
if (key === `p` && event.shiftKey) {
event.preventDefault()
runDesktopCommand(`cycle-view`)
}
}
async function initDesktopMenuEvents() {
try {
const { listen } = await import(`@tauri-apps/api/event`)
unlistenDesktopMenu = await listen<string>(`mdlook://menu-command`, (event) => {
if (event.payload)
runDesktopCommand(event.payload)
})
}
catch (error) {
console.error(`desktop menu init failed`, error)
}
}
onMounted(() => {
// 检测是否为 Utools 环境
isUtools.value = !!(window as any).__MD_UTOOLS__
if (isUtools.value) {
document.documentElement.classList.add(`is-utools`)
}
// 桌面端(Tauri):处理双击 .md 打开、监听运行时打开事件
if (desktopStore.isTauri) {
document.documentElement.classList.add(`is-tauri`)
void initDesktopMenuEvents()
window.addEventListener(`keydown`, handleDesktopShortcut, { capture: true })
}
// 账号 Scope 必须先落定,再恢复本地文件;否则异步切换会把刚打开的文件覆盖。
void bootstrapApp().finally(() => {
if (desktopStore.isTauri)
void desktopStore.init()
})
// 若 URL 带有 open 参数(Markdown 链接),打开导入对话框并自动导入
const params = new URLSearchParams(window.location.search)
const openUrl = params.get(`open`)
if (openUrl && URL.canParse(openUrl) && /^https?:\/\//i.test(openUrl)) {
uiStore.importMdOpenUrl = openUrl
uiStore.isShowImportMdDialog = true
params.delete(`open`)
const newSearch = params.toString()
const newUrl = window.location.pathname + (newSearch ? `?${newSearch}` : ``) + window.location.hash
window.history.replaceState({}, ``, newUrl)
}
window.addEventListener(`mdlook:open-style-panel`, () => {
uiStore.isOpenRightSlider = true
})
})
onUnmounted(() => {
window.removeEventListener(`keydown`, handleDesktopShortcut, true)
unlistenDesktopMenu?.()
})
</script>
<template>
<AppSplash />
<div class="app-shell relative flex w-full">
<DesktopSidebar v-if="desktopStore.isTauri" />
<div class="relative min-w-0 flex-1">
<CodemirrorEditor />
</div>
</div>
<ConfirmDialog />
<WelcomeGuideDialog />
<Toaster
rich-colors
position="top-center"
:duration="1200"
:visible-toasts="1"
:theme="isDark ? 'dark' : 'light'"
/>
</template>
<style lang="less">
html,
body,
#app {
width: 100%;
height: 100vh;
height: 100dvh;
margin: 0;
padding: 0;
overflow: hidden;
}
.app-shell {
box-sizing: border-box;
height: 100vh;
height: 100dvh;
}
// 抵消下拉菜单开启时带来的样式
body {
pointer-events: initial !important;
}
::-webkit-scrollbar {
width: 6px;
height: 6px;
background-color: transparent;
}
::-webkit-scrollbar-track {
border-radius: 6px;
background-color: transparent;
}
::-webkit-scrollbar-thumb {
border-radius: 6px;
background-color: #dadada;
}
.dark ::-webkit-scrollbar-thumb {
background-color: #424242;
}
// Utools 模式下隐藏所有滚动条
.is-utools {
::-webkit-scrollbar {
display: none;
}
// Firefox
* {
scrollbar-width: none;
}
// IE and Edge
* {
-ms-overflow-style: none;
}
}
/* CSS-hints */
.CodeMirror-hints {
position: absolute;
z-index: 10;
overflow-y: auto;
margin: 0;
padding: 2px;
border-radius: 4px;
max-height: 20em;
min-width: 200px;
font-size: 12px;
font-family: monospace;
color: #333333;
background-color: #ffffff;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.08);
}
.CodeMirror-hint {
margin-top: 10px;
padding: 4px 6px;
border-radius: 2px;
white-space: pre;
color: #000000;
cursor: pointer;
&:first-of-type {
margin-top: 0;
}
&:hover {
background: #f0f0f0;
}
}
.search-match {
background-color: #ffeb3b; /* 所有匹配项颜色 */
}
.current-match {
background-color: #ff5722; /* 当前匹配项更鲜艳的颜色 */
}
@media (max-width: 768px) {
.app-shell {
padding-top: env(safe-area-inset-top);
padding-bottom: env(safe-area-inset-bottom);
}
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio'] {
min-height: 44px;
}
}
</style>