Skip to content

Commit 48f38c9

Browse files
authored
fix: update cursor position via CodeMirror for mobile compatibility (#1420)
1 parent 7ef98fd commit 48f38c9

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

apps/web/src/components/editor/Footer.vue

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script setup lang="ts">
2+
import { StateEffect } from '@codemirror/state'
3+
import { EditorView } from '@codemirror/view'
24
import { BookOpen, ChevronRight, ChevronsUpDown, Clock, FileText, Keyboard, Moon, Pilcrow, Search, Sun, Type } from 'lucide-vue-next'
35
import {
46
Popover,
@@ -153,21 +155,26 @@ function cancelGoToLine() {
153155
}
154156
155157
// 监听编辑器变化,更新光标位置
156-
watch(editor, (view, _, onCleanup) => {
157-
if (!view)
158+
const attachedViews = new WeakSet()
159+
160+
watch(editor, (view) => {
161+
if (!view || attachedViews.has(view))
158162
return
159163
160-
updateCursorInfo(view)
164+
attachedViews.add(view)
161165
162-
const onKeyUp = () => updateCursorInfo(view)
163-
const onMouseUp = () => updateCursorInfo(view)
166+
// 初始化一次
167+
updateCursorInfo(view)
164168
165-
view.dom.addEventListener(`keyup`, onKeyUp)
166-
view.dom.addEventListener(`mouseup`, onMouseUp)
169+
const extension = EditorView.updateListener.of((update) => {
170+
// 只在光标或文档变化时更新
171+
if (update.selectionSet || update.docChanged) {
172+
updateCursorInfo(update.view)
173+
}
174+
})
167175
168-
onCleanup(() => {
169-
view.dom.removeEventListener(`keyup`, onKeyUp)
170-
view.dom.removeEventListener(`mouseup`, onMouseUp)
176+
view.dispatch({
177+
effects: StateEffect.appendConfig.of(extension),
171178
})
172179
}, { immediate: true })
173180

0 commit comments

Comments
 (0)