This commit is contained in:
Alex Yatskov 2016-03-18 13:03:10 -07:00
parent 3fd7ab16ac
commit 81ded4fb78

View File

@ -20,32 +20,27 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
function insertBreakAtPoint(e) { function getText(e, length) {
var range; let range = document.caretRangeFromPoint(e.clientX, e.clientY);
var textNode;
var offset;
if (document.caretPositionFromPoint) { let node = range.startContainer;
range = document.caretPositionFromPoint(e.clientX, e.clientY); if (node.nodeType !== 3 /* TEXT_NODE */) {
textNode = range.offsetNode; return '';
offset = range.offset;
} else if (document.caretRangeFromPoint) {
range = document.caretRangeFromPoint(e.clientX, e.clientY);
textNode = range.startContainer;
offset = range.startOffset;
} }
// only split TEXT_NODEs let scan = Math.min(- range.startOffset, length);
if (textNode.nodeType == 3) { console.log(scan);
var replacement = textNode.splitText(offset); range.setEnd(node, range.startOffset + scan);
var br = document.createElement('br');
textNode.parentNode.insertBefore(br, replacement); return range.toString();
}
function onMouseMove(e) {
const text = getText(e);
if (text !== '') {
console.log(text);
} }
} }
function onMouseDown(e) {
insertBreakAtPoint(e);
}
window.addEventListener('mousemove', onMouseMove, false);
window.addEventListener('mousedown', onMouseDown, false);