Skip to content

Commit

Permalink
added save/toggle-comment key bindings
Browse files Browse the repository at this point in the history
also auto-formatted, too hard to undo :O
  • Loading branch information
jpillora committed Jun 1, 2020
1 parent eb0c40f commit 49b009d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 58 deletions.
1 change: 1 addition & 0 deletions agent/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ <h1 class="title">
<script src="vendor/favico.min.js"></script>
<script src="vendor/angular.min.js"></script>
<script src="vendor/codemirror/lib/codemirror.js"></script>
<script src="vendor/codemirror/addon/comment/comment.js"></script>
<script src="vendor/codemirror/addon/mode/loadmode.js"></script>
<script src="vendor/codemirror/mode/meta.js"></script>
<script src="js/cm.js"></script>
Expand Down
61 changes: 38 additions & 23 deletions agent/static/js/directives.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
app.directive("ago", function() {
var since = (function() {
app.directive("ago", function () {
var since = (function () {
var scale = [
["ms", 1000],
["s", 60],
["m", 60],
["h", 24],
["d", 31],
["mth", 12]
["mth", 12],
];
return function(date) {
return function (date) {
var v = +new Date() - date;
for (var i = 0; i < scale.length; i++) {
var s = scale[i];
Expand All @@ -21,25 +21,25 @@ app.directive("ago", function() {

return {
restrict: "A",
link: function(s, e, attrs) {
link: function (s, e, attrs) {
var d, t;
var check = function() {
var check = function () {
clearTimeout(t);
if (d) e.text(since(d));
t = setTimeout(check, 1000);
};
s.$watch(attrs.ago, function(s) {
s.$watch(attrs.ago, function (s) {
d = new Date(s);
check();
});
}
},
};
});

app.directive("cmContainer", function($rootScope) {
app.directive("cmContainer", function ($rootScope) {
return {
restrict: "C",
link: function(scope, jq, attrs) {
link: function (scope, jq, attrs) {
var elem = jq[0];
var name = attrs.name;
if (!name) {
Expand All @@ -49,31 +49,46 @@ app.directive("cmContainer", function($rootScope) {
if (!api) {
throw "api not there";
}
var opts = angular.extend({ viewportMargin: Infinity }, api.opts);
var mac = /Mac OS X/.test(navigator.userAgent);
var key = mac ? "Cmd" : "Ctrl";
var extraKeys = {};
extraKeys[key + "-S"] = function () {
if (typeof api.onsave === "function") {
api.onsave();
}
};
extraKeys[key + "-/"] = "toggleComment";
var opts = angular.extend(
{
viewportMargin: Infinity,
extraKeys: extraKeys,
},
api.opts
);
var editor = CodeMirror(elem, opts);
window["cm" + name] = api;
//optional handler
if (api.onchange) {
editor.doc.on("change", function() {
editor.doc.on("change", function () {
api.onchange();
});
}
var initialMode = api.mode || null;
//code mirror api
api.set = function(val) {
window.requestAnimationFrame(function() {
api.set = function (val) {
window.requestAnimationFrame(function () {
editor.setValue(val || "");
api.followScroll();
});
};
api.get = function() {
api.get = function () {
return editor.getValue();
};
api.append = function(line) {
api.append = function (line) {
editor.replaceRange(line, CodeMirror.Pos(editor.lastLine()));
api.followScroll();
};
api.mode = function(mode) {
api.mode = function (mode) {
editor.setOption("mode", mode);
CodeMirror.autoLoadMode(editor, mode);
};
Expand All @@ -82,15 +97,15 @@ app.directive("cmContainer", function($rootScope) {
}
if (api.followLock) {
api.following = true;
api.followScroll = function() {
api.followScroll = function () {
if (api.following) {
root.log.editor.doc.setSelection({
line: root.log.editor.doc.lineCount(),
ch: 0
ch: 0,
});
}
};
api.followCheck = function() {
api.followCheck = function () {
var info = editor.getScrollInfo();
var scrollh = elem.clientHeight + info.top;
var p = scrollh / info.height;
Expand All @@ -101,19 +116,19 @@ app.directive("cmContainer", function($rootScope) {

editor.on("scroll", api.followCheck);
} else {
api.followScroll = function() {
api.followScroll = function () {
//noop
};
}

scroll;
var followLock = false;
api.follow = function(f) {
api.follow = function (f) {
follow = f;
//on scroll, detect if bottom, if so following=true
//on append, if following, scroll bottom
};
api.editor = editor;
}
},
};
});
70 changes: 38 additions & 32 deletions agent/static/js/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
app.run(function($rootScope, $http, $timeout, localOpts) {
app.run(function ($rootScope, $http, $timeout, localOpts) {
var s = (window.root = $rootScope);
s.title = "webproc";
//issue a refresh on app load
Expand All @@ -7,33 +7,36 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
s.cfg = {
opts: {
theme: "eclipse",
lineNumbers: true
}
lineNumbers: true,
},
onsave: function () {
s.save();
},
};
s.log = {
mode: "log",
followLock: true,
opts: {
theme: "eclipse",
readOnly: true,
lineWrapping: true
}
lineWrapping: true,
},
};
var inputs = {
show: localOpts("shown", {
out: true,
err: true,
agent: false
agent: false,
}),
file: "",
files: null
files: null,
};
s.inputs = inputs;
//server data
var data = (s.data = {});
//===================================
var currId = 0;
var renderLog = function(delta) {
var renderLog = function (delta) {
if (!data.Log) {
return "";
}
Expand All @@ -56,20 +59,20 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
}
return lines.join("");
};
var renderFull = function() {
var renderFull = function () {
s.log.set(renderLog(false).replace(/\n$/, ""));
};
var renderDelta = function() {
var renderDelta = function () {
s.log.append("\n" + renderLog(true).replace(/\n$/, ""));
};
//===================================
var url = location.pathname.replace(/[^\/]+$/, "") + "sync";
var v = (s.v = velox.sse(url, data));
s.reconnect = function() {
s.reconnect = function () {
v.retry();
};
var id = "";
v.onupdate = function() {
v.onupdate = function () {
if (v.id === id) {
renderDelta();
} else {
Expand All @@ -78,17 +81,17 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
id = v.id;
s.$apply();
};
v.onchange = function(connected) {
v.onchange = function (connected) {
s.connected = connected;
s.$apply();
new Favico({
fontFamily: "Icons",
bgColor: connected ? "#21BA45" : "#DB2828"
bgColor: connected ? "#21BA45" : "#DB2828",
}).badge("\uf0e7");
};
//compare client config against server config
s.saved = true;
var checkSaved = function() {
var checkSaved = function () {
if (!inputs.file || !inputs.files) {
s.saved = true;
return;
Expand All @@ -98,14 +101,14 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
s.saved = client === server;
};
//editor changes
s.cfg.onchange = function() {
s.cfg.onchange = function () {
//cache current
inputs.files[inputs.file] = s.cfg.get();
checkSaved();
s.$apply();
};
//handle changes
s.$watch("data.Config.ProgramArgs", function(args) {
s.$watch("data.Config.ProgramArgs", function (args) {
if (!args) {
return;
}
Expand All @@ -121,7 +124,7 @@ app.run(function($rootScope, $http, $timeout, localOpts) {

s.$watch(
"data.Config.ConfigurationFiles",
function(files) {
function (files) {
//received changes to config files
s.files = files || [];
if (s.files.length === 1 || (s.files.length >= 1 && !inputs.file)) {
Expand All @@ -132,7 +135,7 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
);
s.$watch(
"data.Files",
function(files) {
function (files) {
//apply intial file inputs
if (files && !inputs.files) {
inputs.files = angular.copy(files);
Expand All @@ -141,7 +144,7 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
},
true
);
s.$watch("inputs.file", function(file) {
s.$watch("inputs.file", function (file) {
if (!file) return;
//extensions
var mode = "properties";
Expand All @@ -165,7 +168,7 @@ app.run(function($rootScope, $http, $timeout, localOpts) {

s.$watch(
"inputs.show",
function() {
function () {
if (s.log) {
renderFull();
}
Expand All @@ -174,52 +177,55 @@ app.run(function($rootScope, $http, $timeout, localOpts) {
);

//start/restart
s.start = function() {
s.start = function () {
var alreadyRunning = data.Running;
s.start.ing = true;
s.start.err = null;
$http
.put("restart")
.then(
function() {
function () {
s.start.ed = alreadyRunning ? "Restarted" : "Started";
$timeout(function() {
$timeout(function () {
s.start.ed = false;
}, 3000);
},
function(resp) {
function (resp) {
s.start.err = resp.data;
}
)
.finally(function() {
.finally(function () {
s.start.ing = false;
});
};
//commit change
s.save = function() {
s.save = function () {
if (s.saved || s.save.ed) {
return;
}
s.save.ing = true;
s.save.err = null;
currentFile = {};
currentFile[inputs.file] = inputs.files[inputs.file];
$http
.post("save", currentFile)
.then(
function() {
function () {
s.save.ed = true;
$timeout(function() {
$timeout(function () {
s.save.ed = false;
}, 3000);
},
function(resp) {
function (resp) {
s.save.err = resp.data;
}
)
.finally(function() {
.finally(function () {
s.save.ing = false;
});
};

s.revert = function() {
s.revert = function () {
if (data.Files && inputs.file in data.Files) {
s.cfg.set(data.Files[inputs.file]);
}
Expand Down
8 changes: 5 additions & 3 deletions agent/statik/statik.go

Large diffs are not rendered by default.

0 comments on commit 49b009d

Please sign in to comment.