mirror of
https://github.com/fluxscape/fluxscape.git
synced 2026-01-10 14:22:53 +01:00
feat(editor): Add "Used in x places" in Component List menu (#65)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user