File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11<script setup lang="ts">
22import { getHighlighter } from ' #slidev/shiki'
33import { ref , shallowRef } from ' vue'
4+ import { useIME } from ' ../composables/useIME'
45
56const props = defineProps <{
67 placeholder? : string
78}>()
89const content = defineModel <string >({ required: true })
10+ const { composingContent, onInput, onCompositionEnd } = useIME (content )
911
1012const 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')}  ; `" />
21+ <div class =" relative w-full h-max" v-html =" `${highlight(composingContent , 'markdown')}  ; `" />
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 >
You can’t perform that action at this time.
0 commit comments