@@ -34,16 +34,24 @@ const insertTranscript = (transcript: string) => {
3434 return
3535 }
3636
37- // 在单次录音会话期间,持续替换当前的语音插入范围
38- const range = speechRange .value ?? {
39- from: editorInstance .state .selection .from ,
40- to: editorInstance .state .selection .to ,
37+ // autoReplace 模式:替换整个输入框内容
38+ if (speechRange .value === null ) {
39+ // 首次插入,记录起始位置为 0
40+ speechRange .value = {
41+ from: 0 ,
42+ to: 0 ,
43+ }
4144 }
42- const tr = editorInstance .state .tr .insertText (transcript , range .from , range .to )
45+
46+ // 替换从起始位置到当前内容末尾的所有文本
47+ const docSize = editorInstance .state .doc .content .size
48+ const tr = editorInstance .state .tr .insertText (transcript , speechRange .value .from , docSize )
4349 editorInstance .view .dispatch (tr )
50+
51+ // 更新范围,保持起始位置不变,更新结束位置
4452 speechRange .value = {
45- from: range .from ,
46- to: range .from + transcript .length ,
53+ from: speechRange . value .from ,
54+ to: speechRange . value .from + transcript .length ,
4755 }
4856 editorInstance .commands .focus (' end' )
4957}
@@ -62,7 +70,9 @@ const speechOptions = {
6270 emit (' speech-interim' , transcript )
6371 },
6472 onFinal : (transcript : string ) => {
65- insertTranscript (transcript )
73+ if (! props .speechConfig ?.autoReplace ) {
74+ insertTranscript (transcript )
75+ }
6676 emit (' speech-final' , transcript )
6777 },
6878 onEnd : (transcript ? : string ) => {
0 commit comments