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

Maintaining Cursor Position at the End of Line After Using .setMarkdown #345

Closed
katefadeeva opened this issue Feb 1, 2024 · 11 comments
Closed
Labels
bug Something isn't working released

Comments

@katefadeeva
Copy link

I use .setMarkdown, but after using it, the cursor always jumps to the beginning. Could you please tell me how to make the cursor stay at the end of the line after applying .setMarkdown?

@katefadeeva katefadeeva added the bug Something isn't working label Feb 1, 2024
@petyosi
Copy link
Contributor

petyosi commented Feb 1, 2024

Did you try calling the focus() method afterwards? it has options for the selection location.

@katefadeeva
Copy link
Author

Did you try calling the focus() method afterwards? it has options for the selection location.

Yes, I've tried the .focus() function, both inside onChange and through useEffect. I'm trying to solve the issue of limiting the number of characters for input. I was able to limit it, but when I use .setMarkdown inside onChange, it throws me back to the beginning of the line, but I need the cursor to stay in place.

@petyosi petyosi closed this as completed in 95092a2 Feb 3, 2024
Copy link

github-actions bot commented Feb 3, 2024

🎉 This issue has been resolved in version 2.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@petyosi
Copy link
Contributor

petyosi commented Feb 3, 2024

I see. What you want to do makes sense, but doing it through setMarkdown is not the way to go. I shipped a maxLengthPlugin in the latest version, check it out.

@katefadeeva
Copy link
Author

katefadeeva commented Feb 5, 2024

I see. What you want to do makes sense, but doing it through setMarkdown is not the way to go. I shipped a maxLengthPlugin in the latest version, check it out.

Can you please advise if it's possible to add getTextContentSize (used in the plugin) to the Interface: MDXEditorMethods (besides focus, getMarkdown, insertMarkdown, setMarkdown)?

@petyosi
Copy link
Contributor

petyosi commented Feb 5, 2024

There's no point in doing that .The Lexical API is quite rich, and there's no point in trying to re-wrap it to the editor instance. You can use the MDXEditor plugin interface design and its inner utilities to access the lexical editor instance and use practically all of the API available.

@katefadeeva
Copy link
Author

There's no point in doing that .The Lexical API is quite rich, and there's no point in trying to re-wrap it to the editor instance. You can use the MDXEditor plugin interface design and its inner utilities to access the lexical editor instance and use practically all of the API available.

If you want to display a counter for available characters, how can you do that?

@petyosi
Copy link
Contributor

petyosi commented Feb 6, 2024

I don't have a ready made recipe for that, I'm afraid. Take a look at the source code of the plugin I published, it should be fairly easy to extract the count from there. If you have more concrete questions about how to do something, let me know.

@katefadeeva
Copy link
Author

I don't have a ready made recipe for that, I'm afraid. Take a look at the source code of the plugin I published, it should be fairly easy to extract the count from there. If you have more concrete questions about how to do something, let me know.

Can you please tell me the formula you use for character truncation? I can't find it in your code. I would be very grateful for the help; I want to display the remaining available number of characters to the user, but all my functions calculate it differently than yours. Thank you in advance for your response :)

@petyosi
Copy link
Contributor

petyosi commented Feb 15, 2024

It's this part over here?

const prevTextContentSize = prevEditorState.read(() => rootNode.getTextContentSize())
const textContentSize = rootNode.getTextContentSize()

Notice that I just adapted some of the sample code from Lexical.

@katefadeeva
Copy link
Author

katefadeeva commented Feb 15, 2024

Notice that I just adapted some of the sample code from Lexical.

When I get textContent?.length, the number of characters doesn't match up. Line breaks are not accounted for in textContent, but even after adding the number of line breaks, the value still doesn't match by 3 characters.
Here is my component:

<div ref={editorContainerRef}>
      <StyledMDXEditor
          ref={editorRef}
          markdown={text}
          contentEditableClassName="content"
          plugins={pluginsEditor()}
          autoFocus={{defaultSelection: 'rootEnd', preventScroll: true}}
          onChange={(changedMarkdown) => {
              changeText(
                  changedMarkdown.length === 0 || !changedMarkdown
                      ? ''
                      : changedMarkdown,
              );
          }} />
</div>

I get the number of characters using the formula:

editorContainerRef.current?.firstElementChild?.children[1].firstElementChild?.textContent?.length + (text.split('\n').length - 1)

What am I doing wrong?))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

2 participants