UNPKG

prosemirror-test-builder

Version:

Helpers for programatically building ProseMirror test documents

253 lines (216 loc) 5.42 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var prosemirrorSchemaBasic = require('prosemirror-schema-basic'); var prosemirrorSchemaList = require('prosemirror-schema-list'); var prosemirrorModel = require('prosemirror-model'); var noTag = prosemirrorModel.Node.prototype.tag = Object.create(null); function flatten(schema, children, f) { var result = [], pos = 0, tag = noTag; for (var i = 0; i < children.length; i++) { var child = children[i]; if (typeof child == "string") { var re = /<(\w+)>/g, m = void 0, at = 0, out = ""; while (m = re.exec(child)) { out += child.slice(at, m.index); pos += m.index - at; at = m.index + m[0].length; if (tag == noTag) tag = Object.create(null); tag[m[1]] = pos; } out += child.slice(at); pos += child.length - at; if (out) result.push(f(schema.text(out))); } else { if (child.tag && child.tag != prosemirrorModel.Node.prototype.tag) { if (tag == noTag) tag = Object.create(null); for (var _id in child.tag) { tag[_id] = child.tag[_id] + (child.flat || child.isText ? 0 : 1) + pos; } } if (child.flat) { for (var j = 0; j < child.flat.length; j++) { var node = f(child.flat[j]); pos += node.nodeSize; result.push(node); } } else { var _node = f(child); pos += _node.nodeSize; result.push(_node); } } } return { nodes: result, tag: tag }; } function id(x) { return x; } function takeAttrs(attrs, args) { var a0 = args[0]; if (!args.length || a0 && (typeof a0 == "string" || a0 instanceof prosemirrorModel.Node || a0.flat)) return attrs; args.shift(); if (!attrs) return a0; if (!a0) return attrs; var result = {}; for (var prop in attrs) { result[prop] = attrs[prop]; } for (var _prop in a0) { result[_prop] = a0[_prop]; } return result; } function block(type) { var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var result = function result() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var myAttrs = takeAttrs(attrs, args); var _flatten = flatten(type.schema, args, id), nodes = _flatten.nodes, tag = _flatten.tag; var node = type.create(myAttrs, nodes); if (tag != noTag) node.tag = tag; return node; }; if (type.isLeaf) try { result.flat = [type.create(attrs)]; } catch (_) {} return result; } function mark(type, attrs) { return function () { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } var mark = type.create(takeAttrs(attrs, args)); var _flatten2 = flatten(type.schema, args, function (n) { var newMarks = mark.addToSet(n.marks); return newMarks.length > n.marks.length ? n.mark(newMarks) : n; }), nodes = _flatten2.nodes, tag = _flatten2.tag; return { flat: nodes, tag: tag }; }; } function builders(schema, names) { var result = { schema: schema }; for (var name in schema.nodes) { result[name] = block(schema.nodes[name], {}); } for (var _name in schema.marks) { result[_name] = mark(schema.marks[_name], {}); } if (names) for (var _name2 in names) { var value = names[_name2], typeName = value.nodeType || value.markType || _name2, type = void 0; if (type = schema.nodes[typeName]) result[_name2] = block(type, value);else if (type = schema.marks[typeName]) result[_name2] = mark(type, value); } return result; } var schema = new prosemirrorModel.Schema({ nodes: prosemirrorSchemaList.addListNodes(prosemirrorSchemaBasic.schema.spec.nodes, "paragraph block*", "block"), marks: prosemirrorSchemaBasic.schema.spec.marks }); var b = builders(schema, { p: { nodeType: "paragraph" }, pre: { nodeType: "code_block" }, h1: { nodeType: "heading", level: 1 }, h2: { nodeType: "heading", level: 2 }, h3: { nodeType: "heading", level: 3 }, li: { nodeType: "list_item" }, ul: { nodeType: "bullet_list" }, ol: { nodeType: "ordered_list" }, br: { nodeType: "hard_break" }, img: { nodeType: "image", src: "img.png" }, hr: { nodeType: "horizontal_rule" }, a: { markType: "link", href: "foo" } }); function eq(a, b) { return a.eq(b); } var doc = b.doc; var p = b.p; var code_block = b.code_block; var pre = b.pre; var h1 = b.h1; var h2 = b.h2; var h3 = b.h3; var li = b.li; var ul = b.ul; var ol = b.ol; var img = b.img; var hr = b.hr; var br = b.br; var blockquote = b.blockquote; var a = b.a; var em = b.em; var strong = b.strong; var code = b.code; exports.a = a; exports.blockquote = blockquote; exports.br = br; exports.builders = builders; exports.code = code; exports.code_block = code_block; exports.doc = doc; exports.em = em; exports.eq = eq; exports.h1 = h1; exports.h2 = h2; exports.h3 = h3; exports.hr = hr; exports.img = img; exports.li = li; exports.ol = ol; exports.p = p; exports.pre = pre; exports.schema = schema; exports.strong = strong; exports.ul = ul;