Branchwell

Textley / website / assets / site.js

1// Working previews on the home page: the editor window and the agent session.
2// Each control sets a data attribute; the stylesheet does the rest.
3(function () {
4 function press(group, button) {
5 group.forEach(function (b) { b.setAttribute('aria-pressed', String(b === button)); });
6 }
7
8 var win = document.querySelector('.win');
9 if (win) {
10 document.querySelectorAll('[data-open]').forEach(function (button) {
11 button.addEventListener('click', function () { win.dataset.file = button.dataset.open; });
12 });
13 var views = Array.prototype.slice.call(document.querySelectorAll('[data-view-to]'));
14 views.forEach(function (button) {
15 button.addEventListener('click', function () {
16 win.dataset.view = button.dataset.viewTo;
17 press(views, button);
18 });
19 });
20 var looks = Array.prototype.slice.call(document.querySelectorAll('[data-look]'));
21 looks.forEach(function (button) {
22 button.addEventListener('click', function () {
23 win.dataset.appearance = button.dataset.look;
24 press(looks, button);
25 });
26 });
27 // ⌥⌘P switches Markdown and Preview, as it does in the app.
28 document.addEventListener('keydown', function (event) {
29 if (event.altKey && event.metaKey && event.code === 'KeyP' && win.dataset.file === 'log') {
30 event.preventDefault();
31 var next = win.dataset.view === 'source' ? 'preview' : 'source';
32 win.dataset.view = next;
33 press(views, views.filter(function (b) { return b.dataset.viewTo === next; })[0]);
34 }
35 });
36 }
37
38 var session = document.querySelector('.session');
39 if (session) {
40 var modes = Array.prototype.slice.call(document.querySelectorAll('[data-mode-to]'));
41 modes.forEach(function (button) {
42 button.addEventListener('click', function () {
43 session.dataset.mode = button.dataset.modeTo;
44 press(modes, button);
45 });
46 });
47 }
48})();