Open etrepum opened 5 days ago
Not sure whether the best default would be to use pending or the latest reconciled state
I think it would be easier to reason about this API if it's an alias to
editor.getEditorState().read(() => {}, { activeEditor: editor })
and uses latest reconciled state.
I think it would be easier to reason about this API if it's an alias to
editor.getEditorState().read(() => {}, { activeEditor: editor })
@fantactuka This assumes that the editor
is compatible with the EditorState and makes the EditorState from getEditorState
redundant. Is this correct? In this case, I think that editor.read
depicts more the point of let's try read the most up to date state we can find on the editor
In #6347 I implemented the version where the reconciled state is the default and pending state can be acquired with an option.
Reasons why you might want to read from reconciled state:
Reasons why you might want to read from pending state:
An alternative to this "pending" approach would be a flushSync
or discrete
option (or a separate readSync
method) that would force a reconciliation before the read (if necessary) which would give you a consistent view after transforms and DOM reconciliation.
Description
EditorState.read
can only use $functions that don't depend onactiveEditor
which ends up leading users to useEditor.update
even for fundamentally read-only operations. A common use case would be$getNodeFromDOMNode
or$getNodeFromDOM
or to avoid having explicitLexicalEditor
parameters for $functions ($generateHtmlFromNodes
,$getHtmlContent
, etc.).We should have a way to set the
activeEditor
with an additional argument toEditorState.read
.There should also be a version of
read
directly from theLexicalEditor
for symmetry withupdate
. This would also provide an opportunity to optionally get the_pendingEditorState
so that users can "read their own writes" from anupdate
before reconciliation has occurred.The public API changes would be as follows:
Impact
Users would have a simpler to explain model for what a $function can do, as most would transition from using
editor.update
for read-only operations toeditor.read
and people will also likely prefereditor.read
toeditor.getEditorState().read
.The extra argument to
EditorState.read
would mostly be an implementation detail that I don't think most people would use, but it keeps the code path very similar to the current paradigm.