mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-25 11:52:10 +00:00
Show a single page when user clicks Export as PDF (#279)
This commit is contained in:
@@ -7,14 +7,14 @@ import { useScrollActiveId } from '@/components/hooks';
|
||||
import { Button } from '@/components/primitives';
|
||||
import { t, useLanguage } from '@/intl/client';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
import { getPDFUrl } from './params';
|
||||
import { getPDFUrl } from '@/lib/urls';
|
||||
|
||||
/**
|
||||
* Dynamic controls to show active page and to let the user select between modes.
|
||||
*/
|
||||
export function PageControlButtons(props: {
|
||||
pdfHref: string;
|
||||
singlePageMode: boolean;
|
||||
/** Array of the [pageId, divId] */
|
||||
pageIds: Array<[string, string]>;
|
||||
/** Total number of pages targetted by the generation */
|
||||
@@ -22,7 +22,7 @@ export function PageControlButtons(props: {
|
||||
/** Trademark to display */
|
||||
trademark?: React.ReactNode;
|
||||
}) {
|
||||
const { pdfHref, pageIds, total, trademark } = props;
|
||||
const { pdfHref, singlePageMode, pageIds, total, trademark } = props;
|
||||
|
||||
const language = useLanguage();
|
||||
|
||||
@@ -49,15 +49,17 @@ export function PageControlButtons(props: {
|
||||
'z-50',
|
||||
)}
|
||||
>
|
||||
<Button
|
||||
href={getPDFUrl(new URL(pdfHref), {
|
||||
page: activePageId,
|
||||
only: true,
|
||||
}).toString()}
|
||||
variant="secondary"
|
||||
>
|
||||
{t(language, 'pdf_mode_only_page')}
|
||||
</Button>
|
||||
{singlePageMode ? null : (
|
||||
<Button
|
||||
href={getPDFUrl(new URL(pdfHref), {
|
||||
page: activePageId,
|
||||
only: true,
|
||||
}).toString()}
|
||||
variant="secondary"
|
||||
>
|
||||
{t(language, 'pdf_mode_only_page')}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
href={getPDFUrl(new URL(pdfHref), { page: undefined, only: false }).toString()}
|
||||
variant="secondary"
|
||||
|
||||
@@ -28,10 +28,10 @@ import { pagePDFContainerId, PageHrefContext, absoluteHref } from '@/lib/links';
|
||||
import { resolvePageId } from '@/lib/pages';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
import { PDFSearchParams, getPDFSearchParams } from '@/lib/urls';
|
||||
|
||||
import './pdf.css';
|
||||
import { PageControlButtons } from './PageControlButtons';
|
||||
import { PDFSearchParams, getPDFSearchParams } from './params';
|
||||
import { PrintButton } from './PrintButton';
|
||||
|
||||
export const runtime = 'edge';
|
||||
@@ -136,6 +136,7 @@ export default async function PDFHTMLOutput(props: { searchParams: { [key: strin
|
||||
<PageControlButtons
|
||||
pageIds={pageIds}
|
||||
pdfHref={currentPDFUrl}
|
||||
singlePageMode={!!pdfParams.only}
|
||||
total={total}
|
||||
trademark={
|
||||
customization.trademark.enabled ? (
|
||||
|
||||
@@ -11,6 +11,7 @@ import { getDocumentSections } from '@/lib/document';
|
||||
import { absoluteHref } from '@/lib/links';
|
||||
import { ContentRefContext, resolveContentRef } from '@/lib/references';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
import { getPDFUrl, getPDFUrlSearchParams } from '@/lib/urls';
|
||||
|
||||
import { ScrollSectionsList } from './ScrollSectionsList';
|
||||
import { PageFeedbackForm } from '../PageFeedback';
|
||||
@@ -139,7 +140,12 @@ export async function PageAside(props: {
|
||||
{customization.pdf.enabled ? (
|
||||
<div>
|
||||
<a
|
||||
href={absoluteHref('~gitbook/pdf')}
|
||||
href={absoluteHref(
|
||||
`~gitbook/pdf?${getPDFUrlSearchParams({
|
||||
page: page.id,
|
||||
only: true,
|
||||
}).toString()}`,
|
||||
)}
|
||||
className={tcls(
|
||||
'flex',
|
||||
'flex-row',
|
||||
|
||||
@@ -36,25 +36,32 @@ export function getPDFSearchParams(searchParams: URLSearchParams): PDFSearchPara
|
||||
*/
|
||||
export function getPDFUrl(url: URL, params: PDFSearchParams): URL {
|
||||
const copy = new URL(url);
|
||||
getPDFUrlSearchParams(params, copy.searchParams);
|
||||
return copy;
|
||||
}
|
||||
|
||||
export function getPDFUrlSearchParams(
|
||||
params: PDFSearchParams,
|
||||
searchParams = new URLSearchParams({}),
|
||||
): URLSearchParams {
|
||||
if (params?.page) {
|
||||
copy.searchParams.set('page', params.page);
|
||||
searchParams.set('page', params.page);
|
||||
} else {
|
||||
copy.searchParams.delete('page');
|
||||
searchParams.delete('page');
|
||||
}
|
||||
if (params?.only) {
|
||||
copy.searchParams.set('only', 'yes');
|
||||
searchParams.set('only', 'yes');
|
||||
} else {
|
||||
copy.searchParams.delete('only');
|
||||
searchParams.delete('only');
|
||||
}
|
||||
|
||||
// Persist limit and back
|
||||
if (params?.limit) {
|
||||
copy.searchParams.set('limit', String(params.limit));
|
||||
searchParams.set('limit', String(params.limit));
|
||||
}
|
||||
if (params?.back) {
|
||||
copy.searchParams.set('back', String(params.back));
|
||||
searchParams.set('back', String(params.back));
|
||||
}
|
||||
|
||||
return copy;
|
||||
return searchParams;
|
||||
}
|
||||
Reference in New Issue
Block a user