mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Desktop: Fixes #3449: Fixed style caching
This commit is contained in:
parent
b770ffda4d
commit
7f1c25793a
@ -2,7 +2,7 @@ import { NoteBodyEditorProps } from '../../../utils/types';
|
||||
const { buildStyle } = require('lib/theme');
|
||||
|
||||
export default function styles(props: NoteBodyEditorProps) {
|
||||
return buildStyle('AceEditor', props.theme, (theme: any) => {
|
||||
return buildStyle('CodeMirror', props.theme, (theme: any) => {
|
||||
return {
|
||||
root: {
|
||||
position: 'relative',
|
||||
|
@ -336,19 +336,31 @@ function themeStyle(theme) {
|
||||
return themeCache_[cacheKey];
|
||||
}
|
||||
|
||||
const cachedStyles_ = {};
|
||||
const cachedStyles_ = {
|
||||
themeId: null,
|
||||
styles: {},
|
||||
};
|
||||
|
||||
// cacheKey must be a globally unique key, and must change whenever
|
||||
// the dependencies of the style change. If the style depends only
|
||||
// on the theme, a static string can be provided as a cache key.
|
||||
function buildStyle(cacheKey, themeId, callback) {
|
||||
if (cachedStyles_[cacheKey]) cachedStyles_[cacheKey].style;
|
||||
// We clear the cache whenever switching themes
|
||||
if (cachedStyles_.themeId !== themeId) {
|
||||
cachedStyles_.themeId = themeId;
|
||||
cachedStyles_.styles = {};
|
||||
}
|
||||
|
||||
if (cachedStyles_.styles[cacheKey]) return cachedStyles_.styles[cacheKey].style;
|
||||
|
||||
const s = callback(themeStyle(themeId));
|
||||
|
||||
cachedStyles_[cacheKey] = {
|
||||
cachedStyles_.styles[cacheKey] = {
|
||||
style: s,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
return cachedStyles_[cacheKey].style;
|
||||
return cachedStyles_.styles[cacheKey].style;
|
||||
}
|
||||
|
||||
module.exports = { themeStyle, buildStyle, themeById };
|
||||
|
Loading…
Reference in New Issue
Block a user