From 7ffe0518899993cb04644eeb39536de367989cf6 Mon Sep 17 00:00:00 2001 From: Ali Modaresi Date: Tue, 10 Jan 2023 16:41:07 +0100 Subject: [PATCH 1/5] change absolute viewer resources to relative ones --- src/components/server.ts | 2 +- viewer/components/connection.ts | 3 ++- viewer/latexworkshop.ts | 8 ++++---- viewer/viewer.html | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/components/server.ts b/src/components/server.ts index 6a2fc3839..445185560 100644 --- a/src/components/server.ts +++ b/src/components/server.ts @@ -177,7 +177,7 @@ export class Server { } return } - if (request.url === '/config.json') { + if (request.url.includes('config.json')) { const params = lw.viewer.viewerParams() const content = JSON.stringify(params) this.sendOkResponse(response, Buffer.from(content), 'application/json') diff --git a/viewer/components/connection.ts b/viewer/components/connection.ts index 4c54f5392..4f0800722 100644 --- a/viewer/components/connection.ts +++ b/viewer/components/connection.ts @@ -20,7 +20,8 @@ export class WebSocketPort implements IConnectionPort { constructor(lwApp: ILatexWorkshopPdfViewer) { this.lwApp = lwApp const scheme = 'https:' === window.location.protocol ? 'wss' : 'ws' - const server = `${scheme}://${window.location.hostname}:${window.location.port}` + const path = window.location.pathname.substring(0, window.location.pathname.indexOf('viewer.html')) + const server = `${scheme}://${window.location.hostname}:${window.location.port}${path}` this.server = server this.socket = new Promise((resolve, reject) => { const sock = new WebSocket(server) diff --git a/viewer/latexworkshop.ts b/viewer/latexworkshop.ts index ae9e4cee5..f5744bec1 100644 --- a/viewer/latexworkshop.ts +++ b/viewer/latexworkshop.ts @@ -163,7 +163,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer { } private async fetchParams(): Promise { - const response = await fetch('/config.json') + const response = await fetch('config.json') const params = await response.json() as PdfViewerParams return params } @@ -181,7 +181,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer { } private async setupAppOptions() { - const workerPort = new Worker('/build/pdf.worker.js') + const workerPort = new Worker('build/pdf.worker.js') const params = await this.fetchParams() document.addEventListener('webviewerloaded', () => { const color = this.isPrefersColorSchemeDark(params.codeColorTheme) ? params.color.dark : params.color.light @@ -193,7 +193,7 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer { sidebarViewOnLoad: 0, standardFontDataUrl: '/standard_fonts/', workerPort, - workerSrc: '/build/pdf.worker.js', + workerSrc: 'build/pdf.worker.js', forcePageColors: true, ...color } @@ -715,4 +715,4 @@ class LateXWorkshopPdfViewer implements ILatexWorkshopPdfViewer { const extension = new LateXWorkshopPdfViewer() await extension.waitSetupAppOptionsFinished() // @ts-expect-error -await import('/viewer.js') +await import('../../viewer/viewer.js') diff --git a/viewer/viewer.html b/viewer/viewer.html index 280fcb801..343fc2500 100644 --- a/viewer/viewer.html +++ b/viewer/viewer.html @@ -33,9 +33,9 @@ - + - + From e045b772790d6654b077bf3242ded09d8418b677 Mon Sep 17 00:00:00 2001 From: Ali Modaresi Date: Tue, 10 Jan 2023 17:17:08 +0100 Subject: [PATCH 2/5] fix url in open as external link --- src/components/viewer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/viewer.ts b/src/components/viewer.ts index 6ba337e6f..dd8e29b82 100644 --- a/src/components/viewer.ts +++ b/src/components/viewer.ts @@ -57,7 +57,8 @@ export class Viewer { logger.refreshStatus('check', 'statusBar.foreground', `Cannot view file PDF file. File not found: ${pdfFile}`, 'warning') return } - const url = `http://127.0.0.1:${lw.server.port}/viewer.html?file=${PdfFilePathEncoder.encodePathWithPrefix(pdfFile)}` + const baseUri = await vscode.env.asExternalUri(vscode.Uri.parse(`http://127.0.0.1:${lw.server.port}`, true)) + const url = `${baseUri}/viewer.html?file=${PdfFilePathEncoder.encodePathWithPrefix(pdfFile)}` return url } From 368160495a33e7ba4a32655f325ad0cde7522a3e Mon Sep 17 00:00:00 2001 From: Ali Modaresi Date: Tue, 10 Jan 2023 18:07:00 +0100 Subject: [PATCH 3/5] fix url for internal pdf viewer in code-server --- src/components/viewerlib/pdfviewerpanel.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/viewerlib/pdfviewerpanel.ts b/src/components/viewerlib/pdfviewerpanel.ts index 3ba0f01b0..2a3115f85 100644 --- a/src/components/viewerlib/pdfviewerpanel.ts +++ b/src/components/viewerlib/pdfviewerpanel.ts @@ -114,8 +114,8 @@ export class PdfViewerPanelService { static async getPDFViewerContent(pdfFile: vscode.Uri): Promise { const serverPort = lw.server.port // viewer/viewer.js automatically requests the file to server.ts, and server.ts decodes the encoded path of PDF file. - const origUrl = `http://127.0.0.1:${serverPort}/viewer.html?file=${this.encodePathWithPrefix(pdfFile)}` - const url = await vscode.env.asExternalUri(vscode.Uri.parse(origUrl, true)) + const origUrl = await vscode.env.asExternalUri(vscode.Uri.parse(`http://127.0.0.1:${serverPort}`, true)) + const url = vscode.Uri.parse(`${origUrl}/viewer.html?file=${this.encodePathWithPrefix(pdfFile)}`,true) const iframeSrcOrigin = `${url.scheme}://${url.authority}` const iframeSrcUrl = url.toString(true) await this.tweakForCodespaces(url) From 7d2e98ba05e75c84e924acfdeee3fd7610faa283 Mon Sep 17 00:00:00 2001 From: James-Yu Date: Wed, 11 Jan 2023 10:32:58 +0800 Subject: [PATCH 4/5] Dev viewer patch script is modified accordingly --- dev/editviewer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/editviewer.py b/dev/editviewer.py index eeca4c62c..e3b22e307 100644 --- a/dev/editviewer.py +++ b/dev/editviewer.py @@ -12,8 +12,8 @@ fout.write( line.replace('''PDF.js viewer''', '''\n PDF.js viewer''') .replace('''''', '''\n ''') - .replace('''''', '''''') - .replace('''''', '''''') + .replace('''''', '''''') + .replace('''''', '''''') ) with open(args.web + '/viewer.js', 'rt') as fin: From 4fcbed795cea83e6d57150024e66befb6813a068 Mon Sep 17 00:00:00 2001 From: James Yu Date: Mon, 23 Jan 2023 13:36:41 +0800 Subject: [PATCH 5/5] Fix two `/` in viewer url --- src/components/server.ts | 9 ++++++++- src/components/viewer.ts | 13 +++++-------- src/components/viewerlib/pdfviewerpanel.ts | 20 ++++++-------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/components/server.ts b/src/components/server.ts index 445185560..fb09e8d7a 100644 --- a/src/components/server.ts +++ b/src/components/server.ts @@ -69,6 +69,13 @@ export class Server { return portNum } + async getViewerUrl(pdfUri: vscode.Uri): Promise<{url: string, uri: vscode.Uri}> { + // viewer/viewer.js automatically requests the file to server.ts, and server.ts decodes the encoded path of PDF file. + const origUrl = await vscode.env.asExternalUri(vscode.Uri.parse(`http://127.0.0.1:${lw.server.port}`, true)) + const url = origUrl.toString() + (origUrl.toString().endsWith('/') ? '' : '/' ) + `viewer.html?file=${PdfFilePathEncoder.encodePathWithPrefix(pdfUri)}` + return { url, uri: vscode.Uri.parse(url, true) } + } + private get validOrigin(): string { if (this.validOriginUri) { return `${this.validOriginUri.scheme}://${this.validOriginUri.authority}` @@ -177,7 +184,7 @@ export class Server { } return } - if (request.url.includes('config.json')) { + if (request.url.endsWith('/config.json')) { const params = lw.viewer.viewerParams() const content = JSON.stringify(params) this.sendOkResponse(response, Buffer.from(content), 'application/json') diff --git a/src/components/viewer.ts b/src/components/viewer.ts index dd8e29b82..f1158a25c 100644 --- a/src/components/viewer.ts +++ b/src/components/viewer.ts @@ -12,7 +12,6 @@ import { PdfViewerPanelSerializer, PdfViewerPanelService } from './viewerlib/pdf import { PdfViewerManagerService } from './viewerlib/pdfviewermanager' import { ViewerPageLoaded } from './eventbus' import { getLogger } from './logger' -import { PdfFilePathEncoder } from './serverlib/encodepath' import { moveActiveEditor } from '../utils/webview' const logger = getLogger('Viewer') @@ -51,15 +50,13 @@ export class Viewer { } private async checkViewer(sourceFile: string, respectOutDir: boolean = true): Promise { - const pdfFile = this.tex2pdf(sourceFile, respectOutDir) - if (!await lw.lwfs.exists(pdfFile)) { - logger.log(`Cannot find PDF file ${pdfFile}`) - logger.refreshStatus('check', 'statusBar.foreground', `Cannot view file PDF file. File not found: ${pdfFile}`, 'warning') + const pdfUri = this.tex2pdf(sourceFile, respectOutDir) + if (!await lw.lwfs.exists(pdfUri)) { + logger.log(`Cannot find PDF file ${pdfUri.fsPath} .`) + logger.refreshStatus('check', 'statusBar.foreground', `Cannot view file PDF file. File not found: ${pdfUri}`, 'warning') return } - const baseUri = await vscode.env.asExternalUri(vscode.Uri.parse(`http://127.0.0.1:${lw.server.port}`, true)) - const url = `${baseUri}/viewer.html?file=${PdfFilePathEncoder.encodePathWithPrefix(pdfFile)}` - return url + return (await lw.server.getViewerUrl(pdfUri)).url } /** diff --git a/src/components/viewerlib/pdfviewerpanel.ts b/src/components/viewerlib/pdfviewerpanel.ts index 2a3115f85..6ee878be7 100644 --- a/src/components/viewerlib/pdfviewerpanel.ts +++ b/src/components/viewerlib/pdfviewerpanel.ts @@ -6,7 +6,6 @@ import {PdfViewerManagerService} from './pdfviewermanager' import {ViewerStatusChanged} from '../eventbus' import { getLogger } from '../logger' -import { PdfFilePathEncoder } from '../serverlib/encodepath' const logger = getLogger('Viewer', 'Panel') @@ -70,10 +69,6 @@ export class PdfViewerPanelSerializer implements vscode.WebviewPanelSerializer { export class PdfViewerPanelService { private static alreadyOpened = false - private static encodePathWithPrefix(pdfFileUri: vscode.Uri): string { - return PdfFilePathEncoder.encodePathWithPrefix(pdfFileUri) - } - private static async tweakForCodespaces(url: vscode.Uri) { if (this.alreadyOpened) { return @@ -109,16 +104,13 @@ export class PdfViewerPanelService { /** * Returns the HTML content of the internal PDF viewer. * - * @param pdfFile The path of a PDF file to be opened. + * @param pdfUri The path of a PDF file to be opened. */ - static async getPDFViewerContent(pdfFile: vscode.Uri): Promise { - const serverPort = lw.server.port - // viewer/viewer.js automatically requests the file to server.ts, and server.ts decodes the encoded path of PDF file. - const origUrl = await vscode.env.asExternalUri(vscode.Uri.parse(`http://127.0.0.1:${serverPort}`, true)) - const url = vscode.Uri.parse(`${origUrl}/viewer.html?file=${this.encodePathWithPrefix(pdfFile)}`,true) - const iframeSrcOrigin = `${url.scheme}://${url.authority}` - const iframeSrcUrl = url.toString(true) - await this.tweakForCodespaces(url) + static async getPDFViewerContent(pdfUri: vscode.Uri): Promise { + const uri = (await lw.server.getViewerUrl(pdfUri)).uri + const iframeSrcOrigin = `${uri.scheme}://${uri.authority}` + const iframeSrcUrl = uri.toString(true) + await this.tweakForCodespaces(uri) logger.log(`Internal PDF viewer at ${iframeSrcUrl} .`) const rebroadcast: boolean = this.getKeyboardEventConfig() return `