From 595dda11bffacc302d0aeed3598f265c2568543f Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Sat, 24 Mar 2018 19:14:06 +0100 Subject: [PATCH] Control pasting, better IE support --- .../client/components/rte/RTE.js | 49 ++++++++++++++++++- .../components/rte/factories/createToggle.js | 1 + .../client/components/rte/lib/dom.js | 2 + 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/plugins/talk-plugin-rich-text/client/components/rte/RTE.js b/plugins/talk-plugin-rich-text/client/components/rte/RTE.js index 2694c3a26..074809b1d 100644 --- a/plugins/talk-plugin-rich-text/client/components/rte/RTE.js +++ b/plugins/talk-plugin-rich-text/client/components/rte/RTE.js @@ -4,8 +4,9 @@ import styles from './RTE.css'; import cn from 'classnames'; import ContentEditable from 'react-contenteditable'; import Toolbar from './components/Toolbar'; -import { insertNewLine } from './lib/dom'; +import { insertNewLine, insertText } from './lib/dom'; import API from './lib/api'; +import bowser from 'bowser'; class RTE extends React.Component { ref = null; @@ -61,12 +62,53 @@ class RTE extends React.Component { return handled; }; + handleCut = () => { + // IE has issues not firing the onChange event. + if (bowser.msie) { + setTimeout(this.handleChange); + } + }; + + handlePaste = e => { + // Get text representation of clipboard + // This works cross browser. + const text = ( + (e.originalEvent || e).clipboardData || window.clipboardData + ).getData('Text'); + + // insert text manually + insertText(text); + this.handleChange(); + + e.preventDefault(); + return false; + }; + handleMouseUp = () => { setTimeout(() => this.handleSelectionChange()); }; + handleKeyDown = () => { + // IE has issues not firing the onChange event. + if (bowser.msie) { + setTimeout(this.handleChange); + } + }; + + handleKeyUp = () => { + // IE has issues not firing the onChange event. + if (bowser.msie) { + setTimeout(this.handleChange); + } + this.handleSelectionChange(); + }; + handleKeyPress = e => { this.handleSelectionChange(); + // IE has issues not firing the onChange event. + if (bowser.msie) { + setTimeout(this.handleChange); + } if (e.key === 'Enter') { if (!e.shiftKey && this.handleSpecialEnter()) { this.handleChange(); @@ -124,8 +166,11 @@ class RTE extends React.Component { { diff --git a/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js b/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js index e611de234..3531b4333 100644 --- a/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js +++ b/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js @@ -61,6 +61,8 @@ export function insertText(text) { } else { const textNode = document.createTextNode(text); container.insertBefore(textNode, container.childNodes[offset]); + range.setStart(textNode, text.length); + range.setEnd(textNode, text.length); } }