question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pasting Rich Text Into Chrome & Firefox on Windows Adds A Space On Either Side

See original GitHub issue

When pasting rich text into Chrome it adds a space on either side of the string (first screenshot). When pasting in any other browser (Safari or Firefox) it behaves fine (second screenshot). It seems that Chrome is using the HTML text and the others are using plain text. Is there a way to get Chrome to trim the string on paste? Tried using handlePastedText but I’m not sure how to update the state on firing. Chrome:screen shot 2016-05-24 at 1 03 05 pm Safari: screen shot 2016-05-24 at 1 04 47 pm

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:21 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
aemcommented, May 25, 2016

not sure why this would happen, but this code should work to trim the trailing whitespace assuming your change handler is called onChange:

  handlePastedText(text) {
    const {editorState} = this.state;
    const blockMap = ContentState.createFromText(text.trim()).blockMap;
    const newState = Modifier.replaceWithFragment(editorState.getCurrentContent(), editorState.getSelection(), blockMap);
    this.onChange(EditorState.push(editorState, newState, 'insert-fragment'));
    return true;
  },
5reactions
aemcommented, Dec 22, 2016

ES6 class members aren’t auto-bound like they are in React.createClass, implementing members as arrow functions solves this problem:

class TestEditor extends React.Component {
  constructor(props) {
    super(props);
    const initialContent = ContentState.createFromText(props.inititalText);
    const editorState = EditorState.createWithContent(initialContent);
    this.state = {editorState};
  }

  onChange = (editorState) => this.setState({editorState});

  handlePastedText = (text) => {
    const {editorState} = this.state;
    const blockMap = ContentState.createFromText(stripNewLines(text)).blockMap;
    const newState = Modifier.replaceWithFragment(editorState.getCurrentContent(), editorState.getSelection(), blockMap);
    this.onChange(EditorState.push(editorState, newState, 'insert-fragment'));
    return true;
  };

  render() {
    return (
      <Editor editorState={this.state.editorState} onChange={this.onChange}
              handlePastedText={this.handlePastedText} />
    );
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Firefox copy feature always adds extra space before or after ...
The problem is that when I paste, Firefox pastes improperly formatted rich text. My work around is to paste into text editor and...
Read more >
Extra space appended after pasting a value when it ends in a ...
Opera appends two spaces in both the address-bar as input fields. Safari adds one space in both, Chrome adds no spaces in the...
Read more >
Copy-Pasted Text Has Random Extra Spaces...? : r/firefox
If you are copying from a browser page, those may well be non-breaking spaces (Unicode U+00A0; UTF-8 0xA0). Copy your text into a...
Read more >
New features available with macOS Ventura. - Apple
Brings together all the information you're looking for in one rich result. ... other apps and windows are arranged on the left side...
Read more >
Is there a way to copy text from a web page without its ... - Quora
You can copy text from right click disable page. It's very simple and easy. #1. Use Mozilla Firefox or Google Chrome Browser. #2....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found