James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.47k stars 519 forks source link

Bugfix: Synctex does not work while using vscode in browser #4220

Closed wasalm closed 4 months ago

wasalm commented 4 months ago

Although code-server is not officially supported, hereby a bugfix making (ctrl) + click work in the browser.

Namely, before synctex is enabled, it waits before PDF.js is loaded. PDF.js signals this by sending a custom "webviewloaded" event. It first tries to send it to the parent window and if this fails, it send it to its own window. in code-server, this parent exists as a iframe, hence this event is where there are no listeners. Issue is fixed by added a listener to the parent window if possible.

James-Yu commented 4 months ago

This patch will add two listeners to the event if the webpage is not in code-server. parent.document exists even in such environments.

wasalm commented 4 months ago

Agree, it will create two listeners. However, I don't think this will create an issue because

  1. the event is only fired once in viewer.mjs between the lines 14884 and 14896. It is either fired in parent.document or document itself.
  2. in the hypothetical case it would fire multiple times, the promise webViewerLoaded in latexworkshop.ts will only be resolved once, because any second call to resolve() will be ignored.
  3. This event will only be called once, and hence this extra overhead will not be significant.
James-Yu commented 4 months ago

What will be the value of document in a code-server env? If undefined, document ?? parent.document should work well.

wasalm commented 4 months ago

Also it is good to understand why the origional code does work in vscode. The event "webviewerloaded" is only created in viewer.mjs. Here it tries to dispatch this event first in parent.document and if it fails it dispatches it in document itself.

The listener for the "webviewerloaded" event is in the file latexworkshop.ts. It shares the same document with viewer.mjs, because viewer.mjs gets loaded by latexworkshop.ts at line 824. Hence in the origional version, the code will only work if it gets dispatched in document and not in parent.document.

For the code-server the parent document and document are on the same origin and hence it is allowed to dispatch events in the parents. I expect that in vscode this will be different and hence when it tries to dispatch in parent, it will give an error.

wasalm commented 4 months ago

What will be the value of document in a code-server env? If undefined, document ?? parent.document should work well.

document and parent.document are both well-defined objects, so document ?? parent.document will not work. the root issue is that pdf.js might send the "webviewerloaded" event to a different iframe than latexworkshop.ts is expecting