@@ -97,23 +97,29 @@ export default Extension.create({
97
97
typedChars : 0 ,
98
98
}
99
99
//插入内容
100
- const typeWriterInsertContent = ( curContent : any ) => {
101
- // editor.commands.insertContent(curContent)
102
- try {
103
- editor
104
- . chain ( )
105
- . insertContent ( curContent )
106
- . focus ( 'end' , {
107
- scrollIntoView : true ,
108
- } )
109
- . run ( )
110
- } catch ( e ) { }
100
+ const typeWriterInsertContent = async ( curContent : any ) => {
101
+ await new Promise < void > ( ( resolve ) => {
102
+ setTimeout ( ( ) => {
103
+ try {
104
+ editor
105
+ . chain ( )
106
+ . insertContent ( curContent )
107
+ . focus ( 'end' , {
108
+ scrollIntoView : true ,
109
+ } )
110
+ . run ( )
111
+ } catch ( e ) { }
112
+ resolve ( )
113
+ } , 0 )
114
+ } )
111
115
}
116
+ // 取非负数
117
+ const speed = Math . max ( options ?. speed ?? 1 , 0 )
112
118
// 处理内容
113
119
const processNode = async ( node : any , isTopLevel = false ) => {
114
120
if ( node . type === 'paragraph' ) {
115
121
//当前为段落时 插入段落样式
116
- typeWriterInsertContent ( [
122
+ await typeWriterInsertContent ( [
117
123
{ type : 'paragraph' , attrs : node . attrs } ,
118
124
] )
119
125
// 处理段落内容
@@ -136,9 +142,9 @@ export default Extension.create({
136
142
const endIndex = Math . min ( i + step , text . length )
137
143
const currentText = text . slice ( i , endIndex )
138
144
await new Promise < void > ( ( resolve ) => {
139
- typewriterTimer = setTimeout ( ( ) => {
145
+ typewriterTimer = setTimeout ( async ( ) => {
140
146
// 插入当前字符
141
- typeWriterInsertContent ( [
147
+ await typeWriterInsertContent ( [
142
148
{
143
149
type : 'text' ,
144
150
text : currentText ,
@@ -162,17 +168,17 @@ export default Extension.create({
162
168
}
163
169
164
170
resolve ( )
165
- } , options ?. speed ?? 1 )
171
+ } , speed )
166
172
} )
167
173
}
168
174
} else if ( node . type === 'table' ) {
169
- typeWriterInsertContent ( [ node , { type : 'paragraph' } ] )
175
+ await typeWriterInsertContent ( [ node , { type : 'paragraph' } ] )
170
176
editor . commands . enter ( )
171
177
} else {
172
178
if ( isTopLevel ) {
173
- typeWriterInsertContent ( [ node , { type : 'paragraph' } ] )
179
+ await typeWriterInsertContent ( [ node , { type : 'paragraph' } ] )
174
180
} else {
175
- typeWriterInsertContent ( [ node ] )
181
+ await typeWriterInsertContent ( [ node ] )
176
182
}
177
183
}
178
184
}
@@ -213,3 +219,7 @@ export default Extension.create({
213
219
}
214
220
} ,
215
221
} )
222
+
223
+ export const getTypewriterRunState = ( ) => {
224
+ return typewriterState . value . isRunning
225
+ }
0 commit comments