From e5950d89ad820c15400e718e973ce2d3c280a5b5 Mon Sep 17 00:00:00 2001 From: franzz Date: Sat, 8 Dec 2018 00:49:56 +0100 Subject: [PATCH] fix auto save --- masks/write.html | 4 +-- scripts/mythoughts.js | 59 +++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/masks/write.html b/masks/write.html index 5f1ff4b..ffd1067 100755 --- a/masks/write.html +++ b/masks/write.html @@ -42,15 +42,15 @@ { if(typeof oSaveTimer != 'undefined') clearTimeout(oSaveTimer); var bSave = (oEditor.keystrokes % 20 == 0 || bForce); - + if(bSave) { if(self.tmp('saving')) { oSaveTimer = setTimeout(function(){save(true);}, 500); } else { - self.tmp('saving', true); var sContent = oEditor.getContent(); if(!oEditor.isEmpty() || oEditor.id != 0) { + self.tmp('saving', true); oMyThoughts.onFeedback('info', 'Saving...'); getInfo( 'update', diff --git a/scripts/mythoughts.js b/scripts/mythoughts.js index c942d54..b0f4da8 100755 --- a/scripts/mythoughts.js +++ b/scripts/mythoughts.js @@ -290,13 +290,18 @@ function MyThoughts(asGlobals) }; } -class ThoughtDate extends Quill.import('blots/inline') { +//Date format +const Inline = Quill.import('blots/inline'); +class ThoughtDate extends Inline { static create(value) { let node = super.create(); node.setAttribute('class', 'edi_header'); return node; } } +ThoughtDate.blotName = 'thought_date'; +ThoughtDate.tagName = 'div'; +Quill.register(ThoughtDate); class Editor { constructor(sContainerId, bReadOnly) { @@ -305,6 +310,7 @@ class Editor { this.keystrokes = 0; this.line = false; this.readOnly = bReadOnly || false; + this.sCursorPos = ''; //DOM Elements var sEditorId = 'edi'+Math.floor(Math.random() * 1000); @@ -316,11 +322,6 @@ class Editor { this.$NextBtn = $('.next'); this.onKeyStroke = function(e){}; - - //Date format - ThoughtDate.blotName = 'thought_date'; - ThoughtDate.tagName = 'div'; - Quill.register(ThoughtDate); this.oQuill = new Quill('#'+sEditorId, { theme: 'bubble', @@ -331,7 +332,12 @@ class Editor { } }); - //Key strokes & mouse Events + this._initEvents(); + this._postInit(); + } + + _initEvents() { + //Key strokes this.$Editor.keydown((e) => { if($.inArray(e.which, [13, 37, 38, 39, 40]) != -1) this._onChange('', '', 'user', e); else if(e.which==33) this.$PrevBtn.click(); @@ -339,8 +345,10 @@ class Editor { else this.onKeyStroke(e); }); + //On text modification this.oQuill.on('text-change', (delta, oldDelta, sSource) => {this._onChange(delta, oldDelta, sSource);}); + //Mouse wheel events $(window).mousewheel((turn, iDelta) => { var iNewPage = this.page + ((iDelta > 0)?-1:1); this.moveToPage(iNewPage); @@ -350,8 +358,13 @@ class Editor { //Page buttons this.$PrevBtn.click(() => {this.moveToPage(this.page - 1);}); this.$NextBtn.click(() => {this.moveToPage(this.page + 1);}); - - this._postInit(); + } + + _postInit(iPage) { + iPage = iPage || 0; + this._setPageHeight(); + this.moveToPage(iPage); + if(!this.readOnly) this.oQuill.focus(); } setHeader(sHeader) { @@ -385,20 +398,12 @@ class Editor { this.pageHeight = iMaxHeight; } - _postInit(iPage) { - iPage = iPage || 0; - this._setPageHeight(); - this.moveToPage(iPage); - if(!this.readOnly) this.oQuill.focus(); - } - _onChange(delta, oldDelta, sSource, e) { if(sSource == 'user') { var range = this.oQuill.getSelection(); if(range) { - //sCursorPos = ''; var bSelection = (typeof e != 'undefined')?e.shiftKey:false; var oSelBound = this.oQuill.getBounds(range.index, range.length); @@ -425,8 +430,8 @@ class Editor { case 13: //Enter case 40: //Down if(bSelection) { - if(bReset) sCursorPos = 'last'; - if(sCursorPos == 'last') { //Downwards selection, expanding + if(bReset) this.sCursorPos = 'last'; + if(this.sCursorPos == 'last') { //Downwards selection, expanding oSelBound.bottom += this.lineHeight; } else { //Upwards selection, reducing @@ -437,8 +442,8 @@ class Editor { break; case 38: //Up if(bSelection) { - if(bReset) sCursorPos = 'first'; - if(sCursorPos == 'last') { //Downwards selection, reducing + if(bReset) this.sCursorPos = 'first'; + if(this.sCursorPos == 'last') { //Downwards selection, reducing oSelBound.bottom -= this.lineHeight; } else { //Upwards selection, expanding @@ -448,11 +453,11 @@ class Editor { else oSelBound.top = Math.max(0, oSelBound.top - this.lineHeight); break; case 37: //Left - if(bReset && bSelection) sCursorPos = 'first'; + if(bReset && bSelection) this.sCursorPos = 'first'; oSelBound = this.oQuill.getBounds(Math.max(0, range.index - 1), range.length); break; case 39: //Right - if(bReset && bSelection) sCursorPos = 'last'; + if(bReset && bSelection) this.sCursorPos = 'last'; oSelBound = this.oQuill.getBounds(range.index + 1, range.length); break; } @@ -462,12 +467,12 @@ class Editor { //console.log('oSelBound: top='+oSelBound.top+' bottom='+oSelBound.bottom); var sNewPage = this.page; - if( oSelBound.top < oEditorCurBound.top && (!bSelection || sCursorPos == 'first') || - oSelBound.bottom < oEditorCurBound.top && (!bSelection || sCursorPos == 'last')) { + if( oSelBound.top < oEditorCurBound.top && (!bSelection || this.sCursorPos == 'first') || + oSelBound.bottom < oEditorCurBound.top && (!bSelection || this.sCursorPos == 'last')) { sNewPage--; } - else if(oSelBound.bottom > oEditorCurBound.bottom && (!bSelection || sCursorPos == 'last') || - oSelBound.top > oEditorCurBound.bottom && (!bSelection || sCursorPos == 'first')) { + else if(oSelBound.bottom > oEditorCurBound.bottom && (!bSelection || this.sCursorPos == 'last') || + oSelBound.top > oEditorCurBound.bottom && (!bSelection || this.sCursorPos == 'first')) { sNewPage++; } this.moveToPage(sNewPage);