Skip to content

Commit 069bb59

Browse files
committed
完善 useSpeech useSlider 文档和示例
1 parent 9e759d4 commit 069bb59

File tree

6 files changed

+68
-22
lines changed

6 files changed

+68
-22
lines changed

docs/useSlider.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Vue UI hook that provides slide behavior over any HTML element. Supports both mo
88
import {useSlider, useRef} from 'vue-next-use';
99

1010
const Demo = () => {
11-
const ref = React.useRef(null);
11+
const ref = useRef(null);
1212
const {isSliding, value, pos, length} = useSlider(ref);
1313

1414
return (

docs/useSpeech.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `useSpeech`
2+
3+
Vue UI hook that synthesizes human voice that speaks a given string.
4+
5+
6+
## Usage
7+
8+
```jsx
9+
import {useSpeech} from 'vue-next-use';
10+
11+
const Demo = {
12+
setup(props) {
13+
14+
const voices = window.speechSynthesis.getVoices();
15+
16+
const state = useSpeech('Hello world!', {rate: 0.8, pitch: 0.5, voice: voices[0]});
17+
18+
return () => (
19+
<pre>{JSON.stringify(state, null, 2)}</pre>
20+
);
21+
}
22+
};
23+
```

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export {default as useIntersection} from './useIntersection';
4545
export {default as useKeyPress} from './useKeyPress';
4646
export {default as useKeyPressEvent} from './useKeyPressEvent';
4747
export {default as useKeyboardJs} from './useKeyboardJs';
48-
export {default as useMount} from './useMount';
48+
export {default as useMounted} from './useMounted';
4949
export {default as useLocation} from './useLocation';
5050
export {default as useLongPress} from './useLongPress';
5151
export {default as useBattery} from './useBattery';

src/useMount.ts renamed to src/useMounted.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {onMounted} from "vue";
22

3-
export default function useMount(fn: () => void) {
3+
export default function useMounted(fn: () => void) {
44
onMounted(() => {
55
fn();
66
})

src/useSpeech.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import {Ref, ref, onMounted} from "vue";
2-
import {useEffect, useState, useSetState} from "./index";
1+
import {useEffect, useReadonly, useRef, useMounted} from "./index";
32
import {isBrowser} from './misc/util';
4-
import {watch} from "rollup";
53

64
export interface SpeechState {
75
isPlaying: boolean;
@@ -22,8 +20,8 @@ export interface SpeechOptions {
2220

2321
const voices = isBrowser && typeof window.speechSynthesis === 'object' ? window.speechSynthesis.getVoices() : [];
2422

25-
const useSpeech = (text: string, opts: SpeechOptions = {}): Ref<SpeechState> => {
26-
const [state, setState] = useSetState<SpeechState>({
23+
const useSpeech = (text: string, opts: SpeechOptions = {}): SpeechState => {
24+
const [state, setState] = useReadonly<SpeechState>({
2725
isPlaying: false,
2826
lang: opts.lang || 'default',
2927
voice: opts.voice || voices[0],
@@ -32,20 +30,22 @@ const useSpeech = (text: string, opts: SpeechOptions = {}): Ref<SpeechState> =>
3230
volume: opts.volume || 1,
3331
});
3432

35-
const utteranceRef = ref<SpeechSynthesisUtterance | null>(null);
36-
37-
const utterance = new SpeechSynthesisUtterance(text);
38-
opts.lang && (utterance.lang = opts.lang);
39-
opts.voice && (utterance.voice = opts.voice);
40-
utterance.rate = opts.rate || 1;
41-
utterance.pitch = opts.pitch || 1;
42-
utterance.volume = opts.volume || 1;
43-
utterance.onstart = () => setState({isPlaying: true});
44-
utterance.onresume = () => setState({isPlaying: true});
45-
utterance.onend = () => setState({isPlaying: false});
46-
utterance.onpause = () => setState({isPlaying: false});
47-
utteranceRef.value = utterance;
48-
window.speechSynthesis.speak(utteranceRef.value);
33+
const utteranceRef = useRef<SpeechSynthesisUtterance | null>(null);
34+
35+
useMounted(() => {
36+
const utterance = new SpeechSynthesisUtterance(text);
37+
opts.lang && (utterance.lang = opts.lang);
38+
opts.voice && (utterance.voice = opts.voice);
39+
utterance.rate = opts.rate || 1;
40+
utterance.pitch = opts.pitch || 1;
41+
utterance.volume = opts.volume || 1;
42+
utterance.onstart = () => setState({isPlaying: true});
43+
utterance.onresume = () => setState({isPlaying: true});
44+
utterance.onend = () => setState({isPlaying: false});
45+
utterance.onpause = () => setState({isPlaying: false});
46+
utteranceRef.value = utterance;
47+
window.speechSynthesis.speak(utteranceRef.value);
48+
});
4949

5050
return state;
5151
};

stories/useSpeech.stories.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {useSpeech} from "../src/index";
2+
import {ShowDemo, ShowDocs} from './util/index';
3+
4+
export default {
5+
title: 'UI/useSpeech',
6+
argTypes: {},
7+
};
8+
9+
export const Docs = ShowDocs(require('../docs/useSpeech.md'));
10+
11+
export const Demo = ShowDemo({
12+
setup(props) {
13+
14+
const voices = window.speechSynthesis.getVoices();
15+
16+
const state = useSpeech('Hello world!', {rate: 0.8, pitch: 0.5, voice: voices[0]});
17+
18+
return () => (
19+
<pre>{JSON.stringify(state, null, 2)}</pre>
20+
);
21+
}
22+
});
23+

0 commit comments

Comments
 (0)