Skip to content

Commit e089167

Browse files
committed
Added statusBar to show cursor position, selections and so on
1 parent 6e40df2 commit e089167

2 files changed

Lines changed: 122 additions & 2 deletions

File tree

public/css/index.css

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,47 @@ div[contenteditable]:empty:not(:focus):before{
238238

239239
.cm-trailing-space-new-line:before {
240240
content: '↵';
241+
242+
.status-bar {
243+
background: #1c1c1e;
244+
border-top: 1px solid #343434;
245+
color: #ccc;
246+
position: relative;
247+
display: block;
248+
box-sizing: border-box;
249+
font-size: 11px;
250+
line-height: 25px;
251+
height: 26px;
252+
overflow: hidden;
253+
}
254+
255+
.status-bar .status-info {
256+
color: #ccc;
257+
left: 10px;
258+
position: absolute;
259+
white-space: nowrap;
260+
max-width: 65%;
261+
overflow: hidden;
262+
text-overflow: ellipsis;
263+
}
264+
265+
.status-bar .status-info div {
266+
display: inline;
267+
}
268+
269+
.status-bar .status-file {
270+
color: #9a9a9a;
271+
}
272+
273+
.status-bar .status-indicators {
274+
background: #1c1c1e;
275+
color: #ccc;
276+
position: absolute;
277+
right: 10px;
278+
text-align: right;
279+
white-space: nowrap;
280+
max-width: 30%;
281+
overflow: hidden;
282+
text-overflow: ellipsis;
283+
}
241284
}

public/js/index.js

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,53 @@ var editor = CodeMirror.fromTextArea(textit, {
226226
var inlineAttach = inlineAttachment.editors.codemirror4.attach(editor);
227227
defaultTextHeight = parseInt($(".CodeMirror").css('line-height'));
228228

229+
var statusBar = null;
230+
var statusCursor = null;
231+
var statusFile = null;
232+
var statusIndicators = null;
233+
234+
function addStatusBar() {
235+
var element = '<div class="status-bar"><div class="status-info"><div class="status-cursor"></div><div class="status-file"></div></div><div class="status-indicators"></div></div>';
236+
statusBar = $(element);
237+
statusCursor = statusBar.find('.status-cursor');
238+
statusFile = statusBar.find('.status-file');
239+
statusIndicators = statusBar.find('.status-indicators');
240+
editor.addPanel(statusBar[0], {
241+
position: "bottom"
242+
});
243+
}
244+
245+
var selection = null;
246+
247+
function updateStatusBar() {
248+
if (!statusBar) return;
249+
var cursor = editor.getCursor();
250+
var cursorText = 'Line ' + (cursor.line + 1) + ', Columns ' + (cursor.ch + 1);
251+
if (selection) {
252+
var anchor = selection.anchor;
253+
var head = selection.head;
254+
var start = head.line <= anchor.line ? head : anchor;
255+
var end = head.line >= anchor.line ? head : anchor;
256+
var selectionText = ' — Selected ';
257+
var selectionCharCount = Math.abs(head.ch - anchor.ch);
258+
// borrow from brackets EditorStatusBar.js
259+
if (start.line !== end.line) {
260+
var lines = end.line - start.line + 1;
261+
if (end.ch === 0) {
262+
lines--;
263+
}
264+
selectionText += lines + ' lines';
265+
} else if (selectionCharCount > 0)
266+
selectionText += selectionCharCount + ' columns';
267+
if (start.line !== end.line || selectionCharCount > 0)
268+
cursorText += selectionText;
269+
}
270+
statusCursor.text(cursorText);
271+
var fileText = ' — ' + editor.lineCount() + ' Lines';
272+
statusFile.text(fileText);
273+
statusIndicators.text('Length ' + editor.getValue().length);
274+
}
275+
229276
//ui vars
230277
var ui = {
231278
spinner: $(".ui-spinner"),
@@ -454,11 +501,23 @@ function checkResponsive() {
454501
function checkEditorStyle() {
455502
var scrollbarStyle = editor.getOption('scrollbarStyle');
456503
if (scrollbarStyle == 'overlay' || currentMode == modeType.both) {
504+
//save last editor scroll top
505+
var lastTop = editor.getScrollInfo().top;
457506
ui.area.codemirror.css('height', '');
507+
//set editor size to keep status bar on the bottom
508+
editor.setSize(null, ui.area.edit.height());
509+
//restore last editor scroll top
510+
editor.scrollTo(null, lastTop);
458511
} else if (scrollbarStyle == 'native') {
459512
ui.area.codemirror.css('height', 'auto');
460513
$('.CodeMirror-gutters').css('height', $('.CodeMirror-sizer').height());
461514
}
515+
//set editor parent height to fill status bar
516+
if (statusBar)
517+
statusBar.parent().css('height', ui.area.edit.height() - statusBar.outerHeight() + 'px');
518+
//set sizer height to make it at least height as editor
519+
var editorSizerHeight = ui.area.edit.height() - (statusBar ? statusBar.outerHeight() : 0);
520+
$('.CodeMirror-sizer').css('height', editorSizerHeight + 'px');
462521
}
463522

464523
function checkTocStyle() {
@@ -589,6 +648,11 @@ function changeMode(type) {
589648
}
590649
if (currentMode == modeType.edit || currentMode == modeType.both) {
591650
ui.toolbar.uploadImage.fadeIn();
651+
//add and update status bar
652+
if (!statusBar) {
653+
addStatusBar();
654+
updateStatusBar();
655+
}
592656
} else {
593657
ui.toolbar.uploadImage.fadeOut();
594658
}
@@ -1300,12 +1364,15 @@ function checkCursorTag(coord, ele) {
13001364
var top = coord.top;
13011365
var offsetLeft = -3;
13021366
var offsetTop = defaultTextHeight;
1367+
var statusBarHeight = 0;
1368+
if (statusBar)
1369+
statusBarHeight = statusBar.outerHeight();
13031370
if (width > 0 && height > 0) {
13041371
if (left + width + offsetLeft > editorWidth - curosrtagMargin) {
13051372
offsetLeft = -(width + 10);
13061373
}
1307-
if (top + height + offsetTop > Math.max(viewportHeight, editorHeight) + curosrtagMargin && top - height > curosrtagMargin) {
13081374
offsetTop = -(height);
1375+
if (top + height + offsetTop > Math.max(editor.doc.height, editorHeight) + curosrtagMargin - statusBarHeight * 2 && top - height > curosrtagMargin) {
13091376
}
13101377
}
13111378
ele[0].style.left = offsetLeft + 'px';
@@ -1507,6 +1574,13 @@ var cursorActivityTimer = null;
15071574
editor.on('cursorActivity', function (cm) {
15081575
clearTimeout(cursorActivityTimer);
15091576
cursorActivityTimer = setTimeout(cursorActivity, cursorActivityDelay);
1577+
updateStatusBar();
1578+
editor.on('beforeSelectionChange', function (doc, selections) {
1579+
if (selections)
1580+
selection = selections.ranges[0];
1581+
else
1582+
selection = null;
1583+
updateStatusBar();
15101584
});
15111585

15121586
function cursorActivity() {
@@ -1813,10 +1887,13 @@ function checkCursorMenu() {
18131887
var top = coord.top;
18141888
var offsetLeft = 0;
18151889
var offsetTop = defaultTextHeight;
1890+
var statusBarHeight = 0;
1891+
if (statusBar)
1892+
statusBarHeight = statusBar.outerHeight();
18161893
if (left + width + offsetLeft > editorWidth - menuMargin)
18171894
offsetLeft = -(left + width - editorWidth + menuMargin);
1818-
if (top + height + offsetTop > Math.max(viewportHeight, editorHeight) + menuMargin && top - height > menuMargin) {
18191895
offsetTop = -(height + defaultTextHeight);
1896+
if (top + height + offsetTop > Math.max(editor.doc.height, editorHeight) + menuMargin - statusBarHeight * 2 && top - height > menuMargin) {
18201897
upSideDown = true;
18211898
} else {
18221899
upSideDown = false;

0 commit comments

Comments
 (0)