silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2.04k stars 140 forks source link

FR: Custom editors #913

Open MrMugame opened 3 days ago

MrMugame commented 3 days ago

Custom editors (which would need a good name, maybe processor or manipulator or smth) are editors provided by a plug ,similar to a codeWidget, for specific file types. - Note that the file type is determined by the extension, not by some MIME type. This has the reason that sometimes formats aren’t distinguishable (many text based formats for example). This also makes it easy for the user to know which editor is going to be used with a file. - This would look something like this:

name: excalidraw
functions:
  excalidrawEditor:
    path: ./excalidraw.ts:editor
    editor: [".ecalidraw", ".ecldrw"]

The called method would then look something like this, again similar to codeWidget:

export async function editor(): Promise<{ html: string; script: string }> {
  // return excalidraw editor html/script
}

This should just return a barebones editor without a file yet[^1]. The file will be provided to the iframe via messages, which will then trigger a custom event on the iframe window. The specific events here will be:

Saving is handled via a global function either as an api call or a custom message. All this will integrate into the silverbullet experience pretty seamlessly. The attachment files for which an editor (provided by a plug) exists could just be added to the page navigator with some visual mark (or a custom navigator). [^2] Dialog This would obviously also need the features you already proposed, mainly attachment renaming and attachment deletion. There won’t be attachment creation, this will be handled by the plugs themselves using commands and syscalls (writeAttachment) if needed.

Advantages:

Maybe I'm overlooking something really important here, maybe this idea is limited by performance issues, but I think this could be really powerful, especially for ''hackability"

[^1]: A performance issue here could be the object returned by the editor function as this can grow quite large. There could be something like a getAssetPath(...) function, which gets a path to an asset, which is provided by the server. This way all the libraries and stuff could be in assets which are just fetched from the server inside the iframe with normal <script src="..."/> tags. I don’t want to do any premature optimization tho.

[^2]: At some point this visual mark may also exist for markdown files and the markdown editor could be implemented using the api, I just proposed. This would make the editor replaceable, which I think would be pretty cool in terms of “hackability”. We could then also easily provide the components via a library to build other markup editors.

zefhemel commented 2 days ago

This sounds very cool :)