Files: 55335207126eb15c3a0f1834ffb8bbcbd32658a8 / app / sync / go-to.js
2057 bytesRaw
1 | const nest = require('depnest') |
2 | |
3 | exports.gives = nest({ 'app.sync.goTo': true }) |
4 | |
5 | exports.needs = nest({ |
6 | 'app.html.tabs': 'first', |
7 | 'app.sync.locationId': 'first', |
8 | 'history.obs.store': 'first', |
9 | 'history.sync.push': 'first', |
10 | 'router.async.normalise': 'first', |
11 | 'router.async.router': 'first' |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | return nest('app.sync.goTo', goTo) |
16 | |
17 | // TODO consider rolling single arg: |
18 | // goTo({ ...location, tmp: { openBackground, split, position } }) |
19 | // |
20 | // prune `tmp` before pushing into history |
21 | // allows a refactor of catch-keyboard-shortcut + patch-inbox |
22 | // - extracts scrollToMessage into app.page.thread |
23 | // - router.sync.router would take (location, { position }) ? |
24 | |
25 | function goTo (location, options = {}) { |
26 | if (!location) return |
27 | const { |
28 | openBackground = false, |
29 | split = false |
30 | } = options |
31 | |
32 | const tabs = api.app.html.tabs() |
33 | |
34 | // currently do normalisation here only to generate normalised locationId |
35 | api.router.async.normalise(location, (err, loc) => { |
36 | if (err) return console.error(err) |
37 | |
38 | const locationId = api.app.sync.locationId(loc) |
39 | |
40 | var page = tabs.get(locationId) |
41 | |
42 | if (page) { |
43 | tabs.select(locationId) |
44 | api.history.sync.push(loc) |
45 | |
46 | if (loc.action === 'quote' && page.firstChild && page.firstChild.addQuote) { |
47 | page.firstChild.addQuote(loc.value) |
48 | tabs.currentPage().keyboardScroll('last') |
49 | } else if (loc.action === 'reply') { tabs.currentPage().keyboardScroll('last') } |
50 | |
51 | return true |
52 | } |
53 | |
54 | api.router.async.router(loc, (err, page) => { |
55 | if (err) throw err |
56 | |
57 | if (!page) return |
58 | |
59 | page.id = page.id || locationId |
60 | tabs.add(page, !openBackground, split) |
61 | |
62 | if (openBackground) { |
63 | const history = api.history.obs.store() |
64 | var _history = history() |
65 | var current = _history.pop() |
66 | |
67 | history.set([ ..._history, loc, current ]) |
68 | } else { |
69 | api.history.sync.push(loc) |
70 | } |
71 | }) |
72 | }) |
73 | } |
74 | } |
75 |
Built with git-ssb-web