import keymage from "keymage"; import io from 'socket.io-client'; import whiteboard from "./whiteboard"; import keybinds from "./keybinds"; import Picker from "vanilla-picker"; import { dom } from "@fortawesome/fontawesome-svg-core"; import pdfjsLib from "pdfjs-dist/webpack"; import shortcutFunctions from "./shortcutFunctions"; import ReadOnlyService from "./services/ReadOnlyService"; function main() { var whiteboardId = getQueryVariable("whiteboardid"); var randomid = getQueryVariable("randomid"); if (randomid && !whiteboardId) { //set random whiteboard on empty whiteboardid whiteboardId = Array(2).fill(null).map(() => Math.random().toString(36).substr(2)).join(''); const urlParams = new URLSearchParams(window.location.search); urlParams.set('whiteboardid', whiteboardId); window.location.search = urlParams; } whiteboardId = whiteboardId || "myNewWhiteboard"; whiteboardId = unescape(encodeURIComponent(whiteboardId)).replace(/[^a-zA-Z0-9 ]/g, ""); var myUsername = getQueryVariable("username"); var accessToken = getQueryVariable("accesstoken"); myUsername = myUsername || "unknown" + (Math.random() + "").substring(2, 6); accessToken = accessToken || ""; var accessDenied = false; // Custom Html Title var title = getQueryVariable("title"); if (!title === false) { document.title = decodeURIComponent(title); } var url = document.URL.substr(0, document.URL.lastIndexOf('/')); var signaling_socket = null; var urlSplit = url.split("/"); var subdir = ""; for (var i = 3; i < urlSplit.length; i++) { subdir = subdir + '/' + urlSplit[i]; } signaling_socket = io("", { "path": subdir + "/ws-api" }); // Connect even if we are in a subdir behind a reverse proxy signaling_socket.on('connect', function () { console.log("Websocket connected!"); let messageReceivedCount = 0; signaling_socket.on('drawToWhiteboard', function (content) { whiteboard.handleEventsAndData(content, true); $('#messageReceivedCount')[0].innerText = String(messageReceivedCount++); }); signaling_socket.on('refreshUserBadges', function () { whiteboard.refreshUserBadges(); }); signaling_socket.on('wrongAccessToken', function () { if (!accessDenied) { accessDenied = true; showBasicAlert("Access denied! Wrong accessToken!") } }); signaling_socket.on('updateSmallestScreenResolution', function (widthHeight) { whiteboard.updateSmallestScreenResolution(widthHeight["w"], widthHeight["h"]); }); signaling_socket.emit('joinWhiteboard', { wid: whiteboardId, at: accessToken, windowWidthHeight: { w: $(window).width(), h: $(window).height() } }); }); $(document).ready(function () { // by default set in readOnly mode ReadOnlyService.activateReadOnlyMode(); if (getQueryVariable("webdav") == "true") { $("#uploadWebDavBtn").show(); } let messageSentCount = 0; whiteboard.loadWhiteboard("#whiteboardContainer", { //Load the whiteboard whiteboardId: whiteboardId, username: btoa(myUsername), sendFunction: function (content) { if (ReadOnlyService.readOnlyActive) return; //ADD IN LATER THROUGH CONFIG // if (content.t === 'cursor') { // if (whiteboard.drawFlag) return; // } content["at"] = accessToken; signaling_socket.emit('drawToWhiteboard', content); $('#messageSentCount')[0].innerText = String(messageSentCount++); } }); // request whiteboard from server $.get(subdir + "/api/loadwhiteboard", { wid: whiteboardId, at: accessToken }).done(function (data) { whiteboard.loadData(data) }); $(window).resize(function () { signaling_socket.emit('updateScreenResolution', { at: accessToken, windowWidthHeight: { w: $(window).width(), h: $(window).height() } }); }) /*----------------/ Whiteboard actions /----------------*/ var tempLineTool = false; var strgPressed = false; //Handle key actions $(document).on("keydown", function (e) { if (e.which == 16) { if (whiteboard.tool == "pen" && !strgPressed) { tempLineTool = true; whiteboard.ownCursor.hide(); if (whiteboard.drawFlag) { whiteboard.mouseup({ offsetX: whiteboard.prevPos.x, offsetY: whiteboard.prevPos.y }) shortcutFunctions.setTool_line(); whiteboard.mousedown({ offsetX: whiteboard.prevPos.x, offsetY: whiteboard.prevPos.y }) } else { shortcutFunctions.setTool_line(); } } whiteboard.pressedKeys["shift"] = true; //Used for straight lines... } else if (e.which == 17) { strgPressed = true; } //console.log(e.which); }); $(document).on("keyup", function (e) { if (e.which == 16) { if (tempLineTool) { tempLineTool = false; shortcutFunctions.setTool_pen(); whiteboard.ownCursor.show(); } whiteboard.pressedKeys["shift"] = false; } else if (e.which == 17) { strgPressed = false; } }); //Load keybindings from keybinds.js to given functions Object.entries(keybinds).forEach(([key, functionName]) => { const associatedShortcutFunction = shortcutFunctions[functionName]; if (associatedShortcutFunction) { keymage(key, associatedShortcutFunction, { preventDefault: true }); } else { console.error("Function you want to keybind on key:", key, "named:", functionName, "is not available!") } }) // whiteboard clear button $("#whiteboardTrashBtn").click(function () { $("#whiteboardTrashBtnConfirm").show().focus(); $(this).css({ visibility: "hidden" }); }); $("#whiteboardTrashBtnConfirm").mouseout(function () { $(this).hide(); $("#whiteboardTrashBtn").css({ visibility: "inherit" }); }); $("#whiteboardTrashBtnConfirm").click(function () { $(this).hide(); $("#whiteboardTrashBtn").css({ visibility: "inherit" }); whiteboard.clearWhiteboard(); }); // undo button $("#whiteboardUndoBtn").click(function () { whiteboard.undoWhiteboardClick(); }); // redo button $("#whiteboardRedoBtn").click(function () { whiteboard.redoWhiteboardClick(); }); // view only $("#whiteboardLockBtn").click(() => { ReadOnlyService.deactivateReadOnlyMode(); }); $("#whiteboardUnlockBtn").click(() => { ReadOnlyService.activateReadOnlyMode(); }); $("#whiteboardUnlockBtn").hide(); $("#whiteboardLockBtn").show(); // switch tool $(".whiteboard-tool").click(function () { $(".whiteboard-tool").removeClass("active"); $(this).addClass("active"); var activeTool = $(this).attr("tool"); whiteboard.setTool(activeTool); if (activeTool == "mouse" || activeTool == "recSelect") { $(".activeToolIcon").empty(); } else { $(".activeToolIcon").html($(this).html()); //Set Active icon the same as the button icon } }); // upload image button $("#addImgToCanvasBtn").click(function () { if (ReadOnlyService.readOnlyActive) return; showBasicAlert("Please drag the image into the browser."); }); // save image as png $("#saveAsImageBtn").click(function () { var imgData = whiteboard.getImageDataBase64(); var w = window.open('about:blank'); //Firefox will not allow downloads without extra window setTimeout(function () { //FireFox seems to require a setTimeout for this to work. var a = document.createElement('a'); a.href = imgData; a.download = 'whiteboard.png'; w.document.body.appendChild(a); a.click(); w.document.body.removeChild(a); setTimeout(function () { w.close(); }, 100); }, 0); }); // save image to json containing steps $("#saveAsJSONBtn").click(function () { var imgData = whiteboard.getImageDataJson(); var w = window.open('about:blank'); //Firefox will not allow downloads without extra window setTimeout(function () { //FireFox seems to require a setTimeout for this to work. var a = document.createElement('a'); a.href = window.URL.createObjectURL(new Blob([imgData], { type: 'text/json' })); a.download = 'whiteboard.json'; w.document.body.appendChild(a); a.click(); w.document.body.removeChild(a); setTimeout(function () { w.close(); }, 100); }, 0); }); $("#uploadWebDavBtn").click(function () { if ($(".webdavUploadBtn").length > 0) { return; } var webdavserver = localStorage.getItem('webdavserver') || "" var webdavpath = localStorage.getItem('webdavpath') || "/" var webdavusername = localStorage.getItem('webdavusername') || "" var webdavpassword = localStorage.getItem('webdavpassword') || "" var webDavHtml = $('
' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '' + '
Server URL:
Path:path always have to start & end with "/"
Username:
Password:
Note: You have to generate and use app credentials if you have 2 Factor Auth activated on your dav/nextcloud server!
' + '
'); webDavHtml.find(".webdavUploadBtn").click(function () { var webdavserver = webDavHtml.find(".webdavserver").val(); localStorage.setItem('webdavserver', webdavserver); var webdavpath = webDavHtml.find(".webdavpath").val(); localStorage.setItem('webdavpath', webdavpath); var webdavusername = webDavHtml.find(".webdavusername").val(); localStorage.setItem('webdavusername', webdavusername); var webdavpassword = webDavHtml.find(".webdavpassword").val(); localStorage.setItem('webdavpassword', webdavpassword); var base64data = whiteboard.getImageDataBase64(); var webdavaccess = { webdavserver: webdavserver, webdavpath: webdavpath, webdavusername: webdavusername, webdavpassword: webdavpassword } webDavHtml.find(".loadingWebdavText").show(); webDavHtml.find(".webdavUploadBtn").hide(); saveWhiteboardToWebdav(base64data, webdavaccess, function (err) { if (err) { webDavHtml.find(".loadingWebdavText").hide(); webDavHtml.find(".webdavUploadBtn").show(); } else { webDavHtml.parents(".basicalert").remove(); } }); }) showBasicAlert(webDavHtml, { header: "Save to Webdav", okBtnText: "cancel", headercolor: "#0082c9" }) // render newly added icons dom.i2svg(); }); // upload json containing steps $("#uploadJsonBtn").click(function () { $("#myFile").click(); }); $("#shareWhiteboardBtn").click(function () { var url = window.location.href; var s = url.indexOf("&username=") !== -1 ? "&username=" : "username="; //Remove username from url var urlSlpit = url.split(s); var urlStart = urlSlpit[0]; if (urlSlpit.length > 1) { var endSplit = urlSlpit[1].split("&"); endSplit = endSplit.splice(1, 1); urlStart += "&" + endSplit.join("&"); } $("