@prisma-cms/front-editor
Version:
152 lines • 5.48 kB
JavaScript
;
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 = __importDefault(require("react"));
const EditorComponent_1 = __importDefault(require("../../EditorComponent"));
const Title_1 = __importDefault(require("material-ui-icons/Title"));
const transform_1 = __importDefault(require("./EditableText/transform"));
class TextArea extends EditorComponent_1.default {
constructor(props) {
super(props);
const {
// content,
data: { object }, } = props;
const { props: objectProps } = object || {};
const { content } = objectProps || {};
this.state = Object.assign(Object.assign({}, this.state), { content: content || {
children: [],
} });
}
renderPanelView(content) {
return super.renderPanelView(content || (react_1.default.createElement("div", { className: "editor-component--panel-icon" },
react_1.default.createElement(Title_1.default, null),
" TextArea")));
}
renderMainView() {
const { inEditMode } = this.getEditorContext();
let options;
if (inEditMode) {
options = {
contentEditable: true,
suppressContentEditableWarning: true,
onInput: (event) => {
const node = event.target;
const content = this.makeNewContent(node);
const newState = {
newContent: content,
};
// Object.assign(newState, {
// // content,
// });
Object.assign(this.state, newState);
// onChange(content);
this.updateComponentProps({
content,
});
},
};
}
return super.renderMainView(options);
}
renderChildren() {
const { content } = this.state;
return this.renderContent(content) || '';
}
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 ? transform_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);
}
}
TextArea.Name = 'TextArea';
exports.default = TextArea;
//# sourceMappingURL=index.js.map