2024-05-13 23:40:18 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-08-02 00:35:13 +02:00
|
|
|
import { BlockComponentModel, BlockProps } from "@/app/block/blocktypes";
|
2024-08-01 22:06:18 +02:00
|
|
|
import { PlotView } from "@/app/view/plotview/plotview";
|
2024-08-01 21:53:49 +02:00
|
|
|
import { PreviewView, makePreviewModel } from "@/app/view/preview/preview";
|
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-08-02 00:35:13 +02:00
|
|
|
import { atoms, setBlockFocus, useBlockAtom } from "@/store/global";
|
2024-05-28 21:12:28 +02:00
|
|
|
import * as WOS from "@/store/wos";
|
2024-06-21 23:44:11 +02:00
|
|
|
import * as util from "@/util/util";
|
2024-08-01 22:06:18 +02:00
|
|
|
import { CpuPlotView, makeCpuPlotViewModel } from "@/view/cpuplot/cpuplot";
|
|
|
|
import { HelpView } from "@/view/helpview/helpview";
|
2024-07-23 02:08:18 +02:00
|
|
|
import { TerminalView, makeTerminalModel } from "@/view/term/term";
|
2024-08-01 22:06:18 +02:00
|
|
|
import { WaveAi, makeWaveAiViewModel } from "@/view/waveai/waveai";
|
|
|
|
import { WebView, makeWebViewModel } from "@/view/webview/webview";
|
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-08-02 00:35:13 +02:00
|
|
|
import { BlockFrame } from "./blockframe";
|
|
|
|
import { blockViewToIcon, blockViewToName } from "./blockutil";
|
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-07-09 00:04:48 +02:00
|
|
|
function getViewElemAndModel(
|
|
|
|
blockId: string,
|
|
|
|
blockView: string,
|
|
|
|
blockRef: React.RefObject<HTMLDivElement>
|
|
|
|
): { viewModel: ViewModel; viewElem: JSX.Element } {
|
|
|
|
let viewElem: JSX.Element = null;
|
|
|
|
let viewModel: ViewModel = null;
|
2024-07-30 21:33:28 +02:00
|
|
|
if (util.isBlank(blockView)) {
|
|
|
|
viewElem = <CenteredDiv>No View</CenteredDiv>;
|
|
|
|
viewModel = makeDefaultViewModel(blockId);
|
|
|
|
} else if (blockView === "term") {
|
2024-07-23 02:08:18 +02:00
|
|
|
const termViewModel = makeTerminalModel(blockId);
|
|
|
|
viewElem = <TerminalView key={blockId} blockId={blockId} model={termViewModel} />;
|
|
|
|
viewModel = termViewModel;
|
2024-07-09 00:04:48 +02:00
|
|
|
} else if (blockView === "preview") {
|
|
|
|
const previewModel = makePreviewModel(blockId);
|
|
|
|
viewElem = <PreviewView key={blockId} blockId={blockId} model={previewModel} />;
|
|
|
|
viewModel = previewModel;
|
|
|
|
} else if (blockView === "plot") {
|
|
|
|
viewElem = <PlotView key={blockId} />;
|
|
|
|
} else if (blockView === "web") {
|
2024-07-15 18:40:28 +02:00
|
|
|
const webviewModel = makeWebViewModel(blockId);
|
|
|
|
viewElem = <WebView key={blockId} parentRef={blockRef} model={webviewModel} />;
|
|
|
|
viewModel = webviewModel;
|
2024-07-09 00:04:48 +02:00
|
|
|
} else if (blockView === "waveai") {
|
2024-07-25 11:30:49 +02:00
|
|
|
const waveAiModel = makeWaveAiViewModel(blockId);
|
|
|
|
viewElem = <WaveAi key={blockId} model={waveAiModel} />;
|
|
|
|
viewModel = waveAiModel;
|
2024-07-31 23:13:36 +02:00
|
|
|
} else if (blockView === "cpuplot") {
|
|
|
|
const cpuPlotModel = makeCpuPlotViewModel(blockId);
|
|
|
|
viewElem = <CpuPlotView key={blockId} model={cpuPlotModel} />;
|
|
|
|
viewModel = cpuPlotModel;
|
2024-08-01 07:22:52 +02:00
|
|
|
} else if (blockView == "help") {
|
|
|
|
viewElem = <HelpView key={blockId} />;
|
|
|
|
viewModel = makeDefaultViewModel(blockId);
|
2024-06-26 18:31:43 +02:00
|
|
|
}
|
2024-07-09 00:04:48 +02:00
|
|
|
if (viewModel == null) {
|
2024-07-30 21:33:28 +02:00
|
|
|
viewElem = <CenteredDiv>Invalid View "{blockView}"</CenteredDiv>;
|
2024-07-09 00:04:48 +02:00
|
|
|
viewModel = makeDefaultViewModel(blockId);
|
|
|
|
}
|
|
|
|
return { viewElem, viewModel };
|
2024-06-26 18:31:43 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 00:04:48 +02:00
|
|
|
function makeDefaultViewModel(blockId: string): ViewModel {
|
|
|
|
const blockDataAtom = WOS.getWaveObjectAtom<Block>(WOS.makeORef("block", blockId));
|
|
|
|
let viewModel: ViewModel = {
|
|
|
|
viewIcon: jotai.atom((get) => {
|
|
|
|
const blockData = get(blockDataAtom);
|
2024-07-30 21:33:28 +02:00
|
|
|
return blockViewToIcon(blockData?.meta?.view);
|
2024-07-09 00:04:48 +02:00
|
|
|
}),
|
|
|
|
viewName: jotai.atom((get) => {
|
|
|
|
const blockData = get(blockDataAtom);
|
2024-07-30 21:33:28 +02:00
|
|
|
return blockViewToName(blockData?.meta?.view);
|
2024-07-09 00:04:48 +02:00
|
|
|
}),
|
|
|
|
viewText: jotai.atom((get) => {
|
|
|
|
const blockData = get(blockDataAtom);
|
|
|
|
return blockData?.meta?.title;
|
|
|
|
}),
|
2024-07-09 01:36:30 +02:00
|
|
|
preIconButton: jotai.atom(null),
|
|
|
|
endIconButtons: jotai.atom(null),
|
2024-07-09 00:04:48 +02:00
|
|
|
};
|
|
|
|
return viewModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlockPreview = React.memo(({ blockId, layoutModel }: BlockProps) => {
|
2024-07-31 21:49:38 +02:00
|
|
|
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
2024-07-09 00:04:48 +02:00
|
|
|
if (!blockData) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-07-30 21:33:28 +02:00
|
|
|
let { viewModel } = getViewElemAndModel(blockId, blockData?.meta?.view, null);
|
2024-07-09 00:04:48 +02:00
|
|
|
return (
|
|
|
|
<BlockFrame
|
|
|
|
key={blockId}
|
|
|
|
blockId={blockId}
|
|
|
|
layoutModel={layoutModel}
|
|
|
|
preview={true}
|
|
|
|
blockModel={null}
|
|
|
|
viewModel={viewModel}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const BlockFull = React.memo(({ blockId, layoutModel }: BlockProps) => {
|
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-07-03 23:34:55 +02:00
|
|
|
const [focusedChild, setFocusedChild] = React.useState(null);
|
|
|
|
const isFocusedAtom = useBlockAtom<boolean>(blockId, "isFocused", () => {
|
|
|
|
return jotai.atom((get) => {
|
|
|
|
const winData = get(atoms.waveWindow);
|
|
|
|
return winData.activeblockid === blockId;
|
|
|
|
});
|
|
|
|
});
|
2024-07-31 21:49:38 +02:00
|
|
|
const isFocused = jotai.useAtomValue(isFocusedAtom);
|
2024-07-03 23:34:55 +02:00
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
setBlockClicked(isFocused);
|
|
|
|
}, [isFocused]);
|
2024-06-19 20:58:22 +02:00
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
if (!blockClicked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setBlockClicked(false);
|
|
|
|
const focusWithin = blockRef.current?.contains(document.activeElement);
|
|
|
|
if (!focusWithin) {
|
2024-07-03 23:34:55 +02:00
|
|
|
setFocusTarget();
|
2024-06-19 20:58:22 +02:00
|
|
|
}
|
|
|
|
setBlockFocus(blockId);
|
|
|
|
}, [blockClicked]);
|
|
|
|
|
2024-07-03 23:34:55 +02:00
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
if (focusedChild == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setBlockFocus(blockId);
|
|
|
|
}, [focusedChild, blockId]);
|
|
|
|
|
|
|
|
// treat the block as clicked on creation
|
2024-06-26 18:31:43 +02:00
|
|
|
const setBlockClickedTrue = React.useCallback(() => {
|
|
|
|
setBlockClicked(true);
|
|
|
|
}, []);
|
|
|
|
|
2024-07-23 01:41:18 +02:00
|
|
|
let { viewElem, viewModel } = React.useMemo(
|
2024-07-30 21:33:28 +02:00
|
|
|
() => getViewElemAndModel(blockId, blockData?.meta?.view, blockRef),
|
|
|
|
[blockId, blockData?.meta?.view, blockRef]
|
2024-07-23 01:41:18 +02:00
|
|
|
);
|
|
|
|
|
2024-07-03 23:34:55 +02:00
|
|
|
const determineFocusedChild = React.useCallback(
|
|
|
|
(event: React.FocusEvent<HTMLDivElement, Element>) => {
|
|
|
|
setFocusedChild(event.target);
|
|
|
|
},
|
|
|
|
[setFocusedChild]
|
|
|
|
);
|
|
|
|
|
|
|
|
const setFocusTarget = React.useCallback(() => {
|
2024-07-23 01:41:18 +02:00
|
|
|
const ok = viewModel?.giveFocus?.();
|
|
|
|
if (ok) {
|
|
|
|
return;
|
2024-07-03 23:34:55 +02:00
|
|
|
}
|
2024-07-31 07:53:50 +02:00
|
|
|
focusElemRef.current?.focus({ preventScroll: true });
|
2024-07-23 01:41:18 +02:00
|
|
|
}, []);
|
2024-07-09 00:04:48 +02:00
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
if (!blockId || !blockData) return null;
|
2024-07-09 00:04:48 +02:00
|
|
|
|
2024-05-28 00:44:57 +02:00
|
|
|
if (blockDataLoading) {
|
2024-07-09 00:04:48 +02:00
|
|
|
viewElem = <CenteredDiv>Loading...</CenteredDiv>;
|
2024-05-14 08:45:41 +02:00
|
|
|
}
|
2024-07-09 00:04:48 +02:00
|
|
|
const blockModel: BlockComponentModel = {
|
|
|
|
onClick: setBlockClickedTrue,
|
|
|
|
onFocusCapture: determineFocusedChild,
|
|
|
|
blockRef: blockRef,
|
|
|
|
};
|
2024-07-03 23:34:55 +02:00
|
|
|
|
2024-05-13 23:40:18 +02:00
|
|
|
return (
|
2024-06-19 20:58:22 +02:00
|
|
|
<BlockFrame
|
2024-06-26 18:31:43 +02:00
|
|
|
key={blockId}
|
2024-06-19 20:58:22 +02:00
|
|
|
blockId={blockId}
|
2024-07-09 00:04:48 +02:00
|
|
|
layoutModel={layoutModel}
|
2024-06-19 20:58:22 +02:00
|
|
|
preview={false}
|
2024-07-09 00:04:48 +02:00
|
|
|
blockModel={blockModel}
|
|
|
|
viewModel={viewModel}
|
2024-06-19 20:58:22 +02:00
|
|
|
>
|
|
|
|
<div key="focuselem" className="block-focuselem">
|
2024-07-23 01:41:18 +02:00
|
|
|
<input type="text" value="" ref={focusElemRef} id={`${blockId}-dummy-focus`} onChange={() => {}} />
|
2024-06-19 20:58:22 +02:00
|
|
|
</div>
|
2024-07-31 21:49:38 +02:00
|
|
|
<div
|
|
|
|
key="content"
|
|
|
|
className="block-content"
|
|
|
|
style={{ pointerEvents: layoutModel?.disablePointerEvents ? "none" : undefined }}
|
|
|
|
>
|
2024-06-03 20:35:06 +02:00
|
|
|
<ErrorBoundary>
|
2024-07-09 00:04:48 +02:00
|
|
|
<React.Suspense fallback={<CenteredDiv>Loading...</CenteredDiv>}>{viewElem}</React.Suspense>
|
2024-06-03 20:35:06 +02:00
|
|
|
</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-26 18:31:43 +02:00
|
|
|
});
|
2024-05-13 23:40:18 +02:00
|
|
|
|
2024-07-09 00:04:48 +02:00
|
|
|
const Block = React.memo((props: BlockProps) => {
|
|
|
|
if (props.preview) {
|
|
|
|
return <BlockPreview {...props} />;
|
|
|
|
}
|
|
|
|
return <BlockFull {...props} />;
|
|
|
|
});
|
|
|
|
|
|
|
|
export { Block };
|