add signalr support
This commit is contained in:
@ -1,38 +1,53 @@
|
||||
let saveContent = function ($textarea) {
|
||||
let connection = new signalR.HubConnectionBuilder().withUrl("/noteHub").build();
|
||||
|
||||
connection.start().then(function () {
|
||||
console.log('Started websocket listener');
|
||||
}).catch(function (err) {
|
||||
return console.error(err.toString());
|
||||
});
|
||||
|
||||
let showToast = function (selector) {
|
||||
|
||||
const cssClass = 'show';
|
||||
|
||||
// show 'saved' indicator
|
||||
$(selector).addClass(cssClass).delay(800).queue(function (next) {
|
||||
$(this).removeClass(cssClass);
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
let saveContent = function ($textarea) {
|
||||
$textarea = $textarea || $('textarea');
|
||||
|
||||
var content = $textarea.val();
|
||||
|
||||
$.ajax('/api/save', {
|
||||
data: content,
|
||||
contentType: 'text/plain',
|
||||
type: 'POST'
|
||||
}).done(function (data) {
|
||||
|
||||
// show 'saved' indicator
|
||||
$('#saved-indicator').addClass('show').delay(800).queue(function (next) {
|
||||
$(this).removeClass('show');
|
||||
next();
|
||||
});
|
||||
|
||||
}).fail(function () {
|
||||
alert('Could not connect to server. Check your internet connection and try again.');
|
||||
connection.invoke('SaveNote', content).then(function () {
|
||||
showToast('#saved-indicator');
|
||||
});
|
||||
};
|
||||
|
||||
$(function () {
|
||||
|
||||
let $textarea = $('textarea');
|
||||
|
||||
// update content upon sync save
|
||||
connection.on('updateNote', function (content) {
|
||||
$textarea.val(content);
|
||||
showToast('#update-indicator');
|
||||
});
|
||||
|
||||
// set dark mode
|
||||
if (window.location.hash == '#dark') {
|
||||
$('textarea').addClass('dark');
|
||||
$textarea.addClass('dark');
|
||||
}
|
||||
|
||||
var timer = null;
|
||||
|
||||
|
||||
let ignoredKeyCodes = [17, 18, 20, 27, 37, 38, 39, 40, 91];
|
||||
|
||||
// save after a second delay after typing
|
||||
$('textarea').keyup(function (e) {
|
||||
$textarea.keyup(function (e) {
|
||||
|
||||
clearTimeout(timer);
|
||||
|
||||
@ -46,7 +61,7 @@ $(function () {
|
||||
});
|
||||
|
||||
// support tab key in textarea
|
||||
$('textarea').keydown(function (e) {
|
||||
$textarea.keydown(function (e) {
|
||||
if (e.keyCode === 9) { // tab was pressed
|
||||
// get caret position/selection
|
||||
var start = this.selectionStart;
|
||||
@ -66,5 +81,4 @@ $(function () {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user