mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-16 20:24:01 +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
@@ -13,6 +13,7 @@ import {
|
||||
isDOMElement,
|
||||
isDOMNode,
|
||||
getDefaultView,
|
||||
getClipboardData,
|
||||
isPlainTextOnlyPaste,
|
||||
} from '../../utils/dom'
|
||||
import {
|
||||
@@ -453,6 +454,7 @@ export const AndroidEditable = (props: EditableProps): JSX.Element => {
|
||||
onPaste={useCallback(
|
||||
(event: React.ClipboardEvent<HTMLDivElement>) => {
|
||||
// This unfortunately needs to be handled with paste events instead.
|
||||
event.clipboardData = getClipboardData(event.clipboardData)
|
||||
if (
|
||||
hasEditableTarget(editor, event.target) &&
|
||||
!isEventHandled(event, attributes.onPaste) &&
|
||||
|
@@ -233,3 +233,20 @@ export const getPlainText = (domNode: DOMNode) => {
|
||||
|
||||
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