Files: 35b74ae2d81cf0619ba5864d5875785f83255ef3 / app / page / settings.js
3629 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed } = require('mutant') |
3 | const electron = require('electron') |
4 | const path = require('path') |
5 | |
6 | exports.gives = nest('app.page.settings') |
7 | |
8 | exports.needs = nest({ |
9 | 'about.html.image': 'first', |
10 | 'about.obs.name': 'first', |
11 | 'about.obs.description': 'first', |
12 | 'history.sync.push': 'first', |
13 | 'history.obs.store': 'first', |
14 | 'keys.sync.id': 'first', |
15 | 'message.html.markdown': 'first', |
16 | 'settings.sync.get': 'first', |
17 | 'settings.sync.set': 'first', |
18 | 'settings.obs.get': 'first', |
19 | 'translations.sync.strings': 'first' |
20 | }) |
21 | |
22 | const LANGUAGES = ['zh', 'en'] |
23 | |
24 | // TODO - this needs moving somewhere upstream |
25 | // const DEFAULT_SETTINGS = { |
26 | // onboarded: false, |
27 | // language: 'zh' |
28 | // } |
29 | |
30 | exports.create = (api) => { |
31 | return nest('app.page.settings', settings) |
32 | |
33 | function settings (location) { |
34 | // RESET the app when the settings are changed |
35 | api.settings.obs.get('language')(() => { |
36 | console.log('language changed, resetting view') |
37 | |
38 | // clear history back to start page |
39 | api.history.obs.store().set([ |
40 | { page: 'blogIndex' } |
41 | ]) |
42 | api.history.sync.push({page: 'settings'}) |
43 | }) |
44 | |
45 | const feed = api.keys.sync.id() |
46 | const strings = api.translations.sync.strings() |
47 | const currentLanguage = api.settings.sync.get('language') |
48 | |
49 | const editProfile = () => api.history.sync.push({ |
50 | page: 'userEdit', |
51 | feed, |
52 | callback: (err, didEdit) => { |
53 | if (err) throw new Error('Error editing profile', err) |
54 | api.history.sync.push({ page: 'settings' }) |
55 | } |
56 | }) |
57 | |
58 | return h('Page -settings', [ |
59 | h('div.content', [ |
60 | h('h1', strings.settingsPage.title), |
61 | h('section -avatar', [ |
62 | h('div.left'), |
63 | h('div.right', api.about.html.image(feed)) |
64 | ]), |
65 | h('section -name', [ |
66 | h('div.left', strings.settingsPage.section.name), |
67 | h('div.right', [ |
68 | api.about.obs.name(feed), |
69 | h('img', { |
70 | src: path.join(__dirname, '../../assets', 'edit.png'), |
71 | 'ev-click': editProfile |
72 | }) |
73 | // h('i.fa.fa-pencil', { 'ev-click': editProfile }) |
74 | ]) |
75 | ]), |
76 | h('section -introduction', [ |
77 | h('div.left', strings.settingsPage.section.introduction), |
78 | h('div.right', computed(api.about.obs.description(feed), d => api.message.html.markdown(d || ''))) |
79 | ]), |
80 | h('section -language', [ |
81 | h('div.left', strings.settingsPage.section.language), |
82 | h('div.right', LANGUAGES.map(Language)) |
83 | ]), |
84 | h('section -zoom', [ |
85 | h('div.left', strings.settingsPage.section.zoom), |
86 | h('div.right', [ zoomButton(-0.1, '-'), zoomButton(+0.1, '+') ]) |
87 | ]) |
88 | ]) |
89 | ]) |
90 | |
91 | function Language (lang) { |
92 | const selectLang = () => api.settings.sync.set({ language: lang }) |
93 | const className = currentLanguage === lang ? '-strong' : '' |
94 | |
95 | return h('Button -language', |
96 | { |
97 | 'ev-click': () => selectLang(lang), |
98 | className |
99 | }, |
100 | strings.languages[lang] |
101 | ) |
102 | } |
103 | |
104 | function zoomButton (increment, symbol) { |
105 | const { getCurrentWebContents } = electron.remote |
106 | return h('Button -zoom', |
107 | { |
108 | 'ev-click': () => { |
109 | var zoomFactor = api.settings.sync.get('ticktack.electron.zoomFactor', 1) |
110 | var newZoomFactor = zoomFactor + increment |
111 | var zoomFactor = api.settings.sync.set('ticktack.electron.zoomFactor', newZoomFactor) |
112 | getCurrentWebContents().setZoomFactor(newZoomFactor) |
113 | } |
114 | }, |
115 | symbol |
116 | ) |
117 | } |
118 | } |
119 | } |
120 |
Built with git-ssb-web