forked from SergeyMosin/Appointments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc25icons.js
179 lines (151 loc) · 5.41 KB
/
nc25icons.js
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
#! /usr/local/bin/node
// borrowed from: https://github.com/nextcloud/server/blob/master/core/src/icons.js
/* eslint-disable quote-props */
/* eslint-disable node/no-unpublished-import */
const path = require('path')
const fs = require('fs')
const sass = require('sass')
const outputFile = path.join(__dirname, 'scss', '_icons.css')
const colors = {
dark: '000',
white: 'fff',
yellow: 'FC0',
red: 'e9322d',
orange: 'eca700',
green: '46ba61',
grey: '969696',
}
const variables = {}
const icons = {
'appt-public-page': path.join(__dirname, 'img', 'appt-public-page.svg'),
'appt-public-pages': path.join(__dirname, 'img', 'appt-public-pages.svg'),
'appt-go-back': path.join(__dirname, 'img', 'appt-go-back.svg'),
'appt-calendar-clock': path.join(__dirname, 'img', 'appt-calendar-clock.svg'),
'appt-timeslot-settings': path.join(__dirname, 'img', 'appt-timeslot-settings.svg'),
'appt-calendar': path.join(__dirname, 'img', 'appt-calendar.svg'),
'appt-key': path.join(__dirname, 'img', 'appt-key.svg'),
'sched-mode': path.join(__dirname, 'img', 'sched-mode.svg'),
'sched-mode-wt': path.join(__dirname, 'img', 'sched-mode-wt.svg'),
'appt-reminder': path.join(__dirname, 'img', 'appt-reminder.svg'),
'appt-private-mode-page': path.join(__dirname, 'img', 'appt-private-mode-page.svg'),
// 'add': path.join(__dirname, '../img', 'actions', 'add.svg'),
// 'address': path.join(__dirname, '../img', 'actions', 'address.svg'),
// 'alert-outline': path.join(__dirname, '../img', 'actions', 'alert-outline.svg'),
}
const iconsColor = {}
// use this to define aliases to existing icons
// key is the css selector, value is the variable
const iconsAliases = {}
const colorSvg = function (svg = '', color = '000') {
if (!color.match(/^[0-9a-f]{3,6}$/i)) {
// Prevent not-sane colors from being written into the SVG
console.warn(color, 'does not match the required format')
color = '000'
}
// add fill (fill is not present on black elements)
const fillRe = /<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/gmi
svg = svg.replace(fillRe, '<$1 fill="#' + color + '"/>')
// replace any fill or stroke colors
svg = svg.replace(/stroke="#([a-z0-9]{3,6})"/gmi, 'stroke="#' + color + '"')
svg = svg.replace(/fill="#([a-z0-9]{3,6})"/gmi, 'fill="#' + color + '"')
return svg
}
const generateVariablesAliases = function (invert = false) {
let css = ''
Object.keys(variables).forEach(variable => {
if (variable.indexOf('original-') !== -1) {
let finalVariable = variable.replace('original-', '')
if (invert) {
finalVariable = finalVariable.replace('white', 'tempwhite')
.replace('dark', 'white')
.replace('tempwhite', 'dark')
}
css += `${finalVariable}: var(${variable});`
}
})
return css
}
const formatIcon = function (icon, invert = false) {
const color1 = invert ? 'white' : 'dark'
const color2 = invert ? 'dark' : 'white'
return `
.icon-${icon},
.icon-${icon}-dark {
background-image: var(--icon-${icon}-${color1});
}
.icon-${icon}-white,
.icon-${icon}.icon-white {
background-image: var(--icon-${icon}-${color2});
}`
}
const formatIconColor = function (icon) {
const {color} = iconsColor[icon]
return `
.icon-${icon} {
background-image: var(--icon-${icon}-${color});
}`
}
const formatAlias = function (alias, invert = false) {
let icon = iconsAliases[alias]
if (invert) {
icon = icon.replace('white', 'tempwhite')
.replace('dark', 'white')
.replace('tempwhite', 'dark')
}
return `
.${alias} {
background-image: var(--${icon})
}`
}
let css = ''
Object.keys(icons).forEach(icon => {
const path = icons[icon]
const svg = fs.readFileSync(path, 'utf8')
const darkSvg = colorSvg(svg, '000000')
const whiteSvg = colorSvg(svg, 'ffffff')
// console.log("dark:",darkSvg)
// console.log("white:",whiteSvg)
variables[`--original-icon-${icon}-dark`] = Buffer.from(darkSvg, 'utf-8').toString('base64')
variables[`--original-icon-${icon}-white`] = Buffer.from(whiteSvg, 'utf-8').toString('base64')
})
Object.keys(iconsColor).forEach(icon => {
const {path, color} = iconsColor[icon]
const svg = fs.readFileSync(path, 'utf8')
const coloredSvg = colorSvg(svg, colors[color])
variables[`--icon-${icon}-${color}`] = Buffer.from(coloredSvg, 'utf-8').toString('base64')
})
// ICONS VARIABLES LIST
css += ':root {'
Object.keys(variables).forEach(variable => {
const data = variables[variable]
css += `${variable}: url(data:image/svg+xml;base64,${data});`
})
css += '}'
// DEFAULT THEME
css += 'body {'
css += generateVariablesAliases()
Object.keys(icons).forEach(icon => {
css += formatIcon(icon)
})
Object.keys(iconsColor).forEach(icon => {
css += formatIconColor(icon)
})
Object.keys(iconsAliases).forEach(alias => {
css += formatAlias(alias)
})
css += '}'
// DARK THEME MEDIA QUERY
css += '@media (prefers-color-scheme: dark) { body {'
css += generateVariablesAliases(true)
css += '}}'
// DARK THEME
css += '[data-themes*=light] {'
css += generateVariablesAliases()
css += '}'
// DARK THEME
css += '[data-themes*=dark] {'
css += generateVariablesAliases(true)
css += '}'
// WRITE CSS
// console.log("css:",sass.compileString(css).css)
fs.writeFileSync(outputFile, sass.compileString(css, {style: "expanded"}).css)