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