git ssb

10+

Matt McKegg / patchwork



Tree: 461fd4459a46469a272bb9fe6c369b1dd1c9323c

Files: 461fd4459a46469a272bb9fe6c369b1dd1c9323c / main-window.js

9111 bytesRaw
1var combine = require('depject')
2var entry = require('depject/entry')
3var electron = require('electron')
4var h = require('mutant/h')
5var Value = require('mutant/value')
6var when = require('mutant/when')
7var onceTrue = require('mutant/once-true')
8var computed = require('mutant/computed')
9var catchLinks = require('./lib/catch-links')
10var ObserveLinkHover = require('./lib/observe-link-hover')
11var insertCss = require('insert-css')
12var nest = require('depnest')
13var LatestUpdate = require('./lib/latest-update')
14var ref = require('ssb-ref')
15var setupContextMenuAndSpellCheck = require('./lib/context-menu-and-spellcheck')
16var watch = require('mutant/watch')
17
18module.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 width = 510
154 var maxLeft = window.innerWidth - width
155 var maxTop = window.innerHeight - 100
156 var distanceFromRight = window.innerWidth - rect.right
157 var shouldDisplayBeside = rect.bottom > maxTop || rect.left < 50 || distanceFromRight < 50
158
159 if (shouldDisplayBeside && rect.bottom > 50) {
160 if (rect.right > maxLeft && (rect.left - width) < 0) {
161 // no room, just give up!
162 previewElement.set(null)
163 return
164 } else {
165 preview.style.top = `${Math.min(rect.top, maxTop)}px`
166 if (rect.right > maxLeft) {
167 preview.style.left = `${rect.left - width}px`
168 } else {
169 preview.style.left = `${rect.right + 5}px`
170 }
171 }
172 } else {
173 preview.style.top = `${rect.bottom + 5}px`
174 preview.style.left = `${Math.min(rect.left, maxLeft)}px`
175 }
176
177 previewElement.set(preview)
178 } else if (element !== false) {
179 previewElement.set(null)
180 }
181 })
182
183 catchLinks(container, (href, external, anchor) => {
184 if (external) {
185 electron.shell.openExternal(href)
186 } else if (ref.isBlob(href)) {
187 electron.shell.openExternal(api.blob.sync.url(href))
188 } else if (ref.isMsg(href)) {
189 getExternalHandler(href, (err, handler) => {
190 if (err) throw err
191 if (handler) {
192 handler(href)
193 } else {
194 api.app.navigate(href, anchor)
195 }
196 })
197 } else {
198 api.app.navigate(href, anchor)
199 }
200 })
201
202 return [container, previewElement]
203
204 // scoped
205
206 function getSubscribedChannelMenu () {
207 var channels = Array.from(subscribedChannels()).sort(localeCompare)
208
209 if (channels.length) {
210 return {
211 label: i18n('Channels'),
212 submenu: [
213 { label: i18n('Browse All'),
214 click () {
215 setView('/channels')
216 }
217 },
218 {type: 'separator'}
219 ].concat(channels.map(channel => {
220 return {
221 label: `#${channel}`,
222 click () {
223 setView(`#${channel}`)
224 }
225 }
226 }))
227 }
228 } else {
229 return {
230 label: i18n('Browse Channels'),
231 click () {
232 setView('/channels')
233 }
234 }
235 }
236 }
237
238 function dropTab (title, items) {
239 var element = h('a -drop', {
240 'ev-click': (ev) => {
241 var rects = element.getBoundingClientRect()
242 electron.remote.getCurrentWindow().webContents.getZoomFactor((factor) => {
243 var menu = electron.remote.Menu.buildFromTemplate(items.map(item => {
244 if (typeof item === 'function') {
245 return item()
246 } else if (item.separator) {
247 return { type: 'separator' }
248 } else {
249 return {
250 label: item[0],
251 click () {
252 setView(item[1])
253 }
254 }
255 }
256 }))
257 menu.popup(electron.remote.getCurrentWindow(), {
258 x: Math.round(rects.left * factor),
259 y: Math.round(rects.bottom * factor) + 4,
260 async: true
261 })
262 })
263 }
264 }, title)
265 return element
266 }
267
268 function setView (href, anchor) {
269 currentHover.cancel()
270 previewElement.set(null)
271 views.setView(href, anchor)
272 }
273
274 function getExternalHandler (key, cb) {
275 api.sbot.async.get(key, function (err, value) {
276 if (err) return cb(err)
277 cb(null, api.app.sync.externalHandler({key, value}))
278 })
279 }
280
281 function tab (name, view) {
282 var instance = views.get(view)
283 return h('a', {
284 'ev-click': function (ev) {
285 if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) {
286 instance.reload()
287 }
288 },
289 href: view,
290 classList: [
291 when(selected(view), '-selected')
292 ]
293 }, [
294 name,
295 when(instance.pendingUpdates, [
296 ' (', instance.pendingUpdates, ')'
297 ])
298 ])
299 }
300
301 function selected (view) {
302 return computed([views.currentView, view], (currentView, view) => {
303 return currentView === view
304 })
305 }
306}
307
308function overrideConfig (config) {
309 return {
310 'patchwork/config': {
311 gives: nest('config.sync.load'),
312 create: function (api) {
313 return nest('config.sync.load', () => config)
314 }
315 }
316 }
317}
318
319function addCommand (id, cb) {
320 return {
321 [`patchwork/command/${id}`]: {
322 gives: nest(id),
323 create: function (api) {
324 return nest(id, cb)
325 }
326 }
327 }
328}
329
330function localeCompare (a, b) {
331 return a.localeCompare(b)
332}
333

Built with git-ssb-web