1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 18:37:58 +01:00

Closes issue #680.

This commit is contained in:
Kris Maglione
2011-10-05 17:12:43 -04:00
parent 942aa9214a
commit f0028647c3
2 changed files with 43 additions and 37 deletions

View File

@@ -26,6 +26,9 @@ var Editor = Module("editor", {
}); });
}, },
get isCaret() modes.getStack(1).main == modes.CARET,
get isTextEdit() modes.getStack(1).main == modes.TEXT_EDIT,
get editor() DOM(this.element).editor, get editor() DOM(this.element).editor,
getController: function getController(cmd) { getController: function getController(cmd) {
@@ -38,24 +41,24 @@ var Editor = Module("editor", {
get selection() this.editor && this.editor.selection || null, get selection() this.editor && this.editor.selection || null,
get selectionController() this.editor && this.editor.selectionController || null, get selectionController() this.editor && this.editor.selectionController || null,
get isCaret() modes.getStack(1).main == modes.CARET, deselect: function () {
get isTextEdit() modes.getStack(1).main == modes.TEXT_EDIT,
deselectText: function () {
if (this.selection && this.selection.focusNode) if (this.selection && this.selection.focusNode)
this.selection.collapse(this.selection.focusNode, this.selection.collapse(this.selection.focusNode,
this.selection.focusOffset); this.selection.focusOffset);
}, },
get selectionRange() { get selectedRange() {
if (!this.selection)
return null;
if (!this.selection.rangeCount) { if (!this.selection.rangeCount) {
let range = RangeFind.nodeContents(this.editor.rootElement.ownerDocument); let range = RangeFind.nodeContents(this.editor.rootElement.ownerDocument);
range.collapse(true); range.collapse(true);
this.selectionRange = range; this.selectedRange = range;
} }
return this.selection.getRangeAt(0); return this.selection.getRangeAt(0);
}, },
set selectionRange(range) { set selectedRange(range) {
this.selection.removeAllRanges(); this.selection.removeAllRanges();
if (range != null) if (range != null)
this.selection.addRange(range); this.selection.addRange(range);
@@ -90,16 +93,18 @@ var Editor = Module("editor", {
if (count == null) if (count == null)
count = 1; count = 1;
if (!callable(cmd))
cmd = bind("doCommand", controller, cmd);
let didCommand = false; let didCommand = false;
while (count--) { while (count--) {
// some commands need this try/catch workaround, because a cmd_charPrevious triggered // some commands need this try/catch workaround, because a cmd_charPrevious triggered
// at the beginning of the textarea, would hang the doCommand() // at the beginning of the textarea, would hang the doCommand()
// good thing is, we need this code anyway for proper beeping // good thing is, we need this code anyway for proper beeping
// What huh? --Kris
try { try {
if (callable(cmd))
cmd(this.editor, controller); cmd(this.editor, controller);
else
controller.doCommand(cmd);
didCommand = true; didCommand = true;
} }
catch (e) { catch (e) {
@@ -152,7 +157,7 @@ var Editor = Module("editor", {
continue; continue;
if (editor instanceof Ci.nsIPlaintextEditor) { if (editor instanceof Ci.nsIPlaintextEditor) {
this.selectionRange = RangeFind.nodeContents(node); this.selectedRange = RangeFind.nodeContents(node);
editor.insertText(text); editor.insertText(text);
} }
else else
@@ -172,10 +177,10 @@ var Editor = Module("editor", {
// Grab the charcode of the key spec. Using the key name // Grab the charcode of the key spec. Using the key name
// directly will break keys like < // directly will break keys like <
let code = DOM.Event.parse(key)[0].charCode; let code = DOM.Event.parse(key)[0].charCode;
util.assert(code);
let char = String.fromCharCode(code); let char = String.fromCharCode(code);
util.assert(code);
let range = this.selectionRange.cloneRange(); let range = this.selectedRange.cloneRange();
let collapse = DOM(this.element).whiteSpace == "normal"; let collapse = DOM(this.element).whiteSpace == "normal";
// Find the *count*th occurance of *char* before a non-collapsed // Find the *count*th occurance of *char* before a non-collapsed
@@ -345,9 +350,10 @@ var Editor = Module("editor", {
* @see Abbreviation#expand * @see Abbreviation#expand
*/ */
expandAbbreviation: function (mode) { expandAbbreviation: function (mode) {
let elem = this.element; if (!this.selection)
return;
let range = this.selectionRange.cloneRange(); let range = this.selectedRange.cloneRange();
if (!range.collapsed) if (!range.collapsed)
return; return;
@@ -355,7 +361,7 @@ var Editor = Module("editor", {
let abbrev = abbreviations.match(mode, String(range)); let abbrev = abbreviations.match(mode, String(range));
if (abbrev) { if (abbrev) {
range.setStart(range.startContainer, range.endOffset - abbrev.lhs.length); range.setStart(range.startContainer, range.endOffset - abbrev.lhs.length);
this.mungeRange(range, function () abbrev.expand(elem), true); this.mungeRange(range, function () abbrev.expand(this.element), true);
} }
}, },
}, { }, {
@@ -427,11 +433,8 @@ var Editor = Module("editor", {
let node; let node;
while (node = this[meth]()) while (node = this[meth]())
if (node instanceof Ci.nsIDOMText && if (node instanceof Ci.nsIDOMText &&
let ({ style } = DOM(node)) DOM(node).isVisible &&
style.MozUserSelect != "none" && DOM(node).style.MozUserSelect != "none")
style.visibility != "hidden" &&
style.visibility != "collapse" &&
style.display != "none")
return node; return node;
} }
}), }),
@@ -539,7 +542,7 @@ var Editor = Module("editor", {
selection.collapseToStart(); selection.collapseToStart();
} }
else if (stack.pop) else if (stack.pop)
editor.deselectText(); editor.deselect();
} }
}); });
modes.addMode("TEXT_EDIT", { modes.addMode("TEXT_EDIT", {
@@ -764,7 +767,7 @@ var Editor = Module("editor", {
mappings.add([modes.TEXT_EDIT], key, mappings.add([modes.TEXT_EDIT], key,
desc, desc,
function ({ command, count, motion }) { function ({ command, count, motion }) {
let start = editor.selectionRange.cloneRange(); let start = editor.selectedRange.cloneRange();
modes.push(modes.OPERATOR, null, { modes.push(modes.OPERATOR, null, {
forCommand: command, forCommand: command,
@@ -778,8 +781,8 @@ var Editor = Module("editor", {
editor.withSavedValues(["inEditMap"], function () { editor.withSavedValues(["inEditMap"], function () {
this.inEditMap = true; this.inEditMap = true;
let range = RangeFind.union(start, editor.selectionRange); let range = RangeFind.union(start, editor.selectedRange);
editor.selectionRange = select ? range : start; editor.selectedRange = select ? range : start;
doTxn(range, editor); doTxn(range, editor);
}); });
@@ -796,7 +799,7 @@ var Editor = Module("editor", {
desc, desc,
function ({ count, motion }) { function ({ count, motion }) {
dactyl.assert(editor.isTextEdit); dactyl.assert(editor.isTextEdit);
doTxn(editor.selectionRange, editor); doTxn(editor.selectedRange, editor);
}, },
{ count: true, type: "motion" }); { count: true, type: "motion" });
} }
@@ -821,7 +824,7 @@ var Editor = Module("editor", {
dactyl.assert(command == modes.getStack(0).params.forCommand); dactyl.assert(command == modes.getStack(0).params.forCommand);
editor.executeCommand("cmd_beginLine", 1); editor.executeCommand("cmd_beginLine", 1);
editor.executeCommand("cmd_selectLineNext", count || 1); editor.executeCommand("cmd_selectLineNext", count || 1);
let range = editor.selectionRange; let range = editor.selectedRange;
if (command == "c" && !range.collapsed) // Hack. if (command == "c" && !range.collapsed) // Hack.
if (range.endContainer instanceof Text && if (range.endContainer instanceof Text &&
range.endContainer.textContent[range.endOffset - 1] == "\n") range.endContainer.textContent[range.endOffset - 1] == "\n")
@@ -921,7 +924,7 @@ var Editor = Module("editor", {
["u"], "Undo changes", ["u"], "Undo changes",
function (args) { function (args) {
editor.executeCommand("cmd_undo", Math.max(args.count, 1)); editor.executeCommand("cmd_undo", Math.max(args.count, 1));
editor.deselectText(); editor.deselect();
}, },
{ count: true }); { count: true });
@@ -929,7 +932,7 @@ var Editor = Module("editor", {
["<C-r>"], "Redo undone changes", ["<C-r>"], "Redo undone changes",
function (args) { function (args) {
editor.executeCommand("cmd_redo", Math.max(args.count, 1)); editor.executeCommand("cmd_redo", Math.max(args.count, 1));
editor.deselectText(); editor.deselect();
}, },
{ count: true }); { count: true });
@@ -1063,7 +1066,7 @@ var Editor = Module("editor", {
return c == lc ? c.toLocaleUpperCase() : lc; return c == lc ? c.toLocaleUpperCase() : lc;
}); });
var range = editor.selectionRange; var range = editor.selectedRange;
if (range.collapsed) { if (range.collapsed) {
count = count || 1; count = count || 1;
util.dump(count, range); util.dump(count, range);

View File

@@ -403,16 +403,19 @@ var DOM = Class("DOM", {
this[0] instanceof Ci.nsIDOMNSEditableElement; this[0] instanceof Ci.nsIDOMNSEditableElement;
if (this[0].editor instanceof Ci.nsIEditor) if (this[0].editor instanceof Ci.nsIEditor)
return this[0].editor; var editor = this[0].editor;
try { try {
return this[0].QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation) if (!editor)
editor = this[0].QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIEditingSession) .QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIEditingSession)
.getEditorForWindow(this[0]); .getEditorForWindow(this[0]);
} }
catch (e) {} catch (e) {}
return null; editor instanceof Ci.nsIPlaintextEditor;
editor instanceof Ci.nsIHTMLEditor;
return editor;
}, },
get isEditable() !!this.editor, get isEditable() !!this.editor,