Skip to content

Commit c143385

Browse files
authored
fix(terminal): route quicktools inserts to active terminal(if opened) (#2015)
1 parent 3f753cd commit c143385

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

src/handlers/quickTools.js

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,14 @@ quickTools.$input.addEventListener("input", (e) => {
125125
if (!key || key.length > 1) return;
126126
const keyCombination = getKeys({ key });
127127

128-
if (keyCombination.shiftKey && !keyCombination.ctrlKey) {
128+
if (
129+
keyCombination.shiftKey &&
130+
!keyCombination.ctrlKey &&
131+
!keyCombination.altKey &&
132+
!keyCombination.metaKey
133+
) {
129134
resetKeys();
130-
editorManager.editor.insert(shiftKeyMapping(key));
135+
insertText(shiftKeyMapping(key));
131136
return;
132137
}
133138

@@ -296,8 +301,7 @@ export default function actions(action, value) {
296301

297302
switch (action) {
298303
case "insert":
299-
editor.insert(value);
300-
return true;
304+
return insertText(value);
301305

302306
case "command": {
303307
const commandName =
@@ -393,6 +397,12 @@ export default function actions(action, value) {
393397
}
394398

395399
function setInput() {
400+
const terminalInput = getActiveTerminalInput();
401+
if (terminalInput) {
402+
input = terminalInput;
403+
return;
404+
}
405+
396406
const { activeElement } = document;
397407
if (
398408
!activeElement ||
@@ -666,7 +676,16 @@ function getFooterHeight() {
666676

667677
function focusEditor() {
668678
const { editor, activeFile } = editorManager;
669-
if (activeFile.focused) {
679+
if (!activeFile?.focused) {
680+
return;
681+
}
682+
683+
if (activeFile.type === "terminal" && activeFile.terminalComponent) {
684+
activeFile.terminalComponent.focus();
685+
return;
686+
}
687+
688+
if (editor) {
670689
editor.focus();
671690
}
672691
}
@@ -680,7 +699,7 @@ function resetKeys() {
680699
events.ctrl.forEach((cb) => cb(false));
681700
state.meta = false;
682701
events.meta.forEach((cb) => cb(false));
683-
input.focus();
702+
input?.focus?.();
684703
}
685704

686705
/**
@@ -700,6 +719,41 @@ export function getKeys(key = {}) {
700719
};
701720
}
702721

722+
function getActiveTerminalComponent() {
723+
const { activeFile } = editorManager;
724+
if (activeFile?.type !== "terminal") return null;
725+
return activeFile.terminalComponent || null;
726+
}
727+
728+
function getActiveTerminalInput() {
729+
return getActiveTerminalComponent()?.terminal?.textarea || null;
730+
}
731+
732+
function insertText(value) {
733+
const text = String(value ?? "");
734+
if (!text) return false;
735+
736+
const terminalComponent = getActiveTerminalComponent();
737+
if (terminalComponent?.terminal) {
738+
if (typeof terminalComponent.terminal.paste === "function") {
739+
terminalComponent.terminal.paste(text);
740+
terminalComponent.focus();
741+
return true;
742+
}
743+
744+
if (terminalComponent.serverMode && terminalComponent.isConnected) {
745+
terminalComponent.write(text);
746+
terminalComponent.focus();
747+
return true;
748+
}
749+
750+
return false;
751+
}
752+
753+
const { editor } = editorManager;
754+
return editor ? editor.insert(text) : false;
755+
}
756+
703757
function shiftKeyMapping(char) {
704758
switch (char) {
705759
case "1":

0 commit comments

Comments
 (0)