refactoring

This commit is contained in:
Philipp Kühn 2021-04-02 23:53:04 +02:00
parent dd505d2773
commit 6757813fd0
3 changed files with 29 additions and 27 deletions

View File

@ -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
}

View File

@ -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 {}

View File

@ -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)