From af4e6fa36ddeb77524498ee52fa8a2499780dfe5 Mon Sep 17 00:00:00 2001 From: hfutsora <346762712@qq.com> Date: Mon, 25 Dec 2023 14:52:25 +0800 Subject: [PATCH] feat: element position block --- src/SaForm/PcForm/layout/workspace/index.tsx | 2 + .../layout/workspace/position/index.tsx | 75 +++++++++++++++++++ src/SaForm/graph.ts | 1 + src/SaForm/utils/element.ts | 7 +- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/SaForm/PcForm/layout/workspace/position/index.tsx diff --git a/src/SaForm/PcForm/layout/workspace/index.tsx b/src/SaForm/PcForm/layout/workspace/index.tsx index 8b5ed92..0f8c13c 100644 --- a/src/SaForm/PcForm/layout/workspace/index.tsx +++ b/src/SaForm/PcForm/layout/workspace/index.tsx @@ -8,6 +8,7 @@ import Snapline from './snapline' import Group from './group' import Selection from './selection' import Contextmenu from './contextmenu' +import Position from './position' import type { PcGraph } from '../../graph' import type { CSSProperties, PropType } from 'vue' @@ -126,6 +127,7 @@ linear-gradient(90deg, ${boldLineColor} 1px, transparent 0)`, {props.graph.selection.enabled && ( )} + diff --git a/src/SaForm/PcForm/layout/workspace/position/index.tsx b/src/SaForm/PcForm/layout/workspace/position/index.tsx new file mode 100644 index 0000000..2bc6b70 --- /dev/null +++ b/src/SaForm/PcForm/layout/workspace/position/index.tsx @@ -0,0 +1,75 @@ +import { computed, defineComponent, ref } from 'vue' +import { useElementSize } from '@vueuse/core' +import { use } from 'sa-lambda' +import { getRectangle } from '../graph/renderer/utils/rectangle' +import type { CSSProperties, PropType } from 'vue' +import type { BasicGraph } from '@/SaForm/graph' +import { getDeepOffset } from '@/SaForm/utils/element' +import { addUnit } from '@/SaForm/utils/style' + +export default defineComponent({ + name: 'SaFormPositionBlock', + props: { + graph: { + required: true, + type: Object as PropType, + }, + }, + setup(props) { + const rect = computed(() => getRectangle(props.graph.selected)) + + const blockRef = ref() + const { width } = useElementSize(blockRef) + + const blockStyle = computed(() => { + if (props.graph.isDragging) { + return { + left: addUnit( + use(props.graph.selected[0]?.parent, (p) => + p ? getDeepOffset(p, 'x') : 0 + ) + + rect.value.x + + rect.value.width / 2 - + (width.value + 16) / 2 + ), + top: addUnit( + use(props.graph.selected[0]?.parent, (p) => + p ? getDeepOffset(p, 'y') : 0 + ) + + rect.value.y + + rect.value.height + + 10 + ), + } + } + + return { + display: 'none', + } + }) + + return () => ( +
+ X: {rect.value.x} Y: {rect.value.y} +
+ ) + }, +}) diff --git a/src/SaForm/graph.ts b/src/SaForm/graph.ts index c73007b..1d24bf5 100644 --- a/src/SaForm/graph.ts +++ b/src/SaForm/graph.ts @@ -90,6 +90,7 @@ export interface BasicGraph { selection: SelectionSetting isDraft: boolean + isDragging?: boolean grid: GridSetting snapline: SnaplineSetting scroller: Scroller diff --git a/src/SaForm/utils/element.ts b/src/SaForm/utils/element.ts index bd5f5f4..0b698dd 100644 --- a/src/SaForm/utils/element.ts +++ b/src/SaForm/utils/element.ts @@ -55,7 +55,12 @@ export const getDeepOffset = ( ): number => (element.parent ? getDeepOffset(element.parent, attr) : 0) + (attr === 'y' && isTab(element) ? element.attrs['tab-height'] : 0) + - element.attrs[attr] + element.attrs[attr] + + (element.attrs['border-width'] && + element.attrs['border-style'] && + element.attrs['border-style'] !== 'none' + ? element.attrs['border-width'] + : 0) export const createCopyName = (set: Set, name: string): string => { if (!name) return ''