-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathindex.ts
More file actions
900 lines (779 loc) · 24.4 KB
/
index.ts
File metadata and controls
900 lines (779 loc) · 24.4 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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
import type { EditorView } from '@codemirror/view'
import { Compartment, EditorState } from '@codemirror/state'
import { EditorView as CMEditorView } from '@codemirror/view'
import { initRenderer } from '@md/core'
import {
defaultStyleConfig,
themeMap,
widthOptions,
} from '@md/shared/configs'
import { cssSetup, theme as editorTheme } from '@md/shared/editor'
import { toPng } from 'html-to-image'
import { v4 as uuid } from 'uuid'
import DEFAULT_CONTENT from '@/assets/example/markdown.md?raw'
import DEFAULT_CSS_CONTENT from '@/assets/example/theme-css.txt?raw'
import {
addPrefix,
css2json,
customCssWithTemplate,
customizeTheme,
downloadFile,
downloadMD,
exportHTML,
exportPDF,
exportPureHTML,
formatDoc,
postProcessHtml,
processHtmlContent,
renderMarkdown,
sanitizeTitle,
} from '@/utils'
import { copyPlain } from '@/utils/clipboard'
import { data } from './defaultData'
/**********************************
* Post 结构接口
*********************************/
interface Post {
id: string
title: string
content: string
history: {
datetime: string
content: string
}[]
createDatetime: Date
updateDatetime: Date
// 父标签
parentId?: string | null
// 展开状态
collapsed?: boolean
}
export const useStore = defineStore(`store`, () => {
// 是否开启深色模式
const isDark = useDark()
const toggleDark = useToggle(isDark)
// 是否开启 Mac 代码块
const isMacCodeBlock = useStorage(`isMacCodeBlock`, defaultStyleConfig.isMacCodeBlock)
const toggleMacCodeBlock = useToggle(isMacCodeBlock)
// 是否开启代码块行号显示
const isShowLineNumber = useStorage(`isShowLineNumber`, defaultStyleConfig.isShowLineNumber)
const toggleShowLineNumber = useToggle(isShowLineNumber)
// 是否在左侧编辑
const isEditOnLeft = useStorage(`isEditOnLeft`, true)
const toggleEditOnLeft = useToggle(isEditOnLeft)
// 是否开启微信外链接底部引用
const isCiteStatus = useStorage(`isCiteStatus`, defaultStyleConfig.isCiteStatus)
const toggleCiteStatus = useToggle(isCiteStatus)
// 是否开启 AI 工具箱
const showAIToolbox = useStorage(`showAIToolbox`, true)
const toggleAIToolbox = useToggle(showAIToolbox)
// 是否统计字数和阅读时间
const isCountStatus = useStorage(`isCountStatus`, defaultStyleConfig.isCountStatus)
const toggleCountStatus = useToggle(isCountStatus)
// 是否开启段落首行缩进
const isUseIndent = useStorage(addPrefix(`use_indent`), false)
const toggleUseIndent = useToggle(isUseIndent)
const isUseJustify = useStorage(addPrefix(`use_justify`), false)
const toggleUseJustify = useToggle(isUseJustify)
const output = ref(``)
// 文本字体
const theme = useStorage<keyof typeof themeMap>(addPrefix(`theme`), defaultStyleConfig.theme)
// 文本字体
const fontFamily = useStorage(`fonts`, defaultStyleConfig.fontFamily)
// 文本大小
const fontSize = useStorage(`size`, defaultStyleConfig.fontSize)
// 主色
const primaryColor = useStorage(`color`, defaultStyleConfig.primaryColor)
// 代码块主题
const codeBlockTheme = useStorage(`codeBlockTheme`, defaultStyleConfig.codeBlockTheme)
// 图注格式
const legend = useStorage(`legend`, defaultStyleConfig.legend)
// 预览宽度
const previewWidth = useStorage(`previewWidth`, widthOptions[0].value)
const fontSizeNumber = computed(() => Number(fontSize.value.replace(`px`, ``)))
// 内容编辑器
const editor = ref<EditorView | null>(null)
// 预备弃用的旧字段
const editorContent = useStorage(`__editor_content`, DEFAULT_CONTENT)
const isOpenRightSlider = useStorage(addPrefix(`is_open_right_slider`), false)
const isOpenPostSlider = useStorage(addPrefix(`is_open_post_slider`), false)
/*******************************
* 内容列表 posts:默认就带 id
******************************/
const posts = useStorage<Post[]>(addPrefix(`posts`), data.posts)
// currentPostId 先存空串
const currentPostId = useStorage(addPrefix(`current_post_id`), ``)
// 是否为移动端
const isMobile = useStorage(`isMobile`, false)
function handleResize() {
isMobile.value = window.innerWidth <= 768
}
onMounted(() => {
handleResize()
window.addEventListener(`resize`, handleResize)
})
onBeforeUnmount(() => {
window.removeEventListener(`resize`, handleResize)
})
// 在补齐 id 后,若 currentPostId 无效 ➜ 自动指向第一篇
onBeforeMount(() => {
posts.value = posts.value.map((post, index) => {
const now = Date.now()
return {
...post,
id: post.id ?? uuid(),
createDatetime: post.createDatetime ?? new Date(now + index),
updateDatetime: post.updateDatetime ?? new Date(now + index),
}
})
// 兼容:如果本地没有 currentPostId,或指向的文章已不存在
if (!currentPostId.value || !posts.value.some(p => p.id === currentPostId.value)) {
currentPostId.value = posts.value[0]?.id ?? ``
}
})
/** 根据 id 找索引 */
const findIndexById = (id: string) => posts.value.findIndex(p => p.id === id)
/** computed: 让旧代码还能用 index,但底层映射 id */
const currentPostIndex = computed<number>({
get: () => findIndexById(currentPostId.value),
set: (idx) => {
if (idx >= 0 && idx < posts.value.length)
currentPostId.value = posts.value[idx].id
},
})
/** 获取 Post */
const getPostById = (id: string) => posts.value.find(p => p.id === id)
/********************************
* CRUD
********************************/
const addPost = async (title: string, parentId: string | null = null): Promise<void> => {
const newPost: Post = {
id: uuid(),
title,
content: `# ${title}`,
history: [
{ datetime: new Date().toLocaleString(`zh-cn`), content: `# ${title}` },
],
createDatetime: new Date(),
updateDatetime: new Date(),
parentId,
}
const res = await window.storex.post(`posts`, newPost)
posts.value.push(res)
currentPostId.value = res.id
}
const putPost = async (id: string, data: any): Promise<void> => {
window.storex.put(`posts/${id}`, { ...data, history: undefined }).catch(() => {})
}
const renamePost = (id: string, title: string) => {
const post = getPostById(id)
if (post)
post.title = title
}
const delPost = async (id: string): Promise<void> => {
const idx = findIndexById(id)
if (idx === -1)
return
posts.value.splice(idx, 1)
window.storex.delete(`posts/${id}`).catch(() => {})
currentPostId.value = posts.value[Math.min(idx, posts.value.length - 1)]?.id ?? ``
}
const updatePostParentId = (postId: string, parentId: string | null) => {
const post = getPostById(postId)
if (post) {
post.parentId = parentId
post.updateDatetime = new Date()
}
}
// 收起所有文章
const collapseAllPosts = () => {
posts.value.forEach((post) => {
post.collapsed = true
})
}
// 展开所有文章
const expandAllPosts = () => {
posts.value.forEach((post) => {
post.collapsed = false
})
}
/********************************
* 同步编辑器内容
********************************/
watch(currentPostId, () => {
const post = getPostById(currentPostId.value)
if (post && editor.value) {
editor.value.dispatch({
changes: { from: 0, to: editor.value.state.doc.length, insert: post.content },
})
}
})
onMounted(() => {
// 迁移阶段,兼容之前的方案
if (editorContent.value !== DEFAULT_CONTENT) {
const post = getPostById(currentPostId.value)
if (post)
post.content = editorContent.value
editorContent.value = DEFAULT_CONTENT
}
})
// 格式化文档
const formatContent = () => {
if (editor.value) {
formatDoc(editor.value.state.doc.toString()).then((doc) => {
posts.value[currentPostIndex.value].content = doc
editor.value!.dispatch({
changes: { from: 0, to: editor.value!.state.doc.length, insert: doc },
})
})
}
}
// 切换 highlight.js 代码主题
const codeThemeChange = () => {
const cssUrl = codeBlockTheme.value
const el = document.querySelector(`#hljs`)
if (el) {
el.setAttribute(`href`, cssUrl)
}
else {
const link = document.createElement(`link`)
link.setAttribute(`type`, `text/css`)
link.setAttribute(`rel`, `stylesheet`)
link.setAttribute(`href`, cssUrl)
link.setAttribute(`id`, `hljs`)
document.head.appendChild(link)
}
}
// 自义定 CSS 编辑器
const cssEditor = ref<EditorView | null>(null)
const cssEditorThemeCompartment = ref<Compartment | null>(null)
const setCssEditorValue = (content: string) => {
if (cssEditor.value) {
cssEditor.value.dispatch({
changes: { from: 0, to: cssEditor.value.state.doc.length, insert: content },
})
}
}
/**
* 自定义 CSS 内容
* @deprecated 在后续版本中将会移除
*/
const cssContent = useStorage(`__css_content`, DEFAULT_CSS_CONTENT)
const cssContentConfig = useStorage(addPrefix(`css_content_config`), {
active: `方案1`,
tabs: [
{
title: `方案1`,
name: `方案1`,
// 兼容之前的方案
content: cssContent.value || DEFAULT_CSS_CONTENT,
},
],
})
onMounted(() => {
// 清空过往历史记录
cssContent.value = ``
})
const getCurrentTab = () =>
cssContentConfig.value.tabs.find((tab) => {
return tab.name === cssContentConfig.value.active
})!
const tabChanged = (name: string) => {
cssContentConfig.value.active = name
const content = cssContentConfig.value.tabs.find((tab) => {
return tab.name === name
})!.content
setCssEditorValue(content)
}
// 重命名 css 方案
const renameTab = (name: string) => {
const tab = getCurrentTab()!
tab.title = name
tab.name = name
cssContentConfig.value.active = name
}
const addCssContentTab = (name: string) => {
cssContentConfig.value.tabs.push({
name,
title: name,
content: DEFAULT_CSS_CONTENT,
})
cssContentConfig.value.active = name
setCssEditorValue(DEFAULT_CSS_CONTENT)
}
const validatorTabName = (val: string) => {
return cssContentConfig.value.tabs.every(({ name }) => name !== val)
}
const renderer = initRenderer({
theme: customCssWithTemplate(
css2json(getCurrentTab().content),
primaryColor.value,
customizeTheme(themeMap[theme.value], {
fontSize: fontSizeNumber.value,
color: primaryColor.value,
}),
),
fonts: fontFamily.value,
size: fontSize.value,
isUseIndent: isUseIndent.value,
isUseJustify: isUseJustify.value,
isMacCodeBlock: isMacCodeBlock.value,
isShowLineNumber: isShowLineNumber.value,
})
const readingTime = reactive({
chars: 0,
words: 0,
minutes: 0,
})
// 文章标题,用于生成目录
const titleList = ref<{
url: string
title: string
level: number
}[]>([])
// 更新编辑器
const editorRefresh = () => {
codeThemeChange()
renderer.reset({
citeStatus: isCiteStatus.value,
legend: legend.value,
isUseIndent: isUseIndent.value,
isUseJustify: isUseJustify.value,
countStatus: isCountStatus.value,
isMacCodeBlock: isMacCodeBlock.value,
isShowLineNumber: isShowLineNumber.value,
})
const raw = editor.value ? editor.value.state.doc.toString() : ``
const { html: baseHtml, readingTime: readingTimeResult } = renderMarkdown(raw, renderer)
readingTime.chars = raw.length
readingTime.words = readingTimeResult.words
readingTime.minutes = Math.ceil(readingTimeResult.minutes)
output.value = postProcessHtml(baseHtml, readingTimeResult, renderer)
// 提取标题
const div = document.createElement(`div`)
div.innerHTML = output.value
const list = div.querySelectorAll<HTMLElement>(`[data-heading]`)
titleList.value = []
let i = 0
for (const item of list) {
item.setAttribute(`id`, `${i}`)
titleList.value.push({
url: `#${i}`,
title: `${item.textContent}`,
level: Number(item.tagName.slice(1)),
})
i++
}
output.value = div.innerHTML
}
// 更新 CSS
const updateCss = () => {
if (cssEditor.value) {
const json = css2json(cssEditor.value.state.doc.toString())
const newTheme = customCssWithTemplate(
json,
primaryColor.value,
customizeTheme(themeMap[theme.value], {
fontSize: fontSizeNumber.value,
color: primaryColor.value,
}),
)
renderer.setOptions({
theme: newTheme,
})
editorRefresh()
}
}
// 初始化 CSS 编辑器
onMounted(() => {
const cssEditorDom = document.querySelector<HTMLTextAreaElement>(
`#cssEditor`,
)!
cssEditorDom.value = getCurrentTab().content
// 创建 CSS 编辑器的容器
const cssContainer = document.createElement(`div`)
cssEditorDom.parentNode?.replaceChild(cssContainer, cssEditorDom)
// 创建主题 Compartment 用于动态切换
cssEditorThemeCompartment.value = new Compartment()
// 创建 CSS 编辑器
const state = EditorState.create({
doc: getCurrentTab().content,
extensions: [
cssSetup(),
cssEditorThemeCompartment.value.of(editorTheme(isDark.value)),
CMEditorView.updateListener.of((update) => {
if (update.docChanged) {
updateCss()
getCurrentTab().content = update.state.doc.toString()
}
}),
],
})
cssEditor.value = markRaw(new CMEditorView({
state,
parent: cssContainer,
}))
})
watch(isDark, () => {
if (cssEditor.value && cssEditorThemeCompartment.value) {
cssEditor.value.dispatch({
effects: cssEditorThemeCompartment.value.reconfigure(editorTheme(isDark.value)),
})
}
})
// 重置样式
const resetStyle = () => {
isCiteStatus.value = defaultStyleConfig.isCiteStatus
isMacCodeBlock.value = defaultStyleConfig.isMacCodeBlock
isShowLineNumber.value = defaultStyleConfig.isShowLineNumber
isCountStatus.value = defaultStyleConfig.isCountStatus
theme.value = defaultStyleConfig.theme
fontFamily.value = defaultStyleConfig.fontFamily
fontSize.value = defaultStyleConfig.fontSize
primaryColor.value = defaultStyleConfig.primaryColor
codeBlockTheme.value = defaultStyleConfig.codeBlockTheme
legend.value = defaultStyleConfig.legend
cssContentConfig.value = {
active: `方案 1`,
tabs: [
{
title: `方案 1`,
name: `方案 1`,
// 兼容之前的方案
content: cssContent.value || DEFAULT_CSS_CONTENT,
},
],
}
if (cssEditor.value) {
cssEditor.value.dispatch({
changes: { from: 0, to: cssEditor.value.state.doc.length, insert: DEFAULT_CSS_CONTENT },
})
}
updateCss()
editorRefresh()
toast.success(`样式已重置`)
}
// 为函数添加刷新编辑器的功能
const withAfterRefresh = (fn: (...rest: any[]) => void) => (
...rest: any[]
) => {
fn(...rest)
editorRefresh()
}
const getTheme = (size: string, color: string) => {
const newTheme = themeMap[theme.value]
const fontSize = Number(size.replace(`px`, ``))
return customCssWithTemplate(
css2json(getCurrentTab().content),
color,
customizeTheme(newTheme, { fontSize, color }),
)
}
const themeChanged = withAfterRefresh((newTheme: keyof typeof themeMap) => {
renderer.setOptions({
theme: customCssWithTemplate(
css2json(getCurrentTab().content),
primaryColor.value,
customizeTheme(themeMap[newTheme], { fontSize: fontSizeNumber.value }),
),
})
theme.value = newTheme
})
const fontChanged = withAfterRefresh((fonts) => {
renderer.setOptions({
fonts,
})
fontFamily.value = fonts
})
const sizeChanged = withAfterRefresh((size) => {
const theme = getTheme(size, primaryColor.value)
renderer.setOptions({
size,
theme,
})
fontSize.value = size
})
const colorChanged = withAfterRefresh((newColor) => {
const theme = getTheme(fontSize.value, newColor)
renderer.setOptions({
theme,
})
primaryColor.value = newColor
})
const codeBlockThemeChanged = withAfterRefresh((newTheme) => {
codeBlockTheme.value = newTheme
})
const previewWidthChanged = withAfterRefresh((newWidth: string) => {
previewWidth.value = newWidth
})
const legendChanged = withAfterRefresh((newVal) => {
legend.value = newVal
})
const macCodeBlockChanged = withAfterRefresh(() => {
toggleMacCodeBlock()
})
const showLineNumberChanged = withAfterRefresh(() => {
toggleShowLineNumber()
})
const citeStatusChanged = withAfterRefresh(() => {
toggleCiteStatus()
})
const countStatusChanged = withAfterRefresh(() => {
toggleCountStatus()
})
const useIndentChanged = withAfterRefresh(() => {
toggleUseIndent()
})
const useJustifyChanged = withAfterRefresh(() => {
toggleUseJustify()
})
const aiToolboxChanged = withAfterRefresh(() => {
toggleAIToolbox()
})
const editorContent2HTML = () => {
const temp = processHtmlContent(primaryColor.value)
document.querySelector(`#output`)!.innerHTML = output.value
return temp
}
// 导出编辑器内容为 HTML,并且下载到本地
const exportEditorContent2HTML = () => {
exportHTML(primaryColor.value, posts.value[currentPostIndex.value].title)
document.querySelector(`#output`)!.innerHTML = output.value
}
// 导出编辑器内容为无样式 HTML
const exportEditorContent2PureHTML = () => {
if (editor.value) {
exportPureHTML(editor.value.state.doc.toString(), posts.value[currentPostIndex.value].title)
}
}
// 下载卡片
const downloadAsCardImage = async () => {
const el = document.querySelector<HTMLElement>(`#output-wrapper>.preview`)!
const url = await toPng(el, {
backgroundColor: isDark.value ? `` : `#fff`,
skipFonts: true,
pixelRatio: Math.max(window.devicePixelRatio || 1, 2),
style: {
margin: `0`,
},
})
downloadFile(url, `${sanitizeTitle(posts.value[currentPostIndex.value].title)}.png`, `image/png`)
}
// 导出编辑器内容为 PDF
const exportEditorContent2PDF = () => {
exportPDF(primaryColor.value, posts.value[currentPostIndex.value].title)
document.querySelector(`#output`)!.innerHTML = output.value
}
// 导出编辑器内容到本地
const exportEditorContent2MD = () => {
if (editor.value) {
downloadMD(editor.value.state.doc.toString(), posts.value[currentPostIndex.value].title)
}
}
// 导入默认文档
const importDefaultContent = () => {
if (editor.value) {
editor.value.dispatch({
changes: { from: 0, to: editor.value.state.doc.length, insert: DEFAULT_CONTENT },
})
toast.success(`文档已重置`)
}
}
// 清空内容
const clearContent = () => {
if (editor.value) {
editor.value.dispatch({
changes: { from: 0, to: editor.value.state.doc.length, insert: `` },
})
toast.success(`内容已清空`)
}
}
const copyToClipboard = async () => {
if (editor.value) {
const selection = editor.value.state.selection.main
const selectedText = editor.value.state.doc.sliceString(selection.from, selection.to)
copyPlain(selectedText)
}
}
const pasteFromClipboard = async () => {
try {
const text = await navigator.clipboard.readText()
if (editor.value) {
editor.value.dispatch(editor.value.state.replaceSelection(text))
}
}
catch (error) {
console.log(`粘贴失败`, error)
}
}
// 撤销操作
const undo = () => {
if (editor.value) {
// undo 和 redo 由键盘快捷键处理,这里不需要手动实现
console.log(`Undo should be handled by keyboard shortcuts`)
}
}
// 重做操作
const redo = () => {
if (editor.value) {
// undo 和 redo 由键盘快捷键处理,这里不需要手动实现
console.log(`Redo should be handled by keyboard shortcuts`)
}
}
// 是否打开重置样式对话框
const isOpenConfirmDialog = ref(false)
// 重置样式
const resetStyleConfirm = () => {
isOpenConfirmDialog.value = true
}
return {
isDark,
toggleDark,
isEditOnLeft,
toggleEditOnLeft,
isMacCodeBlock,
isShowLineNumber,
isCiteStatus,
citeStatusChanged,
showAIToolbox,
aiToolboxChanged,
isUseIndent,
useIndentChanged,
isUseJustify,
useJustifyChanged,
isCountStatus,
countStatusChanged,
output,
editor,
cssEditor,
theme,
fontFamily,
fontSize,
primaryColor,
codeBlockTheme,
legend,
readingTime,
previewWidth,
previewWidthChanged,
editorRefresh,
themeChanged,
fontChanged,
sizeChanged,
colorChanged,
codeBlockThemeChanged,
legendChanged,
macCodeBlockChanged,
showLineNumberChanged,
formatContent,
exportEditorContent2HTML,
exportEditorContent2PureHTML,
exportEditorContent2MD,
exportEditorContent2PDF,
downloadAsCardImage,
importDefaultContent,
clearContent,
copyToClipboard,
pasteFromClipboard,
undo,
redo,
isOpenConfirmDialog,
resetStyleConfirm,
resetStyle,
cssContentConfig,
addCssContentTab,
validatorTabName,
setCssEditorValue,
tabChanged,
renameTab,
posts,
currentPostId,
currentPostIndex,
getPostById,
addPost,
putPost,
renamePost,
delPost,
isOpenPostSlider,
isOpenRightSlider,
titleList,
isMobile,
updatePostParentId,
collapseAllPosts,
expandAllPosts,
editorContent2HTML,
}
})
export const useDisplayStore = defineStore(`display`, () => {
// 是否展示 CSS 编辑器
const isShowCssEditor = useStorage(`isShowCssEditor`, false)
const toggleShowCssEditor = useToggle(isShowCssEditor)
// 是否展示插入表格对话框
const isShowInsertFormDialog = ref(false)
const toggleShowInsertFormDialog = useToggle(isShowInsertFormDialog)
// 是否展示插入公众号名片对话框
const isShowInsertMpCardDialog = ref(false)
const toggleShowInsertMpCardDialog = useToggle(isShowInsertMpCardDialog)
// 是否展示上传图片对话框
const isShowUploadImgDialog = ref(false)
const toggleShowUploadImgDialog = useToggle(isShowUploadImgDialog)
const aiDialogVisible = ref(false)
const aiImageDialogVisible = ref(false)
function toggleAIDialog(value?: boolean) {
aiDialogVisible.value = value ?? !aiDialogVisible.value
}
function toggleAIImageDialog(value?: boolean) {
aiImageDialogVisible.value = value ?? !aiImageDialogVisible.value
}
return {
isShowCssEditor,
toggleShowCssEditor,
isShowInsertFormDialog,
toggleShowInsertFormDialog,
isShowInsertMpCardDialog,
toggleShowInsertMpCardDialog,
isShowUploadImgDialog,
toggleShowUploadImgDialog,
aiDialogVisible,
toggleAIDialog,
aiImageDialogVisible,
toggleAIImageDialog,
}
})
// 获取所有状态的方法
export function getAllStoreStates() {
const store = useStore()
const displayStore = useDisplayStore()
return {
// 主 store 的状态
isDark: store.isDark,
isEditOnLeft: store.isEditOnLeft,
isMacCodeBlock: store.isMacCodeBlock,
isShowLineNumber: store.isShowLineNumber,
isCiteStatus: store.isCiteStatus,
showAIToolbox: store.showAIToolbox,
isCountStatus: store.isCountStatus,
isUseIndent: store.isUseIndent,
isUseJustify: store.isUseJustify,
isOpenRightSlider: store.isOpenRightSlider,
isOpenPostSlider: store.isOpenPostSlider,
theme: store.theme,
fontFamily: store.fontFamily,
fontSize: store.fontSize,
primaryColor: store.primaryColor,
codeBlockTheme: store.codeBlockTheme,
legend: store.legend,
currentPostId: store.currentPostId,
currentPostIndex: store.currentPostIndex,
posts: store.posts,
cssContentConfig: store.cssContentConfig,
titleList: store.titleList,
readingTime: store.readingTime,
// displayStore 的状态
isShowCssEditor: displayStore.isShowCssEditor,
isShowInsertFormDialog: displayStore.isShowInsertFormDialog,
isShowUploadImgDialog: displayStore.isShowUploadImgDialog,
isShowInsertMpCardDialog: displayStore.isShowInsertMpCardDialog,
aiDialogVisible: displayStore.aiDialogVisible,
aiImageDialogVisible: displayStore.aiImageDialogVisible,
}
}