Skip to content

Commit

Permalink
More migrated components
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Dec 22, 2023
1 parent 6af3eee commit 6c9f0fe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import React from 'react'
import PropTypes from 'prop-types'

import SpecProperty from './_SpecProperty'
import DataProperty from './_DataProperty'
import DataProperty, { Stop } from './_DataProperty'
import ZoomProperty from './_ZoomProperty'
import ExpressionProperty from './_ExpressionProperty'
import {function as styleFunction} from '@maplibre/maplibre-gl-style-spec';
import {findDefaultFromSpec} from '../util/spec-helper';


function isLiteralExpression (value) {
function isLiteralExpression(value: any) {
return (Array.isArray(value) && value.length === 2 && value[0] === "literal");
}

function isGetExpression (value) {
function isGetExpression(value: any) {
return (
Array.isArray(value) &&
value.length === 2 &&
value[0] === "get"
);
}

function isZoomField(value) {
function isZoomField(value: any) {
return (
typeof(value) === 'object' &&
value.stops &&
typeof(value.property) === 'undefined' &&
Array.isArray(value.stops) &&
value.stops.length > 1 &&
value.stops.every(stop => {
value.stops.every((stop: Stop) => {
return (
Array.isArray(stop) &&
stop.length === 2
Expand All @@ -37,22 +36,22 @@ function isZoomField(value) {
);
}

function isIdentityProperty (value) {
function isIdentityProperty(value: any) {
return (
typeof(value) === 'object' &&
value.type === "identity" &&
value.hasOwnProperty("property")
);
}

function isDataStopProperty (value) {
function isDataStopProperty(value: any) {
return (
typeof(value) === 'object' &&
value.stops &&
typeof(value.property) !== 'undefined' &&
value.stops.length > 1 &&
Array.isArray(value.stops) &&
value.stops.every(stop => {
value.stops.every((stop: Stop) => {
return (
Array.isArray(stop) &&
stop.length === 2 &&
Expand All @@ -62,26 +61,26 @@ function isDataStopProperty (value) {
);
}

function isDataField(value) {
function isDataField(value: any) {
return (
isIdentityProperty(value) ||
isDataStopProperty(value)
);
}

function isPrimative (value) {
function isPrimative(value: any): value is string | boolean | number {
const valid = ["string", "boolean", "number"];
return valid.includes(typeof(value));
}

function isArrayOfPrimatives (values) {
function isArrayOfPrimatives(values: any): values is Array<string | boolean | number> {
if (Array.isArray(values)) {
return values.every(isPrimative);
}
return false;
}

function getDataType (value, fieldSpec={}) {
function getDataType(value: any, fieldSpec={} as any) {
if (value === undefined) {
return "value";
}
Expand All @@ -103,35 +102,33 @@ function getDataType (value, fieldSpec={}) {
}


type FieldFunctionProps = {
onChange(fieldName: string, value: any): unknown
fieldName: string
fieldType: string
fieldSpec: any
errors?: unknown[]
value?: any
};

type FieldFunctionState = {
dataType: string
isEditing: boolean
}

/** Supports displaying spec field for zoom function objects
* https://www.mapbox.com/mapbox-gl-style-spec/#types-function-zoom-property
*/
export default class FieldFunction extends React.Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
fieldName: PropTypes.string.isRequired,
fieldType: PropTypes.string.isRequired,
fieldSpec: PropTypes.object.isRequired,
errors: PropTypes.object,

value: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.number,
PropTypes.bool,
PropTypes.array
]),
}

constructor (props) {
super();
export default class FieldFunction extends React.Component<FieldFunctionProps, FieldFunctionState> {
constructor (props: FieldFunctionProps) {
super(props);
this.state = {
dataType: getDataType(props.value, props.fieldSpec),
isEditing: false,
}
}

static getDerivedStateFromProps(props, state) {
static getDerivedStateFromProps(props: FieldFunctionProps, state: FieldFunctionState) {
// Because otherwise when editing values we end up accidentally changing field type.
if (state.isEditing) {
return {};
Expand All @@ -144,7 +141,7 @@ export default class FieldFunction extends React.Component {
}
}

getFieldFunctionType(fieldSpec) {
getFieldFunctionType(fieldSpec: any) {
if (fieldSpec.expression.interpolated) {
return "exponential"
}
Expand Down Expand Up @@ -183,7 +180,7 @@ export default class FieldFunction extends React.Component {
});
}

deleteStop = (stopIdx) => {
deleteStop = (stopIdx: number) => {
const stops = this.props.value.stops.slice(0)
stops.splice(stopIdx, 1)

Expand All @@ -207,7 +204,7 @@ export default class FieldFunction extends React.Component {
if (value.stops) {
zoomFunc = {
base: value.base,
stops: value.stops.map(stop => {
stops: value.stops.map((stop: Stop) => {
return [stop[0].zoom, stop[1] || findDefaultFromSpec(this.props.fieldSpec)];
})
}
Expand Down Expand Up @@ -292,7 +289,7 @@ export default class FieldFunction extends React.Component {
property: "",
type: functionType,
base: value.base,
stops: value.stops.map(stop => {
stops: value.stops.map((stop: Stop) => {
return [{zoom: stop[0], value: stopValue}, stop[1] || findDefaultFromSpec(this.props.fieldSpec)];
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react'
import PropTypes from 'prop-types'

import FieldFunction from './FieldFunction'
import { LayerSpecification } from '@maplibre/maplibre-gl-style-spec'
const iconProperties = ['background-pattern', 'fill-pattern', 'line-pattern', 'fill-extrusion-pattern', 'icon-image']

/** Extract field spec by {@fieldName} from the {@layerType} in the
* style specification from either the paint or layout group */
function getFieldSpec(spec, layerType, fieldName) {
function getFieldSpec(spec: any, layerType: LayerSpecification["type"], fieldName: string) {
const groupName = getGroupName(spec, layerType, fieldName)
const group = spec[groupName + '_' + layerType]
const fieldSpec = group[fieldName]
Expand All @@ -25,7 +25,7 @@ function getFieldSpec(spec, layerType, fieldName) {
return fieldSpec
}

function getGroupName(spec, layerType, fieldName) {
function getGroupName(spec: any, layerType: LayerSpecification["type"], fieldName: string) {
const paint = spec['paint_' + layerType] || {}
if (fieldName in paint) {
return 'paint'
Expand All @@ -34,18 +34,18 @@ function getGroupName(spec, layerType, fieldName) {
}
}

export default class PropertyGroup extends React.Component {
static propTypes = {
layer: PropTypes.object.isRequired,
groupFields: PropTypes.array.isRequired,
onChange: PropTypes.func.isRequired,
spec: PropTypes.object.isRequired,
errors: PropTypes.object,
}
type PropertyGroupProps = {
layer: LayerSpecification
groupFields: string[]
onChange(...args: unknown[]): unknown
spec: any
errors?: unknown[]
};

onPropertyChange = (property, newValue) => {
export default class PropertyGroup extends React.Component<PropertyGroupProps> {
onPropertyChange = (property: string, newValue: any) => {
const group = getGroupName(this.props.spec, this.props.layer.type, property)
this.props.onChange(group , property, newValue)
this.props.onChange(group ,property, newValue)
}

render() {
Expand All @@ -55,7 +55,9 @@ export default class PropertyGroup extends React.Component {

const paint = this.props.layer.paint || {}
const layout = this.props.layer.layout || {}
const fieldValue = fieldName in paint ? paint[fieldName] : layout[fieldName]
const fieldValue = fieldName in paint
? paint[fieldName as keyof typeof paint]
: layout[fieldName as keyof typeof layout]
const fieldType = fieldName in paint ? 'paint' : 'layout';

return <FieldFunction
Expand Down
2 changes: 1 addition & 1 deletion src/components/SpecField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const typeMap = {
formatted: () => Block,
};

type SpecFieldProps = InputFieldSpecProps & {
export type SpecFieldProps = InputFieldSpecProps & {
name?: string
};

Expand Down
8 changes: 4 additions & 4 deletions src/components/_DataProperty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ type DataPropertyValue = {
property?: string
base?: number
type?: string
stops: Stops
stops: Stop[]
}

type Stops = [{
export type Stop = [{
zoom: number
value: number
}, number][]
}, number]

export default class DataProperty extends React.Component<DataPropertyProps, DataPropertyState> {
state = {
Expand Down Expand Up @@ -110,7 +110,7 @@ export default class DataProperty extends React.Component<DataPropertyProps, Dat
}

// Order the stops altering the refs to reflect their new position.
orderStopsByZoom(stops: Stops) {
orderStopsByZoom(stops: Stop[]) {
const mappedWithRef = stops
.map((stop, idx) => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/components/_SpecProperty.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react'

import SpecField from './SpecField'
import SpecField, {SpecFieldProps} from './SpecField'
import FunctionButtons from './_FunctionButtons'

import labelFromFieldName from './_labelFromFieldName'


type SpecPropertyProps = {
type SpecPropertyProps = SpecFieldProps & {
onZoomClick(...args: unknown[]): unknown
onDataClick(...args: unknown[]): unknown
fieldName?: string
Expand Down

0 comments on commit 6c9f0fe

Please sign in to comment.