Skip to content

Commit

Permalink
feat: Sync newly added panes (okonet#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyabo authored and okonet committed Mar 23, 2018
1 parent 4e7b516 commit 5494e52
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/ScrollSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export default class ScrollSync extends Component {
}

if (!this.findPane(node, group)) {
if (this.panes[group].length > 0) {
this.syncScrollPosition(this.panes[group][0], node)
}
this.addEvents(node, group)
this.panes[group].push(node)
}
Expand Down Expand Up @@ -77,7 +80,7 @@ export default class ScrollSync extends Component {
})
}

syncScrollPositions = (scrolledPane, group) => {
syncScrollPosition(scrolledPane, pane) {
const {
scrollTop,
scrollHeight,
Expand All @@ -92,21 +95,25 @@ export default class ScrollSync extends Component {

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

/* Calculate the actual pane height */
const paneHeight = pane.scrollHeight - clientHeight
const paneWidth = pane.scrollWidth - clientWidth
/* Adjust the scrollTop position of it accordingly */
if (vertical && scrollTopOffset > 0) {
pane.scrollTop = proportional ? (paneHeight * scrollTop) / scrollTopOffset : scrollTop // eslint-disable-line
}
if (horizontal && scrollLeftOffset > 0) {
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / scrollLeftOffset : scrollLeft // eslint-disable-line
}
}

syncScrollPositions = (scrolledPane, group) => {
this.panes[group].forEach((pane) => {
/* For all panes beside the currently scrolling one */
if (scrolledPane !== pane) {
/* Remove event listeners from the node that we'll manipulate */
this.removeEvents(pane, group)
/* Calculate the actual pane height */
const paneHeight = pane.scrollHeight - clientHeight
const paneWidth = pane.scrollWidth - clientWidth
/* Adjust the scrollTop position of it accordingly */
if (vertical && scrollTopOffset > 0) {
pane.scrollTop = proportional ? (paneHeight * scrollTop) / scrollTopOffset : scrollTop // eslint-disable-line
}
if (horizontal && scrollLeftOffset > 0) {
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / scrollLeftOffset : scrollLeft // eslint-disable-line
}
this.syncScrollPosition(scrolledPane, pane)
/* Re-attach event listeners after we're done scrolling */
window.requestAnimationFrame(() => {
this.addEvents(pane, group)
Expand Down

0 comments on commit 5494e52

Please sign in to comment.