nytimes / ice

track changes with javascript
Other
1.71k stars 194 forks source link

Paste moves cursor before instead of after pasted content #99

Closed jamienk closed 10 years ago

jamienk commented 10 years ago

I see it's working on the demo site, but for some reason (too many other libraries to easily distill into a simple test case) when I paste with the IceCopyPastePlugin active, it puts my cursor before rather than after the pasted content.

I figured out that the call to this._ice.element.focus() (which refocuses into the contentEditable area) was somehow misplacing the cursor.

I modified the _cleanPaste function so that the refocusing back to ice element comes before the range it set to the end, and that seems to solve the problem.

This:

      // Set focus back to ice element.      
      if(this._ice.env.frame) {
        this._ice.env.frame.contentWindow.focus();
      } else {
        this._ice.element.focus();
      }

      moveTo = moveTo && moveTo.lastChild || moveTo || this._tmpNode;
      // Move the range to the end of moveTo so that the cursor will be at the end of the paste.
      var range = this._ice.getCurrentRange();
      range.setStartAfter(moveTo);
      range.collapse(true);
      this._ice.selection.addRange(range);

instead of this:

      moveTo = moveTo && moveTo.lastChild || moveTo || this._tmpNode;
      // Move the range to the end of moveTo so that the cursor will be at the end of the paste.
      var range = this._ice.getCurrentRange();
      range.setStartAfter(moveTo);
      range.collapse(true);
      this._ice.selection.addRange(range);

      // Set focus back to ice element. 
      if(this._ice.env.frame) {
        this._ice.env.frame.contentWindow.focus();
      } else {
        this._ice.element.focus();
      }