2014-08-19 13:07:35 +04:00
|
|
|
var editors = {
|
|
|
|
data: [],
|
|
|
|
init: function() {
|
|
|
|
editors._initAutocomplete();
|
|
|
|
editors._initTabs();
|
|
|
|
},
|
|
|
|
_initAutocomplete: function() {
|
|
|
|
CodeMirror.registerHelper("hint", "go", function(editor, options) {
|
2014-08-22 05:44:41 +04:00
|
|
|
var word = /[\w$]+/
|
|
|
|
|
|
|
|
var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
|
|
|
|
|
|
|
|
var start = cur.ch, end = start;
|
|
|
|
while (end < curLine.length && word.test(curLine.charAt(end))) ++end;
|
|
|
|
while (start && word.test(curLine.charAt(start - 1))) --start;
|
|
|
|
var curWord = start != end && curLine.slice(start, end);
|
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
var request = {
|
|
|
|
code: editor.getValue(),
|
|
|
|
cursorLine: editor.getCursor().line,
|
|
|
|
cursorCh: editor.getCursor().ch
|
|
|
|
};
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
// XXX: 回调有问题,暂时不使用 WS 协议
|
|
|
|
//editorWS.send(JSON.stringify(request));
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
var autocompleteHints = [];
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
$.ajax({
|
|
|
|
async: false, // 同步执行
|
|
|
|
type: 'POST',
|
|
|
|
url: '/autocomplete',
|
|
|
|
data: JSON.stringify(request),
|
|
|
|
dataType: "json",
|
|
|
|
success: function(data) {
|
|
|
|
var autocompleteArray = data[1];
|
2014-08-22 05:44:41 +04:00
|
|
|
|
|
|
|
if (autocompleteArray) {
|
|
|
|
for (var i = 0; i < autocompleteArray.length; i++) {
|
|
|
|
autocompleteHints[i] = autocompleteArray[i].name;
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
2014-08-19 13:07:35 +04:00
|
|
|
});
|
|
|
|
|
2014-08-22 05:44:41 +04:00
|
|
|
return {list: autocompleteHints, from: CodeMirror.Pos(cur.line, start), to: CodeMirror.Pos(cur.line, end)};
|
|
|
|
});
|
|
|
|
|
|
|
|
CodeMirror.commands.autocompleteAfterDot = function(cm) {
|
|
|
|
var cur = cm.getCursor();
|
2014-08-22 10:06:47 +04:00
|
|
|
// console.log(cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur));
|
2014-08-22 05:44:41 +04:00
|
|
|
|
2014-08-22 08:29:54 +04:00
|
|
|
// var token = cm.getTokenAt(cm.getCursor());
|
2014-08-22 05:44:41 +04:00
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
if (!cm.state.completionActive) {
|
|
|
|
cm.showHint({hint: CodeMirror.hint.go, completeSingle: false});
|
|
|
|
}
|
2014-08-23 16:50:20 +04:00
|
|
|
}, 50)
|
2014-08-22 05:44:41 +04:00
|
|
|
|
|
|
|
return CodeMirror.Pass;
|
|
|
|
};
|
2014-08-22 08:29:54 +04:00
|
|
|
|
|
|
|
CodeMirror.commands.autocompleteAnyWord = function(cm) {
|
|
|
|
cm.showHint({hint: CodeMirror.hint.auto});
|
|
|
|
}
|
2014-08-19 13:07:35 +04:00
|
|
|
},
|
|
|
|
_initTabs: function() {
|
|
|
|
var $tabsPanel = $(".edit-panel .tabs-panel"),
|
|
|
|
$tabs = $(".edit-panel .tabs");
|
|
|
|
|
|
|
|
$tabs.on("click", "span", function() {
|
|
|
|
var $it = $(this);
|
|
|
|
if ($it.hasClass("current")) {
|
|
|
|
return false;
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
2014-08-19 14:05:56 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
var id = $it.data("id");
|
|
|
|
|
|
|
|
$tabs.children("span").removeClass("current");
|
|
|
|
$tabsPanel.children("div").hide();
|
|
|
|
|
|
|
|
$it.addClass("current");
|
|
|
|
$("#editor" + id).parent().show();
|
2014-08-19 14:05:56 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
// set tree node selected
|
|
|
|
var node = tree.fileTree.getNodeByTId(id);
|
|
|
|
tree.fileTree.selectNode(node);
|
|
|
|
wide.curNode = node;
|
2014-08-19 14:05:56 +04:00
|
|
|
|
|
|
|
for (var i = 0, ii = editors.data.length; i < ii; i++) {
|
|
|
|
if (editors.data[i].id === id) {
|
|
|
|
wide.curEditor = editors.data[i].editor;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 17:45:43 +04:00
|
|
|
});
|
2014-08-19 13:07:35 +04:00
|
|
|
},
|
2014-08-19 14:05:56 +04:00
|
|
|
_selectTab: function(id, editor) {
|
2014-08-19 13:07:35 +04:00
|
|
|
var $tabsPanel = $(".edit-panel .tabs-panel"),
|
|
|
|
$tabs = $(".edit-panel .tabs");
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
var $currentTab = $tabs.children(".current");
|
|
|
|
if ($currentTab.data("id") === id) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
$tabs.children("span").removeClass("current");
|
|
|
|
$tabsPanel.children("div").hide();
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
$tabs.children("span[data-id='" + id + "']").addClass("current");
|
|
|
|
$("#editor" + id).parent().show();
|
2014-08-19 14:05:56 +04:00
|
|
|
wide.curEditor = editor;
|
2014-08-19 13:07:35 +04:00
|
|
|
},
|
|
|
|
newEditor: function(data) {
|
|
|
|
var id = wide.curNode.tId;
|
|
|
|
for (var i = 0, ii = editors.data.length; i < ii; i++) {
|
|
|
|
if (editors.data[i].id === id) {
|
2014-08-19 14:05:56 +04:00
|
|
|
editors._selectTab(id, editors.data[i].editor);
|
2014-08-19 13:07:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var $tabsPanel = $(".edit-panel .tabs-panel"),
|
|
|
|
$tabs = $(".edit-panel .tabs");
|
|
|
|
|
|
|
|
$tabs.children("span").removeClass("current");
|
|
|
|
$tabsPanel.children("div").hide();
|
|
|
|
|
|
|
|
$tabsPanel.append('<div><textarea id="editor' + id + '" name="code"></textarea></div>');
|
|
|
|
$tabs.append('<span data-id="' + id + '" class="current">' + wide.curNode.name + '</span>');
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
var editor = CodeMirror.fromTextArea(document.getElementById("editor" + id), {
|
|
|
|
lineNumbers: true,
|
|
|
|
theme: 'lesser-dark',
|
2014-08-21 11:39:07 +04:00
|
|
|
indentUnit: 4,
|
2014-08-19 13:07:35 +04:00
|
|
|
extraKeys: {
|
2014-08-22 08:29:54 +04:00
|
|
|
"Ctrl-\\": "autocompleteAnyWord",
|
2014-08-22 05:44:41 +04:00
|
|
|
".": "autocompleteAfterDot"
|
2014-08-19 13:07:35 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
editor.setSize('100%', 450);
|
|
|
|
editor.setValue(data.content);
|
|
|
|
editor.setOption("mode", data.mode);
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
wide.curEditor = editor;
|
|
|
|
editors.data.push({
|
|
|
|
"editor": editor,
|
|
|
|
"id": id
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removeEditor: function() {
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
}
|
|
|
|
};
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-19 13:07:35 +04:00
|
|
|
editors.init();
|