Skip to content

Commit 3a305b8

Browse files
committed
Update editor other cursor classes and adding option for other cursors features in CodeMirror
1 parent bd3d495 commit 3a305b8

5 files changed

Lines changed: 56 additions & 46 deletions

File tree

public/css/index.css

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,7 @@ body {
241241
.dropdown-menu > li > a {
242242
cursor: pointer;
243243
}
244-
.other-cursors {
245-
position:relative;
246-
z-index:3;
247-
}
248-
.other-cursor {
249-
width: 0px;
250-
position: absolute;
251-
border-right: none;
252-
}
253-
.dropdown-menu.other-cursor {
244+
.dropdown-menu.CodeMirror-other-cursor {
254245
transition: none;
255246
}
256247
.cursortag {
@@ -288,7 +279,7 @@ div[contenteditable]:empty:not(:focus):before{
288279
.dropdown-menu .emoji {
289280
margin-bottom: 0 !important;
290281
}
291-
.dropdown-menu.other-cursor {
282+
.dropdown-menu.CodeMirror-other-cursor {
292283
max-height: 50vh;
293284
width: auto !important;
294285
overflow: auto;

public/js/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ window.editor = CodeMirror.fromTextArea(textit, {
455455
addModeClass: true,
456456
readOnly: true,
457457
autoRefresh: true,
458+
otherCursors: true,
458459
placeholder: "← Start by entering a title here\n===\nVisit /features if you don't know what to do.\nHappy hacking :)"
459460
});
460461
var inlineAttach = inlineAttachment.editors.codemirror4.attach(editor);
@@ -2719,7 +2720,7 @@ socket.on('online users', function (data) {
27192720
console.debug(data);
27202721
onlineUsers = data.users;
27212722
updateOnlineStatus();
2722-
$('.other-cursors').children().each(function (key, value) {
2723+
$('.CodeMirror-other-cursors').children().each(function (key, value) {
27232724
var found = false;
27242725
for (var i = 0; i < data.users.length; i++) {
27252726
var user = data.users[i];
@@ -3044,11 +3045,11 @@ function buildCursor(user) {
30443045
iconClass = 'fa-desktop';
30453046
break;
30463047
}
3047-
if ($('.other-cursors').length <= 0) {
3048-
$("<div class='other-cursors'>").insertAfter('.CodeMirror-cursors');
3048+
if ($('.CodeMirror-other-cursors').length <= 0) {
3049+
$("<div class='CodeMirror-other-cursors'>").insertAfter('.CodeMirror-cursors');
30493050
}
30503051
if ($('div[data-clientid="' + user.id + '"]').length <= 0) {
3051-
var cursor = $('<div data-clientid="' + user.id + '" class="other-cursor" style="display:none;"></div>');
3052+
var cursor = $('<div data-clientid="' + user.id + '" class="CodeMirror-other-cursor" style="display:none;"></div>');
30523053
cursor.attr('data-line', user.cursor.line);
30533054
cursor.attr('data-ch', user.cursor.ch);
30543055
cursor.attr('data-offset-left', 0);
@@ -3119,7 +3120,7 @@ function buildCursor(user) {
31193120

31203121
cursor[0].style.left = coord.left + 'px';
31213122
cursor[0].style.top = coord.top + 'px';
3122-
$('.other-cursors').append(cursor);
3123+
$('.CodeMirror-other-cursors').append(cursor);
31233124

31243125
if (!user.idle)
31253126
cursor.stop(true).fadeIn();
@@ -3622,8 +3623,8 @@ function checkCursorMenuInner() {
36223623
// get cursor
36233624
var cursor = editor.getCursor();
36243625
// set element cursor data
3625-
if (!dropdown.hasClass('other-cursor'))
3626-
dropdown.addClass('other-cursor');
3626+
if (!dropdown.hasClass('CodeMirror-other-cursor'))
3627+
dropdown.addClass('CodeMirror-other-cursor');
36273628
dropdown.attr('data-line', cursor.line);
36283629
dropdown.attr('data-ch', cursor.ch);
36293630
// get coord position

public/vendor/codemirror/codemirror.min.js

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/vendor/codemirror/lib/codemirror.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
/* Can style cursor different in overwrite (non-insert) mode */
8787
.CodeMirror-overwrite .CodeMirror-cursor {}
8888

89+
.CodeMirror-other-cursor {
90+
width: 0px;
91+
position: absolute;
92+
border-right: none;
93+
}
94+
8995
.cm-tab { display: inline-block; text-decoration: inherit; }
9096

9197
.CodeMirror-rulers {
@@ -316,6 +322,11 @@ div.CodeMirror-dragcursors {
316322
visibility: visible;
317323
}
318324

325+
div.CodeMirror-other-cursors {
326+
position:relative;
327+
z-index:3;
328+
}
329+
319330
.CodeMirror-selected { background: #d9d9d9; }
320331
.CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; }
321332
.CodeMirror-crosshair { cursor: crosshair; }
@@ -338,6 +349,9 @@ div.CodeMirror-dragcursors {
338349
.CodeMirror div.CodeMirror-cursors {
339350
visibility: hidden;
340351
}
352+
.CodeMirror div.CodeMirror-other-cursors {
353+
visibility: hidden;
354+
}
341355
}
342356

343357
/* See issue #2901 */

public/vendor/codemirror/lib/codemirror.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@
23302330
for (var i = 0; i < doc.sel.ranges.length; i++) {
23312331
if (primary === false && i == doc.sel.primIndex) continue;
23322332
var range = doc.sel.ranges[i];
2333-
//if (range.from().line >= cm.display.viewTo || range.to().line < cm.display.viewFrom) continue;
2333+
if (!cm.options.otherCursors && (range.from().line >= cm.display.viewTo || range.to().line < cm.display.viewFrom)) continue;
23342334
var collapsed = range.empty();
23352335
if (collapsed || cm.options.showCursorWhenSelecting)
23362336
drawSelectionCursor(cm, range.head, curFragment);
@@ -2357,16 +2357,20 @@
23572357
otherCursor.style.top = pos.other.top + "px";
23582358
otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px";
23592359
}
2360-
2361-
$('.other-cursor').each(function(key, value) {
2362-
var line = parseInt($(value).attr('data-line'));
2363-
var ch = parseInt($(value).attr('data-ch'));
2364-
var offsetLeft = parseFloat($(value).attr('data-offset-left'));
2365-
var offsetTop = parseFloat($(value).attr('data-offset-top'));
2366-
var coord = cm.charCoords({line: line, ch: ch}, 'windows');
2367-
$(value)[0].style.left = coord.left + offsetLeft + 'px';
2368-
$(value)[0].style.top = coord.top + offsetTop + 'px';
2369-
});
2360+
2361+
if (cm.options.otherCursors) {
2362+
var others = cm.display.lineSpace.getElementsByClassName('CodeMirror-other-cursor');
2363+
for (var i = 0, l = others.length; i < l; i++) {
2364+
var other = others[i];
2365+
var line = parseInt(other.getAttribute('data-line'));
2366+
var ch = parseInt(other.getAttribute('data-ch'));
2367+
var offsetLeft = parseFloat(other.getAttribute('data-offset-left'));
2368+
var offsetTop = parseFloat(other.getAttribute('data-offset-top'));
2369+
var coord = cm.charCoords({line: line, ch: ch}, 'windows');
2370+
other.style.left = coord.left + offsetLeft + 'px';
2371+
other.style.top = coord.top + offsetTop + 'px';
2372+
}
2373+
}
23702374
}
23712375

23722376
// Draws the given range as a highlighted selection

0 commit comments

Comments
 (0)