Skip to content

Commit 2226808

Browse files
committed
feat: init post list
1 parent c147b9d commit 2226808

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

src/stores/index.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,46 @@ export const useStore = defineStore(`store`, () => {
5252
// 内容编辑器编辑器
5353
const editor = ref<CodeMirror.EditorFromTextArea | null>(null)
5454
// 编辑区域内容
55+
// 预备弃用
5556
const editorContent = useStorage(`__editor_content`, DEFAULT_CONTENT)
57+
// 文章列表
58+
const posts = useStorage(addPrefix(`posts`), [{
59+
title: `文章1`,
60+
content: DEFAULT_CONTENT,
61+
}])
62+
// 当前文章
63+
const currentPostIndex = useStorage(addPrefix(`currentPostIndex`), 0)
64+
65+
const addPost = (title: string) => {
66+
currentPostIndex.value = posts.value.push({
67+
title,
68+
content: DEFAULT_CONTENT,
69+
}) - 1
70+
toast.success(`文章新增成功`)
71+
}
72+
73+
const delPost = (index: number) => {
74+
posts.value.splice(index, 1)
75+
currentPostIndex.value = 0
76+
toast.success(`文章删除成功`)
77+
}
78+
79+
watch(currentPostIndex, () => {
80+
toRaw(editor.value!).setValue(posts.value[currentPostIndex.value].content)
81+
})
82+
83+
onMounted(() => {
84+
// 迁移阶段,兼容之前的方案
85+
if (editorContent.value !== DEFAULT_CONTENT) {
86+
posts.value[currentPostIndex.value].content = editorContent.value
87+
editorContent.value = DEFAULT_CONTENT
88+
}
89+
})
5690

5791
// 格式化文档
5892
const formatContent = () => {
5993
formatDoc((editor.value!).getValue()).then((doc) => {
60-
editorContent.value = doc
94+
posts.value[currentPostIndex.value].content = doc
6195
toRaw(editor.value!).setValue(doc)
6296
})
6397
}
@@ -421,14 +455,17 @@ export const useStore = defineStore(`store`, () => {
421455
isOpenConfirmDialog,
422456
resetStyleConfirm,
423457
resetStyle,
424-
editorContent,
425458

426459
cssContentConfig,
427460
addCssContentTab,
428461
validatorTabName,
429462
setCssEditorValue,
430463
tabChanged,
431464
renameTab,
465+
posts,
466+
currentPostIndex,
467+
addPost,
468+
delPost,
432469
}
433470
})
434471

src/views/CodemirrorEditor.vue

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ import {
2323
ContextMenuShortcut,
2424
ContextMenuTrigger,
2525
} from '@/components/ui/context-menu'
26+
import {
27+
Dialog,
28+
DialogContent,
29+
DialogDescription,
30+
DialogFooter,
31+
DialogHeader,
32+
DialogTitle,
33+
DialogTrigger,
34+
} from '@/components/ui/dialog'
35+
import { Input } from '@/components/ui/input'
36+
2637
import { altKey, altSign, ctrlKey, shiftKey, shiftSign } from '@/config'
2738
import { useDisplayStore, useStore } from '@/stores'
2839
import {
@@ -32,13 +43,14 @@ import {
3243
} from '@/utils'
3344
import fileApi from '@/utils/file'
3445
import CodeMirror from 'codemirror'
46+
import { Plus, Trash } from 'lucide-vue-next'
3547
import { storeToRefs } from 'pinia'
3648
import { onMounted, ref, toRaw, watch } from 'vue'
3749
import { toast } from 'vue-sonner'
3850
3951
const store = useStore()
4052
const displayStore = useDisplayStore()
41-
const { isDark, output, editor, editorContent } = storeToRefs(store)
53+
const { isDark, output, editor } = storeToRefs(store)
4254
const { isShowCssEditor } = storeToRefs(displayStore)
4355
4456
const {
@@ -201,7 +213,7 @@ function initEditor() {
201213
const editorDom = document.querySelector<HTMLTextAreaElement>(`#editor`)!
202214
203215
if (!editorDom.value) {
204-
editorDom.value = editorContent.value
216+
editorDom.value = store.posts[store.currentPostIndex].content
205217
}
206218
editor.value = CodeMirror.fromTextArea(editorDom, {
207219
mode: `text/x-markdown`,
@@ -248,7 +260,7 @@ function initEditor() {
248260
clearTimeout(changeTimer.value)
249261
changeTimer.value = setTimeout(() => {
250262
onEditorRefresh()
251-
editorContent.value = e.getValue()
263+
store.posts[store.currentPostIndex].content = e.getValue()
252264
}, 300)
253265
})
254266
@@ -385,6 +397,16 @@ onMounted(() => {
385397
onEditorRefresh()
386398
mdLocalToRemote()
387399
})
400+
401+
const isOpen = ref(false)
402+
403+
const addPostInputVal = ref(``)
404+
405+
function addPost() {
406+
store.addPost(addPostInputVal.value)
407+
isOpen.value = false
408+
addPostInputVal.value = ``
409+
}
388410
</script>
389411

390412
<template>
@@ -396,10 +418,46 @@ onMounted(() => {
396418
@end-copy="endCopy"
397419
/>
398420
<main class="container-main flex-1">
399-
<div class="container-main-section grid h-full border-1" :class="isShowCssEditor ? 'grid-cols-3' : 'grid-cols-2'">
421+
<div class="container-main-section h-full flex border-1">
422+
<nav class="space-y-1 w-50 border-r bg-gray/20 p-2 dark:bg-gray/40">
423+
<Dialog v-model:open="isOpen">
424+
<DialogTrigger as-child>
425+
<Button variant="outline" class="w-full" size="xs">
426+
<Plus /> 新增文章
427+
</Button>
428+
</DialogTrigger>
429+
<DialogContent>
430+
<DialogHeader>
431+
<DialogTitle>新增文章</DialogTitle>
432+
<DialogDescription>
433+
请输入文章名称
434+
</DialogDescription>
435+
</DialogHeader>
436+
<Input v-model="addPostInputVal" />
437+
<DialogFooter>
438+
<Button @click="addPost()">
439+
确 定
440+
</Button>
441+
</DialogFooter>
442+
</DialogContent>
443+
</Dialog>
444+
<a
445+
v-for="(post, index) in store.posts"
446+
:key="post.title"
447+
href="#"
448+
:class="{
449+
'bg-primary text-primary-foreground': store.currentPostIndex === index,
450+
}"
451+
class="hover:text-primary-foreground hover:bg-primary/90 dark:bg-muted dark:hover:bg-muted h-8 w-full inline-flex items-center justify-start gap-2 whitespace-nowrap rounded px-4 text-sm transition-colors dark:text-white dark:hover:text-white"
452+
@click="store.currentPostIndex = index"
453+
>
454+
<span>{{ post.title }}</span>
455+
<Trash v-if="index == store.currentPostIndex" class="ml-auto size-4" @click.stop="store.delPost(index)" />
456+
</a>
457+
</nav>
400458
<div
401459
ref="codeMirrorWrapper"
402-
class="codeMirror-wrapper border-r-1"
460+
class="codeMirror-wrapper flex-1 border-r-1"
403461
:class="{
404462
'order-1': !store.isEditOnLeft,
405463
}"
@@ -443,7 +501,7 @@ onMounted(() => {
443501
id="preview"
444502
ref="preview"
445503
:span="isShowCssEditor ? 8 : 12"
446-
class="preview-wrapper p-5"
504+
class="preview-wrapper flex-1 p-5"
447505
>
448506
<div id="output-wrapper" :class="{ output_night: !backLight }">
449507
<div class="preview border shadow-xl">
@@ -457,7 +515,7 @@ onMounted(() => {
457515
</div>
458516
</div>
459517
</div>
460-
<CssEditor />
518+
<CssEditor class="flex-1" />
461519
</div>
462520
</main>
463521

0 commit comments

Comments
 (0)