Skip to content

Commit

Permalink
Read data-slate-fragment when application/x-slate-fragment is missing (
Browse files Browse the repository at this point in the history
…#4454)

* fix missing slate-fragment onpaste ion safari

* add changeset
  • Loading branch information
imdbsd committed Aug 13, 2021
1 parent 935b3a7 commit d06706c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-planes-wonder.md
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fix to read fragment from data-slate-fragment when application/x-slate-fragment is missing
Expand Up @@ -453,8 +453,9 @@ export const AndroidEditable = (props: EditableProps): JSX.Element => {
)}
onPaste={useCallback(
(event: React.ClipboardEvent<HTMLDivElement>) => {
// This unfortunately needs to be handled with paste events instead.
// this will make application/x-slate-fragment exist when onPaste attributes is passed
event.clipboardData = getClipboardData(event.clipboardData)
// This unfortunately needs to be handled with paste events instead.
if (
hasEditableTarget(editor, event.target) &&
!isEventHandled(event, attributes.onPaste) &&
Expand Down
13 changes: 11 additions & 2 deletions packages/slate-react/src/plugin/with-react.ts
Expand Up @@ -9,7 +9,11 @@ import {
NATIVE_OPERATIONS,
flushNativeEvents,
} from '../utils/native'
import { isDOMText, getPlainText } from '../utils/dom'
import {
isDOMText,
getPlainText,
getSlateFragmentAttribute,
} from '../utils/dom'
import { findCurrentLineRange } from '../utils/lines'

/**
Expand Down Expand Up @@ -213,7 +217,12 @@ export const withReact = <T extends Editor>(editor: T) => {
}

e.insertData = (data: DataTransfer) => {
const fragment = data.getData('application/x-slate-fragment')
/**
* Checking copied fragment from application/x-slate-fragment or data-slate-fragment
*/
const fragment =
data.getData('application/x-slate-fragment') ||
getSlateFragmentAttribute(data)

if (fragment) {
const decoded = decodeURIComponent(window.atob(fragment))
Expand Down
18 changes: 16 additions & 2 deletions packages/slate-react/src/utils/dom.ts
Expand Up @@ -234,11 +234,25 @@ export const getPlainText = (domNode: DOMNode) => {
return text
}

/**
* Get x-slate-fragment attribute from data-slate-fragment
*/
const catchSlateFragment = /data-slate-fragment="(.+?)"/m
export const getSlateFragmentAttribute = (
dataTransfer: DataTransfer
): string | void => {
const htmlData = dataTransfer.getData('text/html')
const [, fragment] = htmlData.match(catchSlateFragment) || []
return fragment
}

/**
* Get the x-slate-fragment attribute that exist in text/html data
* and append it to the DataTransfer object
*/
export const getClipboardData = (dataTransfer: DataTransfer): DataTransfer => {
if (!dataTransfer.getData('application/x-slate-fragment')) {
const htmlData = dataTransfer.getData('text/html')
const [, fragment] = htmlData.match(catchSlateFragment) || []
const fragment = getSlateFragmentAttribute(dataTransfer)
if (fragment) {
const clipboardData = new DataTransfer()
dataTransfer.types.forEach(type => {
Expand Down

0 comments on commit d06706c

Please sign in to comment.