function getExpandedSelectionForMark()

in fronts-client/src/components/inputs/richtext/utils/command-helpers.ts [71:104]


function getExpandedSelectionForMark(state: EditorState, currentMark: any) {
	if (!currentMark) {
		return { from: state.selection.from, to: state.selection.to, href: null };
	}
	const { $from, $to } = state.selection;
	let startIndex = $from.index();
	let endIndex = $to.indexAfter();
	while (
		startIndex > 0 &&
		currentMark.isInSet($from.parent.child(startIndex - 1).marks)
	) {
		startIndex--;
	}
	while (
		endIndex < $from.parent.childCount &&
		currentMark.isInSet($from.parent.child(endIndex).marks)
	) {
		endIndex++;
	}
	let startPos = $from.start();
	let endPos = startPos;
	for (let i = 0; i < endIndex; i++) {
		const size = $from.parent.child(i).nodeSize;
		if (i < startIndex) {
			startPos += size;
		}

		endPos += size;
	}

	const href = currentMark.attrs ? currentMark.attrs.href : null;

	return { from: startPos, to: endPos, href };
}