(core) Add Command API to Grist Plugin API

Summary:
The new Command API provides limited access to Grist Commands from within cusotm
widgets. This includes the ability to perform undo and redo, which is bound to
the same keyboard shortcut as Grist by default.

Test Plan: Browser tests.

Reviewers: jarek

Reviewed By: jarek

Subscribers: paulfitz, jarek

Differential Revision: https://phab.getgrist.com/D4050
This commit is contained in:
George Gevoian
2023-09-26 16:37:27 -04:00
parent 9b36fb4dab
commit f38df564a9
8 changed files with 69 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import * as commands from 'app/client/components/commands';
import {Cursor} from 'app/client/components/Cursor';
import {GristDoc} from 'app/client/components/GristDoc';
import {
CommandAPI,
ConfigNotifier,
CustomSectionAPIImpl,
GristDocAPIImpl,
@@ -240,6 +241,10 @@ export class CustomView extends Disposable {
access,
this._promptAccess.bind(this)),
new MinimumLevel(AccessLevel.none));
frame.exposeAPI(
"CommandAPI",
new CommandAPI(access),
new MinimumLevel(AccessLevel.none));
frame.useEvents(RecordNotifier.create(frame, view), new MinimumLevel(AccessLevel.read_table));
frame.useEvents(TableNotifier.create(frame, view), new MinimumLevel(AccessLevel.read_table));
frame.exposeAPI(

View File

@@ -1,4 +1,6 @@
import BaseView from 'app/client/components/BaseView';
import {CommandName} from 'app/client/components/commandList';
import * as commands from 'app/client/components/commands';
import {GristDoc} from 'app/client/components/GristDoc';
import {hooks} from 'app/client/Hooks';
import {get as getBrowserGlobals} from 'app/client/lib/browserGlobals';
@@ -484,6 +486,26 @@ export class WidgetAPIImpl implements WidgetAPI {
}
}
const COMMAND_MINIMUM_ACCESS_LEVELS: Map<CommandName, AccessLevel> = new Map([
['undo', AccessLevel.full],
['redo', AccessLevel.full],
]);
export class CommandAPI {
constructor(private _currentAccess: AccessLevel) {}
public async run(commandName: CommandName): Promise<unknown> {
const minimumAccess = COMMAND_MINIMUM_ACCESS_LEVELS.get(commandName);
if (minimumAccess === undefined || !isSatisfied(this._currentAccess, minimumAccess)) {
// If the command name is unrecognized, or the current access level doesn't meet the
// command's minimum access level, do nothing.
return;
}
return await commands.allCommands[commandName].run();
}
}
/************************
* Events that are sent to the CustomWidget.
*

View File

@@ -65,6 +65,8 @@ export const widgetApi = rpc.getStub<WidgetAPI>('WidgetAPI', checkers.WidgetAPI)
*/
export const sectionApi = rpc.getStub<CustomSectionAPI>('CustomSectionAPI', checkers.CustomSectionAPI);
export const commandApi = rpc.getStub<any>('CommandAPI');
/**
* Shortcut for [[GristView.allowSelectBy]].
*/
@@ -437,6 +439,12 @@ export async function addImporter(name: string, path: string, mode: 'fullscreen'
});
}
export function enableKeyboardShortcuts() {
const Mousetrap = require('mousetrap');
Mousetrap.bind('mod+z', () => commandApi.run('undo'));
Mousetrap.bind(['mod+shift+z', 'ctrl+y'], () => commandApi.run('redo'));
}
/**
* Options when initializing connection to Grist.
*/