I have created a Links Plugin that displays a form when a link is clicked. The view part of it is working, but I am having trouble writing the updateMark command needed to set the attrs of the <a> tag.
I used the prosemirror-command's toggleMark command as a base, and it works if the user selects the link before clicking it.
My issue is when there is no selection, only a cursor.
function updateMark(markType: MarkType, attrs: object) {
return function(state: any, dispatch: any) {
let {$cursor, ranges} = state.selection
if (dispatch) {
if ($cursor) {
// This path does not work.
dispatch(state.tr.setStoredMarks([markType.create(attrs)]))
} else {
// This path seems fine
let tr = state.tr
for (let i = 0; i < ranges.length; i++) {
let {$from, $to} = ranges[i]
tr.addMark($from.pos, $to.pos, markType.create(attrs))
}
dispatch(tr.scrollIntoView())
}
}
return true
}
}
Part of the problem is that the link described above is actually 3 different nodes, each with the same Link mark on it. I think the solution would be to only allow text inside a tags. Any other suggestions?
function updateMark(markType: MarkType, attrs: object) {
return function (state: EditorState, dispatch: any) {
if (dispatch) {
let from = state.selection.from
let to = state.selection.to
if (from === to) {
const position = state.tr.doc.resolve(from)
const parentInfo = position.parent.childBefore(position.parentOffset)
from = parentInfo.offset + 1
to = from + (parentInfo.node?.nodeSize || 0) + 1
}
dispatch(state.tr.addMark(from, to, markType.create(attrs)), state.tr.scrollIntoView())
}
return true
}
}