UNPKG

@prisma-cms/front-editor

Version:
159 lines 5.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; 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 CSSTransform_1 = __importDefault(require("../../Tag/HtmlTag/CSSTransform")); class EditableText extends react_1.Component { constructor(props) { super(props); this.onInput = (event) => { const { onChange } = this.props; const node = event.target; const content = this.makeNewContent(node); const newState = { newContent: content, }; Object.assign(this.state, newState); onChange(content); }; const { components } = props; this.state = Object.assign(Object.assign({}, this.state), { content: components || { children: [], } }); } updateContent(node) { const content = { attributes: {}, }; const nodes = node.childNodes; let NodeName = node.nodeName.toLowerCase(); if (NodeName === '#text') { NodeName = undefined; } if (node.nodeType === Node.TEXT_NODE) { content.text = node.textContent; } else if (node.nodeType === Node.ELEMENT_NODE) { const attributes = node.attributes; node.getAttributeNames().map((name) => { let value = attributes[name].value; switch (name) { case 'id': case 'src': case 'href': // case "editable": break; case 'class': name = 'className'; break; case 'style': try { value = value ? CSSTransform_1.default(value) : undefined; } catch (error) { console.error(error); value = undefined; } break; default: return; } Object.assign(content.attributes, { [name]: value, }); }); const children = []; nodes.forEach((node) => { children.push(this.updateContent(node, {})); }); Object.assign(content, { children, }); } content.tag = NodeName; return content; } renderContent(node, key) { if (!node) { return null; } const { text, tag: Tag, children, attributes } = node, other = __rest(node, ["text", "tag", "children", "attributes"]); let content = text; if (children && children.length) { content = children.map((n, index) => this.renderContent(n, index)); } if (Tag) { content = (react_1.default.createElement(Tag, Object.assign({ key: key }, attributes, other), content)); } return content; } makeNewContent(node) { const nodes = node.childNodes; const children = []; const content = {}; nodes.forEach((n) => { children.push(this.updateContent(n)); }); Object.assign(content, { children, }); return content; } componentDidCatch(error, info) { console.error(error, info); } render() { const { content } = this.state; const { inEditMode } = this.props; return (react_1.default.createElement("div", { key: "content", style: inEditMode ? { height: '100%', minHeight: '1rem', } : undefined, contentEditable: inEditMode ? true : false, suppressContentEditableWarning: true, onInput: this.onInput }, this.renderContent(content) || react_1.default.createElement("div", null))); } } EditableText.propTypes = { onChange: prop_types_1.default.func, components: prop_types_1.default.object, inEditMode: prop_types_1.default.bool.isRequired, }; EditableText.defaultProps = { allowedTags: [], inEditMode: false, }; exports.default = EditableText; //# sourceMappingURL=index.js.map