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; global $sql;
$query = "UPDATE active SET position=? WHERE path=? AND username=?;"; $positions = json_decode( $positions, true );
$bind_variables = array( $_POST["position"], $this->path, $this->username ); $query = "";
$return = $sql->query( $query, $bind_variables, 0, "rowCount" ); $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(); $Active->MarkFileAsFocused();
} }
if ($_GET['action']=='save_position') { if ($_GET['action']=='save_positions') {
ignore_user_abort( true );
$Active->username = $_SESSION['user']; $Active->username = $_SESSION['user'];
$Active->path = $_POST['path']; $Active->savePositions( $_POST["positions"] );
$Active->savePosition();
} }

View file

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

View file

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

View file

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