Skip to content

Commit 8e3d7e1

Browse files
authored
Merge pull request #260 from china-wangxu/minor-problem2
Minor problem2
2 parents cb473ec + 353720d commit 8e3d7e1

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/components/menus/toolbar/base/font-family.vue

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
:text="t('base.fontFamily.text')"
44
menu-type="select"
55
hide-text
6-
:select-value="editor?.getAttributes('textStyle').fontFamily || null"
6+
:select-value="
7+
isTypeRunning
8+
? null
9+
: editor?.getAttributes('textStyle').fontFamily || null
10+
"
711
:style="{ width: $toolbar.mode !== 'classic' ? '144px' : '90px' }"
812
filterable
913
@menu-click="setFontFamily"
@@ -44,6 +48,14 @@ const options = inject('options')
4448
const $toolbar = useState('toolbar', options)
4549
const $recent = useState('recent', options)
4650
51+
import { getTypewriterRunState } from '@/extensions/type-writer'
52+
let isTypeRunning = $ref(false)
53+
watch(
54+
() => getTypewriterRunState(),
55+
(newValue: boolean) => {
56+
isTypeRunning = newValue
57+
},
58+
)
4759
const usedFonts = $ref<string[]>([])
4860
// https://www.cnblogs.com/gaidalou/p/8479452.html
4961
const fontDetect = (font?: string) => {

src/components/menus/toolbar/base/font-size.vue

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
hide-text
77
style="width: 80px"
88
:select-options="fontSizes"
9-
:select-value="editor?.getAttributes('textStyle').fontSize || '14px'"
9+
:select-value="
10+
isTypeRunning
11+
? null
12+
: editor?.getAttributes('textStyle').fontSize || '14px'
13+
"
1014
v-bind="$attrs"
1115
:placeholder="t('base.fontSize.text')"
1216
filterable
@@ -43,7 +47,14 @@ const $toolbar = useState('toolbar', options)
4347
const disableItem = (name: string) => {
4448
return options.value.toolbar?.disableMenuItems.includes(name)
4549
}
46-
50+
import { getTypewriterRunState } from '@/extensions/type-writer'
51+
let isTypeRunning = $ref(false)
52+
watch(
53+
() => getTypewriterRunState(),
54+
(newValue: boolean) => {
55+
isTypeRunning = newValue
56+
},
57+
)
4758
const fontSizes = [
4859
{ label: t('base.fontSize.default'), value: '14px', order: 4 },
4960
{ label: t('base.fontSize.42pt'), value: '42pt', order: 20 }, // 56

src/extensions/type-writer.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,29 @@ export default Extension.create({
9797
typedChars: 0,
9898
}
9999
//插入内容
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+
})
111115
}
116+
// 取非负数
117+
const speed = Math.max(options?.speed ?? 1, 0)
112118
// 处理内容
113119
const processNode = async (node: any, isTopLevel = false) => {
114120
if (node.type === 'paragraph') {
115121
//当前为段落时 插入段落样式
116-
typeWriterInsertContent([
122+
await typeWriterInsertContent([
117123
{ type: 'paragraph', attrs: node.attrs },
118124
])
119125
// 处理段落内容
@@ -136,9 +142,9 @@ export default Extension.create({
136142
const endIndex = Math.min(i + step, text.length)
137143
const currentText = text.slice(i, endIndex)
138144
await new Promise<void>((resolve) => {
139-
typewriterTimer = setTimeout(() => {
145+
typewriterTimer = setTimeout(async () => {
140146
// 插入当前字符
141-
typeWriterInsertContent([
147+
await typeWriterInsertContent([
142148
{
143149
type: 'text',
144150
text: currentText,
@@ -162,17 +168,17 @@ export default Extension.create({
162168
}
163169

164170
resolve()
165-
}, options?.speed ?? 1)
171+
}, speed)
166172
})
167173
}
168174
} else if (node.type === 'table') {
169-
typeWriterInsertContent([node, { type: 'paragraph' }])
175+
await typeWriterInsertContent([node, { type: 'paragraph' }])
170176
editor.commands.enter()
171177
} else {
172178
if (isTopLevel) {
173-
typeWriterInsertContent([node, { type: 'paragraph' }])
179+
await typeWriterInsertContent([node, { type: 'paragraph' }])
174180
} else {
175-
typeWriterInsertContent([node])
181+
await typeWriterInsertContent([node])
176182
}
177183
}
178184
}
@@ -213,3 +219,7 @@ export default Extension.create({
213219
}
214220
},
215221
})
222+
223+
export const getTypewriterRunState = () => {
224+
return typewriterState.value.isRunning
225+
}

0 commit comments

Comments
 (0)