我想使用ACE Editor创建一个协作式文本编辑器。我发现我应该使用addDynamicMarker将其他用户的光标添加到编辑器中,您可以找到它的文档here。
使用以下代码片段,我可以将远程连接用户的光标添加到编辑器中。但我不知道怎么把它绑在绳子上。我的意思是,如果您在第一行输入,您将看到所有内容都将根据您输入的行数向下移动,但动态游标(RED remote cursor)将保持其位置不变:
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] || layer.element.lastChild;
parentNode.appendChild(marker_element);
}
}, true);#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;
}<!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>
我可以尝试创建一个复杂的函数来监听编辑器的更改,如果有新行,更新光标的位置,但我认为ACE编辑器可能有一些更好的内部函数。
提前感谢您提供的任何线索或指导。
发布于 2019-12-25 04:41:45
Ace有两个api帮助更新游标位置,对于范围,有一个范围列表,供cloud9 https://github.com/c9/core/blob/c4d1c59dc8d6619bdca3dbe740291cd5cd26352c/plugins/c9.ide.collab/cursor_layer.js#L196中的协作游标实现使用;对于单点,有一个锚点。
var anchor = editor.session.doc.createAnchor(10, 10)
editor.session.insert({row: 0, column: 0}, "\n")
anchor.getPosition().row == 11当不再需要anchor.detach()时,调用它是很重要的,否则它将被附加到文档中,不会被收集。
https://stackoverflow.com/questions/59439989
复制相似问题