Files: 2b05be8ee023fdd9580d6a845ba074a425756685 / app / html / settings / language.js
2387 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, watch, computed } = require('mutant') |
3 | |
4 | exports.gives = nest({ |
5 | 'app.html.settings': true |
6 | }) |
7 | |
8 | exports.needs = nest({ |
9 | 'app.html.settings': 'map', |
10 | 'settings.obs.get': 'first', |
11 | 'settings.sync.set': 'first' |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | return nest({ |
16 | 'app.html.settings': locale |
17 | }) |
18 | |
19 | function locale () { |
20 | const state = { |
21 | locale: api.settings.obs.get('patchbay.localeCode'), |
22 | nextLocale: Value(), |
23 | processing: Value(false), |
24 | success: Value(null) |
25 | } |
26 | |
27 | if (window.spellCheckHandler.currentSpellcheckerLanguage) { |
28 | state.nextLocale.set(getCurrentLocale()) |
29 | } |
30 | |
31 | watch(state.locale, code => { |
32 | if (!window.spellCheckHandler) return console.error('spellchecker not installed') |
33 | if (!code) return state.locale.set(getCurrentLocale()) |
34 | if (code === getCurrentLocale()) return |
35 | |
36 | state.processing.set(true) |
37 | |
38 | window.spellCheckHandler.switchLanguage(code) |
39 | .then(() => { |
40 | state.processing.set(false) |
41 | const currentLocale = getCurrentLocale() |
42 | |
43 | if (currentLocale === code) { |
44 | state.success.set(true) |
45 | } else { |
46 | state.success.set(false) |
47 | state.nextLocale.set(currentLocale) |
48 | state.locale.set(currentLocale) |
49 | } |
50 | }) |
51 | .catch(err => { |
52 | state.processing.set(false) |
53 | console.error(err) |
54 | }) |
55 | }) |
56 | |
57 | return { |
58 | title: 'Language', |
59 | body: h('Language', [ |
60 | h('p', 'This is only used for the spell checker currently. You have to use a valid language code (e.g. en-GB), but invalid guesses will be fixed!'), |
61 | h('div', [ |
62 | h('input', { |
63 | value: state.nextLocale, |
64 | 'ev-input': ev => { |
65 | state.success.set(null) |
66 | state.nextLocale.set(ev.target.value.trim()) |
67 | } |
68 | }), |
69 | computed([state.processing, state.success], (processing, success) => { |
70 | if (processing) return h('i.fa.fa-spinner.fa-pulse') |
71 | |
72 | if (success) return h('i.fa.fa-check') |
73 | |
74 | return h('button', |
75 | { 'ev-click': () => state.locale.set(state.nextLocale()) }, |
76 | 'set' |
77 | ) |
78 | }) |
79 | ]) |
80 | ]) |
81 | } |
82 | } |
83 | } |
84 | |
85 | function getCurrentLocale () { |
86 | return window.spellCheckHandler.currentSpellcheckerLanguage |
87 | } |
88 |
Built with git-ssb-web