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 1 commit
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
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.5.0 - unreleased

### Added

- Ability to copy and paste with the middle mouse button click.
Copy link
Contributor

@greg-fer greg-fer Nov 28, 2024

Choose a reason for hiding this comment

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

Suggested change
- Ability to copy and paste with the middle mouse button click.
- Ability to copy and paste the selected serial terminal output with the middle mouse button.


## 1.4.3 - 2024-11-11

### Changed
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
149 changes: 112 additions & 37 deletions src/components/Terminal/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,46 +293,89 @@ export default ({
}

useEffect(() => {
if (xtermRef.current != null) {
xtermRef.current?.terminal.attachCustomKeyEventHandler(event => {
const onMouseClick = (e: MouseEvent) => {
const isMiddleMouseClick = e.button === 1;

if (!isMiddleMouseClick) return;

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

if (hasSelection) {
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();

// @ts-expect-error Selection is checked by hasSelection
clipboard.writeText(selection.trim(), 'clipboard');
xtermRef.current?.terminal.clearSelection();
}

const hasClipboard = clipboard.readText('clipboard').trim() !== '';

if (!hasSelection && !lineMode && hasClipboard) {
handleUserInputShellMode(clipboard.readText('clipboard'));
}
};

const xTermRefLocal = xtermRef.current;

if (xTermRefLocal == null) {
return;
}

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

return () => {
xTermRefLocal?.terminal.element?.removeEventListener(
'mousedown',
onMouseClick
);
};
}, [xtermRef, lineMode, 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 false;
}
}
return !hasSelection;
case 'KeyV':
if (!lineMode) {
handleUserInputShellMode(
clipboard.readText('clipboard')
);
event.preventDefault();
}
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 +426,38 @@ export default ({
setHistoryLine(undefined);
}
}}
onMouseDown={e => {
const isMiddleMouseClick = e.button === 1;

if (!isMiddleMouseClick) return;

const hasClipboard =
clipboard.readText('clipboard').trim() !==
'';

if (!hasClipboard) {
return;
}

if (historyLine != null) {
setCmdLine(
`${historyLine}${clipboard.readText(
'clipboard'
)}`
);

resetHistoryScroll();
setHistoryLine(undefined);

return;
}

setCmdLine(
`${cmdLine}${clipboard.readText(
'clipboard'
)}`
);
}}
/>
</div>
<button
Expand Down
Loading