#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import { useCallback, useEffect, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";
import {
Alert,
Box,
Card,
Group,
Image,
Select,
Stack,
Text,
TextInput,
} from "@mantine/core";
import { useMediaQuery } from "@mantine/hooks";
import { Button as DSButton } from "@editor/ui/Button";
import { SegmentedControl } from "@editor/ui/SegmentedControl";
import { useTranslation } from "react-i18next";
import { LogoIcon } from "@editor/components/shared/LogoIcon";
import { Wordmark } from "@editor/components/shared/Wordmark";
import { Icon } from "@editor/ui/Icon";
import {
MobileDrawCanvas,
type MobileDrawCanvasHandle,
} from "@editor/components/mobileSign/MobileDrawCanvas";
import apiClient from "@editor/services/apiClient";
import { privateContentClassName } from "@editor/components/shared/privateContentClassName";
// Use the configured API base (e.g. api.stirling.com), not the page origin.
const API_BASE = (apiClient.defaults.baseURL ?? "").replace(/\/+$/, "");
type SignatureTab = "draw" | "type" | "photo";
// Ink pigments, not UI theme colours: they are baked into the exported PNG
// and transferred to the desktop, so they must be fixed literals.
const INK_COLORS = [
{ value: "#101010", labelKey: "black", label: "Black" }, // theme-allow-color ink pigment, serialized into the signature
{ value: "#1d4ed8", labelKey: "blue", label: "Blue" }, // theme-allow-color ink pigment, serialized into the signature
];
const PEN_SIZES = [
{ value: 2, label: "S" },
{ value: 3.5, label: "M" },
{ value: 6, label: "L" },
];
/**
* The sign tool's own text-mode fonts, so a typed signature transfers as
* data and stays editable there. `css` approximates each for the on-phone
* preview; `value` is what the desktop's font parameter understands.
*/
const TYPE_FONTS = [
{
value: "Helvetica",
css: "Helvetica, Arial, sans-serif",
label: "Helvetica",
},
{
value: "Times-Roman",
css: "'Times New Roman', Times, serif",
label: "Times",
},
{
value: "Courier",
css: "'Courier New', Courier, monospace",
label: "Courier",
},
{ value: "Arial", css: "Arial, sans-serif", label: "Arial" },
{ value: "Georgia", css: "Georgia, serif", label: "Georgia" },
];
async function dataUrlToBlob(dataUrl: string): Promise<Blob> {
const response = await fetch(dataUrl);
return response.blob();
}
/**
* MobileSignPage
*
* Phone-side page for sending a signature to the desktop: draw one (the main
* path), type one, or photograph one. Reached by scanning the QR code shown in
* the editor's Sign tool; the session comes from the QR URL and rides the same
* transfer backend as the mobile scanner.
*/
export default function MobileSignPage() {
const { t } = useTranslation();
const [searchParams] = useSearchParams();
const sessionId = searchParams.get("session");
// Landscape phones (not tablets — hence the height cap) get a compact
// layout: branding hidden, tighter padding, shorter pad, so the canvas and
// the Send button fit on screen together.
const compactLandscape =
useMediaQuery("(orientation: landscape) and (max-height: 32rem)") ?? false;
const [sessionValid, setSessionValid] = useState<boolean | null>(null);
const [tab, setTab] = useState<SignatureTab>("draw");
const [hasInk, setHasInk] = useState(false);
const [typedText, setTypedText] = useState("");
const [typeFont, setTypeFont] = useState(TYPE_FONTS[0].value);
const [inkColor, setInkColor] = useState(INK_COLORS[0].value);
const [penSize, setPenSize] = useState(PEN_SIZES[1].value);
const [photoDataUrl, setPhotoDataUrl] = useState<string | null>(null);
const [photoError, setPhotoError] = useState<string | null>(null);
const [isSending, setIsSending] = useState(false);
const [sendError, setSendError] = useState<string | null>(null);
const [sentCount, setSentCount] = useState(0);
const [justSent, setJustSent] = useState(false);
const canvasHandle = useRef<MobileDrawCanvasHandle>(null);
const photoInputRef = useRef<HTMLInputElement>(null);
const cameraInputRef = useRef<HTMLInputElement>(null);
// Validate the session up front, so a stale QR shows one clear error rather
// than a canvas whose Send fails.
useEffect(() => {
if (!sessionId) {
setSessionValid(false);
return;
}
(async () => {
try {
const response = await fetch(
`${API_BASE}/api/v1/mobile-scanner/validate-session/${sessionId}`,
);
const data = response.ok ? await response.json() : null;
setSessionValid(Boolean(data?.valid));
} catch {
setSessionValid(false);
}
})();
}, [sessionId]);
const handlePhotoSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
if (!file.type.startsWith("image/")) {
setPhotoError(
t("mobileSign.photo.invalidType", "Please choose an image file."),
);
return;
}
setPhotoError(null);
const reader = new FileReader();
reader.onload = (event) =>
setPhotoDataUrl((event.target?.result as string) ?? null);
reader.readAsDataURL(file);
};
/**
* What this tab sends: ink and photos as image files, typed signatures as a
* JSON payload (text + font + colour) so the desktop keeps them editable in
* the sign tool's text mode. The filename prefix tells the desktop which
* source the signature belongs to.
*/
const buildUpload = useCallback(async (): Promise<{
blob: Blob;
filename: string;
} | null> => {
if (tab === "draw") {
const dataUrl = canvasHandle.current?.exportPng();
if (!dataUrl) return null;
return {
blob: await dataUrlToBlob(dataUrl),
filename: `signature-draw-${Date.now()}.png`,
};
}
if (tab === "type") {
const text = typedText.trim();
if (!text) return null;
const payload = JSON.stringify({
text,
fontFamily: typeFont,
color: inkColor,
});
return {
blob: new Blob([payload], { type: "application/json" }),
filename: `signature-text-${Date.now()}.json`,
};
}
if (!photoDataUrl) return null;
const blob = await dataUrlToBlob(photoDataUrl);
const extension = blob.type === "image/jpeg" ? "jpg" : "png";
return {
blob,
filename: `signature-photo-${Date.now()}.${extension}`,
};
}, [tab, typedText, typeFont, inkColor, photoDataUrl]);
const canSend =
(tab === "draw" && hasInk) ||
(tab === "type" && typedText.trim().length > 0) ||
(tab === "photo" && photoDataUrl !== null);
const handleSend = async () => {
if (!sessionId) return;
setIsSending(true);
setSendError(null);
try {
const upload = await buildUpload();
if (!upload) return;
const formData = new FormData();
formData.append("files", upload.blob, upload.filename);
const response = await fetch(
`${API_BASE}/api/v1/mobile-scanner/upload/${sessionId}`,
{ method: "POST", body: formData },
);
if (!response.ok) {
throw new Error(`Upload failed with status ${response.status}`);
}
setSentCount((count) => count + 1);
setJustSent(true);
// Reset the inputs so "send another" starts clean
canvasHandle.current?.clear();
setTypedText("");
setPhotoDataUrl(null);
if (photoInputRef.current) photoInputRef.current.value = "";
if (cameraInputRef.current) cameraInputRef.current.value = "";
} catch (err) {
console.error("[MobileSignPage] upload failed:", err);
setSendError(
t(
"mobileSign.sendError",
"Could not send the signature. Check the connection and try again.",
),
);
} finally {
setIsSending(false);
}
};
const header = (
<Group justify="center" gap="xs" py="md">
<LogoIcon style={{ width: 28, height: 28 }} />
<Wordmark style={{ height: 18 }} />
</Group>
);
if (sessionValid === null) {
return (
<Box p="md">
{header}
<Text ta="center" c="dimmed">
{t("mobileSign.validating", "Checking session…")}
</Text>
</Box>
);
}
if (!sessionValid) {
return (
<Box p="md" maw={480} mx="auto">
{header}
<Alert
icon={<Icon name="circle-alert" size={"1rem"} />}
color="red"
title={t("mobileSign.invalidSession", "Session expired")}
>
{t(
"mobileSign.invalidSessionMessage",
"This QR code is no longer valid. Open the Sign tool on your computer and scan the new code.",
)}
</Alert>
</Box>
);
}
return (
<Box
p={compactLandscape ? "xs" : "md"}
maw={640}
mx="auto"
style={{ minHeight: "100dvh", display: "flex", flexDirection: "column" }}
>
{!compactLandscape && header}
{justSent && (
<Alert
icon={<Icon name="circle-check" size={"1rem"} />}
color="green"
mb="sm"
withCloseButton
onClose={() => setJustSent(false)}
>
{t(
"mobileSign.sentMessage",
"Signature sent to your computer. You can send another or close this page.",
)}
</Alert>
)}
{sendError && (
<Alert
icon={<Icon name="circle-alert" size={"1rem"} />}
color="red"
mb="sm"
>
{sendError}
</Alert>
)}
<Box mb="sm">
<SegmentedControl<SignatureTab>
fullWidth
value={tab}
onChange={setTab}
ariaLabel={t("mobileSign.tabsLabel", "Signature source")}
options={[
// Same order as the sign tool's sources: canvas, image, text
{ value: "draw", label: t("mobileSign.tab.draw", "Draw") },
{ value: "photo", label: t("mobileSign.tab.photo", "Photo") },
{ value: "type", label: t("mobileSign.tab.type", "Type") },
]}
/>
</Box>
{tab === "draw" && (
<Stack gap="sm" style={{ flex: 1 }}>
<Card
withBorder
radius="md"
p={0}
style={{
// A DEFINITE height, not flex/min-height: the canvas inside is
// sized `height: 100%`, which resolves to the 150px intrinsic
// canvas default when the parent's height is indefinite —
// leaving most of the visible pad ignoring input. Landscape
// phones get a shorter, wider pad that still fits on screen.
height: compactLandscape
? "min(50dvh, 14rem)"
: "min(48dvh, 26rem)",
flexShrink: 0,
// Checkerboard-free plain white: signatures are stamped on paper-
// white pages, so drawing on white shows what you will get.
background: "white",
overflow: "hidden",
}}
>
<MobileDrawCanvas
ref={canvasHandle}
penColor={inkColor}
penSize={penSize}
onHasInkChange={setHasInk}
/>
</Card>
<Group justify="space-between" wrap="nowrap">
<Group gap="xs" wrap="nowrap">
{INK_COLORS.map((color) => (
<Box
key={color.value}
component="button"
onClick={() => setInkColor(color.value)}
aria-label={t(color.labelKey, color.label)}
style={{
width: 32,
height: 32,
borderRadius: "50%",
background: color.value,
cursor: "pointer",
border:
inkColor === color.value
? "3px solid var(--mantine-color-blue-4)"
: "3px solid transparent",
}}
/>
))}
<SegmentedControl
size="xs"
value={String(penSize)}
onChange={(value) => setPenSize(Number(value))}
ariaLabel={t("mobileSign.penSizeLabel", "Pen size")}
options={PEN_SIZES.map((size) => ({
value: String(size.value),
label: size.label,
}))}
/>
</Group>
<Group gap="xs" wrap="nowrap">
<DSButton
variant="secondary"
size="sm"
disabled={!hasInk}
onClick={() => canvasHandle.current?.undo()}
leftSection={<Icon name="undo-2" size={16} />}
>
{t("mobileSign.undo", "Undo")}
</DSButton>
<DSButton
variant="secondary"
accent="danger"
size="sm"
disabled={!hasInk}
onClick={() => canvasHandle.current?.clear()}
leftSection={<Icon name="trash" size={16} />}
>
{t("mobileSign.clear", "Clear")}
</DSButton>
</Group>
</Group>
</Stack>
)}
{tab === "type" && (
<Stack gap="sm">
<TextInput
size="lg"
value={typedText}
onChange={(e) => setTypedText(e.target.value)}
placeholder={t("mobileSign.type.placeholder", "Your name")}
autoComplete="name"
/>
<Select
value={typeFont}
onChange={(value) => value && setTypeFont(value)}
data={TYPE_FONTS.map(({ value, label }) => ({ value, label }))}
allowDeselect={false}
/>
<Group gap="xs">
{INK_COLORS.map((color) => (
<Box
key={color.value}
component="button"
onClick={() => setInkColor(color.value)}
aria-label={t(color.labelKey, color.label)}
style={{
width: 32,
height: 32,
borderRadius: "50%",
background: color.value,
cursor: "pointer",
border:
inkColor === color.value
? "3px solid var(--mantine-color-blue-4)"
: "3px solid transparent",
}}
/>
))}
</Group>
<Card
withBorder
radius="md"
style={{
background: "white",
minHeight: 120,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<Text
style={{
fontFamily:
TYPE_FONTS.find((font) => font.value === typeFont)?.css ??
"sans-serif",
fontSize: "2.5rem",
color: inkColor,
lineHeight: 1.2,
wordBreak: "break-word",
}}
>
{typedText ||
t("mobileSign.type.previewPlaceholder", "Signature preview")}
</Text>
</Card>
</Stack>
)}
{tab === "photo" && (
<Stack gap="sm">
<Text size="sm" c="dimmed">
{t(
"mobileSign.photo.hint",
"Photograph a signature on white paper, or choose an existing image.",
)}
</Text>
{photoDataUrl ? (
<Card withBorder radius="md" style={{ background: "white" }}>
<Image
className={privateContentClassName}
src={photoDataUrl}
alt={t("mobileSign.photo.previewAlt", "Signature")}
fit="contain"
mah={220}
/>
</Card>
) : null}
<Group grow>
<DSButton
variant="secondary"
onClick={() => cameraInputRef.current?.click()}
leftSection={<Icon name="camera" size={18} />}
>
{t("mobileSign.photo.takePhoto", "Take a photo")}
</DSButton>
<DSButton
variant="secondary"
onClick={() => photoInputRef.current?.click()}
leftSection={<Icon name="image-plus" size={18} />}
>
{t("mobileSign.photo.fromGallery", "From gallery")}
</DSButton>
</Group>
{photoError && (
<Text size="sm" c="red">
{photoError}
</Text>
)}
{/* Two inputs, not one: `capture` makes the OS open the camera
directly, but an input carrying it never offers the gallery,
so each source needs its own. */}
<input
ref={cameraInputRef}
type="file"
accept="image/*"
capture="environment"
style={{ display: "none" }}
onChange={handlePhotoSelect}
/>
<input
ref={photoInputRef}
type="file"
accept="image/*"
style={{ display: "none" }}
onChange={handlePhotoSelect}
/>
</Stack>
)}
<Box mt="md">
<DSButton
fullWidth
size="lg"
disabled={!canSend}
loading={isSending}
onClick={handleSend}
leftSection={<Icon name="send" size={18} />}
>
{sentCount > 0
? t("mobileSign.sendAnother", "Send another signature")
: t("mobileSign.send", "Send to computer")}
</DSButton>
</Box>
</Box>
);
}