UNPKG

@prisma-cms/front-editor

Version:
121 lines 4.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.nodeChildsToEditorComponentObjectComponents = void 0; const HtmlTag_1 = __importDefault(require("../../../Tag/HtmlTag")); const CSSTransform_1 = __importDefault(require("../../../Tag/HtmlTag/CSSTransform")); /** * Convert HTML Node to EditorComponentObject JSON */ const nodeToEditorComponentObject = (node, content) => { var _a; if (!content) { content = { name: 'HtmlTag', component: 'HtmlTag', props: {}, components: [], }; } /** * Если это реакт-нода, то возвращаем его состояние */ if (node instanceof Element) { const { reactComponent, editorComponentObject } = node; if (editorComponentObject) { return editorComponentObject; } if (reactComponent && !(reactComponent instanceof HtmlTag_1.default)) { const component = reactComponent.getObjectWithMutations(); return component; } } const nodes = node.childNodes; let NodeName = node.nodeName.toLowerCase(); if (NodeName === '#text') { NodeName = undefined; } if (node.nodeType === Node.TEXT_NODE) { // https://stackoverflow.com/questions/12754256/removing-invalid-characters-in-javascript content.props.text = ((_a = node.textContent) === null || _a === void 0 ? void 0 : _a.replace(/\uFFFD/g, '')) || ''; } else if (node instanceof Element && node.nodeType === Node.ELEMENT_NODE) { const attributes = node.attributes; node.getAttributeNames().map((name) => { // let value = attributes[name].value; var _a, _b; let value = (_b = (_a = attributes.getNamedItem(name)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined; switch (name) { // case "id": // case "src": // case "href": // // case "editable": // break; case 'props': case 'object': case 'data': return null; case 'contenteditable': // name = "contentEditable"; // break; return null; case 'class': name = 'className'; break; case 'staticcontext': name = 'staticContext'; break; case 'style': try { // console.log("CSSTransform style", value); value = value ? CSSTransform_1.default(value) : undefined; // console.log("CSSTransform new style", value); } catch (error) { console.error(error); value = undefined; } break; default: } content && Object.assign(content.props, { [name]: value, }); return null; }); const components = []; nodes.forEach((node) => { const component = nodeToEditorComponentObject(node); component && components.push(component); }); Object.assign(content, { components, }); } content.props.tag = NodeName; return content; }; /** * Конвертируем содержимое HTML-ноды в компоненты */ const nodeChildsToEditorComponentObjectComponents = (node) => { const nodes = node.childNodes; const components = []; const content = {}; nodes.forEach((n) => { // console.log('makeNewContent n', n) const component = nodeToEditorComponentObject(n); // console.log('makeNewContent component', component) component && components.push(component); }); Object.assign(content, { components, }); return content; }; exports.nodeChildsToEditorComponentObjectComponents = nodeChildsToEditorComponentObjectComponents; exports.default = nodeToEditorComponentObject; //# sourceMappingURL=nodeToEditorComponentObject.js.map