Skip to content

Commit a3dc8ae

Browse files
committed
fix #8444 -- support latex and pdf viewing on compute servers
- this was pretty simple and probably fixed other issues involving things other than jupyter/latex on a compute server. - obviously won't work without user actually installing latex, which is I think not installed on any compute server images
1 parent b320864 commit a3dc8ae

File tree

8 files changed

+54
-17
lines changed

8 files changed

+54
-17
lines changed

src/packages/frontend/file-associations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ file_associations["tex"] = {
216216
icon: "tex-file",
217217
opts: { mode: "stex2", indent_unit: 2, tab_size: 2, spellcheck: true },
218218
name: "LaTeX",
219-
exclude_from_compute_server: true,
219+
exclude_from_compute_server: false,
220220
ext: "tex",
221221
};
222222

src/packages/frontend/frame-editors/frame-tree/util.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Utility functions useful for frame-tree editors.
99

1010
import { path_split, separate_file_extension } from "@cocalc/util/misc";
1111
import { fileURL } from "@cocalc/frontend/lib/cocalc-urls";
12+
import { getComputeServerId } from "@cocalc/frontend/frame-editors/generic/client";
1213

1314
export function parse_path(path: string): {
1415
directory: string;
@@ -24,6 +25,13 @@ export function raw_url(
2425
project_id: string,
2526
path: string,
2627
compute_server_id?: number,
28+
param?: string,
2729
): string {
28-
return fileURL({ project_id, path, compute_server_id });
30+
return fileURL({
31+
project_id,
32+
path,
33+
compute_server_id:
34+
compute_server_id ?? getComputeServerId({ project_id, path }),
35+
param,
36+
});
2937
}

src/packages/frontend/frame-editors/generic/client.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ export function server_time(): Date {
2929
return webapp_client.time_client.server_time();
3030
}
3131

32+
export function getComputeServerId({
33+
project_id,
34+
path,
35+
}: {
36+
project_id: string;
37+
path: string;
38+
}) {
39+
let compute_server_id =
40+
redux.getProjectActions(project_id).getComputeServerIdForFile({ path }) ??
41+
0;
42+
if (compute_server_id && excludeFromComputeServer(path)) {
43+
compute_server_id = 0;
44+
}
45+
return compute_server_id;
46+
}
47+
3248
// async version of the webapp_client exec -- let's you run any code in a project!
3349
// If the second argument filePath is the file this is being used for as a second argument,
3450
// it always runs code on the compute server that the given file is on.
@@ -37,13 +53,10 @@ export async function exec(
3753
filePath?: string,
3854
): Promise<ExecOutput> {
3955
if (filePath) {
40-
let compute_server_id =
41-
redux
42-
.getProjectActions(opts.project_id)
43-
.getComputeServerIdForFile({ path: filePath }) ?? 0;
44-
if (compute_server_id && excludeFromComputeServer(filePath)) {
45-
compute_server_id = 0;
46-
}
56+
const compute_server_id = getComputeServerId({
57+
project_id: opts.project_id,
58+
path: filePath,
59+
});
4760
opts = { ...opts, compute_server_id };
4861
}
4962
return await webapp_client.project_client.exec(opts);

src/packages/frontend/frame-editors/latex-editor/editor.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { ErrorsAndWarnings } from "./errors-and-warnings";
2121
import { LatexWordCount } from "./latex-word-count";
2222
import { PDFEmbed } from "./pdf-embed";
2323
import { PDFJS } from "./pdfjs";
24-
import { pdf_path } from "./util";
2524

2625
export const pdfjsCommands = set([
2726
"print",
@@ -114,7 +113,6 @@ const pdfjs_canvas: EditorDescription = {
114113
"print",
115114
"download_pdf",
116115
]),
117-
path: pdf_path,
118116
renderer: "canvas",
119117
} as const;
120118

@@ -177,7 +175,6 @@ const pdf_embed: EditorDescription = {
177175
icon: "file-pdf",
178176
commands: set(["print", "save", "download"]),
179177
component: PDFEmbed,
180-
path: pdf_path,
181178
} as const;
182179

183180
const EDITOR_SPEC = {

src/packages/frontend/frame-editors/latex-editor/pdf-embed.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This is a renderer using the embed tag, so works with browsers that have a PDF v
99

1010
import { React } from "@cocalc/frontend/app-framework";
1111
import { raw_url } from "@cocalc/frontend/frame-editors/frame-tree/util";
12+
import { pdf_path } from "./util";
13+
import { getComputeServerId } from "@cocalc/frontend/frame-editors/generic/client";
1214

1315
export interface Props {
1416
actions: any;
@@ -25,7 +27,12 @@ export const PDFEmbed: React.FC<Props> = React.memo((props: Props) => {
2527
const embedRef = React.useRef<any>(null);
2628

2729
function render_embed(): React.JSX.Element {
28-
const src: string = `${raw_url(project_id, path)}?param=${reload}`;
30+
let src = raw_url(
31+
project_id,
32+
pdf_path(path),
33+
getComputeServerId({ project_id, path }),
34+
`param=${reload}`,
35+
);
2936
return (
3037
<embed
3138
ref={embedRef}

src/packages/frontend/frame-editors/latex-editor/pdfjs-doc-cache.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
3838
*/
3939
import { getDocument as pdfjs_getDocument } from "pdfjs-dist";
4040
import type { PDFDocumentProxy } from "pdfjs-dist/webpack.mjs";
41-
4241
import { raw_url } from "@cocalc/frontend/frame-editors/frame-tree/util";
4342
import { pdf_path } from "./util";
43+
import { getComputeServerId } from "@cocalc/frontend/frame-editors/generic/client";
4444

4545
const options = {
4646
maxSize: MAX_PAGES,
@@ -54,8 +54,12 @@ export function url_to_pdf(
5454
path: string,
5555
reload: number,
5656
): string {
57-
const url = raw_url(project_id, pdf_path(path));
58-
return `${url}?param=${reload}`;
57+
return raw_url(
58+
project_id,
59+
pdf_path(path),
60+
getComputeServerId({ project_id, path }),
61+
`param=${reload}`,
62+
);
5963
}
6064

6165
const doc_cache = new LRU(options);

src/packages/frontend/frame-editors/latex-editor/pdfjs.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type { Set as iSet } from "immutable";
1313
import type { PDFDocumentProxy, PDFPageProxy } from "pdfjs-dist/webpack.mjs";
1414
import { useCallback, useEffect, useRef, useState } from "react";
1515
import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
16-
1716
import {
1817
redux,
1918
useActions,

src/packages/frontend/lib/cocalc-urls.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ export function fileURL({
1212
project_id,
1313
compute_server_id,
1414
path,
15+
param,
1516
}: {
1617
project_id: string;
1718
path: string;
1819
compute_server_id?: number;
20+
param?: string;
1921
}): string {
2022
let url = join(appBasePath, project_id, "files", encode_path(path));
2123
if (compute_server_id) {
2224
url += `?id=${compute_server_id}`;
2325
}
26+
if (param) {
27+
if (compute_server_id) {
28+
url += "&" + param;
29+
} else {
30+
url += "?" + param;
31+
}
32+
}
2433
return url;
2534
}
2635

0 commit comments

Comments
 (0)