UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

246 lines 7.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _1 = require("../../"); /** * Deduce a set of marks from a style declaration. */ function marksFromStyle(schema, style) { var marks = []; styles: for (var i = 0; i < style.length; i++) { var name_1 = style.item(i); var value = style.getPropertyValue(name_1); switch (name_1) { case 'text-decoration-color': case 'text-decoration-style': continue styles; case 'text-decoration-line': case 'text-decoration': switch (value) { case 'line-through': marks = schema.marks.strike.create().addToSet(marks); continue styles; } break; case 'font-family': if (value === 'monospace') { marks = schema.marks.code.create().addToSet(marks); continue styles; } } throw new Error("Unable to derive a mark for CSS " + name_1 + ": " + value); } return marks; } exports.marksFromStyle = marksFromStyle; /** * Create a fragment by adding a set of marks to each node. */ function addMarks(fragment, marks) { var result = fragment; for (var i = 0; i < fragment.childCount; i++) { var child = result.child(i); var newChild = child; for (var _i = 0, marks_1 = marks; _i < marks_1.length; _i++) { var mark = marks_1[_i]; newChild = newChild.mark(mark.addToSet(newChild.marks)); } result = result.replaceChild(i, newChild); } return result; } exports.addMarks = addMarks; /** * * Traverse the DOM node and build an array of the breadth-first-search traversal * through the tree. * * Detection of supported vs unsupported content happens at this stage. Unsupported * nodes do not have their children traversed. Doing this avoids attempting to * decode unsupported content descendents into ProseMirror nodes. */ function findTraversalPath(roots) { var inqueue = roots.slice(); var outqueue = []; var elem; while (elem = inqueue.shift()) { outqueue.push(elem); var children_1 = void 0; if (isNodeSupportedContent(elem) && (children_1 = childrenOfNode(elem))) { var childIndex = void 0; for (childIndex = 0; childIndex < children_1.length; childIndex++) { var child = children_1[childIndex]; inqueue.push(child); } } } return outqueue; } exports.findTraversalPath = findTraversalPath; function childrenOfNode(node) { var tag = getNodeName(node); if (tag === 'AC:STRUCTURED-MACRO') { return getAcTagChildNodes(node, 'AC:RICH-TEXT-BODY'); } return node.childNodes; } /** * Return an array containing the child nodes in a fragment. * * @param fragment */ function children(fragment) { var nodes = []; for (var i = 0; i < fragment.childCount; i++) { nodes.push(fragment.child(i)); } return nodes; } exports.children = children; /** * Quickly determine if a DOM node is supported (i.e. can be represented in the ProseMirror * schema). * * When a node is not supported, its children are not traversed — instead the entire node content * is stored inside an `unsupportedInline`. * * @param node */ function isNodeSupportedContent(node) { if (node.nodeType === Node.TEXT_NODE || node.nodeType === Node.CDATA_SECTION_NODE) { return true; } if (node instanceof HTMLElement || node.nodeType === Node.ELEMENT_NODE) { var tag = getNodeName(node); switch (tag) { case 'DEL': case 'S': case 'B': case 'STRONG': case 'I': case 'EM': case 'CODE': case 'SUB': case 'SUP': case 'U': case 'BLOCKQUOTE': case 'SPAN': case 'H1': case 'H2': case 'H3': case 'H4': case 'H5': case 'H6': case 'BR': case 'HR': case 'UL': case 'OL': case 'LI': case 'P': case 'A': case 'FAB:MENTION': case 'FAB:MEDIA': case 'AC:STRUCTURED-MACRO': return true; } } return false; } function getAcName(node) { return (node.getAttribute('ac:name') || '').toUpperCase(); } exports.getAcName = getAcName; function getNodeName(node) { return node.nodeName.toUpperCase(); } exports.getNodeName = getNodeName; function getAcParameter(node, parameter) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; if (getNodeName(child) === 'AC:PARAMETER' && getAcName(child) === parameter.toUpperCase()) { return child.textContent; } } return null; } exports.getAcParameter = getAcParameter; function getAcTagContent(node, tagName) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; if (getNodeName(child) === tagName) { return child.textContent; } } return null; } exports.getAcTagContent = getAcTagContent; function getAcTagChildNodes(node, tagName) { var child = getAcTagNode(node, tagName); if (child) { // return html collection only if childNodes are found return child.childNodes.length ? child.childNodes : null; } return null; } exports.getAcTagChildNodes = getAcTagChildNodes; function getAcTagNode(node, tagName) { for (var i = 0; i < node.childNodes.length; i++) { var child = node.childNodes[i]; if (getNodeName(child) === tagName) { return child; } } return null; } exports.getAcTagNode = getAcTagNode; function getMacroAttribute(node, attribute) { return (node.getAttribute('data-macro-' + attribute) || ''); } exports.getMacroAttribute = getMacroAttribute; function getMacroParameters(node) { var params = {}; getMacroAttribute(node, 'parameters').split('|').forEach(function (paramStr) { var param = paramStr.split('='); if (param.length) { params[param[0]] = param[1]; } }); return params; } exports.getMacroParameters = getMacroParameters; function createCodeFragment(schema, codeContent, language, title) { var content = []; var nodeSize = 0; if (!!title) { var titleNode = schema.nodes.heading.create({ level: 5 }, schema.text(title, [schema.marks.strong.create()])); content.push(titleNode); nodeSize += titleNode.nodeSize; } var codeBlockNode = schema.nodes.codeBlock.create({ language: language }, schema.text(codeContent)); content.push(codeBlockNode); nodeSize += codeBlockNode.nodeSize; return _1.Fragment.from(content); } exports.createCodeFragment = createCodeFragment; function hasClass(node, className) { if (node && node.className) { return node.className.indexOf(className) > -1; } return false; } exports.hasClass = hasClass; /* * Contructs a struct string of replacement blocks and marks for a given node */ function getContent(node, convertedNodes) { var fragment = _1.Fragment.fromArray([]); for (var childIndex = 0; childIndex < node.childNodes.length; childIndex++) { var child = node.childNodes[childIndex]; var thing = convertedNodes.get(child); if (thing instanceof _1.Fragment || thing instanceof _1.Node) { fragment = fragment.append(_1.Fragment.from(thing)); } } return fragment; } exports.getContent = getContent; //# sourceMappingURL=utils.js.map