mirror of
https://github.com/maputnik/editor.git
synced 2026-08-24 05:57:25 +00:00
Initial commit to upgrade code mirror
This commit is contained in:
@@ -1,33 +1,28 @@
|
||||
import React from "react";
|
||||
import {MdDelete, MdUndo} from "react-icons/md";
|
||||
import stringifyPretty from "json-stringify-pretty-compact";
|
||||
import { type WithTranslation, withTranslation } from "react-i18next";
|
||||
|
||||
import Block from "./Block";
|
||||
import InputButton from "./InputButton";
|
||||
import labelFromFieldName from "../libs/label-from-field-name";
|
||||
import FieldJson from "./FieldJson";
|
||||
import type { StylePropertySpecification } from "maplibre-gl";
|
||||
|
||||
|
||||
type ExpressionPropertyInternalProps = {
|
||||
onDelete?(...args: unknown[]): unknown
|
||||
fieldName: string
|
||||
fieldType?: string
|
||||
fieldSpec?: object
|
||||
fieldSpec?: StylePropertySpecification
|
||||
value?: any
|
||||
errors?: {[key: string]: {message: string}}
|
||||
onChange?(...args: unknown[]): unknown
|
||||
onDelete?(...args: unknown[]): unknown
|
||||
onChange(value: object): void
|
||||
onUndo?(...args: unknown[]): unknown
|
||||
canUndo?(...args: unknown[]): unknown
|
||||
onFocus?(...args: unknown[]): unknown
|
||||
onBlur?(...args: unknown[]): unknown
|
||||
} & WithTranslation;
|
||||
|
||||
type ExpressionPropertyState = {
|
||||
jsonError: boolean
|
||||
};
|
||||
|
||||
class ExpressionPropertyInternal extends React.Component<ExpressionPropertyInternalProps, ExpressionPropertyState> {
|
||||
class ExpressionPropertyInternal extends React.Component<ExpressionPropertyInternalProps> {
|
||||
static defaultProps = {
|
||||
errors: {},
|
||||
onFocus: () => {},
|
||||
@@ -41,21 +36,8 @@ class ExpressionPropertyInternal extends React.Component<ExpressionPropertyInter
|
||||
};
|
||||
}
|
||||
|
||||
onJSONInvalid = (_err: Error) => {
|
||||
this.setState({
|
||||
jsonError: true,
|
||||
});
|
||||
};
|
||||
|
||||
onJSONValid = () => {
|
||||
this.setState({
|
||||
jsonError: false,
|
||||
});
|
||||
};
|
||||
|
||||
render() {
|
||||
const {t, errors, fieldName, fieldType, value, canUndo} = this.props;
|
||||
const {jsonError} = this.state;
|
||||
const {t, value, canUndo} = this.props;
|
||||
const undoDisabled = canUndo ? !canUndo() : true;
|
||||
|
||||
const deleteStopBtn = (
|
||||
@@ -82,57 +64,22 @@ class ExpressionPropertyInternal extends React.Component<ExpressionPropertyInter
|
||||
</>
|
||||
);
|
||||
|
||||
const fieldKey = fieldType === undefined ? fieldName : `${fieldType}.${fieldName}`;
|
||||
|
||||
const fieldError = errors![fieldKey];
|
||||
const errorKeyStart = `${fieldKey}[`;
|
||||
const foundErrors = [];
|
||||
|
||||
function getValue(data: any) {
|
||||
return stringifyPretty(data, {indent: 2, maxLength: 38});
|
||||
}
|
||||
|
||||
if (jsonError) {
|
||||
foundErrors.push({message: "Invalid JSON"});
|
||||
}
|
||||
else {
|
||||
Object.entries(errors!)
|
||||
.filter(([key, _error]) => {
|
||||
return key.startsWith(errorKeyStart);
|
||||
})
|
||||
.forEach(([_key, error]) => {
|
||||
return foundErrors.push(error);
|
||||
});
|
||||
|
||||
if (fieldError) {
|
||||
foundErrors.push(fieldError);
|
||||
}
|
||||
}
|
||||
|
||||
return <Block
|
||||
// this feels like an incorrect type...? `foundErrors` is an array of objects, not a single object
|
||||
error={foundErrors as any}
|
||||
fieldSpec={this.props.fieldSpec}
|
||||
label={t(labelFromFieldName(this.props.fieldName))}
|
||||
action={deleteStopBtn}
|
||||
wideMode={true}
|
||||
>
|
||||
<FieldJson
|
||||
mode={{name: "mgl"}}
|
||||
lint={{
|
||||
context: "expression",
|
||||
spec: this.props.fieldSpec,
|
||||
}}
|
||||
lintType="expression"
|
||||
spec={this.props.fieldSpec}
|
||||
className="maputnik-expression-editor"
|
||||
onFocus={this.props.onFocus}
|
||||
onBlur={this.props.onBlur}
|
||||
onJSONInvalid={this.onJSONInvalid}
|
||||
onJSONValid={this.onJSONValid}
|
||||
layer={value}
|
||||
value={value}
|
||||
lineNumbers={false}
|
||||
maxHeight={200}
|
||||
lineWrapping={true}
|
||||
getValue={getValue}
|
||||
onChange={this.props.onChange}
|
||||
/>
|
||||
</Block>;
|
||||
|
||||
Reference in New Issue
Block a user