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