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

Add copy and paste control strip buttons #241

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 apple2js.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
<button id="toggle-printer" onclick="Apple2.openPrinterModal()" title="Toggle Printer">
<i class="fas fa-print"></i>
</button>
<div class="spacer short"></div>
<button onclick="Apple2.copy()" title="Copy">
<i class="fas fa-copy"></i>
</button>
<button onclick="Apple2.paste()" title="Paste">
<i class="fas fa-paste"></i>
</button>
<div class="spacer"></div>
<button onclick="window.open('https://github.com/whscullin/apple2js#readme', 'blank')" title="About">
<i class="fas fa-info"></i>
Expand Down
9 changes: 8 additions & 1 deletion apple2jse.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@
<button id="toggle-printer" onclick="Apple2.openPrinterModal()" title="Toggle Printer">
<i class="fas fa-print"></i>
</button>
<div class="spacer"></div>
<div class="spacer short"></div>
<button onclick="Apple2.copy()" title="Copy">
<i class="fas fa-copy"></i>
</button>
<button onclick="Apple2.paste()" title="Paste">
<i class="fas fa-paste"></i>
</button>
<div class="spacer"></div>
<button onclick="window.open('https://github.com/whscullin/apple2js#readme', 'blank')" title="About">
<i class="fas fa-info"></i>
</button>
Expand Down
5 changes: 5 additions & 0 deletions css/apple2.css
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ a.button:hover {
flex-grow: 1;
}

.spacer.short {
flex-grow: 0;
width: 16px;
}

#reset-row {
align-items: center;
display: flex;
Expand Down
25 changes: 25 additions & 0 deletions js/components/ClipboardCopy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { h } from 'preact';
import cs from 'classnames';
import { VideoModes } from 'js/videomodes';

import styles from './css/ControlButton.module.scss';

export interface ClipboardCopyProps {
vm: VideoModes | undefined;
}

export function ClipboardCopy({ vm }: ClipboardCopyProps) {
const doCopy = function () {
const asyncCopy = async function () {
if (vm) {
await navigator.clipboard.writeText(vm.getText());
}
};
void asyncCopy();
};
return (
<button className={styles.iconButton} onClick={doCopy} title="Copy">
<i className={cs('fa', 'fa-copy')} />
</button>
);
}
26 changes: 26 additions & 0 deletions js/components/ClipboardPaste.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { h } from 'preact';
import cs from 'classnames';
import Apple2IO from 'js/apple2io';

import styles from './css/ControlButton.module.scss';

export interface ClipboardPasteProps {
io: Apple2IO | undefined;
}

export function ClipboardPaste({ io }: ClipboardPasteProps) {
function doPaste() {
const asyncPaste = async function () {
if (io) {
const text = await navigator.clipboard.readText();
io.setKeyBuffer(text);
}
};
void asyncPaste();
}
return (
<button className={styles.iconButton} onClick={doPaste} title="Paste">
<i className={cs('fa', 'fa-paste')} />
</button>
);
}
11 changes: 10 additions & 1 deletion js/components/ControlStrip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { CPUMeter } from './CPUMeter';
import { Inset } from './Inset';
import { useHotKey } from './hooks/useHotKey';
import { AudioControl } from './AudioControl';
import { ClipboardCopy } from './ClipboardCopy';
import { ClipboardPaste } from './ClipboardPaste';
import { OptionsModal } from './OptionsModal';
import { OptionsContext } from './OptionsContext';
import { Printer } from './Printer';
Expand All @@ -14,8 +16,10 @@ import { JoyStick } from '../ui/joystick';
import { Screen, SCREEN_FULL_PAGE } from '../ui/screen';
import { System } from '../ui/system';

import styles from './css/ControlStrip.module.scss';
import Apple2IO from 'js/apple2io';
import { VideoModes } from 'js/videomodes';

import styles from './css/ControlStrip.module.scss';

const README = 'https://github.com/whscullin/apple2js#readme';

Expand All @@ -41,13 +45,15 @@ export const ControlStrip = ({
}: ControlStripProps) => {
const [showOptions, setShowOptions] = useState(false);
const [io, setIO] = useState<Apple2IO>();
const [vm, setVM] = useState<VideoModes>();
const options = useContext(OptionsContext);

useEffect(() => {
if (apple2) {
const io = apple2.getIO();
const vm = apple2.getVideoModes();
setIO(io);
setVM(vm);

const system = new System(io, e);
options.addOptions(system);
Expand Down Expand Up @@ -94,6 +100,9 @@ export const ControlStrip = ({
<AudioControl apple2={apple2} />
<Printer io={io} slot={1} />
<Cassette io={io} />
<div style={{ flexGrow: 0, width: 16 }} />
<ClipboardCopy vm={vm} />
<ClipboardPaste io={io} />
<div style={{ flexGrow: 1 }} />
<ControlButton onClick={doReadme} title="About" icon="info" />
<ControlButton
Expand Down
15 changes: 15 additions & 0 deletions js/ui/apple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,21 @@ export function openOptions() {
optionsModal.openModal();
}

export function copy() {
const asyncCopy = async function () {
await navigator.clipboard.writeText(vm.getText());
};
void asyncCopy();
}

export function paste() {
const asyncPaste = async function () {
const text = await navigator.clipboard.readText();
io.setKeyBuffer(text);
};
void asyncPaste();
}

export function openPrinterModal() {
const mimeType = 'application/octet-stream';
const data = _printer.getRawOutput();
Expand Down
Loading