@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
107 lines • 4.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var nodes_1 = require("./nodes");
var marks_1 = require("./marks");
var validator_1 = require("../validator");
var ReactSerializer = (function () {
function ReactSerializer(providers, eventHandlers, portal, objectContext) {
this.providers = providers;
this.eventHandlers = eventHandlers;
this.portal = portal;
this.rendererContext = objectContext;
}
ReactSerializer.prototype.serializeFragment = function (fragment, props, target, key) {
var _this = this;
if (props === void 0) { props = {}; }
if (target === void 0) { target = nodes_1.Doc; }
if (key === void 0) { key = 'root-0'; }
var content = ReactSerializer.getChildNodes(fragment).map(function (node, index) {
if (nodes_1.isTextWrapper(node.type.name)) {
return _this.serializeTextWrapper(node.content);
}
return _this.serializeFragment(node.content, _this.getProps(node), nodes_1.toReact(node), node.type.name + "-" + index);
});
return this.renderNode(target, props, key, content);
};
ReactSerializer.prototype.serializeTextWrapper = function (content) {
var _this = this;
return ReactSerializer
.buildMarkStructure(content)
.map(function (mark, index) { return _this.serializeMark(mark, index); });
};
ReactSerializer.prototype.serializeMark = function (mark, index) {
var _this = this;
if (index === void 0) { index = 0; }
if (mark.type.name === 'text') {
return mark.text;
}
var content = (mark.content || []).map(function (child, index) { return _this.serializeMark(child, index); });
return this.renderMark(marks_1.toReact(mark), this.getMarkProps(mark), mark.type.name + "-" + index, content);
};
// tslint:disable-next-line:variable-name
ReactSerializer.prototype.renderNode = function (Node, props, key, content) {
return (React.createElement(Node, tslib_1.__assign({ key: key }, props), content));
};
// tslint:disable-next-line:variable-name
ReactSerializer.prototype.renderMark = function (Mark, props, key, content) {
return (React.createElement(Mark, tslib_1.__assign({ key: key }, props), content));
};
ReactSerializer.prototype.getProps = function (node) {
return tslib_1.__assign({ text: node.text, providers: this.providers, eventHandlers: this.eventHandlers, portal: this.portal, rendererContext: this.rendererContext }, node.attrs);
};
ReactSerializer.prototype.getMarkProps = function (mark) {
return mark.attrs;
};
ReactSerializer.getChildNodes = function (fragment) {
var children = [];
fragment.forEach(function (node) {
children.push(node);
});
return nodes_1.mergeTextNodes(children);
};
ReactSerializer.getMarks = function (node) {
if (!node.marks || node.marks.length === 0) {
return [];
}
return validator_1.getMarksByOrder(node.marks);
};
ReactSerializer.buildMarkStructure = function (content) {
var _this = this;
var currentMarkNode;
return content.reduce(function (acc, node, index) {
var nodeMarks = _this.getMarks(node);
if (nodeMarks.length === 0) {
currentMarkNode = node;
acc.push(currentMarkNode);
}
else {
nodeMarks.forEach(function (mark, markIndex) {
var isSameAsPrevious = validator_1.isSameMark(mark, currentMarkNode);
var previousIsInMarks = markIndex > 0 && nodeMarks.some(function (m) { return validator_1.isSameMark(m, currentMarkNode); });
if (!isSameAsPrevious) {
var newMarkNode = tslib_1.__assign({}, mark, { content: [] });
if (previousIsInMarks) {
currentMarkNode.content.push(newMarkNode);
currentMarkNode = newMarkNode;
}
else {
acc.push(newMarkNode);
currentMarkNode = newMarkNode;
}
}
});
currentMarkNode.content.push(node);
}
return acc;
}, []);
};
ReactSerializer.fromSchema = function (schema, providers, eventHandlers) {
// TODO: Do we actually need the schema here?
return new ReactSerializer(providers, eventHandlers);
};
return ReactSerializer;
}());
exports.default = ReactSerializer;
//# sourceMappingURL=index.js.map