2024-05-13 23:40:18 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-06-14 01:49:25 +02:00
|
|
|
import { CodeEdit } from "@/app/view/codeedit";
|
2024-05-15 22:36:50 +02:00
|
|
|
import { PlotView } from "@/app/view/plotview";
|
2024-05-28 21:12:28 +02:00
|
|
|
import { PreviewView } from "@/app/view/preview";
|
2024-06-17 19:18:38 +02:00
|
|
|
import { TerminalView } from "@/app/view/term/term";
|
2024-06-03 20:35:06 +02:00
|
|
|
import { ErrorBoundary } from "@/element/errorboundary";
|
2024-05-17 07:48:23 +02:00
|
|
|
import { CenteredDiv } from "@/element/quickelems";
|
2024-06-19 20:58:22 +02:00
|
|
|
import { atoms, globalStore, useBlockAtom } from "@/store/global";
|
2024-05-28 21:12:28 +02:00
|
|
|
import * as WOS from "@/store/wos";
|
2024-06-14 19:46:03 +02:00
|
|
|
import clsx from "clsx";
|
2024-06-19 20:58:22 +02:00
|
|
|
import { produce } from "immer";
|
2024-06-19 08:44:53 +02:00
|
|
|
import * as jotai from "jotai";
|
2024-05-28 21:12:28 +02:00
|
|
|
import * as React from "react";
|
2024-05-13 23:40:18 +02:00
|
|
|
|
2024-05-14 08:45:41 +02:00
|
|
|
import "./block.less";
|
2024-05-13 23:40:18 +02:00
|
|
|
|
2024-06-14 19:46:03 +02:00
|
|
|
const HoverPixels = 15;
|
|
|
|
const HoverTimeoutMs = 100;
|
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
interface BlockProps {
|
|
|
|
blockId: string;
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
onClose?: () => void;
|
2024-06-20 08:00:57 +02:00
|
|
|
dragHandleRef?: React.RefObject<HTMLDivElement>;
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
2024-05-16 22:22:46 +02:00
|
|
|
|
2024-06-19 08:44:53 +02:00
|
|
|
function getBlockHeaderText(blockData: Block): string {
|
|
|
|
if (!blockData) {
|
|
|
|
return "no block data";
|
|
|
|
}
|
|
|
|
return `${blockData?.view} [${blockData.oid.substring(0, 8)}]`;
|
|
|
|
}
|
|
|
|
|
2024-06-20 08:00:57 +02:00
|
|
|
interface FramelessBlockHeaderProps {
|
|
|
|
blockId: string;
|
|
|
|
onClose?: () => void;
|
|
|
|
dragHandleRef?: React.RefObject<HTMLDivElement>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FramelessBlockHeader = ({ blockId, onClose, dragHandleRef }: FramelessBlockHeaderProps) => {
|
2024-06-11 22:19:29 +02:00
|
|
|
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
|
|
|
|
return (
|
2024-06-20 08:00:57 +02:00
|
|
|
<div key="header" className="block-header" ref={dragHandleRef}>
|
2024-06-19 08:44:53 +02:00
|
|
|
<div className="block-header-text text-fixed">{getBlockHeaderText(blockData)}</div>
|
Implement outer drop direction, add rudimentary drag preview image rendering (#29)
This PR adds support for Outer variants of each DropDirection.
When calculating the drop direction, the cursor position is calculated
relevant to the box over which it is hovering. The following diagram
shows how drop directions are calculated. The colored in center is
currently not supported, it is assigned to the top, bottom, left, right
direction for now, though it will ultimately be its own distinct
direction.
![IMG_3505](https://github.com/wavetermdev/thenextwave/assets/16651283/a7ea7387-b95d-4831-9e29-d3225b824c97)
When an outer drop direction is provided for a move operation, if the
reference node flexes in the same axis as the drop direction, the new
node will be inserted at the same level as the parent of the reference
node. If the reference node flexes in a different direction or the
reference node does not have a grandparent, the operation will fall back
to its non-Outer variant.
This also removes some chatty debug statements, adds a blur to the
currently-dragging node to indicate that it cannot be dropped onto, and
simplifies the deriving of the layout state atom from the tab atom so
there's no longer another intermediate derived atom for the layout node.
This also adds rudimentary support for rendering custom preview images
for any tile being dragged. Right now, this is a simple block containing
the block ID, but this can be anything. This resolves an issue where
letting React-DnD generate its own previews could take up to a half
second, and would block dragging until complete. For Monaco, this was
outright failing.
It also fixes an issue where the tile layout could animate on first
paint. Now, I use React Suspense to prevent the layout from displaying
until all the children have loaded.
2024-06-11 22:03:41 +02:00
|
|
|
{onClose && (
|
|
|
|
<div className="close-button" onClick={onClose}>
|
|
|
|
<i className="fa fa-solid fa-xmark-large" />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-14 19:46:03 +02:00
|
|
|
const hoverStateOff = "off";
|
|
|
|
const hoverStatePending = "pending";
|
|
|
|
const hoverStateOn = "on";
|
|
|
|
|
2024-06-19 08:44:53 +02:00
|
|
|
interface BlockFrameProps {
|
|
|
|
blockId: string;
|
|
|
|
onClose?: () => void;
|
2024-06-19 20:58:22 +02:00
|
|
|
onClick?: () => void;
|
2024-06-19 08:44:53 +02:00
|
|
|
preview: boolean;
|
|
|
|
children?: React.ReactNode;
|
2024-06-19 20:58:22 +02:00
|
|
|
blockRef?: React.RefObject<HTMLDivElement>;
|
2024-06-20 08:00:57 +02:00
|
|
|
dragHandleRef?: React.RefObject<HTMLDivElement>;
|
2024-06-19 08:44:53 +02:00
|
|
|
}
|
|
|
|
|
2024-06-20 08:00:57 +02:00
|
|
|
const BlockFrame_Tech = ({
|
|
|
|
blockId,
|
|
|
|
onClose,
|
|
|
|
onClick,
|
|
|
|
preview,
|
|
|
|
blockRef,
|
|
|
|
dragHandleRef,
|
|
|
|
children,
|
|
|
|
}: BlockFrameProps) => {
|
2024-06-19 08:44:53 +02:00
|
|
|
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
|
|
|
const isFocusedAtom = useBlockAtom<boolean>(blockId, "isFocused", () => {
|
|
|
|
return jotai.atom((get) => {
|
|
|
|
const winData = get(atoms.waveWindow);
|
|
|
|
return winData.activeblockid === blockId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let isFocused = jotai.useAtomValue(isFocusedAtom);
|
|
|
|
if (preview) {
|
|
|
|
isFocused = true;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
"block",
|
|
|
|
"block-frame-tech",
|
|
|
|
isFocused ? "block-focused" : null,
|
|
|
|
preview ? "block-preview" : null
|
|
|
|
)}
|
2024-06-19 20:58:22 +02:00
|
|
|
onClick={onClick}
|
|
|
|
ref={blockRef}
|
2024-06-19 08:44:53 +02:00
|
|
|
>
|
2024-06-20 08:00:57 +02:00
|
|
|
<div className="block-frame-tech-header" ref={dragHandleRef}>
|
|
|
|
{getBlockHeaderText(blockData)}
|
|
|
|
</div>
|
2024-06-19 08:44:53 +02:00
|
|
|
<div className="block-frame-tech-close" onClick={onClose}>
|
|
|
|
<i className="fa fa-solid fa-xmark fa-fw " />
|
|
|
|
</div>
|
|
|
|
{preview ? <div className="block-frame-preview" /> : children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-20 08:00:57 +02:00
|
|
|
const BlockFrame_Frameless = ({
|
|
|
|
blockId,
|
|
|
|
onClose,
|
|
|
|
onClick,
|
|
|
|
preview,
|
|
|
|
blockRef,
|
|
|
|
dragHandleRef,
|
|
|
|
children,
|
|
|
|
}: BlockFrameProps) => {
|
2024-06-19 20:58:22 +02:00
|
|
|
const localBlockRef = React.useRef<HTMLDivElement>(null);
|
2024-06-19 08:44:53 +02:00
|
|
|
const [showHeader, setShowHeader] = React.useState(preview ? true : false);
|
2024-06-14 19:46:03 +02:00
|
|
|
const hoverState = React.useRef(hoverStateOff);
|
|
|
|
|
2024-06-19 20:58:22 +02:00
|
|
|
// this forward the lcal ref to the blockRef
|
|
|
|
React.useImperativeHandle(blockRef, () => localBlockRef.current);
|
|
|
|
|
2024-06-14 19:46:03 +02:00
|
|
|
React.useEffect(() => {
|
2024-06-19 08:44:53 +02:00
|
|
|
if (preview) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-19 20:58:22 +02:00
|
|
|
const block = localBlockRef.current;
|
2024-06-14 19:46:03 +02:00
|
|
|
let hoverTimeout: NodeJS.Timeout = null;
|
|
|
|
const handleMouseMove = (event) => {
|
|
|
|
const rect = block.getBoundingClientRect();
|
|
|
|
if (event.clientY - rect.top <= HoverPixels) {
|
|
|
|
if (hoverState.current == hoverStateOff) {
|
|
|
|
hoverTimeout = setTimeout(() => {
|
|
|
|
if (hoverState.current == hoverStatePending) {
|
|
|
|
hoverState.current = hoverStateOn;
|
|
|
|
setShowHeader(true);
|
|
|
|
}
|
|
|
|
}, HoverTimeoutMs);
|
|
|
|
hoverState.current = hoverStatePending;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hoverTimeout) {
|
|
|
|
if (hoverState.current == hoverStatePending) {
|
|
|
|
hoverState.current = hoverStateOff;
|
|
|
|
}
|
|
|
|
clearTimeout(hoverTimeout);
|
|
|
|
hoverTimeout = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
block.addEventListener("mousemove", handleMouseMove);
|
|
|
|
return () => {
|
|
|
|
block.removeEventListener("mousemove", handleMouseMove);
|
|
|
|
};
|
|
|
|
});
|
2024-06-19 08:44:53 +02:00
|
|
|
let mouseLeaveHandler = () => {
|
|
|
|
if (preview) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setShowHeader(false);
|
|
|
|
hoverState.current = hoverStateOff;
|
|
|
|
};
|
|
|
|
return (
|
2024-06-19 20:58:22 +02:00
|
|
|
<div
|
|
|
|
className="block block-frame-frameless"
|
|
|
|
ref={localBlockRef}
|
|
|
|
onMouseLeave={mouseLeaveHandler}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
2024-06-19 08:44:53 +02:00
|
|
|
<div
|
|
|
|
className={clsx("block-header-animation-wrap", showHeader ? "is-showing" : null)}
|
|
|
|
onMouseLeave={mouseLeaveHandler}
|
|
|
|
>
|
2024-06-20 08:00:57 +02:00
|
|
|
<FramelessBlockHeader blockId={blockId} onClose={onClose} dragHandleRef={dragHandleRef} />
|
2024-06-19 08:44:53 +02:00
|
|
|
</div>
|
|
|
|
{preview ? <div className="block-frame-preview" /> : children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const BlockFrame = (props: BlockFrameProps) => {
|
|
|
|
const blockId = props.blockId;
|
|
|
|
const [blockData, blockDataLoading] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
|
|
|
const tabData = jotai.useAtomValue(atoms.tabAtom);
|
2024-05-24 23:08:24 +02:00
|
|
|
|
2024-06-19 08:44:53 +02:00
|
|
|
if (!blockId || !blockData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// if 0 or 1 blocks, use frameless, otherwise use tech
|
|
|
|
const numBlocks = tabData?.blockids?.length ?? 0;
|
|
|
|
if (numBlocks <= 1) {
|
|
|
|
return <BlockFrame_Frameless {...props} />;
|
|
|
|
}
|
|
|
|
return <BlockFrame_Tech {...props} />;
|
|
|
|
};
|
|
|
|
|
2024-06-19 20:58:22 +02:00
|
|
|
function setBlockFocus(blockId: string) {
|
|
|
|
let winData = globalStore.get(atoms.waveWindow);
|
|
|
|
if (winData.activeblockid === blockId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
winData = produce(winData, (draft) => {
|
|
|
|
draft.activeblockid = blockId;
|
|
|
|
});
|
|
|
|
WOS.setObjectValue(winData, globalStore.set, true);
|
|
|
|
}
|
|
|
|
|
2024-06-20 08:00:57 +02:00
|
|
|
const Block = ({ blockId, onClose, dragHandleRef }: BlockProps) => {
|
2024-05-14 08:45:41 +02:00
|
|
|
let blockElem: JSX.Element = null;
|
2024-06-19 20:58:22 +02:00
|
|
|
const focusElemRef = React.useRef<HTMLInputElement>(null);
|
|
|
|
const blockRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
const [blockClicked, setBlockClicked] = React.useState(false);
|
2024-05-28 00:44:57 +02:00
|
|
|
const [blockData, blockDataLoading] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
2024-06-19 20:58:22 +02:00
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
if (!blockClicked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setBlockClicked(false);
|
|
|
|
const focusWithin = blockRef.current?.contains(document.activeElement);
|
|
|
|
if (!focusWithin) {
|
|
|
|
focusElemRef.current?.focus();
|
|
|
|
}
|
|
|
|
setBlockFocus(blockId);
|
|
|
|
}, [blockClicked]);
|
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
if (!blockId || !blockData) return null;
|
2024-05-28 00:44:57 +02:00
|
|
|
if (blockDataLoading) {
|
|
|
|
blockElem = <CenteredDiv>Loading...</CenteredDiv>;
|
|
|
|
} else if (blockData.view === "term") {
|
2024-05-14 08:45:41 +02:00
|
|
|
blockElem = <TerminalView blockId={blockId} />;
|
|
|
|
} else if (blockData.view === "preview") {
|
|
|
|
blockElem = <PreviewView blockId={blockId} />;
|
2024-05-15 22:36:50 +02:00
|
|
|
} else if (blockData.view === "plot") {
|
|
|
|
blockElem = <PlotView />;
|
2024-06-03 20:35:06 +02:00
|
|
|
} else if (blockData.view === "codeedit") {
|
2024-06-14 08:54:04 +02:00
|
|
|
blockElem = <CodeEdit text={null} filename={null} />;
|
2024-05-14 08:45:41 +02:00
|
|
|
}
|
2024-05-13 23:40:18 +02:00
|
|
|
return (
|
2024-06-19 20:58:22 +02:00
|
|
|
<BlockFrame
|
|
|
|
blockId={blockId}
|
|
|
|
onClose={onClose}
|
|
|
|
preview={false}
|
|
|
|
onClick={() => setBlockClicked(true)}
|
|
|
|
blockRef={blockRef}
|
2024-06-20 08:00:57 +02:00
|
|
|
dragHandleRef={dragHandleRef}
|
2024-06-19 20:58:22 +02:00
|
|
|
>
|
|
|
|
<div key="focuselem" className="block-focuselem">
|
|
|
|
<input type="text" value="" ref={focusElemRef} onChange={() => {}} />
|
|
|
|
</div>
|
2024-05-14 08:45:41 +02:00
|
|
|
<div key="content" className="block-content">
|
2024-06-03 20:35:06 +02:00
|
|
|
<ErrorBoundary>
|
|
|
|
<React.Suspense fallback={<CenteredDiv>Loading...</CenteredDiv>}>{blockElem}</React.Suspense>
|
|
|
|
</ErrorBoundary>
|
2024-05-14 08:45:41 +02:00
|
|
|
</div>
|
2024-06-19 08:44:53 +02:00
|
|
|
</BlockFrame>
|
2024-05-13 23:40:18 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-19 08:44:53 +02:00
|
|
|
export { Block, BlockFrame };
|