Skip to content

Commit 5e4571b

Browse files
committed
decaffeinate views
1 parent 221d71e commit 5e4571b

File tree

2 files changed

+183
-137
lines changed

2 files changed

+183
-137
lines changed

lib/ui/views.coffee

Lines changed: 0 additions & 137 deletions
This file was deleted.

lib/ui/views.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
'use babel'
2+
import * as Highlighter from './highlighter';
3+
import client from '../connection/client';
4+
import { once } from '../misc';
5+
6+
const getlazy = client.import('getlazy');
7+
8+
let views;
9+
export default views = {
10+
dom({tag, attrs, contents}, opts) {
11+
const view = document.createElement(tag);
12+
for (let k in attrs) {
13+
let v = attrs[k];
14+
if (v instanceof Array) { v = v.join(' '); }
15+
view.setAttribute(k, v);
16+
}
17+
if (contents != null) {
18+
if (contents.constructor !== Array) {
19+
contents = [contents];
20+
}
21+
for (let child of contents) {
22+
view.appendChild(this.render(child, opts));
23+
}
24+
}
25+
return view;
26+
},
27+
28+
html(...args) {
29+
const obj = args[0],
30+
{
31+
content
32+
} = obj,
33+
val = obj.block,
34+
block = val != null ? val : false;
35+
let view = this.render(block ? this.tags.div() : this.tags.span());
36+
view.innerHTML = content;
37+
return view = view.children.length === 1 ? view.children[0] : view;
38+
},
39+
40+
tree({head, children, expand}, opts) {
41+
this.ink.tree.treeView(this.render(head, opts),
42+
children.map(x=> this.render(this.tags.div([x]), opts)),
43+
{expand});
44+
},
45+
46+
lazy({head, id}, opts) {
47+
const conn = client.conn;
48+
if (opts.registerLazy != null) {
49+
opts.registerLazy(id);
50+
} else {
51+
console.warn('Unregistered lazy view');
52+
}
53+
let view;
54+
return view = this.ink.tree.treeView(this.render(head, opts), [], {
55+
onToggle: once(() => {
56+
if (client.conn !== conn) { return; }
57+
getlazy(id).then(children => {
58+
const body = view.querySelector(':scope > .body');
59+
children.map(x => this.render(this.tags.div([x]), opts)).forEach(x => {
60+
body.appendChild(this.ink.ansiToHTML(x));
61+
});
62+
});
63+
})
64+
}
65+
);
66+
},
67+
68+
subtree({label, child}, opts) {
69+
return this.render((child.type === "tree" ?{
70+
type: "tree",
71+
head: this.tags.span([label, child.head]),
72+
children: child.children
73+
}
74+
// children: child.children.map((x) => @tags.span "gutted", x)
75+
:
76+
this.tags.span("gutted", [label, child])), opts);
77+
},
78+
79+
copy({view, text}, opts) {
80+
view = this.render(view, opts);
81+
atom.commands.add(view, {
82+
'core:copy'(e) {
83+
atom.clipboard.write(text);
84+
e.stopPropagation();
85+
}
86+
}
87+
);
88+
return view;
89+
},
90+
91+
link({file, line, contents}) {
92+
const view = this.render(this.tags.a({href: '#'}, contents));
93+
// TODO: maybe need to dispose of the tooltip onclick and readd them, but
94+
// that doesn't seem to be necessary
95+
let tt;
96+
if (this.ink.Opener.isUntitled(file)) {
97+
tt = atom.tooltips.add(view, {title() { return 'untitled'; }});
98+
} else {
99+
tt = atom.tooltips.add(view, {title() { return file; }});
100+
}
101+
view.onclick = e => {
102+
this.ink.Opener.open(file, line, {
103+
pending: atom.config.get('core.allowPendingPaneItems')
104+
});
105+
e.stopPropagation();
106+
};
107+
view.addEventListener('DOMNodeRemovedFromDocument', () => {
108+
tt.dispose();
109+
});
110+
return view;
111+
},
112+
113+
number({value, full}) {
114+
let rounded = value.toPrecision(3);
115+
if (rounded.toString().length < full.length) { rounded += '…'; }
116+
const view = this.render(this.tags.span('syntax--constant syntax--numeric', rounded));
117+
let isfull = false;
118+
view.onclick = function(e) {
119+
view.innerText = !isfull ? full : rounded;
120+
isfull = !isfull;
121+
e.stopPropagation();
122+
};
123+
return view;
124+
},
125+
126+
code({text, attrs, scope}) {
127+
const grammar = atom.grammars.grammarForScopeName("source.julia");
128+
const block = (attrs != null ? attrs.block : undefined) || false; // attrs?.block || false
129+
const highlighted = Highlighter.highlight(text, grammar, {scopePrefix: 'syntax--', block});
130+
return this.render({type: 'html', block, content: highlighted});
131+
},
132+
133+
latex({attrs, text}) {
134+
const block = (attrs != null ? attrs.block : undefined) || false; // attrs?.block || false
135+
const latex = this.ink.KaTeX.texify(text, block);
136+
return this.render({type: 'html', block, content: latex});
137+
},
138+
139+
views: {
140+
// TODO Remove unnecessary use of Array.from
141+
dom(...a) { return views.dom(...Array.from(a || [])); },
142+
html(...a) { return views.html(...Array.from(a || [])); },
143+
tree(...a) { return views.tree(...Array.from(a || [])); },
144+
lazy(...a) { return views.lazy(...Array.from(a || [])); },
145+
subtree(...a) { return views.subtree(...Array.from(a || [])); },
146+
link(...a) { return views.link(...Array.from(a || [])); },
147+
copy(...a) { return views.copy(...Array.from(a || [])); },
148+
number(...a) { return views.number(...Array.from(a || [])); },
149+
code(...a) { return views.code(...Array.from(a || [])); },
150+
latex(...a) { return views.latex(...Array.from(a || [])); }
151+
},
152+
153+
render(data, opts = {}) {
154+
if (this.views.hasOwnProperty(data.type)) {
155+
const r = this.views[data.type](data, opts);
156+
this.ink.ansiToHTML(r);
157+
return r;
158+
} else if ((data != null ? data.constructor : undefined) === String) { // data?.constructor === String
159+
return new Text(data);
160+
} else {
161+
return this.render(`julia-client: can't render ${(data != null ? data.type : undefined)}`); // data?.type
162+
}
163+
},
164+
165+
tag(tag, attrs, contents) {
166+
if ((attrs != null ? attrs.constructor : undefined) === String) { // attrs?.constructor === String
167+
attrs = {class: attrs};
168+
}
169+
if ((attrs != null ? attrs.constructor : undefined) !== Object) { // attrs?.constructor !== Object
170+
[contents, attrs] = [attrs, undefined];
171+
}
172+
return {
173+
type: 'dom',
174+
tag,
175+
attrs,
176+
contents
177+
};
178+
},
179+
180+
tags: {}
181+
};
182+
183+
['div', 'span', 'a', 'strong', 'table', 'tr', 'td', 'webview'].forEach(tag => views.tags[tag] = (attrs, contents) => views.tag(tag, attrs, contents));

0 commit comments

Comments
 (0)