-
Notifications
You must be signed in to change notification settings - Fork 536
/
viewer.ts
422 lines (403 loc) · 15.7 KB
/
viewer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import * as vscode from 'vscode'
import type ws from 'ws'
import * as path from 'path'
import * as os from 'os'
import * as cs from 'cross-spawn'
import { lw } from '../lw'
import type { SyncTeXRecordToPDF, SyncTeXRecordToPDFAll, ViewerMode } from '../types'
import * as manager from './viewer/pdfviewermanager'
import { populate } from './viewer/pdfviewerpanel'
import type { ClientRequest, PdfViewerParams, PdfViewerState } from '../../types/latex-workshop-protocol-types/index'
import { Client } from './viewer/client'
import { moveActiveEditor } from '../utils/webview'
const logger = lw.log('Viewer')
export {
getParams,
getViewerState,
handler,
isViewing,
locate,
viewInWebviewPanel,
refresh,
view
}
export { serializer } from './viewer/pdfviewerpanel'
export { hook } from './viewer/pdfviewerhook'
lw.watcher.pdf.onChange(pdfUri => {
if (lw.compile.compiledPDFWriting === 0 || path.relative(lw.compile.compiledPDFPath, pdfUri.fsPath) !== '') {
refresh(pdfUri)
}
})
lw.onConfigChange(['view.pdf.toolbar.hide.timeout', 'view.pdf.invert', 'view.pdf.invertMode', 'view.pdf.color', 'view.pdf.internal'], () => {
reload()
})
const isViewing = (fileUri: vscode.Uri) => manager.getClients(fileUri) !== undefined
function reload(): void {
manager.getClients()?.forEach(client => {
client.send({type: 'reload'})
})
}
/**
* Refreshes PDF viewers of `pdfFile`.
*
* @param pdfFile The path of a PDF file. If `pdfFile` is `undefined`,
* refreshes all the PDF viewers.
*/
function refresh(pdfUri?: vscode.Uri): void {
logger.log(`Call refreshExistingViewer: ${pdfUri ?? 'undefined'} .`)
if (pdfUri === undefined) {
manager.getClients()?.forEach(client => {
client.send({type: 'refresh'})
})
return
}
const clientSet = manager.getClients(pdfUri)
if (!clientSet) {
logger.log(`Not found PDF viewers to refresh: ${pdfUri}`)
return
}
logger.log(`Refresh PDF viewer: ${pdfUri}`)
clientSet.forEach(client => {
client.send({type: 'refresh'})
})
}
async function getUrl(pdfUri: vscode.Uri): Promise<string | undefined> {
if (!await lw.file.exists(pdfUri)) {
logger.log(`Cannot find PDF file ${pdfUri}`)
logger.refreshStatus('check', 'statusBar.foreground', `Cannot view file PDF file. File not found: ${pdfUri}`, 'warning')
return
}
return (await lw.server.getUrl(pdfUri)).url
}
async function view(pdfUri: vscode.Uri, mode?: 'tab' | 'browser' | 'external'): Promise<void> {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
const tabEditorGroup = configuration.get('view.pdf.tab.editorGroup') as string
let viewerMode: ViewerMode = mode ?? configuration.get<ViewerMode>('view.pdf.viewer', 'tab')
if (mode === 'tab' && configuration.get<ViewerMode>('view.pdf.viewer', 'tab') === 'legacy') {
viewerMode = 'legacy'
}
if (viewerMode === 'browser') {
return viewInBrowser(pdfUri)
} else if (viewerMode === 'tab') {
return viewInCustomEditor(pdfUri)
} else if (viewerMode === 'legacy' || viewerMode === 'singleton') {
return viewInWebviewPanel(pdfUri, tabEditorGroup, true)
} else if (viewerMode === 'external') {
return viewInExternal(pdfUri)
} else {
return viewInCustomEditor(pdfUri)
}
}
/**
* Opens the PDF uri in the browser.
*
* @param pdfUri The path of a PDF file.
*/
async function viewInBrowser(pdfUri: vscode.Uri): Promise<void> {
const url = await getUrl(pdfUri)
if (!url) {
return
}
manager.create(pdfUri)
lw.watcher.pdf.add(pdfUri)
try {
logger.log(`Serving PDF file at ${url}`)
await vscode.env.openExternal(vscode.Uri.parse(url, true))
logger.log(`Open PDF viewer for ${pdfUri.toString(true)}`)
} catch (e: unknown) {
void vscode.window.showInputBox({
prompt: 'Unable to open browser. Please copy and visit this link.',
value: url
})
logger.logError(`Failed opening PDF viewer for ${pdfUri.toString(true)}`, e)
}
}
async function viewInCustomEditor(pdfUri: vscode.Uri): Promise<void> {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
const editorGroup = configuration.get('view.pdf.tab.editorGroup') as string
const showOptions: vscode.TextDocumentShowOptions = {
viewColumn: vscode.ViewColumn.Active,
preserveFocus: true
}
if (editorGroup === 'left') {
const currentColumn = vscode.window.activeTextEditor?.viewColumn
if (currentColumn && currentColumn > 1) {
showOptions.viewColumn = currentColumn - 1
await vscode.commands.executeCommand('vscode.openWith', pdfUri, 'latex-workshop-pdf-hook', showOptions)
await vscode.commands.executeCommand('workbench.action.focusRightGroup')
} else {
await vscode.commands.executeCommand('vscode.openWith', pdfUri, 'latex-workshop-pdf-hook', showOptions)
await moveActiveEditor('left', true)
}
} else if (editorGroup === 'right') {
const currentColumn = vscode.window.activeTextEditor?.viewColumn
showOptions.viewColumn = (currentColumn ?? 0) + 1
await vscode.commands.executeCommand('vscode.openWith', pdfUri, 'latex-workshop-pdf-hook', showOptions)
await vscode.commands.executeCommand('workbench.action.focusLeftGroup')
} else {
await vscode.commands.executeCommand('vscode.openWith', pdfUri, 'latex-workshop-pdf-hook', showOptions)
await moveActiveEditor(editorGroup, true)
}
logger.log(`Open PDF tab for ${pdfUri.toString(true)}`)
}
async function viewInWebviewPanel(pdfUri: vscode.Uri, tabEditorGroup: string, preserveFocus: boolean): Promise<void> {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
const singleton = configuration.get<ViewerMode>('view.pdf.viewer', 'tab') === 'singleton'
if (singleton) {
const panels = manager.getPanels(pdfUri)
if (panels && panels.size > 0) {
panels.forEach(panel => panel.webviewPanel.reveal(undefined, true))
logger.log(`Reveal the existing PDF tab for ${pdfUri.toString(true)}`)
return
}
}
const activeDocument = vscode.window.activeTextEditor?.document
const webviewPanel = vscode.window.createWebviewPanel('latex-workshop-pdf', path.basename(pdfUri.path), {
viewColumn: vscode.ViewColumn.Active,
preserveFocus: tabEditorGroup === 'current'
}, {
enableScripts: true,
retainContextWhenHidden: true
})
const viewerPanel = await populate(pdfUri, webviewPanel)
manager.insert(viewerPanel)
if (!viewerPanel) {
return
}
if (tabEditorGroup !== 'current' && activeDocument) {
await moveActiveEditor(tabEditorGroup, preserveFocus)
}
logger.log(`Open PDF tab for ${pdfUri.toString(true)}`)
}
/**
* Opens the PDF file of in the external PDF viewer.
*
* @param pdfUri The path of a PDF file.
*/
function viewInExternal(pdfUri: vscode.Uri): void {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
let command = configuration.get('view.pdf.external.viewer.command') as string
let args = configuration.get('view.pdf.external.viewer.args') as string[]
if (!command) {
switch (process.platform) {
case 'win32':
command = 'SumatraPDF.exe'
args = ['%PDF%']
break
case 'linux':
command = 'xdg-open'
args = ['%PDF%']
break
case 'darwin':
command = 'open'
args = ['%PDF%']
break
default:
break
}
}
if (args) {
args = args.map(arg => arg.replace('%PDF%', pdfUri.fsPath))
}
logger.log(`Open external viewer for ${pdfUri.toString(true)}`)
logger.logCommand('Execute the external PDF viewer command', command, args)
const proc = cs.spawn(command, args, {cwd: path.dirname(pdfUri.fsPath), detached: true})
let stdout = ''
proc.stdout.on('data', newStdout => {
stdout += newStdout
})
let stderr = ''
proc.stderr.on('data', newStderr => {
stderr += newStderr
})
const cb = () => {
void logger.log(`The external PDF viewer stdout: ${stdout}`)
void logger.log(`The external PDF viewer stderr: ${stderr}`)
}
proc.on('error', cb)
proc.on('exit', cb)
}
/**
* Handles the request from the internal PDF viewer.
*
* @param websocket The WebSocket connecting with the viewer.
* @param msg A message from the viewer in JSON fromat.
*/
function handler(websocket: ws, msg: string): void {
const data = JSON.parse(msg) as ClientRequest
if (data.type !== 'ping') {
logger.log(`Handle data type: ${data.type}`)
}
switch (data.type) {
case 'open': {
const pdfUri = vscode.Uri.parse(data.pdfFileUri, true)
const clientSet = manager.getClients(pdfUri)
if (clientSet === undefined) {
break
}
const client = new Client(websocket)
clientSet.add(client)
client.onDidDispose(() => {
clientSet.delete(client)
})
break
}
case 'loaded': {
lw.event.fire(lw.event.ViewerPageLoaded)
const configuration = vscode.workspace.getConfiguration('latex-workshop')
if (configuration.get('synctex.afterBuild.enabled') as boolean) {
logger.log('SyncTex after build invoked.')
const uri = vscode.Uri.parse(data.pdfFileUri, true)
lw.locate.synctex.toPDF(uri)
}
break
}
case 'reverse_synctex': {
const uri = vscode.Uri.parse(data.pdfFileUri, true)
void lw.locate.synctex.toTeX(data, uri)
break
}
case 'external_link': {
const uri = vscode.Uri.parse(data.url)
if (['http', 'https'].includes(uri.scheme)) {
void vscode.env.openExternal(uri)
} else {
void vscode.window.showInputBox({
prompt: 'For security reasons, please copy and visit this link manually.',
value: data.url
})
}
break
}
case 'ping': {
// nothing to do
break
}
case 'add_log': {
logger.log(`${data.message}`)
break
}
case 'copy': {
if ((data.isMetaKey && os.platform() === 'darwin') ||
(!data.isMetaKey && os.platform() !== 'darwin')) {
void vscode.env.clipboard.writeText(data.content as string)
}
break
}
default: {
logger.log(`Unknown websocket message: ${msg}`)
break
}
}
}
function getParams(): PdfViewerParams {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
const invertType = configuration.get('view.pdf.invertMode.enabled') as string
const invertEnabled =
(invertType === 'auto' && vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark) ||
invertType === 'always' ||
(invertType === 'compat' && (configuration.get('view.pdf.invert') as number) > 0)
const pack: PdfViewerParams = {
toolbar: configuration.get('view.pdf.toolbar.hide.timeout') as number,
scale: configuration.get('view.pdf.zoom') as string,
trim: configuration.get('view.pdf.trim') as number,
scrollMode: configuration.get('view.pdf.scrollMode') as number,
spreadMode: configuration.get('view.pdf.spreadMode') as number,
hand: configuration.get('view.pdf.hand') as boolean,
invertMode: {
enabled: invertEnabled,
brightness: configuration.get('view.pdf.invertMode.brightness') as number,
grayscale: configuration.get('view.pdf.invertMode.grayscale') as number,
hueRotate: configuration.get('view.pdf.invertMode.hueRotate') as number,
invert: configuration.get('view.pdf.invert') as number,
sepia: configuration.get('view.pdf.invertMode.sepia') as number,
},
color: {
light: {
pageColorsForeground: configuration.get('view.pdf.color.light.pageColorsForeground') || 'CanvasText',
pageColorsBackground: configuration.get('view.pdf.color.light.pageColorsBackground') || 'Canvas',
backgroundColor: configuration.get('view.pdf.color.light.backgroundColor', '#ffffff'),
pageBorderColor: configuration.get('view.pdf.color.light.pageBorderColor', 'lightgrey'),
},
dark: {
pageColorsForeground: configuration.get('view.pdf.color.dark.pageColorsForeground') || 'CanvasText',
pageColorsBackground: configuration.get('view.pdf.color.dark.pageColorsBackground') || 'Canvas',
backgroundColor: configuration.get('view.pdf.color.dark.backgroundColor', '#ffffff'),
pageBorderColor: configuration.get('view.pdf.color.dark.pageBorderColor', 'lightgrey'),
},
},
codeColorTheme: vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Light ? 'light' : 'dark',
keybindings: {
synctex: configuration.get('view.pdf.internal.synctex.keybinding') as 'ctrl-click' | 'double-click',
},
}
return pack
}
/**
* Reveals the position of `record` on the internal PDF viewers.
*
* @param pdfUri The path of a PDF file.
* @param record The position to be revealed.
*/
async function locate(pdfUri: vscode.Uri, record: SyncTeXRecordToPDF | SyncTeXRecordToPDFAll[]): Promise<void> {
let clientSet = manager.getClients(pdfUri)
if (clientSet === undefined || clientSet.size === 0) {
logger.log(`PDF is not opened: ${pdfUri.toString(true)} , try opening.`)
await view(pdfUri)
clientSet = manager.getClients(pdfUri)
}
if (clientSet === undefined || clientSet.size === 0) {
logger.log(`PDF cannot be opened: ${pdfUri.toString(true)} .`)
return
}
const needDelay = showInvisibleWebviewPanel(pdfUri)
for (const client of clientSet) {
setTimeout(() => {
client.send({type: 'synctex', data: record})
}, needDelay ? 200 : 0)
logger.log(`Try to synctex ${pdfUri.toString(true)}`)
}
}
/**
* Reveals the internal PDF viewer of `pdfUri`.
* The first one is revealed.
*
* @param pdfUri The path of a PDF file.
* @returns Returns `true` if `WebviewPanel.reveal` called.
*/
function showInvisibleWebviewPanel(pdfUri: vscode.Uri): boolean {
const panelSet = manager.getPanels(pdfUri)
if (!panelSet) {
return false
}
const activeViewColumn = vscode.window.activeTextEditor?.viewColumn
for (const panel of panelSet) {
const isSyntexOn = !panel.state || panel.state.synctexEnabled
if (panel.webviewPanel.viewColumn !== activeViewColumn
&& !panel.webviewPanel.visible
&& isSyntexOn) {
panel.webviewPanel.reveal(panel.webviewPanel.viewColumn, true)
return true
}
if (panel.webviewPanel.visible && isSyntexOn) {
return false
}
if (panel.webviewPanel.viewColumn !== activeViewColumn) {
return false
}
}
return false
}
/**
* !! Test only
* Returns the state of the internal PDF viewer of `pdfFilePath`.
*
* @param pdfUri The path of a PDF file.
*/
function getViewerState(pdfUri: vscode.Uri): (PdfViewerState | undefined)[] {
const panelSet = manager.getPanels(pdfUri)
if (!panelSet) {
return []
}
return Array.from(panelSet).map( e => e.state )
}