Skip to content

Commit 923b712

Browse files
committed
core: add no color print style
1 parent 3779c6c commit 923b712

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

packages/server/client/printer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const logger = new Logger('printer');
1414

1515
let timer = null;
1616

17-
export async function ConvertCodeToPDF(code, lang, filename, team, location) {
17+
export async function ConvertCodeToPDF(code, lang, filename, team, location, codeColor = false) {
1818
compiler ||= await createTypstCompiler();
19-
const typst = generateTypst(team, location, filename, lang);
19+
const typst = generateTypst(team, location, filename, lang, codeColor);
2020
compiler.addSource('/main.typst', typst);
2121
compiler.addSource(`/${filename}`, code);
2222
const docs = await compiler.compile({
@@ -32,7 +32,7 @@ export async function printFile(doc) {
3232
_id, tid, code, lang, filename, team, location,
3333
} = doc;
3434
try {
35-
const docs = await ConvertCodeToPDF(code || 'empty file', lang, filename, team, location);
35+
const docs = await ConvertCodeToPDF(code || 'empty file', lang, filename, team, location, config.printColor);
3636
fs.writeFileSync(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`), docs);
3737
if (config.printers.length) {
3838
// eslint-disable-next-line no-constant-condition

packages/server/client/typst.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function createTypstCompiler() {
6565
return compiler;
6666
}
6767

68-
export function generateTypst(team: string, location: string, filename: string, lang: string) {
68+
export function generateTypst(team: string, location: string, filename: string, lang: string, codeColor: boolean) {
6969
return `
7070
#let print(
7171
team: "",
@@ -92,7 +92,7 @@ export function generateTypst(team: string, location: string, filename: string,
9292
]
9393
)
9494
95-
raw(read(filename), lang: lang)
95+
raw(read(filename), lang: lang, block: true${codeColor ? '' : ', theme: "BW.tmtheme"'})
9696
body
9797
}
9898

packages/server/handler/printer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PrintAdminHandler extends AuthHandler {
2424
}
2525
fs.ensureDirSync(path.resolve(process.cwd(), 'data/.pdf'));
2626
code.code = fs.readFileSync(path.resolve(process.cwd(), 'data/codes', `${code.tid}#${code._id}`)).toString();
27-
const doc = await ConvertCodeToPDF(code.code || 'empty file', code.lang, code.filename, code.team, code.location);
27+
const doc = await ConvertCodeToPDF(code.code || 'empty file', code.lang, code.filename, code.team, code.location, params.color ?? true);
2828
this.response.type = 'application/pdf';
2929
this.response.disposition = 'attachment; filename="code.pdf"';
3030
this.response.body = Buffer.from(doc);

packages/ui/app/components/PrintTasksTable.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import {
1010
IconCheck, IconEye, IconHourglassEmpty, IconPrinter, IconRefresh, IconX,
1111
} from '@tabler/icons-react';
1212

13-
function PrintTaskRow({ task, refresh }) {
13+
function PrintTaskRow({ colorCode, task, refresh }) {
1414
const [loading, setLoading] = React.useState(false);
1515

1616
const codeActions = async (_id, operation) => {
1717
setLoading(true);
1818
const post = () => fetch('/print', {
1919
method: 'POST',
2020
headers: { 'Content-Type': 'application/json' },
21-
body: JSON.stringify({ _id, operation }),
21+
body: JSON.stringify({ _id, operation, color: colorCode }),
2222
});
2323
if (operation === 'view') {
2424
const pdf = await (await post()).blob();
@@ -93,7 +93,7 @@ function PrintTaskRow({ task, refresh }) {
9393
);
9494
}
9595

96-
export function PrintTasksTable({ codes, refresh }) {
96+
export function PrintTasksTable({ colorCode, codes, refresh }) {
9797
return (
9898
<Table
9999
horizontalSpacing="md" verticalSpacing="xs" miw={700}
@@ -108,7 +108,7 @@ export function PrintTasksTable({ codes, refresh }) {
108108
<Table.Th>Actions</Table.Th>
109109
</Table.Tr>
110110
</Table.Thead>
111-
<Table.Tbody>{ codes.map((task) => <PrintTaskRow key={task._id} task={task} refresh={refresh} />) }</Table.Tbody>
111+
<Table.Tbody>{ codes.map((task) => <PrintTaskRow key={task._id} task={task} colorCode={colorCode} refresh={refresh} />) }</Table.Tbody>
112112
</Table>
113113
);
114114
}

packages/ui/app/pages/Print.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import {
3-
Card, Center, Grid, Group, LoadingOverlay, Text, Title,
3+
Card, Center, Grid, Group, LoadingOverlay, Switch, Text, Title,
44
} from '@mantine/core';
55
import { useQuery } from '@tanstack/react-query';
66
import { PrintClientAdd, PrintTaskAdd } from '../components/PrintAdd';
@@ -16,6 +16,8 @@ export default function Print() {
1616

1717
const load = query.isLoading || query.isFetching || query.isRefetching;
1818

19+
const [colorCode, setColorCode] = React.useState(false);
20+
1921
return (
2022
<>
2123
<Grid>
@@ -24,13 +26,17 @@ export default function Print() {
2426
<LoadingOverlay visible={load} zIndex={1000} />
2527
<Group justify="space-between" mb="xs">
2628
<Title order={3}>Print Tasks</Title>
27-
<PrintTaskAdd refresh={query.refetch} />
29+
<Group mb="xs">
30+
Color Code
31+
<Switch checked={colorCode} onChange={(ev) => setColorCode(ev.currentTarget.checked)} />
32+
<PrintTaskAdd refresh={query.refetch} />
33+
</Group>
2834
</Group>
2935
{(!(query.isLoading || query.isFetching) && (!(query.data?.codes || []).length) ? (
3036
<Center mt="md">
3137
<Text c="dimmed">No tasks found</Text>
3238
</Center>
33-
) : (<PrintTasksTable codes={query.data?.codes || []} refresh={query.refetch} />))}
39+
) : (<PrintTasksTable colorCode={colorCode} codes={query.data?.codes || []} refresh={query.refetch} />))}
3440
</Card>
3541
</Grid.Col>
3642
<Grid.Col span={3}>

0 commit comments

Comments
 (0)