Palette work starts.

This commit is contained in:
Thomas Wilburn 2013-09-10 09:19:14 -07:00
parent bd7265e165
commit bd964ed279
10 changed files with 204 additions and 8 deletions

View file

@ -22,7 +22,7 @@ module.exports = function(grunt) {
}
},
compress: {
package: {
pack: {
options: {
archive: "build/caret.zip",
pretty: true

View file

@ -11,5 +11,9 @@
//Sublime compatibility
"^-l": "sublime:expand-to-line",
"^-d": { "ace": "selectMoreAfter" }
"^-d": { "ace": "selectMoreAfter" },
"^-p": { "command": "palette:open" },
"^-P": { "command": "palette:open", "argument": "command" },
"^-r": { "command": "palette:open", "argument": "reference" },
"^-g": { "command": "palette:open", "argument": "goto" }
}

45
css/palette.less Normal file
View file

@ -0,0 +1,45 @@
.palette {
display: none;
width: 300px;
position: fixed;
top: 20px;
left: ~"calc(50% - 150px)";
background: #333;
color: #AAA;
z-index: 999;
padding: 4px 12px;
box-shadow: 4px 4px 16px rgba(0, 0, 0, .2);
text-size: 16px;
&.active {
display: block;
}
h1 {
font-size: 12px;
padding: 0;
margin: 0;
}
input {
width: 100%;
border: 2px solid #555;
border-radius: 4px;
padding: 8px;
background: transparent;
color: white;
font-weight: bold;
font-size: 18px;
}
.results {
padding: 0;
margin: 0;
list-style-type: none;
li {
border-top: 1px solid #555;
display: block;
}
}
}

View file

@ -3,4 +3,5 @@
@import "editor";
@import "tabs";
@import "ui";
@import "dialog";
@import "dialog";
@import "palette";

View file

@ -12,5 +12,8 @@ define(["command", "editor"], function(command, editor) {
command.on("ace:command", function(cmd) {
editor.execCommand(cmd);
});
//unbind the key for the palette, whatever it does.
editor.commands.bindKey("Ctrl-P", null);
});

View file

@ -6,9 +6,6 @@ define(["file", "command", "settings!ace,user", "dom2"], function(File, command,
var aceConfig = Settings.get("ace");
var editor = window.editor = ace.edit("editor");
var session = window.session = editor.getSession();
session.setMode("ace/mode/javascript");
var container = document.body.find(".editor-container");
var containerSize = container.getBoundingClientRect();

View file

@ -1,4 +1,4 @@
require(["command", "keys", "sessions", "menus"], function(command) {
require(["command", "keys", "sessions", "menus", "palette"], function(command) {
var frame = chrome.app.window.current();

116
js/palette.js Normal file
View file

@ -0,0 +1,116 @@
define(["sessions", "command", "dom2"], function(sessions, command) {
var palette = document.find(".palette");
var input = palette.find("input");
var resultList = palette.find(".results");
var resultTemplate = document.find("#palette-result").content;
var isCommand = false;
var last = null;
var re = {
file: /^([^:#@]*)/,
line: /:(\d*)/,
reference: /@([^:#]*)/,
search: /#([^:@]*)/
};
var prefixes = {
":": "line",
"@": "reference",
"#": "search"
};
var modes = {
"line": ":",
"search": "#",
"reference": "@"
};
var parseRequest = function(query) {
var first = query[0];
var mode = prefixes[first] || isCommand ? "command" : "file";
var results = [];
//parsing and searching goes here
var file = re.file.test(query) && re.file.exec(query)[1];
var line = re.line.test(query) && Number(re.line.exec(query)[1]);
var search = re.search.test(query) && re.search.exec(query)[1];
var reference = re.reference.test(query) && re.reference.exec(query)[1];
var openFileNames = sessions.getFilenames();
var tabs;
if (file) {
var fuzzyFile = new RegExp(file.split("").join(".*"));
var matches = openFileNames.filter(function(name) {
return fuzzyFile.test(name);
});
tabs = matches.map(sessions.getTabByName);
results.push(matches.join(", "));
} else {
results.push("File: current file");
tabs = [ sessions.getCurrent() ];
}
if (line) {
results.push("Line: " + line);
}
if (search) {
results.push("Search: " + search);
}
if (reference) {
results.push("Reference: " + reference);
}
resultList.innerHTML = "";
results.forEach(function(r, i) {
var element = resultTemplate.cloneNode(true).find("li");
element.innerHTML = r;
if (!i) {
element.classList.add("current");
}
resultList.appendChild(element);
})
};
var navigateList = function() {};
command.on("palette:open", function(mode) {
palette.classList.add("active");
isCommand = mode == "command";
palette.find(".mode").innerHTML = isCommand ?
"Command:" : "Go To:"
input.value = modes[mode] || "";
input.focus();
//store starting position
last = {
session: sessions.getCurrent(),
row: null,
column: null
}
});
input.on("blur", function() {
palette.classList.remove("active");
if (last) {
//restore position on cancel
}
});
input.on("keydown", function(e) {
if (e.keyCode == 27) {
return input.blur();
}
if (e.keyCode == 38 || e.keyCode == 40) {
navigateList(e.keyCode == 38 ? "up" : "down");
}
});
input.on("keyup", function(e) {
parseRequest(input.value);
});
});

View file

@ -309,7 +309,24 @@ define([
command.on("session:open-launch", openFromLaunchData);
return {
addFile: addTab
addFile: addTab,
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 });
}
}
});

View file

@ -22,6 +22,19 @@
</div>
</div>
<div class="palette">
<h1 class="mode"></h1>
<input class="request"></input>
<ul class="results"></ul>
</div>
<template id="palette-result">
<li>
<div class="meta"></div>
<div class="detail"></div>
</li>
</template>
<script src="js/ace/ace.js"></script>
<script data-main="./js/main.js" src="require.js"></script>