Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support virtual render with scroller plugin #4243

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions packages/x6-plugin-scroller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ export class Scroller
this.setup()
this.startListening()
this.updateClassName()
this.scrollerImpl.init()
this.scrollerImpl.center()
}

// #region api

resize(width?: number, height?: number) {
this.scrollerImpl.resize(width, height)
const size = {
width: this.scrollerImpl.options.width,
height: this.scrollerImpl.options.height,
}
this.trigger('resize', size)
this.graph.trigger('resize', size)
}

resizePage(width?: number, height?: number) {
Expand Down Expand Up @@ -105,8 +112,15 @@ export class Scroller
return this
}

center(optons?: ScrollerImpl.CenterOptions) {
return this.centerPoint(optons)
/**
* Returns the untransformed size and origin of the current viewport.
*/
getVisibleArea() {
return this.scrollerImpl.getVisibleArea()
}

center(options?: ScrollerImpl.CenterOptions) {
return this.centerPoint(options)
}

centerPoint(
Expand All @@ -119,7 +133,7 @@ export class Scroller
y: number,
options?: ScrollerImpl.CenterOptions,
): this
centerPoint(optons?: ScrollerImpl.CenterOptions): this
centerPoint(options?: ScrollerImpl.CenterOptions): this
centerPoint(
x?: number | null | ScrollerImpl.CenterOptions,
y?: number | null,
Expand Down Expand Up @@ -336,6 +350,10 @@ export class Scroller
this.onRightMouseDown,
)
}
this.onScroll = this.onScroll.bind(this)
this.scrollerImpl.container.addEventListener('scroll', this.onScroll, {
passive: true,
})
}

protected stopListening() {
Expand All @@ -358,6 +376,7 @@ export class Scroller
this.onRightMouseDown,
)
}
this.scrollerImpl.container.removeEventListener('scroll', this.onScroll)
}

protected onRightMouseDown(e: Dom.MouseDownEvent) {
Expand All @@ -368,6 +387,12 @@ export class Scroller
}
}

protected onScroll(e: Event) {
const args = { e }
this.trigger('scroll', args)
this.graph.trigger('scroller:scroll', args)
}

protected preparePanning({ e }: { e: Dom.MouseDownEvent }) {
const allowPanning = this.allowPanning(e, true)
const selection = this.graph.getPlugin<any>('selection')
Expand Down
14 changes: 7 additions & 7 deletions packages/x6-plugin-scroller/src/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ export class ScrollerImpl extends View<ScrollerImpl.EventArgs> {
Dom.append(this.content, graphContainer)
Dom.appendTo(this.content, this.container)

this.startListening()
this.backgroundManager = new ScrollerImpl.Background(this)
}

init() {
if (!this.options.pageVisible) {
this.graph.grid.update()
}

this.backgroundManager = new ScrollerImpl.Background(this)

if (!this.options.autoResize) {
this.update()
}
this.startListening()
}

protected startListening() {
Expand Down Expand Up @@ -490,8 +490,8 @@ export class ScrollerImpl extends View<ScrollerImpl.EventArgs> {
/**
* Position the center of graph to the center of the viewport.
*/
center(optons?: ScrollerImpl.CenterOptions) {
return this.centerPoint(optons)
center(options?: ScrollerImpl.CenterOptions) {
return this.centerPoint(options)
}

/**
Expand All @@ -510,7 +510,7 @@ export class ScrollerImpl extends View<ScrollerImpl.EventArgs> {
y: number,
options?: ScrollerImpl.CenterOptions,
): this
centerPoint(optons?: ScrollerImpl.CenterOptions): this
centerPoint(options?: ScrollerImpl.CenterOptions): this
centerPoint(
x?: number | null | ScrollerImpl.CenterOptions,
y?: number | null,
Expand Down
9 changes: 2 additions & 7 deletions packages/x6/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,7 @@ export class Graph extends Basecoat<EventArgs> {
}

resize(width?: number, height?: number) {
const scroller = this.getPlugin<any>('scroller')
if (scroller) {
scroller.resize(width, height)
} else {
this.transform.resize(width, height)
}
this.size.resize(width, height)
return this
}

Expand Down Expand Up @@ -722,7 +717,7 @@ export class Graph extends Basecoat<EventArgs> {
y: number,
options?: Transform.CenterOptions,
): this
centerPoint(optons?: Transform.CenterOptions): this
centerPoint(options?: Transform.CenterOptions): this
centerPoint(
x?: number | null | Transform.CenterOptions,
y?: number | null,
Expand Down
17 changes: 13 additions & 4 deletions packages/x6/src/graph/virtual-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { Base } from './base'

export class VirtualRenderManager extends Base {
protected init() {
this.resetRenderArea = FunctionExt.throttle(this.resetRenderArea, 200, {
leading: true,
})
this.resetRenderArea = FunctionExt.throttle(
FunctionExt.throttle(this.resetRenderArea, 200, {
leading: true,
}),
1,
{ leading: false },
)
this.resetRenderArea()
this.startListening()
}
Expand All @@ -14,12 +18,14 @@ export class VirtualRenderManager extends Base {
this.graph.on('translate', this.resetRenderArea, this)
this.graph.on('scale', this.resetRenderArea, this)
this.graph.on('resize', this.resetRenderArea, this)
this.graph.on('scroller:scroll', this.resetRenderArea, this)
}

protected stopListening() {
this.graph.off('translate', this.resetRenderArea, this)
this.graph.off('scale', this.resetRenderArea, this)
this.graph.off('resize', this.resetRenderArea, this)
this.graph.off('scroller:scroll', this.resetRenderArea, this)
}

enableVirtualRender() {
Expand All @@ -34,7 +40,10 @@ export class VirtualRenderManager extends Base {

resetRenderArea() {
if (this.options.virtual) {
const renderArea = this.graph.getGraphArea()
const scroller = this.graph.getPlugin<any>('scroller')
const renderArea = scroller
? scroller.getVisibleArea()
: this.graph.getGraphArea()
this.graph.renderer.setRenderArea(renderArea)
}
}
Expand Down
Loading