mirror of
https://github.com/outline/outline.git
synced 2026-12-03 01:59:03 +03:00
Add download options to revision menu (#11048)
This commit is contained in:
1 parent
7e2d2542b0
commit
8ee51a864a
6 files changed
+120
-6
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import copy from "copy-to-clipboard";
|
||||
import { LinkIcon, RestoreIcon, TrashIcon } from "outline-icons";
|
||||
import { LinkIcon, RestoreIcon, TrashIcon, DownloadIcon } from "outline-icons";
|
||||
import { matchPath } from "react-router-dom";
|
||||
import { toast } from "sonner";
|
||||
import { ExportContentType } from "@shared/types";
|
||||
import stores from "~/stores";
|
||||
import { createAction } from "~/actions";
|
||||
import { createAction, createActionWithChildren } from "~/actions";
|
||||
import { RevisionSection } from "~/actions/sections";
|
||||
import env from "~/env";
|
||||
import history from "~/utils/history";
|
||||
import {
|
||||
documentHistoryPath,
|
||||
@@ -101,4 +103,78 @@ export const copyLinkToRevision = (revisionId: string) =>
|
||||
},
|
||||
});
|
||||
|
||||
export const downloadRevisionAsHTML = (revisionId: string) =>
|
||||
createAction({
|
||||
name: ({ t }) => t("HTML"),
|
||||
analyticsName: "Download revision as HTML",
|
||||
section: RevisionSection,
|
||||
keywords: "html export",
|
||||
icon: <DownloadIcon />,
|
||||
iconInContextMenu: false,
|
||||
visible: ({ activeDocumentId }) =>
|
||||
!!activeDocumentId &&
|
||||
stores.policies.abilities(activeDocumentId).download,
|
||||
perform: async () => {
|
||||
const revision = stores.revisions.get(revisionId);
|
||||
await revision?.download(ExportContentType.Html);
|
||||
},
|
||||
});
|
||||
|
||||
export const downloadRevisionAsPDF = (revisionId: string) =>
|
||||
createAction({
|
||||
name: ({ t }) => t("PDF"),
|
||||
analyticsName: "Download revision as PDF",
|
||||
section: RevisionSection,
|
||||
keywords: "export",
|
||||
icon: <DownloadIcon />,
|
||||
iconInContextMenu: false,
|
||||
visible: ({ activeDocumentId }) =>
|
||||
!!(
|
||||
activeDocumentId &&
|
||||
stores.policies.abilities(activeDocumentId).download &&
|
||||
env.PDF_EXPORT_ENABLED
|
||||
),
|
||||
perform: ({ t }) => {
|
||||
const id = toast.loading(`${t("Exporting")}…`);
|
||||
const revision = stores.revisions.get(revisionId);
|
||||
return revision
|
||||
?.download(ExportContentType.Pdf)
|
||||
.finally(() => id && toast.dismiss(id));
|
||||
},
|
||||
});
|
||||
|
||||
export const downloadRevisionAsMarkdown = (revisionId: string) =>
|
||||
createAction({
|
||||
name: ({ t }) => t("Markdown"),
|
||||
analyticsName: "Download revision as Markdown",
|
||||
section: RevisionSection,
|
||||
keywords: "md markdown export",
|
||||
icon: <DownloadIcon />,
|
||||
iconInContextMenu: false,
|
||||
visible: ({ activeDocumentId }) =>
|
||||
!!activeDocumentId &&
|
||||
stores.policies.abilities(activeDocumentId).download,
|
||||
perform: async () => {
|
||||
const revision = stores.revisions.get(revisionId);
|
||||
await revision?.download(ExportContentType.Markdown);
|
||||
},
|
||||
});
|
||||
|
||||
export const downloadRevision = (revisionId: string) =>
|
||||
createActionWithChildren({
|
||||
name: ({ t, isMenu }) => (isMenu ? t("Download") : t("Download revision")),
|
||||
analyticsName: "Download revision",
|
||||
section: RevisionSection,
|
||||
icon: <DownloadIcon />,
|
||||
keywords: "export",
|
||||
visible: ({ activeDocumentId }) =>
|
||||
!!activeDocumentId &&
|
||||
stores.policies.abilities(activeDocumentId).download,
|
||||
children: [
|
||||
downloadRevisionAsHTML(revisionId),
|
||||
downloadRevisionAsPDF(revisionId),
|
||||
downloadRevisionAsMarkdown(revisionId),
|
||||
],
|
||||
});
|
||||
|
||||
export const rootRevisionActions = [];
|
||||
@@ -13,6 +13,7 @@ import type Revision from "~/models/Revision";
|
||||
import { ActionSeparator } from "~/actions";
|
||||
import {
|
||||
copyLinkToRevision,
|
||||
downloadRevision,
|
||||
restoreRevision,
|
||||
} from "~/actions/definitions/revisions";
|
||||
import { Avatar, AvatarSize } from "~/components/Avatar";
|
||||
@@ -45,7 +46,12 @@ const RevisionListItem = ({ item, document, ...rest }: Props) => {
|
||||
const ref = useRef<HTMLAnchorElement>(null);
|
||||
|
||||
const actions = useMemo(
|
||||
() => [restoreRevision, ActionSeparator, copyLinkToRevision(item.id)],
|
||||
() => [
|
||||
restoreRevision,
|
||||
ActionSeparator,
|
||||
copyLinkToRevision(item.id),
|
||||
downloadRevision(item.id),
|
||||
],
|
||||
[item.id]
|
||||
);
|
||||
const contextMenuAction = useMenuAction(actions);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { OverflowMenuButton } from "~/components/Menu/OverflowMenuButton";
|
||||
import { ActionSeparator } from "~/actions";
|
||||
import {
|
||||
copyLinkToRevision,
|
||||
downloadRevision,
|
||||
restoreRevision,
|
||||
} from "~/actions/definitions/revisions";
|
||||
import { useMemo } from "react";
|
||||
@@ -20,7 +21,12 @@ type Props = {
|
||||
function RevisionMenu({ document, revisionId }: Props) {
|
||||
const { t } = useTranslation();
|
||||
const actions = useMemo(
|
||||
() => [restoreRevision, ActionSeparator, copyLinkToRevision(revisionId)],
|
||||
() => [
|
||||
restoreRevision,
|
||||
ActionSeparator,
|
||||
copyLinkToRevision(revisionId),
|
||||
downloadRevision(revisionId),
|
||||
],
|
||||
[revisionId]
|
||||
);
|
||||
|
||||
|
||||
+22
-1
@@ -1,5 +1,5 @@
|
||||
import { computed, observable } from "mobx";
|
||||
import { type ProsemirrorData } from "@shared/types";
|
||||
import { type ExportContentType, type ProsemirrorData } from "@shared/types";
|
||||
import { isRTL } from "@shared/utils/rtl";
|
||||
import Document from "./Document";
|
||||
import User from "./User";
|
||||
@@ -8,6 +8,7 @@ import Field from "./decorators/Field";
|
||||
import Relation from "./decorators/Relation";
|
||||
import type RevisionsStore from "~/stores/RevisionsStore";
|
||||
import { ChangesetHelper } from "@shared/editor/lib/ChangesetHelper";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
|
||||
class Revision extends ParanoidModel {
|
||||
static modelName = "Revision";
|
||||
@@ -100,6 +101,26 @@ class Revision extends ParanoidModel {
|
||||
get changeset() {
|
||||
return ChangesetHelper.getChangeset(this.data, this.before?.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers a download of the revision in the specified format.
|
||||
*
|
||||
* @param contentType The format to download the revision in.
|
||||
* @returns A promise that resolves when the download is triggered.
|
||||
*/
|
||||
download = (contentType: ExportContentType) =>
|
||||
client.post(
|
||||
`/revisions.export`,
|
||||
{
|
||||
id: this.id,
|
||||
},
|
||||
{
|
||||
download: true,
|
||||
headers: {
|
||||
accept: contentType,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export default Revision;
|
||||
@@ -8,7 +8,7 @@ import { Op } from "sequelize";
|
||||
import { UserRole } from "@shared/types";
|
||||
import { RevisionHelper } from "@shared/utils/RevisionHelper";
|
||||
import slugify from "@shared/utils/slugify";
|
||||
import { ValidationError } from "@server/errors";
|
||||
import { ValidationError, IncorrectEditionError } from "@server/errors";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||
@@ -227,6 +227,10 @@ router.post(
|
||||
centered: true,
|
||||
includeMermaid: true,
|
||||
});
|
||||
} else if (accept?.includes("application/pdf")) {
|
||||
throw IncorrectEditionError(
|
||||
"PDF export is not available in the community edition"
|
||||
);
|
||||
} else if (accept?.includes("text/markdown")) {
|
||||
contentType = "text/markdown";
|
||||
content = DocumentHelper.toMarkdown(revision);
|
||||
|
||||
@@ -144,6 +144,7 @@
|
||||
"New Application": "New Application",
|
||||
"This version of the document was deleted": "This version of the document was deleted",
|
||||
"Link copied": "Link copied",
|
||||
"Download revision": "Download revision",
|
||||
"Dark": "Dark",
|
||||
"Light": "Light",
|
||||
"System": "System",
|
||||
|
||||
Reference in new issue
Block a user