mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +02:00
fix(android): Get fragment from text/html when application/x-slate-fragment is missing on copy-paste slate fragment in android (#4433)
* fix(android): clipboard application/x-slate-fragment is missing on paste in android * add event onCut * add changeset * make slate fragment available when props onPaste, call preventDefault * . * get the fragment from text/html when it missing * remove setData * use getClipboardData instead of hooks
This commit is contained in:
committed by
GitHub
parent
d32ce0f49b
commit
a1f925bddf
5
.changeset/strange-rabbits-refuse.md
Normal file
5
.changeset/strange-rabbits-refuse.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix copy-paste a slate fragment on android editable
|
@@ -13,6 +13,7 @@ import {
|
|||||||
isDOMElement,
|
isDOMElement,
|
||||||
isDOMNode,
|
isDOMNode,
|
||||||
getDefaultView,
|
getDefaultView,
|
||||||
|
getClipboardData,
|
||||||
isPlainTextOnlyPaste,
|
isPlainTextOnlyPaste,
|
||||||
} from '../../utils/dom'
|
} from '../../utils/dom'
|
||||||
import {
|
import {
|
||||||
@@ -453,6 +454,7 @@ export const AndroidEditable = (props: EditableProps): JSX.Element => {
|
|||||||
onPaste={useCallback(
|
onPaste={useCallback(
|
||||||
(event: React.ClipboardEvent<HTMLDivElement>) => {
|
(event: React.ClipboardEvent<HTMLDivElement>) => {
|
||||||
// This unfortunately needs to be handled with paste events instead.
|
// This unfortunately needs to be handled with paste events instead.
|
||||||
|
event.clipboardData = getClipboardData(event.clipboardData)
|
||||||
if (
|
if (
|
||||||
hasEditableTarget(editor, event.target) &&
|
hasEditableTarget(editor, event.target) &&
|
||||||
!isEventHandled(event, attributes.onPaste) &&
|
!isEventHandled(event, attributes.onPaste) &&
|
||||||
|
@@ -233,3 +233,20 @@ export const getPlainText = (domNode: DOMNode) => {
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const catchSlateFragment = /data-slate-fragment="(.+?)"/m
|
||||||
|
export const getClipboardData = (dataTransfer: DataTransfer): DataTransfer => {
|
||||||
|
if (!dataTransfer.getData('application/x-slate-fragment')) {
|
||||||
|
const htmlData = dataTransfer.getData('text/html')
|
||||||
|
const [, fragment] = htmlData.match(catchSlateFragment) || []
|
||||||
|
if (fragment) {
|
||||||
|
const clipboardData = new DataTransfer()
|
||||||
|
dataTransfer.types.forEach(type => {
|
||||||
|
clipboardData.setData(type, dataTransfer.getData(type))
|
||||||
|
})
|
||||||
|
clipboardData.setData('application/x-slate-fragment', fragment)
|
||||||
|
return clipboardData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dataTransfer
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user