Some perf improvements when updating the layers list, also throttled color picker.

This commit is contained in:
orangemug 2019-10-12 10:10:33 +01:00
parent 935dfa1704
commit 7fa17d81ac
3 changed files with 57 additions and 1 deletions

View file

@ -29,6 +29,7 @@
"detect-browser": "^4.5.0",
"file-saver": "^1.3.8",
"jsonlint": "github:josdejong/jsonlint#85a19d7",
"lodash": "^4.17.15",
"lodash.capitalize": "^4.2.1",
"lodash.clamp": "^4.0.3",
"lodash.clonedeep": "^4.5.0",

View file

@ -2,6 +2,7 @@ import React from 'react'
import Color from 'color'
import ChromePicker from 'react-color/lib/components/chrome/Chrome'
import PropTypes from 'prop-types'
import lodash from 'lodash';
function formatColor(color) {
const rgb = color.rgb
@ -23,6 +24,15 @@ class ColorField extends React.Component {
pickerOpened: false
}
constructor () {
super();
this.onChangeNoCheck = lodash.throttle(this.onChangeNoCheck, 1000/30);
}
onChangeNoCheck (v) {
this.props.onChange(formatColor(v));
}
//TODO: I much rather would do this with absolute positioning
//but I am too stupid to get it to work together with fixed position
//and scrollbars so I have to fallback to JavaScript
@ -82,7 +92,7 @@ class ColorField extends React.Component {
}}>
<ChromePicker
color={currentColor}
onChange={c => this.props.onChange(formatColor(c))}
onChange={c => this.onChangeNoCheck(formatColor(c))}
/>
<div
className="maputnik-color-picker-offset"

View file

@ -1,6 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import lodash from 'lodash';
import LayerListGroup from './LayerListGroup'
import LayerListItem from './LayerListItem'
@ -116,6 +117,50 @@ class LayerListContainer extends React.Component {
return collapsed === undefined ? true : collapsed
}
shouldComponentUpdate (nextProps, nextState) {
// Always update on state change
if (this.state !== nextState) {
return true;
}
// This component tree only requires id and visibility from the layers
// objects
function getRequiredProps (layer) {
const out = {
id: layer.id,
};
if (layer.layout) {
out.layout = {
visibility: layer.layout.visibility
};
}
return out;
}
const layersEqual = lodash.isEqual(
nextProps.layers.map(getRequiredProps),
this.props.layers.map(getRequiredProps),
);
function withoutLayers (props) {
const out = {
...props
};
delete out['layers'];
return out;
}
// Compare the props without layers because we've already compared them
// efficiently above.
const propsEqual = lodash.isEqual(
withoutLayers(this.props),
withoutLayers(nextProps)
);
const propsChanged = !(layersEqual && propsEqual);
return propsChanged;
}
render() {
const listItems = []