The codemirror theme option controls the css class applied to items in the editor. It won't help you here unless you want to get really tricky (but we should be able to find a better solution).
Fortunately, the solution for you problem is pretty simple.
tl;dr: When Joplin exits the editor it re-adds the custom css to the document. This means it takes priority for styling. You can force your font size styling to always take priority by adding !important
.
If you have the time, I have a few cool things to show you.
Inspecting the html/css
When you add css to Joplin via loadChromeCSSFile
it is adding a <style>
tag within the <head>
block of the document. (Look right at the bottom thats your plugin!)
Here it is after pressing the increase font size button a couple times (Looks like there are some duplicates, I'll leave that up to you to diagnose/fix).
Finally, If you leave the note (go to settings) and come back, you'll see that the codemirror styles are added below yours. (This is because the codemirror styles are removed when the editor is closed, and added back when it is opened)
This is what is causing your issue. Your CSS is sustained, it's just that the priority is now lower than the codemirror css. To overcome this you can add !important
to your css which will make your font-size take priority.
You can remove the onChange
and onNoteChange
events because they don't do anything for your plugin (I suspect you added them while trying to fix your issue above).
BTW, I read through your source and it has a lot of code duplication. I would encourage you to review it (after you make these changes) and try to identify where you are duplicating code. I'd like to see it again once you do. This is shaping up to be a very nice plugin!
Extra
CodeMirror refresh
I noticed that you are replacing the entire body of the note in order to get the editor to refresh. That's a neat trick, but ultimately it's just calling into codemirror.refresh()
which is what you need to do after changing the CSS. Joplin provides a command to do this (kinda), using editor.execCommand
.
If you apply it to your code (see below), you'll see that the font size changing is much smoother!
- let newNoteBody = note.body;
- await joplin.commands.execute("textSelectAll");
- await joplin.commands.execute("replaceSelection", newNoteBody);
+ await joplin.commands.execute('editor.execCommand', {
+ name: 'refresh',
+ args: [],
+ });