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

EZP-32248: Retain content tree state #1679

Merged
merged 3 commits into from
Dec 23, 2020
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export default class ContentTree extends Component {
this.addWidthChangeListener = this.addWidthChangeListener.bind(this);
this.handleResizeEnd = this.handleResizeEnd.bind(this);
this._refTreeContainer = React.createRef();
this.scrollTimeout = null;
this.scrollPositionSet = false;

this.state = {
resizeStartPositionX: 0,
containerWidth: 0,
containerWidth: this.getConfig('width'),
resizedContainerWidth: 0,
isResizing: false,
};
Expand All @@ -26,10 +28,46 @@ export default class ContentTree extends Component {
this.clearDocumentResizingListeners();
}

componentDidMount() {
this.containerSrollRef.addEventListener('scroll', (event) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.containerSrollRef.addEventListener('scroll', (event) => {
this.containerScrollRef.addEventListener('scroll', (event) => {

clearTimeout(this.scrollTimeout);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clearTimeout(this.scrollTimeout);
window.clearTimeout(this.scrollTimeout);

To show that this is from window object


this.scrollTimeout = setTimeout(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.scrollTimeout = setTimeout(
this.scrollTimeout = window.setTimeout(

(scrollTop) => {
this.saveConfig('scrollTop', scrollTop);
},
50,
event.currentTarget.scrollTop
);
});
}

componentDidUpdate(prevState) {
if (this.state.containerWidth !== prevState.containerWidth) {
this.saveConfig('width', this.state.containerWidth);

document.body.dispatchEvent(new CustomEvent('ez-content-tree-resized'));
}

if (this.props.items.length && !this.scrollPositionSet) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.props.items.length && !this.scrollPositionSet) {
if (this.props.items && this.props.items.length && !this.scrollPositionSet) {

?

this.scrollPositionSet = true;

this.containerSrollRef.scrollTo(0, this.getConfig('scrollTop'));
}
}

saveConfig(id, value) {
const data = JSON.parse(window.localStorage.getItem('ez-content-tree-config') || '{}');

data[id] = value;

window.localStorage.setItem('ez-content-tree-config', JSON.stringify(data));
}

getConfig(id) {
const data = JSON.parse(window.localStorage.getItem('ez-content-tree-config') || '{}');

return data[id];
}

changeContainerWidth({ clientX }) {
Expand Down Expand Up @@ -78,13 +116,8 @@ export default class ContentTree extends Component {
}

renderList() {
const { items } = this.props;

if (!items || !items.length) {
return;
}

const {
items,
loadMoreSubitems,
currentLocationId,
onClickItem,
Expand All @@ -108,8 +141,8 @@ export default class ContentTree extends Component {
};

return (
<div className="m-tree__scrollable-wrapper">
<List {...attrs} />
<div className="m-tree__scrollable-wrapper" ref={(ref) => (this.containerSrollRef = ref)}>
{!items || !items.length ? null : <List {...attrs} />}
</div>
);
}
Expand Down