fix auto save

This commit is contained in:
2018-12-08 00:49:56 +01:00
parent 389fd02f08
commit e5950d89ad
2 changed files with 34 additions and 29 deletions

View File

@@ -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);