UNPKG

plot-plan-designer

Version:

Design Editor Tools with React.js + ant.design + fabric.js

114 lines (113 loc) 4.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importStar(require("react")); const prop_types_1 = __importDefault(require("prop-types")); const react_ace_1 = __importDefault(require("react-ace")); const debounce_1 = __importDefault(require("lodash/debounce")); require("ace-builds/src-noconflict/mode-json"); require("ace-builds/src-noconflict/theme-github"); class InputJson extends react_1.Component { constructor() { super(...arguments); this.state = { text: this.props.value || '', }; this.onChange = (value) => { if (this.debouncedValidate) { this.debouncedValidate(); } this.setState({ text: value, }); }; this.onValidate = (annotations) => { if (annotations.length) { const errors = annotations .filter((annotation) => annotation.type === 'error') .map((annotation) => { return new Error(`${annotation.row}:${annotation.column} ${annotation.text} error`); }); this.debouncedValidate(errors); } }; } componentDidMount() { this.debouncedValidate = (0, debounce_1.default)((errors) => { const { onValidate } = this.props; if (onValidate) { onValidate(errors); } const { onChange } = this.props; if (onChange) { onChange(this.state.text); } }, 200); } UNSAFE_componentWillReceiveProps(nextProps) { if (nextProps.value !== this.props.value) { this.setState({ text: nextProps.value, }); } } render() { const { defaultValue, width, height, disabled } = this.props; const { text } = this.state; return (react_1.default.createElement(react_ace_1.default, { ref: (c) => { this.aceRef = c; }, mode: "json", theme: "github", width: width, height: height, defaultValue: defaultValue || text, value: text, editorProps: { $blockScrolling: true, }, onChange: this.onChange, onValidate: this.onValidate, readOnly: disabled })); } } InputJson.propTypes = { defaultValue: prop_types_1.default.string, value: prop_types_1.default.string, width: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number]), height: prop_types_1.default.oneOfType([prop_types_1.default.string, prop_types_1.default.number]), onChange: prop_types_1.default.func, onValidate: prop_types_1.default.func, disabled: prop_types_1.default.bool, }; InputJson.defaultProps = { width: '100%', height: '200px', disabled: false, }; exports.default = InputJson;