Skip to content

Commit 5494e52

Browse files
ilyabookonet
authored andcommitted
feat: Sync newly added panes (#17)
1 parent 4e7b516 commit 5494e52

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/ScrollSync.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export default class ScrollSync extends Component {
4141
}
4242

4343
if (!this.findPane(node, group)) {
44+
if (this.panes[group].length > 0) {
45+
this.syncScrollPosition(this.panes[group][0], node)
46+
}
4447
this.addEvents(node, group)
4548
this.panes[group].push(node)
4649
}
@@ -77,7 +80,7 @@ export default class ScrollSync extends Component {
7780
})
7881
}
7982

80-
syncScrollPositions = (scrolledPane, group) => {
83+
syncScrollPosition(scrolledPane, pane) {
8184
const {
8285
scrollTop,
8386
scrollHeight,
@@ -92,21 +95,25 @@ export default class ScrollSync extends Component {
9295

9396
const { proportional, vertical, horizontal } = this.props
9497

98+
/* Calculate the actual pane height */
99+
const paneHeight = pane.scrollHeight - clientHeight
100+
const paneWidth = pane.scrollWidth - clientWidth
101+
/* Adjust the scrollTop position of it accordingly */
102+
if (vertical && scrollTopOffset > 0) {
103+
pane.scrollTop = proportional ? (paneHeight * scrollTop) / scrollTopOffset : scrollTop // eslint-disable-line
104+
}
105+
if (horizontal && scrollLeftOffset > 0) {
106+
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / scrollLeftOffset : scrollLeft // eslint-disable-line
107+
}
108+
}
109+
110+
syncScrollPositions = (scrolledPane, group) => {
95111
this.panes[group].forEach((pane) => {
96112
/* For all panes beside the currently scrolling one */
97113
if (scrolledPane !== pane) {
98114
/* Remove event listeners from the node that we'll manipulate */
99115
this.removeEvents(pane, group)
100-
/* Calculate the actual pane height */
101-
const paneHeight = pane.scrollHeight - clientHeight
102-
const paneWidth = pane.scrollWidth - clientWidth
103-
/* Adjust the scrollTop position of it accordingly */
104-
if (vertical && scrollTopOffset > 0) {
105-
pane.scrollTop = proportional ? (paneHeight * scrollTop) / scrollTopOffset : scrollTop // eslint-disable-line
106-
}
107-
if (horizontal && scrollLeftOffset > 0) {
108-
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / scrollLeftOffset : scrollLeft // eslint-disable-line
109-
}
116+
this.syncScrollPosition(scrolledPane, pane)
110117
/* Re-attach event listeners after we're done scrolling */
111118
window.requestAnimationFrame(() => {
112119
this.addEvents(pane, group)

0 commit comments

Comments
 (0)