UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

67 lines 2.37 kB
import { createToggleBlockMarkOnRangeNext } from '../commands'; const getDefaultBlockNodeTypes = tr => { const { paragraph } = tr.doc.type.schema.nodes; return paragraph ? [paragraph] : []; }; export const getBlockMarkAttrs = (node, markType) => { if (!node || !markType) { return false; } const blockMark = node.marks.find(mark => mark.type === markType); return blockMark ? blockMark.attrs : false; }; export const getFirstParagraphBlockMarkAttrs = (node, markType) => { if (!node || !markType) { return false; } const { paragraph } = node.type.schema.nodes; if (!paragraph) { return false; } const findFirstParagraphBlockMarkAttrs = currentNode => { if (currentNode.type === paragraph) { return getBlockMarkAttrs(currentNode, markType); } for (let index = 0; index < currentNode.childCount; index++) { const childResult = findFirstParagraphBlockMarkAttrs(currentNode.child(index)); if (childResult !== false) { return childResult; } } return false; }; return findFirstParagraphBlockMarkAttrs(node); }; export const reconcileBlockMarkInRange = (tr, from, to, markType, markAttrs, blockNodeTypes = getDefaultBlockNodeTypes(tr)) => { if (!markType || blockNodeTypes.length === 0) { return false; } return createToggleBlockMarkOnRangeNext(markType, () => markAttrs, blockNodeTypes)(from, to, tr); }; export const reconcileBlockMarkForContainerAtPos = (tr, containerPos, markType, markAttrs, blockNodeTypes = getDefaultBlockNodeTypes(tr)) => { const containerNode = tr.doc.nodeAt(containerPos); if (!containerNode) { return false; } return reconcileBlockMarkInRange(tr, containerPos, containerPos + containerNode.nodeSize, markType, markAttrs, blockNodeTypes); }; export const reconcileBlockMarkForParagraphAtPos = (tr, pos, markType, markAttrs) => { const { paragraph } = tr.doc.type.schema.nodes; if (!paragraph) { return false; } const resolvedPos = tr.doc.resolve(Math.max(0, Math.min(pos, tr.doc.content.size))); for (let depth = resolvedPos.depth; depth > 0; depth--) { const node = resolvedPos.node(depth); if (node.type === paragraph) { return reconcileBlockMarkInRange(tr, resolvedPos.before(depth), resolvedPos.before(depth) + node.nodeSize, markType, markAttrs, [paragraph]); } } return false; };