microsoft / microsoft/monaco-editor

[Bug] getTargetAtClientPoint() does not respect ViewZones and OverlayWidget on Edge & Chrome

Open
#3,435 0 comments 0 reactions 1 assignee View on GitHub

@alexdima is already working on this.

Since Dec 12, 2022.

bug editor-core
Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Reproducible in vscode.dev or in VS Code Desktop?
  • Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
Monaco Editor Playground Code
var jsCode = [
	'"use strict";',
	'function Person(age) {',
	'	if (age) {',
	'		this.age = age;',
	'	}',
	'}',
	'Person.prototype.getAge = function () {',
	'	return this.age;',
	'};'
].join('\n');

var editor = monaco.editor.create(document.getElementById('container'), {
	value: jsCode,
	language: 'javascript',
	glyphMargin: true,
	contextmenu: false
});

var decorations = editor.deltaDecorations(
	[],
	[
		{
			range: new monaco.Range(3, 1, 3, 1),
			options: {
				isWholeLine: true,
				className: 'myContentClass',
				glyphMarginClassName: 'myGlyphMarginClass'
			}
		}
	]
);

// Add a zone to make hit testing more interesting
var viewZoneId = null;
editor.changeViewZones(function (changeAccessor) {
	var domNode = document.createElement('div');
	domNode.style.background = 'lightgreen';
	viewZoneId = changeAccessor.addZone({
		afterLineNumber: 3,
		heightInLines: 3,
		domNode: domNode
	});
});

// Add a content widget (scrolls inline with text)
var contentWidget = {
	domNode: null,
	getId: function () {
		return 'my.content.widget';
	},
	getDomNode: function () {
		if (!this.domNode) {
			this.domNode = document.createElement('div');
			this.domNode.innerHTML = 'My content widget';
			this.domNode.style.background = 'grey';
		}
		return this.domNode;
	},
	getPosition: function () {
		return {
			position: {
				lineNumber: 7,
				column: 8
			},
			preference: [
				monaco.editor.ContentWidgetPositionPreference.ABOVE,
				monaco.editor.ContentWidgetPositionPreference.BELOW
			]
		};
	}
};
editor.addContentWidget(contentWidget);

// Add an overlay widget
var overlayWidget = {
	domNode: null,
	getId: function () {
		return 'my.overlay.widget';
	},
	getDomNode: function () {
		if (!this.domNode) {
			this.domNode = document.createElement('div');
			this.domNode.innerHTML = 'My overlay widget';
			this.domNode.style.background = 'grey';
			this.domNode.style.right = '30px';
			this.domNode.style.top = '50px';
		}
		return this.domNode;
	},
	getPosition: function () {
		return null;
	}
};
editor.addOverlayWidget(overlayWidget);

var output = document.getElementById('output');
function showEvent(str) {
    while (output.childNodes.length > 20) {
		output.removeChild(output.firstChild.nextSibling.nextSibling);
	}
	output.appendChild(document.createTextNode(str));
	output.appendChild(document.createElement('br'));
}

const editorElement = document.getElementById('container');
editorElement.addEventListener('mouseup', (e) => {
	const mouseTarget = editor.getTargetAtClientPoint(e.clientX, e.clientY);

	const pageY = e.clientY + window.scrollY;
	const editorTop = editor.getDomNode().getBoundingClientRect().top + window.scrollY;
	const scrollTop = editor.getScrollTop();

	const lineBottom = editor.getBottomForLineNumber(mouseTarget.position.lineNumber);
	const lineTop = editor.getTopForLineNumber(mouseTarget.position.lineNumber);

	const withinLine = (lineBottom >= (pageY + scrollTop - editorTop)) && (lineTop <= (pageY + scrollTop - editorTop));

	showEvent('mouseup - target:' + mouseTarget.type + ' line:' + mouseTarget.position.lineNumber + ' withinLine:' + withinLine);
});

editor.onMouseUp(function (e) {
	showEvent('onMouseUp - target:' + e.target.type);
});
.myGlyphMarginClass {
	background: red;
}
.myContentClass {
	background: lightblue;
}
<div id="output" style="height: 29%; font-family: 'Courier New', monospace">
</div>
<div style="background: #ccc; height: 1%"></div>
<div id="container" style="height: 70%"></div>
Reproduction Steps

Click on the different zones in the Monaco editor and watch the output. There is one line for the Monaco mouse event and one for an event listener at each click.

Actual (Problematic) Behavior

The mouse target type returned by getTargetAtClientPoint() does not respect ViewZones and OverlayWidget on Edge & Chrome browsers.

Expected Behavior

The function getTargetAtClientPoint() should report the correct target.

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.