ACE Editor DynamicMarker

by rezaamya

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.7/ace.js" integrity="sha256-C7DTYRJLG+B/VEzHGeoPMw699nsTQYPAXHKXZb+q04E=" crossorigin="anonymous"></script>
</head>
<body>

<div id="editor">
function foo(items) {
    var x = items * items;
    return x;
}

var test = foo(2);

console.log(test); //should be 4

console.log(foo(1)); //should be 1

console.log(foo(3)); //should be 9
</div>

</body>
</html>

CSS

#editor {
	/*height: 150px;*/
	position: absolute;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
}

.other_user_selection {
	position: absolute;
	z-index: 6;
}

.other_user_selection.blue {
	background-color: blue;
	background-color: rgba(0, 0, 255, 0.5);
}

/*ACE Collab ext*/
.ace-multi-cursor {
  position: absolute;
  pointer-events: auto;
  z-index: 10;
}

.ace-multi-cursor-tooltip {
  position: absolute;
  white-space: nowrap;
  color: #FFFFFF;
  text-shadow: 0 0 1px #000000;
  opacity: 1;
  font-size: 12px;
  padding: 2px;
  font-family: sans-serif;

  transition: opacity 0.5s ease-out;
  -webkit-transition: opacity 0.5s ease-out;
  -moz-transition: opacity 0.5s ease-out;
  -ms-transition: opacity 0.5s ease-out;
  -o-transition: opacity 0.5s ease-out;
}

.ace-multi-selection {
  position: absolute;
  pointer-events: auto;
  z-index: 10;
  opacity: 0.3;
}

.ace-radar-view {
  position: relative;
  min-width: 6px;
}

.ace-radar-view-scroll-indicator {
  position: absolute;
  left: 0;
  right: 0;
  border-radius: 4px;
  cursor: pointer;
  border-style: double;
  border-width: 3px;
}

.ace-radar-view-cursor-indicator {
  position: absolute;
  left: 0;
  right: 0;
  height: 4px;
  border-radius: 3px;
  cursor: pointer;
  border: 1px solid black;
}

.ace-radar-view-wrapper {
  position: relative;
  float: left;

  height: 100%;
  width: 6px;

  margin-right: 4px;
}

JavaScript

var editor = ace.edit("editor");
    editor.setTheme("ace/theme/monokai");
    editor.session.setMode("ace/mode/javascript");
  
	var label = "I am red remote cursor :)";
	
    var marker_element = document.createElement("div");
    var cursor_element = document.createElement("div");
    cursor_element.className = "ace-multi-cursor";
    cursor_element.style.background = "red";
    marker_element.append(cursor_element);

    var tooltip_element = document.createElement("div");
    tooltip_element.className = "ace-multi-cursor-tooltip";
    tooltip_element.style.background = "red";
    tooltip_element.style.opacity = "1";
    tooltip_element.innerHTML = label;
    marker_element.append(tooltip_element);
	
	var buildDom = ace.require("ace/lib/dom").buildDom
	editor.session.addDynamicMarker({
		contentNode: null,
		update: function(_, layer, session, config) {
			var screen_position = editor.session.documentToScreenPosition(6, 6);

			var top = layer.$getTop(screen_position.row, config);
			var left = layer.$padding + screen_position.column * config.characterWidth;
			var height = config.lineHeight;

			var cursor_top = top + 2;
			var cursor_height = height - 3;
			var cursor_left = left;
			var cursor_width = 2;
			
			cursor_element.style.height = `${cursor_height}px`;
			cursor_element.style.width = `${cursor_width}px`;
			cursor_element.style.top = `${cursor_top}px`;
			cursor_element.style.left = `${cursor_left}px`;

			let toolTipTop = cursor_top - height;
			if (toolTipTop < 5) {
			  toolTipTop = cursor_top + height - 1;
			}

			const toolTipLeft = cursor_left;
			tooltip_element.style.top = `${toolTipTop - 2}px`;
			tooltip_element.style.left = `${toolTipLeft - 2}px`;

			// Remove the content node from whatever parent it might have now
			// and add it to the new parent node.
			marker_element.remove();
			layer.elt("remote-cursor", "");
			const parentNode = layer.element.childNodes[layer.i - 1] ||...