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
| <!-- Editor.vue -->
<template>
<div class="markdown-editor">
<div class="editor-container">
<textarea
ref="editor"
v-model="content"
@input="handleInput"
@keydown="handleKeydown"
@scroll="handleScroll"
placeholder="开始写作..."
class="editor-textarea"
spellcheck="false"
></textarea>
</div>
<Preview
:content="renderedContent"
:scroll-ratio="scrollRatio"
@scroll="handlePreviewScroll"
/>
</div>
</template>
<script setup>
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import { marked } from 'marked'
import DOMPurify from 'dompurify'
import Preview from './Preview.vue'
const props = defineProps({
modelValue: {
type: String,
default: ''
},
theme: {
type: String,
default: 'light'
}
})
const emit = defineEmits(['update:modelValue', 'change'])
const content = ref(props.modelValue)
const scrollRatio = ref(0)
const editor = ref(null)
// 配置marked
marked.setOptions({
breaks: true,
gfm: true,
headerIds: true,
mangle: false
})
// 渲染Markdown
const renderedContent = computed(() => {
if (!content.value) return ''
try {
const html = marked(content.value)
return DOMPurify.sanitize(html)
} catch (error) {
console.error('Markdown parsing error:', error)
return '<p>解析错误</p>'
}
})
// 处理输入
const handleInput = () => {
emit('update:modelValue', content.value)
emit('change', content.value)
}
// 处理键盘事件
const handleKeydown = (e) => {
// Tab键插入空格
if (e.key === 'Tab') {
e.preventDefault()
insertText(' ')
}
// Ctrl/Cmd + B 粗体
if ((e.ctrlKey || e.metaKey) && e.key === 'b') {
e.preventDefault()
wrapText('**', '**')
}
// Ctrl/Cmd + I 斜体
if ((e.ctrlKey || e.metaKey) && e.key === 'i') {
e.preventDefault()
wrapText('*', '*')
}
// Ctrl/Cmd + K 链接
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault()
wrapText('[', '](url)')
}
}
// 插入文本
const insertText = (text) => {
const textarea = editor.value
const start = textarea.selectionStart
const end = textarea.selectionEnd
content.value =
content.value.substring(0, start) +
text +
content.value.substring(end)
// 设置光标位置
setTimeout(() => {
textarea.selectionStart = textarea.selectionEnd = start + text.length
}, 0)
}
// 包裹选中文本
const wrapText = (before, after) => {
const textarea = editor.value
const start = textarea.selectionStart
const end = textarea.selectionEnd
const selectedText = content.value.substring(start, end)
const replacement = before + selectedText + after
content.value =
content.value.substring(0, start) +
replacement +
content.value.substring(end)
// 设置选中范围
setTimeout(() => {
textarea.selectionStart = start + before.length
textarea.selectionEnd = start + before.length + selectedText.length
}, 0)
}
// 同步滚动
const handleScroll = (e) => {
const textarea = e.target
const ratio = textarea.scrollTop / (textarea.scrollHeight - textarea.clientHeight)
scrollRatio.value = ratio
}
const handlePreviewScroll = (ratio) => {
const textarea = editor.value
const targetScroll = ratio * (textarea.scrollHeight - textarea.clientHeight)
textarea.scrollTop = targetScroll
}
// 自动保存
let saveTimer = null
const autoSave = () => {
clearTimeout(saveTimer)
saveTimer = setTimeout(() => {
localStorage.setItem('markdown-draft', content.value)
}, 1000)
}
watch(content, autoSave)
// 初始化时加载草稿
onMounted(() => {
const draft = localStorage.getItem('markdown-draft')
if (draft && !content.value) {
content.value = draft
}
})
onUnmounted(() => {
clearTimeout(saveTimer)
})
</script>
<style scoped>
.markdown-editor {
display: flex;
height: 100%;
gap: 1px;
background: var(--border-color);
}
.editor-container {
flex: 1;
background: var(--bg-color);
overflow: hidden;
}
.editor-textarea {
width: 100%;
height: 100%;
padding: 20px;
border: none;
resize: none;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 14px;
line-height: 1.6;
background: var(--bg-color);
color: var(--text-color);
outline: none;
}
.editor-textarea::placeholder {
color: var(--text-secondary);
}
</style>
|