mirror of
https://github.com/fluxscape/fluxscape.git
synced 2026-01-12 07:12:54 +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';
|
import { EventDispatcher } from '../../../../shared/utils/EventDispatcher';
|
||||||
|
|
||||||
export type NodeReference = {
|
export type NodeReference = {
|
||||||
type: NodeLibraryNodeType;
|
type: NodeLibraryNodeType | undefined;
|
||||||
displayName: string;
|
displayName: string;
|
||||||
referenaces: {
|
referenaces: {
|
||||||
displayName: string;
|
displayName: string;
|
||||||
@@ -25,6 +25,14 @@ const NodeReferencesContext = createContext<NodeReferencesContext>({
|
|||||||
nodeReferences: [],
|
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 {
|
export interface NodeReferencesContextProps {
|
||||||
children: Slot;
|
children: Slot;
|
||||||
}
|
}
|
||||||
@@ -89,6 +97,7 @@ export function NodeReferencesContextProvider({ children }: NodeReferencesContex
|
|||||||
}))
|
}))
|
||||||
.sort((a, b) => b.referenaces.length - a.referenaces.length);
|
.sort((a, b) => b.referenaces.length - a.referenaces.length);
|
||||||
|
|
||||||
|
HACK_nodeReferences = results;
|
||||||
setNodeReferences(results);
|
setNodeReferences(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ import { EventDispatcher } from '../../../../../shared/utils/EventDispatcher';
|
|||||||
import View from '../../../../../shared/view';
|
import View from '../../../../../shared/view';
|
||||||
import { NodeGraphEditor } from '../../nodegrapheditor';
|
import { NodeGraphEditor } from '../../nodegrapheditor';
|
||||||
import * as NewPopupLayer from '../../PopupLayer/index';
|
import * as NewPopupLayer from '../../PopupLayer/index';
|
||||||
|
import { type PopupMenuItem } from '../../PopupLayer/index';
|
||||||
import { ToastLayer } from '../../ToastLayer/ToastLayer';
|
import { ToastLayer } from '../../ToastLayer/ToastLayer';
|
||||||
import { ComponentsPanelFolder } from './ComponentsPanelFolder';
|
import { ComponentsPanelFolder } from './ComponentsPanelFolder';
|
||||||
import { ComponentTemplates } from './ComponentTemplates';
|
import { ComponentTemplates } from './ComponentTemplates';
|
||||||
|
import { HACK_findNodeReference } from '@noodl-contexts/NodeReferencesContext';
|
||||||
|
|
||||||
const PopupLayer = require('@noodl-views/popuplayer');
|
const PopupLayer = require('@noodl-views/popuplayer');
|
||||||
const ComponentsPanelTemplate = require('../../../templates/componentspanel.html');
|
const ComponentsPanelTemplate = require('../../../templates/componentspanel.html');
|
||||||
@@ -961,7 +963,7 @@ export class ComponentsPanelView extends View {
|
|||||||
forRuntimeType: this.getRuntimeType()
|
forRuntimeType: this.getRuntimeType()
|
||||||
});
|
});
|
||||||
|
|
||||||
let items: TSFixme[] = templates.map((t) => ({
|
let items: PopupMenuItem[] = templates.map((t) => ({
|
||||||
icon: IconName.Plus,
|
icon: IconName.Plus,
|
||||||
label: t.label,
|
label: t.label,
|
||||||
onClick: () => {
|
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([
|
items = items.concat([
|
||||||
{
|
{
|
||||||
icon: IconName.Pencil,
|
icon: IconName.Pencil,
|
||||||
@@ -1011,6 +1017,9 @@ export class ComponentsPanelView extends View {
|
|||||||
_this.onDeleteClicked(scope, el);
|
_this.onDeleteClicked(scope, el);
|
||||||
evt.stopPropagation();
|
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({
|
const menu = new NewPopupLayer.PopupMenu({
|
||||||
items: items
|
items: items
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user