move readOnly checks into commandManager

This commit is contained in:
nightwing 2011-11-19 21:10:34 +04:00
commit d926ff88a9
3 changed files with 193 additions and 204 deletions

View file

@ -99,7 +99,10 @@ var CommandManager = function(platform, commands) {
if (!command)
return false;
if (editor.$readOnly && !command.readOnly)
return false;
command.exec(editor, args || {});
return true;
};

View file

@ -52,11 +52,13 @@ function bindKey(win, mac) {
exports.commands = [{
name: "selectall",
bindKey: bindKey("Ctrl-A", "Command-A"),
exec: function(editor) { editor.selectAll(); }
exec: function(editor) { editor.selectAll(); },
readOnly: true
}, {
name: "removeline",
bindKey: bindKey("Ctrl-D", "Command-D"),
exec: function(editor) { editor.removeLines(); }
name: "centerselection",
bindKey: bindKey("Ctrl-L", "Ctrl-L"),
exec: function(editor) { editor.centerSelection(); },
readOnly: true
}, {
name: "gotoline",
bindKey: bindKey("Ctrl-G", "Command-L"),
@ -65,26 +67,202 @@ exports.commands = [{
if (!isNaN(line)) {
editor.gotoLine(line);
}
}
},
readOnly: true
}, {
name: "togglecomment",
bindKey: bindKey("Ctrl-7", "Command-7"),
exec: function(editor) { editor.toggleCommentLines(); }
name: "fold",
bindKey: bindKey("Alt-L", "Alt-L"),
exec: function(editor) { editor.session.toggleFold(false); },
readOnly: true
}, {
name: "unfold",
bindKey: bindKey("Alt-Shift-L", "Alt-Shift-L"),
exec: function(editor) { editor.session.toggleFold(true); },
readOnly: true
}, {
name: "foldall",
bindKey: bindKey("Alt-0", "Alt-0"),
exec: function(editor) { editor.session.foldAll(); },
readOnly: true
}, {
name: "unfoldall",
bindKey: bindKey("Alt-Shift-0", "Alt-Shift-0"),
exec: function(editor) { editor.session.unfold(); },
readOnly: true
}, {
name: "findnext",
bindKey: bindKey("Ctrl-R", "Command-G"),
exec: function(editor) { editor.findNext(); }
exec: function(editor) { editor.findNext(); },
readOnly: true
}, {
name: "findprevious",
bindKey: bindKey("Ctrl-Shift-R", "Command-Shift-G"),
exec: function(editor) { editor.findPrevious(); }
exec: function(editor) { editor.findPrevious(); },
readOnly: true
}, {
name: "find",
bindKey: bindKey("Ctrl-F", "Command-F"),
exec: function(editor) {
var needle = prompt("Find:", editor.getCopyText());
editor.find(needle);
}
},
readOnly: true
}, {
name: "overwrite",
bindKey: bindKey("Insert", "Insert"),
exec: function(editor) { editor.toggleOverwrite(); },
readOnly: true
}, {
name: "selecttostart",
bindKey: bindKey("Ctrl-Shift-Home|Alt-Shift-Up", "Command-Shift-Up"),
exec: function(editor) { editor.getSelection().selectFileStart(); },
readOnly: true
}, {
name: "gotostart",
bindKey: bindKey("Ctrl-Home|Ctrl-Up", "Command-Home|Command-Up"),
exec: function(editor) { editor.navigateFileStart(); },
readOnly: true
}, {
name: "selectup",
bindKey: bindKey("Shift-Up", "Shift-Up"),
exec: function(editor) { editor.getSelection().selectUp(); },
readOnly: true
}, {
name: "golineup",
bindKey: bindKey("Up", "Up|Ctrl-P"),
exec: function(editor, args) { editor.navigateUp(args.times); },
readOnly: true
}, {
name: "selecttoend",
bindKey: bindKey("Ctrl-Shift-End|Alt-Shift-Down", "Command-Shift-Down"),
exec: function(editor) { editor.getSelection().selectFileEnd(); },
readOnly: true
}, {
name: "gotoend",
bindKey: bindKey("Ctrl-End|Ctrl-Down", "Command-End|Command-Down"),
exec: function(editor) { editor.navigateFileEnd(); },
readOnly: true
}, {
name: "selectdown",
bindKey: bindKey("Shift-Down", "Shift-Down"),
exec: function(editor) { editor.getSelection().selectDown(); },
readOnly: true
}, {
name: "golinedown",
bindKey: bindKey("Down", "Down|Ctrl-N"),
exec: function(editor, args) { editor.navigateDown(args.times); },
readOnly: true
}, {
name: "selectwordleft",
bindKey: bindKey("Ctrl-Shift-Left", "Option-Shift-Left"),
exec: function(editor) { editor.getSelection().selectWordLeft(); },
readOnly: true
}, {
name: "gotowordleft",
bindKey: bindKey("Ctrl-Left", "Option-Left"),
exec: function(editor) { editor.navigateWordLeft(); },
readOnly: true
}, {
name: "selecttolinestart",
bindKey: bindKey("Alt-Shift-Left", "Command-Shift-Left"),
exec: function(editor) { editor.getSelection().selectLineStart(); },
readOnly: true
}, {
name: "gotolinestart",
bindKey: bindKey("Alt-Left|Home", "Command-Left|Home|Ctrl-A"),
exec: function(editor) { editor.navigateLineStart(); },
readOnly: true
}, {
name: "selectleft",
bindKey: bindKey("Shift-Left", "Shift-Left"),
exec: function(editor) { editor.getSelection().selectLeft(); },
readOnly: true
}, {
name: "gotoleft",
bindKey: bindKey("Left", "Left|Ctrl-B"),
exec: function(editor, args) { editor.navigateLeft(args.times); },
readOnly: true
}, {
name: "selectwordright",
bindKey: bindKey("Ctrl-Shift-Right", "Option-Shift-Right"),
exec: function(editor) { editor.getSelection().selectWordRight(); },
readOnly: true
}, {
name: "gotowordright",
bindKey: bindKey("Ctrl-Right", "Option-Right"),
exec: function(editor) { editor.navigateWordRight(); },
readOnly: true
}, {
name: "selecttolineend",
bindKey: bindKey("Alt-Shift-Right", "Command-Shift-Right"),
exec: function(editor) { editor.getSelection().selectLineEnd(); },
readOnly: true
}, {
name: "gotolineend",
bindKey: bindKey("Alt-Right|End", "Command-Right|End|Ctrl-E"),
exec: function(editor) { editor.navigateLineEnd(); },
readOnly: true
}, {
name: "selectright",
bindKey: bindKey("Shift-Right", "Shift-Right"),
exec: function(editor) { editor.getSelection().selectRight(); },
readOnly: true
}, {
name: "gotoright",
bindKey: bindKey("Right", "Right|Ctrl-F"),
exec: function(editor, args) { editor.navigateRight(args.times); },
readOnly: true
}, {
name: "selectpagedown",
bindKey: bindKey("Shift-PageDown", "Shift-PageDown"),
exec: function(editor) { editor.selectPageDown(); },
readOnly: true
}, {
name: "pagedown",
bindKey: bindKey(null, "PageDown"),
exec: function(editor) { editor.scrollPageDown(); },
readOnly: true
}, {
name: "gotopagedown",
bindKey: bindKey("PageDown", "Option-PageDown|Ctrl-V"),
exec: function(editor) { editor.gotoPageDown(); },
readOnly: true
}, {
name: "selectpageup",
bindKey: bindKey("Shift-PageUp", "Shift-PageUp"),
exec: function(editor) { editor.selectPageUp(); },
readOnly: true
}, {
name: "pageup",
bindKey: bindKey(null, "PageUp"),
exec: function(editor) { editor.scrollPageUp(); },
readOnly: true
}, {
name: "gotopageup",
bindKey: bindKey("PageUp", "Option-PageUp"),
exec: function(editor) { editor.gotoPageUp(); },
readOnly: true
}, {
name: "selectlinestart",
bindKey: bindKey("Shift-Home", "Shift-Home"),
exec: function(editor) { editor.getSelection().selectLineStart(); },
readOnly: true
}, {
name: "selectlineend",
bindKey: bindKey("Shift-End", "Shift-End"),
exec: function(editor) { editor.getSelection().selectLineEnd(); },
readOnly: true
},
// commands disabled in readOnly mode
{
name: "removeline",
bindKey: bindKey("Ctrl-D", "Command-D"),
exec: function(editor) { editor.removeLines(); }
}, {
name: "togglecomment",
bindKey: bindKey("Ctrl-7", "Command-7"),
exec: function(editor) { editor.toggleCommentLines(); }
}, {
name: "replace",
bindKey: bindKey("Ctrl-H", "Command-Option-F"),
@ -117,10 +295,6 @@ exports.commands = [{
name: "redo",
bindKey: bindKey("Ctrl-Shift-Z|Ctrl-Y", "Command-Shift-Z|Command-Y"),
exec: function(editor) { editor.redo(); }
}, {
name: "overwrite",
bindKey: bindKey("Insert", "Insert"),
exec: function(editor) { editor.toggleOverwrite(); }
}, {
name: "copylinesup",
bindKey: bindKey("Ctrl-Alt-Up", "Command-Option-Up"),
@ -129,22 +303,6 @@ exports.commands = [{
name: "movelinesup",
bindKey: bindKey("Alt-Up", "Option-Up"),
exec: function(editor) { editor.moveLinesUp(); }
}, {
name: "selecttostart",
bindKey: bindKey("Ctrl-Shift-Home|Alt-Shift-Up", "Command-Shift-Up"),
exec: function(editor) { editor.getSelection().selectFileStart(); }
}, {
name: "gotostart",
bindKey: bindKey("Ctrl-Home|Ctrl-Up", "Command-Home|Command-Up"),
exec: function(editor) { editor.navigateFileStart(); }
}, {
name: "selectup",
bindKey: bindKey("Shift-Up", "Shift-Up"),
exec: function(editor) { editor.getSelection().selectUp(); }
}, {
name: "golineup",
bindKey: bindKey("Up", "Up|Ctrl-P"),
exec: function(editor, args) { editor.navigateUp(args.times); }
}, {
name: "copylinesdown",
bindKey: bindKey("Ctrl-Alt-Down", "Command-Option-Down"),
@ -153,102 +311,6 @@ exports.commands = [{
name: "movelinesdown",
bindKey: bindKey("Alt-Down", "Option-Down"),
exec: function(editor) { editor.moveLinesDown(); }
}, {
name: "selecttoend",
bindKey: bindKey("Ctrl-Shift-End|Alt-Shift-Down", "Command-Shift-Down"),
exec: function(editor) { editor.getSelection().selectFileEnd(); }
}, {
name: "gotoend",
bindKey: bindKey("Ctrl-End|Ctrl-Down", "Command-End|Command-Down"),
exec: function(editor) { editor.navigateFileEnd(); }
}, {
name: "selectdown",
bindKey: bindKey("Shift-Down", "Shift-Down"),
exec: function(editor) { editor.getSelection().selectDown(); }
}, {
name: "golinedown",
bindKey: bindKey("Down", "Down|Ctrl-N"),
exec: function(editor, args) { editor.navigateDown(args.times); }
}, {
name: "selectwordleft",
bindKey: bindKey("Ctrl-Shift-Left", "Option-Shift-Left"),
exec: function(editor) { editor.getSelection().selectWordLeft(); }
}, {
name: "gotowordleft",
bindKey: bindKey("Ctrl-Left", "Option-Left"),
exec: function(editor) { editor.navigateWordLeft(); }
}, {
name: "selecttolinestart",
bindKey: bindKey("Alt-Shift-Left", "Command-Shift-Left"),
exec: function(editor) { editor.getSelection().selectLineStart(); }
}, {
name: "gotolinestart",
bindKey: bindKey("Alt-Left|Home", "Command-Left|Home|Ctrl-A"),
exec: function(editor) { editor.navigateLineStart(); }
}, {
name: "selectleft",
bindKey: bindKey("Shift-Left", "Shift-Left"),
exec: function(editor) { editor.getSelection().selectLeft(); }
}, {
name: "gotoleft",
bindKey: bindKey("Left", "Left|Ctrl-B"),
exec: function(editor, args) { editor.navigateLeft(args.times); }
}, {
name: "selectwordright",
bindKey: bindKey("Ctrl-Shift-Right", "Option-Shift-Right"),
exec: function(editor) { editor.getSelection().selectWordRight(); }
}, {
name: "gotowordright",
bindKey: bindKey("Ctrl-Right", "Option-Right"),
exec: function(editor) { editor.navigateWordRight(); }
}, {
name: "selecttolineend",
bindKey: bindKey("Alt-Shift-Right", "Command-Shift-Right"),
exec: function(editor) { editor.getSelection().selectLineEnd(); }
}, {
name: "gotolineend",
bindKey: bindKey("Alt-Right|End", "Command-Right|End|Ctrl-E"),
exec: function(editor) { editor.navigateLineEnd(); }
}, {
name: "selectright",
bindKey: bindKey("Shift-Right", "Shift-Right"),
exec: function(editor) { editor.getSelection().selectRight(); }
}, {
name: "gotoright",
bindKey: bindKey("Right", "Right|Ctrl-F"),
exec: function(editor, args) { editor.navigateRight(args.times); }
}, {
name: "selectpagedown",
bindKey: bindKey("Shift-PageDown", "Shift-PageDown"),
exec: function(editor) { editor.selectPageDown(); }
}, {
name: "pagedown",
bindKey: bindKey(null, "PageDown"),
exec: function(editor) { editor.scrollPageDown(); }
}, {
name: "gotopagedown",
bindKey: bindKey("PageDown", "Option-PageDown|Ctrl-V"),
exec: function(editor) { editor.gotoPageDown(); }
}, {
name: "selectpageup",
bindKey: bindKey("Shift-PageUp", "Shift-PageUp"),
exec: function(editor) { editor.selectPageUp(); }
}, {
name: "pageup",
bindKey: bindKey(null, "PageUp"),
exec: function(editor) { editor.scrollPageUp(); }
}, {
name: "gotopageup",
bindKey: bindKey("PageUp", "Option-PageUp"),
exec: function(editor) { editor.gotoPageUp(); }
}, {
name: "selectlinestart",
bindKey: bindKey("Shift-Home", "Shift-Home"),
exec: function(editor) { editor.getSelection().selectLineStart(); }
}, {
name: "selectlineend",
bindKey: bindKey("Shift-End", "Shift-End"),
exec: function(editor) { editor.getSelection().selectLineEnd(); }
}, {
name: "del",
bindKey: bindKey("Delete", "Delete|Ctrl-D"),
@ -292,10 +354,6 @@ exports.commands = [{
exec: function(editor, args) {
editor.insert(lang.stringRepeat(args.text || "", args.times || 1));
}
}, {
name: "centerselection",
bindKey: bindKey("Ctrl-L", "Ctrl-L"),
exec: function(editor) { editor.centerSelection(); }
}, {
name: "splitline",
bindKey: bindKey("Ctrl-K", "Ctrl-O"),
@ -312,24 +370,6 @@ exports.commands = [{
name: "tolowercase",
bindKey: bindKey("Ctrl-Shift-U", "Ctrl-Shift-U"),
exec: function(editor) { editor.toLowerCase(); }
}, {
name: "fold",
bindKey: bindKey("Alt-L", "Alt-L"),
exec: function(editor) {
editor.session.toggleFold(false);
}
}, {
name: "unfold",
bindKey: bindKey("Alt-Shift-L", "Alt-Shift-L"),
exec: function(editor) { editor.session.toggleFold(true); }
}, {
name: "foldall",
bindKey: bindKey("Alt-0", "Alt-0"),
exec: function(editor) { editor.session.foldAll(); }
}, {
name: "unfoldall",
bindKey: bindKey("Alt-Shift-0", "Alt-Shift-0"),
exec: function(editor) { editor.session.unfold(); }
}];
});

View file

@ -429,9 +429,6 @@ var Editor = function(renderer, session) {
};
this.insert = function(text) {
if (this.$readOnly)
return;
var session = this.session;
var mode = session.getMode();
@ -641,9 +638,6 @@ var Editor = function(renderer, session) {
};
this.remove = function(dir) {
if (this.$readOnly)
return;
if (this.selection.isEmpty()){
if(dir == "left")
this.selection.selectLeft();
@ -665,9 +659,6 @@ var Editor = function(renderer, session) {
};
this.removeWordRight = function() {
if (this.$readOnly)
return;
if (this.selection.isEmpty())
this.selection.selectWordRight();
@ -676,9 +667,6 @@ var Editor = function(renderer, session) {
};
this.removeWordLeft = function() {
if (this.$readOnly)
return;
if (this.selection.isEmpty())
this.selection.selectWordLeft();
@ -687,9 +675,6 @@ var Editor = function(renderer, session) {
};
this.removeToLineStart = function() {
if (this.$readOnly)
return;
if (this.selection.isEmpty())
this.selection.selectLineStart();
@ -698,9 +683,6 @@ var Editor = function(renderer, session) {
};
this.removeToLineEnd = function() {
if (this.$readOnly)
return;
if (this.selection.isEmpty())
this.selection.selectLineEnd();
@ -715,9 +697,6 @@ var Editor = function(renderer, session) {
};
this.splitLine = function() {
if (this.$readOnly)
return;
if (!this.selection.isEmpty()) {
this.session.remove(this.getSelectionRange());
this.clearSelection();
@ -729,9 +708,6 @@ var Editor = function(renderer, session) {
};
this.transposeLetters = function() {
if (this.$readOnly)
return;
if (!this.selection.isEmpty()) {
return;
}
@ -755,9 +731,6 @@ var Editor = function(renderer, session) {
};
this.toLowerCase = function() {
if (this.$readOnly)
return;
var originalRange = this.getSelectionRange();
if (this.selection.isEmpty()) {
this.selection.selectWord();
@ -770,9 +743,6 @@ var Editor = function(renderer, session) {
};
this.toUpperCase = function() {
if (this.$readOnly)
return;
var originalRange = this.getSelectionRange();
if (this.selection.isEmpty()) {
this.selection.selectWord();
@ -785,9 +755,6 @@ var Editor = function(renderer, session) {
};
this.indent = function() {
if (this.$readOnly)
return;
var session = this.session;
var range = this.getSelectionRange();
@ -811,26 +778,17 @@ var Editor = function(renderer, session) {
};
this.blockOutdent = function() {
if (this.$readOnly)
return;
var selection = this.session.getSelection();
this.session.outdentRows(selection.getRange());
};
this.toggleCommentLines = function() {
if (this.$readOnly)
return;
var state = this.session.getState(this.getCursorPosition().row);
var rows = this.$getSelectedRows();
this.session.getMode().toggleCommentLines(state, this.session, rows.first, rows.last);
};
this.removeLines = function() {
if (this.$readOnly)
return;
var rows = this.$getSelectedRows();
var range;
if (rows.first == 0 || rows.last+1 < this.session.getLength())
@ -845,18 +803,12 @@ var Editor = function(renderer, session) {
};
this.moveLinesDown = function() {
if (this.$readOnly)
return;
this.$moveLines(function(firstRow, lastRow) {
return this.session.moveLinesDown(firstRow, lastRow);
});
};
this.moveLinesUp = function() {
if (this.$readOnly)
return;
this.$moveLines(function(firstRow, lastRow) {
return this.session.moveLinesUp(firstRow, lastRow);
});
@ -870,9 +822,6 @@ var Editor = function(renderer, session) {
};
this.copyLinesUp = function() {
if (this.$readOnly)
return;
this.$moveLines(function(firstRow, lastRow) {
this.session.duplicateLines(firstRow, lastRow);
return 0;
@ -880,9 +829,6 @@ var Editor = function(renderer, session) {
};
this.copyLinesDown = function() {
if (this.$readOnly)
return;
this.$moveLines(function(firstRow, lastRow) {
return this.session.duplicateLines(firstRow, lastRow);
});