Skip to content

Commit

Permalink
Make it harder to steal character keys from the outer app (#549)
Browse files Browse the repository at this point in the history
- Move "S" key processing to the demo app (nobody needs it in the library),
- Require "Alt" key with numpad Plus/Minus (a kind of a breaking change but not documented and not used a lot),
- Process editing keys only when editing (an additional safety measure),
- Use `event.code` instead of deprecated `keyCode`.

The Alt+NumPlus, Alt+NumMinus will still be processed in the miew library unless miew.enableHotKeys(false) is called.
  • Loading branch information
paulsmirnov authored Aug 19, 2024
1 parent 6acff3d commit e35706d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
5 changes: 5 additions & 0 deletions packages/miew/demo/scripts/ui/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,11 @@ Menu.prototype._init = function () {
}
}
}
if (!self._terminal.is(':visible')) {
if (e.code === 'KeyS') {
settings.set('ao', !settings.now.ao);
}
}
});

$(`${self._menuId} [data-toggle="panel"]`).on('click', function _setPanel() {
Expand Down
112 changes: 58 additions & 54 deletions packages/miew/src/Miew.js
Original file line number Diff line number Diff line change
Expand Up @@ -2855,65 +2855,69 @@ Miew.prototype._onKeyDown = function (event) {
return;
}

switch (event.keyCode) {
case 'C'.charCodeAt(0):
if (settings.now.editing) {
// editing keys
if (settings.now.editing) {
switch (event.code) {
case 'KeyC':
this._enterComponentEditMode();
}
break;
case 'F'.charCodeAt(0):
if (settings.now.editing) {
break;
case 'KeyF':
this._enterFragmentEditMode();
break;
case 'KeyA':
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._applyComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._applyFragmentEdit();
break;
default:
break;
}
break;
case 'KeyD':
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._discardComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._discardFragmentEdit();
break;
default:
break;
}
break;
default:
}
}

// other keys
switch (event.code) {
case 'NumpadAdd':
if (event.altKey) {
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.expandSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
}
break;
case 'A'.charCodeAt(0):
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._applyComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._applyFragmentEdit();
break;
default: break;
}
break;
case 'D'.charCodeAt(0):
switch (this._editMode) {
case EDIT_MODE.COMPONENT:
this._discardComponentEdit();
break;
case EDIT_MODE.FRAGMENT:
this._discardFragmentEdit();
break;
default: break;
case 'NumpadSubtract':
if (event.altKey) {
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.shrinkSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
}
break;
case 'S'.charCodeAt(0):
event.preventDefault();
event.stopPropagation();
settings.set('ao', !settings.now.ao);
this._needRender = true;
break;
case 107:
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.expandSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
break;
case 109:
event.preventDefault();
event.stopPropagation();
this._forEachComplexVisual((visual) => {
visual.shrinkSelection();
visual.rebuildSelectionGeometry();
});
this._updateInfoPanel();
this._needRender = true;
break;
default:
}
};
Expand All @@ -2923,7 +2927,7 @@ Miew.prototype._onKeyUp = function (event) {
return;
}

if (event.keyCode === 'X'.charCodeAt(0)) {
if (event.code === 'KeyX') {
this._extractRepresentation();
}
};
Expand Down

0 comments on commit e35706d

Please sign in to comment.