From 0c0bfe48ad608ad8bda86d12d5c2dd925cfd8731 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:38:25 +0100 Subject: [PATCH] perf(additional-components): inject slots to avoid performance drop --- .../src/minimap/MiniMap.vue | 30 +++---------------- .../src/minimap/MiniMapNode.ts | 10 ++++--- .../src/minimap/types.ts | 5 +++- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/packages/additional-components/src/minimap/MiniMap.vue b/packages/additional-components/src/minimap/MiniMap.vue index c1ad3e26..361bae70 100644 --- a/packages/additional-components/src/minimap/MiniMap.vue +++ b/packages/additional-components/src/minimap/MiniMap.vue @@ -8,6 +8,7 @@ import type { PanelPosition } from '../panel' import { Panel } from '../panel' import type { MiniMapNodeFunc, MiniMapProps, ShapeRendering } from './types' import MiniMapNode from './MiniMapNode' +import { MiniMapSlots } from './types' const { width, @@ -34,14 +35,7 @@ const { id, edges, viewport, dimensions, emits, nodes, d3Selection, d3Zoom } = u const el = ref() -const slots = useSlots() - -const hasSlot = (type: string) => { - const slotType = slots[type] - - console.log(slotType && slotType().length > 0) - return !!(slotType && slotType().length > 0) -} +provide(MiniMapSlots, useSlots()) const elementWidth = computed(() => width ?? attrs.style?.width ?? defaultWidth) @@ -219,29 +213,13 @@ export default { :stroke-color="nodeStrokeColorFunc(node)" :stroke-width="nodeStrokeWidth" :shape-rendering="shapeRendering" + :type="node.type" @click="onNodeClick($event, node)" @dblclick="onNodeDblClick($event, node)" @mouseenter="onNodeMouseEnter($event, node)" @mousemove="onNodeMouseMove($event, node)" @mouseleave="onNodeMouseLeave($event, node)" - > - - - + /> diff --git a/packages/additional-components/src/minimap/MiniMapNode.ts b/packages/additional-components/src/minimap/MiniMapNode.ts index bdbf173a..098e5f1c 100644 --- a/packages/additional-components/src/minimap/MiniMapNode.ts +++ b/packages/additional-components/src/minimap/MiniMapNode.ts @@ -1,12 +1,14 @@ -import type { CSSProperties } from 'vue' +import type { CSSProperties, Slots } from 'vue' import type { MiniMapNodeProps } from './types' +import { MiniMapSlots } from './types' export default defineComponent({ name: 'MiniMapNode', - props: ['id', 'position', 'dimensions', 'strokeWidth', 'strokeColor', 'borderRadius', 'color', 'shapeRendering'], + props: ['id', 'position', 'dimensions', 'strokeWidth', 'strokeColor', 'borderRadius', 'color', 'shapeRendering', 'type'], emits: ['click', 'dblclick', 'mouseenter', 'mousemove', 'mouseleave'], - setup(props: MiniMapNodeProps, { attrs, emit, slots }) { + setup(props: MiniMapNodeProps, { attrs, emit }) { const style = (attrs.style ?? {}) as CSSProperties + const miniMapSlots: Slots = inject(MiniMapSlots)! return () => [ h('rect', { @@ -29,7 +31,7 @@ export default defineComponent({ onMousemove: (e: MouseEvent) => emit('mousemove', e), onMouseleave: (e: MouseEvent) => emit('mouseleave', e), }), - slots?.default?.(), + miniMapSlots[props.type] && miniMapSlots[props.type]!(props), ] }, }) diff --git a/packages/additional-components/src/minimap/types.ts b/packages/additional-components/src/minimap/types.ts index 6263a5fd..0176cba5 100644 --- a/packages/additional-components/src/minimap/types.ts +++ b/packages/additional-components/src/minimap/types.ts @@ -1,5 +1,5 @@ import type { Dimensions, GraphNode, XYPosition } from '@vue-flow/core' -import type { CSSProperties } from 'vue' +import type { CSSProperties, InjectionKey, Slots } from 'vue' import type { PanelPosition } from '../panel' /** expects a node and returns a color value */ @@ -38,6 +38,7 @@ export interface MiniMapProps { /** these props are passed to mini map node slots */ export interface MiniMapNodeProps { id: string + type: string parentNode?: string selected?: boolean dragging?: boolean @@ -49,3 +50,5 @@ export interface MiniMapNodeProps { strokeColor?: string strokeWidth?: number } + +export const MiniMapSlots: InjectionKey = Symbol('MiniMapSlots')