2013-09-01 18:09:09 +00:00
|
|
|
define([
|
|
|
|
"editor",
|
|
|
|
"dialog",
|
|
|
|
"command",
|
|
|
|
"file",
|
2013-10-07 06:03:14 +00:00
|
|
|
"tab",
|
2013-09-04 19:45:51 +00:00
|
|
|
"settings!ace,user",
|
|
|
|
"aceBindings"
|
2013-09-24 18:03:39 +00:00
|
|
|
],
|
2013-10-07 06:03:14 +00:00
|
|
|
function(editor, dialog, command, File, Tab, Settings) {
|
2013-08-21 00:35:25 +00:00
|
|
|
|
|
|
|
var tabs = [];
|
2013-09-03 15:47:59 +00:00
|
|
|
var cfg = Settings.get("ace");
|
2013-09-04 15:53:11 +00:00
|
|
|
var userConfig = Settings.get("user");
|
2013-09-13 18:13:00 +00:00
|
|
|
var syntax = document.find(".syntax");
|
2013-09-24 18:03:39 +00:00
|
|
|
var stack = [];
|
|
|
|
var stackOffset = 0;
|
2013-08-21 00:35:25 +00:00
|
|
|
|
|
|
|
var renderTabs = function() {
|
2013-09-01 18:09:09 +00:00
|
|
|
var tabContainer = document.find(".tabs");
|
2013-08-21 00:35:25 +00:00
|
|
|
var contents = "";
|
2013-08-22 06:35:14 +00:00
|
|
|
var current = editor.getSession();
|
|
|
|
tabContainer.innerHTML = "";
|
2013-08-21 00:35:25 +00:00
|
|
|
tabs.forEach(function(tab, index) {
|
2013-08-22 06:35:14 +00:00
|
|
|
var span = document.createElement("span");
|
2013-09-13 18:13:00 +00:00
|
|
|
span.setAttribute("draggable", true);
|
2013-08-22 06:35:14 +00:00
|
|
|
span.setAttribute("command", "session:raise-tab");
|
|
|
|
span.setAttribute("argument", index);
|
2013-09-13 18:13:00 +00:00
|
|
|
span.className = "tab";
|
2013-08-22 06:35:14 +00:00
|
|
|
if (tab === current) {
|
2013-09-13 18:13:00 +00:00
|
|
|
span.className += " active";
|
2013-08-22 06:35:14 +00:00
|
|
|
}
|
2013-09-14 01:11:47 +00:00
|
|
|
span.innerHTML = tab.fileName + (tab.modified ? " •" : "");
|
2013-08-22 06:35:14 +00:00
|
|
|
var close = document.createElement("a");
|
|
|
|
close.innerHTML = "×";
|
|
|
|
close.className = "close";
|
|
|
|
close.setAttribute("command", "session:close-tab");
|
|
|
|
close.setAttribute("argument", index);
|
|
|
|
span.append(close);
|
|
|
|
tabContainer.append(span);
|
2013-08-21 00:35:25 +00:00
|
|
|
});
|
2013-09-24 19:38:34 +00:00
|
|
|
setRetained();
|
2013-09-04 22:01:32 +00:00
|
|
|
};
|
2013-08-21 00:35:25 +00:00
|
|
|
|
2013-10-07 06:03:14 +00:00
|
|
|
command.on("sessions:render", renderTabs);
|
|
|
|
|
|
|
|
var setRetained = function() {
|
|
|
|
var keep = [];
|
|
|
|
tabs.forEach(function(tab, i) {
|
|
|
|
if (!tab.file || tab.file.virtual) return;
|
|
|
|
keep[i] = tab.file.retain();
|
|
|
|
});
|
|
|
|
keep = keep.filter(function(m) { return m });
|
|
|
|
if (keep.length) {
|
|
|
|
chrome.storage.local.set({ retained: keep });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-01 04:21:21 +00:00
|
|
|
var setTabSyntax = function(tab) {
|
2013-09-04 15:53:11 +00:00
|
|
|
tab.setTabSize(userConfig.indentation || 2);
|
|
|
|
tab.setUseWrapMode(userConfig.wordWrap);
|
2013-09-16 17:31:07 +00:00
|
|
|
tab.setWrapLimit(userConfig.wrapLimit || null);
|
2013-09-20 19:42:24 +00:00
|
|
|
tab.setNewLineMode(userConfig.lineEnding || "auto");
|
2013-09-13 15:01:52 +00:00
|
|
|
tab.setUseWorker(userConfig.useWorker);
|
2013-09-05 01:36:27 +00:00
|
|
|
var syntaxValue = "plain_text";
|
2013-09-13 15:25:39 +00:00
|
|
|
if (tab.syntaxMode) {
|
|
|
|
syntaxValue = tab.syntaxMode;
|
|
|
|
} else if (tab.file) {
|
|
|
|
if (tab.file.virtual) {
|
|
|
|
//settings files are special
|
|
|
|
syntaxValue = "javascript";
|
|
|
|
tab.setMode("ace/mode/javascript");
|
2013-09-14 02:35:57 +00:00
|
|
|
} else if (tab.file.entry) {
|
2013-09-13 15:25:39 +00:00
|
|
|
var found = false;
|
|
|
|
var extension = tab.file.entry.name.split(".").pop();
|
|
|
|
for (var i = 0; i < cfg.modes.length; i++) {
|
|
|
|
var mode = cfg.modes[i];
|
|
|
|
if (mode.extensions.indexOf(extension) > -1) {
|
|
|
|
tab.setMode("ace/mode/" + mode.name);
|
|
|
|
syntaxValue = mode.name;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-01 04:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 15:25:39 +00:00
|
|
|
tab.syntaxMode = syntaxValue;
|
2013-09-01 04:21:21 +00:00
|
|
|
}
|
2013-09-13 20:26:46 +00:00
|
|
|
tab.setMode("ace/mode/" + syntaxValue);
|
2013-09-05 01:36:27 +00:00
|
|
|
syntax.value = syntaxValue;
|
2013-09-01 04:21:21 +00:00
|
|
|
};
|
|
|
|
|
2013-08-21 00:47:07 +00:00
|
|
|
var addTab = function(contents, file) {
|
2013-08-22 06:35:14 +00:00
|
|
|
var current = editor.getSession();
|
2013-10-07 06:03:14 +00:00
|
|
|
var tab;
|
2013-08-31 20:41:51 +00:00
|
|
|
//reuse tab if opening a file into an empty tab
|
|
|
|
if (file && !current.file && !current.modified) {
|
2013-10-07 06:03:14 +00:00
|
|
|
tab = current;
|
|
|
|
tab.setValue(contents);
|
|
|
|
tab.setFile(file);
|
|
|
|
tab.modified = false;
|
2013-08-22 06:35:14 +00:00
|
|
|
} else {
|
2013-10-07 06:03:14 +00:00
|
|
|
tab = new Tab(contents, file);
|
|
|
|
stack.unshift(tab);
|
|
|
|
tabs.push(tab);
|
2013-08-22 06:35:14 +00:00
|
|
|
}
|
2013-10-07 06:03:14 +00:00
|
|
|
setTabSyntax(tab);
|
|
|
|
raiseTab(tab);
|
|
|
|
renderTabs();
|
|
|
|
return tab;
|
2013-08-21 00:35:25 +00:00
|
|
|
};
|
|
|
|
|
2013-08-21 00:47:07 +00:00
|
|
|
var removeTab = function(index) {
|
2013-08-31 20:23:41 +00:00
|
|
|
if (!index) {
|
|
|
|
index = tabs.indexOf(editor.getSession());
|
|
|
|
}
|
2013-09-01 19:23:13 +00:00
|
|
|
var tab = tabs[index];
|
2013-09-24 18:37:40 +00:00
|
|
|
stack = stack.filter(function(t) { return t !== tab });
|
2013-09-01 19:23:13 +00:00
|
|
|
|
|
|
|
var continuation = function() {
|
2013-09-04 22:01:32 +00:00
|
|
|
tab.drop();
|
2013-09-01 19:23:13 +00:00
|
|
|
tabs = tabs.filter(function(tab, i) {
|
|
|
|
if (i == index) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2013-09-04 22:01:32 +00:00
|
|
|
});
|
2013-09-01 19:23:13 +00:00
|
|
|
if (tabs.length == 0) {
|
|
|
|
return addTab();
|
2013-08-22 06:35:14 +00:00
|
|
|
}
|
2013-09-01 19:23:13 +00:00
|
|
|
var next = index - 1;
|
|
|
|
if (next < 0) {
|
|
|
|
next = 0;
|
|
|
|
}
|
2013-10-07 06:03:14 +00:00
|
|
|
raiseTabByIndex(next);
|
2013-09-13 15:01:52 +00:00
|
|
|
};
|
2013-09-01 19:23:13 +00:00
|
|
|
|
|
|
|
if (tab.modified) {
|
|
|
|
dialog(
|
2013-09-24 14:57:22 +00:00
|
|
|
tab.fileName + " has been modified.\nDo you want to save changes?",
|
|
|
|
[{label: "Save", value: true}, {label: "Don't save", value: false}, { label: "Cancel" }],
|
2013-09-01 19:23:13 +00:00
|
|
|
function(confirm) {
|
2013-09-24 14:57:22 +00:00
|
|
|
if (typeof confirm !== "boolean") {
|
2013-09-14 02:35:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-09-01 19:23:13 +00:00
|
|
|
if (confirm) {
|
2013-09-14 02:35:57 +00:00
|
|
|
return tab.save(continuation);
|
2013-09-01 19:23:13 +00:00
|
|
|
}
|
|
|
|
continuation();
|
2013-09-13 15:01:52 +00:00
|
|
|
});
|
2013-09-14 02:35:57 +00:00
|
|
|
return;
|
2013-09-01 19:23:13 +00:00
|
|
|
} else {
|
|
|
|
continuation();
|
2013-09-01 18:36:56 +00:00
|
|
|
}
|
2013-08-21 00:47:07 +00:00
|
|
|
};
|
|
|
|
|
2013-10-07 06:03:14 +00:00
|
|
|
var raiseTab = function(tab) {
|
|
|
|
editor.setSession(tab);
|
|
|
|
syntax.value = tab.syntaxMode || "plain_text";
|
|
|
|
renderTabs();
|
|
|
|
editor.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
var raiseBlurred = function(tab) {
|
|
|
|
editor.setSession(tab);
|
|
|
|
syntax.value = tab.syntaxMode || "plain_text";
|
|
|
|
renderTabs();
|
|
|
|
};
|
|
|
|
|
|
|
|
var raiseTabByIndex = function(index) {
|
2013-08-21 00:35:25 +00:00
|
|
|
var tab = tabs[index];
|
2013-10-07 06:03:14 +00:00
|
|
|
raiseTab(tab);
|
2013-09-21 00:39:04 +00:00
|
|
|
command.fire("session:check-file");
|
2013-08-21 00:47:07 +00:00
|
|
|
};
|
2013-08-21 00:35:25 +00:00
|
|
|
|
2013-10-07 06:03:14 +00:00
|
|
|
var resetStack = function(tab) {
|
|
|
|
var raised = tab || stack[stackOffset];
|
|
|
|
stack = stack.filter(function(t) { return t != raised });
|
|
|
|
stack.unshift(raised);
|
|
|
|
}
|
|
|
|
|
2013-09-24 18:37:40 +00:00
|
|
|
var watchCtrl = function(e) {
|
2013-09-24 18:03:39 +00:00
|
|
|
if (e.keyCode == 17) {
|
2013-10-07 06:03:14 +00:00
|
|
|
resetStack();
|
2013-09-24 18:37:40 +00:00
|
|
|
document.body.off("keyup", watchCtrl);
|
|
|
|
ctrl = false;
|
2013-09-24 18:03:39 +00:00
|
|
|
}
|
2013-09-24 18:37:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var ctrl = false;
|
2013-09-24 18:03:39 +00:00
|
|
|
|
2013-10-07 06:03:14 +00:00
|
|
|
var switchTab = function() {
|
2013-09-24 18:37:40 +00:00
|
|
|
if (!ctrl) {
|
|
|
|
ctrl = true;
|
|
|
|
stackOffset = 0;
|
|
|
|
document.body.on("keyup", watchCtrl);
|
|
|
|
}
|
2013-09-24 18:03:39 +00:00
|
|
|
stackOffset = (stackOffset + 1) % stack.length;
|
2013-10-07 06:03:14 +00:00
|
|
|
raiseTab(stack[stackOffset]);
|
2013-09-13 15:01:52 +00:00
|
|
|
};
|
2013-09-01 04:21:21 +00:00
|
|
|
|
2013-10-07 06:03:14 +00:00
|
|
|
command.on("session:raise-tab", function(index) {
|
|
|
|
var tab = tabs[index];
|
|
|
|
raiseTab(tab);
|
|
|
|
resetStack(tab);
|
|
|
|
});
|
|
|
|
command.on("session:close-tab", removeTab);
|
|
|
|
command.on("session:change-tab", switchTab);
|
|
|
|
|
2013-09-13 18:13:00 +00:00
|
|
|
var enableTabDragDrop = function() {
|
|
|
|
var tabContainer = document.find(".tabs");
|
|
|
|
var draggedTab = null;
|
|
|
|
tabContainer.on("dragstart", function(e) {
|
|
|
|
if (!e.target.matches(".tab")) return;
|
2013-09-14 06:59:43 +00:00
|
|
|
e.target.style.opacity = .4;
|
|
|
|
setTimeout(function() {
|
|
|
|
e.target.classList.add("dragging");
|
|
|
|
}, 50);
|
2013-09-13 18:13:00 +00:00
|
|
|
e.dataTransfer.effectAllowed = "move";
|
2013-09-14 20:55:54 +00:00
|
|
|
e.dataTransfer.setData("application/x-tab-id", e.target.getAttribute("argument"));
|
2013-09-13 18:13:00 +00:00
|
|
|
draggedTab = e.target;
|
|
|
|
draggedTab.ondragend = function() {
|
|
|
|
draggedTab = null;
|
2013-09-14 06:59:43 +00:00
|
|
|
e.target.style.opacity = null;
|
|
|
|
e.target.classList.remove("dragging");
|
2013-09-13 18:13:00 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
tabContainer.on("dragover", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.dropEffect = "move";
|
|
|
|
});
|
2013-09-14 06:59:43 +00:00
|
|
|
tabContainer.on("dragenter", function(e) {
|
|
|
|
if (!e.target.matches(".tab")) return;
|
|
|
|
e.target.classList.add("hovering");
|
|
|
|
});
|
|
|
|
tabContainer.on("dragleave", function(e) {
|
|
|
|
if (!e.target.matches(".tab")) return;
|
|
|
|
e.target.classList.remove("hovering");
|
|
|
|
});
|
2013-09-13 18:13:00 +00:00
|
|
|
tabContainer.on("drop", function(e) {
|
|
|
|
if (!draggedTab) return;
|
|
|
|
var target = e.target;
|
|
|
|
var location = "before";
|
|
|
|
var x = e.offsetX;
|
|
|
|
while (!target.matches(".tab")) {
|
|
|
|
if (target == tabContainer) {
|
|
|
|
var elements = tabContainer.findAll(".tab");
|
|
|
|
location = "after";
|
|
|
|
elements.forEach(function(el) {
|
|
|
|
if (el.offsetLeft < x) {
|
|
|
|
target = el;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
target = target.parentElement;
|
|
|
|
x += target.offsetLeft;
|
|
|
|
}
|
2013-09-14 20:55:54 +00:00
|
|
|
var from = tabs[e.dataTransfer.getData("application/x-tab-id") * 1];
|
2013-09-13 18:13:00 +00:00
|
|
|
var onto = tabs[target.getAttribute("argument") * 1];
|
|
|
|
var reordered = [];
|
|
|
|
tabs.forEach(function(t) {
|
|
|
|
if (t == from) return;
|
|
|
|
if (t == onto && location == "before") {
|
|
|
|
reordered.push(from);
|
|
|
|
}
|
|
|
|
reordered.push(t);
|
|
|
|
if (t == onto && location == "after") {
|
|
|
|
reordered.push(from);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tabs = reordered;
|
|
|
|
renderTabs();
|
|
|
|
});
|
|
|
|
};
|
2013-08-22 06:35:14 +00:00
|
|
|
|
2013-09-02 01:26:22 +00:00
|
|
|
var init = function() {
|
|
|
|
cfg.modes.forEach(function(mode) {
|
|
|
|
var option = document.createElement("option");
|
|
|
|
option.innerHTML = mode.label;
|
|
|
|
option.value = mode.name;
|
|
|
|
syntax.append(option);
|
|
|
|
});
|
|
|
|
addTab("");
|
2013-10-07 06:03:14 +00:00
|
|
|
renderTabs();
|
2013-09-13 18:13:00 +00:00
|
|
|
enableTabDragDrop();
|
2013-09-16 18:16:06 +00:00
|
|
|
reset();
|
2013-09-02 01:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var reset = function() {
|
2013-09-03 15:47:59 +00:00
|
|
|
cfg = Settings.get("ace");
|
2013-09-04 19:26:09 +00:00
|
|
|
userConfig = Settings.get("user");
|
2013-09-13 15:01:52 +00:00
|
|
|
tabs.forEach(function(tab) {
|
|
|
|
setTabSyntax(tab);
|
|
|
|
})
|
2013-09-02 01:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
command.on("init:startup", init);
|
|
|
|
command.on("init:restart", reset);
|
|
|
|
|
|
|
|
command.on("session:syntax", function(mode) {
|
2013-09-13 15:25:39 +00:00
|
|
|
var session = editor.getSession();
|
|
|
|
session.setMode("ace/mode/" + mode);
|
|
|
|
session.syntaxMode = mode;
|
2013-09-05 15:28:40 +00:00
|
|
|
editor.focus();
|
2013-09-02 01:26:22 +00:00
|
|
|
});
|
2013-08-22 06:35:14 +00:00
|
|
|
|
2013-09-05 01:36:27 +00:00
|
|
|
|
2013-09-11 03:34:28 +00:00
|
|
|
var locationMemory = null;
|
|
|
|
|
2013-09-03 15:47:59 +00:00
|
|
|
return {
|
2013-09-10 16:19:14 +00:00
|
|
|
addFile: addTab,
|
2013-09-16 18:16:06 +00:00
|
|
|
addDefaultsFile: function(name) {
|
|
|
|
Settings.load(name, function() {
|
|
|
|
var tab = addTab(Settings.getAsString(name, true));
|
|
|
|
tab.syntaxMode = "javascript";
|
|
|
|
setTabSyntax(tab);
|
|
|
|
tab.fileName = name + ".json";
|
|
|
|
renderTabs();
|
|
|
|
});
|
|
|
|
},
|
2013-09-21 19:21:25 +00:00
|
|
|
getAllTabs: function() {
|
|
|
|
return tabs;
|
|
|
|
},
|
2013-09-10 16:19:14 +00:00
|
|
|
getCurrent: function() {
|
|
|
|
return editor.getSession();
|
|
|
|
},
|
|
|
|
getTabByIndex: function(index) {
|
|
|
|
return tabs[index];
|
|
|
|
},
|
|
|
|
getTabByName: function(name) {
|
|
|
|
for (var i = 0; i < tabs.length; i++) {
|
|
|
|
if (tabs[i].fileName == name) {
|
|
|
|
return tabs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
getFilenames: function() {
|
|
|
|
return tabs.map(function(t) { return t.fileName });
|
2013-09-11 03:34:28 +00:00
|
|
|
},
|
|
|
|
setCurrent: function(tab) {
|
2013-10-07 06:03:14 +00:00
|
|
|
raiseTab(tab);
|
2013-09-11 03:34:28 +00:00
|
|
|
},
|
|
|
|
saveLocation: function() {
|
|
|
|
var session = editor.getSession();
|
|
|
|
var position = editor.getCursorPosition();
|
|
|
|
locationMemory = {
|
|
|
|
tab: session,
|
|
|
|
cursor: position
|
2013-09-11 06:15:04 +00:00
|
|
|
};
|
2013-09-11 03:34:28 +00:00
|
|
|
},
|
|
|
|
restoreLocation: function() {
|
2013-09-11 06:15:04 +00:00
|
|
|
if (!locationMemory) return;
|
2013-10-07 06:03:14 +00:00
|
|
|
raiseTab(locationMemory.tab);
|
2013-09-11 03:34:28 +00:00
|
|
|
editor.moveCursorToPosition(locationMemory.cursor);
|
2013-09-16 18:58:37 +00:00
|
|
|
},
|
|
|
|
renderTabs: renderTabs
|
2013-09-03 15:47:59 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 00:53:03 +00:00
|
|
|
});
|