add Ctrl-D keyboard shortcut in docs iframe

This way it still works when the docs are focused. Otherwise the browser
would try to bookmark the page.
This commit is contained in:
David Lechner
2021-01-27 16:02:37 -06:00
parent ca8192084d
commit 1d7414bd83
+20 -1
View File
@@ -2,9 +2,12 @@
// Copyright (c) 2020 The Pybricks Authors
import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import SplitterLayout from 'react-splitter-layout';
import { toggleBoolean } from '../actions/settings';
import { RootState } from '../reducers';
import { SettingId } from '../settings/user';
import { isMacOS } from '../utils/os';
import Editor from './Editor';
import SettingsDrawer from './SettingsDrawer';
import StatusBar from './StatusBar';
@@ -17,6 +20,7 @@ import './app.scss';
function App(): JSX.Element {
const showDocs = useSelector((s: RootState): boolean => s.settings.showDocs);
const [dragging, setDragging] = useState(false);
const dispatch = useDispatch();
return (
<div className="app">
@@ -119,6 +123,21 @@ function App(): JSX.Element {
}
}
});
// Override browser default key bindings in iframe.
contentWindow.document.addEventListener('keydown', (e) => {
// use Ctrl-D/Cmd-D to toggle docs
if (
(isMacOS()
? e.metaKey && !e.ctrlKey
: e.ctrlKey && !e.metaKey) &&
!e.altKey &&
e.key == 'd'
) {
e.preventDefault();
dispatch(toggleBoolean(SettingId.ShowDocs));
}
});
}}
src="static/docs/index.html"
allowFullScreen={true}