From 34c3d071121309f05572b47b447272464f868776 Mon Sep 17 00:00:00 2001 From: Eric Tuvesson Date: Thu, 5 Sep 2024 13:35:15 +0200 Subject: [PATCH] feat(editor): Add "Used in x places" in Component List menu (#65) --- .../NodeReferencesContext.tsx | 11 +++++++++- .../panels/componentspanel/ComponentsPanel.ts | 21 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/noodl-editor/src/editor/src/contexts/NodeReferencesContext/NodeReferencesContext.tsx b/packages/noodl-editor/src/editor/src/contexts/NodeReferencesContext/NodeReferencesContext.tsx index fff24a1..b14e8f9 100644 --- a/packages/noodl-editor/src/editor/src/contexts/NodeReferencesContext/NodeReferencesContext.tsx +++ b/packages/noodl-editor/src/editor/src/contexts/NodeReferencesContext/NodeReferencesContext.tsx @@ -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({ 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); } diff --git a/packages/noodl-editor/src/editor/src/views/panels/componentspanel/ComponentsPanel.ts b/packages/noodl-editor/src/editor/src/views/panels/componentspanel/ComponentsPanel.ts index 9c842ca..a358c6b 100644 --- a/packages/noodl-editor/src/editor/src/views/panels/componentspanel/ComponentsPanel.ts +++ b/packages/noodl-editor/src/editor/src/views/panels/componentspanel/ComponentsPanel.ts @@ -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 });