Add project files.
This commit is contained in:
53
wwwroot/js/site.js
Normal file
53
wwwroot/js/site.js
Normal file
@ -0,0 +1,53 @@
|
||||
let saveContent = function ($textarea) {
|
||||
$textarea = $textarea || $('textarea');
|
||||
|
||||
var content = $textarea.val();
|
||||
|
||||
$.ajax('/api/save', {
|
||||
data: content,
|
||||
contentType: 'text/plain',
|
||||
type: 'POST'
|
||||
}).done(function (data) {
|
||||
|
||||
// saved!
|
||||
|
||||
}).fail(function () {
|
||||
alert('Could not connect to server. Check your internet connection and try again.');
|
||||
});
|
||||
};
|
||||
|
||||
$(function () {
|
||||
|
||||
// bind on blur event
|
||||
$('textarea').on('blur', function () {
|
||||
saveContent($(this));
|
||||
});
|
||||
|
||||
// 20 second interval
|
||||
setInterval(saveContent, 20000);
|
||||
|
||||
//onbeforeunload = saveContent();
|
||||
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user