Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add export page command, save to file if single result #244

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/components/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export type ExportDialogProps = {
title?: string;
columns?: Column[];
isExportDiscourseGraph?: boolean;
initialPanel?: "sendTo" | "export";
};

type ExportDialogComponent = (
Expand All @@ -102,6 +103,7 @@ const ExportDialog: ExportDialogComponent = ({
columns,
title = "Share Data",
isExportDiscourseGraph = false,
initialPanel,
}) => {
const exportId = useMemo(() => nanoid(), []);
useEffect(() => {
Expand Down Expand Up @@ -148,8 +150,8 @@ const ExportDialog: ExportDialogComponent = ({
const [livePages, setLivePages] = useState<Result[]>([]);
const [selectedTabId, setSelectedTabId] = useState("sendto");
useEffect(() => {
if (isExportDiscourseGraph) setSelectedTabId("export");
}, [isExportDiscourseGraph]);
if (initialPanel === "export") setSelectedTabId("export");
}, [initialPanel]);
const [discourseGraphEnabled, setDiscourseGraphEnabled] = useState(() => {
return getExtensionAPI().settings.get("discourse-graphs");
});
Expand Down Expand Up @@ -522,9 +524,6 @@ const ExportDialog: ExportDialogComponent = ({
(e) => e.name === activeExportType
);
if (exportType && window.RoamLazy) {
const zip = await window.RoamLazy.JSZip().then(
(j) => new j()
);
setDialogOpen(true);
setLoading(false);
updateExportProgress({
Expand All @@ -548,6 +547,19 @@ const ExportDialog: ExportDialogComponent = ({
return;
}

if (files.length === 1) {
const { title, content } = files[0];
const blob = new Blob([content], {
type: "text/plain;charset=utf-8",
});
saveAs(blob, title);
onClose();
return;
}

const zip = await window.RoamLazy.JSZip().then(
(j) => new j()
);
files.forEach(({ title, content }) =>
zip.file(title, content)
);
Expand Down
1 change: 1 addition & 0 deletions src/discourseGraphsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ const initializeDiscourseGraphsMode = async (args: OnloadArgs) => {
results,
title: "Export Discourse Graph",
isExportDiscourseGraph: true,
initialPanel: "export",
});
},
});
Expand Down
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import { GroupedShapes } from "./components/CanvasDrawer";
import isDiscourseNode from "./utils/isDiscourseNode";
import { fireQuerySync } from "./utils/fireQuery";
import parseQuery from "./utils/parseQuery";
import { render as exportRender } from "./components/Export";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";

const loadedElsewhere = document.currentScript
? document.currentScript.getAttribute("data-source") === "discourse-graph"
Expand Down Expand Up @@ -721,7 +723,23 @@ svg.rs-svg-container {
}, 200);
},
});

extensionAPI.ui.commandPalette.addCommand({
label: "Export Current Page",
callback: () => {
const pageUid = getCurrentPageUid();
const pageTitle = getPageTitleByPageUid(pageUid);
exportRender({
results: [
{
uid: pageUid,
text: pageTitle,
},
],
title: "Export Current Page",
initialPanel: "export",
});
},
});
extensionAPI.ui.commandPalette.addCommand({
label: "Preview Current Query Builder Results",
callback: () => {
Expand Down
Loading