1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Regression: Plugin CSS files were no longer being loaded correctly

Fixed regression introduced in 36871d9cb0
This commit is contained in:
Laurent Cozic 2022-11-01 14:07:45 +00:00
parent 1cf121b52c
commit 99a61f1283

View File

@ -7,22 +7,16 @@ import uuid from '@joplin/lib/uuid';
import { reg } from '@joplin/lib/registry';
const loadedPluginIdSet = new Set<string>();
export default function useExternalPlugins(CodeMirror: any, plugins: PluginStates) {
const [options, setOptions] = useState({});
useEffect(() => {
let newOptions = {};
const contentScripts = contentScriptsToCodeMirrorPlugin(plugins);
const addedElements: HTMLElement[] = [];
for (const contentScript of contentScripts) {
try {
if (loadedPluginIdSet.has(contentScript.id)) {
continue;
}
const mod = contentScript.module;
if (mod.codeMirrorResources) {
@ -58,33 +52,37 @@ export default function useExternalPlugins(CodeMirror: any, plugins: PluginState
if (asset.inline) {
cssStrings.push(asset.text);
} else {
addScript(shim.fsDriver().resolveRelativePathWithinDir(contentScript.assetPath, asset.name), contentScript.id);
addedElements.push(addScript(shim.fsDriver().resolveRelativePathWithinDir(contentScript.assetPath, asset.name), contentScript.id));
}
}
if (cssStrings.length > 0) {
addInlineCss(cssStrings, contentScript.id);
addedElements.push(addInlineCss(cssStrings, contentScript.id));
}
}
if (mod.plugin) {
mod.plugin(CodeMirror);
}
loadedPluginIdSet.add(contentScript.id);
} catch (error) {
reg.logger().error(error.toString());
}
}
setOptions(newOptions);
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
}, [plugins]);
return () => {
for (const element of addedElements) {
element.remove();
}
};
}, [plugins, CodeMirror]);
function addInlineCss(cssStrings: string[], id: string) {
const element = document.createElement('style');
element.setAttribute('id', `content-script-${id}-inline-${uuid.createNano()}`);
document.head.appendChild(element);
element.appendChild(document.createTextNode(cssStrings.join('\n')));
return element;
}
function addScript(path: string, id: string) {
@ -93,6 +91,7 @@ export default function useExternalPlugins(CodeMirror: any, plugins: PluginState
element.setAttribute('rel', 'stylesheet');
element.setAttribute('href', path);
document.head.appendChild(element);
return element;
}
return options;