From 3b01234914036a8c2a21a8959a4001b507f15a83 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 8 Jul 2024 15:32:10 -0700 Subject: [PATCH] POC to get new context menu items from the viewmodel -> settings --- frontend/app/block/block.tsx | 61 +++++++++++++---------------------- frontend/app/view/preview.tsx | 30 +++++++++++++++++ 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/frontend/app/block/block.tsx b/frontend/app/block/block.tsx index 7848cde06..46c1267b4 100644 --- a/frontend/app/block/block.tsx +++ b/frontend/app/block/block.tsx @@ -158,7 +158,12 @@ function getBlockHeaderText(blockIcon: string, blockData: Block, settings: Setti return [blockIconElem, viewString + blockIdStr]; } -function handleHeaderContextMenu(e: React.MouseEvent, blockData: Block, onClose: () => void) { +function handleHeaderContextMenu( + e: React.MouseEvent, + blockData: Block, + viewModel: ViewModel, + onClose: () => void +) { e.preventDefault(); e.stopPropagation(); let menu: ContextMenuItem[] = []; @@ -168,6 +173,12 @@ function handleHeaderContextMenu(e: React.MouseEvent, blockData: alert("Not Implemented"); }, }); + menu.push({ + label: "Minimize", + click: () => { + alert("Not Implemented"); + }, + }); menu.push({ label: "Move to New Window", click: () => { @@ -186,6 +197,11 @@ function handleHeaderContextMenu(e: React.MouseEvent, blockData: navigator.clipboard.writeText(blockData.oid); }, }); + const extraItems = viewModel?.getSettingsMenuItems?.(); + if (extraItems && extraItems.length > 0) { + menu.push({ type: "separator" }); + menu.push(...extraItems); + } menu.push({ type: "separator" }); menu.push({ label: "Close Block", @@ -226,42 +242,6 @@ const BlockFrame_Default_Component = ({ if (isFocused && blockData?.meta?.["frame:bordercolor:focused"]) { style.borderColor = blockData.meta["frame:bordercolor:focused"]; } - let handleSettings = (e: React.MouseEvent) => { - let menuItems: ContextMenuItem[] = []; - menuItems.push({ - label: "Focus Block", - click: () => { - alert("Not Implemented"); - }, - }); - menuItems.push({ label: "Minimize" }); - menuItems.push({ type: "separator" }); - menuItems.push({ - label: "Move to New Window", - click: () => { - let currentTabId = globalStore.get(atoms.activeTabId); - try { - services.WindowService.MoveBlockToNewWindow(currentTabId, blockData.oid); - } catch (e) { - console.error("error moving block to new window", e); - } - }, - }); - menuItems.push({ type: "separator" }); - menuItems.push({ - label: "Copy BlockId", - click: () => { - navigator.clipboard.writeText(blockData.oid); - }, - }); - menuItems.push({ type: "separator" }); - menuItems.push({ label: "Close", click: layoutModel?.onClose }); - ContextMenuModel.showContextMenu(menuItems, e); - }; - if (preview) { - handleSettings = null; - } - return (
handleHeaderContextMenu(e, blockData, layoutModel?.onClose)} + onContextMenu={(e) => handleHeaderContextMenu(e, blockData, viewModel, layoutModel?.onClose)} >
{hasBackButton && !hasForwardButton && ( @@ -296,7 +276,10 @@ const BlockFrame_Default_Component = ({ {util.isBlank(viewText) ? null :
{viewText}
}
-
+
handleHeaderContextMenu(e, blockData, viewModel, layoutModel?.onClose)} + >
diff --git a/frontend/app/view/preview.tsx b/frontend/app/view/preview.tsx index cf5db5243..c0f6b900a 100644 --- a/frontend/app/view/preview.tsx +++ b/frontend/app/view/preview.tsx @@ -119,6 +119,36 @@ export class PreviewModel implements ViewModel { const newPath = splitPath.join("/"); globalStore.set(this.fileName, newPath); } + + getSettingsMenuItems(): ContextMenuItem[] { + const menuItems: ContextMenuItem[] = []; + menuItems.push({ + label: "Copy Full Path", + click: () => { + const fileName = globalStore.get(this.fileName); + if (fileName == null) { + return; + } + navigator.clipboard.writeText(fileName); + }, + }); + menuItems.push({ + label: "Copy File Name", + click: () => { + let fileName = globalStore.get(this.fileName); + if (fileName == null) { + return; + } + if (fileName.endsWith("/")) { + fileName = fileName.substring(0, fileName.length - 1); + } + const splitPath = fileName.split("/"); + const baseName = splitPath[splitPath.length - 1]; + navigator.clipboard.writeText(baseName); + }, + }); + return menuItems; + } } function makePreviewModel(blockId: string): PreviewModel {