Skip to content

Commit

Permalink
feat: element position block
Browse files Browse the repository at this point in the history
  • Loading branch information
Hfutsora committed Dec 25, 2023
1 parent ba274ea commit af4e6fa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/SaForm/PcForm/layout/workspace/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -126,6 +127,7 @@ linear-gradient(90deg, ${boldLineColor} 1px, transparent 0)`,
{props.graph.selection.enabled && (
<Selection graph={props.graph} />
)}
<Position graph={props.graph} />
</div>
</AutoScale>

Expand Down
75 changes: 75 additions & 0 deletions src/SaForm/PcForm/layout/workspace/position/index.tsx
Original file line number Diff line number Diff line change
@@ -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<BasicGraph>,
},
},
setup(props) {
const rect = computed(() => getRectangle(props.graph.selected))

const blockRef = ref()
const { width } = useElementSize(blockRef)

const blockStyle = computed<CSSProperties>(() => {
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 () => (
<div
ref={blockRef}
class="position-block"
style={{
position: 'absolute',
backgroundColor: 'var(--c-bg)',
display: 'flex',
alignItems: 'center',
borderRadius: '4px',
fontSize: '12px',
zIndex: 100,
'pointer-events': 'none',
width: 'fit-content',
padding: '2px 8px',
boxShadow: '0 2px 4px var(--c-divider)',
boxSizing: 'border-box',
...blockStyle.value,
}}
>
X: {rect.value.x} <span class="ml-2">Y: {rect.value.y}</span>
</div>
)
},
})
1 change: 1 addition & 0 deletions src/SaForm/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface BasicGraph {
selection: SelectionSetting

isDraft: boolean
isDragging?: boolean
grid: GridSetting
snapline: SnaplineSetting
scroller: Scroller
Expand Down
7 changes: 6 additions & 1 deletion src/SaForm/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, name: string): string => {
if (!name) return ''
Expand Down

0 comments on commit af4e6fa

Please sign in to comment.