Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setFragmentData without clipboardData object #4333

Merged
merged 3 commits into from
Aug 6, 2021

Conversation

dylans
Copy link
Collaborator

@dylans dylans commented Jun 10, 2021

Description

It is highly useful to get a copy of the exact selection and it is available in slate-react as e.setFragmentData. Unfortunately the type definition currently requires a DataTransfer object which is difficult to stub as this object comes from ClipboardData or drag-n-drop events. In practice, the only requirement slate-react actually needs is getData/setData. Users could provide a data object that looks like this (let me know if this should be provided as a utility method):

  const getDataTransfer = () => {
    const storage = new Map()
    return {
      getData: (key: string) => storage.get(key),
      setData: (key: string, value: string) => storage.set(key, value),
    }
  }

Issue
Fixes: Allows an easy way to get access to the slate tree in application/x-slate-fragment, 'text/html', or 'text/plain' without a copy/paste or DnD operation.

Example

The API footprint is exactly the same other than how the DataTransfer object gets passed in. Using the above method, users could do something like:

const data = ReactEditor.setFragmentData(getDataTransfer())
const fragment =  data.getData('application/x-slate-fragment')
if (fragment) {
  const decoded = decodeURIComponent(window.atob(fragment));
  const parsed = JSON.parse(decoded) as Node[];
  console.log(parsed) // see the slate tree for the selection
}

It's not 100% certain to me if setFragmentData should be async, so I've left it sync.

Context
See above. Let me know if this should work differently and I'll update accordingly.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test. (Note: I did not write a new test for this change)
  • 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.)

@changeset-bot
Copy link

changeset-bot bot commented Jun 10, 2021

🦋 Changeset detected

Latest commit: 2711328

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Patch

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

@dylans
Copy link
Collaborator Author

dylans commented Jun 17, 2021

Any chance this one can get a review? It's really just relaxing the type definition to what is actually needed within the method, and returning data.

This makes it much easier to get the slate ast from whatever is selected within the document.

The workaround currently is to clone the method in my codebase, but given how many workarounds are included I really don't want to maintain a fork. Thanks!

Copy link
Collaborator

@timbuckley timbuckley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely benign type-relaxing, no need for tests IMO. Approved!

(Btw, in comments w/ code examples, it's helpful to add a lang tag to the tics, eg: )

` ` `ts 
const code = 'is highlighted in typescript'
` ` `

rendering:

const code = 'is highlighted in typescript'

@dylans dylans merged commit e0776c5 into ianstormtaylor:main Aug 6, 2021
@dylans dylans deleted the decouple-fragment-data-clipboard branch August 21, 2021 11:17
dylans added a commit to dylans/slate that referenced this pull request Sep 13, 2021
* allow setFragmentData to be used without a copy/paste or DnD data structure

* Add changeset

* return data for use outside of setFragmentData call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants