Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with dynamic strings #595

Open
okboy5555 opened this issue Dec 23, 2023 · 6 comments
Open

How to deal with dynamic strings #595

okboy5555 opened this issue Dec 23, 2023 · 6 comments
Labels

Comments

@okboy5555
Copy link

Description

I am implementing an AI dialogue function, strings,i need to dynamically change the value of strings,since my data is being streamed, it may need to change while typing.
After 2 days of trying,I found that it seems that such a function cannot be achieved.

const typed = new Typed(dialogueContentRef.current, {
      strings: dialogueContentRef.current.strings,
      typeSpeed: 50,
      showCursor: false,
    });
    dialogueContentRef.current.typed = typed;

when i try to change strings value

dialogueContentRef.current.strings.push(messageValue)  // it can't work
dialogueContentRef.current.strings = ['test']  // it can work

Now, not providing a way to continue typing, I have to reset, like this

dialogueContentRef.current..reset()

The problem with this is that all the text is printed from scratch, which doesn't satisfy my expectations.
I hope that when I receive new text, I can continue to print
like this

this demo shows print 'test1', after xxx s, print 'test2'

print('test1');
settimeOut(xxx);
print('test2');
settimeOut(xxx);
print('test3');
.
.
.

Do you have any good suggestions, because currently it seems that strings cannot be accepted with a state

@mattboldt
Copy link
Owner

Right now, I don't think this is possible with Typed.js without resetting. I started a draft PR playing around with some options: #596

The main hurdle here is that Typed.js assumes it should backspace every sentence it types before moving into the next one. This doesn't work when streaming new data in, like ChatGPT. I'll need to come up with a better way to append without deleting the previous text.

However, if you do want to backspace the previous text, and just begin typing the newly appended sentence, that's a much easier lift, and I could get that in a branch fairly quickly. I just wasn't sure if that's a real use-case.

@livingemoji
Copy link

changing the text being typed without resetting the entire animation, you'll need to update the strings array of the Typed instance. but you can try creating a function that updates the strings array and then destroys the current Typed instance and creates a new one with the updated strings

@chris-moon
Copy link

Unfortunately, destroying the instance will cause it to start typing from scratch again. If you don't destroy it, it will keep typing with the new data, but unfortunately, you get a bunch of cursors for each time it re-renders.

@BoBoooooo
Copy link

I have the same scene with ChatGPT stream output . Hope typed js can support this scene. thanks

@costea93
Copy link

costea93 commented Jul 3, 2024

@BoBoooooo did you manage to do it? I am wondering how do you make it to render DOM elements not just simple texts?

@perrocochino666
Copy link

perrocochino666 commented Jul 11, 2024

Please @mattboldt add this feature, It would be a great step for typed.js, I have this site: www.progreferon.com where songs and albums info load dynamically, I did everything and came to the same conclusions that are mentioned here, please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants