Open mm21 opened 1 year ago
Hello, this is expected to happen when there isn't any code editor in the active context. Is that the case?
I've just committed to master a change with a more reasonable timeout returning null instead of the current behavior.
Thanks for checking. There is a code editor in the context where I'd like to toggle it, which I think is the active context. Here's a screenshot of my current progress:
But maybe I'm misunderstanding the purpose of this API. Ideally I'd like to get a reference to the code editor in my widget's doRender()
, then hide/show it when the toggle button is clicked. Is this use case supported?
If it just needs to be visually hidden, you don't necessarily need the editor instance to do so. You could use css with the right selector (.CodeMirror-wrap
) to directly hide it. I also worked on a project like this, but hiding the editor wasn't necessary in my case https://github.com/rauenzi/Trilium-MarkdownPreview
Where in your code are you calling getActiveContextCodeEditor
?
Thanks for the suggestion. This does work, but then it shows/hides the code editor on other notes as well. I had figured there would be a separate editor object for each note, but now I realize they all share the same DOM element. One problem is if the editor is hidden, the content doesn't seem to get refreshed when switching to another note - when shown again, it shows the content of the previous note.
That's a really great project, thanks for sharing! I'd like to write most of my notes in Markdown so the only thing missing for me would be the ability to hide the editor by default. I have a plan to eventually sync a folder of Markdown files and use a custom renderer for links to other notes and images. (When syncing the folder I'll generate a noteId
from the relative path of the .md/image file, so the renderer can use the same algorithm to lookup the target note/image.)
Where in your code are you calling
getActiveContextCodeEditor
?
I tried calling it in doRender()
(handling it with .then()
) and awaiting it in refreshWithNote()
.
I'd like to write most of my notes in Markdown so the only thing missing for me would be the ability to hide the editor by default.
A markdown-based editor may or may not be a plugin I'm working on in secret 🤫
I tried calling it in
doRender()
(handling it with.then()
) and awaiting it inrefreshWithNote()
.
As far as I know, doRender()
is only called on the initial render and it's up to the widget to update their elements on refreshWithNote
. Without seeing more of the code, it's hard to diagnose exactly what's going on. You can try calling it in the noteSwitch
event, or make sure you're awaiting note.getNoteComplement
in refreshWithNote
. Without awaiting the complement you might get the previous editor.
This seemed to work for me:
async refreshWithNote(note) {
await note.getNoteComplement();
const editor = await api.getActiveContextCodeEditor();
console.log(editor?.doc?.getValue())
}
Thanks for the tips. For some reason it still timed out when awaiting note.getNoteComplement
first in refreshWithNote
. However I was able to get the intended behavior by selecting .note-detail-code
, handling the case where it isn't created yet upon initial load.
Also I found out the editor DOM wasn't get updated when the containing div had display: none
, and this seems to be the expected behavior. Positioning it off the screen allowed it to still get content updates.
The next challenge was making it hidden by default for Markdown code notes while keeping other code types visible, but that was easy enough by implementing noteSwitched
. Here's the complete code for reference:
And the #appCss
note:
Trilium Version
d93e00a
What operating system are you using?
Ubuntu
What is your setup?
Server access only
Operating System Version
Ubuntu 22.04
Description
Hi,
I'm developing a widget to render Markdown code notes. I'd like to toggle the visibility of the code editor, but
getActiveContextCodeEditor()
times out when awaited inrefreshWithNote()
:And when called in
doRender()
it returnsundefined
.Error logs
Backend log: