Repository navigation
Force update on Markdown preview webview visible - #150150
Song Xie (aifreedom) wants to merge 2 commits into
Conversation
|
|
||
| this._register(this._webviewPanel.onDidChangeViewState(event => { | ||
| if (event.webviewPanel.visible) { | ||
| this.refresh(true); |
There was a problem hiding this comment.
This fixes the problem but two issues:
-
This changes means the preview gets updated every time it is revealed, even if the content has not changed (and the current change also incorrectly refreshes the preview when active state changes)
Instead we should only update the preview if preview has become out of sync with the document content
-
Updating the content on reveal will cause a flash where we see the old content before the new content is loaded.
Not sure of the best approach to fix this but open to ideas
There was a problem hiding this comment.
Matt Bierner (@mjbvz) Thanks for the comments. I also agree that the fix is far from perfect.
I think I'll need to understand the webview better to find a better approach.
My understanding of the issue is that when the Markdown file is edited, the preview webview's content is updated by javascript. However, the HTML source is not updated in this process.
vscode/extensions/markdown-language-features/src/preview/preview.ts
Lines 389 to 393 in 993c36c
But when the webview panel disappears and reappears, the changes made by JavaScript are not kept. It's like after a webpage's content gets modified by javascript, a refresh in the browser will bring back the original state.
One approach I have in mind is to have a flag to indicate that what's currently shown in the webview is different from the HTML. And the next time when it's loaded, it knows a hard refresh is needed. Or, if we do the hard refresh when the webview disappears and needs to be refreshed, the updated HTML can avoid the flash on reveal.
|
Use retainContextWhenHidden to persist state when the webview disappears and reappears. It fixes the flash and avoids unnecessary refreshes. But it does need more memory according to the doc. Preview.test.md.-.aifreedom.-.Code.-.OSS.Dev.2022-05-23.16-46-30.mp4 |
|
I don't think we should add |
|
The root cause is the webview's states don't get persisted when the webview is hidden. Because the preview webview directly receives the rendered HTML and shows it, the state of it would be the rendered HTML itself. Sure, you can implement |
|
You didn't discuss the scenario of hiding the preview tab, then modifying the md file, then showing the tab again. Serialization won't help with this case since it will become out of date while the preview tab is hidden. |
This PR fixes #147718
The cause of the issue seems to be that when the preview webview becomes visible from invisible, the content is initialized using the webview's
html. But when the webview's content is updated, it's updated using JavaScript in the webview, which doesn't update thehtmlproperty.The fix forces the webview to refresh when the webview becomes visible. Honestly, it's not the most elegant fix. I'm open to suggestions on how to fix the root cause better.
Test
test.md.-.aifreedom.-.Code.-.OSS.Dev.2022-05-22.21-24-24.mp4