-
Notifications
You must be signed in to change notification settings - Fork 409
feat: add terminal-preview linking functionality #1852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ import { createRef, memo, useCallback, useEffect, useMemo, useState } from "reac | |
import { CSVView } from "./csvview"; | ||
import { DirectoryPreview } from "./directorypreview"; | ||
import "./preview.scss"; | ||
import { useTerminalPreviewSync } from "./sync"; | ||
import { toggleTerminalPreviewLink } from "./sync"; | ||
|
||
const MaxFileSize = 1024 * 1024 * 10; // 10MB | ||
const MaxCSVSize = 1024 * 1024 * 1; // 1MB | ||
|
@@ -172,6 +174,7 @@ export class PreviewModel implements ViewModel { | |
refreshCallback: () => void; | ||
directoryKeyDownHandler: (waveEvent: WaveKeyboardEvent) => boolean; | ||
codeEditKeyDownHandler: (waveEvent: WaveKeyboardEvent) => boolean; | ||
isUpdatingFromTerminal: boolean; | ||
|
||
constructor(blockId: string, nodeModel: BlockNodeModel) { | ||
this.viewType = "preview"; | ||
|
@@ -450,6 +453,7 @@ export class PreviewModel implements ViewModel { | |
}); | ||
|
||
this.noPadding = atom(true); | ||
this.isUpdatingFromTerminal = false; | ||
} | ||
|
||
markdownShowTocToggle() { | ||
|
@@ -528,6 +532,26 @@ export class PreviewModel implements ViewModel { | |
const blockOref = WOS.makeORef("block", this.blockId); | ||
await services.ObjectService.UpdateObjectMeta(blockOref, updateMeta); | ||
|
||
const linkedTerminalId = blockMeta?.["preview:linked_terminal"]; | ||
if (linkedTerminalId && !this.isUpdatingFromTerminal) { | ||
const fileInfo = await RpcApi.FileInfoCommand(TabRpcClient, { | ||
info: { | ||
path: await this.formatRemoteUri(newPath, globalStore.get), | ||
}, | ||
}); | ||
if (fileInfo?.isdir) { | ||
const terminalBlockRef = WOS.makeORef("block", linkedTerminalId); | ||
await services.ObjectService.UpdateObjectMeta(terminalBlockRef, { | ||
"cmd:cwd": fileInfo.dir | ||
}); | ||
|
||
await RpcApi.ControllerInputCommand(TabRpcClient, { | ||
blockid: linkedTerminalId, | ||
inputdata64: btoa(`cd "${fileInfo.dir}"\n`), | ||
}); | ||
} | ||
} | ||
|
||
// Clear the saved file buffers | ||
globalStore.set(this.fileContentSaved, null); | ||
globalStore.set(this.newFileContent, null); | ||
|
@@ -685,6 +709,29 @@ export class PreviewModel implements ViewModel { | |
await navigator.clipboard.writeText(fileInfo.name); | ||
}), | ||
}); | ||
|
||
menuItems.push({ type: "separator" }); | ||
menuItems.push({ | ||
label: "Link to Terminal (from clipboard)", | ||
click: () => | ||
fireAndForget(async () => { | ||
const terminalId = await navigator.clipboard.readText(); | ||
if (!terminalId) return; | ||
await toggleTerminalPreviewLink(terminalId, this.blockId); | ||
}), | ||
}); | ||
|
||
menuItems.push({ | ||
label: "Unlink from Terminal", | ||
click: () => | ||
fireAndForget(async () => { | ||
const linkedTerminalId = globalStore.get(this.blockAtom)?.meta?.["preview:linked_terminal"]; | ||
if (linkedTerminalId) { | ||
await toggleTerminalPreviewLink(linkedTerminalId, this.blockId); | ||
} | ||
}), | ||
}); | ||
|
||
Comment on lines
+717
to
+739
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider enhancing the terminal linking UX. The current implementation has several areas for improvement:
Consider implementing:
menuItems.push({
- label: "Link to Terminal (from clipboard)",
+ label: "Link to Terminal",
click: () =>
fireAndForget(async () => {
- const terminalId = await navigator.clipboard.readText();
- if (!terminalId) return;
+ // Show terminal selector modal
+ const terminalId = await showTerminalSelectorModal();
+ if (!terminalId) return;
+
+ try {
await toggleTerminalPreviewLink(terminalId, this.blockId);
+ showToast("Terminal linked successfully");
+ } catch (error) {
+ showToast("Failed to link terminal: " + error.message, "error");
+ }
}),
});
|
||
const mimeType = jotaiLoadableValue(globalStore.get(this.fileMimeTypeLoadable), ""); | ||
if (mimeType == "directory") { | ||
menuItems.push({ | ||
|
@@ -1024,6 +1071,8 @@ function PreviewView({ | |
model: PreviewModel; | ||
}) { | ||
const connStatus = useAtomValue(model.connStatus); | ||
useTerminalPreviewSync(model); | ||
|
||
if (connStatus?.status != "connected") { | ||
return null; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2025, Command Line Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { atoms, globalStore } from "@/store/global"; | ||
import * as WOS from "@/store/wos"; | ||
import { PreviewModel } from "./preview"; | ||
import * as React from "react"; | ||
import { useAtomValue } from "jotai"; | ||
import { ObjectService } from "@/store/services"; | ||
|
||
declare module "@/store/wos" { | ||
interface MetaType { | ||
"preview:linked_terminal"?: string | null; | ||
"cmd:cwd"?: string; | ||
} | ||
} | ||
|
||
/** | ||
* Synchronizes the Terminal's current working directory with the Preview's directory. | ||
* This is used when the Terminal and Preview panes are linked. | ||
*/ | ||
export function useTerminalPreviewSync(previewModel: PreviewModel) { | ||
const blockData = useAtomValue(previewModel.blockAtom); | ||
const linkedTerminalId = blockData?.meta?.["preview:linked_terminal"]; | ||
|
||
React.useEffect(() => { | ||
if (!linkedTerminalId) { | ||
return; | ||
} | ||
|
||
const terminalBlockAtom = WOS.getWaveObjectAtom(WOS.makeORef("block", linkedTerminalId)); | ||
const unsubscribe = globalStore.sub(terminalBlockAtom, () => { | ||
const terminalBlock = globalStore.get(terminalBlockAtom); | ||
const terminalCwd = terminalBlock?.meta?.["cmd:cwd"]; | ||
if (terminalCwd) { | ||
previewModel.isUpdatingFromTerminal = true; | ||
previewModel.goHistory(terminalCwd) | ||
.catch(console.error) | ||
.finally(() => { | ||
previewModel.isUpdatingFromTerminal = false; | ||
}); | ||
} | ||
}); | ||
|
||
return () => { | ||
unsubscribe(); | ||
previewModel.isUpdatingFromTerminal = false; | ||
}; | ||
}, [linkedTerminalId, previewModel]); | ||
} | ||
|
||
/** | ||
* Links or unlinks a Terminal and Preview pane for directory synchronization | ||
*/ | ||
export async function toggleTerminalPreviewLink(terminalId: string, previewId: string) { | ||
const previewBlockRef = WOS.makeORef("block", previewId); | ||
const previewBlock = globalStore.get(WOS.getWaveObjectAtom(previewBlockRef)); | ||
|
||
if (previewBlock?.meta?.["preview:linked_terminal"] === terminalId) { | ||
await ObjectService.UpdateObjectMeta(previewBlockRef, { | ||
"preview:linked_terminal": null | ||
}); | ||
return; | ||
} | ||
|
||
await ObjectService.UpdateObjectMeta(previewBlockRef, { | ||
"preview:linked_terminal": terminalId | ||
}); | ||
|
||
const terminalBlock = globalStore.get(WOS.getWaveObjectAtom(WOS.makeORef("block", terminalId))); | ||
const terminalCwd = terminalBlock?.meta?.["cmd:cwd"]; | ||
if (terminalCwd) { | ||
const previewModel = new PreviewModel(previewId, null); | ||
previewModel.isUpdatingFromTerminal = true; | ||
try { | ||
await previewModel.goHistory(terminalCwd); | ||
} finally { | ||
previewModel.isUpdatingFromTerminal = false; | ||
} | ||
} | ||
prawwtocol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.