w8tcha / CKEditor-TextSelection-Plugin

The plugin that makes the editor keep it's text-selection when switching between WYSIWYG and Source mode, and scrolls the selection into the viewport.
MIT License
10 stars 7 forks source link

scroll caret / selection into view #21

Closed sebhaase closed 9 years ago

sebhaase commented 9 years ago

It should be easy to force a scroll into view on the caret / selection position. Where would I have to add that function ?

sebhaase commented 9 years ago

This seems to work: http://roysharon.com/blog/37

function scrollIntoView(t) {
   if (typeof(t) != 'object') return;

   if (t.getRangeAt) {
      // we have a Selection object
      if (t.rangeCount == 0) return;
      t = t.getRangeAt(0);
   }

   if (t.cloneRange) {
      // we have a Range object
      var r = t.cloneRange();   // do not modify the source range
      r.collapse(true);     // collapse to start
      var t = r.startContainer;
      // if start is an element, then startOffset is the child number
      // in which the range starts
      if (t.nodeType == 1) t = t.childNodes[r.startOffset];
   }

   // if t is not an element node, then we need to skip back until we find the
   // previous element with which we can call scrollIntoView()
   o = t;
   while (o && o.nodeType != 1) o = o.previousSibling;
   t = o || t.parentNode;
   if (t) t.scrollIntoView();
}

// call  scrollIntoView(window.getSelection()); 

ref. http://roysharon.com/blog/wp-content/uploads/2010/12/scrollIntoView.js

w8tcha commented 9 years ago

It should be easy to force a scroll into view on the caret / selection position. Where would I have to add that function ?

The plugin already has that function to scroll to the selected postion.

sebhaase commented 9 years ago

For me it scrolls only when going from source-mode to wysiwyg mode - but not when switching into the source-code editor mode.

w8tcha commented 9 years ago

Yes this is a known issue, which wont be changed. You could use the better CodeMirror Plugin which replaces the default mode