1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-28 17:39:57 +02:00

add fragment handling to core onPaste

This commit is contained in:
Ian Storm Taylor
2016-07-27 14:34:11 -07:00
parent d20b8511bb
commit c85671aba4
2 changed files with 21 additions and 13 deletions

View File

@@ -532,21 +532,13 @@ class Content extends React.Component {
// If html, and the html includes a `data-fragment` attribute, it's actually
// a raw-serialized JSON fragment from a previous cut/copy, so deserialize
// it and insert it normally.
// it and update the data.
if (data.type == 'html' && ~data.html.indexOf('<span data-fragment="')) {
const regexp = /data-fragment="([^\s]+)"/
const matches = regexp.exec(data.html)
const [ full, encoded ] = matches
const fragment = Base64.deserializeNode(encoded)
let { state } = this.props
state = state
.transform()
.insertFragment(fragment)
.apply()
this.onChange(state)
return
data.type = 'fragment'
data.fragment = Base64.deserializeNode(encoded)
}
this.props.onPaste(e, data)

View File

@@ -4,8 +4,6 @@ import Character from '../models/character'
import Placeholder from '../components/placeholder'
import React from 'react'
import String from '../utils/string'
import keycode from 'keycode'
import { IS_WINDOWS, IS_MAC } from '../utils/environment'
/**
* The default plugin.
@@ -482,12 +480,30 @@ function Plugin(options = {}) {
function onPaste(e, data, state) {
switch (data.type) {
case 'fragment':
return onPasteFragment(e, data, state)
case 'text':
case 'html':
return onPasteText(e, data, state)
}
}
/**
* On paste fragment.
*
* @param {Event} e
* @param {Object} data
* @param {State} state
* @return {State}
*/
function onPasteFragment(e, data, state) {
return state
.transform()
.insertFragment(data.fragment)
.apply()
}
/**
* On paste text, split blocks at new lines.
*