Skip to content

Commit

Permalink
fix: awkward pausing issues due to how deletions were being handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed May 4, 2022
1 parent 2894f0f commit bf3adc8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/typeit/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/typeit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typeit",
"version": "8.5.3",
"version": "8.5.4",
"description": "The most versatile animated typing utility on the planet.",
"author": "Alex MacArthur <alex@macarthur.me> (https://macarthur.me)",
"license": "GPL-3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/src/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let Queue = function (initialItems: QueueItem[]) {
* Given an index, set an item in the queue.
*/
let set = function (index: number, item: QueueItem): void {
let keys = _q.keys();
let keys = [..._q.keys()];

_q.set(keys[index], item);
};
Expand Down
14 changes: 11 additions & 3 deletions packages/typeit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ const TypeIt: TypeItInstance = function (element, options = {}) {
let derivedCursorPosition = _getDerivedCursorPosition();
derivedCursorPosition && (await _move({ value: derivedCursorPosition }));

for (let _i of _queue.getTypeable()) {
// Grab all characters currently mounted to the DOM,
// in order to wipe the slate clean before restarting.
for (let _i of _getAllChars()) {
await _wait(_delete, _getPace(1));
}

_queue.reset();

_queue.set(0, { delay });
};

Expand Down Expand Up @@ -246,7 +247,13 @@ const TypeIt: TypeItInstance = function (element, options = {}) {

if (queueItem.typeable && !_statuses.frozen) _disableCursorBlink(true);

await fireItem(queueItem, _wait);
// Because calling .delete() with no parameters will attempt to
// delete all "typeable" characters, we may overfetch, since some characters
// in the queue may already be deleted. This ensures that we do not attempt to
// delete a character that isn't actually mounted to the DOM.
if (!queueItem.deletable || (queueItem.deletable && _getAllChars().length)) {
await fireItem(queueItem, _wait);
}

_disableCursorBlink(false);

Expand Down Expand Up @@ -366,6 +373,7 @@ const TypeIt: TypeItInstance = function (element, options = {}) {
{
func: _delete,
delay: instant ? 0 : _getPace(1),
deletable: true
},
rounds
),
Expand Down
1 change: 1 addition & 0 deletions packages/typeit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type QueueItem = {
delay?: number;
char?: any;
typeable?: boolean;
deletable?: boolean;
};

export type Element = HTMLElement &
Expand Down

0 comments on commit bf3adc8

Please sign in to comment.