mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
some cleanup
This commit is contained in:
@@ -31,7 +31,13 @@ export interface ICustomWidget {
|
||||
*/
|
||||
renderAfterReady?: boolean;
|
||||
|
||||
fromPlugin?: string;
|
||||
/**
|
||||
* If the widget came from a plugin, we track that here.
|
||||
*/
|
||||
source?: {
|
||||
pluginId: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,64 +70,20 @@ export function isSatisfied(current: AccessLevel, minimum: AccessLevel) {
|
||||
return ordered(current) >= ordered(minimum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the best match for a widgetId/pluginId combination among the
|
||||
* given widgets. An exact widgetId match is required. A pluginId match
|
||||
* is preferred but not required.
|
||||
*/
|
||||
export function matchWidget(widgets: ICustomWidget[], options: {
|
||||
widgetId?: string,
|
||||
widgetId: string,
|
||||
pluginId?: string,
|
||||
}): ICustomWidget|undefined {
|
||||
console.log("MATCHING", {
|
||||
widgets,
|
||||
options,
|
||||
});
|
||||
const prefs = sortBy(widgets, (w) => {
|
||||
return [w.widgetId !== options.widgetId,
|
||||
(w.fromPlugin||'') !== options.pluginId]
|
||||
(w.source?.pluginId||'') !== options.pluginId]
|
||||
});
|
||||
if (prefs.length === 0) { return; }
|
||||
if (options.widgetId && prefs[0].widgetId !== options.widgetId) {
|
||||
return;
|
||||
}
|
||||
console.log("ORDERED", prefs);
|
||||
console.log("MATCHED", prefs[0]);
|
||||
if (prefs[0].widgetId !== options.widgetId) { return };
|
||||
return prefs[0];
|
||||
}
|
||||
|
||||
export function filterWidgets(widgets: ICustomWidget[], options: {
|
||||
preferPlugin?: boolean,
|
||||
keepWidgetIdUnique?: boolean,
|
||||
}) {
|
||||
const folders = new Map<string, ICustomWidget[]>();
|
||||
for (const widget of widgets) {
|
||||
const widgetId = widget.widgetId;
|
||||
if (!folders.has(widgetId)) { folders.set(widgetId, []); }
|
||||
const widgetFolder = folders.get(widgetId)!;
|
||||
widgetFolder.push(widget);
|
||||
}
|
||||
let finalResults: ICustomWidget[] = widgets;
|
||||
if (options.preferPlugin !== undefined) {
|
||||
const results = [];
|
||||
const seen = new Set<string>();
|
||||
for (const widget of widgets) {
|
||||
const folder = folders.get(widget.widgetId)!;
|
||||
if (folder.length === 1) {
|
||||
results.push(widget);
|
||||
continue;
|
||||
}
|
||||
if (seen.has(widget.widgetId)) { continue; }
|
||||
seen.add(widget.widgetId);
|
||||
const folderSorted = sortBy(folder, (w) => Boolean(w.fromPlugin) !== options.preferPlugin);
|
||||
results.push(folderSorted[0]!);
|
||||
}
|
||||
finalResults = results;
|
||||
}
|
||||
if (options.keepWidgetIdUnique) {
|
||||
const results = [];
|
||||
const seen = new Set<string>();
|
||||
for (const widget of widgets) {
|
||||
if (seen.has(widget.widgetId)) { continue; }
|
||||
seen.add(widget.widgetId);
|
||||
results.push(widget);
|
||||
}
|
||||
finalResults = results;
|
||||
}
|
||||
return finalResults;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user