Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Save selection and scroll position #109

Closed
wants to merge 5 commits into from

2 participants

@Infocatcher

Seems to works, but really this is just "proof of concept".
And IE doesn't supported. :)
And I test only in Firefox 11.0.

So code should be improved and tested.

@Infocatcher
Oops, wrongly copypasted...

*Fixed

@Infocatcher
And I test only in Firefox 11.0.

Works in IE 9 (!) and Chrome 17 too.
Has strange bugs in Opera 11.62.

@einars
Owner

Closing as old and inactive, sorry

@einars einars closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 29, 2012
  1. @Infocatcher
  2. @Infocatcher

    Update index.html

    Infocatcher authored
  3. @Infocatcher

    Update index.html

    Infocatcher authored
Commits on Apr 3, 2012
  1. @Infocatcher
  2. @Infocatcher

    Replace tabs with spaces

    Infocatcher authored
This page is out of date. Refresh to see the latest.
Showing with 111 additions and 3 deletions.
  1. +111 −3 index.html
View
114 index.html
@@ -329,10 +329,15 @@
space_before_conditional: space_before_conditional,
indent_scripts:indent_scripts};
+ var sourceNode = document.getElementById("source"); //~ todo: use jQuery?
+ if(typeof sourceNode.selectionStart == "number") //~ todo: add IE support
+ var selectionStorage = new SelectionStorage(source, sourceNode.selectionStart, sourceNode.selectionEnd);
+ var sTop = sourceNode.scrollTop/sourceNode.scrollHeight;
+ var sLeft = sourceNode.scrollLeft/sourceNode.scrollWidth;
+
if (looks_like_html(source)) {
- $('#source').val(
- style_html(source, opts)
- );
+ var v = style_html(source, opts);
+ $('#source').val(v);
} else {
if ($('#detect-packers').attr('checked')) {
source = unpacker_filter(source);
@@ -341,9 +346,112 @@
$('#source').val(v);
}
+ if(selectionStorage) {
+ var sel = selectionStorage.getSelection(v);
+ if(sel) {
+ sourceNode.selectionStart = sel.selectionStart;
+ sourceNode.selectionEnd = sel.selectionEnd;
+ //~ todo: add "real" scroll into view
+ }
+ }
+ //sourceNode.scrollTop = sourceNode.scrollHeight*sTop;
+ //sourceNode.scrollLeft = sourceNode.scrollWidth*sLeft;
+ sTop = Math.round(sourceNode.scrollHeight*sTop);
+ sLeft = Math.round(sourceNode.scrollWidth*sLeft);
+ function restoreScroll() {
+ sourceNode.scrollTop = sTop;
+ sourceNode.scrollLeft = sLeft;
+ }
+ function ensureScrolled() {
+ if(
+ sourceNode.scrollTop == sTop && sourceNode.scrollLeft == sLeft
+ || new Date().getTime() > stopTime
+ )
+ return;
+ restoreScroll();
+ setTimeout(ensureScrolled, 5);
+ }
+
+ stopTime = new Date().getTime() + 300;
+ restoreScroll();
+ setTimeout(ensureScrolled, 5); // Firefox >= 4 scrolls to the end of selection
+
the.beautify_in_progress = false;
}
+function SelectionStorage(str, selStart, selEnd) {
+ this.specialSelection = this.SELECT_UNKNOWN;
+ if(selEnd == str.length) {
+ if(selStart == 0) {
+ this.specialSelection = this.SELECT_ALL;
+ return;
+ }
+ if(selStart == selEnd) {
+ this.specialSelection = this.CARET_AT_END;
+ return;
+ }
+ this.specialSelection = this.SELECT_END;
+ }
+ this.start = this.prepare(str.substring(0, selStart));
+ if(this.specialSelection != this.SELECT_END)
+ this.selected = this.prepare(str.substring(selStart, selEnd));
+}
+SelectionStorage.prototype = {
+ SELECT_UNKNOWN: 0,
+ SELECT_ALL: 1,
+ CARET_AT_END: 2,
+ SELECT_END: 3,
+ prepare: function(s) {
+ return s.replace(/\s+/g, "");
+ },
+ getSelection: function(newStr) {
+ if(this.specialSelection == this.SELECT_ALL)
+ return { selectionStart: 0, selectionEnd: newStr.length };
+ if(this.specialSelection == this.CARET_AT_END) {
+ var l = newStr.length;
+ return { selectionStart: l, selectionEnd: l };
+ }
+
+ var selStart, selEnd;
+ var old = this.start;
+ if(!old) {
+ selStart = 0;
+ old = this.selected;
+ if(!old)
+ return { selectionStart: 0, selectionEnd: 0 };
+ }
+ var pos = 0;
+ var posStop = old.length;
+ for(var i = 0, l = newStr.length; i < l; ++i) {
+ var chr = newStr.charAt(i);
+ if(/^\s$/.test(chr)) {
+ if(selStart && skipSpaces)
+ ++selStart;
+ continue;
+ }
+ skipSpaces = false;
+ var oldChr = old.charAt(pos++);
+
+ if(chr != oldChr)
+ return null;
+ if(pos == posStop) {
+ if(selStart != undefined)
+ return { selectionStart: selStart, selectionEnd: i + 1 };
+ if(this.specialSelection == this.SELECT_END)
+ return { selectionStart: i + 1, selectionEnd: l };
+ selStart = i + 1;
+ old = this.selected;
+ if(!old)
+ return { selectionStart: selStart, selectionEnd: selStart };
+ skipSpaces = true;
+ pos = 0;
+ posStop = old.length;
+ }
+ }
+ return null;
+ }
+};
+
function looks_like_html(source)
{
// <foo> - looks like html
Something went wrong with that request. Please try again.