Files: 1441fdeb23ee6ecb5c47ac1013bcecd533a96d62 / file.js
1502 bytesRaw
1 | var fs = require('fs') |
2 | var h = require('hyperscript') |
3 | |
4 | exports.gives = 'command' |
5 | |
6 | exports.create = function (api) { |
7 | return function (file) { |
8 | return function (cb) { |
9 | fs.stat(file, function (err, stat) { |
10 | if(err || !stat.isFile()) |
11 | cb() |
12 | else |
13 | fs.readFile(file, 'utf8', function (err, text) { |
14 | var ta = h('textarea.high', text) |
15 | ta.selectionStart = 0; ta.selectionEnd = 0 |
16 | var status = h('pre.status') |
17 | var wrapper = h('div.column.high', {onfocus: function () { |
18 | ta.focus() |
19 | }}, ta, h('div', status)) |
20 | var timer |
21 | function pos (ev) { |
22 | clearTimeout(timer) |
23 | timer = setTimeout(function () { |
24 | status.textContent = [ta.selectionStart, ta.selectionEnd].join(', ') |
25 | }, 10) |
26 | } |
27 | ta.onkeydown = pos |
28 | pos() |
29 | |
30 | //make element focusable |
31 | wrapper.setAttribute('tabindex', '0') |
32 | |
33 | wrapper.onkeydown = function (ev) { |
34 | console.log(ev.keyCode, ev) |
35 | if(ev.ctrlKey && ev.keyCode == 83) {//ctrl-s, save |
36 | fs.writeFile(file, ta.value, function (err) { |
37 | if(err) status.textContent = err.message |
38 | else status.textContent = 'saved '+file+', '+Buffer.byteLength(ta.value)+' bytes' |
39 | }) |
40 | } |
41 | } |
42 | |
43 | cb(null, wrapper) |
44 | }) |
45 | }) |
46 | } |
47 | } |
48 | } |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 |
Built with git-ssb-web