@atlaskit/editor-wikimarkup-transformer
Version:
Wiki markup transformer for JIRA and Confluence
152 lines (147 loc) • 5.76 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TableBuilder = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* Return the cell type based on the delimeter
*/
function getType(style) {
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
return /\|\|/.test(style) ? 'tableHeader' : 'tableCell';
}
var TableBuilder = exports.TableBuilder = /*#__PURE__*/function () {
function TableBuilder(schema) {
var _this = this;
(0, _classCallCheck2.default)(this, TableBuilder);
(0, _defineProperty2.default)(this, "emptyTableCell", function () {
var _this$schema$nodes = _this.schema.nodes,
tableCell = _this$schema$nodes.tableCell,
paragraph = _this$schema$nodes.paragraph;
return tableCell.createChecked({}, paragraph.createChecked());
});
(0, _defineProperty2.default)(this, "emptyTableRow", function () {
var tableRow = _this.schema.nodes.tableRow;
return tableRow.createChecked({}, _this.emptyTableCell());
});
/**
* Build prosemirror table node
* @returns {PMNode}
*/
(0, _defineProperty2.default)(this, "buildTableNode", function () {
var root = _this.root;
var table = _this.schema.nodes.table;
var content = root.rows.map(_this.buildTableRowNode);
if (content.length === 0) {
content.push(_this.emptyTableRow());
}
return table.createChecked({}, content);
});
/**
* Build prosemirror tr node
* @returns {PMNode}
*/
(0, _defineProperty2.default)(this, "buildTableRowNode", function (row) {
var tableRow = _this.schema.nodes.tableRow;
return tableRow.createChecked({}, row.cells.map(_this.buildTableCellNode));
});
/**
* Build prosemirror td/th node
* @param {TableCell} cell
* @returns {PMNode}
*/
(0, _defineProperty2.default)(this, "buildTableCellNode", function (cell) {
var type = cell.type,
content = cell.content;
if (content.length === 0) {
content.push(_this.schema.nodes.paragraph.createChecked());
}
var cellNode = _this.schema.nodes[type];
return cellNode.createChecked({}, content);
});
this.schema = schema;
this.root = {
rows: []
};
}
/**
* Return the type of the base element
* @returns {string}
*/
return (0, _createClass2.default)(TableBuilder, [{
key: "type",
get: function get() {
return 'table';
}
/**
* Add new cells to the table
* @param {AddCellArgs[]} cells
*/
}, {
key: "add",
value: function add(cells) {
if (!cells.length) {
return;
}
// Iterate the cells and create TH/TD based on the delimeter
var index = 0;
var _iterator = _createForOfIteratorHelper(cells),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var cell = _step.value;
var content = cell.content,
style = cell.style;
var cellType = getType(style);
// For the first item, determine if it's a new row or not
if (index === 0) {
this.addRow();
}
var newCell = {
type: cellType,
content: content
};
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.lastRow.cells.push(newCell);
index += 1;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
/**
* Build a prosemirror table from the data
* @returns {PMNode}
*/
}, {
key: "buildPMNode",
value: function buildPMNode() {
return this.buildTableNode();
}
}, {
key: "addRow",
value:
/**
* Add a new row to the table
*/
function addRow() {
var rows = this.root.rows;
var row = {
cells: []
};
rows.push(row);
this.lastRow = row;
}
}]);
}();