2024-05-13 23:40:18 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-07-09 08:13:12 +02:00
|
|
|
import { useLongClick } from "@/app/hook/useLongClick";
|
2024-07-18 08:41:33 +02:00
|
|
|
import { Button } from "@/element/button";
|
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-20 21:34:32 +02:00
|
|
|
import { ContextMenuModel } from "@/store/contextmenu";
|
2024-06-25 23:56:37 +02:00
|
|
|
import { atoms, globalStore, setBlockFocus, useBlockAtom } from "@/store/global";
|
|
|
|
import * as services from "@/store/services";
|
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-07-09 00:04:48 +02:00
|
|
|
import { PlotView } from "@/view/plotview";
|
|
|
|
import { PreviewView, makePreviewModel } from "@/view/preview";
|
2024-07-23 02:08:18 +02:00
|
|
|
import { TerminalView, makeTerminalModel } from "@/view/term/term";
|
2024-07-25 11:30:49 +02:00
|
|
|
import { WaveAi, makeWaveAiViewModel } from "@/view/waveai";
|
2024-07-15 18:40:28 +02:00
|
|
|
import { WebView, makeWebViewModel } from "@/view/webview";
|
2024-06-14 19:46:03 +02:00
|
|
|
import clsx from "clsx";
|
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-07-09 00:04:48 +02:00
|
|
|
interface LayoutComponentModel {
|
|
|
|
onClose?: () => void;
|
|
|
|
dragHandleRef?: React.RefObject<HTMLDivElement>;
|
|
|
|
}
|
2024-06-14 19:46:03 +02:00
|
|
|
|
2024-06-04 22:05:44 +02:00
|
|
|
interface BlockProps {
|
|
|
|
blockId: string;
|
2024-07-09 00:04:48 +02:00
|
|
|
preview: boolean;
|
|
|
|
layoutModel: LayoutComponentModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BlockComponentModel {
|
|
|
|
onClick?: () => void;
|
|
|
|
onFocusCapture?: React.FocusEventHandler<HTMLDivElement>;
|
|
|
|
blockRef?: React.RefObject<HTMLDivElement>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BlockFrameProps {
|
|
|
|
blockId: string;
|
|
|
|
blockModel?: BlockComponentModel;
|
|
|
|
layoutModel?: LayoutComponentModel;
|
|
|
|
viewModel?: ViewModel;
|
|
|
|
preview: boolean;
|
|
|
|
numBlocksInTab?: number;
|
|
|
|
children?: React.ReactNode;
|
2024-06-04 22:05:44 +02:00
|
|
|
}
|
2024-06-22 00:15:38 +02:00
|
|
|
|
|
|
|
const colorRegex = /^((#[0-9a-f]{6,8})|([a-z]+))$/;
|
|
|
|
|
|
|
|
function processTitleString(titleString: string): React.ReactNode[] {
|
|
|
|
if (titleString == null) {
|
2024-06-21 23:44:11 +02:00
|
|
|
return null;
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
const tagRegex = /<(\/)?([a-z]+)(?::([#a-z0-9@-]+))?>/g;
|
2024-06-21 23:44:11 +02:00
|
|
|
let lastIdx = 0;
|
|
|
|
let match;
|
|
|
|
let partsStack = [[]];
|
2024-06-22 00:15:38 +02:00
|
|
|
while ((match = tagRegex.exec(titleString)) != null) {
|
2024-06-21 23:44:11 +02:00
|
|
|
const lastPart = partsStack[partsStack.length - 1];
|
2024-06-22 00:15:38 +02:00
|
|
|
const before = titleString.substring(lastIdx, match.index);
|
2024-06-21 23:44:11 +02:00
|
|
|
lastPart.push(before);
|
|
|
|
lastIdx = match.index + match[0].length;
|
|
|
|
const [_, isClosing, tagName, tagParam] = match;
|
|
|
|
if (tagName == "icon" && !isClosing) {
|
|
|
|
if (tagParam == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
const iconClass = util.makeIconClass(tagParam, false);
|
|
|
|
if (iconClass == null) {
|
2024-06-21 23:44:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
lastPart.push(<i key={match.index} className={iconClass} />);
|
2024-06-21 23:44:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tagName == "c" || tagName == "color") {
|
|
|
|
if (isClosing) {
|
|
|
|
if (partsStack.length <= 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
partsStack.pop();
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-22 00:15:38 +02:00
|
|
|
if (tagParam == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!tagParam.match(colorRegex)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-21 23:44:11 +02:00
|
|
|
let children = [];
|
|
|
|
const rtag = React.createElement("span", { key: match.index, style: { color: tagParam } }, children);
|
|
|
|
lastPart.push(rtag);
|
|
|
|
partsStack.push(children);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tagName == "i" || tagName == "b") {
|
|
|
|
if (isClosing) {
|
|
|
|
if (partsStack.length <= 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
partsStack.pop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let children = [];
|
|
|
|
const rtag = React.createElement(tagName, { key: match.index }, children);
|
|
|
|
lastPart.push(rtag);
|
|
|
|
partsStack.push(children);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-06-22 00:15:38 +02:00
|
|
|
partsStack[partsStack.length - 1].push(titleString.substring(lastIdx));
|
2024-06-21 23:44:11 +02:00
|
|
|
return partsStack[0];
|
|
|
|
}
|
2024-05-16 22:22:46 +02:00
|
|
|
|
2024-07-06 00:47:35 +02:00
|
|
|
function getBlockHeaderIcon(blockIcon: string, blockData: Block): React.ReactNode {
|
|
|
|
let blockIconElem: React.ReactNode = null;
|
|
|
|
if (util.isBlank(blockIcon)) {
|
|
|
|
blockIcon = "square";
|
|
|
|
}
|
|
|
|
let iconColor = blockData?.meta?.["icon:color"];
|
|
|
|
if (iconColor && !iconColor.match(colorRegex)) {
|
|
|
|
iconColor = null;
|
|
|
|
}
|
|
|
|
let iconStyle = null;
|
|
|
|
if (!util.isBlank(iconColor)) {
|
|
|
|
iconStyle = { color: iconColor };
|
|
|
|
}
|
|
|
|
const iconClass = util.makeIconClass(blockIcon, true);
|
|
|
|
if (iconClass != null) {
|
|
|
|
blockIconElem = <i key="icon" style={iconStyle} className={clsx(`block-frame-icon`, iconClass)} />;
|
|
|
|
}
|
|
|
|
return blockIconElem;
|
|
|
|
}
|
|
|
|
|
2024-06-25 01:25:53 +02:00
|
|
|
function getBlockHeaderText(blockIcon: string, blockData: Block, settings: SettingsConfigType): React.ReactNode {
|
2024-06-19 08:44:53 +02:00
|
|
|
if (!blockData) {
|
|
|
|
return "no block data";
|
|
|
|
}
|
2024-06-25 01:25:53 +02:00
|
|
|
let blockIdStr = "";
|
|
|
|
if (settings?.blockheader?.showblockids) {
|
|
|
|
blockIdStr = ` [${blockData.oid.substring(0, 8)}]`;
|
|
|
|
}
|
2024-07-06 00:47:35 +02:00
|
|
|
let blockIconElem = getBlockHeaderIcon(blockIcon, blockData);
|
2024-06-21 23:44:11 +02:00
|
|
|
if (!util.isBlank(blockData?.meta?.title)) {
|
|
|
|
try {
|
2024-06-22 00:15:38 +02:00
|
|
|
const rtn = processTitleString(blockData.meta.title) ?? [];
|
2024-06-25 01:25:53 +02:00
|
|
|
return [blockIconElem, ...rtn, blockIdStr == "" ? null : blockIdStr];
|
2024-06-21 23:44:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error("error processing title", blockData.meta.title, e);
|
2024-06-25 01:25:53 +02:00
|
|
|
return [blockIconElem, blockData.meta.title + blockIdStr];
|
2024-06-21 23:44:11 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-24 23:34:31 +02:00
|
|
|
let viewString = blockData?.view;
|
|
|
|
if (blockData.controller == "cmd") {
|
|
|
|
viewString = "cmd";
|
|
|
|
}
|
2024-06-25 01:25:53 +02:00
|
|
|
return [blockIconElem, viewString + blockIdStr];
|
2024-06-19 08:44:53 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 00:32:10 +02:00
|
|
|
function handleHeaderContextMenu(
|
|
|
|
e: React.MouseEvent<HTMLDivElement>,
|
|
|
|
blockData: Block,
|
|
|
|
viewModel: ViewModel,
|
|
|
|
onClose: () => void
|
|
|
|
) {
|
2024-06-25 01:34:56 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
let menu: ContextMenuItem[] = [];
|
|
|
|
menu.push({
|
|
|
|
label: "Focus Block",
|
|
|
|
click: () => {
|
|
|
|
alert("Not Implemented");
|
|
|
|
},
|
|
|
|
});
|
2024-07-09 00:32:10 +02:00
|
|
|
menu.push({
|
|
|
|
label: "Minimize",
|
|
|
|
click: () => {
|
|
|
|
alert("Not Implemented");
|
|
|
|
},
|
|
|
|
});
|
2024-06-25 01:34:56 +02:00
|
|
|
menu.push({
|
|
|
|
label: "Move to New Window",
|
|
|
|
click: () => {
|
2024-06-25 23:56:37 +02:00
|
|
|
let currentTabId = globalStore.get(atoms.activeTabId);
|
|
|
|
try {
|
|
|
|
services.WindowService.MoveBlockToNewWindow(currentTabId, blockData.oid);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("error moving block to new window", e);
|
|
|
|
}
|
2024-06-25 01:34:56 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
menu.push({ type: "separator" });
|
2024-06-25 02:44:31 +02:00
|
|
|
menu.push({
|
|
|
|
label: "Copy BlockId",
|
|
|
|
click: () => {
|
|
|
|
navigator.clipboard.writeText(blockData.oid);
|
|
|
|
},
|
|
|
|
});
|
2024-07-09 00:32:10 +02:00
|
|
|
const extraItems = viewModel?.getSettingsMenuItems?.();
|
|
|
|
if (extraItems && extraItems.length > 0) {
|
|
|
|
menu.push({ type: "separator" });
|
|
|
|
menu.push(...extraItems);
|
|
|
|
}
|
2024-06-25 02:44:31 +02:00
|
|
|
menu.push({ type: "separator" });
|
2024-06-25 01:34:56 +02:00
|
|
|
menu.push({
|
|
|
|
label: "Close Block",
|
|
|
|
click: onClose,
|
|
|
|
});
|
|
|
|
ContextMenuModel.showContextMenu(menu, e);
|
|
|
|
}
|
|
|
|
|
2024-07-09 08:13:12 +02:00
|
|
|
const IconButton = React.memo(({ decl, className }: { decl: HeaderIconButton; className?: string }) => {
|
|
|
|
const buttonRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
useLongClick(buttonRef, decl.click, decl.longClick);
|
|
|
|
return (
|
|
|
|
<div ref={buttonRef} className={clsx(className)} title={decl.title}>
|
|
|
|
<i className={util.makeIconClass(decl.icon, true)} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-07-15 18:40:28 +02:00
|
|
|
const Input = React.memo(({ decl, className }: { decl: HeaderInput; className: string }) => {
|
2024-07-18 08:41:33 +02:00
|
|
|
const { value, ref, isDisabled, onChange, onKeyDown, onFocus, onBlur } = decl;
|
2024-07-15 18:40:28 +02:00
|
|
|
return (
|
|
|
|
<div className="input-wrapper">
|
|
|
|
<input
|
|
|
|
ref={ref}
|
2024-07-18 08:41:33 +02:00
|
|
|
disabled={isDisabled}
|
2024-07-15 18:40:28 +02:00
|
|
|
className={className}
|
|
|
|
value={value}
|
|
|
|
onChange={(e) => onChange(e)}
|
|
|
|
onKeyDown={(e) => onKeyDown(e)}
|
|
|
|
onFocus={(e) => onFocus(e)}
|
|
|
|
onBlur={(e) => onBlur(e)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-07-06 00:47:35 +02:00
|
|
|
const BlockFrame_Default_Component = ({
|
|
|
|
blockId,
|
2024-07-09 00:04:48 +02:00
|
|
|
layoutModel,
|
|
|
|
viewModel,
|
|
|
|
blockModel,
|
2024-07-06 00:47:35 +02:00
|
|
|
preview,
|
|
|
|
numBlocksInTab,
|
|
|
|
children,
|
|
|
|
}: BlockFrameProps) => {
|
|
|
|
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
|
|
|
const settingsConfig = jotai.useAtomValue(atoms.settingsConfigAtom);
|
|
|
|
const isFocusedAtom = useBlockAtom<boolean>(blockId, "isFocused", () => {
|
|
|
|
return jotai.atom((get) => {
|
|
|
|
const winData = get(atoms.waveWindow);
|
|
|
|
return winData?.activeblockid === blockId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let isFocused = jotai.useAtomValue(isFocusedAtom);
|
2024-07-26 03:05:32 +02:00
|
|
|
const viewIconUnion = util.useAtomValueSafe(viewModel.viewIcon) ?? blockViewToIcon(blockData?.view);
|
|
|
|
const viewName = util.useAtomValueSafe(viewModel.viewName) ?? blockViewToName(blockData?.view);
|
2024-07-09 08:13:12 +02:00
|
|
|
const headerTextUnion = util.useAtomValueSafe(viewModel.viewText);
|
|
|
|
const preIconButton = util.useAtomValueSafe(viewModel.preIconButton);
|
|
|
|
const endIconButtons = util.useAtomValueSafe(viewModel.endIconButtons);
|
2024-07-06 00:47:35 +02:00
|
|
|
if (preview) {
|
|
|
|
isFocused = true;
|
|
|
|
}
|
|
|
|
let 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"];
|
|
|
|
}
|
2024-07-09 08:13:12 +02:00
|
|
|
let viewIconElem: JSX.Element = null;
|
|
|
|
if (viewIconUnion == null || typeof viewIconUnion === "string") {
|
|
|
|
const viewIcon = viewIconUnion as string;
|
|
|
|
viewIconElem = <div className="block-frame-view-icon">{getBlockHeaderIcon(viewIcon, blockData)}</div>;
|
|
|
|
} else {
|
|
|
|
viewIconElem = <IconButton decl={viewIconUnion} className="block-frame-view-icon" />;
|
|
|
|
}
|
2024-07-09 01:36:30 +02:00
|
|
|
let preIconButtonElem: JSX.Element = null;
|
|
|
|
if (preIconButton) {
|
2024-07-09 08:13:12 +02:00
|
|
|
preIconButtonElem = <IconButton decl={preIconButton} className="block-frame-preicon-button" />;
|
2024-07-09 01:36:30 +02:00
|
|
|
}
|
|
|
|
let endIconsElem: JSX.Element[] = [];
|
|
|
|
if (endIconButtons && endIconButtons.length > 0) {
|
|
|
|
for (let idx = 0; idx < endIconButtons.length; idx++) {
|
|
|
|
const button = endIconButtons[idx];
|
2024-07-09 08:13:12 +02:00
|
|
|
endIconsElem.push(<IconButton key={idx} decl={button} className="block-frame-endicon-button" />);
|
2024-07-09 01:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-09 08:13:12 +02:00
|
|
|
const settingsDecl: HeaderIconButton = {
|
|
|
|
elemtype: "iconbutton",
|
|
|
|
icon: "cog",
|
|
|
|
title: "Settings",
|
|
|
|
click: (e) => handleHeaderContextMenu(e, blockData, viewModel, layoutModel?.onClose),
|
|
|
|
};
|
2024-07-09 01:36:30 +02:00
|
|
|
endIconsElem.push(
|
2024-07-09 08:13:12 +02:00
|
|
|
<IconButton key="settings" decl={settingsDecl} className="block-frame-endicon-button block-frame-settings" />
|
2024-07-09 01:36:30 +02:00
|
|
|
);
|
2024-07-09 08:13:12 +02:00
|
|
|
const closeDecl: HeaderIconButton = {
|
|
|
|
elemtype: "iconbutton",
|
|
|
|
icon: "xmark-large",
|
|
|
|
title: "Close",
|
|
|
|
click: layoutModel?.onClose,
|
|
|
|
};
|
2024-07-09 01:36:30 +02:00
|
|
|
endIconsElem.push(
|
2024-07-09 08:13:12 +02:00
|
|
|
<IconButton key="close" decl={closeDecl} className="block-frame-endicon-button block-frame-default-close" />
|
2024-07-09 01:36:30 +02:00
|
|
|
);
|
2024-07-15 18:40:28 +02:00
|
|
|
|
|
|
|
function renderHeaderElements(headerTextUnion: HeaderElem[]): JSX.Element[] {
|
|
|
|
const headerTextElems: JSX.Element[] = [];
|
|
|
|
|
|
|
|
function renderElement(elem: HeaderElem, key: number): JSX.Element {
|
|
|
|
if (elem.elemtype == "iconbutton") {
|
|
|
|
return (
|
|
|
|
<IconButton
|
|
|
|
key={key}
|
|
|
|
decl={elem}
|
|
|
|
className={clsx("block-frame-header-iconbutton", elem.className)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (elem.elemtype == "input") {
|
|
|
|
return <Input key={key} decl={elem} className={clsx("block-frame-input", elem.className)} />;
|
|
|
|
} else if (elem.elemtype == "text") {
|
|
|
|
return (
|
|
|
|
<div key={key} className="block-frame-text">
|
|
|
|
{elem.text}
|
|
|
|
</div>
|
|
|
|
);
|
2024-07-18 08:41:33 +02:00
|
|
|
} else if (elem.elemtype == "textbutton") {
|
|
|
|
return (
|
2024-07-25 21:00:35 +02:00
|
|
|
<Button key={key} className={elem.className} onClick={(e) => elem.onClick(e)}>
|
2024-07-18 08:41:33 +02:00
|
|
|
{elem.text}
|
|
|
|
</Button>
|
|
|
|
);
|
2024-07-15 18:40:28 +02:00
|
|
|
} else if (elem.elemtype == "div") {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={key}
|
|
|
|
className={clsx("block-frame-div", elem.className)}
|
|
|
|
onMouseOver={elem.onMouseOver}
|
|
|
|
onMouseOut={elem.onMouseOut}
|
|
|
|
>
|
|
|
|
{elem.children.map((child, childIdx) => renderElement(child, childIdx))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let idx = 0; idx < headerTextUnion.length; idx++) {
|
|
|
|
const elem = headerTextUnion[idx];
|
|
|
|
const renderedElement = renderElement(elem, idx);
|
|
|
|
if (renderedElement) {
|
|
|
|
headerTextElems.push(renderedElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return headerTextElems;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headerTextElems: JSX.Element[] = [];
|
2024-07-09 08:13:12 +02:00
|
|
|
if (typeof headerTextUnion === "string") {
|
|
|
|
if (!util.isBlank(headerTextUnion)) {
|
|
|
|
headerTextElems.push(
|
|
|
|
<div key="text" className="block-frame-text">
|
|
|
|
{headerTextUnion}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(headerTextUnion)) {
|
2024-07-15 18:40:28 +02:00
|
|
|
headerTextElems.push(...renderHeaderElements(headerTextUnion));
|
2024-07-09 08:13:12 +02:00
|
|
|
}
|
2024-07-15 18:40:28 +02:00
|
|
|
|
2024-07-06 00:47:35 +02:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
"block",
|
|
|
|
"block-frame-default",
|
|
|
|
isFocused ? "block-focused" : null,
|
|
|
|
preview ? "block-preview" : null,
|
2024-07-25 05:34:22 +02:00
|
|
|
numBlocksInTab == 1 ? "block-no-highlight" : null,
|
|
|
|
"block-" + blockId
|
2024-07-06 00:47:35 +02:00
|
|
|
)}
|
2024-07-09 00:04:48 +02:00
|
|
|
onClick={blockModel?.onClick}
|
|
|
|
onFocusCapture={blockModel?.onFocusCapture}
|
|
|
|
ref={blockModel?.blockRef}
|
2024-07-06 00:47:35 +02:00
|
|
|
style={style}
|
|
|
|
>
|
2024-07-09 07:31:38 +02:00
|
|
|
<div className="block-mask"></div>
|
|
|
|
<div className="block-frame-default-inner">
|
|
|
|
<div
|
|
|
|
className="block-frame-default-header"
|
|
|
|
ref={layoutModel?.dragHandleRef}
|
|
|
|
onContextMenu={(e) => handleHeaderContextMenu(e, blockData, viewModel, layoutModel?.onClose)}
|
|
|
|
>
|
2024-07-09 08:13:12 +02:00
|
|
|
{preIconButtonElem}
|
2024-07-09 07:31:38 +02:00
|
|
|
<div className="block-frame-default-header-iconview">
|
2024-07-09 08:13:12 +02:00
|
|
|
{viewIconElem}
|
2024-07-26 03:05:32 +02:00
|
|
|
<div className="block-frame-view-type">{viewName}</div>
|
2024-07-09 07:31:38 +02:00
|
|
|
{settingsConfig?.blockheader?.showblockids && (
|
|
|
|
<div className="block-frame-blockid">[{blockId.substring(0, 8)}]</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2024-07-15 18:40:28 +02:00
|
|
|
<div className="block-frame-textelems-wrapper">{headerTextElems}</div>
|
2024-07-09 07:31:38 +02:00
|
|
|
<div className="block-frame-end-icons">{endIconsElem}</div>
|
2024-07-06 00:47:35 +02:00
|
|
|
</div>
|
2024-07-09 07:31:38 +02:00
|
|
|
{preview ? <div className="block-frame-preview" /> : children}
|
2024-07-06 00:47:35 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const BlockFrame_Default = React.memo(BlockFrame_Default_Component) as typeof BlockFrame_Default_Component;
|
|
|
|
|
2024-06-26 18:31:43 +02:00
|
|
|
const BlockFrame = React.memo((props: BlockFrameProps) => {
|
2024-06-19 08:44:53 +02:00
|
|
|
const blockId = props.blockId;
|
2024-07-09 08:13:12 +02:00
|
|
|
const [blockData] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
2024-06-19 08:44:53 +02:00
|
|
|
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;
|
|
|
|
}
|
2024-07-06 00:47:35 +02:00
|
|
|
let FrameElem = BlockFrame_Default;
|
|
|
|
const numBlocks = tabData?.blockids?.length ?? 0;
|
|
|
|
return <FrameElem {...props} numBlocksInTab={numBlocks} />;
|
2024-06-26 18:31:43 +02:00
|
|
|
});
|
2024-06-19 08:44:53 +02:00
|
|
|
|
2024-06-22 01:01:51 +02:00
|
|
|
function blockViewToIcon(view: string): string {
|
|
|
|
if (view == "term") {
|
2024-07-06 00:47:35 +02:00
|
|
|
return "terminal";
|
2024-06-22 01:01:51 +02:00
|
|
|
}
|
|
|
|
if (view == "preview") {
|
|
|
|
return "file";
|
|
|
|
}
|
2024-06-26 18:39:41 +02:00
|
|
|
if (view == "web") {
|
|
|
|
return "globe";
|
|
|
|
}
|
2024-07-03 23:32:55 +02:00
|
|
|
if (view == "waveai") {
|
|
|
|
return "sparkles";
|
|
|
|
}
|
2024-07-26 03:05:32 +02:00
|
|
|
return "square";
|
2024-06-22 01:01:51 +02:00
|
|
|
}
|
|
|
|
|
2024-07-06 00:47:35 +02:00
|
|
|
function blockViewToName(view: string): string {
|
|
|
|
if (view == "term") {
|
|
|
|
return "Terminal";
|
|
|
|
}
|
|
|
|
if (view == "preview") {
|
|
|
|
return "Preview";
|
|
|
|
}
|
|
|
|
if (view == "web") {
|
|
|
|
return "Web";
|
|
|
|
}
|
|
|
|
if (view == "waveai") {
|
|
|
|
return "WaveAI";
|
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2024-07-09 00:04:48 +02:00
|
|
|
function getViewElemAndModel(
|
|
|
|
blockId: string,
|
|
|
|
blockView: string,
|
|
|
|
blockRef: React.RefObject<HTMLDivElement>
|
|
|
|
): { viewModel: ViewModel; viewElem: JSX.Element } {
|
|
|
|
if (blockView == null) {
|
|
|
|
return { viewElem: null, viewModel: null };
|
|
|
|
}
|
|
|
|
let viewElem: JSX.Element = null;
|
|
|
|
let viewModel: ViewModel = null;
|
|
|
|
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-06-26 18:31:43 +02:00
|
|
|
}
|
2024-07-09 00:04:48 +02:00
|
|
|
if (viewModel == null) {
|
|
|
|
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);
|
|
|
|
return blockViewToIcon(blockData?.view);
|
|
|
|
}),
|
|
|
|
viewName: jotai.atom((get) => {
|
|
|
|
const blockData = get(blockDataAtom);
|
|
|
|
return blockViewToName(blockData?.view);
|
|
|
|
}),
|
|
|
|
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) => {
|
|
|
|
const [blockData, blockDataLoading] = WOS.useWaveObjectValue<Block>(WOS.makeORef("block", blockId));
|
|
|
|
if (!blockData) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let { viewModel } = getViewElemAndModel(blockId, blockData?.view, null);
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let isFocused = jotai.useAtomValue(isFocusedAtom);
|
|
|
|
|
|
|
|
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(
|
|
|
|
() => getViewElemAndModel(blockId, blockData?.view, blockRef),
|
|
|
|
[blockId, blockData?.view, blockRef]
|
|
|
|
);
|
|
|
|
|
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-23 01:41:18 +02:00
|
|
|
focusElemRef.current.focus({ preventScroll: true });
|
|
|
|
}, []);
|
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-05-14 08:45:41 +02:00
|
|
|
<div key="content" className="block-content">
|
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 };
|