mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-12 10:14:02 +02:00
Read data-slate-fragment when application/x-slate-fragment is missing (#4454)
* fix missing slate-fragment onpaste ion safari * add changeset
This commit is contained in:
committed by
GitHub
parent
935b3a79d6
commit
d06706c9e1
@@ -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) &&
|
||||
|
@@ -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'
|
||||
|
||||
/**
|
||||
@@ -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))
|
||||
|
@@ -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 => {
|
||||
|
Reference in New Issue
Block a user