Stackedit/public/res/extensions/buttonFocusMode.js

43 lines
1.4 KiB
JavaScript
Raw Normal View History

2013-09-12 23:25:25 +00:00
define([
"jquery",
"underscore",
"crel",
"classes/Extension"
2014-03-19 22:10:59 +00:00
], function($, _, crel, Extension) {
2013-09-12 23:25:25 +00:00
2014-03-17 02:01:46 +00:00
var buttonFocusMode = new Extension("buttonFocusMode", 'Button "Focus Mode"', true, true);
2013-09-12 23:25:25 +00:00
buttonFocusMode.settingsBlock = "When typing, scrolls automatically the editor to always have the caret centered verticaly.";
2013-11-07 23:10:38 +00:00
var aceEditor;
2013-09-12 23:25:25 +00:00
buttonFocusMode.onAceCreated = function(aceEditorParam) {
aceEditor = aceEditorParam;
};
var isMouseActive = false;
function doFocusMode() {
2014-03-17 02:01:46 +00:00
if(aceEditor) {
if(isMouseActive === true) {
return;
}
var positionInDocument = aceEditor.selection.getCursor();
var positionInScreen = aceEditor.session.documentToScreenPosition(positionInDocument.row, positionInDocument.column);
aceEditor.session.setScrollTop((positionInScreen.row + 0.5) * aceEditor.renderer.lineHeight - aceEditor.renderer.$size.scrollerHeight / 2);
2013-09-12 23:25:25 +00:00
}
}
buttonFocusMode.onReady = function() {
2014-03-17 02:01:46 +00:00
if(aceEditor) {
aceEditor.getSession().selection.on('changeCursor', doFocusMode);
aceEditor.container.addEventListener('keydown', function() {
2013-09-12 23:25:25 +00:00
isMouseActive = false;
2014-03-17 02:01:46 +00:00
}, true);
aceEditor.container.addEventListener('mousedown', function() {
isMouseActive = true;
}, true);
return;
}
2013-09-12 23:25:25 +00:00
};
return buttonFocusMode;
2014-03-17 02:01:46 +00:00
});