2022-11-29 15:14:15 +00:00
|
|
|
|
let saveContent = function ($textarea) {
|
|
|
|
|
$textarea = $textarea || $('textarea');
|
|
|
|
|
|
|
|
|
|
var content = $textarea.val();
|
|
|
|
|
|
|
|
|
|
$.ajax('/api/save', {
|
|
|
|
|
data: content,
|
|
|
|
|
contentType: 'text/plain',
|
|
|
|
|
type: 'POST'
|
|
|
|
|
}).done(function (data) {
|
|
|
|
|
|
2022-12-01 02:33:41 +00:00
|
|
|
|
// show 'saved' indicator
|
|
|
|
|
$('#saved-indicator').addClass('show').delay(800).queue(function (next) {
|
|
|
|
|
$(this).removeClass('show');
|
|
|
|
|
next();
|
|
|
|
|
});
|
2022-11-29 15:14:15 +00:00
|
|
|
|
|
|
|
|
|
}).fail(function () {
|
|
|
|
|
alert('Could not connect to server. Check your internet connection and try again.');
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
|
|
2022-12-01 02:53:54 +00:00
|
|
|
|
// save after half-second delay after typing
|
2022-12-01 02:33:41 +00:00
|
|
|
|
var timer = null;
|
|
|
|
|
$('textarea').keyup(function () {
|
|
|
|
|
clearTimeout(timer);
|
|
|
|
|
timer = setTimeout(function () {
|
|
|
|
|
saveContent();
|
|
|
|
|
}, 500);
|
2022-11-29 15:14:15 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 20 second interval
|
2022-12-01 02:53:54 +00:00
|
|
|
|
// setInterval(saveContent, 60000);
|
2022-11-29 15:14:15 +00:00
|
|
|
|
|
|
|
|
|
// support tab key in textarea
|
|
|
|
|
$('textarea').keydown(function (e) {
|
|
|
|
|
if (e.keyCode === 9) { // tab was pressed
|
|
|
|
|
// get caret position/selection
|
|
|
|
|
var start = this.selectionStart;
|
|
|
|
|
end = this.selectionEnd;
|
|
|
|
|
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
|
|
// set textarea value to: text before caret + tab + text after caret
|
|
|
|
|
$this.val($this.val().substring(0, start)
|
|
|
|
|
+ '\t'
|
|
|
|
|
+ $this.val().substring(end));
|
|
|
|
|
|
|
|
|
|
// put caret at right position again
|
|
|
|
|
this.selectionStart = this.selectionEnd = start + 1;
|
|
|
|
|
|
|
|
|
|
// prevent the focus lose
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|