IcarusWorks / ember-key-manager

A service for (un)binding keyboard up and down events.
MIT License
42 stars 11 forks source link

1.0 broke the ability to add arbitrary properties to macros #60

Open steveszc opened 3 years ago

steveszc commented 3 years ago

It was previously possible to add arbitrary properties to the macro objects, this seems to be broken after the 1.0.0 release.

Usecase: In the TrueCoach app we added groupHeader and summary properties to the macro objects so that we could show users what keyboard shortcuts are available and what they do, and store all of that information on the macro object itself.

This seems to no longer be possible since the Macro's constructor is manually setting only specific options that it expects. https://github.com/IcarusWorks/ember-key-manager/blob/591992f6fdf541da07ba44b40e6b00f186ad381d/addon/utils/macro.ts#L28-L51

A possible solution may be a meta property that can act as a grab-bag for additional user-land properties for the macro.

patrickberkeley commented 3 years ago

A meta prop would be one way to go. Another solution would be to allow optional key-value pairs on MacroOptions.

export interface MacroOptions {
  callback: KeyMacroModifierCallback;
  executionKey: string;
  modifierKeys?: string[],
  keyEvent?: KeyEvent;
  element?: HTMLElement;
  isDisabledOnInput?: boolean;
  priority?: number;
  groupName?: string | null;
  isDisabled?: boolean;
  [key: string]: unknown; // <--- added
}

Then loop all the keys (completely untested):

  constructor(options: MacroOptions) {
    Object.keys(options).forEach((key) => {
      if (options[key] === 'modifierKeys') {
        this.modifierKeys = A(options.modifierKeys);
      } else {
        this[key] = options[key];
      }
    });
  }
steveszc commented 3 years ago

I think either approach would be fine. Your suggestion would be nice since it keep compatibility with pre-1.x functionality. But I also like the explicitness of a meta property and the clear delineation it provides between the MacroOptions properties and custom properties.