Allow Immerable class instances as Element and Text nodes #4665
SmilinBrian
started this conversation in
Ideas
Replies: 1 comment
-
For future reference: Slate makes a lot of assumptions in transforms/node.ts to be able to manipulate |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We are updating TextExpander from using Slate 0.47 to current. It seemed like a good opportunity to convert some of our older JavaScript code to TypeScript as well. With that in mind, I set off creating TypeScript classes for the inline elements that we use.
I made a base class, and made it
immerable
:and then made subclasses for the various inlines, such as this date macro inline:
With things like that defined, it was going to really clean up the code where we create and insert new inlines - we'd have a clear constructor for a date macro instead of our old 0.47 way of adding certain data keys when creating a Node.
Then when I put it to the test my design hopes were dashed - Slate expects incoming data to consist of "plain objects" and uses
isPlainObject()
to test that:…from
slate/packages/slate/src/interfaces/element.ts
Line 34 in 3ad3aac
There are similar tests for Text, Descendant, etc.
Obviously my ContentElements are not plain objects, so the design needs major changing. But, because my
ContentElement
sub-classes areimmerable
, I think it would have worked except for thoseisPlainObject()
tests.So I propose that we move the basic "is this incoming object acceptable in the most basic terms" (currently
isPlainObject()
) to be an overrideable function onEditor
.Specifically, in the
BaseEditor
interface, add:then
createEditor
would provide a default implementation:and it would require an
Editor.isAcceptableObject(editor, candidate)
implementation to tie it all together.Or simply change the
isPlainObject()
tests to call a utility function that checksisPlainObject(candidate) || candidate[immerable]
.This would allow us to gain more "reasoning" about inlines and blocks by making them TypeScript class instances and, as long as developers make them
immerable
and are careful, everything should work.Beta Was this translation helpful? Give feedback.
All reactions