Proposal: Storage of the user keyboard shortcut preferences

Hello!

Context

XWIKI-16216 asks to let users rebind the keyboard shortcuts from their user profile. We already decided where the UI goes. A proposed implementation is in PR #2981.

Today, the shortcuts are defined at the wiki level only, as translation keys like core.shortcuts.view.edit=e. An admin can change them for the whole wiki with a translation page (documentation), but a user can’t change them for themselves.

Proposal

Storage

I propose a new class XWiki.UserPreferenceShortcutsClass with two string properties:

  • translationKey: the shortcut being overridden, for example core.shortcuts.view.information.
  • value: the user’s shortcut, for example b. An empty value would mean the user unbound this shortcut.

A user profile would hold one object per shortcut the user changed, and nothing else. If there’s no object for a shortcut, the wiki default from the translations would apply. A user who never touches the feature would have no extra data at all.

The value would use the same syntax as the translation it overrides, so it could hold any shortcut the wiki defaults can: a modifier combination like Alt+C, a special key like F2, or a sequence like x+x+x+a. In the user profile of the PR, the shortcut is a plain text field. Its hint recommends a single key to keep it simple, but combinations like Alt+Q work too. A sequence would be stored the same way, but the developer shortcuts of the PR register a customized value as a single-key shortcut.

Using the translation key as the identifier means the storage wouldn’t need to change when a shortcut is added, including one added by an extension. This is the main reason I moved away from the first version of the PR, which added one property per shortcut to XWiki.XWikiUsers.

This proposal wouldn’t block extensibility, but it wouldn’t complete it either. Two things would still be missing for an extension to make its shortcuts customizable:

  • The preferences UI of the PR is a fixed list of the core shortcuts, with no extension point to add rows.
  • #getShortcutValue is a macro of the Flamingo skin, which an extension can’t rely on.

Both could be added later on top of this storage, for example with the Java component of option 3 below.

Resolving a shortcut

A Velocity macro #getShortcutValue($translationKey) in the skin would resolve the shortcut to use:

It would read the user profile from $crtUserDoc, which is already loaded when a page is displayed, so getting all the shortcuts of a page would cost one getObject call per shortcut and no extra document loading.

Where it’s used

In the Flamingo skin, every place that registers a shortcut from a translation would go through this macro:

The editor buttons (Save, Cancel, Preview…) are a special case. Their shortcuts are registered in actionButtons.js. This skin file is evaluated with Velocity, but its URL is the same for every user and it’s served with Cache-Control: public for 30 days. Putting per-user values in it would serve one user’s shortcuts to another through the browser and proxy caches. Instead, the button would carry its shortcut in a data-shortcut attribute and the script would read it from there. Buttons that don’t have this attribute would keep the current default shortcuts.

Saving

In the preferences edit form, each shortcut would be a text input with a Reset button. The shortcuts would be saved with the form, like the other preferences, so Save and Cancel would apply to them too:

This would rely on the standard addedObjects and deletedObjects parameters of the save action. A small script would only decide, for each row, what the form submits:

Data

Since the translation key would identify the shortcut, it would become part of the stored user data:

  • An object whose translation key no longer exists, for example after a shortcut is removed, would simply be ignored. It wouldn’t break anything, but it would stay on the profile since the UI no longer shows that shortcut.
  • If a profile holds two objects for the same key, the first one would be used. The UI would never create a duplicate, since it updates the existing object.
  • Renaming a core.shortcuts.* key would silently drop the users’ customizations of that shortcut. This has happened before, core.shortcuts.edit.saveandcontinue became core.shortcuts.edit.save in 10.6. From now on, such a rename would need a migration of the objects, or would need to be avoided.

Open question: where the class lives

In the PR, XWiki.UserPreferenceShortcutsClass is a wiki page in the xwiki-platform-user-profile-ui XAR. But the skin (macros.vm, menus_content.vm, docextra.vm, shortcuts.vm) is what reads it, and the skin doesn’t depend on the user profile UI. If the class page is missing, getObject finds nothing and every shortcut falls back to its default, so nothing breaks, but the dependency isn’t explicit.

  1. Keep the class in xwiki-platform-user-profile-ui, next to the UI that edits it.
  2. Create the class with a mandatory document initializer in Java, so it exists as soon as the wiki starts, whatever is installed. It would go in xwiki-platform-user-default, as org.xwiki.user.internal.shortcut.UserPreferenceShortcutsClassDocumentInitializer extending AbstractMandatoryClassInitializer. This module is a core extension already bundled in the WAR, it’s about users, and it keeps the class out of oldcore, where new code shouldn’t go. This is the same pattern as EditorBindingClassDocumentInitializer in xwiki-platform-edit-default.
  3. Option 2, plus a Java component that resolves the shortcuts of the current user and lists the available ones, exposed through a script service in xwiki-platform-user-script, instead of the Velocity macro. This is also what would let extensions declare their own customizable shortcuts.

Opinion

+1 for the storage design described above because:

  • It only stores what the user actually changed.
  • Adding a shortcut, including from an extension, would never require changing the class, so it wouldn’t block full extensibility later.
  • Resetting to the default would just be deleting an object, and wiki-level defaults would keep working as before.

For the open question, I’m +1 for option 2 because:

  • The skin reads the class, so the class should exist independently of an optional UI module.

Conclusion

Do you agree with storing the shortcut preferences as one object per customized shortcut on the user profile? Which option do you prefer for the location of the class?

I’ll close this topic in a week and update the PR with its result if it doesn’t spur a large discussion.

Thank you!
Lucas C.

Sounds like an important improvement, I would at least make the current limitation clear in the UI.

General observation that we need to be careful with this part as it could hurt page rendering performance. But I believe we can cache and accurately invalidate the needed resources.

Did you consider introduce a dedicated xwiki-platform-shortcuts modules (and its ui, default, webjar sub-modules)?

Do you mean with a comment? I agree that it’s important, but the changes are already massive so I’d rather propose an implementation of this new API only after the mvp is merged and somewhat stable.

I did not consider it. I’m not sure how we decide when a feature deserves its own module. For now I made it fit mostly with the user profile but you might consider it clutters its code and we want it separated.

Thanks for your feedback Manuel!

I mean a short explanatory translatable text in the UI, to make it easy to understand why a given shortcut is not proposed in the configuration (i.e., because it comes from an extension).

We group modules by functional domains, shortcuts being really different from user profile, I tend to think that shortcut modules would make sense, unless someone disagree.
One way to think about it is that a shortcut menu in the global administration would also perfectly make sense and would be odd if provided by the user modules.

I haven’t followed the discussion precisely and I could be totally off but:

  • if it’s about to introduce some generic shortcut api, that will be used by other modules, then I agree it makes sense
  • OTOH, modules which need to add shortcuts for their feature should still do it in their own code.

The second point means that if we have a shortcut module it shouldn’t register any shortcut ideally as these should be registered by each module.

Okay!
Thank you for your help on the topic. I’ll update the PR to move this new code to its own xwiki-platform-shortcuts module :+1:

In general +1. I would suggest providing the shortcuts by a script service and adding a Java component behind it with caching that returns the shortcuts instead of handling this in Velocity code to ensure that this won’t become a performance bottleneck, in particular with many user-defined shortcuts which would be inefficient to look up. That would also go well with the mandatory document initializer and the enw module.

Well, this was possible before Loading... by creating a wiki page translation bundle scoped to the current user. I have the feeling that your proposal just works around this limitation. I’m wondering if it wouldn’t be better to add back support for user-level translation bundles by checking the translation values at registration time:

  • if the author doesn’t have script rights then verify if all the translation values are safe; reject the registration if not safe
  • if the author has script rights then register the translation bundle without checking if the values are safe

What does a safe value mean? We can start by allowing only alphanumeric and +, i.e. what’s needed for shortcut keys.

Thanks,
Marius