From 98f1dbea6c5ebd44ca99061118b51c56073b20f6 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Thu, 22 Jul 2021 22:51:24 -0400 Subject: [PATCH] tweaks to events on hub start --- BinaryDad.Coding/wwwroot/js/hub.js | 41 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/BinaryDad.Coding/wwwroot/js/hub.js b/BinaryDad.Coding/wwwroot/js/hub.js index b15f83c..935fa33 100644 --- a/BinaryDad.Coding/wwwroot/js/hub.js +++ b/BinaryDad.Coding/wwwroot/js/hub.js @@ -8,6 +8,8 @@ let $yourNameText = $('#your-name-text'); let $yourColorText = $('#your-color-text'); + let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9'; + let isLive = false; let isLiveMode = function () { @@ -30,6 +32,16 @@ connection.invoke('SaveName', $yourNameText.val(), color); }; + let updateCode = function (editor, index) { + + let user = $yourNameText.val(); + let code = editor.getValue(); + + connection.invoke('UpdateCode', user, index, code, isTeacher).catch(function (err) { + return console.error(err.toString()); + }); + }; + let setHeight = function () { var editorHeight = $(window).height() - $('header').outerHeight() - $('footer').outerHeight(); @@ -45,8 +57,6 @@ isLiveMode(); setHeight(); - let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9'; - if (isTeacher) { $liveModeLink.hide(); $liveModeStatus.hide(); @@ -56,21 +66,6 @@ let $editors = $('.ace_editor'); - $editors.each(function (i, e) { - - let editor = ace.edit(e); - - editor.session.on('change', function () { - - let user = $yourNameText.val(); - let code = editor.getValue(); - - connection.invoke('UpdateCode', user, i, code, isTeacher).catch(function (err) { - return console.error(err.toString()); - }); - }); - }); - connection.on('ReceiveCode', function (user, index, code) { if (!isTeacher && isLive) { @@ -97,6 +92,18 @@ sendName(); + $editors.each(function (i, e) { + + let editor = ace.edit(e); + + editor.session.on('change', function () { + updateCode(editor, i); + }); + + // send initial code + updateCode(editor, i); + }); + }).catch(function (err) { return console.error(err.toString()); });