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

[wip] Add new keyboard shortcuts #355

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
51 changes: 35 additions & 16 deletions app/components/Builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
PuzzleSizeIcon,
EnterKey,
ExclamationKey,
AtSignKey,
} from './Icons';
import { AuthProps } from './AuthContext';
import { Histogram } from './Histogram';
Expand Down Expand Up @@ -1056,6 +1057,14 @@ const GridMode = ({
}
}, []);

const { autofillEnabled, setAutofillEnabled } = props;
const toggleAutofillEnabled = useCallback(() => {
if (autofillEnabled) {
showSnackbar('Autofill Disabled');
}
setAutofillEnabled(!autofillEnabled);
}, [autofillEnabled, setAutofillEnabled, showSnackbar]);

const physicalKeyboardHandler = useCallback(
(e: KeyboardEvent) => {
const tagName = (e.target as HTMLElement)?.tagName?.toLowerCase();
Expand All @@ -1066,12 +1075,13 @@ const GridMode = ({
const mkey = fromKeyboardEvent(e);
if (isSome(mkey)) {
/* TODO this logic belongs in the reducer */
if (mkey.value.k === KeyK.Enter && !state.isEnteringRebus) {
const key = mkey.value;
if (key.k === KeyK.Enter && !state.isEnteringRebus) {
reRunAutofill();
e.preventDefault();
return;
}
if (mkey.value.k === KeyK.Exclamation) {
if (key.k === KeyK.Exclamation) {
const entry = getMostConstrainedEntry();
if (entry !== null) {
const ca: ClickedEntryAction = {
Expand All @@ -1083,13 +1093,29 @@ const GridMode = ({
e.preventDefault();
return;
}
if (key.k === KeyK.AtSign) {
if (autofillEnabled) {
showSnackbar('Autofill Disabled');
}
setAutofillEnabled(!autofillEnabled);
e.preventDefault();
return;
}

const kpa: KeypressAction = { type: 'KEYPRESS', key: mkey.value };
dispatch(kpa);
e.preventDefault();
}
},
[dispatch, reRunAutofill, state.isEnteringRebus, getMostConstrainedEntry]
[
state.isEnteringRebus,
dispatch,
reRunAutofill,
getMostConstrainedEntry,
autofillEnabled,
setAutofillEnabled,
showSnackbar
]
);
useEventListener(
'keydown',
Expand Down Expand Up @@ -1200,14 +1226,6 @@ const GridMode = ({
return { left, right };
}, [state.grid, state.active, dispatch]);

const { autofillEnabled, setAutofillEnabled } = props;
const toggleAutofillEnabled = useCallback(() => {
if (autofillEnabled) {
showSnackbar('Autofill Disabled');
}
setAutofillEnabled(!autofillEnabled);
}, [autofillEnabled, setAutofillEnabled, showSnackbar]);

const stats = useMemo(() => {
let totalLength = 0;
const lengthHistogram: Array<number> = new Array(
Expand Down Expand Up @@ -1296,11 +1314,6 @@ const GridMode = ({
>
{() => (
<>
<TopBarDropDownLink
icon={autofillReverseIcon}
text={autofillReverseText}
onClick={toggleAutofillEnabled}
/>
<TopBarDropDownLink
icon={<FaSignInAlt />}
text="Jump to Most Constrained"
Expand All @@ -1316,6 +1329,12 @@ const GridMode = ({
}
}}
/>
<TopBarDropDownLink
icon={autofillReverseIcon}
text={autofillReverseText}
shortcutHint={<AtSignKey />}
onClick={toggleAutofillEnabled}
/>
<TopBarDropDownLink
icon={<MdRefresh />}
text="Rerun Autofiller"
Expand Down
191 changes: 52 additions & 139 deletions app/components/Icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,23 @@ export const Rebus = () => {
);
};

export const EscapeKey = () => {
const KeyIcon = (props: {
text: string;
width?: number;
stretchText?: boolean;
largeFont?: boolean;
className?: string;
textY?: number;
}) => {
return (
<svg
width="1em"
width={props.width ? `${props.width}em` : '1em'}
height="1em"
preserveAspectRatio="none"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
className={props.className}
>
<rect
x="5"
Expand All @@ -178,155 +187,59 @@ export const EscapeKey = () => {
/>
<text
x="50"
y="70"
y={props.textY || 70}
textAnchor="middle"
fontSize="60"
fontSize={props.largeFont ? 90 : 60}
fontWeight="bold"
lengthAdjust="spacingAndGlyphs"
{...(props.stretchText && { lengthAdjust: 'spacingAndGlyphs' })}
textLength="75"
>
esc
{props.text}
</text>
</svg>
);
};

export const EnterKey = () => {
return (
<svg
width="1em"
height="1em"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
>
<rect
x="5"
y="12"
rx="10"
ry="10"
width="90"
height="76"
fill="transparent"
stroke="currentColor"
strokeWidth="5"
/>
<text
x="50"
y="70"
textAnchor="middle"
fontSize="60"
fontWeight="bold"
lengthAdjust="spacingAndGlyphs"
textLength="75"
>
enter
</text>
</svg>
);
};
export const EscapeKey = () => <KeyIcon text="esc" stretchText={true} />;
export const EnterKey = () => (
<KeyIcon text="enter" width={1.5} stretchText={true} />
);

export const BacktickKey = () => {
return (
<svg
width="1em"
height="1em"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
>
<rect
x="5"
y="12"
rx="10"
ry="10"
width="90"
height="76"
fill="transparent"
stroke="currentColor"
strokeWidth="5"
/>
<text
x="50"
y="95"
textAnchor="middle"
fontSize="90"
fontWeight="bold"
textLength="75"
>
`
</text>
</svg>
);
};
export const BacktickKey = () => (
<KeyIcon text="`" largeFont={true} textY={95} />
);
export const PeriodKey = () => <KeyIcon text="." largeFont={true} />;
export const ExclamationKey = () => <KeyIcon text="!" />;
export const HashKey = () => <KeyIcon text="#" />;
export const AtSignKey = () => <KeyIcon text="@" />;

export const PeriodKey = () => {
return (
<svg
width="1em"
height="1em"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
>
<rect
x="5"
y="12"
rx="10"
ry="10"
width="90"
height="76"
fill="transparent"
stroke="currentColor"
strokeWidth="5"
/>
<text
x="50"
y="70"
textAnchor="middle"
fontSize="90"
fontWeight="bold"
textLength="75"
>
.
</text>
</svg>
);
};
const CtrlKey = () => (
<KeyIcon
text="ctrl"
stretchText={true}
width={1.1}
css={{ marginRight: '0.1em' }}
/>
);

export const ExclamationKey = () => {
return (
<svg
width="1em"
height="1em"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
>
<rect
x="5"
y="12"
rx="10"
ry="10"
width="90"
height="76"
fill="transparent"
stroke="currentColor"
strokeWidth="5"
/>
<text
x="50"
y="77"
textAnchor="middle"
fontSize="80"
fontWeight="bold"
textLength="75"
>
!
</text>
</svg>
);
};
export const CtrlHashKey = () => (
<>
<CtrlKey />
<HashKey />
</>
);
export const CtrlExclamationKey = () => (
<>
<CtrlKey />
<ExclamationKey />
</>
);
export const CtrlAtSignKey = () => (
<>
<CtrlKey />
<AtSignKey />
</>
);

const CheckOrReveal = ({
x,
Expand Down
Loading