Created
June 12, 2017 01:48
-
-
Save rksm/07618734685c9fffc96afe42d11ab586 to your computer and use it in GitHub Desktop.
extractHTMLFromTextMorph (via dom range)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function extractHTMLFromTextMorph(textMorph, range) { | |
// let html = extractHTMLFromTextMorph(this, this.selection); | |
evt.domEvt.clipboardData.setData("text/html", textLayerNode.outerHTML); | |
let node = textMorph.env.renderer.getNodeForMorph(textMorph), | |
textLength = this.document.stringSize, | |
firstLineNode = node.querySelector(".newtext-text-layer.actual"), | |
// _ = console.log(`START, looking for ${textMorph.positionToIndex(range.start)}`), | |
start = findNodeWithOffsetAt(textMorph.positionToIndex(range.start), firstLineNode, undefined, textLength, false), | |
// _3 = console.log(`END, looking for ${textMorph.positionToIndex(range.end)}`), | |
end = findNodeWithOffsetAt(textMorph.positionToIndex(range.end), firstLineNode, undefined, textLength, false), | |
el = document.createElement("div"), | |
r = document.createRange() | |
r.setStart(start.node, start.offset); | |
r.setEnd(end.node, end.offset); | |
el.appendChild(r.cloneContents()/*fragment*/); | |
return el.innerHTML; | |
} | |
function findNodeWithOffsetAt(index, node, currentIndex = 0, maxLength = Infinity, debug = false) { | |
if (index >= maxLength) return {offset: 0, node, found: true}; | |
if (index == currentIndex) return {offset: 0, node, found: true}; | |
let {TEXT_NODE, ELEMENT_NODE} = node; | |
if (node.nodeType === TEXT_NODE) { | |
debug && console.log(`In text node ${currentIndex} "${node.textContent}"`); | |
var nextIndex = currentIndex + node.length; | |
if (index < nextIndex) { | |
debug && console.log({node, found: true, offset: index - currentIndex}, currentIndex); | |
return {node, found: true, offset: index - currentIndex}; | |
} else if (index === nextIndex) { | |
// at line end = newline | |
debug && console.log({node, found: true, offset: node.length}); | |
return {node, found: true, offset: node.length}; | |
} | |
currentIndex = nextIndex; | |
} else if (node.nodeType === ELEMENT_NODE) { | |
if (String(node.tagName).toLowerCase() === "br") { | |
currentIndex = currentIndex + 1; | |
if (index === currentIndex) { | |
debug && console.log({node, found: true, offset: 0}) | |
return {node, found: true, offset: 0}; | |
} | |
} else { | |
debug && console.log(`In element node ${currentIndex} "${node}"`); | |
for (let child of node.childNodes) { | |
let result = findNodeWithOffsetAt(index, child, currentIndex, maxLength, debug); | |
if (result.found) return result; | |
currentIndex = result.currentIndex; | |
} | |
} | |
} | |
return {offset: 0, node, found: false, currentIndex}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment