web: add menu item to allow copying a note as text or markdown (#3193)

Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
This commit is contained in:
Muhammad Ali
2023-10-02 16:09:35 +05:00
committed by GitHub
parent 7b08b5c035
commit 9027be7bbb

View File

@@ -49,7 +49,8 @@ import {
AddToNotebook,
RemoveShortcutLink,
Plus,
Tag
Tag,
Copy
} from "../icons";
import TimeAgo from "../time-ago";
import ListItem from "../list-item";
@@ -85,6 +86,7 @@ import {
ReferencesWithDateEdited
} from "../list-container/types";
import { SchemeColors } from "@notesnook/theme";
import Vault from "../../common/vault";
type NoteProps = {
tags: Item[];
@@ -454,6 +456,32 @@ const menuItems: (note: any, items?: any[]) => MenuItem[] = (
multiSelect: true,
isPro: true
},
{
type: "button",
key: "copy",
title: "Copy as",
icon: Copy.path,
menu: {
items: [
{
type: "button",
key: "copy-as-text",
tooltip: `Export as Text`,
title: "Text",
icon: Plaintext.path,
onClick: () => copyNote(note.id, "txt")
},
{
type: "button",
key: "copy-as-markdown",
tooltip: `Export as Markdown`,
title: "Markdown",
icon: Markdown.path,
onClick: () => copyNote(note.id, "md")
}
]
}
},
{
type: "button",
key: "duplicate",
@@ -667,3 +695,25 @@ function tagsMenuItems(items: any[]): MenuItem[] {
return menuItems;
}
async function copyNote(noteId: string, format: "md" | "txt") {
try {
const note = db.notes?.note(noteId);
if (!note) throw new Error("No note with this id exists.");
if (note?.data.locked && !(await Vault.unlockVault()))
throw new Error("Please unlock this note to copy it.");
const rawContent = await db.content?.raw(note.data.contentId);
const content = note?.data.locked
? await db.vault?.decryptContent(rawContent)
: rawContent;
const text = await note.export(format, content, false);
if (!text) throw new Error(`Could not convert note to ${format}.`);
await navigator.clipboard.writeText(text);
showToast("success", "Copied!");
} catch (e) {
if (e instanceof Error)
showToast("error", `Failed to copy note: ${e.message}.`);
}
}