mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-15 11:44:05 +02:00
feat: allow to change clipboard fragment format name (#5267)
* feat: allow to change clipboard fragment format name * chore: add changeset
This commit is contained in:
5
.changeset/afraid-cougars-smell.md
Normal file
5
.changeset/afraid-cougars-smell.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Allow to change clipboard fragment format name
|
@@ -45,7 +45,10 @@ import { ReactEditor } from './react-editor'
|
|||||||
* See https://docs.slatejs.org/concepts/11-typescript to learn how.
|
* See https://docs.slatejs.org/concepts/11-typescript to learn how.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const withReact = <T extends BaseEditor>(editor: T): T & ReactEditor => {
|
export const withReact = <T extends BaseEditor>(
|
||||||
|
editor: T,
|
||||||
|
clipboardFormatKey = 'x-slate-fragment'
|
||||||
|
): T & ReactEditor => {
|
||||||
const e = editor as T & ReactEditor
|
const e = editor as T & ReactEditor
|
||||||
const { apply, onChange, deleteBackward, addMark, removeMark } = e
|
const { apply, onChange, deleteBackward, addMark, removeMark } = e
|
||||||
|
|
||||||
@@ -263,7 +266,7 @@ export const withReact = <T extends BaseEditor>(editor: T): T & ReactEditor => {
|
|||||||
const string = JSON.stringify(fragment)
|
const string = JSON.stringify(fragment)
|
||||||
const encoded = window.btoa(encodeURIComponent(string))
|
const encoded = window.btoa(encodeURIComponent(string))
|
||||||
attach.setAttribute('data-slate-fragment', encoded)
|
attach.setAttribute('data-slate-fragment', encoded)
|
||||||
data.setData('application/x-slate-fragment', encoded)
|
data.setData(`application/${clipboardFormatKey}`, encoded)
|
||||||
|
|
||||||
// Add the content to a <div> so that we can get its inner HTML.
|
// Add the content to a <div> so that we can get its inner HTML.
|
||||||
const div = contents.ownerDocument.createElement('div')
|
const div = contents.ownerDocument.createElement('div')
|
||||||
@@ -287,7 +290,7 @@ export const withReact = <T extends BaseEditor>(editor: T): T & ReactEditor => {
|
|||||||
* Checking copied fragment from application/x-slate-fragment or data-slate-fragment
|
* Checking copied fragment from application/x-slate-fragment or data-slate-fragment
|
||||||
*/
|
*/
|
||||||
const fragment =
|
const fragment =
|
||||||
data.getData('application/x-slate-fragment') ||
|
data.getData(`application/${clipboardFormatKey}`) ||
|
||||||
getSlateFragmentAttribute(data)
|
getSlateFragmentAttribute(data)
|
||||||
|
|
||||||
if (fragment) {
|
if (fragment) {
|
||||||
|
@@ -256,15 +256,18 @@ export const getSlateFragmentAttribute = (
|
|||||||
* Get the x-slate-fragment attribute that exist in text/html data
|
* Get the x-slate-fragment attribute that exist in text/html data
|
||||||
* and append it to the DataTransfer object
|
* and append it to the DataTransfer object
|
||||||
*/
|
*/
|
||||||
export const getClipboardData = (dataTransfer: DataTransfer): DataTransfer => {
|
export const getClipboardData = (
|
||||||
if (!dataTransfer.getData('application/x-slate-fragment')) {
|
dataTransfer: DataTransfer,
|
||||||
|
clipboardFormatKey = 'x-slate-fragment'
|
||||||
|
): DataTransfer => {
|
||||||
|
if (!dataTransfer.getData(`application/${clipboardFormatKey}`)) {
|
||||||
const fragment = getSlateFragmentAttribute(dataTransfer)
|
const fragment = getSlateFragmentAttribute(dataTransfer)
|
||||||
if (fragment) {
|
if (fragment) {
|
||||||
const clipboardData = new DataTransfer()
|
const clipboardData = new DataTransfer()
|
||||||
dataTransfer.types.forEach(type => {
|
dataTransfer.types.forEach(type => {
|
||||||
clipboardData.setData(type, dataTransfer.getData(type))
|
clipboardData.setData(type, dataTransfer.getData(type))
|
||||||
})
|
})
|
||||||
clipboardData.setData('application/x-slate-fragment', fragment)
|
clipboardData.setData(`application/${clipboardFormatKey}`, fragment)
|
||||||
return clipboardData
|
return clipboardData
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user