slate
slate copied to clipboard
Generic typescript
Description
This is an attempt at making Slate work with TypeScript generics.
Issue Fixes: https://github.com/ianstormtaylor/slate/issues/3725 Fixes: https://github.com/ianstormtaylor/slate/issues/3416
Example
The best example is looking at the new TypeScript example on this branch. (Other examples have been purposefully simplified to have very minimal type annotations, to keep them easier to understand.)
Context
This works by changing the Editor
type to be Editor<V>
where V
represents the "value" being edited by Slate. In the most generic editor, V
would be equivalent to Element[]
(since that is what is accepted as children of the editor). But in a custom editor, you might have Editor<Array<Paragraph | Quote>>
.
Other Editor
-and-Node
-related methods have been also made generic, so for example if you use Editor.leaf(editor, path)
it knows that the return value is a Text
node. But more specifically, it knows that it is the text node of the type you've defined in your custom elements (with any marks you've defined).
This replaces the declaration merging approach, and provides some benefits. One of the drawbacks to declaration merging was that it was impossible to know whether you were dealing with an "unknown" or "known" element, since the underlying type was changed. Similarly, having two editors on the page with different schemas wasn't possible to represent. Hopefully this approach with generics will be able to smoothly replace the declaration merging approach. (While being easy to migrate to, since you can pass those same custom element definitions into Editor
still.)
It's still a work in progress.
Checks
- [x] The new code matches the existing patterns and styles.
- [x] The tests pass with
yarn test
. - [x] The linter passes with
yarn lint
. (Fix errors withyarn fix
.) - [ ] The relevant examples still work. (Run examples with
yarn start
.) - [ ] You've added a changeset if changing functionality. (Add one with
yarn changeset add
.)
🦋 Changeset detected
Latest commit: b207bd87f99581a8004f3981af3bea7649dac364
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 4 packages
Name | Type |
---|---|
slate | Minor |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
/release:next
A new release has been made for this pull request. You can install it with yarn add [email protected]
.
Excited about this branch, it makes a lot more sense than overwriting type definitions. Is there a rough ETA before a @next release?
Also pretty excited about this. Thanks for exploring this!
I'd switch to Slate in a heartbeat if it didn't require using some weird hacky global override thing -- unfortunately it looks like this PR is now broken. :/
We would really need this as well, are there any status updates?
I unfortunately don't have the time to complete that PR, so I've forked that PR into Plate. If you're looking to use these types, see Plate 11.
Is there someone kind and brave enough to lift this PR up and continue the work on it? Or is the official approach against this?
Is there someone kind and brave enough to lift this PR up and continue the work on it? Or is the official approach against this?
The official approach is that we definitely want to finish this, but no one has had time to do so. Plate seems to be doing pretty well with bounties attached to work they've not had time to finish, but I haven't wanted to go that route with Slate.
At this point, a good path would probably be to look at what Plate has done, see if it makes sense to wrap this up, and if so land it. More likely there are some things to improve that were uncovered in that work.
After we land this, a decent test suite for slate-react, and some optimizations for Reach 18 and 19, I'm ready to call it 1.0. (Just in time to start thinking about switching to EditContext API if everyone supports it (currently in Chrome, waiting for indication from others): https://w3c.github.io/edit-context/ (this is a substantial change))