From ae3f79f4ad173347b54293d58f499f79a5f30dde Mon Sep 17 00:00:00 2001 From: orangemug Date: Wed, 9 Sep 2020 15:55:20 +0100 Subject: [PATCH 1/2] Attempt to correctly upgrade old-style filter functions to expressions. --- src/components/FilterEditor.jsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/components/FilterEditor.jsx b/src/components/FilterEditor.jsx index d99d8a9..77f9b0d 100644 --- a/src/components/FilterEditor.jsx +++ b/src/components/FilterEditor.jsx @@ -2,8 +2,9 @@ import React from 'react' import PropTypes from 'prop-types' import { combiningFilterOps } from '../libs/filterops.js' import {mdiTableRowPlusAfter} from '@mdi/js'; +import {isEqual} from 'lodash'; -import {latest, validate, migrate} from '@mapbox/mapbox-gl-style-spec' +import {latest, validate, migrate, convertFilter} from '@mapbox/mapbox-gl-style-spec' import InputSelect from './InputSelect' import Block from './Block' import SingleFilterEditor from './SingleFilterEditor' @@ -155,7 +156,11 @@ export default class FilterEditor extends React.Component { static getDerivedStateFromProps (props, currentState) { const {filter} = props; - const displaySimpleFilter = checkIfSimpleFilter(combiningFilter(props)); + const tempFilter = combiningFilter(props); + const expression = convertFilter(tempFilter); + + // If we convert a filter that is an expression to an expression it'll remain the same in value + const displaySimpleFilter = !isEqual(expression, tempFilter); // Upgrade but never downgrade if (!displaySimpleFilter && currentState.displaySimpleFilter === true) { From bb4320088762f43bf31c3e74c5647a0c900a381b Mon Sep 17 00:00:00 2001 From: orangemug Date: Wed, 9 Sep 2020 16:12:18 +0100 Subject: [PATCH 2/2] Move logic into checkIfSimpleFilter function and added FILTER_OPS check. --- src/components/FilterEditor.jsx | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/components/FilterEditor.jsx b/src/components/FilterEditor.jsx index 77f9b0d..19c86a0 100644 --- a/src/components/FilterEditor.jsx +++ b/src/components/FilterEditor.jsx @@ -62,24 +62,19 @@ function createStyleFromFilter (filter) { }; } -/** - * This is doing way more work than we need it to, however validating a whole - * style if the only thing that's exported from mapbox-gl-style-spec at the - * moment. Not really an issue though as it take ~0.1ms to calculate. - */ +const FILTER_OPS = [ + "all", + "any", + "none" +]; + +// If we convert a filter that is an expression to an expression it'll remain the same in value function checkIfSimpleFilter (filter) { - if (!filter || !combiningFilterOps.includes(filter[0])) { - return false; + if (filter.length === 1 && FILTER_OPS.includes(filter[0])) { + return true; } - - // Because "none" isn't supported by the next expression syntax we can test - // with ["none", ...] because it'll return false if it's a new style - // expression. - const moddedFilter = ["none", ...filter.slice(1)]; - const tmpStyle = createStyleFromFilter(moddedFilter) - - const errors = validate(tmpStyle); - return (errors.length < 1); + const expression = convertFilter(filter); + return !isEqual(expression, filter); } function hasCombiningFilter(filter) { @@ -156,11 +151,7 @@ export default class FilterEditor extends React.Component { static getDerivedStateFromProps (props, currentState) { const {filter} = props; - const tempFilter = combiningFilter(props); - const expression = convertFilter(tempFilter); - - // If we convert a filter that is an expression to an expression it'll remain the same in value - const displaySimpleFilter = !isEqual(expression, tempFilter); + const displaySimpleFilter = checkIfSimpleFilter(combiningFilter(props)); // Upgrade but never downgrade if (!displaySimpleFilter && currentState.displaySimpleFilter === true) {