Skip to content

Commit

Permalink
fix(VisibilityToolbar): Apply visibility when viewer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
floryst committed May 11, 2018
1 parent f235d8f commit 9547d02
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/widgets/VisibilityToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ export default class VisibilityToolbar extends React.Component {
};

this.containerEl = null;
this.subscribes = [];

this.toggleDropdown = this.toggleDropdown.bind(this);
this.onDocMouseDown = this.onDocMouseDown.bind(this);
}

componentDidMount() {
document.addEventListener('mousedown', this.onDocMouseDown, true);
this.subscribeToViewer(this.props.viewer);
}

componentDidUpdate(prevProps) {
if (prevProps.viewer !== this.props.viewer) {
this.subscribeToViewer(this.props.viewer);
}
}

componentWillUnmount() {
document.removeEventListener('mousedown', this.onDocMouseDown, true);
this.subscribeToViewer(null);
}

onDocMouseDown(ev) {
Expand All @@ -38,6 +47,15 @@ export default class VisibilityToolbar extends React.Component {
}
}

subscribeToViewer(viewer) {
while (this.subscribes.length) {
this.subscribes.pop().unsubscribe();
}
if (viewer) {
this.subscribes = [viewer.onModified(viewer.applyVisibility)];
}
}

toggleDropdown(ev) {
this.setState(({ dropdownVisible }) => ({
dropdownVisible: !dropdownVisible,
Expand Down

0 comments on commit 9547d02

Please sign in to comment.