fix autosave
This commit is contained in:
146
masks/write.html
146
masks/write.html
@@ -1,15 +1,15 @@
|
||||
<div id="write">
|
||||
<div id="write_feedback"></div>
|
||||
<div id="nav">
|
||||
<a class="fal fa-prev"></a>
|
||||
<span class="page_nb">1</span>
|
||||
<A class="fal fa-next"></a>
|
||||
</div>
|
||||
<div id="editor_container">
|
||||
<div id="editor_content">
|
||||
<div id="editor" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="nav">
|
||||
<div class="nav-elem"><a class="fal fa-fw fa-prev"></a></div>
|
||||
<div class="nav-elem"><span class="page_nb"></span></div>
|
||||
<div class="nav-elem"><a class="fal fa-fw fa-next"></a></div>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
oMyThoughts.pageInit = function(asHash, bFirstPage)
|
||||
@@ -18,6 +18,7 @@
|
||||
self.vars('default_text', "\n");
|
||||
self.vars('page', 0);
|
||||
self.vars('keystrokes', 0);
|
||||
self.vars('saving', false);
|
||||
|
||||
//DOM
|
||||
self.vars('editor', $('#editor_content'));
|
||||
@@ -30,32 +31,23 @@
|
||||
toolbar: [['bold', 'italic', 'underline'], [{ 'list': 'ordered'}, { 'list': 'bullet' }], ['clean']]
|
||||
}
|
||||
});
|
||||
/*oQuill.setContents([
|
||||
{insert: "So, I'm there, wondering about myself and my future - such a luxury by the way - when suddenly, someone approaches. mid-40s i'd say, elegant cold women, the kind you've learned not to talk to, to eventually avoid feeling repressed, or bad for yourself, or both."},
|
||||
{insert: '\n'},
|
||||
{insert: "Anyhow, it would seem that in this particular situation, the choice wasn't given to me to decide whether or not to talk to that precise women. She seats down next to me and engage a conversation. 'How is it you're here today?' I'm already regretting not having gone away when i still had the chance."},
|
||||
{insert: '\n'},
|
||||
{insert: "'Hello to you too' I say, still willing to show some manhood, even though I already know this women crushes manhood by pairs as a breakfast ritual."},
|
||||
{insert: '\n'},
|
||||
{insert: "In-extremist, I manage to babble some excuses about a rigorous lunch break time and leave the premises."},
|
||||
{insert: '\n'},
|
||||
{insert: "So, I'm there, wondering about myself and my future - such a luxury by the way - when suddenly, someone approaches. mid-40s i'd say, elegant cold women, the kind you've learned not to talk to, to eventually avoid feeling repressed, or bad for yourself, or both."},
|
||||
{insert: '\n'},
|
||||
{insert: "Anyhow, it would seem that in this particular situation, the choice wasn't given to me to decide whether or not to talk to that precise women. She seats down next to me and engage a conversation. 'How is it you're here today?' I'm already regretting not having gone away when i still had the chance."},
|
||||
{insert: '\n'},
|
||||
{insert: "'Hello to you too' I say, still willing to show some manhood, even though I already know this women crushes manhood by pairs as a breakfast ritual."},
|
||||
{insert: '\n'},
|
||||
{insert: "In-extremist, I manage to babble some excuses about a rigorous lunch break time and leave the premises."}
|
||||
]);*/
|
||||
|
||||
setLastContent();
|
||||
setPageHeight();
|
||||
//Add last thought
|
||||
setLastContent(oQuill, function(){
|
||||
setPageHeight();
|
||||
moveToPage(self.vars('page'));
|
||||
});
|
||||
|
||||
//Key strokes Events
|
||||
//Key strokes & mouse Events
|
||||
$('#editor').keydown(function(e){
|
||||
if($.inArray(e.which, [13, 37, 38, 39, 40]) != -1) onChange('', '', 'user', e);
|
||||
});
|
||||
oQuill.on('text-change', onChange);
|
||||
$(window).mousewheel(function(turn, iDelta) {
|
||||
var iNewPage = self.vars('page') + ((iDelta > 0)?-1:1);
|
||||
moveToPage(iNewPage);
|
||||
return false;
|
||||
});
|
||||
|
||||
//Page buttons
|
||||
$('.fa-prev').click(function(){moveToPage(self.vars('page')-1);});
|
||||
@@ -63,7 +55,6 @@
|
||||
|
||||
//Init
|
||||
oQuill.focus();
|
||||
moveToPage(self.vars('page'));
|
||||
};
|
||||
|
||||
oMyThoughts.onFeedback = function(sType, sMsg)
|
||||
@@ -87,16 +78,18 @@
|
||||
|
||||
function setPageHeight()
|
||||
{
|
||||
var $Page = $('#editor_container').css('height', 'calc(100% - 4em)');
|
||||
var $Page = $('#editor_container');
|
||||
var iHeight = $Page.height();
|
||||
|
||||
var iLineHeight = parseInt($('#editor_container p').css('line-height'));
|
||||
var iMaxHeight = Math.floor(iHeight / iLineHeight) * iLineHeight;
|
||||
$Page.height(iMaxHeight);
|
||||
$Page.height(iMaxHeight+'px');
|
||||
|
||||
self.vars('line-height', iLineHeight);
|
||||
self.vars('page_height', iMaxHeight);
|
||||
self.vars('page-height', iMaxHeight);
|
||||
}
|
||||
|
||||
function setLastContent(oQuill)
|
||||
function setLastContent(oQuill, fCallback)
|
||||
{
|
||||
getInfo
|
||||
(
|
||||
@@ -104,8 +97,8 @@
|
||||
function(sDesc, asData)
|
||||
{
|
||||
self.vars('id', asData.id);
|
||||
|
||||
oQuill.setContents(asData.ops);
|
||||
if(typeof fCallback == 'function') fCallback();
|
||||
},
|
||||
{id: 'last'}
|
||||
);
|
||||
@@ -119,8 +112,8 @@
|
||||
if(range)
|
||||
{
|
||||
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)};
|
||||
var oEditorCurBound = { top: self.vars('page-height') * self.vars('page'),
|
||||
bottom: self.vars('page-height') * (self.vars('page') + 1)};
|
||||
|
||||
//Anticipating arrows
|
||||
if(e)
|
||||
@@ -161,25 +154,30 @@
|
||||
|
||||
function moveToPage(iNewPage)
|
||||
{
|
||||
if(iNewPage!=self.vars('page'))
|
||||
var iContentHeight = self.vars('editor').height();
|
||||
var iLastPage = Math.floor(iContentHeight / self.vars('page-height'));
|
||||
|
||||
//console.log(iNewPage);
|
||||
|
||||
if(iNewPage >= 0 && iNewPage <= iLastPage)
|
||||
{
|
||||
var iOldPage = self.vars('page');
|
||||
self.vars('page', iNewPage);
|
||||
if(iNewPage!=self.vars('page'))
|
||||
{
|
||||
var iOldPage = self.vars('page');
|
||||
self.vars('page', iNewPage);
|
||||
|
||||
//Page Position
|
||||
var iOffset = self.vars('page') * self.vars('page-height') * -1;
|
||||
self.vars('editor').css('top', iOffset);
|
||||
}
|
||||
|
||||
//Page Number
|
||||
$('.page_nb').text(self.vars('page') + 1);
|
||||
|
||||
//Page Position
|
||||
var iOffset = self.vars('page') * self.vars('page_height') * -1;
|
||||
self.vars('editor').css('top', iOffset);
|
||||
//Detect First/Last Page
|
||||
$('.fa-prev').toggle(self.vars('page')!=0);
|
||||
$('.fa-next').toggle(self.vars('page')!=iLastPage);
|
||||
}
|
||||
|
||||
//Detect First Page
|
||||
$('.fa-prev').fadeTo('fast', (self.vars('page')==0)?0:1);
|
||||
|
||||
//Detect last page
|
||||
var bLastPage = (self.vars('editor').height() < self.vars('page_height') * (self.vars('page') + 1));
|
||||
$('.fa-next').fadeTo('fast', bLastPage?0:1);
|
||||
}
|
||||
|
||||
function incKeyStrokes()
|
||||
@@ -190,28 +188,40 @@
|
||||
|
||||
function save()
|
||||
{
|
||||
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.onFeedback('notice', 'Saved ('+asData.led.substr(11, 5)+')');
|
||||
},
|
||||
{
|
||||
id: self.vars('id'),
|
||||
content: sContent
|
||||
},
|
||||
function(sError)
|
||||
{
|
||||
oMyThoughts.onFeedback('error', 'Not saved! An error occured: '+sError);
|
||||
},
|
||||
'POST'
|
||||
);
|
||||
if(typeof oSaveTimer != 'undefined') clearTimeout(oSaveTimer);
|
||||
|
||||
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'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user