local fonts & fix page switch (selection)

This commit is contained in:
2018-10-31 21:18:16 +01:00
parent d09f0fd49f
commit 427c46a1b8
15 changed files with 252 additions and 67 deletions

View File

@@ -35,12 +35,14 @@
//Add last thought
setLastContent(oQuill, function(){
setPageHeight();
moveToPage(self.vars('page'));
moveToPage('last');
});
//Key strokes & mouse Events
$('#editor').keydown(function(e){
if($.inArray(e.which, [13, 37, 38, 39, 40]) != -1) onChange('', '', 'user', e);
else if(e.which==33) $('.fa-prev').click();
else if(e.which==34) $('.fa-next').click();
});
oQuill.on('text-change', onChange);
$(window).mousewheel(function(turn, iDelta) {
@@ -72,8 +74,7 @@
oMyThoughts.onQuitPage = function()
{
save();
return true;
return save(true);
};
function setPageHeight()
@@ -98,53 +99,91 @@
{
self.vars('id', asData.id);
oQuill.setContents(asData.ops);
//$('#context').append($('#editor .ql-editor').html());
//oQuill.setContents([]);
if(typeof fCallback == 'function') fCallback();
},
{id: 'last'}
);
}
function onChange(delta, oldDelta, source, e)
function onChange(delta, oldDelta, sSource, e)
{
if(source == 'user')
if(sSource == 'user')
{
var range = oQuill.getSelection();
if(range)
{
//sCursorPos = '';
var bSelection = (typeof e != 'undefined')?e.shiftKey:false;
var oSelBound = oQuill.getBounds(range.index, range.length);
var oEditorCurBound = { top: self.vars('page-height') * self.vars('page'),
bottom: self.vars('page-height') * (self.vars('page') + 1)};
//Anticipating arrows
//console.log('oEditorCurBound: top='+oEditorCurBound.top+' bottom='+oEditorCurBound.bottom);
//console.log('---------------------');
//console.log('range.length = '+range.length);
//console.log('oSelBound: top='+oSelBound.top+' bottom='+oSelBound.bottom);
//Detecting new selection & saving original line
if(!self.tmp('line') && bSelection) self.tmp('line', $.extend({}, oSelBound));
else if(!bSelection) self.tmp('line', false);
//Detecting navigating back to original line
var bReset = (self.tmp('line') && self.tmp('line').top == oSelBound.top && self.tmp('line').bottom == oSelBound.bottom);
//Anticipating arrows (downside of using keydown event)
if(e)
{
switch(e.which)
{
case 13: //Enter
case 40: //Down
if(bSelection) {
if(bReset) sCursorPos = 'last';
if(sCursorPos == 'last') { //Downwards selection, expanding
oSelBound.bottom += self.vars('line-height');
}
else { //Upwards selection, reducing
oSelBound.top += self.vars('line-height');
}
}
else oSelBound.bottom += self.vars('line-height');
break;
case 40: //down
oSelBound.bottom += self.vars('line-height');
case 38: //Up
if(bSelection) {
if(bReset) sCursorPos = 'first';
if(sCursorPos == 'last') { //Downwards selection, reducing
oSelBound.bottom -= self.vars('line-height');
}
else { //Upwards selection, expanding
oSelBound.top -= self.vars('line-height');
}
}
else oSelBound.top = Math.max(0, oSelBound.top - self.vars('line-height'));
break;
case 38: //up
oSelBound.top = Math.max(0, oSelBound.top - self.vars('line-height'));
break;
case 37: // left
case 37: //Left
if(bReset && bSelection) sCursorPos = 'first';
oSelBound = oQuill.getBounds(Math.max(0, range.index - 1), range.length);
break;
case 39: // right
case 39: //Right
if(bReset && bSelection) sCursorPos = 'last';
oSelBound = oQuill.getBounds(range.index + 1, range.length);
break;
}
}
else incKeyStrokes();
//console.log('oSelBound: top='+oSelBound.top+' bottom='+oSelBound.bottom);
var sNewPage = self.vars('page');
if(oSelBound.top < oEditorCurBound.top)
{
if( oSelBound.top < oEditorCurBound.top && (!bSelection || sCursorPos == 'first') ||
oSelBound.bottom < oEditorCurBound.top && (!bSelection || sCursorPos == 'last')) {
sNewPage--;
}
else if(oSelBound.bottom > oEditorCurBound.bottom)
{
else if(oSelBound.bottom > oEditorCurBound.bottom && (!bSelection || sCursorPos == 'last') ||
oSelBound.top > oEditorCurBound.bottom && (!bSelection || sCursorPos == 'first')) {
sNewPage++;
}
moveToPage(sNewPage);
@@ -157,7 +196,7 @@
var iContentHeight = self.vars('editor').height();
var iLastPage = Math.floor(iContentHeight / self.vars('page-height'));
//console.log(iNewPage);
if(iNewPage=='last') iNewPage = iLastPage;
if(iNewPage >= 0 && iNewPage <= iLastPage)
{
@@ -183,45 +222,47 @@
function incKeyStrokes()
{
self.vars('keystrokes', self.vars('keystrokes') + 1);
if(self.vars('keystrokes') % 20 == 0) save();
save();
}
function save()
function save(bForce)
{
if(typeof oSaveTimer != 'undefined') clearTimeout(oSaveTimer);
var bSave = (self.vars('keystrokes') % 20 == 0 || bForce);
if(!self.vars('saving') && self.vars('keystrokes') != self.vars('lastKeystrokes')) {
self.vars('saving', true);
var iCurrentKeystrokes = self.vars('keystrokes');
var sContent = oQuill.getContents().ops;
if(sContent[0] != self.vars('default_text'))
{
oMyThoughts.onFeedback('info', 'Saving...');
getInfo
(
'update',
function(sDesc, asData)
{
self.vars('id', asData.id);
oMyThoughts.feedback('notice', 'Saved ('+asData.led.substr(11, 5)+')');
self.vars('saving', false);
self.vars('lastKeystrokes', iCurrentKeystrokes);
oSaveTimer = setTimeout(save, 1000*10);
},
{
id: self.vars('id'),
content: sContent
},
function(sError)
{
oMyThoughts.feedback('error', 'Not saved! An error occured: '+sError);
self.vars('saving', false);
oSaveTimer = setTimeout(save, 1000);
},
'POST'
);
if(bSave) {
if(self.vars('saving')) {
oSaveTimer = setTimeout(function(){save(true);}, 500);
}
else {
self.vars('saving', true);
var sContent = oQuill.getContents().ops;
if(sContent[0] != self.vars('default_text')) {
oMyThoughts.onFeedback('info', 'Saving...');
getInfo(
'update',
function(sDesc, asData) {
self.vars('id', asData.id);
oMyThoughts.feedback('notice', 'Saved ('+asData.led.substr(11, 5)+')');
self.vars('saving', false);
},
{
id: self.vars('id'),
content: sContent
},
function(sError) {
oMyThoughts.feedback('error', 'Not saved! An error occured: '+sError);
self.vars('saving', false);
oSaveTimer = setTimeout(save, 1000);
},
'POST'
);
}
}
}
else {
oSaveTimer = setTimeout(function(){save(true);}, 1000*10);
}
return true;
}
</script>