Changing custom attribute of <img> via extend on Image-extension will trigger reload of image and loss of selection ` #5985
-
Hi, The problem is that if I set the float-attribute or any other custom style attribute, two things happen:
The first isn't that great an issue, because a proper browser will get it from the cache. I just noticed it because I have 'disable cache' enabled in the Chrome Developer tools. Now I have noticed that if I select an image in the Chrome developer console and set the 'SRC' attribute to the same value it already is, then the image is reloaded. So here lies part of the problem. The second one ruins my GUI: because temporarily there is no image selected, the popups I display disappear. This is annoying because one of the popups enables the user to enter values for e.g. border, margin or padding. Now everytime I change the value, the images is reloaded, the selection disappears and the popup disappears. Now, I can run a command to immediately restore the selection afterwards (see code below), but the 'onSelectionChange' lifecycle hook of the editor was already executed. I could do some save-state of my toolbar and restore it after the image was updated, but I already tried to restore the focus to the input-element for e.g. the margin, padding or border and I cannot make the browser return the focus. Probably because there are timing problems: I want to restore the focus to a control that was hidden because the popup is not yet restored..... Any help would be greatly appreciated. Here is my code. First the function that creates a new extension
In my Angular component that embeds the tiptap-editor-component I have this function:
The HTML for the popup looks like this:
To detect if an image is selected I use the 'onSelectionUpdate' property of the editor:
This will in turn call a function that (amongst other things) does this: It will call the 'hide()' function of all popups that are related to the selected image. The console.log is appearing twice in the log, even if I immediately restore the focus to the selected image.
Please find below the installed extensions
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
It is a little difficult to understand if this is your issue exactly, but my best interpretation is that because It would probably be ideal to change In your case, though, I think that you can probably use something like this instead: const originalSelection = this.editor.state.selection
this.editor.chain().updateAttributes('image', { float: float }).command(({ tr }) => {
tr.setSelection(originalSelection)
return true
}).run() This should run all the transactions in one cycle, and should:
I hope this was helpful, if not, could you include a screen recording to better understand the context of what you are seeing |
Beta Was this translation helpful? Give feedback.
It is a little difficult to understand if this is your issue exactly, but my best interpretation is that because
updateAttributes
changes the selection, you are seeing unwanted selection events?It would probably be ideal to change
updateAttributes
to not mess with selections, but technically this can change behavior of existing commands, so I think we can only change this for the next major version.In your case, though, I think that you can probably use something like this instead: