ProseMirror / prosemirror

The ProseMirror WYSIWYM editor
http://prosemirror.net/
MIT License
7.53k stars 334 forks source link

Widget decoration getting stale reference to view when drawn #1440

Closed delashum closed 6 months ago

delashum commented 6 months ago

Problem

The specific problem I am trying to solve it to draw an outline around the entire table selection when the selection changes but I am running into a case where the widget is being painted before the DOM has updated so I don't have access to the latest DOM. If I understand correctly per the conversation in https://github.com/ProseMirror/prosemirror/issues/721 the callback provided to the widget shouldn't be called until the view has fully applied the state changes so this seems like a bug.

Minimal repro

https://codesandbox.io/p/sandbox/dreamy-swartz-s23w7h?file=%2Fsrc%2Findex.js

  1. Open developer console
  2. Drag to select some of the cells in the table (Must at least include the cell that has overflowing text)
  3. Hit "Backspace"
  4. Observe that the logged table height / width that is logged is stale
  5. If you now change your cell selection again (or anything to trigger that widget to draw again) you'll now get the correct table dimensions, but too late
marijnh commented 6 months ago

If I understand correctly per the conversation in https://github.com/ProseMirror/prosemirror/issues/721 the callback provided to the widget shouldn't be called until the view has fully applied the state changes so this seems like a bug.

You didn't understand that correctly. This callback is called to draw the widget, so as part of the update. It should just draw the widget, possibly saving the view reference to use in event handlers, and definitely not poke around in the editor DOM.