Codemirror tooltip cutoff and duplicated

originated from LaTeX renders incorrectly in tables - #9 by dawni.

it seems tables get their own editorview (please document this), causing tooltips to render twice, one inside the table for the editorview of the table and one outside. See CodeMirror Tooltip Example for an mwe.

And in tables and canvas the tooltip gets cut off, this can be mitigated with the editorextension tooltips({parent: document.documentElement}) I think?
(or something along the lines of this in a ViewPlugin where it reconfigures the parent using the activeDocument), but would rather have this handled by obsidian than plugin devs.

or at the least documentation mentioning it.

since canvas uses and iframe and tables are embedded, both the tooltip config has to be changed for both and the getCoords in the tooltip itself has to be changed for the iframe of canvas.

for getCoords i used obsidian-latex-suite/src/editor_extensions/math_tooltip.ts at 5fdcca013a8daa41cdb7debe12e41a53035298a5 · artisticat1/obsidian-latex-suite · GitHub
e.g.

function getParentDocumentCoords(view: EditorView, pos: number, above: boolean): Rect {
	const coords = view.coordsAtPos(pos);
	if (!coords) return view.dom.getBoundingClientRect();
	const frame = view.dom.win.frameElement as HTMLElement | null;
	if (!frame) return coords;


	const frameRect = frame.getBoundingClientRect();
	const scaleX = frame.clientWidth ? frameRect.width / frame.clientWidth : 1;
	const scaleY = frame.clientHeight ? frameRect.height / frame.clientHeight : 1;
	const frameLeft = frameRect.left + frame.clientLeft * scaleX;
	const frameTop = frameRect.top + frame.clientTop * scaleY;
	const frameBottom = frameRect.bottom - (frame.clientHeight - frame.clientTop - frame.clientHeight) * scaleY;
	// const coordTop = above ? Math.max(coords.top, 0) : coords.top < 0 ? frameRect.bottom : coords.top;
	const coordTop = Math.max(coords.top, 0) * scaleY;
	const coordBottom = Math.max(coords.bottom, 0) * scaleY;
	const top = frameTop + coordTop;
	const bottom = frameTop + coordBottom;


	const left = frameLeft + coords.left * scaleX;
	const right = frameLeft + coords.right * scaleX;
	// dont scroll down beyond the top and bottom of the frame goes to back top/bottom depending on above.
	if (above) {
		const above_top = coords.top < 0 || top > frameBottom ? frameTop : top;
		return {
			right,
			left,
			top: above_top,
			bottom: top,
		};
	} else {
		const below_top = coords.bottom < 0 || bottom > frameBottom ? frameBottom : bottom;
		return {
			right,
			left,
			top: below_top,
			bottom: below_top,
		}	
	}
}

for the config obsidian-latex-suite/src/editor_extensions/math_tooltip.ts at 5fdcca013a8daa41cdb7debe12e41a53035298a5 · artisticat1/obsidian-latex-suite · GitHub
e.g.

const tooltipSpaceExtension = tooltips({
	position: "absolute",
	tooltipSpace() {
		const body = activeDocument.body
		const bodyRect = body.getBoundingClientRect();
		return {
			bottom: bodyRect.bottom,
			left: bodyRect.left,
			right: bodyRect.right,
			top: bodyRect.top,
		}
	},
	parent: activeDocument.body,
})

and on every update remove the tooltip if the view doesn’t have focus otherwise it will pop up over the other views/modals.