// Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 import { blockViewToIcon, blockViewToName, ConnectionButton, getBlockHeaderIcon, IconButton, Input, } from "@/app/block/blockutil"; import { Button } from "@/app/element/button"; import { ContextMenuModel } from "@/app/store/contextmenu"; import { atoms, globalStore, WOS } from "@/app/store/global"; import * as services from "@/app/store/services"; import { MagnifyIcon } from "@/element/magnify"; import { NodeModel } from "@/layout/index"; import { checkKeyPressed, keydownWrapper } from "@/util/keyutil"; import * as util from "@/util/util"; import clsx from "clsx"; import * as jotai from "jotai"; import * as React from "react"; import { BlockFrameProps } from "./blocktypes"; function handleHeaderContextMenu( e: React.MouseEvent, blockData: Block, viewModel: ViewModel, magnified: boolean, onMagnifyToggle: () => void, onClose: () => void ) { e.preventDefault(); e.stopPropagation(); let menu: ContextMenuItem[] = [ { label: magnified ? "Un-magnify Block" : "Magnify Block", click: () => { onMagnifyToggle(); }, }, { label: "Move to New Window", click: () => { const currentTabId = globalStore.get(atoms.activeTabId); try { services.WindowService.MoveBlockToNewWindow(currentTabId, blockData.oid); } catch (e) { console.error("error moving block to new window", e); } }, }, { type: "separator" }, { label: "Copy BlockId", click: () => { navigator.clipboard.writeText(blockData.oid); }, }, ]; const extraItems = viewModel?.getSettingsMenuItems?.(); if (extraItems && extraItems.length > 0) menu.push({ type: "separator" }, ...extraItems); menu.push( { type: "separator" }, { label: "Close Block", click: onClose, } ); ContextMenuModel.showContextMenu(menu, e); } function getViewIconElem(viewIconUnion: string | HeaderIconButton, blockData: Block): JSX.Element { if (viewIconUnion == null || typeof viewIconUnion === "string") { const viewIcon = viewIconUnion as string; return
{getBlockHeaderIcon(viewIcon, blockData)}
; } else { return ; } } const OptMagnifyButton = React.memo( ({ magnified, toggleMagnify }: { magnified: boolean; toggleMagnify: () => void }) => { const magnifyDecl: HeaderIconButton = { elemtype: "iconbutton", icon: , title: magnified ? "Minimize" : "Magnify", click: toggleMagnify, }; return ; } ); function computeEndIcons( viewModel: ViewModel, magnified: boolean, toggleMagnify: () => void, onClose: () => void, onContextMenu: (e: React.MouseEvent) => void ): JSX.Element[] { const endIconsElem: JSX.Element[] = []; const endIconButtons = util.useAtomValueSafe(viewModel.endIconButtons); if (endIconButtons && endIconButtons.length > 0) { endIconsElem.push(...endIconButtons.map((button, idx) => )); } const settingsDecl: HeaderIconButton = { elemtype: "iconbutton", icon: "cog", title: "Settings", click: onContextMenu, }; endIconsElem.push(); endIconsElem.push(); const closeDecl: HeaderIconButton = { elemtype: "iconbutton", icon: "xmark-large", title: "Close", click: onClose, }; endIconsElem.push(); return endIconsElem; } const BlockFrame_Header = ({ nodeModel, viewModel, preview }: BlockFrameProps) => { const [blockData] = WOS.useWaveObjectValue(WOS.makeORef("block", nodeModel.blockId)); const viewName = util.useAtomValueSafe(viewModel.viewName) ?? blockViewToName(blockData?.meta?.view); const settingsConfig = jotai.useAtomValue(atoms.settingsConfigAtom); const viewIconUnion = util.useAtomValueSafe(viewModel.viewIcon) ?? blockViewToIcon(blockData?.meta?.view); const preIconButton = util.useAtomValueSafe(viewModel.preIconButton); const headerTextUnion = util.useAtomValueSafe(viewModel.viewText); const magnified = jotai.useAtomValue(nodeModel.isMagnified); const dragHandleRef = preview ? null : nodeModel.dragHandleRef; const onContextMenu = React.useCallback( (e: React.MouseEvent) => { handleHeaderContextMenu(e, blockData, viewModel, magnified, nodeModel.toggleMagnify, nodeModel.onClose); }, [magnified] ); const endIconsElem = computeEndIcons( viewModel, magnified, nodeModel.toggleMagnify, nodeModel.onClose, onContextMenu ); const viewIconElem = getViewIconElem(viewIconUnion, blockData); let preIconButtonElem: JSX.Element = null; if (preIconButton) { preIconButtonElem = ; } const headerTextElems: JSX.Element[] = []; if (typeof headerTextUnion === "string") { if (!util.isBlank(headerTextUnion)) { headerTextElems.push(
{headerTextUnion}
); } } else if (Array.isArray(headerTextUnion)) { headerTextElems.push(...renderHeaderElements(headerTextUnion, preview)); } return (
{preIconButtonElem}
{viewIconElem}
{viewName}
{settingsConfig?.blockheader?.showblockids && (
[{nodeModel.blockId.substring(0, 8)}]
)}
{headerTextElems}
{endIconsElem}
); }; const HeaderTextElem = React.memo(({ elem, preview }: { elem: HeaderElem; preview: boolean }) => { if (elem.elemtype == "iconbutton") { return ; } else if (elem.elemtype == "input") { return ; } else if (elem.elemtype == "text") { return
{elem.text}
; } else if (elem.elemtype == "textbutton") { return ( ); } else if (elem.elemtype == "connectionbutton") { return ; } else if (elem.elemtype == "div") { return (
{elem.children.map((child, childIdx) => ( ))}
); } return null; }); function renderHeaderElements(headerTextUnion: HeaderElem[], preview: boolean): JSX.Element[] { const headerTextElems: JSX.Element[] = []; for (let idx = 0; idx < headerTextUnion.length; idx++) { const elem = headerTextUnion[idx]; const renderedElement = ; if (renderedElement) { headerTextElems.push(renderedElement); } } return headerTextElems; } const BlockMask = ({ nodeModel }: { nodeModel: NodeModel }) => { const isFocused = jotai.useAtomValue(nodeModel.isFocused); const blockNum = jotai.useAtomValue(nodeModel.blockNum); const isLayoutMode = jotai.useAtomValue(atoms.controlShiftDelayAtom); const [blockData] = WOS.useWaveObjectValue(WOS.makeORef("block", nodeModel.blockId)); const style: React.CSSProperties = {}; if (!isFocused && blockData?.meta?.["frame:bordercolor"]) { style.borderColor = blockData.meta["frame:bordercolor"]; } if (isFocused && blockData?.meta?.["frame:bordercolor:focused"]) { style.borderColor = blockData.meta["frame:bordercolor:focused"]; } let innerElem = null; if (isLayoutMode) { innerElem = (
{blockNum}
); } return (
{innerElem}
); }; const BlockFrame_Default_Component = (props: BlockFrameProps) => { const { nodeModel, viewModel, blockModel, preview, numBlocksInTab, children } = props; const [blockData] = WOS.useWaveObjectValue(WOS.makeORef("block", nodeModel.blockId)); const isFocused = jotai.useAtomValue(nodeModel.isFocused); const viewIconUnion = util.useAtomValueSafe(viewModel.viewIcon) ?? blockViewToIcon(blockData?.meta?.view); const customBg = util.useAtomValueSafe(viewModel.blockBg); const viewIconElem = getViewIconElem(viewIconUnion, blockData); function handleKeyDown(waveEvent: WaveKeyboardEvent): boolean { if (checkKeyPressed(waveEvent, "Cmd:m")) { nodeModel.toggleMagnify(); return true; } if (viewModel?.keyDownHandler) { return viewModel.keyDownHandler(waveEvent); } return false; } const innerStyle: React.CSSProperties = {}; if (!preview && customBg?.bg != null) { innerStyle.background = customBg.bg; if (customBg["bg:opacity"] != null) { innerStyle.opacity = customBg["bg:opacity"]; } if (customBg["bg:blendmode"] != null) { innerStyle.backgroundBlendMode = customBg["bg:blendmode"]; } } const previewElem =
{viewIconElem}
; return (
{preview ? previewElem : children}
); }; const BlockFrame_Default = React.memo(BlockFrame_Default_Component) as typeof BlockFrame_Default_Component; const BlockFrame = React.memo((props: BlockFrameProps) => { const blockId = props.nodeModel.blockId; const [blockData] = WOS.useWaveObjectValue(WOS.makeORef("block", blockId)); const tabData = jotai.useAtomValue(atoms.tabAtom); if (!blockId || !blockData) { return null; } let FrameElem = BlockFrame_Default; const numBlocks = tabData?.blockids?.length ?? 0; return ; }); export { BlockFrame };