Merge pull request #729 from orangemug/fix/issue-712-v2

Correctly upgrade old-style filter functions to expressions
This commit is contained in:
pathmapper 2020-09-15 19:10:49 +02:00 committed by GitHub
commit 63b14933ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,8 +2,9 @@ import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { combiningFilterOps } from '../libs/filterops.js' import { combiningFilterOps } from '../libs/filterops.js'
import {mdiTableRowPlusAfter} from '@mdi/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 InputSelect from './InputSelect'
import Block from './Block' import Block from './Block'
import SingleFilterEditor from './SingleFilterEditor' import SingleFilterEditor from './SingleFilterEditor'
@ -61,24 +62,19 @@ function createStyleFromFilter (filter) {
}; };
} }
/** const FILTER_OPS = [
* This is doing way more work than we need it to, however validating a whole "all",
* style if the only thing that's exported from mapbox-gl-style-spec at the "any",
* moment. Not really an issue though as it take ~0.1ms to calculate. "none"
*/ ];
// If we convert a filter that is an expression to an expression it'll remain the same in value
function checkIfSimpleFilter (filter) { function checkIfSimpleFilter (filter) {
if (!filter || !combiningFilterOps.includes(filter[0])) { if (filter.length === 1 && FILTER_OPS.includes(filter[0])) {
return false; return true;
} }
const expression = convertFilter(filter);
// Because "none" isn't supported by the next expression syntax we can test return !isEqual(expression, filter);
// 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);
} }
function hasCombiningFilter(filter) { function hasCombiningFilter(filter) {