Skip to content

Commit bdabdec

Browse files
fix: display IME characters during composition (#2263)
Co-authored-by: ras0q <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent d00c18d commit bdabdec

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { ModelRef } from 'vue'
2+
import { ref, watch } from 'vue'
3+
4+
export function useIME(content: ModelRef<string>) {
5+
const composingContent = ref(content.value)
6+
watch(content, (v) => {
7+
if (v !== composingContent.value) {
8+
composingContent.value = v
9+
}
10+
})
11+
12+
function onInput(e: Event) {
13+
if (!(e instanceof InputEvent) || !(e.target instanceof HTMLTextAreaElement)) {
14+
return
15+
}
16+
17+
if (e.isComposing) {
18+
composingContent.value = e.target.value
19+
}
20+
else {
21+
content.value = e.target.value
22+
}
23+
}
24+
25+
function onCompositionEnd() {
26+
content.value = composingContent.value
27+
}
28+
29+
return { composingContent, onInput, onCompositionEnd }
30+
}

packages/client/internals/ShikiEditor.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script setup lang="ts">
22
import { getHighlighter } from '#slidev/shiki'
33
import { ref, shallowRef } from 'vue'
4+
import { useIME } from '../composables/useIME'
45
56
const props = defineProps<{
67
placeholder?: string
78
}>()
89
const content = defineModel<string>({ required: true })
10+
const { composingContent, onInput, onCompositionEnd } = useIME(content)
911
1012
const textareaEl = ref<HTMLTextAreaElement | null>(null)
1113
@@ -16,10 +18,12 @@ getHighlighter().then(h => highlight.value = h)
1618
<template>
1719
<div class="absolute left-3 right-0 inset-y-2 font-mono overflow-x-hidden overflow-y-auto cursor-text">
1820
<div v-if="highlight" class="relative w-full h-max min-h-full">
19-
<div class="relative w-full h-max" v-html="`${highlight(content, 'markdown')}&nbsp;`" />
21+
<div class="relative w-full h-max" v-html="`${highlight(composingContent, 'markdown')}&nbsp;`" />
2022
<textarea
21-
ref="textareaEl" v-model="content" :placeholder="props.placeholder"
23+
ref="textareaEl" v-model="composingContent" :placeholder="props.placeholder"
2224
class="absolute inset-0 resize-none text-transparent bg-transparent focus:outline-none caret-black dark:caret-white overflow-y-hidden"
25+
@input="onInput"
26+
@compositionend="onCompositionEnd"
2327
/>
2428
</div>
2529
</div>

0 commit comments

Comments
 (0)