Skip to content

Commit 5542f58

Browse files
committed
Prevent delete/backspace from deleting blocks in fullscreen or when project uses those keys
#1106
1 parent 4f50de5 commit 5542f58

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/lib/vm-listener-hoc.jsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {setCustomStageSize} from '../reducers/custom-stage-size';
2626
import {openUnknownPlatformModal} from '../reducers/modals';
2727
import implementGuiAPI from './tw-extension-gui-api';
28+
import {BLOCKS_TAB_INDEX} from '../reducers/editor-tab';
2829

2930
let compileErrorCounter = 0;
3031

@@ -176,6 +177,23 @@ const vmListenerHOC = function (WrappedComponent) {
176177
if (e.keyCode === 8) {
177178
e.preventDefault();
178179
}
180+
181+
// TW: prevent delete and backspace from deleting blocks in the editor while in fullscreen
182+
// or in the editor too if the project has been using these keys for something
183+
const blockEditorDeleteOperation = scratchKey => (
184+
this.props.isEditorObscured || (
185+
this.props.isEditorUsable &&
186+
this.props.vm.runtime.ioDevices.keyboard.hasUsedKey(scratchKey)
187+
)
188+
);
189+
if (
190+
(e.keyCode === 8 && blockEditorDeleteOperation('backspace')) ||
191+
(e.keyCode === 46 && blockEditorDeleteOperation('delete'))
192+
) {
193+
e.stopPropagation();
194+
e.stopImmediatePropagation();
195+
}
196+
179197
// tw: prevent ' and / from opening quick find in Firefox
180198
if (e.keyCode === 222 || e.keyCode === 191) {
181199
e.preventDefault();
@@ -200,6 +218,8 @@ const vmListenerHOC = function (WrappedComponent) {
200218
const {
201219
/* eslint-disable no-unused-vars */
202220
attachKeyboardEvents,
221+
isEditorObscured,
222+
isEditorUsable,
203223
projectChanged,
204224
shouldUpdateTargets,
205225
shouldUpdateProjectChanged,
@@ -237,6 +257,8 @@ const vmListenerHOC = function (WrappedComponent) {
237257
}
238258
VMListener.propTypes = {
239259
attachKeyboardEvents: PropTypes.bool,
260+
isEditorObscured: PropTypes.bool.isRequired,
261+
isEditorUsable: PropTypes.bool.isRequired,
240262
onBlockDragUpdate: PropTypes.func.isRequired,
241263
onGreenFlag: PropTypes.func,
242264
onKeyDown: PropTypes.func,
@@ -275,6 +297,15 @@ const vmListenerHOC = function (WrappedComponent) {
275297
};
276298
const mapStateToProps = state => ({
277299
hasCloudVariables: state.scratchGui.tw.hasCloudVariables,
300+
isEditorObscured: (
301+
!state.scratchGui.mode.isPlayerOnly &&
302+
state.scratchGui.mode.isFullScreen
303+
),
304+
isEditorUsable: (
305+
!state.scratchGui.mode.isPlayerOnly &&
306+
!state.scratchGui.mode.isFullScreen &&
307+
state.scratchGui.editorTab.activeTabIndex === BLOCKS_TAB_INDEX
308+
),
278309
projectChanged: state.scratchGui.projectChanged,
279310
// Do not emit target or project updates in fullscreen or player only mode
280311
// or when recording sounds (it leads to garbled recordings on low-power machines)

0 commit comments

Comments
 (0)