react-simple-wysiwyg icon indicating copy to clipboard operation
react-simple-wysiwyg copied to clipboard

Custom Toolbar Button

Open MC-AHOP opened this issue 11 months ago • 6 comments

Hey,

first of all, thank you for this repo!
I have a specific use case where I need a custom link button with extended behavior.

Basically, what I want to achieve is: when you click the link button, a popup appears where you can choose your link option using tabs:

  • URL
  • Internal file
  • Related post

To implement this, I created a wrapper for a custom button with a dialog and some additional logic.

My problem is that every action inside the dialog seems to be canceled by the EditorProvider tag. However, I need the EditorContext for my implementation to work.

Is this an issue on my side, or could it be a known bug?

Here are the relevant code snippets:

Editor Toolbar:

<Toolbar>
    <BtnStyles />
    <BtnBold />
    <BtnItalic />
    <BtnBulletList />
    <BtnNumberedList />
    <BtnStrikeThrough />
    <BtnLink />
    <BtnUndo />
    <BtnRedo />
    <MyLinkButton />
</Toolbar>

MyLinkButton Component:

return (
<>
     <dialog>
          <input type="text" />
     </dialog>
    <button ... />
</>
)

It's basic, but I think it demonstrates the concept.

Can you help me with this?
Of course, I'm happy to elaborate further if this isn't enough information!

MC-AHOP avatar Jan 27 '25 00:01 MC-AHOP

What do you mean? Does MyLinkButton get remounted too frequently?

megahertz avatar Jan 27 '25 05:01 megahertz

Could be, everytime I click into the Textfield, die cursor instantly gets removed. And Button Events are cancelled, so if I wanna open another Dropdown e.g. to select files, the dropdown wont open up. I honestly have no direct idea how this happens :/

MC-AHOP avatar Jan 27 '25 09:01 MC-AHOP

After some observation, it seems that the editor including the buttons and the containing dialog Element gets remounted, rerendered or just updated, everytime I click on any controll of the dialog.

MC-AHOP avatar Jan 27 '25 09:01 MC-AHOP

Please try wrapping your component by the memo HOC. If that isn't helpful, please send me the whole sample that reproduces it.

megahertz avatar Jan 27 '25 10:01 megahertz

i solved the issue by moving the dialog to the outer component (before EditorProvider) and passing the EditorContext via setState from inside EditorProvider.

But now I found another Issue. When I create a Link inside of a P (with the Default Link Button), its not possable to remove it, because $selections returns the p instead of the a

The HTML inside the editor would be

<p>This is a typed Text with a <a href="#">link</a></p>

When I select the link and press the link button again, selection is the P element and $el is the wrapping div.

MC-AHOP avatar Jan 27 '25 11:01 MC-AHOP

If the standard document.execCommand('unlink') doesn't work, you must do low-level DOM manipulation. I can't help with it unfortunately.

megahertz avatar Jan 27 '25 14:01 megahertz