plot-plan-designer
Version:
Design Editor Tools with React.js + ant.design + fabric.js
184 lines (183 loc) • 7.95 kB
JavaScript
"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 antd_1 = require("antd");
const debounce_1 = __importDefault(require("lodash/debounce"));
const react_ace_1 = __importDefault(require("react-ace"));
require("ace-builds/src-noconflict/mode-html");
require("ace-builds/src-noconflict/mode-css");
require("ace-builds/src-noconflict/mode-javascript");
require("ace-builds/src-noconflict/theme-github");
const AcePreview_1 = __importDefault(require("./AcePreview"));
const defaultStyle = {
padding: 12,
};
class AceEditor extends react_1.Component {
constructor() {
super(...arguments);
this.handlers = {
onChangeHTML: (0, debounce_1.default)((value) => {
this.setState({
html: value,
htmlAnnotations: this.htmlRef.editor.getSession().getAnnotations(),
}, () => {
if (this.props.onChangeHTML) {
this.props.onChangeHTML(value);
}
});
}, 500),
onChangeCSS: (0, debounce_1.default)((value) => {
this.setState({
css: value,
cssAnnotations: this.cssRef.editor.getSession().getAnnotations(),
}, () => {
if (this.props.onChangeCSS) {
this.props.onChangeCSS(value);
}
});
}, 500),
onChangeJS: (0, debounce_1.default)((value) => {
this.setState({
js: value,
jsAnnotations: this.jsRef.editor.getSession().getAnnotations(),
}, () => {
if (this.props.onChangeJS) {
this.props.onChangeJS(value);
}
});
}, 500),
onValidateHTML: (annotations) => {
let i = annotations.length;
const len = annotations.length;
while (i--) {
if (/doctype first\. Expected/.test(annotations[i].text)) {
annotations.splice(i, 1);
}
else if (/Unexpected End of file\. Expected/.test(annotations[i].text)) {
annotations.splice(i, 1);
}
}
if (len > annotations.length) {
this.htmlRef.editor.getSession().setAnnotations(annotations);
}
},
getAnnotations: () => {
const { htmlAnnotations, cssAnnotations, jsAnnotations } = this.state;
return {
htmlAnnotations,
cssAnnotations,
jsAnnotations,
};
},
getCodes: () => {
const { html, css, js } = this.state;
return {
html,
css,
js,
};
},
};
this.state = {
html: this.props.html,
css: this.props.css,
js: this.props.js,
htmlAnnotations: [],
cssAnnotations: [],
jsAnnotations: [],
};
}
componentWillUnmount() {
this.htmlRef.editor.destroy();
this.cssRef.editor.destroy();
this.jsRef.editor.destroy();
}
render() {
const { isHTML, isCSS, isJS, isPreview } = this.props;
const { html, css, js } = this.state;
return (react_1.default.createElement(antd_1.Row, null,
isHTML ? (react_1.default.createElement(antd_1.Col, { span: 12, style: defaultStyle },
react_1.default.createElement(antd_1.Form.Item, { label: "HTML", colon: false },
react_1.default.createElement(react_ace_1.default, { ref: (c) => {
this.htmlRef = c;
}, mode: "html", theme: "github", width: "100%", height: "200px", defaultValue: html, value: html, editorProps: {
$blockScrolling: true,
}, onChange: this.handlers.onChangeHTML })))) : null,
isCSS ? (react_1.default.createElement(antd_1.Col, { span: 12, style: defaultStyle },
react_1.default.createElement(antd_1.Form.Item, { label: "CSS", colon: false },
react_1.default.createElement(react_ace_1.default, { ref: (c) => {
this.cssRef = c;
}, mode: "css", theme: "github", width: "100%", height: "200px", defaultValue: css, value: css, editorProps: {
$blockScrolling: true,
}, onChange: this.handlers.onChangeCSS })))) : null,
isJS ? (react_1.default.createElement(antd_1.Col, { span: 12, style: defaultStyle },
react_1.default.createElement(antd_1.Form.Item, { label: "JS", colon: false },
react_1.default.createElement(react_ace_1.default, { ref: (c) => {
this.jsRef = c;
}, mode: "javascript", theme: "github", width: "100%", height: "200px", defaultValue: js, value: js, editorProps: {
$blockScrolling: true,
}, onChange: this.handlers.onChangeJS })))) : null,
isPreview ? (react_1.default.createElement(antd_1.Col, { span: 12, style: defaultStyle },
react_1.default.createElement(antd_1.Form.Item, { label: "Preview", colon: false },
react_1.default.createElement(AcePreview_1.default, { html: html, css: css, js: js })))) : null));
}
}
AceEditor.propTypes = {
isHTML: prop_types_1.default.bool,
isCSS: prop_types_1.default.bool,
isJS: prop_types_1.default.bool,
isPreview: prop_types_1.default.bool,
html: prop_types_1.default.string,
css: prop_types_1.default.string,
js: prop_types_1.default.string,
onChangeJS: prop_types_1.default.func,
onChangeCSS: prop_types_1.default.func,
onChangeHTML: prop_types_1.default.func,
};
AceEditor.defaultProps = {
isHTML: true,
isCSS: true,
isJS: true,
isPreview: true,
html: '',
css: '',
js: '',
};
exports.default = AceEditor;