Skip to content

Commit

Permalink
Actually fix detection of Backspace and Delay elements
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jstejada committed Oct 21, 2017
1 parent 29f5b93 commit c5f4b6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 7 additions & 13 deletions src/Typist.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,17 @@ export default class Typist extends Component {
typeLine = (line, lineIdx) => {
if (!this.mounted) { return Promise.resolve(); }

// If `line` is not a string, it is either a `Backspace` or a `Delay` element.
// See `extractTextFromElement`
const isBackspaceOrDelayElement = typeof line !== 'string';

let decoratedLine = line;
const { onLineTyped } = this.props;

if (isBackspaceOrDelayElement) {
if (line.type && line.type.name === 'Backspace') {
if (line.props.delay > 0) {
this.introducedDelay = line.props.delay;
}
decoratedLine = String('🔙').repeat(line.props.count);
} else if (line.type && line.type.name === 'Delay') {
this.introducedDelay = line.props.ms;
decoratedLine = '⏰';
if (utils.isBackspaceElement(line)) {
if (line.props.delay > 0) {
this.introducedDelay = line.props.delay;
}
decoratedLine = String('🔙').repeat(line.props.count);
} else if (utils.isDelayElement(line)) {
this.introducedDelay = line.props.ms;
decoratedLine = '⏰';
}

return new Promise((resolve, reject) => {
Expand Down
12 changes: 10 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ export function exclude(obj, keys) {
return res;
}

export function isBackspaceElement(element) {
return element && element.type === Backspace;
}

export function isDelayElement(element) {
return element && element.type === Delay;
}

export function extractTextFromElement(element) {
const stack = element ? [element] : [];
const lines = [];

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

const isNonTypistElement = (
React.isValidElement(element) &&
!(element.type === Delay || element.type === Backspace)
!(isBackspaceElement(element) || isDelayElement(element))
);

if (isNonTypistElement) {
Expand Down

0 comments on commit c5f4b6f

Please sign in to comment.