slate icon indicating copy to clipboard operation
slate copied to clipboard

Generic typescript

Open ianstormtaylor opened this issue 3 years ago • 10 comments

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 with yarn 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.)

ianstormtaylor avatar Apr 07 '21 14:04 ianstormtaylor

🦋 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

changeset-bot[bot] avatar Apr 07 '21 14:04 changeset-bot[bot]

/release:next

ianstormtaylor avatar Apr 07 '21 15:04 ianstormtaylor

A new release has been made for this pull request. You can install it with yarn add [email protected].

github-actions[bot] avatar Apr 07 '21 15:04 github-actions[bot]

Excited about this branch, it makes a lot more sense than overwriting type definitions. Is there a rough ETA before a @next release?

ChildishForces avatar Apr 16 '21 17:04 ChildishForces

Also pretty excited about this. Thanks for exploring this!

cmditch avatar Jun 08 '21 20:06 cmditch

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. :/

image

LoganDark avatar Jul 06 '21 22:07 LoganDark

We would really need this as well, are there any status updates?

ionTea avatar Oct 27 '21 08:10 ionTea

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.

zbeyens avatar May 11 '22 10:05 zbeyens

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?

maral avatar Mar 13 '24 20:03 maral

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))

dylans avatar Mar 14 '24 13:03 dylans