EZQMS-291: fix documents node selections issues (#3845)

Signed-off-by: Anna No <anna.no@xored.com>
This commit is contained in:
Anna No 2023-10-17 15:32:59 +07:00 committed by GitHub
parent c199ab8e00
commit 09df36fa95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { Command, CommandProps, Mark, getMarkAttributes, getMarkType, mergeAttributes } from '@tiptap/core'
import { Command, CommandProps, Mark, getMarkType, getMarksBetween, mergeAttributes } from '@tiptap/core'
import { Node, Mark as ProseMirrorMark } from 'prosemirror-model'
import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
@ -32,17 +32,23 @@ export interface NodeUuidStorage {
}
const findSelectionNodeUuidMark = (state: EditorState): ProseMirrorMark | undefined => {
if (state.selection === null || state.selection === undefined) {
const { doc, selection } = state
if (selection === null || selection === undefined) {
return
}
let nodeUuidMark: ProseMirrorMark | undefined
state.doc.nodesBetween(state.selection.from, state.selection.to, (node) => {
if (nodeUuidMark !== null || nodeUuidMark !== undefined) {
return false
for (const range of selection.ranges) {
if (nodeUuidMark === undefined) {
doc.nodesBetween(range.$from.pos, range.$to.pos, (node) => {
if (nodeUuidMark !== undefined) {
return false
}
nodeUuidMark = findNodeUuidMark(node)
})
}
nodeUuidMark = findNodeUuidMark(node)
})
}
return nodeUuidMark
}
@ -104,10 +110,18 @@ export const NodeUuidExtension = Mark.create<NodeUuidOptions, NodeUuidStorage>({
new Plugin({
key: new PluginKey('handle-node-uuid-click-plugin'),
props: {
handleClick (view) {
const attrs = getMarkAttributes(view.state, view.state.schema.marks[NAME])
const nodeUuid = attrs?.[NAME]
if (nodeUuid !== null || nodeUuid !== undefined) {
handleClick (view, pos) {
const markRanges =
getMarksBetween(Math.max(0, pos - 1), pos + 1, view.state.doc)?.filter(
(markRange) => markRange.mark.type.name === NAME && markRange.from <= pos && markRange.to >= pos
) ?? []
let nodeUuid: string | null = null
if (markRanges.length > 0) {
nodeUuid = markRanges[0].mark.attrs[NAME]
}
if (nodeUuid !== null) {
options.onNodeClicked?.(nodeUuid)
}