Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ThemeService.js « Services « js « src - github.com/marius-wieschollek/passwords-webextension.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 105230fcbdfd9079d67d1b4d4f985206bb3a9729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import SettingsService from '@js/Services/SettingsService';
import MessageService from '@js/Services/MessageService';
import SystemService from '@js/Services/SystemService';
import ErrorManager from '@js/Manager/ErrorManager';

class ThemeService {

    get FONT_MAPPING() {
        return {
            default  : '-apple-system, BlinkMacSystemFont, Ubuntu, Calibri, "Helvetica Neue", sans-serif',
            mono     : 'FreeMono, "Courier New", monospace',
            sans     : 'Ubuntu, Calibri, "Helvetica Neue", sans-serif',
            serif    : '"Times New Roman", Numbus, serif',
            light    : '"Comfortaa Light","Lato Light","Corbel Light","Gill Sans Light", sans-serif',
            nextcloud: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
            dyslexic : 'OpenDyslexic, Dyslexie, sans-serif'
        };
    }

    constructor() {
        /** @type {(ThemeRepository|null)} **/
        this._repository = null;
        this._style = null;
    }

    /**
     *
     * @param {ThemeRepository} repository
     */
    init(repository) {
        this._repository = repository;
    }


    async getBadgeIcon() {
        let theme = await this.getCurrentTheme(),
            icon  = theme.getBadgeIcon();

        return await SystemService.getBrowserApi().runtime.getURL(`img/${icon}.svg`);
    }

    async getBadgeTextColor() {
        let theme = await this.getCurrentTheme();

        return theme.getBadgeForegroundColor();
    }

    async getBadgeBackgroundColor() {
        let theme = await this.getCurrentTheme();

        return theme.getBadgeBackgroundColor();
    }

    async apply() {
        let theme = await this.getCurrentTheme();
        this.applyTheme(theme);
    }

    /**
     *
     * @param {Theme} theme
     */
    applyTheme(theme) {
        this._createStyleSheet(
            theme,
            this._applyFont(theme.getFont()),
            this._applyColors(theme.getColors()),
            this._applyVariables(theme.getVariables())
        );
    }

    /**
     *
     * @return {Promise<Theme>}
     */
    async getCurrentTheme() {
        let current = await SettingsService.getValue('theme.current');

        if(this._repository !== null) {
            try {
                return await this._repository.findById(current);
            } catch(e) {
                ErrorManager.logError(e);
                return await this._repository.findById('light');
            }
        }

        let reply = await MessageService.send({type: 'theme.show', payload: current});
        if(reply.getType() === 'theme.item') return reply.getPayload();

        reply = await MessageService.send({type: 'theme.show', payload: 'light'});
        return reply.getPayload();
    }

    /**
     *
     * @param {Object} colors
     * @return {{}}
     * @private
     */
    _applyColors(colors) {
        if(!colors) return {};

        let css = {};
        for(let color in colors) {
            if(!colors.hasOwnProperty(color)) continue;
            css[`--${color}-color`] = colors[color];
        }

        for(let toast of ['info', 'success', 'warning', 'error']) {
            let color = `${toast}-fg`;
            if(colors.hasOwnProperty(color)) {
                css[`--${toast}-hv-color`] = `${colors[color]}40`;
            }
        }

        return css;
    }

    /**
     *
     * @param {Object} font
     * @return {{}}
     * @private
     */
    _applyFont(font) {
        if(!font) return {};

        let css = {};
        if(font.hasOwnProperty('family') && font.family) {
            let mapping = this.FONT_MAPPING;

            if(mapping.hasOwnProperty(font.family)) {
                css['--font-family'] = mapping[font.family];
            } else {
                css['--font-family'] = font.family;
            }
        }

        if(font.hasOwnProperty('size') && font.size) css['--font-size'] = font.size;

        return css;
    }

    /**
     *
     * @param {Object} variables
     * @return {{}}
     * @private
     */
    _applyVariables(variables) {
        if(!variables) return {};

        let css = {};
        for(let variable in variables) {
            if(!variables.hasOwnProperty(variable)) continue;
            css[`--${variable}`] = variables[variable];
        }

        return css;
    }

    /**
     *
     * @param {Theme} theme
     * @param {Object} variables
     * @private
     */
    _createStyleSheet(theme, ...variables) {
        let css = '';
        variables = Object.assign(...variables);
        for(let variable in variables) {
            if(!variables.hasOwnProperty(variable)) continue;

            let value = variables[variable].replace(';', ''),
                key   = variable.replace(';', '');

            css += `${key}: ${value};`;
        }

        css = `:root { ${css} }`;
        if(theme.getStyle()) css = `@import url("/css/themes/${theme.getId()}.css");\n${css}`;

        if(this._style === null) {
            this._style = document.createElement('style');
            this._style.setAttribute('type', 'text/css');
            document.body.appendChild(this._style);
        }

        this._style.innerHTML = css;
    }
}

export default new ThemeService();