Skip to content
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

Copy & paste with middle mouse button click #98

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.5.0 - unreleased

### Added

- Support for copying and pasting the console output with the middle mouse
button.

## 1.4.3 - 2024-11-11

### Changed
Expand Down
3 changes: 3 additions & 0 deletions doc/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ This is where you can type a command or paste it in and send it to the device by

Use the **CLEAR CONSOLE** button in the bottom right corner to clear the console view.

!!! info "Tip"
You can select the console output and use the middle mouse button to copy and paste it.

## Settings tab

In this tab, you can configure the volatile **XTerm Scrollback** buffer for the console and the size of the persistent **History File**.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pc-nrfconnect-serial-terminal",
"version": "1.4.3",
"version": "1.5.0",
"description": "Terminal emulator for serial port connections",
"displayName": "Serial Terminal",
"repository": {
Expand Down
177 changes: 139 additions & 38 deletions src/components/Terminal/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,46 +293,109 @@ export default ({
}

useEffect(() => {
if (xtermRef.current != null) {
xtermRef.current?.terminal.attachCustomKeyEventHandler(event => {
const selection = xtermRef.current?.terminal.getSelection();
const hasSelection =
selection !== undefined && selection.length > 0;
const { platform } = process;
const isMac = platform === 'darwin';
if (
((!isMac && event.ctrlKey) || (isMac && event.metaKey)) &&
!event.repeat &&
event.type === 'keydown'
) {
switch (event.code) {
case 'KeyC':
if (hasSelection) {
clipboard.writeText(
selection.trim(),
'clipboard'
);
xtermRef.current?.terminal.clearSelection();
event.preventDefault();
}
return !hasSelection;
case 'KeyV':
if (!lineMode) {
handleUserInputShellMode(
clipboard.readText('clipboard')
);
event.preventDefault();
}
return false;
case 'KeyA':
xtermRef.current?.terminal.selectAll();
const onMouseClick = (e: MouseEvent) => {
const isMiddleMouseClick = e.button === 1;

if (!isMiddleMouseClick) return;

const hasSelection = xtermRef.current?.terminal.hasSelection();

// if (! hasSelection) { return; }

const selection = xtermRef.current?.terminal.getSelection();
const selectedText = selection?.trim();

const textToPaste = hasSelection
? selectedText
: clipboard.readText('clipboard');

if (!textToPaste) return;

if (selectedText && hasSelection) {
clipboard.writeText(selectedText, 'clipboard');
xtermRef.current?.terminal.clearSelection();
}

if (!lineMode) {
// shell mode
handleUserInputShellMode(textToPaste);
return;
}

/* Line mode */
if (historyLine != null) {
setCmdLine(`${historyLine}${textToPaste}`);

resetHistoryScroll();
setHistoryLine(undefined);

return;
}

setCmdLine(`${cmdLine}${textToPaste}`);
};

const xTermRefLocal = xtermRef.current;

if (xTermRefLocal == null) {
return;
}

xTermRefLocal?.terminal.element?.addEventListener(
'mousedown',
onMouseClick
);

return () => {
xTermRefLocal?.terminal.element?.removeEventListener(
'mousedown',
onMouseClick
);
};
}, [xtermRef, lineMode, cmdLine, historyLine, handleUserInputShellMode]);

useEffect(() => {
if (xtermRef.current == null) {
return;
}

xtermRef.current?.terminal.attachCustomKeyEventHandler(event => {
const selection = xtermRef.current?.terminal.getSelection();

const hasSelection =
selection !== undefined && selection.length > 0;
const { platform } = process;
const isMac = platform === 'darwin';

if (
((!isMac && event.ctrlKey) || (isMac && event.metaKey)) &&
!event.repeat &&
event.type === 'keydown'
) {
switch (event.code) {
case 'KeyC':
if (hasSelection) {
clipboard.writeText(selection.trim(), 'clipboard');
xtermRef.current?.terminal.clearSelection();
event.preventDefault();
}
return !hasSelection;
case 'KeyV':
if (!lineMode) {
handleUserInputShellMode(
clipboard.readText('clipboard')
);
event.preventDefault();
return false;
}
}
return false;
case 'KeyA':
xtermRef.current?.terminal.selectAll();
event.preventDefault();
return false;
}
return true;
});
}
}
return true;
});
}, [xtermRef, lineMode, handleUserInputShellMode, cmdLine]);

return (
Expand Down Expand Up @@ -383,6 +446,44 @@ export default ({
setHistoryLine(undefined);
}
}}
// onMouseDown={e => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove before merge

// const isMiddleMouseClick = e.button === 1;
//
// if (!isMiddleMouseClick) return;
//
// const hasClipboard =
// clipboard.readText('clipboard').trim() !==
// '';
//
// console.log('onMouseDown e', e);
// console.log(
// 'lineModeInputRef: selectionStart ',
// lineModeInputRef.current?.selectionStart
// );
//
// if (!hasClipboard) {
// return;
// }
//
// if (historyLine != null) {
// setCmdLine(
// `${historyLine}${clipboard.readText(
// 'clipboard'
// )}`
// );
//
// resetHistoryScroll();
// setHistoryLine(undefined);
//
// return;
// }
//
// setCmdLine(
// `${cmdLine}${clipboard.readText(
// 'clipboard'
// )}`
// );
// }}
/>
</div>
<button
Expand Down