Introduce menu entry support

Summary:
[interesting] since it shows how Flipper APIs are exposed through sandy. However, the next diff is a much simpler example of that

This diff adds support for adding menu entries for sandy plugin (renamed keyboard actions to menus, as it always creates a menu entry, but not necessarily a keyboard shortcut)

```

  client.addMenuEntry(
    // custom entry
    {
      label: 'Reset Selection',
      topLevelMenu: 'Edit',
      handler: () => {
        selectedID.set(null);
      },
    },
    // based on built-in action (sets standard label, shortcut)
    {
      action: 'createPaste',
      handler: () => {
        console.log('creating paste');
      },
    },
  );
```

Most of this diff is introducing the concept of FlipperUtils, a set of static Flipper methods (not related to a device or client) that can be used from Sandy. This will for example be used to implement things as `createPaste` as well

Reviewed By: nikoant

Differential Revision: D22766990

fbshipit-source-id: ce90af3b700e6c3d9a779a3bab4673ba356f3933
This commit is contained in:
Michel Weststrate
2020-08-04 07:44:56 -07:00
committed by Facebook GitHub Bot
parent 94eaaf5dca
commit 9c202a4a10
21 changed files with 335 additions and 48 deletions

View File

@@ -0,0 +1,66 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
export type DefaultKeyboardAction = 'clear' | 'goToBottom' | 'createPaste';
export type TopLevelMenu = 'Edit' | 'View' | 'Window' | 'Help';
export type MenuEntry = BuiltInMenuEntry | CustomMenuEntry;
export type NormalizedMenuEntry = {
label: string;
accelerator?: string;
topLevelMenu: TopLevelMenu;
handler: () => void;
action: string;
};
export type CustomMenuEntry = {
label: string;
accelerator?: string;
topLevelMenu: TopLevelMenu;
handler: () => void;
};
export type BuiltInMenuEntry = {
action: keyof typeof buildInMenuEntries;
handler: () => void;
};
export const buildInMenuEntries = {
clear: {
label: 'Clear',
accelerator: 'CmdOrCtrl+K',
topLevelMenu: 'View',
action: 'clear',
},
goToBottom: {
label: 'Go To Bottom',
accelerator: 'CmdOrCtrl+B',
topLevelMenu: 'View',
action: 'goToBottom',
},
createPaste: {
label: 'Create Paste',
topLevelMenu: 'Edit',
action: 'createPaste',
},
} as const;
export function normalizeMenuEntry(entry: MenuEntry): NormalizedMenuEntry;
export function normalizeMenuEntry(entry: any): NormalizedMenuEntry {
const builtInEntry:
| NormalizedMenuEntry
| undefined = (buildInMenuEntries as any)[entry.action];
return builtInEntry
? {...builtInEntry, ...entry}
: {
...entry,
action: entry.action || entry.label,
};
}