(core) Show default context menu on link

Summary:
also:
  - closes opened menu if any when click on a custom widget
  - closes opened menu if any when F2

Test Plan: Include test case

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3269
This commit is contained in:
Cyprien P
2022-02-16 15:15:27 +01:00
parent f224afcc62
commit afa90cc365
6 changed files with 31 additions and 5 deletions

View File

@@ -24,6 +24,9 @@ export function gristLink(href: string|Observable<string>, ...args: IDomArgs<HTM
dom.attr("href", href),
dom.attr("target", "_blank"),
dom.on("click", ev => onClickHyperLink(ev, typeof href === 'string' ? href : href.get())),
// stop propagation to prevent the grist custom context menu to show up and let the default one
// to show up instead.
dom.on("contextmenu", ev => ev.stopPropagation()),
// As per Google and Mozilla recommendations to prevent opened links
// from running on the same process as Grist:
// https://developers.google.com/web/tools/lighthouse/audits/noopener

View File

@@ -17,11 +17,28 @@ export interface IOptionFull<T> {
icon?: IconName;
}
let _lastOpenedController: weasel.IOpenController|null = null;
// Close opened menu if any, otherwise do nothing.
export function closeRegisteredMenu() {
if (_lastOpenedController) { _lastOpenedController.close(); }
}
// Register `ctl` to make sure it is closed when `closeMenu()` is called.
export function registerMenuOpen(ctl: weasel.IOpenController) {
_lastOpenedController = ctl;
ctl.onDispose(() => _lastOpenedController = null);
}
// For string options, we can use a string for label and value without wrapping into an object.
export type IOption<T> = (T & string) | IOptionFull<T>;
export function menu(createFunc: weasel.MenuCreateFunc, options?: weasel.IMenuOptions): DomElementMethod {
return weasel.menu(createFunc, {...defaults, ...options});
const wrappedCreateFunc = (ctl: weasel.IOpenController) => {
registerMenuOpen(ctl);
return createFunc(ctl);
};
return weasel.menu(wrappedCreateFunc, {...defaults, ...options});
}
// TODO Weasel doesn't allow other options for submenus, but probably should.