monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] GlyphMarginWidget seems not working correctly in modifiedEditor

Open lucifan opened this issue 1 year ago • 1 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.45.0#XQAAAAL_CAAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscxFFkJ8Zmn-4FNw7EJFXgGEG8BcQ_Fjg3kpvZbppnCzrIpn59Pt_K7bHHUFAytminI-rT7FODkiWGR_GOJXp0qJBskL81HIRliV0AzzE2LjzEUOKfuVt0Dyn-SzlP6OeT-5ewKRDQGED3pQ3mkKir1Hcogm6hwWgQb8-XQY5l16M7bbhfJeHTCoQT3jGerAeYadW2sBYxICpWaHCbFS7DQmeSJ0ZubV7tfq2qaiT2RsG9yjgG8x5lPRIEpz9k7e5oc-qQamNKk_k8YL2NoLG91XWO0qHf0fZPECLgCSBOI4jIN-qrqlbEgdyMWImzh1hpiLEEl0Po0-MxF44j3hb4NsRcC6h04KFVRBioNQkHN0dc91olhx4S2Uvy5FCYbaRESkp8nLzPTrIRnkzoQw231lmuvdAISJfQsE9cbdSQkclkKxEcfy92MCFVqg9SeVQInLT6PTul1-vTZq5vg04yL6FtJa2OEMWiPRXdRwvwOnrFqihZhB3soJuMn_7S1533ZdWqJg-0olxB9DrAUC21ZohHwc3T03Fc5ae7ece0ZxIokZ0tFF2-bvwdgCpCLQwb3OeDhfE8YLjifnyGDB__Kj2db5nDP2GShvWl5797MH_yUTElHhrnlxYkLRn6xQp6bKLEl0l9kwN9dWxVgE8ZvzqmuwQg14ibJpCPHLIOR6_lzdaPbgf3HwbWh39K8fF5my33szUL276GSYVRytTiZFyPbloAqsp4AI8HqjYPdPb-7synKzGTIkKzdieVXX4gZEXa1Ao3xSYKxXal8p0Vi1M1Q44nA9DgL70UdwcakdAUnWmvCRyQghf5h-pdNwdmjDoaFMsp_IsduJOehNHLMAgKWC1vuxpN0ES_l1Ba5fpGwUkt9nAM2t8XpUe7exh9YITgK_6NZEtLPeGRArwKkHiw6uwFCSwzWqkeSEiFdIFKUzDbzSh1vexQGpaf496nnpRkFCtuR8HYSnXIbELyoR4sEaLXrAa0xoyIZ-SW66LWe-vkDU6kzOsqvIVSVKFvSSIk5FPrZRp5NQ9VmII2Bq0Nh0Q1euiM7r5uWV_hqRURN_2GJDWHCNqJeZ5aL7rzRi4JMwhNm-b3_-wN5Zg

Monaco Editor Playground Code

const originalText = `
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
`

const modifiedText = `
function search(array, target) {
  let left = 0;
  let right = array.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (array[mid] === target) {
      return mid;
    } else if (array[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}
`
function updateGlyphMargin(editor, isPosOriginal, lineNumber) {
	var glyphMarginWidget = {
		getId: function () {
			return isPosOriginal ? 'addCommentIconLeft' : 'addCommentIconRight';
		},
		getDomNode: function () {
			const domNode = document.createElement('div');
			domNode.innerHTML = '+';
			domNode.dataset.lineNumber = lineNumber.toString();
			domNode.title = 'add comment';
			return domNode;
		},
		getPosition: function () {
			console.log(lineNumber)
			return {
				lane: 1,
                                zIndex: 1,
                                range: new monaco.Range(lineNumber,1,lineNumber,1)
			};
		}
	};
	editor.removeGlyphMarginWidget(glyphMarginWidget);
	editor.addGlyphMarginWidget(glyphMarginWidget);
}


// Hover on each property to see its docs!
const newEditor = monaco.editor.createDiffEditor(document.getElementById("container"), {
	theme: 'vs-dark',
	automaticLayout: true,
	readOnly: true
});

newEditor.setModel({
	original: monaco.editor.createModel(originalText, 'javascript'),
	modified: monaco.editor.createModel(modifiedText, 'javascript')
});

const originalEditor = newEditor.getOriginalEditor();
const modifiedEditor = newEditor.getModifiedEditor();

originalEditor.onMouseMove((e) => {
	if (e.target.position?.lineNumber) {
		updateGlyphMargin(originalEditor,  true, e.target.position.lineNumber)
	}
});

modifiedEditor.onMouseMove((e) => {
	if (e.target.position?.lineNumber) {
		updateGlyphMargin(modifiedEditor, false, e.target.position.lineNumber)
	}
});

Reproduction Steps

just apply the code and try to hover on originalEditor and modifiedEditor, only originalEditor show glyphMarginWidget, modifedEditor got nothing.

Actual (Problematic) Behavior

The glyphMarginWidget works well in originalEditor but not working in modifiedEditor. With chrome developer tool, we can see that the glyphMarginWidget always got display: none

image

Expected Behavior

modifiedEditor should show glyphMarginWidget like originalEditor when hover event trigger.

Additional Context

No response

lucifan avatar Jan 08 '24 08:01 lucifan

+1

ghostbody avatar Jan 23 '24 11:01 ghostbody