1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Desktop: Note background does not change when theme automatically updated via system

This commit is contained in:
Laurent Cozic 2023-02-24 18:00:14 +00:00
parent ec6567c68d
commit d1e545ac2c

View File

@ -154,34 +154,66 @@
setPercentScroll(percentScroll_);
}
// Note that this function keeps track of what's been added so as not to add the same CSS files multiple times
// It also means that once an asset has been added it is never removed from the view, which in many case is
// desirable, but still something to keep in mind.
// Note that this function keeps track of what's been added so as not to
// add the same CSS files multiple times.
function addPluginAssets(assets) {
if (!assets) return;
const pluginAssetsContainer = document.getElementById('joplin-container-pluginAssetsContainer');
const processedAssetIds = [];
for (let i = 0; i < assets.length; i++) {
const asset = assets[i];
// # and ? can be used in valid paths and shouldn't be treated as the start of a query or fragment
const encodedPath = asset.path
.replaceAll('#','%23')
.replaceAll('?','%3F')
const assetId = asset.name ? asset.name : encodedPath;
processedAssetIds.push(assetId);
if (pluginAssetsAdded_[assetId]) continue;
pluginAssetsAdded_[assetId] = true;
let element = null;
if (asset.mime === 'application/javascript') {
const script = document.createElement('script');
script.src = encodedPath;
pluginAssetsContainer.appendChild(script);
element = document.createElement('script');
element.src = encodedPath;
pluginAssetsContainer.appendChild(element);
} else if (asset.mime === 'text/css') {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = encodedPath
pluginAssetsContainer.appendChild(link);
element = document.createElement('link');
element.rel = 'stylesheet';
element.href = encodedPath
pluginAssetsContainer.appendChild(element);
}
pluginAssetsAdded_[assetId] = {
element,
}
}
// Once we have added the relevant assets, we also remove those that
// are no longer needed. It's necessary in particular for the CSS
// generated by noteStyle - if we don't remove it, we might end up
// with two or more stylesheet and that will create conflicts.
//
// It was happening for example when automatically switching from
// light to dark theme, and then back to light theme - in that case
// the viewer would remain dark because it would use the dark
// stylesheet that would still be in the DOM.
for (const [assetId, asset] of Object.entries(pluginAssetsAdded_)) {
if (!processedAssetIds.includes(assetId)) {
try {
asset.element.remove();
} catch (error) {
// We don't throw an exception but we log it since
// it shouldn't happen
console.warn('Tried to remove an asset but got an error', error);
}
pluginAssetsAdded_[assetId] = null;
}
}
}