-
-
Notifications
You must be signed in to change notification settings - Fork 921
Expand file tree
/
Copy pathapp.tsx
More file actions
395 lines (366 loc) · 14.2 KB
/
app.tsx
File metadata and controls
395 lines (366 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2026, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import "./i18n/index";
import {
clearBadgesForBlockOnFocus,
clearBadgesForTabOnFocus,
getBadgeAtom,
getBlockBadgeAtom,
} from "@/app/store/badge";
import { ClientModel } from "@/app/store/client-model";
import { FocusManager } from "@/app/store/focusManager";
import { GlobalModel } from "@/app/store/global-model";
import { globalStore } from "@/app/store/jotaiStore";
import { getTabModelByTabId, TabModelContext } from "@/app/store/tab-model";
import { WaveEnvContext } from "@/app/waveenv/waveenv";
import { makeWaveEnvImpl } from "@/app/waveenv/waveenvimpl";
import { Workspace } from "@/app/workspace/workspace";
import { getLayoutModelForStaticTab } from "@/layout/index";
import { ContextMenuModel } from "@/store/contextmenu";
import { atoms, createBlock, getSettingsPrefixAtom, refocusNode } from "@/store/global";
import { appHandleKeyDown, keyboardMouseDownHandler } from "@/store/keymodel";
import { getElemAsStr } from "@/util/focusutil";
import * as keyutil from "@/util/keyutil";
import { PLATFORM } from "@/util/platformutil";
import * as util from "@/util/util";
import clsx from "clsx";
import debug from "debug";
import { Provider, useAtomValue } from "jotai";
import "overlayscrollbars/overlayscrollbars.css";
import { useEffect, useRef } from "react";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
import { AppBackground } from "./app-bg";
import { CenteredDiv } from "./element/quickelems";
import "./app.scss";
// tailwindsetup.css should come *after* app.scss (don't remove the newline above otherwise prettier will reorder these imports)
import "../tailwindsetup.css";
const dlog = debug("wave:app");
const focusLog = debug("wave:focus");
const App = ({ onFirstRender }: { onFirstRender: () => void }) => {
const tabId = useAtomValue(atoms.staticTabId);
const waveEnvRef = useRef(makeWaveEnvImpl());
useEffect(() => {
onFirstRender();
}, []);
return (
<Provider store={globalStore}>
<WaveEnvContext.Provider value={waveEnvRef.current}>
<TabModelContext.Provider value={getTabModelByTabId(tabId)}>
<AppInner />
</TabModelContext.Provider>
</WaveEnvContext.Provider>
</Provider>
);
};
function isContentEditableBeingEdited(): boolean {
const activeElement = document.activeElement;
return (
activeElement &&
activeElement.getAttribute("contenteditable") !== null &&
activeElement.getAttribute("contenteditable") !== "false"
);
}
function canEnablePaste(): boolean {
const activeElement = document.activeElement;
return activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || isContentEditableBeingEdited();
}
function canEnableCopy(): boolean {
const sel = window.getSelection();
return !util.isBlank(sel?.toString());
}
function canEnableCut(): boolean {
const sel = window.getSelection();
if (document.activeElement?.classList.contains("xterm-helper-textarea")) {
return false;
}
return !util.isBlank(sel?.toString()) && canEnablePaste();
}
async function getClipboardURL(): Promise<URL> {
try {
const clipboardText = await navigator.clipboard.readText();
if (clipboardText == null) {
return null;
}
const url = new URL(clipboardText);
if (!url.protocol.startsWith("http")) {
return null;
}
return url;
} catch (e) {
return null;
}
}
async function handleContextMenu(e: React.MouseEvent<HTMLDivElement>) {
e.preventDefault();
const canPaste = canEnablePaste();
const canCopy = canEnableCopy();
const canCut = canEnableCut();
const clipboardURL = await getClipboardURL();
if (!canPaste && !canCopy && !canCut && !clipboardURL) {
return;
}
const menu: ContextMenuItem[] = [];
if (canCut) {
menu.push({ label: "Cut", role: "cut" });
}
if (canCopy) {
menu.push({ label: "Copy", role: "copy" });
}
if (canPaste) {
menu.push({ label: "Paste", role: "paste" });
}
if (clipboardURL) {
menu.push({ type: "separator" });
menu.push({
label: "Open Clipboard URL (" + clipboardURL.hostname + ")",
click: () => {
createBlock({
meta: {
view: "web",
url: clipboardURL.toString(),
},
});
},
});
}
ContextMenuModel.getInstance().showContextMenu(menu, e);
}
function AppSettingsUpdater() {
const windowSettingsAtom = getSettingsPrefixAtom("window");
const windowSettings = useAtomValue(windowSettingsAtom);
useEffect(() => {
const isTransparentOrBlur =
(windowSettings?.["window:transparent"] || windowSettings?.["window:blur"]) ?? false;
const opacity = util.boundNumber(windowSettings?.["window:opacity"] ?? 0.8, 0, 1);
const baseBgColor = windowSettings?.["window:bgcolor"];
const mainDiv = document.getElementById("main");
// console.log("window settings", windowSettings, isTransparentOrBlur, opacity, baseBgColor, mainDiv);
if (isTransparentOrBlur) {
mainDiv.classList.add("is-transparent");
if (opacity != null) {
document.body.style.setProperty("--window-opacity", `${opacity}`);
} else {
document.body.style.removeProperty("--window-opacity");
}
} else {
mainDiv.classList.remove("is-transparent");
document.body.style.removeProperty("--window-opacity");
}
if (baseBgColor != null) {
document.body.style.setProperty("--main-bg-color", baseBgColor);
} else {
document.body.style.removeProperty("--main-bg-color");
}
}, [windowSettings]);
return null;
}
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() {
return null;
// for debugging
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;
}
const MacOSFirstClickHandler = () => {
useEffect(() => {
if (PLATFORM !== "darwin") {
return;
}
let windowFocusTime: number = null;
let cancelNextClick = false;
const handleWindowFocus = (e: FocusEvent) => {
windowFocusTime = Date.now();
};
const getBlockIdFromTarget = (target: EventTarget): string => {
let elem = target as HTMLElement;
while (elem != null) {
const blockId = elem.dataset?.blockid;
if (blockId) {
return blockId;
}
elem = elem.parentElement;
}
return null;
};
const isAIPanelTarget = (target: EventTarget): boolean => {
let elem = target as HTMLElement;
while (elem != null) {
if (elem.dataset?.aipanel) {
return true;
}
elem = elem.parentElement;
}
return false;
};
const handleMouseDown = (e: MouseEvent) => {
const timeDiff = Date.now() - windowFocusTime;
if (windowFocusTime != null && timeDiff < 50) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
cancelNextClick = true;
const blockId = getBlockIdFromTarget(e.target);
if (blockId != null) {
setTimeout(() => {
console.log("macos first-click, focusing block", blockId);
refocusNode(blockId);
}, 10);
} else if (isAIPanelTarget(e.target)) {
setTimeout(() => {
console.log("macos first-click, focusing AI panel");
FocusManager.getInstance().setWaveAIFocused(true);
}, 10);
}
console.log("macos first-click detected, canceled", timeDiff + "ms");
return;
}
cancelNextClick = false;
};
const handleClick = (e: MouseEvent) => {
if (!cancelNextClick) {
return;
}
cancelNextClick = false;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
console.log("macos first-click (click event) canceled");
};
window.addEventListener("focus", handleWindowFocus);
window.addEventListener("mousedown", handleMouseDown, true);
window.addEventListener("click", handleClick, true);
return () => {
window.removeEventListener("focus", handleWindowFocus);
window.removeEventListener("mousedown", handleMouseDown, true);
window.removeEventListener("click", handleClick, true);
};
}, []);
return null;
};
const AppKeyHandlers = () => {
useEffect(() => {
const staticKeyDownHandler = keyutil.keydownWrapper(appHandleKeyDown);
const staticMouseDownHandler = (e: MouseEvent) => {
keyboardMouseDownHandler(e);
GlobalModel.getInstance().setIsActive();
};
document.addEventListener("keydown", staticKeyDownHandler);
document.addEventListener("mousedown", staticMouseDownHandler);
return () => {
document.removeEventListener("keydown", staticKeyDownHandler);
document.removeEventListener("mousedown", staticMouseDownHandler);
};
}, []);
return null;
};
const BadgeAutoClearing = () => {
const tabId = useAtomValue(atoms.staticTabId);
const documentHasFocus = useAtomValue(atoms.documentHasFocus);
const layoutModel = getLayoutModelForStaticTab();
const focusedNode = useAtomValue(layoutModel.focusedNode);
const focusedBlockId = focusedNode?.data?.blockId;
const badge = useAtomValue(getBlockBadgeAtom(focusedBlockId));
const tabTransientBadge = useAtomValue(getBadgeAtom(tabId != null ? `tab:${tabId}` : null));
const prevFocusedBlockIdRef = useRef<string>(null);
const prevDocHasFocusRef = useRef<boolean>(false);
const prevTabDocHasFocusRef = useRef<boolean>(false);
useEffect(() => {
if (!focusedBlockId || !badge || !documentHasFocus) {
prevFocusedBlockIdRef.current = focusedBlockId;
prevDocHasFocusRef.current = documentHasFocus;
return;
}
const focusSwitched =
prevFocusedBlockIdRef.current !== focusedBlockId || prevDocHasFocusRef.current !== documentHasFocus;
prevFocusedBlockIdRef.current = focusedBlockId;
prevDocHasFocusRef.current = documentHasFocus;
const delay = focusSwitched ? 500 : 3000;
const timeoutId = setTimeout(() => {
if (!document.hasFocus()) {
return;
}
const currentFocusedNode = globalStore.get(layoutModel.focusedNode);
if (currentFocusedNode?.data?.blockId === focusedBlockId) {
clearBadgesForBlockOnFocus(focusedBlockId);
}
}, delay);
return () => clearTimeout(timeoutId);
}, [focusedBlockId, badge, documentHasFocus]);
useEffect(() => {
if (!tabId || !tabTransientBadge || !documentHasFocus) {
prevTabDocHasFocusRef.current = documentHasFocus;
return;
}
const focusSwitched = prevTabDocHasFocusRef.current !== documentHasFocus;
prevTabDocHasFocusRef.current = documentHasFocus;
const delay = focusSwitched ? 500 : 3000;
const timeoutId = setTimeout(() => {
if (!document.hasFocus()) {
return;
}
clearBadgesForTabOnFocus(tabId);
}, delay);
return () => clearTimeout(timeoutId);
}, [tabId, tabTransientBadge, documentHasFocus]);
return null;
};
const AppInner = () => {
const prefersReducedMotion = useAtomValue(atoms.prefersReducedMotionAtom);
const client = useAtomValue(ClientModel.getInstance().clientAtom);
const windowData = useAtomValue(GlobalModel.getInstance().windowDataAtom);
const isFullScreen = useAtomValue(atoms.isFullScreen);
if (client == null || windowData == null) {
return (
<div className="flex flex-col w-full h-full">
<AppBackground />
<CenteredDiv>invalid configuration, client or window was not loaded</CenteredDiv>
</div>
);
}
return (
<div
className={clsx("flex flex-col w-full h-full", PLATFORM, {
fullscreen: isFullScreen,
"prefers-reduced-motion": prefersReducedMotion,
})}
onContextMenu={handleContextMenu}
>
<AppBackground />
<MacOSFirstClickHandler />
<AppKeyHandlers />
<AppFocusHandler />
<AppSettingsUpdater />
<BadgeAutoClearing />
<DndProvider backend={HTML5Backend}>
<Workspace />
</DndProvider>
</div>
);
};
export { App };