2024-05-13 23:40:18 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-08-15 03:40:41 +02:00
|
|
|
import { useWaveObjectValue } from "@/app/store/wos";
|
2024-05-28 21:12:28 +02:00
|
|
|
import { Workspace } from "@/app/workspace/workspace";
|
2024-06-25 02:44:31 +02:00
|
|
|
import { ContextMenuModel } from "@/store/contextmenu";
|
2024-09-13 01:02:18 +02:00
|
|
|
import { PLATFORM, WOS, atoms, getApi, globalStore, removeFlashError, useSettingsPrefixAtom } from "@/store/global";
|
2024-08-30 01:06:15 +02:00
|
|
|
import { appHandleKeyDown } from "@/store/keymodel";
|
2024-08-15 03:40:41 +02:00
|
|
|
import { getWebServerEndpoint } from "@/util/endpoints";
|
2024-08-30 01:06:15 +02:00
|
|
|
import { getElemAsStr } from "@/util/focusutil";
|
2024-06-21 21:32:38 +02:00
|
|
|
import * as keyutil from "@/util/keyutil";
|
2024-06-20 00:42:19 +02:00
|
|
|
import * as util from "@/util/util";
|
2024-08-19 23:16:09 +02:00
|
|
|
import useResizeObserver from "@react-hook/resize-observer";
|
2024-08-15 03:40:41 +02:00
|
|
|
import clsx from "clsx";
|
|
|
|
import Color from "color";
|
2024-07-29 20:55:10 +02:00
|
|
|
import * as csstree from "css-tree";
|
2024-08-30 01:06:15 +02:00
|
|
|
import debug from "debug";
|
2024-05-10 05:24:24 +02:00
|
|
|
import * as jotai from "jotai";
|
2024-08-15 03:40:41 +02:00
|
|
|
import "overlayscrollbars/overlayscrollbars.css";
|
2024-06-21 21:32:38 +02:00
|
|
|
import * as React from "react";
|
2024-06-04 22:05:44 +02:00
|
|
|
import { DndProvider } from "react-dnd";
|
|
|
|
import { HTML5Backend } from "react-dnd-html5-backend";
|
2024-08-19 23:16:09 +02:00
|
|
|
import { debounce } from "throttle-debounce";
|
2024-06-21 05:04:00 +02:00
|
|
|
import "./app.less";
|
2024-08-15 03:40:41 +02:00
|
|
|
import { CenteredDiv } from "./element/quickelems";
|
2024-06-21 05:04:00 +02:00
|
|
|
|
2024-08-30 01:06:15 +02:00
|
|
|
const dlog = debug("wave:app");
|
|
|
|
const focusLog = debug("wave:focus");
|
|
|
|
|
2024-05-10 05:24:24 +02:00
|
|
|
const App = () => {
|
2024-06-21 21:32:38 +02:00
|
|
|
let Provider = jotai.Provider;
|
2024-05-10 05:24:24 +02:00
|
|
|
return (
|
2024-05-14 08:45:41 +02:00
|
|
|
<Provider store={globalStore}>
|
2024-05-10 05:24:24 +02:00
|
|
|
<AppInner />
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-06-25 02:44:31 +02:00
|
|
|
function isContentEditableBeingEdited() {
|
|
|
|
const activeElement = document.activeElement;
|
|
|
|
return (
|
|
|
|
activeElement &&
|
|
|
|
activeElement.getAttribute("contenteditable") !== null &&
|
|
|
|
activeElement.getAttribute("contenteditable") !== "false"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function canEnablePaste() {
|
|
|
|
const activeElement = document.activeElement;
|
|
|
|
return activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || isContentEditableBeingEdited();
|
|
|
|
}
|
|
|
|
|
|
|
|
function canEnableCopy() {
|
|
|
|
const sel = window.getSelection();
|
|
|
|
return !util.isBlank(sel?.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
function canEnableCut() {
|
|
|
|
const sel = window.getSelection();
|
|
|
|
if (document.activeElement?.classList.contains("xterm-helper-textarea")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !util.isBlank(sel?.toString()) && canEnablePaste();
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:42:19 +02:00
|
|
|
function handleContextMenu(e: React.MouseEvent<HTMLDivElement>) {
|
2024-06-25 02:44:31 +02:00
|
|
|
e.preventDefault();
|
|
|
|
const canPaste = canEnablePaste();
|
|
|
|
const canCopy = canEnableCopy();
|
|
|
|
const canCut = canEnableCut();
|
|
|
|
if (!canPaste && !canCopy && !canCut) {
|
|
|
|
return;
|
2024-06-20 00:42:19 +02:00
|
|
|
}
|
2024-06-25 02:44:31 +02:00
|
|
|
let menu: ContextMenuItem[] = [];
|
|
|
|
if (canCut) {
|
|
|
|
menu.push({ label: "Cut", role: "cut" });
|
2024-06-20 00:42:19 +02:00
|
|
|
}
|
2024-06-25 02:44:31 +02:00
|
|
|
if (canCopy) {
|
|
|
|
menu.push({ label: "Copy", role: "copy" });
|
2024-06-20 00:42:19 +02:00
|
|
|
}
|
2024-06-25 02:44:31 +02:00
|
|
|
if (canPaste) {
|
|
|
|
menu.push({ label: "Paste", role: "paste" });
|
2024-06-20 00:42:19 +02:00
|
|
|
}
|
2024-06-25 02:44:31 +02:00
|
|
|
ContextMenuModel.showContextMenu(menu, e);
|
2024-06-20 00:42:19 +02:00
|
|
|
}
|
|
|
|
|
2024-07-26 22:30:11 +02:00
|
|
|
function AppSettingsUpdater() {
|
2024-08-28 03:49:49 +02:00
|
|
|
const windowSettings = useSettingsPrefixAtom("window");
|
2024-07-26 22:30:11 +02:00
|
|
|
React.useEffect(() => {
|
2024-08-28 03:49:49 +02:00
|
|
|
const isTransparentOrBlur =
|
|
|
|
(windowSettings?.["window:transparent"] || windowSettings?.["window:blur"]) ?? false;
|
|
|
|
const opacity = util.boundNumber(windowSettings?.["window:opacity"] ?? 0.8, 0, 1);
|
|
|
|
let baseBgColor = windowSettings?.["window:bgcolor"];
|
2024-07-27 01:18:53 +02:00
|
|
|
if (isTransparentOrBlur) {
|
2024-07-26 22:30:11 +02:00
|
|
|
document.body.classList.add("is-transparent");
|
|
|
|
const rootStyles = getComputedStyle(document.documentElement);
|
|
|
|
if (baseBgColor == null) {
|
|
|
|
baseBgColor = rootStyles.getPropertyValue("--main-bg-color").trim();
|
|
|
|
}
|
|
|
|
const color = new Color(baseBgColor);
|
|
|
|
const rgbaColor = color.alpha(opacity).string();
|
|
|
|
document.body.style.backgroundColor = rgbaColor;
|
|
|
|
} else {
|
|
|
|
document.body.classList.remove("is-transparent");
|
|
|
|
document.body.style.opacity = null;
|
|
|
|
}
|
2024-08-28 03:49:49 +02:00
|
|
|
}, [windowSettings]);
|
2024-07-26 22:30:11 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-08-30 01:06:15 +02:00
|
|
|
function appFocusIn(e: FocusEvent) {
|
|
|
|
focusLog("focusin", getElemAsStr(e.target), "<=", getElemAsStr(e.relatedTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
function appFocusOut(e: FocusEvent) {
|
|
|
|
focusLog("focusout", getElemAsStr(e.target), "=>", getElemAsStr(e.relatedTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
function appSelectionChange(e: Event) {
|
|
|
|
const selection = document.getSelection();
|
|
|
|
focusLog("selectionchange", getElemAsStr(selection.anchorNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
function AppFocusHandler() {
|
2024-08-30 02:00:24 +02:00
|
|
|
return null;
|
|
|
|
|
|
|
|
// for debugging
|
2024-08-30 01:06:15 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
document.addEventListener("focusin", appFocusIn);
|
|
|
|
document.addEventListener("focusout", appFocusOut);
|
|
|
|
document.addEventListener("selectionchange", appSelectionChange);
|
|
|
|
const ivId = setInterval(() => {
|
|
|
|
const activeElement = document.activeElement;
|
|
|
|
if (activeElement instanceof HTMLElement) {
|
|
|
|
focusLog("activeElement", getElemAsStr(activeElement));
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("focusin", appFocusIn);
|
|
|
|
document.removeEventListener("focusout", appFocusOut);
|
|
|
|
document.removeEventListener("selectionchange", appSelectionChange);
|
|
|
|
clearInterval(ivId);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-07-29 20:55:10 +02:00
|
|
|
function encodeFileURL(file: string) {
|
|
|
|
const webEndpoint = getWebServerEndpoint();
|
|
|
|
return webEndpoint + `/wave/stream-file?path=${encodeURIComponent(file)}&no404=1`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function processBackgroundUrls(cssText: string): string {
|
|
|
|
if (util.isBlank(cssText)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
cssText = cssText.trim();
|
|
|
|
if (cssText.endsWith(";")) {
|
|
|
|
cssText = cssText.slice(0, -1);
|
|
|
|
}
|
|
|
|
const attrRe = /^background(-image):\s*/;
|
|
|
|
cssText = cssText.replace(attrRe, "");
|
|
|
|
const ast = csstree.parse("background: " + cssText, {
|
|
|
|
context: "declaration",
|
|
|
|
});
|
|
|
|
let hasJSUrl = false;
|
|
|
|
csstree.walk(ast, {
|
|
|
|
visit: "Url",
|
|
|
|
enter(node) {
|
|
|
|
const originalUrl = node.value.trim();
|
|
|
|
if (originalUrl.startsWith("javascript:")) {
|
|
|
|
hasJSUrl = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newUrl = encodeFileURL(originalUrl);
|
|
|
|
node.value = newUrl;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (hasJSUrl) {
|
|
|
|
console.log("invalid background, contains a 'javascript' protocol url which is not allowed");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const rtnStyle = csstree.generate(ast);
|
|
|
|
if (rtnStyle == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return rtnStyle.replace(/^background:\s*/, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function AppBackground() {
|
2024-08-19 23:16:09 +02:00
|
|
|
const bgRef = React.useRef<HTMLDivElement>(null);
|
2024-07-29 20:55:10 +02:00
|
|
|
const tabId = jotai.useAtomValue(atoms.activeTabId);
|
|
|
|
const [tabData] = useWaveObjectValue<Tab>(WOS.makeORef("tab", tabId));
|
|
|
|
const bgAttr = tabData?.meta?.bg;
|
|
|
|
const style: React.CSSProperties = {};
|
|
|
|
if (!util.isBlank(bgAttr)) {
|
|
|
|
try {
|
|
|
|
const processedBg = processBackgroundUrls(bgAttr);
|
|
|
|
if (!util.isBlank(processedBg)) {
|
|
|
|
const opacity = util.boundNumber(tabData?.meta?.["bg:opacity"], 0, 1) ?? 0.5;
|
|
|
|
style.opacity = opacity;
|
|
|
|
style.background = processedBg;
|
|
|
|
const blendMode = tabData?.meta?.["bg:blendmode"];
|
|
|
|
if (!util.isBlank(blendMode)) {
|
|
|
|
style.backgroundBlendMode = blendMode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("error processing background", e);
|
|
|
|
}
|
|
|
|
}
|
2024-08-19 23:16:09 +02:00
|
|
|
const getAvgColor = React.useCallback(
|
2024-08-19 23:29:58 +02:00
|
|
|
debounce(30, () => {
|
2024-08-19 23:16:09 +02:00
|
|
|
if (
|
|
|
|
bgRef.current &&
|
|
|
|
PLATFORM !== "darwin" &&
|
|
|
|
bgRef.current &&
|
|
|
|
"windowControlsOverlay" in window.navigator
|
|
|
|
) {
|
|
|
|
const titlebarRect: Dimensions = (window.navigator.windowControlsOverlay as any).getTitlebarAreaRect();
|
|
|
|
const bgRect = bgRef.current.getBoundingClientRect();
|
|
|
|
if (titlebarRect && bgRect) {
|
|
|
|
const windowControlsLeft = titlebarRect.width - titlebarRect.height;
|
|
|
|
const windowControlsRect: Dimensions = {
|
|
|
|
top: titlebarRect.top,
|
|
|
|
left: windowControlsLeft,
|
|
|
|
height: titlebarRect.height,
|
|
|
|
width: bgRect.width - bgRect.left - windowControlsLeft,
|
|
|
|
};
|
|
|
|
getApi().updateWindowControlsOverlay(windowControlsRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[bgRef, style]
|
|
|
|
);
|
2024-08-19 23:28:01 +02:00
|
|
|
React.useLayoutEffect(getAvgColor, [getAvgColor]);
|
2024-08-19 23:16:09 +02:00
|
|
|
useResizeObserver(bgRef, getAvgColor);
|
|
|
|
|
|
|
|
return <div ref={bgRef} className="app-background" style={style} />;
|
2024-07-29 20:55:10 +02:00
|
|
|
}
|
|
|
|
|
2024-08-03 00:39:22 +02:00
|
|
|
const AppKeyHandlers = () => {
|
2024-06-21 21:32:38 +02:00
|
|
|
React.useEffect(() => {
|
2024-08-20 03:28:28 +02:00
|
|
|
const staticKeyDownHandler = keyutil.keydownWrapper(appHandleKeyDown);
|
2024-06-21 21:32:38 +02:00
|
|
|
document.addEventListener("keydown", staticKeyDownHandler);
|
2024-08-03 00:39:22 +02:00
|
|
|
|
2024-06-21 21:32:38 +02:00
|
|
|
return () => {
|
|
|
|
document.removeEventListener("keydown", staticKeyDownHandler);
|
|
|
|
};
|
|
|
|
}, []);
|
2024-08-03 00:39:22 +02:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2024-09-13 01:02:18 +02:00
|
|
|
const FlashError = () => {
|
|
|
|
const flashErrors = jotai.useAtomValue(atoms.flashErrors);
|
|
|
|
const [hoveredId, setHoveredId] = React.useState<string>(null);
|
|
|
|
const [ticker, setTicker] = React.useState<number>(0);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (flashErrors.length == 0 || hoveredId != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const now = Date.now();
|
|
|
|
for (let ferr of flashErrors) {
|
|
|
|
if (ferr.expiration == null || ferr.expiration < now) {
|
|
|
|
removeFlashError(ferr.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(() => setTicker(ticker + 1), 1000);
|
|
|
|
}, [flashErrors, ticker, hoveredId]);
|
|
|
|
|
|
|
|
if (flashErrors.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyError(id: string) {
|
|
|
|
const ferr = flashErrors.find((f) => f.id === id);
|
|
|
|
if (ferr == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let text = "";
|
|
|
|
if (ferr.title != null) {
|
|
|
|
text += ferr.title;
|
|
|
|
}
|
|
|
|
if (ferr.message != null) {
|
|
|
|
if (text.length > 0) {
|
|
|
|
text += "\n";
|
|
|
|
}
|
|
|
|
text += ferr.message;
|
|
|
|
}
|
|
|
|
navigator.clipboard.writeText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertNewlinesToBreaks(text) {
|
|
|
|
return text.split("\n").map((part, index) => (
|
|
|
|
<React.Fragment key={index}>
|
|
|
|
{part}
|
|
|
|
<br />
|
|
|
|
</React.Fragment>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flash-error-container">
|
|
|
|
{flashErrors.map((err, idx) => (
|
|
|
|
<div
|
|
|
|
key={idx}
|
|
|
|
className={clsx("flash-error", { hovered: hoveredId === err.id })}
|
|
|
|
onClick={() => copyError(err.id)}
|
|
|
|
onMouseEnter={() => setHoveredId(err.id)}
|
|
|
|
onMouseLeave={() => setHoveredId(null)}
|
|
|
|
title="Click to Copy Error Message"
|
|
|
|
>
|
|
|
|
<div className="flash-error-scroll">
|
|
|
|
{err.title != null ? <div className="flash-error-title">{err.title}</div> : null}
|
|
|
|
{err.message != null ? (
|
|
|
|
<div className="flash-error-message">{convertNewlinesToBreaks(err.message)}</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-03 00:39:22 +02:00
|
|
|
const AppInner = () => {
|
2024-09-07 01:20:27 +02:00
|
|
|
const prefersReducedMotion = jotai.useAtomValue(atoms.prefersReducedMotionAtom);
|
2024-08-03 00:39:22 +02:00
|
|
|
const client = jotai.useAtomValue(atoms.client);
|
|
|
|
const windowData = jotai.useAtomValue(atoms.waveWindow);
|
2024-08-15 06:47:09 +02:00
|
|
|
const isFullScreen = jotai.useAtomValue(atoms.isFullScreen);
|
|
|
|
|
2024-08-03 00:39:22 +02:00
|
|
|
if (client == null || windowData == null) {
|
|
|
|
return (
|
|
|
|
<div className="mainapp">
|
|
|
|
<AppBackground />
|
|
|
|
<CenteredDiv>invalid configuration, client or window was not loaded</CenteredDiv>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-07-22 22:33:10 +02:00
|
|
|
|
2024-05-10 05:24:24 +02:00
|
|
|
return (
|
2024-08-15 06:47:09 +02:00
|
|
|
<div
|
|
|
|
className={clsx("mainapp", PLATFORM, {
|
|
|
|
fullscreen: isFullScreen,
|
2024-09-07 01:20:27 +02:00
|
|
|
"prefers-reduced-motion": prefersReducedMotion,
|
2024-08-15 06:47:09 +02:00
|
|
|
})}
|
|
|
|
onContextMenu={handleContextMenu}
|
|
|
|
>
|
2024-07-29 20:55:10 +02:00
|
|
|
<AppBackground />
|
2024-08-03 00:39:22 +02:00
|
|
|
<AppKeyHandlers />
|
2024-08-30 01:06:15 +02:00
|
|
|
<AppFocusHandler />
|
2024-07-26 22:30:11 +02:00
|
|
|
<AppSettingsUpdater />
|
2024-06-04 22:05:44 +02:00
|
|
|
<DndProvider backend={HTML5Backend}>
|
|
|
|
<Workspace />
|
|
|
|
</DndProvider>
|
2024-09-13 01:02:18 +02:00
|
|
|
<FlashError />
|
2024-05-10 05:24:24 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { App };
|