1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-12 09:19:48 +01:00

Remove space after first char when copying from Slate (#716)

Right now if you copy "This thing" from Slate and paste
into a plain text app you will get "T his thing" because
Slate is making the first character a data holding span.

This diff makes the data containing span empty to eliminate
this extra space.

Closes #562
This commit is contained in:
Victor Pontis 2017-04-11 21:37:08 -07:00 committed by Ian Storm Taylor
parent 5c7e31b332
commit 3e573453a8

View File

@ -261,17 +261,11 @@ function Plugin(options = {}) {
const zws = [].slice.call(contents.querySelectorAll('[data-slate-zero-width]')) const zws = [].slice.call(contents.querySelectorAll('[data-slate-zero-width]'))
zws.forEach(zw => zw.parentNode.removeChild(zw)) zws.forEach(zw => zw.parentNode.removeChild(zw))
// Wrap the first character of the selection in a span that has the encoded // Insert an empty span that has the encoded fragment attached as an attribute.
// fragment attached as an attribute, so it will show up in the copied HTML. const dataContainer = window.document.createElement('span')
const wrapper = window.document.createElement('span')
const text = contents.childNodes[0] const text = contents.childNodes[0]
const char = text.textContent.slice(0, 1) dataContainer.setAttribute('data-slate-fragment', encoded)
const first = window.document.createTextNode(char) contents.insertBefore(dataContainer, text)
const rest = text.textContent.slice(1)
text.textContent = rest
wrapper.appendChild(first)
wrapper.setAttribute('data-slate-fragment', encoded)
contents.insertBefore(wrapper, text)
// Add the phony content to the DOM, and select it, so it will be copied. // Add the phony content to the DOM, and select it, so it will be copied.
const body = window.document.querySelector('body') const body = window.document.querySelector('body')