From 67ef9dc21cf422fb2dfef72e6cd04407e0ed4c0b Mon Sep 17 00:00:00 2001 From: YangFong Date: Sat, 21 Dec 2024 14:18:01 +0800 Subject: [PATCH] feat: display character count --- src/views/CodemirrorEditor.vue | 38 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/views/CodemirrorEditor.vue b/src/views/CodemirrorEditor.vue index ac75dce..2403ea7 100644 --- a/src/views/CodemirrorEditor.vue +++ b/src/views/CodemirrorEditor.vue @@ -170,12 +170,15 @@ watch(isDark, () => { toRaw(editor.value)?.setOption?.(`theme`, theme) }) +const charCount = ref(0) + // 初始化编辑器 function initEditor() { const editorDom = document.querySelector(`#editor`)! if (!editorDom.value) { editorDom.value = store.posts[store.currentPostIndex].content + charCount.value = store.posts[store.currentPostIndex].content.replace(/\s/g, ``).length } editor.value = CodeMirror.fromTextArea(editorDom, { mode: `text/x-markdown`, @@ -222,7 +225,9 @@ function initEditor() { clearTimeout(changeTimer.value) changeTimer.value = setTimeout(() => { onEditorRefresh() - store.posts[store.currentPostIndex].content = e.getValue() + const value = e.getValue() + store.posts[store.currentPostIndex].content = value + charCount.value = value.replace(/\s/g, ``).length }, 300) }) @@ -414,23 +419,28 @@ onMounted(() => { -
-
-
-
-
-
-
- 正在生成 +
+
+
+
+
+
+
+
+ 正在生成 +
+
+ {{ charCount }} 个字符 +