fix terminal paste handler -- use paste fn, unify key handlers. add extra if stmts to focusNode calls in block

This commit is contained in:
sawka 2024-09-03 00:02:03 -07:00
parent 70ef76be62
commit c94ff3495b
2 changed files with 26 additions and 30 deletions

View File

@ -151,14 +151,18 @@ const BlockFull = React.memo(({ nodeModel, viewModel }: FullBlockProps) => {
if (!focusWithin) { if (!focusWithin) {
setFocusTarget(); setFocusTarget();
} }
nodeModel.focusNode(); if (!isFocused) {
nodeModel.focusNode();
}
}, [blockClicked]); }, [blockClicked]);
React.useLayoutEffect(() => { React.useLayoutEffect(() => {
if (focusedChild == null) { if (focusedChild == null) {
return; return;
} }
nodeModel.focusNode(); if (!isFocused) {
nodeModel.focusNode();
}
}, [focusedChild]); }, [focusedChild]);
// treat the block as clicked on creation // treat the block as clicked on creation

View File

@ -214,6 +214,9 @@ const TerminalView = ({ blockId, model }: TerminalViewProps) => {
React.useEffect(() => { React.useEffect(() => {
function handleTerminalKeydown(event: KeyboardEvent): boolean { function handleTerminalKeydown(event: KeyboardEvent): boolean {
const waveEvent = keyutil.adaptFromReactOrNativeKeyEvent(event); const waveEvent = keyutil.adaptFromReactOrNativeKeyEvent(event);
if (waveEvent.type != "keydown") {
return true;
}
if (keyutil.checkKeyPressed(waveEvent, "Cmd:Escape")) { if (keyutil.checkKeyPressed(waveEvent, "Cmd:Escape")) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
@ -236,6 +239,22 @@ const TerminalView = ({ blockId, model }: TerminalViewProps) => {
return false; return false;
} }
} }
if (keyutil.checkKeyPressed(waveEvent, "Ctrl:Shift:v")) {
const p = navigator.clipboard.readText();
p.then((text) => {
termRef.current?.terminal.paste(text);
// termRef.current?.handleTermData(text);
});
event.preventDefault();
event.stopPropagation();
return true;
} else if (keyutil.checkKeyPressed(waveEvent, "Ctrl:Shift:c")) {
const sel = termRef.current?.terminal.getSelection();
navigator.clipboard.writeText(sel);
event.preventDefault();
event.stopPropagation();
return true;
}
if (shellProcStatusRef.current != "running" && keyutil.checkKeyPressed(waveEvent, "Enter")) { if (shellProcStatusRef.current != "running" && keyutil.checkKeyPressed(waveEvent, "Enter")) {
// restart // restart
WshServer.ControllerRestartCommand({ blockid: blockId }); WshServer.ControllerRestartCommand({ blockid: blockId });
@ -331,35 +350,8 @@ const TerminalView = ({ blockId, model }: TerminalViewProps) => {
blockId: blockId, blockId: blockId,
}; };
function handleKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {
const waveEvent = keyutil.adaptFromReactOrNativeKeyEvent(e);
if (keyutil.checkKeyPressed(waveEvent, "Cmd:Shift:v")) {
const p = navigator.clipboard.readText();
p.then((text) => {
termRef.current?.handleTermData(text);
});
e.preventDefault();
e.stopPropagation();
return true;
} else if (keyutil.checkKeyPressed(waveEvent, "Cmd:Shift:c")) {
const sel = termRef.current?.terminal.getSelection();
navigator.clipboard.writeText(sel);
e.preventDefault();
e.stopPropagation();
return true;
}
}
const changeConnection = React.useCallback(
async (connName: string) => {
await WshServer.SetMetaCommand({ oref: WOS.makeORef("block", blockId), meta: { connection: connName } });
await WshServer.ControllerRestartCommand({ blockid: blockId });
},
[blockId]
);
return ( return (
<div className={clsx("view-term", "term-mode-" + termMode)} onKeyDown={handleKeyDown} ref={viewRef}> <div className={clsx("view-term", "term-mode-" + termMode)} ref={viewRef}>
<TermThemeUpdater blockId={blockId} termRef={termRef} /> <TermThemeUpdater blockId={blockId} termRef={termRef} />
<TermStickers config={stickerConfig} /> <TermStickers config={stickerConfig} />
<div key="conntectElem" className="term-connectelem" ref={connectElemRef}></div> <div key="conntectElem" className="term-connectelem" ref={connectElemRef}></div>