Skip to content

Commit

Permalink
enhance: keyboard events (esc, enter, etc) for modals
Browse files Browse the repository at this point in the history
  • Loading branch information
debanjandhar12 committed Jan 17, 2023
1 parent b7b1e99 commit 6bc8ca8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ui/Confirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,28 @@ export async function Confirm(msg: string): Promise<boolean> {
</div>
</div>
</div>`;
const onKeydown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
// @ts-ignore
window.parent.sync_cancel_action();
}
else if (e.key === 'Enter') {
// @ts-ignore
window.parent.sync_yes_action();
}
}
window.parent.document.addEventListener('keydown', onKeydown);
// @ts-ignore
window.parent.sync_yes_action = () => {
resolve(true);
window.parent.document.body.removeChild(div);
window.parent.document.removeEventListener('keydown', onKeydown);
}
// @ts-ignore
window.parent.sync_cancel_action = () => {
resolve(false);
window.parent.document.body.removeChild(div);
window.parent.document.removeEventListener('keydown', onKeydown);
}
window.parent.document.body.appendChild(div);
});
Expand Down
52 changes: 52 additions & 0 deletions src/ui/OcclusionEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ export async function OcclusionEditor(imgURL: string, occlusionArr: Array<any>):
root.render(<OcclusionEditorComponent imgURL={imgURL} occlusionArr={occlusionArr} ref={fabricRef} />);
} catch (e) { resolve(false); window.parent.document.body.removeChild(div); logseq.App.showMsg("Failed to mount OcclusionEditor! Error Message: " + e); console.error(e); }

const onKeydown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
window.parent.occlusion_cancel_action();
}
else if (e.key === 'Enter') {
window.parent.occlusion_save_action();
}
else if (e.key === 's' && e.ctrlKey) {
window.parent.occlusion_save_action();
}
};
window.parent.document.addEventListener('keydown', onKeydown);
// @ts-ignore
window.parent.occlusion_save_action = () => {
// Iterate over all objects and save them
Expand All @@ -67,11 +79,13 @@ export async function OcclusionEditor(imgURL: string, occlusionArr: Array<any>):
});
resolve(occlusionArr);
window.parent.document.body.removeChild(div);
window.parent.document.removeEventListener('keydown', onKeydown);
}
// @ts-ignore
window.parent.occlusion_cancel_action = () => {
resolve(false);
window.parent.document.body.removeChild(div);
window.parent.document.removeEventListener('keydown', onKeydown);
}
});
}
Expand Down Expand Up @@ -162,6 +176,44 @@ const OcclusionEditorComponent = React.forwardRef(({imgURL, occlusionArr}, fabri
fabricRef.current.on('object:modified', preventOutOfBounds);
}, [fabricRef]);

// Handle some key events
React.useEffect(() => {
const onKeydown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
fabricRef.current.discardActiveObject();
}
else if (e.key === 'ArrowUp') {
if (fabricSelection) {
fabricSelection.top -= 1;
fabricRef.current.renderAll();
e.preventDefault();
}
}
else if (e.key === 'ArrowDown') {
if (fabricSelection) {
fabricSelection.top += 1;
fabricRef.current.renderAll();
e.preventDefault();
}
}
else if (e.key === 'ArrowLeft') {
if (fabricSelection) {
fabricSelection.left -= 1;
fabricRef.current.renderAll();
}
}
else if (e.key === 'ArrowRight') {
if (fabricSelection) {
fabricSelection.left += 1;
fabricRef.current.renderAll();
}
}
};
window.parent.document.addEventListener('keydown', onKeydown);
return () => {
window.parent.document.removeEventListener('keydown', onKeydown);
};
});
// Create the UI
const addOcclusion = () => {
let randomLocation = {
Expand Down
7 changes: 7 additions & 0 deletions src/ui/SelectPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export async function SelectPrompt(msg: string, options: string[]): Promise<stri
</div>
</div>
</div>`;
const onKeydown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
window.parent.select_cancel_action();
}
};
window.parent.document.addEventListener('keydown', onKeydown);
// @ts-ignore
window.parent.handle_select_action = () => {
const select = div.querySelector('#select-prompt-selector');
Expand All @@ -40,5 +46,6 @@ export async function SelectPrompt(msg: string, options: string[]): Promise<stri
window.parent.document.body.removeChild(div);
}
window.parent.document.body.appendChild(div);
(div.querySelector('#select-prompt-selector') as HTMLSelectElement).focus();
});
}

0 comments on commit 6bc8ca8

Please sign in to comment.