diff --git a/packages/core/src/helpers/findChildren.ts b/packages/core/src/helpers/findChildren.ts new file mode 100644 index 00000000..c580ec49 --- /dev/null +++ b/packages/core/src/helpers/findChildren.ts @@ -0,0 +1,22 @@ +import { Node as ProseMirrorNode } from 'prosemirror-model' +import { Predicate } from '../types' + +type NodeWithPos = { + node: ProseMirrorNode, + pos: number, +} + +export default function findChildren(node: ProseMirrorNode, predicate: Predicate): NodeWithPos[] { + const nodesWithPos: NodeWithPos[] = [] + + node.descendants((child, pos) => { + if (predicate(child)) { + nodesWithPos.push({ + node: child, + pos, + }) + } + }) + + return nodesWithPos +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 78e57616..9e650ac9 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -12,6 +12,9 @@ export { default as markPasteRule } from './pasteRules/markPasteRule' export { default as callOrReturn } from './utilities/callOrReturn' export { default as mergeAttributes } from './utilities/mergeAttributes' +export { default as findChildren } from './helpers/findChildren' +export { default as findParentNode } from './helpers/findParentNode' +export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos' export { default as generateHTML } from './helpers/generateHTML' export { default as getSchema } from './helpers/getSchema' export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment' @@ -22,7 +25,6 @@ export { default as isNodeActive } from './helpers/isNodeActive' export { default as isNodeEmpty } from './helpers/isNodeEmpty' export { default as isNodeSelection } from './helpers/isNodeSelection' export { default as isTextSelection } from './helpers/isTextSelection' -export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos' export interface Commands {} diff --git a/packages/extension-code-block-lowlight/src/lowlight-plugin.ts b/packages/extension-code-block-lowlight/src/lowlight-plugin.ts index 30744f1d..84eec5d3 100644 --- a/packages/extension-code-block-lowlight/src/lowlight-plugin.ts +++ b/packages/extension-code-block-lowlight/src/lowlight-plugin.ts @@ -2,26 +2,7 @@ import { Plugin, PluginKey } from 'prosemirror-state' import { Decoration, DecorationSet } from 'prosemirror-view' import { Node as ProsemirrorNode } from 'prosemirror-model' import low from 'lowlight/lib/core' - -type NodeWithPos = { - node: ProsemirrorNode, - pos: number, -} - -const findBlockNodes = (doc: ProsemirrorNode) => { - const nodes: NodeWithPos[] = [] - - doc.descendants((node, pos) => { - if (node.isBlock) { - nodes.push({ - node, - pos, - }) - } - }) - - return nodes -} +import { findChildren } from '@tiptap/core' function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] { return nodes @@ -48,8 +29,7 @@ function parseNodes(nodes: any[], className: string[] = []): { text: string, cla function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) { const decorations: Decoration[] = [] - findBlockNodes(doc) - .filter(block => block.node.type.name === name) + findChildren(doc, node => node.type.name === name) .forEach(block => { let startPos = block.pos + 1 const { language } = block.node.attrs @@ -88,10 +68,8 @@ export function LowlightPlugin({ name }: { name: string }) { // https://discuss.prosemirror.net/t/how-to-update-multiple-inline-decorations-on-node-change/1493 const oldNodeName = oldState.selection.$head.parent.type.name const newNodeName = newState.selection.$head.parent.type.name - const oldNodes = findBlockNodes(oldState.doc) - .filter(node => node.node.type.name === name) - const newNodes = findBlockNodes(newState.doc) - .filter(node => node.node.type.name === name) + const oldNodes = findChildren(oldState.doc, node => node.type.name === name) + const newNodes = findChildren(newState.doc, node => node.type.name === name) // Apply decorations if selection includes named node, or transaction changes named node. if (transaction.docChanged && ([oldNodeName, newNodeName].includes(name)