-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathcoolParameters.js
More file actions
171 lines (154 loc) Β· 5.95 KB
/
coolParameters.js
File metadata and controls
171 lines (154 loc) Β· 5.95 KB
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
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { loadState } from '@nextcloud/initial-state'
const getUIDefaults = () => {
const defaults = loadState('richdocuments', 'uiDefaults', {})
const statusBar = 'true'
const textRuler = 'false'
const sidebar = 'false'
const saveAsMode = 'group'
const uiMode = defaults.UIMode ?? 'notebookbar'
let uiDefaults = 'TextRuler=' + textRuler + ';'
uiDefaults += 'TextSidebar=' + sidebar + ';TextStatusbar=' + statusBar + ';'
uiDefaults += 'PresentationSidebar=' + sidebar + ';PresentationStatusbar=' + statusBar + ';'
uiDefaults += 'SpreadsheetSidebar=' + sidebar + ';SpreadsheetStatusbar=' + statusBar + ';'
uiDefaults += 'UIMode=' + uiMode + ';'
uiDefaults += 'UITheme=' + getUITheme() + ';'
uiDefaults += 'SaveAsMode=' + saveAsMode + ';'
return uiDefaults
}
const getCollaboraTheme = () => {
return loadState('richdocuments', 'theme', 'nextcloud')
}
const getUITheme = () => {
const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
let dataset = {}
try {
dataset = (document.body.dataset.themes ? document.body.dataset : parent?.document.body.dataset) ?? {}
} catch (e) {
// Ignore errors here in case we run into cross-origin domains
}
const nextcloudDarkMode = dataset?.themeDark === '' || dataset?.themeDarkHighcontrast === ''
const matchedDarkMode = (!dataset?.themes || dataset?.themes === '' || dataset?.themeDefault === '') ? systemDarkMode : nextcloudDarkMode
return matchedDarkMode ? 'dark' : 'light'
}
const createDataThemeDiv = (elementType, theme) => {
const element = document.createElement(elementType)
element.setAttribute('id', 'cool-var-source-' + theme)
element.setAttribute('data-theme-' + theme, '')
document.body.appendChild(element)
return element
}
const generateCSSVarTokens = () => {
/* NC versus COOL */
const cssVarMap = {
'--color-primary-element-text': '--co-primary-text',
'--color-primary-element': '--co-primary-element:--co-text-accent',
'--color-primary-element-light': '--co-primary-light:--co-primary-element-light',
'--color-error': '--co-color-error',
'--color-warning': '--co-color-warning',
'--color-success': '--co-color-success',
'--border-radius': '--co-border-radius',
'--border-radius-large': '--co-border-radius-large',
'--color-loading-light': '--co-loading-light',
'--color-loading-dark': '--co-loading-dark',
'--color-box-shadow': '--co-box-shadow',
'--color-border': '--co-border',
'--color-border-dark': '--co-border-dark',
'--border-radius-pill': '--co-border-radius-pill',
}
let str = ''
const lightElement = createDataThemeDiv('div', 'light')
try {
for (const cssVarKey in cssVarMap) {
let cStyle = window.getComputedStyle(lightElement).getPropertyValue(cssVarKey)
if (!cStyle) {
// try suffix -dark instead
cStyle = window.getComputedStyle(lightElement).getPropertyValue(cssVarKey + '-dark')
}
if (!cStyle) continue // skip if it is not set
const varNames = cssVarMap[cssVarKey].split(':')
for (let i = 0; i < varNames.length; ++i) {
str += varNames[i] + '=' + cStyle.trim() + ';'
}
}
} catch (e) {
// Skip extracting css vars if we cannot access parent
}
// New dark mode compatible way to hand over our Nextcloud variables in both light/dark to Collabora
const darkElement = createDataThemeDiv('div', 'dark')
const handover = [
'--color-main-background',
'--color-main-background-rgb',
'--color-main-background-translucent',
'--color-main-background-blur',
'--color-main-text',
'--color-text-maxcontrast',
'--color-box-shadow',
'--color-box-shadow-rgb',
'--default-font-size',
'--border-radius',
'--border-radius-large',
'--border-radius-rounded',
'--border-radius-pill',
'--default-clickable-area',
'--default-line-height',
'--default-grid-baseline',
'--color-primary',
'--color-primary-default',
'--color-primary-text',
'--color-primary-hover',
'--color-primary-light',
'--color-primary-light-text',
'--color-primary-light-hover',
'--color-primary-element',
'--color-primary-element-hover',
'--color-primary-element-text',
'--color-primary-element-light',
'--color-primary-element-light-hover',
'--color-primary-element-light-text',
'--color-primary-element-text-dark',
'--primary-invert-if-bright',
'--background-invert-if-bright',
'--background-invert-if-dark',
]
for (const i in handover) {
const varName = handover[i]
const lightStyle = window.getComputedStyle(lightElement).getPropertyValue(varName)
const darkStyle = window.getComputedStyle(darkElement).getPropertyValue(varName)
if (lightStyle) str += varName.replace('--', '--nc-light-') + '=' + lightStyle.trim() + ';'
if (darkStyle) str += varName.replace('--', '--nc-dark-') + '=' + darkStyle.trim() + ';'
// Workaround for now as we need primary-invert-if-dark which is not available on server yet
if (varName === '--primary-invert-if-bright') {
if (lightStyle.trim() === 'no') {
str += '--nc-light-primary-invert-if-dark=invert(1);'
str += '--nc-dark-primary-invert-if-dark=invert(1);'
} else {
str += '--nc-light-primary-invert-if-dark=no;'
str += '--nc-dark-primary-invert-if-dark=no;'
}
}
}
// cleanup theme elements after extracting property values
lightElement.remove()
darkElement.remove()
const customLogo = loadState('richdocuments', 'theming-customLogo', false)
if (customLogo) {
str += `--nc-custom-logo=${encodeURIComponent(customLogo)};`
}
const rootEl = document.querySelector(':root')
const logoBgColor = window.getComputedStyle(rootEl).getPropertyValue('--nc-logo-background')
str += '--nc-logo-background=' + (logoBgColor === '' ? 'transparent' : logoBgColor) + ';'
const logoDisplay = window.getComputedStyle(rootEl).getPropertyValue('--nc-logo-display')
if (logoDisplay) {
str += `--nc-logo-display=${logoDisplay};`
}
return str.replace(/["']/g, '\\\'')
}
export {
getUIDefaults,
getCollaboraTheme,
generateCSSVarTokens,
}