refactoring
This commit is contained in:
22
packages/core/src/helpers/findChildren.ts
Normal file
22
packages/core/src/helpers/findChildren.ts
Normal 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
|
||||||
|
}
|
||||||
@@ -12,6 +12,9 @@ export { default as markPasteRule } from './pasteRules/markPasteRule'
|
|||||||
export { default as callOrReturn } from './utilities/callOrReturn'
|
export { default as callOrReturn } from './utilities/callOrReturn'
|
||||||
export { default as mergeAttributes } from './utilities/mergeAttributes'
|
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 generateHTML } from './helpers/generateHTML'
|
||||||
export { default as getSchema } from './helpers/getSchema'
|
export { default as getSchema } from './helpers/getSchema'
|
||||||
export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment'
|
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 isNodeEmpty } from './helpers/isNodeEmpty'
|
||||||
export { default as isNodeSelection } from './helpers/isNodeSelection'
|
export { default as isNodeSelection } from './helpers/isNodeSelection'
|
||||||
export { default as isTextSelection } from './helpers/isTextSelection'
|
export { default as isTextSelection } from './helpers/isTextSelection'
|
||||||
export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos'
|
|
||||||
|
|
||||||
export interface Commands {}
|
export interface Commands {}
|
||||||
|
|
||||||
|
|||||||
@@ -2,26 +2,7 @@ import { Plugin, PluginKey } from 'prosemirror-state'
|
|||||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||||
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
||||||
import low from 'lowlight/lib/core'
|
import low from 'lowlight/lib/core'
|
||||||
|
import { findChildren } from '@tiptap/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
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
|
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
|
||||||
return nodes
|
return nodes
|
||||||
@@ -48,8 +29,7 @@ function parseNodes(nodes: any[], className: string[] = []): { text: string, cla
|
|||||||
function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
|
function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
|
||||||
const decorations: Decoration[] = []
|
const decorations: Decoration[] = []
|
||||||
|
|
||||||
findBlockNodes(doc)
|
findChildren(doc, node => node.type.name === name)
|
||||||
.filter(block => block.node.type.name === name)
|
|
||||||
.forEach(block => {
|
.forEach(block => {
|
||||||
let startPos = block.pos + 1
|
let startPos = block.pos + 1
|
||||||
const { language } = block.node.attrs
|
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
|
// https://discuss.prosemirror.net/t/how-to-update-multiple-inline-decorations-on-node-change/1493
|
||||||
const oldNodeName = oldState.selection.$head.parent.type.name
|
const oldNodeName = oldState.selection.$head.parent.type.name
|
||||||
const newNodeName = newState.selection.$head.parent.type.name
|
const newNodeName = newState.selection.$head.parent.type.name
|
||||||
const oldNodes = findBlockNodes(oldState.doc)
|
const oldNodes = findChildren(oldState.doc, node => node.type.name === name)
|
||||||
.filter(node => node.node.type.name === name)
|
const newNodes = findChildren(newState.doc, node => node.type.name === name)
|
||||||
const newNodes = findBlockNodes(newState.doc)
|
|
||||||
.filter(node => node.node.type.name === name)
|
|
||||||
|
|
||||||
// Apply decorations if selection includes named node, or transaction changes named node.
|
// Apply decorations if selection includes named node, or transaction changes named node.
|
||||||
if (transaction.docChanged && ([oldNodeName, newNodeName].includes(name)
|
if (transaction.docChanged && ([oldNodeName, newNodeName].includes(name)
|
||||||
|
|||||||
Reference in New Issue
Block a user