Skip to content

Commit c5f4b6f

Browse files
committed
Actually fix detection of Backspace and Delay elements
d122bd1 attempted to fix detection of `Backspace` and `Delay` elements by checking their `type` instead of their `type.name` However, due to oversight, we were still performing the check with name inside Typist.jsx. This commit fixes that
1 parent 29f5b93 commit c5f4b6f

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/Typist.jsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,17 @@ export default class Typist extends Component {
114114
typeLine = (line, lineIdx) => {
115115
if (!this.mounted) { return Promise.resolve(); }
116116

117-
// If `line` is not a string, it is either a `Backspace` or a `Delay` element.
118-
// See `extractTextFromElement`
119-
const isBackspaceOrDelayElement = typeof line !== 'string';
120-
121117
let decoratedLine = line;
122118
const { onLineTyped } = this.props;
123119

124-
if (isBackspaceOrDelayElement) {
125-
if (line.type && line.type.name === 'Backspace') {
126-
if (line.props.delay > 0) {
127-
this.introducedDelay = line.props.delay;
128-
}
129-
decoratedLine = String('🔙').repeat(line.props.count);
130-
} else if (line.type && line.type.name === 'Delay') {
131-
this.introducedDelay = line.props.ms;
132-
decoratedLine = '⏰';
120+
if (utils.isBackspaceElement(line)) {
121+
if (line.props.delay > 0) {
122+
this.introducedDelay = line.props.delay;
133123
}
124+
decoratedLine = String('🔙').repeat(line.props.count);
125+
} else if (utils.isDelayElement(line)) {
126+
this.introducedDelay = line.props.ms;
127+
decoratedLine = '⏰';
134128
}
135129

136130
return new Promise((resolve, reject) => {

src/utils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,22 @@ export function exclude(obj, keys) {
3333
return res;
3434
}
3535

36+
export function isBackspaceElement(element) {
37+
return element && element.type === Backspace;
38+
}
39+
40+
export function isDelayElement(element) {
41+
return element && element.type === Delay;
42+
}
43+
3644
export function extractTextFromElement(element) {
3745
const stack = element ? [element] : [];
3846
const lines = [];
3947

4048
while (stack.length > 0) {
4149
const current = stack.pop();
4250
if (React.isValidElement(current)) {
43-
if (current.type === Backspace || current.type === Delay) {
51+
if (isBackspaceElement(current) || isDelayElement(current)) {
4452
// If it is a `Backspace` or `Delay` element, we want to keep it in our
4553
// `textLines` state. These will serve as markers when updating the
4654
// state of the text
@@ -88,7 +96,7 @@ function cloneElementWithSpecifiedTextAtIndex(element, textLines, textIdx) {
8896

8997
const isNonTypistElement = (
9098
React.isValidElement(element) &&
91-
!(element.type === Delay || element.type === Backspace)
99+
!(isBackspaceElement(element) || isDelayElement(element))
92100
);
93101

94102
if (isNonTypistElement) {

0 commit comments

Comments
 (0)