Skip to content

Commit

Permalink
Fix Ace trapping focus by overwriting Tab key
Browse files Browse the repository at this point in the history
Esc disables Ace's focus trapping and reenables the default browser
behavior for the `Tab` and `Shift+Tab` keys.

On macOS `Option+Tab` and `Option+Shift+Tab` seem to work too

See ajaxorg/ace#3149 (comment)
  • Loading branch information
maxpatiiuk committed Aug 5, 2021
1 parent acdd73c commit ed646d6
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions specifyweb/frontend/js_src/lib/appresources.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ function fileExtFor(resource) {
return "";
}

// Copied from https://github.com/ajaxorg/ace/issues/3149#issuecomment-444570508
function setCommandEnabled(editor, name, enabled) {
const command = editor.commands.byName[name];
if (!command.bindKeyOriginal)
command.bindKeyOriginal = command.bindKey;
command.bindKey = enabled ? command.bindKeyOriginal : null;
editor.commands.addCommand(command);
// special case for backspace and delete which will be called from
// textarea if not handled by main commandb binding
if (!enabled) {
var key = command.bindKeyOriginal;
if (key && typeof key == "object")
key = key[editor.commands.platform];
if (/backspace|delete/i.test(key))
editor.commands.bindKey(key, "null")
}
}

const AppResourcePage = Backbone.View.extend({
__name__: "AppresourcePage",
id: "appresource-page",
Expand Down Expand Up @@ -144,6 +162,21 @@ const ResourceDataView = Backbone.View.extend({
editor.setValue(this.appresourceData.get('data'));
editor.setPrintMarginColumn(null);
editor.clearSelection();

editor.on('focus', ()=>{
setCommandEnabled(editor, "indent", true);
setCommandEnabled(editor, "outdent", true);
});

editor.commands.addCommand({
name: "escape",
bindKey: {win: "Esc", mac: "Esc"},
exec() {
setCommandEnabled(editor, "indent", false);
setCommandEnabled(editor, "outdent", false);
}
});

editor.on("change", () => {
this.appresourceData.set('data', editor.getValue(), {changedBy: editor});
});
Expand Down Expand Up @@ -446,7 +479,7 @@ const DisciplineResourcesView = Backbone.View.extend({
type="button"
class="fake-link"
>
${this.discipline.get('name')}
${this.discipline.get('name')}
<small>(${this.count})</small>
</button>`)),
$('<div>').append(
Expand Down Expand Up @@ -520,7 +553,7 @@ const CollectionResourcesView = Backbone.View.extend({
type="button"
class="fake-link"
>
${this.collection.get('collectionname')}
${this.collection.get('collectionname')}
<small>(${this.count})</small>
</button>`)),
$('<div>').append(
Expand All @@ -531,7 +564,7 @@ const CollectionResourcesView = Backbone.View.extend({
type="button"
class="fake-link"
>
${adminText('userTypes')}
${adminText('userTypes')}
<small>(${this.userTypeView.count})</small>
</button>`)),
this.userTypeView.render().el,
Expand All @@ -541,7 +574,7 @@ const CollectionResourcesView = Backbone.View.extend({
type="button"
class="fake-link"
>
${adminText('users')}
${adminText('users')}
<small>(${this.userView.count})</small>
</button>`)),
this.userView.render().el
Expand Down Expand Up @@ -621,7 +654,7 @@ const UserTypeResourcesView = Backbone.View.extend({
type="button"
class="fake-link"
>
${this.usertype}
${this.usertype}
<small>(${this.count})</small>
</button>`)),
this.resourceList.render().$el
Expand Down Expand Up @@ -703,7 +736,7 @@ const UserResourcesView = Backbone.View.extend({
type="button"
class="fake-link"
>
${this.user.get('name')}
${this.user.get('name')}
<small>(${this.count})</small>
</button>`)),
this.resourceList.render().$el
Expand Down

0 comments on commit ed646d6

Please sign in to comment.