Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function handlePaste(input) {
const {event, type, path} = input
const html = event.clipboardData.getData('text/html')
// check if schema has the code type
const hasCodeType = type.of.map(({name}) => name).includes('code')
if (!hasCodeType) {
console.log('Run `sanity install @sanity/code-input, and add `type: "code"` to your schema.')
}
if (html && hasCodeType) {
const blocks = blockTools.htmlToBlocks(html, type, {
rules: [
{
deserialize(el, next, block) {
/**
* `el` and `next` is DOM Elements
* learn all about them:
* https://developer.mozilla.org/en-US/docs/Web/API/Element
**/
if (!el || !el.children || (el.tagName && el.tagName.toLowerCase() !== 'pre')) {
return undefined
}
const code = el.children[0]
const childNodes =
code && code.tagName.toLowerCase() === 'code' ? code.childNodes : el.childNodes
let text = ''
return wait(100).then(() => {
onProgress({status: 'html'})
const blocks = blockTools.htmlToBlocks(html, blockContentType)
onProgress({status: 'blocks'})
const value = deserialize(blocks, blockContentType)
pasteController.setValue(value)
ensureNoPlaceholder(editor)
editor.insertFragment(pasteController.value.document)
pasteController.setValue(deserialize(null, blockContentType))
onProgress({status: null})
return editor
})
}
function onPaste(event, data, change) {
if (data.type != 'html') {
return null
}
if (data.isShift) {
return null
}
const blockContentType = blockEditor.props.type
const blocks = blockTools.htmlToBlocks(data.html, {blockContentType})
const {document} = blockTools.blocksToSlateState(blocks, blockContentType)
return change.insertFragment(Document.fromJSON(document))
}