Files: a97b2ac07374b443c84bbd1f8d2c42f40456a4bb / main-window.js
8897 bytesRaw
1 | var combine = require('depject') |
2 | var entry = require('depject/entry') |
3 | var electron = require('electron') |
4 | var h = require('mutant/h') |
5 | var Value = require('mutant/value') |
6 | var when = require('mutant/when') |
7 | var onceTrue = require('mutant/once-true') |
8 | var computed = require('mutant/computed') |
9 | var catchLinks = require('./lib/catch-links') |
10 | var ObserveLinkHover = require('./lib/observe-link-hover') |
11 | var insertCss = require('insert-css') |
12 | var nest = require('depnest') |
13 | var LatestUpdate = require('./lib/latest-update') |
14 | var ref = require('ssb-ref') |
15 | var setupContextMenuAndSpellCheck = require('./lib/context-menu-and-spellcheck') |
16 | var watch = require('mutant/watch') |
17 | |
18 | module.exports = function (config) { |
19 | var sockets = combine( |
20 | overrideConfig(config), |
21 | addCommand('app.navigate', setView), |
22 | require('./modules'), |
23 | require('./plugs'), |
24 | require('patch-settings'), |
25 | require('patchcore'), |
26 | require('./overrides') |
27 | ) |
28 | |
29 | var api = entry(sockets, nest({ |
30 | 'config.sync.load': 'first', |
31 | 'keys.sync.id': 'first', |
32 | 'sbot.obs.connection': 'first', |
33 | 'sbot.async.get': 'first', |
34 | 'blob.sync.url': 'first', |
35 | 'page.html.render': 'first', |
36 | 'app.html.search': 'first', |
37 | 'app.html.channels': 'first', |
38 | 'app.views': 'first', |
39 | 'app.sync.externalHandler': 'first', |
40 | 'app.html.progressNotifier': 'first', |
41 | 'profile.sheet.edit': 'first', |
42 | 'profile.html.preview': 'first', |
43 | 'app.navigate': 'first', |
44 | 'channel.obs.subscribed': 'first', |
45 | 'settings.obs.get': 'first', |
46 | 'intl.sync.i18n': 'first', |
47 | })) |
48 | |
49 | setupContextMenuAndSpellCheck(api.config.sync.load()) |
50 | |
51 | const i18n = api.intl.sync.i18n |
52 | |
53 | var id = api.keys.sync.id() |
54 | var latestUpdate = LatestUpdate() |
55 | var subscribedChannels = api.channel.obs.subscribed(id) |
56 | |
57 | // prompt to setup profile on first use |
58 | onceTrue(api.sbot.obs.connection, (sbot) => { |
59 | sbot.latestSequence(sbot.id, (_, key) => { |
60 | if (key == null) { |
61 | api.profile.sheet.edit() |
62 | } |
63 | }) |
64 | }) |
65 | |
66 | var views = api.app.views(api.page.html.render, [ |
67 | '/public', '/private', id, '/mentions' |
68 | ]) |
69 | |
70 | var pendingCount = views.get('/mentions').pendingUpdates |
71 | |
72 | watch(pendingCount, count => { |
73 | electron.remote.app.setBadgeCount(count) |
74 | }) |
75 | |
76 | |
77 | watch(api.settings.obs.get('patchwork.theme', 'light'), name => { |
78 | Array.from(document.head.children) |
79 | .filter(c => c.tagName == 'STYLE') |
80 | .forEach(c => c.innerText = '') |
81 | |
82 | var theme = require('./styles')[name] |
83 | if (!theme) theme = require('./styles')['light'] |
84 | insertCss(theme) |
85 | }) |
86 | |
87 | var container = h(`MainWindow -${process.platform}`, [ |
88 | h('div.top', [ |
89 | h('span.history', [ |
90 | h('a', { |
91 | 'ev-click': views.goBack, |
92 | classList: [ when(views.canGoBack, '-active') ] |
93 | }), |
94 | h('a', { |
95 | 'ev-click': views.goForward, |
96 | classList: [ when(views.canGoForward, '-active') ] |
97 | }) |
98 | ]), |
99 | h('span.nav', [ |
100 | tab(i18n("Public"), '/public'), |
101 | tab(i18n("Private"), '/private'), |
102 | dropTab(i18n('More'), [ |
103 | getSubscribedChannelMenu, |
104 | [i18n('Gatherings'), '/gatherings'], |
105 | [i18n('Extended Network'), '/all'], |
106 | {separator: true}, |
107 | [i18n('Settings'), '/settings'] |
108 | ]) |
109 | ]), |
110 | h('span.appTitle', [ |
111 | h('span.title', i18n("Patchwork")), |
112 | api.app.html.progressNotifier() |
113 | ]), |
114 | h('span', [ api.app.html.search(api.app.navigate) ]), |
115 | h('span.nav', [ |
116 | tab(i18n('Profile'), id), |
117 | tab(i18n('Mentions'), '/mentions') |
118 | ]) |
119 | ]), |
120 | when(latestUpdate, |
121 | h('div.info', [ |
122 | h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [ |
123 | h('strong', ['Patchwork ', latestUpdate, i18n(' has been released.')]), i18n(' Click here to download and view more info!'), |
124 | h('a.ignore', {'ev-click': latestUpdate.ignore}, 'X') |
125 | ]) |
126 | ]) |
127 | ), |
128 | views.html |
129 | ]) |
130 | |
131 | var currentHover = ObserveLinkHover(container, 500) |
132 | var previewElement = Value() |
133 | |
134 | currentHover(element => { |
135 | var href = element && element.getAttribute('href') |
136 | var preview = null |
137 | |
138 | if (href) { |
139 | if (ref.isFeed(href)) { |
140 | preview = api.profile.html.preview(href) |
141 | } else if (href.includes('://')) { |
142 | preview = h('ProfilePreview', [ |
143 | h('section', [ |
144 | h('strong', [i18n('External Link'), ' ๐']), h('br'), |
145 | h('code', href) |
146 | ]) |
147 | ]) |
148 | } |
149 | } |
150 | |
151 | if (preview) { |
152 | var rect = element.getBoundingClientRect() |
153 | var maxLeft = window.innerWidth - 510 |
154 | var maxTop = window.innerHeight - 100 |
155 | var distanceFromRight = window.innerWidth - rect.right |
156 | var shouldDisplayBeside = rect.bottom > maxTop || rect.left < 100 || distanceFromRight < 100 |
157 | |
158 | if (shouldDisplayBeside && rect.bottom > 50) { |
159 | preview.style.top = `${Math.min(rect.top, maxTop)}px` |
160 | if (rect.right > maxLeft) { |
161 | preview.style.left = `${rect.left - 510}px` |
162 | } else { |
163 | preview.style.left = `${rect.right + 5}px` |
164 | } |
165 | } else { |
166 | preview.style.top = `${rect.bottom + 5}px` |
167 | preview.style.left = `${Math.min(rect.left, maxLeft)}px` |
168 | } |
169 | |
170 | previewElement.set(preview) |
171 | } else if (element !== false) { |
172 | previewElement.set(null) |
173 | } |
174 | }) |
175 | |
176 | catchLinks(container, (href, external, anchor) => { |
177 | if (external) { |
178 | electron.shell.openExternal(href) |
179 | } else if (ref.isBlob(href)) { |
180 | electron.shell.openExternal(api.blob.sync.url(href)) |
181 | } else if (ref.isMsg(href)) { |
182 | getExternalHandler(href, (err, handler) => { |
183 | if (err) throw err |
184 | if (handler) { |
185 | handler(href) |
186 | } else { |
187 | api.app.navigate(href, anchor) |
188 | } |
189 | }) |
190 | } else { |
191 | api.app.navigate(href, anchor) |
192 | } |
193 | }) |
194 | |
195 | return [container, previewElement] |
196 | |
197 | // scoped |
198 | |
199 | function getSubscribedChannelMenu () { |
200 | var channels = Array.from(subscribedChannels()).sort(localeCompare) |
201 | |
202 | if (channels.length) { |
203 | return { |
204 | label: i18n('Channels'), |
205 | submenu: [ |
206 | { label: i18n('Browse All'), |
207 | click () { |
208 | setView('/channels') |
209 | } |
210 | }, |
211 | {type: 'separator'} |
212 | ].concat(channels.map(channel => { |
213 | return { |
214 | label: `#${channel}`, |
215 | click () { |
216 | setView(`#${channel}`) |
217 | } |
218 | } |
219 | })) |
220 | } |
221 | } else { |
222 | return { |
223 | label: i18n('Browse Channels'), |
224 | click () { |
225 | setView('/channels') |
226 | } |
227 | } |
228 | } |
229 | } |
230 | |
231 | function dropTab (title, items) { |
232 | var element = h('a -drop', { |
233 | 'ev-click': (ev) => { |
234 | var rects = element.getBoundingClientRect() |
235 | electron.remote.getCurrentWindow().webContents.getZoomFactor((factor) => { |
236 | var menu = electron.remote.Menu.buildFromTemplate(items.map(item => { |
237 | if (typeof item === 'function') { |
238 | return item() |
239 | } else if (item.separator) { |
240 | return { type: 'separator' } |
241 | } else { |
242 | return { |
243 | label: item[0], |
244 | click () { |
245 | setView(item[1]) |
246 | } |
247 | } |
248 | } |
249 | })) |
250 | menu.popup(electron.remote.getCurrentWindow(), { |
251 | x: Math.round(rects.left * factor), |
252 | y: Math.round(rects.bottom * factor) + 4, |
253 | async: true |
254 | }) |
255 | }) |
256 | } |
257 | }, title) |
258 | return element |
259 | } |
260 | |
261 | function setView (href, anchor) { |
262 | currentHover.cancel() |
263 | previewElement.set(null) |
264 | views.setView(href, anchor) |
265 | } |
266 | |
267 | function getExternalHandler (key, cb) { |
268 | api.sbot.async.get(key, function (err, value) { |
269 | if (err) return cb(err) |
270 | cb(null, api.app.sync.externalHandler({key, value})) |
271 | }) |
272 | } |
273 | |
274 | function tab (name, view) { |
275 | var instance = views.get(view) |
276 | return h('a', { |
277 | 'ev-click': function (ev) { |
278 | if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) { |
279 | instance.reload() |
280 | } |
281 | }, |
282 | href: view, |
283 | classList: [ |
284 | when(selected(view), '-selected') |
285 | ] |
286 | }, [ |
287 | name, |
288 | when(instance.pendingUpdates, [ |
289 | ' (', instance.pendingUpdates, ')' |
290 | ]) |
291 | ]) |
292 | } |
293 | |
294 | function selected (view) { |
295 | return computed([views.currentView, view], (currentView, view) => { |
296 | return currentView === view |
297 | }) |
298 | } |
299 | } |
300 | |
301 | function overrideConfig (config) { |
302 | return { |
303 | 'patchwork/config': { |
304 | gives: nest('config.sync.load'), |
305 | create: function (api) { |
306 | return nest('config.sync.load', () => config) |
307 | } |
308 | } |
309 | } |
310 | } |
311 | |
312 | function addCommand (id, cb) { |
313 | return { |
314 | [`patchwork/command/${id}`]: { |
315 | gives: nest(id), |
316 | create: function (api) { |
317 | return nest(id, cb) |
318 | } |
319 | } |
320 | } |
321 | } |
322 | |
323 | function localeCompare (a, b) { |
324 | return a.localeCompare(b) |
325 | } |
326 |
Built with git-ssb-web