2013-09-12 23:25:25 +00:00
|
|
|
define([
|
|
|
|
"jquery",
|
|
|
|
"underscore",
|
2014-03-18 01:10:22 +00:00
|
|
|
"caret",
|
2013-09-12 23:25:25 +00:00
|
|
|
"crel",
|
|
|
|
"classes/Extension"
|
2014-03-18 01:10:22 +00:00
|
|
|
], function($, _, caret, 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
|
|
|
}
|
|
|
|
}
|
2014-03-18 01:10:22 +00:00
|
|
|
|
|
|
|
var $editorElt;
|
|
|
|
//var $positionHelper = $('<span>').css('display', 'inline-block');
|
|
|
|
var coef = 0.2;
|
|
|
|
function doFocus() {
|
|
|
|
setTimeout(function() {
|
|
|
|
if(!($editorElt && $editorElt[0].focused)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
var range = window.getSelection().getRangeAt(0);
|
|
|
|
range.insertNode($positionHelper[0]);
|
|
|
|
var parentNode = $positionHelper[0].parentNode;
|
|
|
|
*/
|
|
|
|
var editorHeight = $editorElt.height();
|
|
|
|
var cursorMinY = coef*editorHeight;
|
|
|
|
var cursorMaxY = (1-coef)*editorHeight;
|
|
|
|
var cursorY = $editorElt.caret('offset').top - $editorElt.offset().top;
|
2014-03-19 00:33:57 +00:00
|
|
|
//console.log($editorElt.find('.pre-content').caret('offset'));
|
|
|
|
//console.log(window.getSelection().getRangeAt(0).getBoundingClientRect());
|
2014-03-18 01:10:22 +00:00
|
|
|
//$positionHelper.detach();
|
|
|
|
//parentNode.normalize();
|
|
|
|
/*
|
|
|
|
if(cursorY < cursorMinY) {
|
|
|
|
$editorElt.scrollTop($editorElt.scrollTop() - cursorMinY + cursorY);
|
|
|
|
}
|
|
|
|
else if(cursorY > cursorMaxY) {
|
|
|
|
$editorElt.scrollTop($editorElt.scrollTop() + cursorY - cursorMaxY);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}, 0);
|
|
|
|
}
|
2013-09-12 23:25:25 +00:00
|
|
|
|
2014-03-18 01:10:22 +00:00
|
|
|
buttonFocusMode.onLayoutResize = doFocus;
|
|
|
|
|
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;
|
|
|
|
}
|
2014-03-18 01:10:22 +00:00
|
|
|
$editorElt = $('#wmd-input').on('keydown', function(event) {
|
2014-03-17 02:01:46 +00:00
|
|
|
if(event.altKey || event.ctrlKey || event.shiftKey || event.metaKey) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-18 01:10:22 +00:00
|
|
|
doFocus();
|
2013-09-12 23:25:25 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return buttonFocusMode;
|
2014-03-17 02:01:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|