-
Notifications
You must be signed in to change notification settings - Fork 74
Feature: live-preview from memory on keystroke #101
Description
- I have searched the issues of this repo and believe that this is not a duplicate.
- I have searched the discussions and believe that my question is not already covered.
Feature Request
Overview
I would like to implement the capability to display a live preview of the current typst document that also changes on every keystroke akin to how live preview works in the web app, or, similarly, in VSCode for markdown documents. This is different from the already existing option to export the PDF on every keystroke because
- exporting the PDF on every keystroke writes to the disk on every change, which seems wasteful. Whereas live preview, as described here, is to only read the preview image from memory, which is both faster and does not cause wear on the disk.
- It can be more easily optimized as you could potential only render those images which are currently visible. Also, it should be possible to leverage incremental compilation.
Here's how it could look like and how I implemented it crudely for a proof-of-concept.
Implementation
The way I implemented it for a proof of concept is to expose a command preview
that takes a file URI and returns a base64 encoded string containing the rendered document as PNG. Then, the extension issues a executeCommand("typst-lsp.preview", uri)
on every change to the currently opened editor window. The string is then wrapped in a image tag <img src="data:image/png;base64,..." />
and displayed in a simple webview panel.
Of course, this is far from the best possible implementation. As I do not have much experience with the LSP or with async in Rust, I would be interested to hear your ideas on how to implement this efficiently. With some mentoring, I am confident that I can pull this off :) I am looking forward to your responses!