Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
steveszc opened this issue Jan 6, 2021 · 2 comments
Open

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

steveszc opened this issue Jan 6, 2021 · 2 comments

Comments

@steveszc
Copy link

steveszc commented Jan 6, 2021

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.

constructor(options: MacroOptions) {
if (options.modifierKeys !== undefined) {
this.modifierKeys = A(options.modifierKeys);
}
if (options.element !== undefined) {
this.element = options.element;
}
this.callback = options.callback;
this.executionKey = options.executionKey;
if (options.keyEvent) {
this.keyEvent = options.keyEvent;
}
if (options.isDisabledOnInput !== undefined) {
this.isDisabledOnInput = options.isDisabledOnInput;
}
if (options.priority !== undefined) {
this.priority = options.priority;
}
if (options.groupName !== undefined) {
this.groupName = options.groupName;
}
if (options.isDisabled !== undefined) {
this.isDisabled = options.isDisabled;
}

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

@steveszc steveszc changed the title 1.0 (typescript??) broke the ability to add arbitrary properties to macros 1.0 broke the ability to add arbitrary properties to macros Jan 6, 2021
@patrickberkeley
Copy link
Member

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
Copy link
Author

steveszc commented Jan 7, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants