feat(editor): Add "Used in x places" in Component List menu (#65)

This commit is contained in:
Eric Tuvesson
2024-09-05 13:35:15 +02:00
committed by GitHub
parent d85dce8d02
commit 34c3d07112
2 changed files with 30 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ import { ProjectModel } from '@noodl-models/projectmodel';
import { EventDispatcher } from '../../../../shared/utils/EventDispatcher';
export type NodeReference = {
type: NodeLibraryNodeType;
type: NodeLibraryNodeType | undefined;
displayName: string;
referenaces: {
displayName: string;
@@ -25,6 +25,14 @@ const NodeReferencesContext = createContext<NodeReferencesContext>({
nodeReferences: [],
});
// Since all the editor code is not written in React we need a way to be able to
// access this information outside of React too.
let HACK_nodeReferences: NodeReference[] = [];
export function HACK_findNodeReference(componentName: string): NodeReference | undefined {
return HACK_nodeReferences.find(x => x.type?.fullName === componentName);
}
export interface NodeReferencesContextProps {
children: Slot;
}
@@ -89,6 +97,7 @@ export function NodeReferencesContextProvider({ children }: NodeReferencesContex
}))
.sort((a, b) => b.referenaces.length - a.referenaces.length);
HACK_nodeReferences = results;
setNodeReferences(results);
}

View File

@@ -17,9 +17,11 @@ import { EventDispatcher } from '../../../../../shared/utils/EventDispatcher';
import View from '../../../../../shared/view';
import { NodeGraphEditor } from '../../nodegrapheditor';
import * as NewPopupLayer from '../../PopupLayer/index';
import { type PopupMenuItem } from '../../PopupLayer/index';
import { ToastLayer } from '../../ToastLayer/ToastLayer';
import { ComponentsPanelFolder } from './ComponentsPanelFolder';
import { ComponentTemplates } from './ComponentTemplates';
import { HACK_findNodeReference } from '@noodl-contexts/NodeReferencesContext';
const PopupLayer = require('@noodl-views/popuplayer');
const ComponentsPanelTemplate = require('../../../templates/componentspanel.html');
@@ -961,7 +963,7 @@ export class ComponentsPanelView extends View {
forRuntimeType: this.getRuntimeType()
});
let items: TSFixme[] = templates.map((t) => ({
let items: PopupMenuItem[] = templates.map((t) => ({
icon: IconName.Plus,
label: t.label,
onClick: () => {
@@ -987,6 +989,10 @@ export class ComponentsPanelView extends View {
});
}
// Find references
const nodeReference = HACK_findNodeReference(scope.comp.name);
const nodeReferencesText = `Used in ${nodeReference?.referenaces?.length || 0} places`;
items = items.concat([
{
icon: IconName.Pencil,
@@ -1011,6 +1017,9 @@ export class ComponentsPanelView extends View {
_this.onDeleteClicked(scope, el);
evt.stopPropagation();
}
},
{
label: nodeReferencesText
}
]);
@@ -1110,6 +1119,16 @@ export class ComponentsPanelView extends View {
}
]);
if (scope.canBecomeRoot) {
// Find references
const nodeReference = HACK_findNodeReference(scope.folder.component.name);
const nodeReferencesText = `Used in ${nodeReference?.referenaces?.length || 0} places`;
items = items.concat([{
label: nodeReferencesText
}]);
}
const menu = new NewPopupLayer.PopupMenu({
items: items
});