I would like to change mark’s attribute, at current caret position, when just clicking on text.
My question is in the code below :
pm.on('selectionChange', () => {
let sel = pm.selection;
if (sel.from && sel.to) {
// if selection length is 0 (just clicked on text)
if (sel.from.cmp(sel.to) === 0 ) {
let node = pm.doc.nodeAfter(sel.from);
if (node) {
// my question is here
// how to retrieve node position start and node position end
// to replace mark at selection pos
}
}
}
}
let start
if(node.isTextblock) {
start = sel.from.shorten()
} else {
start = sel.from
}
let end = start.move(1)
(I think that’s right, I’m going off of pure memory here)
The reason being, if your selection is in a text block the path component of from is the path to the node and the offset is the position within the text, but for a block selection the path component is the path to the parent node and the offset is the blocks position in relation to it’s siblings
This is currently a bit of a pain, but you can use chunkBefore or chunkAfter (depending on which directly you want to bias towards) to get the start of the text node at a given position.