Optimized saving of cursor positions.

This commit is contained in:
xevidos 2019-04-17 10:42:43 -04:00
parent 06765e5884
commit e4de3fa871
5 changed files with 81 additions and 52 deletions

View file

@ -192,16 +192,30 @@ class Active extends Common {
}
}
public function savePosition() {
public function savePositions( $positions ) {
global $sql;
$query = "UPDATE active SET position=? WHERE path=? AND username=?;";
$bind_variables = array( $_POST["position"], $this->path, $this->username );
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
$positions = json_decode( $positions, true );
$query = "";
$bind_variables = array();
if( $return > 0 ) {
if( json_last_error() == JSON_ERROR_NONE ) {
echo formatJSEND( "success" );
foreach( $positions as $path => $cursor ) {
$query .= "UPDATE active SET position=? WHERE path=? AND username=?;";
array_push( $bind_variables, json_encode( $cursor ), $path, $this->username );
}
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
if( $return > 0 ) {
exit( formatJSEND( "success" ) );
}
} else {
exit( formatJSEND( "success" ) );
}
}
}

View file

@ -86,8 +86,9 @@ if ($_GET['action']=='focused') {
$Active->MarkFileAsFocused();
}
if ($_GET['action']=='save_position') {
if ($_GET['action']=='save_positions') {
ignore_user_abort( true );
$Active->username = $_SESSION['user'];
$Active->path = $_POST['path'];
$Active->savePosition();
$Active->savePositions( $_POST["positions"] );
}

View file

@ -1,7 +1,8 @@
/*
* Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
* as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact.
* Copyright (c) Codiad & Kent Safranski (codiad.com),
* Isaac Brown ( telaaedifex.com ) distributed as-is and without
* warranty under the MIT License. See [root]/license.txt for more.
* This information must remain intact.
*/
(function(global, $) {
@ -37,8 +38,7 @@
// List of active file positions
positions: {},
position_timer: null,
//////////////////////////////////////////////////////////////////
//
// Check if a file is open.
@ -101,7 +101,7 @@
}
_this.add(path, session, focus);
if( ! ( _this.positions[`${path}`] === undefined ) ) {
if( ! ( _this.positions[`${path}`] === undefined ) && focus ) {
_this.setPosition( _this.positions[`${path}`] );
}
@ -148,7 +148,7 @@
_this.focus($(this).parent('li').attr('data-path'));
}
});
// Remove from list.
$('#list-active-files a>span')
.live('click', function(e) {
@ -282,9 +282,11 @@
});
}
});
// Prompt if a user tries to close window without saving all filess
window.onbeforeunload = function(e) {
codiad.active.uploadPositions();
if ($('#list-active-files li.changed')
.length > 0) {
var e = e || window.event;
@ -404,7 +406,12 @@
/* Check for users registered on the file. */
this.check(path);
if( ! ( this.positions[`${path}`] === undefined ) ) {
this.setPosition( this.positions[`${path}`] );
}
/* Notify listeners. */
amplify.publish('active.onFocus', path);
},
@ -992,35 +999,27 @@
}
},
uploadPositions: function() {
$.ajax({
type: 'POST',
url: codiad.active.controller + '?action=save_positions',
data: {
positions: ( JSON.stringify( codiad.active.positions ) )
},
success: function( data ) {
},
});
},
savePosition: function() {
let editor = codiad.editor.getActive();
let session = editor.getSession();
let path = session.path;
let position = this.getPosition( null, true );
let position = this.getPosition( path, true );
this.positions[path] = position;
setTimeout( function() {
if( ( codiad.active.position_timer + 500 ) > Date.now() ) {
return;
}
$.ajax({
type: 'POST',
url: codiad.active.controller + '?action=save_position',
data: {
path: path,
position: JSON.stringify( position )
},
success: function( data ) {
codiad.active.position_timer = Date.now();
},
});
}, 500);
},
getPosition: function( path=null, live=false ) {
@ -1052,6 +1051,16 @@
position = this.positions[path].position;
}
}
if( position == null ) {
position = {
column: 0,
row: 0
};
}
return position;
},

View file

@ -474,7 +474,7 @@
this.setSession(session, i);
this.changeListener(i);
//this.cursorTracking(i);
this.cursorTracking(i);
this.clickListener(i);
this.bindKeys(i);
@ -1272,17 +1272,19 @@
//////////////////////////////////////////////////////////////////
cursorTracking: function(i) {
i = i || this.getActive();
if (! i) return;
clearInterval(codiad._cursorPoll);
codiad._cursorPoll = setInterval(function() {
$('#cursor-position')
.html(i18n('Ln') + ': '
i.selection.on("changeCursor", function(e){
codiad.active.savePosition();
$('#cursor-position')
.html(i18n('Ln') + ': '
+ (i.getCursorPosition().row + 1)
+ ' · ' + i18n('Col') + ': '
+ i.getCursorPosition().column
);
}, 100);
);
});
},
//////////////////////////////////////////////////////////////////

View file

@ -1,18 +1,21 @@
/*
* Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
* as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact.
* Copyright (c) Codiad & Kent Safranski (codiad.com),
* Isaac Brown ( telaaedifex.com ) distributed as-is and without
* warranty under the MIT License. See [root]/license.txt for more.
* This information must remain intact.
*/
(function(global, $) {
var codiad = global.codiad;
$(function() {
codiad.poller.init();
});
codiad.poller = {
poller: null,
interval: 10000,
init: function() {
@ -20,11 +23,11 @@
let _this = this;
let interval = null;
setInterval( function() {
_this.poller = setInterval( function() {
_this.checkAuth();
_this.saveDrafts();
codiad.active.uploadPositions();
}, _this.interval);
},