bevacqua / woofmark

:dog2: Barking up the DOM tree. A modular, progressive, and beautiful Markdown and HTML editor
https://bevacqua.github.io/woofmark
MIT License
1.62k stars 74 forks source link

trim leading/trailing <br> from pasted content #34

Open jywarren opened 7 years ago

jywarren commented 7 years ago

Following up from https://github.com/bevacqua/domador/issues/7#issuecomment-236286556 -- related to inserted <br> when pasting content copied from a webpage (even a single word) in at least Chrome.

jywarren commented 7 years ago

Looking here: http://stackoverflow.com/questions/6035071/intercept-paste-event-in-javascript

And attempting to insert resulting intercepted text at the current insertion point here: https://github.com/bevacqua/woofmark/blob/master/src/InputHistory.js#L183

jywarren commented 7 years ago

Worked on this a bit but got stuck; I modified handlePaste() in InputHistory.js like this:

function handlePaste (state, e) {
  if (state.inputState && state.inputState.text !== state.surface.read(state.inputMode) && state.refreshing === null) {
    var pastedText;
    if (window.clipboardData && window.clipboardData.getData) { // IE
      pastedText = window.clipboardData.getData('Text');
    } else if (e.clipboardData && e.clipboardData.getData) {
      pastedText = e.clipboardData.getData('text/plain');
    }
    var chunks = state.inputState.getChunks();
    pastedText = pastedText.replace(/(\A<br\s?\/?\w?>)|(<br\s?\/?\w?>\z)/g, '');
    chunks.before += pastedText;
    state.inputState.setChunks(chunks);
    state.historyMode = 'paste';
    state.saveState();
    state.refreshState();
  }
  return false;
}

I successfully intercept the paste, but modifying chunks is not working; perhaps modifying state.inputState.getChunks() is not right.

I'll circle back to this later.