-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into meteor-update-2.9
- Loading branch information
Showing
16 changed files
with
221 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { performance } from 'perf_hooks'; | ||
|
||
import { Meteor } from 'meteor/meteor'; | ||
import less from 'less'; | ||
import AutoPrefixerLessPlugin from 'less-plugin-autoprefixer'; | ||
import { Settings } from '@rocket.chat/models'; | ||
import type { ISetting, ISettingColor } from '@rocket.chat/core-typings'; | ||
|
||
import { withDebouncing } from '../../../lib/utils/highOrderFunctions'; | ||
import { settingsRegistry, settings } from '../../settings/server'; | ||
import type { Logger } from '../../logger/server'; | ||
|
||
export class Theme { | ||
private variables: Record< | ||
string, | ||
{ | ||
type: 'font' | 'color'; | ||
value: unknown; | ||
editor?: ISettingColor['editor']; | ||
} | ||
> = {}; | ||
|
||
private customCSS = ''; | ||
|
||
private compileDelayed: () => void = withDebouncing({ wait: 100 })(Meteor.bindEnvironment(this.compile.bind(this))); | ||
|
||
private logger: Logger; | ||
|
||
public constructor({ logger }: { logger: Logger }) { | ||
this.logger = logger; | ||
this.watchSettings(); | ||
} | ||
|
||
private watchSettings() { | ||
settingsRegistry.add('css', ''); | ||
settingsRegistry.addGroup('Layout'); | ||
|
||
settings.watchByRegex(/^theme-./, (key, value) => { | ||
if (key === 'theme-custom-css' && !!value) { | ||
this.customCSS = String(value); | ||
} else { | ||
const name = key.replace(/^theme-[a-z]+-/, ''); | ||
if (this.variables[name]) { | ||
this.variables[name].value = value; | ||
} | ||
} | ||
|
||
this.compileDelayed(); | ||
}); | ||
} | ||
|
||
private compile() { | ||
const options: Less.Options = { | ||
compress: true, | ||
plugins: [new AutoPrefixerLessPlugin()], | ||
}; | ||
|
||
const start = performance.now(); | ||
|
||
less.render(this.customCSS, options, (err, data) => { | ||
this.logger.info({ stop_rendering: performance.now() - start }); | ||
|
||
if (err) { | ||
this.logger.error(err); | ||
return; | ||
} | ||
|
||
Settings.updateValueById('css', data?.css); | ||
|
||
Meteor.startup(() => { | ||
Meteor.setTimeout(() => { | ||
process.emit('message', { refresh: 'client' }); | ||
}, 200); | ||
}); | ||
}); | ||
} | ||
|
||
public addVariable(type: 'font', name: string, value: ISetting['value'], section: ISetting['section'], persist?: boolean): void; | ||
|
||
public addVariable( | ||
type: 'color', | ||
name: string, | ||
value: ISettingColor['value'], | ||
section: ISettingColor['section'], | ||
persist: boolean, | ||
editor: ISettingColor['editor'], | ||
): void; | ||
|
||
public addVariable( | ||
type: 'font' | 'color', | ||
name: string, | ||
value: ISetting['value'], | ||
section: ISetting['section'], | ||
persist = true, | ||
editor?: ISettingColor['editor'], | ||
) { | ||
this.variables[name] = { | ||
type, | ||
value, | ||
editor, | ||
}; | ||
|
||
if (!persist) { | ||
return; | ||
} | ||
|
||
// TODO: this is a hack to make the type checker happy | ||
const config: Partial<ISetting> & { allowedTypes?: ['color', 'expression'] } = { | ||
group: 'Layout', | ||
type, | ||
section, | ||
public: true, | ||
...(type === 'color' && { | ||
editor, | ||
allowedTypes: ['color', 'expression'], | ||
}), | ||
}; | ||
|
||
settingsRegistry.add(`theme-${type}-${name}`, value, config); | ||
} | ||
|
||
public getCss() { | ||
return String(settings.get('css') || ''); | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import crypto from 'crypto'; | ||
|
||
import { WebApp } from 'meteor/webapp'; | ||
|
||
import { settings } from '../../settings/server'; | ||
import { Logger } from '../../logger/server'; | ||
import { addStyle } from '../../ui-master/server/inject'; | ||
import { Theme } from './Theme'; | ||
|
||
const logger = new Logger('rocketchat:theme'); | ||
|
||
export const theme = new Theme({ logger }); | ||
|
||
settings.watch('css', () => { | ||
addStyle('css-theme', theme.getCss()); | ||
process.emit('message', { refresh: 'client' }); | ||
}); | ||
|
||
WebApp.rawConnectHandlers.use((req, res, next) => { | ||
const path = req.url?.split('?')[0]; | ||
const prefix = __meteor_runtime_config__.ROOT_URL_PATH_PREFIX || ''; | ||
|
||
if (path !== `${prefix}/theme.css`) { | ||
next(); | ||
return; | ||
} | ||
|
||
const data = theme.getCss(); | ||
|
||
res.setHeader('Content-Type', 'text/css; charset=UTF-8'); | ||
res.setHeader('Content-Length', data.length); | ||
res.setHeader('ETag', `"${crypto.createHash('sha1').update(data).digest('hex')}"`); | ||
res.end(data, 'utf-8'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.