Skip to content

Commit

Permalink
global history
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Jul 23, 2012
1 parent af79f2e commit bc35ca5
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 87 deletions.
9 changes: 8 additions & 1 deletion Makefile
@@ -1,3 +1,10 @@
FILES = chrome/ \
locale/ \
modules/ \
bootstrap.js \
chrome.manifest \
install.rdf

all:
rm -f jsterm.xpi && zip -r jsterm.xpi bootstrap.js chrome/ locale/ chrome.manifest install.rdf
rm -f jsterm.xpi && zip -r jsterm.xpi $(FILES)
wget --post-file=$(PWD)/jsterm.xpi http://localhost:8888/
89 changes: 88 additions & 1 deletion bootstrap.js
Expand Up @@ -3,6 +3,7 @@ const Cc = Components.classes;
const Ci = Components.interfaces;

Cu.import("resource://gre/modules/Services.jsm");
//Cu.import("resource://jsterm/modules/JSTermManager.jsm");

let trackedWindows;
let wObserver;
Expand Down Expand Up @@ -71,7 +72,7 @@ function shutdown() {
function install() {}
function uninstall() {}

/* ----------- */
/* ***** resource **** */

let JSTermManager = {
_map: new WeakMap(),
Expand Down Expand Up @@ -254,6 +255,7 @@ JSTerm.prototype = {

termWindow.onload = function() {
termWindow.JSTermUI.init(JSTermManager,
JSTermGlobalHistory,
this.browser,
this.browser.contentWindow,
this.chromeWin,
Expand Down Expand Up @@ -289,3 +291,88 @@ JSTerm.prototype = {
this.chromeWin = null;
}
}


let JSTermGlobalHistory = {
_limit: 5,
_entries: [],

_cut: function() {
let newStart = this._entries.length - this._limit;
if (newStart <= 0) return;

this._entries = this._entries.slice(newStart);

for (let cursor of this._cursors) {
if (cursor) {
cursor.idx -= newStart;
cursor.origin -= newStart;
}
}
},
add: function(aEntry) {
if (!aEntry) {
return;
}
if (this._entries.length) {
let lastEntry = this._entries[this._entries.length - 1];
if (lastEntry == aEntry)
return;
}
this._entries.push(aEntry);

if (this._entries.length > this._limit) {
this._cut();
}
},
initFromPref: function() {
},
saveToPref: function() {
},


_cursors: [],
getCursor: function(aInitialValue) {
let cursor = {idx: this._entries.length,
origin: this._entries.length,
initialEntry: aInitialValue};
this._cursors.push(cursor);
return cursor;
},
releaseCursor: function(cursor) {
this._cursors[cursor.idx] = null;
},
getEntryForCursor: function(cursor) {
if (cursor.idx < 0) {
return "";
} else if (cursor.idx < cursor.origin) {
return this._entries[cursor.idx];
} else {
return cursor.initialEntry;
}
},
canGoBack: function(cursor) {
return (cursor.idx > 0)
},
canGoForward: function(cursor) {
return (cursor.idx < cursor.origin);
},
goBack: function(cursor) {
if (this.canGoBack(cursor)) {
cursor.idx--;
return true;
} else {
return false;
}
},
goForward: function(cursor) {
if (this.canGoForward(cursor)) {
cursor.idx++;
return true;
} else {
return false;
}
},
}


1 change: 1 addition & 0 deletions chrome.manifest
@@ -1,2 +1,3 @@
content jsterm chrome/
locale jsterm en-US locale/en-US/
resource jsterm modules/
156 changes: 71 additions & 85 deletions chrome/jsterm.js
Expand Up @@ -5,15 +5,15 @@ Cu.import("resource:///modules/WebConsoleUtils.jsm");

/**
* Todo
* . ctrl-w & keybinding doesn't work in window mode
* . using the close button doesn't uncheck the button
* . keybindings for linux & windows
* . print() is slow
* . undock
* . save history and share it
* . Use jsm's
* . make width/height persistent
* . delete listeners & map
* . underline the current autocompletion item
* . :connectToCurrentTab
* . :connect (remote protocole)
*/

const JSTERM_MARK = "orion.annotation.jstermobject";
Expand Down Expand Up @@ -69,7 +69,7 @@ let JSTermUI = {
this.input.focus();
},

init: function(aManager, aBrowser, aContent, aChrome, aDefaultContent) {
init: function(aManager, aGlobalHistory, aBrowser, aContent, aChrome, aDefaultContent) {
this.manager = aManager;
this.browser = aBrowser;
this.content = aContent;
Expand All @@ -87,13 +87,13 @@ let JSTermUI = {
if (aDefaultContent) {
defaultInputText = aDefaultContent.input;
defaultOutputText = aDefaultContent.output;
this.history.init(aDefaultContent.history);
} else {
defaultInputText = "";
defaultOutputText = "// type ':help' for help\n// Report bug here: https://github.com/paulrouget/firefox-jsterm";
this.history.init();
}

this.history = new JSTermLocalHistory(aGlobalHistory);

let outputContainer = document.querySelector("#output-container");
this.inputContainer = document.querySelector("#input-container");
this.output.init(outputContainer, {
Expand Down Expand Up @@ -159,7 +159,6 @@ let JSTermUI = {
this.ensureInputIsAlwaysVisible(this.input);
this.input.editorElement.addEventListener("keydown", this.handleKeys, true);


this.input.addEventListener(SourceEditor.EVENTS.TEXT_CHANGED, function() {
this.multiline = this.isMultiline(this.input.getText());
}.bind(this));
Expand Down Expand Up @@ -188,86 +187,16 @@ let JSTermUI = {
e.editorElement.style.height = (height) + "px";
},

history: {
_entries: [],
cursor: 0,
browsing: false,
init: function(entries) {
JSTermUI.input.addEventListener(SourceEditor.EVENTS.SELECTION, function() {
this.browsing = false;
}.bind(this));
if (entries) {
this._entries = entries;
}
},
copy: function() {
return this._entries.concat();
},
add: function(entry) {
if (!entry) return;
if (this._entries.length) {
let lastEntry = this._entries[this._entries.length - 1];
if (lastEntry == entry)
return;
}
this._entries.push(entry);
},

startBrowsing: function(originalText) {
this.originalText = originalText;
this.browsing = true;
this.cursor = this._entries.length;
},
goBack: function() {
if (this.canGoBack()) {
this.cursor--;
let entry = this.getEntryAtIndex(this.cursor);
JSTermUI.input.setText(entry);
JSTermUI.input.setCaretPosition(JSTermUI.input.getLineCount(), 1000);
if (JSTermUI.isMultiline(entry)) {
JSTermUI.multiline = true;
} else {
JSTermUI.multiline = false;
}
this.browsing = true;
}
},
goForward: function() {
if (this.canGoForward()) {
this.cursor++;
let entry = this.getEntryAtIndex(this.cursor);
JSTermUI.input.setText(entry);
JSTermUI.input.setCaretPosition(JSTermUI.input.getLineCount(), 1000);
if (JSTermUI.isMultiline(entry)) {
JSTermUI.multiline = true;
} else {
JSTermUI.multiline = false;
}
this.browsing = true;
}
},
canGoBack: function() {
return this.browsing && (this.cursor > 0);
},
canGoForward: function() {
return this.browsing && (this.cursor < this._entries.length);
},
getEntryAtIndex: function(idx) {
if (idx == this._entries.length) {
return this.originalText;
}
return this._entries[idx];
},
},

ensureInputIsAlwaysVisible: function(editor) {
editor.addEventListener(SourceEditor.EVENTS.TEXT_CHANGED, function() {
this.container.scrollTop = this.container.scrollTopMax;
}.bind(this));
},

newEntry: function(code) {
this.history.stopBrowsing();
this.history.add(code);

this.input.setText("");
this.multiline = false;
this.printedSomething = false;
Expand Down Expand Up @@ -397,6 +326,10 @@ let JSTermUI = {
handleKeys: function(e) {
let code = this.input.getText();

if (e.keyCode != 38 && e.keyCode != 40) {
this.history.stopBrowsing();
}

if (e.keyCode == 13 && e.shiftKey) {
if (this.multiline) {
e.stopPropagation();
Expand Down Expand Up @@ -429,21 +362,27 @@ let JSTermUI = {
}

if (e.keyCode == 38) {
if (!this.history.browsing && this.multiline) {
if (!this.history.isBrowsing() && this.multiline) {
return;
}
e.stopPropagation();
e.preventDefault();
if (!this.history.browsing) {
if (!this.history.isBrowsing() ) {
this.history.startBrowsing(this.input.getText());
}
this.history.goBack();
let entry = this.history.goBack();
if (entry) {
JSTermUI.input.setText(entry);
JSTermUI.input.setCaretPosition(JSTermUI.input.getLineCount(), 1000);
}
}
if (e.keyCode == 40) {
if (this.history.browsing) {
if (this.history.isBrowsing()) {
e.stopPropagation();
e.preventDefault();
this.history.goForward();
let entry = this.history.goForward();
JSTermUI.input.setText(entry);
JSTermUI.input.setCaretPosition(JSTermUI.input.getLineCount(), 1000);
}
}
},
Expand Down Expand Up @@ -501,7 +440,6 @@ let JSTermUI = {
return {
input: this.input.getText(),
output: this.output.getText(),
history: this.history.copy(),
};
},

Expand Down Expand Up @@ -999,3 +937,51 @@ PropertyTreeView2.prototype = {
this._treeBox.rowCountChanged(0, this._rows.length);
},
};

/** HISTORY **/

function JSTermLocalHistory(aGlobalHistory) {
this.global = aGlobalHistory;
}
JSTermLocalHistory.prototype = {
_browsing: false,
isBrowsing: function() {
return this._browsing;
},
startBrowsing: function(aInitialValue) {
this._browsing = true;
this.cursor = this.global.getCursor(aInitialValue);
},
stopBrowsing: function() {
if (this.isBrowsing()) {
this._browsing = false;
this.global.releaseCursor(this.cursor);
this.cursor = null;
}
},
add: function(entry) {
this.global.add(entry);
},
canGoBack: function() {
return this.isBrowsing() && this.global.canGoBack(this.cursor);
},
canGoForward: function() {
return this.isBrowsing() && this.global.canGoForward(this.cursor);
},
goBack: function() {
if (this.canGoBack()) {
this.global.goBack(this.cursor);
let entry = this.global.getEntryForCursor(this.cursor);
return entry;
}
return null;
},
goForward: function() {
if (this.canGoForward()) {
this.global.goForward(this.cursor);
let entry = this.global.getEntryForCursor(this.cursor);
return entry;
}
return null;
},
}
Binary file modified jsterm.xpi
Binary file not shown.

0 comments on commit bc35ca5

Please sign in to comment.