refactoring

This commit is contained in:
Philipp Kühn 2021-01-28 09:57:20 +01:00
parent 4407d9a3d1
commit 6a5dde0101
5 changed files with 28 additions and 30 deletions

View File

@ -1,19 +1,13 @@
import { Mark } from 'prosemirror-model'
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { MarkRange } from '../types'
export type MarkPosition = { export default function getMarksBetween(from: number, to: number, state: EditorState): MarkRange[] {
mark: Mark, let marks: MarkRange[] = []
start: number,
end: number,
}
export default function getMarksBetween(start: number, end: number, state: EditorState): MarkPosition[] { state.doc.nodesBetween(from, to, (node, pos) => {
let marks: MarkPosition[] = []
state.doc.nodesBetween(start, end, (node, pos) => {
marks = [...marks, ...node.marks.map(mark => ({ marks = [...marks, ...node.marks.map(mark => ({
start: pos, from: pos,
end: pos + node.nodeSize, to: pos + node.nodeSize,
mark, mark,
}))] }))]
}) })

View File

@ -1,14 +1,8 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes' import objectIncludes from '../utilities/objectIncludes'
import getMarkType from './getMarkType' import getMarkType from './getMarkType'
import { AnyObject } from '../types' import { AnyObject, MarkRange } from '../types'
export type MarkRange = {
mark: Mark,
from: number,
to: number,
}
export default function isMarkActive( export default function isMarkActive(
state: EditorState, state: EditorState,

View File

@ -1,14 +1,8 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model' import { NodeType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes' import objectIncludes from '../utilities/objectIncludes'
import getNodeType from './getNodeType' import getNodeType from './getNodeType'
import { AnyObject } from '../types' import { AnyObject, NodeRange } from '../types'
export type NodeRange = {
node: Node,
from: number,
to: number,
}
export default function isNodeActive( export default function isNodeActive(
state: EditorState, state: EditorState,

View File

@ -24,7 +24,7 @@ export default function (regexp: RegExp, markType: MarkType, getAttributes?: Fun
const { excluded } = item.mark.type const { excluded } = item.mark.type
return excluded.find((type: MarkType) => type.name === markType.name) return excluded.find((type: MarkType) => type.name === markType.name)
}) })
.filter(item => item.end > textStart) .filter(item => item.to > textStart)
if (excludedMarks.length) { if (excludedMarks.length) {
return null return null

View File

@ -1,4 +1,8 @@
import { Node as ProseMirrorNode, ParseOptions } from 'prosemirror-model' import {
Node as ProseMirrorNode,
Mark as ProseMirrorMark,
ParseOptions,
} from 'prosemirror-model'
import { import {
EditorView, EditorView,
Decoration, Decoration,
@ -138,4 +142,16 @@ export type Range = {
to: number, to: number,
} }
export type NodeRange = {
node: ProseMirrorNode,
from: number,
to: number,
}
export type MarkRange = {
mark: ProseMirrorMark,
from: number,
to: number,
}
export type Predicate = (node: ProseMirrorNode) => boolean export type Predicate = (node: ProseMirrorNode) => boolean