-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
377 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/erd-editor/src/components/erd/virtual-scroll/VirtualScroll.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { css } from '@dineug/r-html'; | ||
|
||
export const vertical = css` | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
width: 8px; | ||
height: calc(100% - 8px); | ||
overflow: hidden; | ||
padding-top: 4px; | ||
`; | ||
|
||
export const horizontal = css` | ||
position: absolute; | ||
left: 0; | ||
bottom: 0; | ||
width: calc(100% - 8px); | ||
height: 8px; | ||
overflow: hidden; | ||
padding-left: 4px; | ||
`; | ||
|
||
export const ghostThumb = css` | ||
will-change: transform; | ||
cursor: pointer; | ||
&:hover > div { | ||
background-color: var(--scrollbar-thumb-hover); | ||
} | ||
&[data-selected] > div { | ||
background-color: var(--scrollbar-thumb-hover); | ||
} | ||
`; | ||
|
||
const thumb = css` | ||
background-color: var(--scrollbar-thumb); | ||
border-radius: 4px; | ||
`; | ||
|
||
export const verticalThumb = css` | ||
width: 4px; | ||
height: 100%; | ||
${thumb}; | ||
`; | ||
|
||
export const horizontalThumb = css` | ||
width: 100%; | ||
height: 4px; | ||
${thumb}; | ||
`; |
150 changes: 150 additions & 0 deletions
150
packages/erd-editor/src/components/erd/virtual-scroll/VirtualScroll.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { createRef, FC, html, ref } from '@dineug/r-html'; | ||
|
||
import { useAppContext } from '@/components/appContext'; | ||
import { scrollToAction } from '@/engine/modules/settings/atom.actions'; | ||
|
||
import { useVirtualScroll } from './useVirtualScroll'; | ||
import * as styles from './VirtualScroll.styles'; | ||
|
||
export type VirtualScrollProps = {}; | ||
|
||
const VirtualScroll: FC<VirtualScrollProps> = (props, ctx) => { | ||
const app = useAppContext(ctx); | ||
const { | ||
state, | ||
getWidthRatio, | ||
getHeightRatio, | ||
onScrollLeftStart, | ||
onScrollTopStart, | ||
} = useVirtualScroll(ctx); | ||
const horizontal = createRef<HTMLDivElement>(); | ||
const vertical = createRef<HTMLDivElement>(); | ||
|
||
const handleMoveLeft = (event: MouseEvent) => { | ||
const el = event.target as HTMLElement | null; | ||
if (!el) return; | ||
|
||
const canMove = !el.closest('.virtual-scroll-ghost-thumb'); | ||
if (!canMove) return; | ||
|
||
const { store } = app.value; | ||
const { | ||
editor: { viewport }, | ||
settings, | ||
} = store.state; | ||
const ratio = getWidthRatio(); | ||
const $horizontal = horizontal.value; | ||
const rect = $horizontal.getBoundingClientRect(); | ||
const clientX = event.clientX; | ||
|
||
const x = clientX - rect.x; | ||
const absoluteX = x / ratio; | ||
const scrollLeft = absoluteX - viewport.width / 2; | ||
|
||
store.dispatch( | ||
scrollToAction({ | ||
scrollLeft: -1 * scrollLeft, | ||
scrollTop: settings.scrollTop, | ||
}) | ||
); | ||
|
||
onScrollLeftStart(event); | ||
}; | ||
|
||
const handleMoveTop = (event: MouseEvent) => { | ||
const el = event.target as HTMLElement | null; | ||
if (!el) return; | ||
|
||
const canMove = !el.closest('.virtual-scroll-ghost-thumb'); | ||
if (!canMove) return; | ||
|
||
const { store } = app.value; | ||
const { | ||
editor: { viewport }, | ||
} = store.state; | ||
const ratio = getHeightRatio(); | ||
const $vertical = vertical.value; | ||
const rect = $vertical.getBoundingClientRect(); | ||
const clientY = event.clientY; | ||
|
||
const y = clientY - rect.y; | ||
const absoluteY = y / ratio; | ||
const scrollTop = absoluteY - viewport.height / 2; | ||
|
||
store.dispatch( | ||
scrollToAction({ | ||
scrollLeft: store.state.settings.scrollLeft, | ||
scrollTop: -1 * scrollTop, | ||
}) | ||
); | ||
|
||
onScrollTopStart(event); | ||
}; | ||
|
||
return () => { | ||
const { store } = app.value; | ||
const { | ||
editor: { viewport }, | ||
settings: { width, height, scrollLeft, scrollTop }, | ||
} = store.state; | ||
|
||
const wRatio = getWidthRatio(); | ||
const hRatio = getHeightRatio(); | ||
const w = viewport.width * wRatio; | ||
const h = viewport.height * hRatio; | ||
const left = -1 * scrollLeft * wRatio; | ||
const top = -1 * scrollTop * hRatio; | ||
|
||
const showHorizontal = viewport.width < width; | ||
const showVertical = viewport.height < height; | ||
|
||
return html` | ||
${showHorizontal | ||
? html` | ||
<div | ||
class=${['virtual-scroll', styles.horizontal]} | ||
${ref(horizontal)} | ||
@mousedown=${handleMoveLeft} | ||
> | ||
<div | ||
class=${['virtual-scroll-ghost-thumb', styles.ghostThumb]} | ||
style=${{ | ||
width: `${w}px`, | ||
height: '100%', | ||
transform: `translate(${left}px, 0px)`, | ||
}} | ||
?data-selected=${state.selected === 'horizontal'} | ||
@mousedown=${onScrollLeftStart} | ||
> | ||
<div class=${styles.horizontalThumb}></div> | ||
</div> | ||
</div> | ||
` | ||
: null} | ||
${showVertical | ||
? html` | ||
<div | ||
class=${['virtual-scroll', styles.vertical]} | ||
${ref(vertical)} | ||
@mousedown=${handleMoveTop} | ||
> | ||
<div | ||
class=${['virtual-scroll-ghost-thumb', styles.ghostThumb]} | ||
style=${{ | ||
width: '100%', | ||
height: `${h}px`, | ||
transform: `translate(0px, ${top}px)`, | ||
}} | ||
?data-selected=${state.selected === 'vertical'} | ||
@mousedown=${onScrollTopStart} | ||
> | ||
<div class=${styles.verticalThumb}></div> | ||
</div> | ||
</div> | ||
` | ||
: null} | ||
`; | ||
}; | ||
}; | ||
|
||
export default VirtualScroll; |
Oops, something went wrong.