2023-11-22 07:02:49 +00:00
|
|
|
import { Editor } from "@tiptap/react";
|
|
|
|
import {
|
|
|
|
Archive,
|
|
|
|
ArchiveIcon,
|
|
|
|
ArchiveRestoreIcon,
|
|
|
|
ClipboardIcon,
|
|
|
|
Copy,
|
|
|
|
Link,
|
|
|
|
Lock,
|
|
|
|
Unlock,
|
|
|
|
XCircle,
|
|
|
|
} from "lucide-react";
|
|
|
|
import { NextRouter } from "next/router";
|
|
|
|
import { IVerticalDropdownItemProps } from "../components/vertical-dropdown-menu";
|
|
|
|
import {
|
|
|
|
IDuplicationConfig,
|
|
|
|
IPageArchiveConfig,
|
|
|
|
IPageLockConfig,
|
|
|
|
} from "../types/menu-actions";
|
|
|
|
import { copyMarkdownToClipboard, CopyPageLink } from "./menu-actions";
|
2023-11-20 16:01:12 +00:00
|
|
|
|
2023-11-22 07:02:49 +00:00
|
|
|
export interface MenuOptionsProps {
|
|
|
|
editor: Editor;
|
|
|
|
router: NextRouter;
|
|
|
|
duplicationConfig?: IDuplicationConfig;
|
|
|
|
pageLockConfig?: IPageLockConfig;
|
|
|
|
pageArchiveConfig?: IPageArchiveConfig;
|
2023-12-07 06:34:21 +00:00
|
|
|
onActionCompleteHandler: (action: {
|
|
|
|
title: string;
|
|
|
|
message: string;
|
|
|
|
type: "success" | "error" | "warning" | "info";
|
|
|
|
}) => void;
|
2023-11-20 16:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 07:02:49 +00:00
|
|
|
export const getMenuOptions = ({
|
|
|
|
editor,
|
|
|
|
router,
|
|
|
|
duplicationConfig,
|
|
|
|
pageLockConfig,
|
|
|
|
pageArchiveConfig,
|
2023-12-07 06:34:21 +00:00
|
|
|
onActionCompleteHandler,
|
2023-11-22 07:02:49 +00:00
|
|
|
}: MenuOptionsProps) => {
|
2023-11-20 16:01:12 +00:00
|
|
|
const KanbanMenuOptions: IVerticalDropdownItemProps[] = [
|
|
|
|
{
|
2023-11-22 07:02:49 +00:00
|
|
|
key: 1,
|
2023-11-20 16:01:12 +00:00
|
|
|
type: "copy_markdown",
|
|
|
|
Icon: ClipboardIcon,
|
2023-12-07 06:34:21 +00:00
|
|
|
action: () => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: "Markdown Copied",
|
|
|
|
message: "Page Copied as Markdown",
|
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
copyMarkdownToClipboard(editor);
|
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
label: "Copy markdown",
|
2023-11-20 16:01:12 +00:00
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
// {
|
|
|
|
// key: 2,
|
|
|
|
// type: "close_page",
|
|
|
|
// Icon: XCircle,
|
|
|
|
// action: () => router.back(),
|
|
|
|
// label: "Close page",
|
|
|
|
// },
|
2023-11-20 16:01:12 +00:00
|
|
|
{
|
2023-11-22 07:02:49 +00:00
|
|
|
key: 3,
|
2023-11-20 16:01:12 +00:00
|
|
|
type: "copy_page_link",
|
|
|
|
Icon: Link,
|
2023-12-07 06:34:21 +00:00
|
|
|
action: () => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: "Link Copied",
|
|
|
|
message: "Link to the page has been copied to clipboard",
|
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
CopyPageLink();
|
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
label: "Copy page link",
|
2023-11-20 16:01:12 +00:00
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
];
|
2023-11-20 16:01:12 +00:00
|
|
|
|
|
|
|
// If duplicateConfig is given, page duplication will be allowed
|
|
|
|
if (duplicationConfig) {
|
|
|
|
KanbanMenuOptions.push({
|
2023-11-22 07:02:49 +00:00
|
|
|
key: KanbanMenuOptions.length++,
|
2023-11-20 16:01:12 +00:00
|
|
|
type: "duplicate_page",
|
|
|
|
Icon: Copy,
|
2023-12-07 06:34:21 +00:00
|
|
|
action: () => {
|
|
|
|
duplicationConfig
|
|
|
|
.action()
|
|
|
|
.then(() => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: "Page Copied",
|
|
|
|
message:
|
|
|
|
"Page has been copied as 'Copy of' followed by page title",
|
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: "Copy Failed",
|
|
|
|
message: "Sorry, page cannot be copied, please try again later.",
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
label: "Make a copy",
|
|
|
|
});
|
2023-11-20 16:01:12 +00:00
|
|
|
}
|
|
|
|
// If Lock Configuration is given then, lock page option will be available in the kanban menu
|
|
|
|
if (pageLockConfig) {
|
|
|
|
KanbanMenuOptions.push({
|
2023-11-22 07:02:49 +00:00
|
|
|
key: KanbanMenuOptions.length++,
|
2023-11-20 16:01:12 +00:00
|
|
|
type: pageLockConfig.is_locked ? "unlock_page" : "lock_page",
|
|
|
|
Icon: pageLockConfig.is_locked ? Unlock : Lock,
|
2023-11-22 07:02:49 +00:00
|
|
|
label: pageLockConfig.is_locked ? "Unlock page" : "Lock page",
|
2023-12-07 06:34:21 +00:00
|
|
|
action: () => {
|
|
|
|
const state = pageLockConfig.is_locked ? "Unlocked" : "Locked";
|
|
|
|
pageLockConfig
|
|
|
|
.action()
|
|
|
|
.then(() => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: `Page ${state}`,
|
|
|
|
message: `Page has been ${state}, no one will be able to change the state of lock except you.`,
|
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: `Page cannot be ${state}`,
|
|
|
|
message: `Sorry, page cannot be ${state}, please try again later`,
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
});
|
2023-11-20 16:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Archiving will be visible in the menu bar config once the pageArchiveConfig is given.
|
|
|
|
if (pageArchiveConfig) {
|
|
|
|
KanbanMenuOptions.push({
|
2023-11-22 07:02:49 +00:00
|
|
|
key: KanbanMenuOptions.length++,
|
2023-11-20 16:01:12 +00:00
|
|
|
type: pageArchiveConfig.is_archived ? "unarchive_page" : "archive_page",
|
|
|
|
Icon: pageArchiveConfig.is_archived ? ArchiveRestoreIcon : Archive,
|
2023-11-22 07:02:49 +00:00
|
|
|
label: pageArchiveConfig.is_archived ? "Restore page" : "Archive page",
|
2023-12-07 06:34:21 +00:00
|
|
|
action: () => {
|
|
|
|
const state = pageArchiveConfig.is_archived ? "Unarchived" : "Archived";
|
|
|
|
pageArchiveConfig
|
|
|
|
.action()
|
|
|
|
.then(() => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: `Page ${state}`,
|
|
|
|
message: `Page has been ${state}, you can checkout all archived tab and can restore the page later.`,
|
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
onActionCompleteHandler({
|
|
|
|
title: `Page cannot be ${state}`,
|
|
|
|
message: `Sorry, page cannot be ${state}, please try again later.`,
|
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2023-11-22 07:02:49 +00:00
|
|
|
});
|
2023-11-20 16:01:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 07:02:49 +00:00
|
|
|
return KanbanMenuOptions;
|
|
|
|
};
|