Skip to content

Commit 9262dbf

Browse files
authored
Merge pull request #7 from jason89521/remove-typist-core
2 parents c676da0 + 0a79639 commit 9262dbf

9 files changed

Lines changed: 270 additions & 320 deletions

File tree

src/TypistCore.ts

Lines changed: 0 additions & 187 deletions
This file was deleted.

src/components/Delay.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ type Props = {
33
ms: number;
44
};
55

6-
const Pause = (props: Props) => {
6+
const Delay = (props: Props) => {
77
return null;
88
};
99

10-
export default Pause;
10+
export default Delay;

src/components/Main.tsx

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import React, { useCallback, useEffect, useRef, useState } from 'react';
2+
3+
import type { Delay, TypedChildren, TypistProps } from '../types/TypistProps';
4+
import insertCursor from '../utils/insertCursor';
5+
import { defaultSplitter, emptyFunc } from '../utils/defaultFuncs';
6+
7+
import getTypedChildrenArray from '../utils/getTypedChildrenArray';
8+
import getActions from '../utils/getActions';
9+
10+
const Main = ({
11+
cursor,
12+
disabled = false,
13+
restartKey,
14+
children,
15+
splitter = defaultSplitter,
16+
typingDelay = 75,
17+
backspaceDelay = typingDelay,
18+
onTypingDone,
19+
startDelay = 0,
20+
finishDelay = 0,
21+
loop = false,
22+
pause = false,
23+
}: TypistProps) => {
24+
const [typedChildrenArray, setTypedChildrenArray] = useState<TypedChildren[]>([]);
25+
const [currentIndex, setCurrentIndex] = useState(-1);
26+
const clearTimerRef = useRef(emptyFunc);
27+
const loopRef = useRef(loop);
28+
const pauseRef = useRef(pause);
29+
30+
const timeoutPromise = useCallback((delay: Delay) => {
31+
return new Promise((resolve, reject) => {
32+
const ms = typeof delay === 'number' ? delay : delay();
33+
const timeoutId = setTimeout(resolve, ms);
34+
clearTimerRef.current = () => {
35+
clearTimeout(timeoutId);
36+
reject();
37+
};
38+
});
39+
}, []);
40+
41+
const pausePromise = useCallback(() => {
42+
return new Promise<void>((resolve, reject) => {
43+
const intervalId = setInterval(() => {
44+
if (!pauseRef.current) {
45+
clearInterval(intervalId);
46+
resolve();
47+
}
48+
});
49+
clearTimerRef.current = () => {
50+
clearInterval(intervalId);
51+
reject();
52+
};
53+
});
54+
}, []);
55+
56+
const loopPromise = useCallback(() => {
57+
return new Promise<void>((resolve, reject) => {
58+
const intervalId = setInterval(() => {
59+
if (loopRef.current) {
60+
clearInterval(intervalId);
61+
resolve();
62+
}
63+
});
64+
clearTimerRef.current = () => {
65+
clearInterval(intervalId);
66+
reject();
67+
};
68+
});
69+
}, []);
70+
71+
useEffect(() => {
72+
loopRef.current = loop;
73+
pauseRef.current = pause;
74+
}, [loop, pause]);
75+
76+
useEffect(() => {
77+
const typedChildrenArray = getTypedChildrenArray(children, splitter);
78+
setTypedChildrenArray(typedChildrenArray);
79+
if (disabled) {
80+
setCurrentIndex(typedChildrenArray.length - 1);
81+
return;
82+
}
83+
84+
(async () => {
85+
try {
86+
do {
87+
setCurrentIndex(-1);
88+
const actions = getActions(children, splitter);
89+
await timeoutPromise(startDelay);
90+
for (const { type, payload } of actions) {
91+
if (type === 'TYPE_TOKEN') {
92+
await pausePromise();
93+
setCurrentIndex(prev => prev + 1);
94+
await timeoutPromise(typingDelay);
95+
} else if (type === 'BACKSPACE') {
96+
let amount = payload;
97+
while (amount--) {
98+
await pausePromise();
99+
setCurrentIndex(prev => prev + 1);
100+
await timeoutPromise(backspaceDelay);
101+
}
102+
} else if (type === 'PASTE') {
103+
setCurrentIndex(prev => prev + 1);
104+
} else if (type === 'PAUSE') {
105+
await timeoutPromise(payload);
106+
}
107+
}
108+
onTypingDone?.();
109+
await timeoutPromise(finishDelay);
110+
await loopPromise();
111+
} while (loopRef.current);
112+
} catch (error) {
113+
// do nothing
114+
}
115+
})();
116+
117+
return () => {
118+
clearTimerRef.current();
119+
};
120+
121+
// eslint-disable-next-line react-hooks/exhaustive-deps
122+
}, [restartKey, disabled]);
123+
124+
const typedChildren = typedChildrenArray[currentIndex];
125+
return <>{cursor ? insertCursor(typedChildren, cursor) : typedChildren}</>;
126+
};
127+
128+
export default Main;

src/components/Typist.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Main from './Main';
2+
import Delay from './Delay';
3+
import Backspace from './Backspace';
4+
import Paste from './Paste';
5+
6+
const Typist = Object.assign(Main, { Delay, Backspace, Paste });
7+
8+
export default Typist;

src/components/Typist.tsx

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)