fix(node-resizer): enforce min/max width/height on nodes
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
<script lang="ts" setup>
|
||||
// todo: add plugin to emit resize events via vue flow store; requires plugin API to be added to core
|
||||
import { computed, inject, watch } from 'vue'
|
||||
import type { NodeDimensionChange } from '@vue-flow/core'
|
||||
import { NodeIdInjection, useVueFlow } from '@vue-flow/core'
|
||||
import ResizeControl from './ResizeControl.vue'
|
||||
import type { ControlLinePosition, ControlPosition, NodeResizerProps, OnResize, OnResizeStart } from './types'
|
||||
import { ResizeControlVariant } from './types'
|
||||
|
||||
withDefaults(defineProps<NodeResizerProps>(), {
|
||||
const props = withDefaults(defineProps<NodeResizerProps>(), {
|
||||
isVisible: true,
|
||||
})
|
||||
|
||||
@@ -14,9 +17,58 @@ const emits = defineEmits<{
|
||||
(event: 'resizeEnd', resizeEvent: OnResizeStart): void
|
||||
}>()
|
||||
|
||||
const { findNode, emits: triggerEmits } = useVueFlow()
|
||||
|
||||
const handleControls: ControlPosition[] = ['top-left', 'top-right', 'bottom-left', 'bottom-right']
|
||||
|
||||
const lineControls: ControlLinePosition[] = ['top', 'right', 'bottom', 'left']
|
||||
|
||||
const contextNodeId = inject(NodeIdInjection, null)
|
||||
|
||||
const id = computed(() => (typeof props.nodeId === 'string' ? props.nodeId : contextNodeId))
|
||||
|
||||
watch(
|
||||
[() => props.minWidth, () => props.minHeight, () => props.maxWidth, () => props.maxHeight],
|
||||
([minWidth, minHeight, maxWidth, maxHeight]) => {
|
||||
const node = findNode(id.value)
|
||||
|
||||
if (node) {
|
||||
const dimensionChange: NodeDimensionChange = {
|
||||
id: node.id,
|
||||
type: 'dimensions',
|
||||
updateStyle: true,
|
||||
dimensions: {
|
||||
width: node.dimensions.width,
|
||||
height: node.dimensions.height,
|
||||
},
|
||||
}
|
||||
|
||||
if (minWidth && node.dimensions.width < minWidth) {
|
||||
dimensionChange.dimensions!.width = minWidth
|
||||
}
|
||||
|
||||
if (minHeight && node.dimensions.height < minHeight) {
|
||||
dimensionChange.dimensions!.height = minHeight
|
||||
}
|
||||
|
||||
if (maxWidth && node.dimensions.width > maxWidth) {
|
||||
dimensionChange.dimensions!.width = maxWidth
|
||||
}
|
||||
|
||||
if (maxHeight && node.dimensions.height > maxHeight) {
|
||||
dimensionChange.dimensions!.height = maxHeight
|
||||
}
|
||||
|
||||
if (
|
||||
dimensionChange.dimensions!.width !== node.dimensions.width ||
|
||||
dimensionChange.dimensions!.height !== node.dimensions.height
|
||||
) {
|
||||
triggerEmits.nodesChange([dimensionChange])
|
||||
}
|
||||
}
|
||||
},
|
||||
{ flush: 'post', immediate: true },
|
||||
)
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -54,7 +106,7 @@ export default {
|
||||
:key="c"
|
||||
:class="handleClassName"
|
||||
:style="handleStyle"
|
||||
:node-id="nodeId"
|
||||
:node-id="id"
|
||||
:position="c"
|
||||
:color="color"
|
||||
:min-width="minWidth"
|
||||
|
||||
Reference in New Issue
Block a user