Notes/wwwroot/js/site.js

105 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-01-05 13:46:01 -05:00
let connection = new signalR.HubConnectionBuilder()
2023-01-26 16:44:50 -05:00
.withAutomaticReconnect()
2023-03-09 22:06:21 -05:00
.withUrl(`/noteHub?noteName=${noteName}`)
2023-01-26 16:44:50 -05:00
.build();
2022-11-29 10:14:15 -05:00
2023-01-31 09:35:24 -05:00
function start() {
connection.start().then(function () {
console.log('Started websocket listener');
}).catch(function (err) {
console.error(err.toString());
2024-01-25 20:12:50 -05:00
location.reload();
});
}
2022-11-29 10:14:15 -05:00
2023-01-31 09:35:24 -05:00
function showToast(selector) {
2022-11-29 10:14:15 -05:00
2023-01-25 08:48:50 -05:00
const cssClass = 'show';
2022-11-29 10:14:15 -05:00
2023-01-25 08:48:50 -05:00
// show 'saved' indicator
$(selector).addClass(cssClass).delay(800).queue(function (next) {
$(this).removeClass(cssClass);
next();
});
}
2023-01-31 09:35:24 -05:00
function saveContent($textarea) {
2023-01-25 08:48:50 -05:00
$textarea = $textarea || $('textarea');
var content = $textarea.val();
2025-01-09 11:41:18 -05:00
connection.invoke('SaveNote', noteName, content).then(function () {
2023-01-25 08:48:50 -05:00
showToast('#saved-indicator');
2023-01-26 11:13:12 -05:00
}).catch(function (err) {
console.error(err.toString());
setTimeout(start, 2000);
2022-11-29 10:14:15 -05:00
});
};
// start the signalr connection
start();
2022-11-29 10:14:15 -05:00
$(function () {
2023-01-31 09:35:24 -05:00
const $textarea = $('textarea');
2023-01-25 08:48:50 -05:00
2023-01-31 09:35:36 -05:00
// set focus on load
$textarea.focus();
2023-01-25 08:48:50 -05:00
// update content upon sync save
connection.on('updateNote', function (content) {
$textarea.val(content);
showToast('#update-indicator');
});
2023-07-28 20:19:59 +00:00
2023-01-25 08:48:50 -05:00
// update content after reconnected
connection.onreconnected(function() {
$.get(`api/note/${noteName}`, function(content) {
$textarea.val(content);
showToast('#update-indicator');
console.log('Refreshed after disconnect');
});
});
2023-01-31 09:35:24 -05:00
let timer = null;
2023-01-25 08:48:50 -05:00
2023-01-31 09:35:24 -05:00
const ignoredKeyCodes = [17, 18, 20, 27, 37, 38, 39, 40, 91];
2023-01-09 15:54:20 -05:00
2023-01-11 10:57:23 -05:00
// save after a second delay after typing
2023-01-25 08:48:50 -05:00
$textarea.keyup(function (e) {
2022-12-02 15:18:04 -05:00
// only set timer if key isn't ignored
if (!ignoredKeyCodes.includes(e.keyCode)) {
2023-01-09 15:54:20 -05:00
clearTimeout(timer);
2022-12-02 15:18:04 -05:00
timer = setTimeout(function () {
saveContent();
}, 1000);
}
2022-11-29 10:14:15 -05:00
});
// support tab key in textarea
2023-01-25 08:48:50 -05:00
$textarea.keydown(function (e) {
2022-11-29 10:14:15 -05:00
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)
2023-01-26 13:32:51 -05:00
+ ' '
2022-11-29 10:14:15 -05:00
+ $this.val().substring(end));
// put caret at right position again
2023-01-26 13:32:51 -05:00
this.selectionStart = this.selectionEnd = start + 2;
2022-11-29 10:14:15 -05:00
// prevent the focus lose
return false;
}
});
});