diff --git a/core/client.js b/core/client.js index 58893de..cb1bfed 100644 --- a/core/client.js +++ b/core/client.js @@ -5,7 +5,7 @@ const buttonCdownContainer = document.getElementById('button-container-cdown') const buttonCdowntimeContainer = document.getElementById('button-container-cdowntime') const buttonExtensibleContainer = document.getElementById('button-container-extensible') const buttonTimeContainer = document.getElementById('button-container-time') -const buttonClose = document.getElementById('stop-code') +const buttonClose = document.getElementById('stopCode') const buttonWiki = document.getElementById('button-wiki') const leftButtons = document.getElementById('left-button') const textVersion = document.getElementById('span-version') @@ -102,7 +102,7 @@ socket.addEventListener('close', (event) => { }) window.addEventListener('unload', (event) => { - socket.send(JSON.stringify({ action: 'stop-code' })) + socket.send(JSON.stringify({ action: 'stopCode' })) }) buttonClose.addEventListener('click', () => { diff --git a/core/index.html b/core/index.html index df3bd9c..77348c4 100644 --- a/core/index.html +++ b/core/index.html @@ -19,7 +19,7 @@
- { controlButton.addEventListener('click', () => { if (controlButton.textContent === translateElements.timer.buttons.start) { - socket.send(JSON.stringify({ action: 'startCdown', classElement })) + socket.send(JSON.stringify({ action: 'startTimer', classElement })) } else { socket.send(JSON.stringify({ action: 'pauseCdown', classElement })) } @@ -196,7 +196,7 @@ addContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('addtime-')) { - socket.send(JSON.stringify({ action: 'editTimeCdown', time: `+${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `+${data[1]}`, classElement })) } } }) @@ -212,7 +212,7 @@ subContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('subtime-')) { - socket.send(JSON.stringify({ action: 'editTimeCdown', time: `-${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `-${data[1]}`, classElement })) } } }) @@ -225,7 +225,7 @@ textMsg.addEventListener('focus', () => { }) textMsg.addEventListener('blur', () => { - socket.send(JSON.stringify({ action: 'editMsgCdown', msg: textMsg.textContent, classElement })) + socket.send(JSON.stringify({ action: 'editMsg', msg: textMsg.textContent, classElement })) if (textMsg.textContent === '') { textMsg.textContent = translateElements.timer.phMsgEnd textMsg.style.color = '#555' diff --git a/core/template/cdowntime/control/control.js b/core/template/cdowntime/control/control.js index fd6dcb6..621070f 100644 --- a/core/template/cdowntime/control/control.js +++ b/core/template/cdowntime/control/control.js @@ -151,7 +151,7 @@ timeData.addEventListener('change', () => { }) timezoneSelector.addEventListener('change', () => { - socket.send(JSON.stringify({ action: 'changeTimezoneCdownTime', timezone: timezoneSelector.value, classElement })) + socket.send(JSON.stringify({ action: 'changeTimezoneTime', timezone: timezoneSelector.value, classElement })) window.location.reload() }) @@ -167,7 +167,7 @@ addContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('addtime-')) { - socket.send(JSON.stringify({ action: 'editTimeCdownTime', time: `+${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `+${data[1]}`, classElement })) } } }) @@ -183,7 +183,7 @@ subContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('subtime-')) { - socket.send(JSON.stringify({ action: 'editTimeCdownTime', time: `-${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `-${data[1]}`, classElement })) } } }) @@ -196,7 +196,7 @@ textMsg.addEventListener('focus', () => { }) textMsg.addEventListener('blur', () => { - socket.send(JSON.stringify({ action: 'editMsgCdownTime', msg: textMsg.textContent, classElement })) + socket.send(JSON.stringify({ action: 'editMsg', msg: textMsg.textContent, classElement })) if (textMsg.textContent === '') { textMsg.textContent = translateElements.timer.phMsgEnd textMsg.style.color = '#555' diff --git a/core/template/crono/control/control.js b/core/template/crono/control/control.js index 7f21442..ea78d3f 100644 --- a/core/template/crono/control/control.js +++ b/core/template/crono/control/control.js @@ -140,7 +140,7 @@ languageSelector.addEventListener('change', () => { controlButton.addEventListener('click', () => { if (controlButton.textContent === translateElements.timer.buttons.start) { - socket.send(JSON.stringify({ action: 'startCrono', classElement })) + socket.send(JSON.stringify({ action: 'startTimer', classElement })) } else { socket.send(JSON.stringify({ action: 'pauseCrono', classElement })) } @@ -162,7 +162,7 @@ addContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('addtime-')) { - socket.send(JSON.stringify({ action: 'editTimeCrono', time: `+${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `+${data[1]}`, classElement })) } } }) @@ -178,7 +178,7 @@ subContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('subtime-')) { - socket.send(JSON.stringify({ action: 'editTimeCrono', time: `-${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `-${data[1]}`, classElement })) } } }) diff --git a/core/template/extensible/control/control.js b/core/template/extensible/control/control.js index af55c1f..a147b05 100644 --- a/core/template/extensible/control/control.js +++ b/core/template/extensible/control/control.js @@ -180,7 +180,7 @@ selectorLang.addEventListener('change', () => { controlButton.addEventListener('click', () => { if (controlButton.textContent === translateElements.timer.buttons.start) { - socket.send(JSON.stringify({ action: 'startExtensible', classElement })) + socket.send(JSON.stringify({ action: 'startTimer', classElement })) } else { socket.send(JSON.stringify({ action: 'pauseExtensible', classElement })) } @@ -218,7 +218,7 @@ addContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('addtime-')) { - socket.send(JSON.stringify({ action: 'editTimeExtensible', time: `+${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `+${data[1]}`, classElement })) } } }) @@ -234,7 +234,7 @@ subContainer.addEventListener('click', (event) => { const data = button.id.split('-') if (button.id.startsWith('subtime-')) { - socket.send(JSON.stringify({ action: 'editTimeExtensible', time: `-${data[1]}`, classElement })) + socket.send(JSON.stringify({ action: 'editTime', time: `-${data[1]}`, classElement })) } } }) @@ -255,7 +255,7 @@ textMsg.addEventListener('focus', () => { }) textMsg.addEventListener('blur', () => { - socket.send(JSON.stringify({ action: 'editMsgExtensible', msg: textMsg.textContent, classElement })) + socket.send(JSON.stringify({ action: 'editMsg', msg: textMsg.textContent, classElement })) if (textMsg.textContent === '') { textMsg.textContent = translateElements.timer.phMsgEnd textMsg.style.color = '#555' diff --git a/functions.js b/functions.js index 1cc9e13..4403519 100644 --- a/functions.js +++ b/functions.js @@ -128,8 +128,8 @@ exports.getFonts = () => { // Initialize configuration settings exports.initConfig = async () => { const Config = { - lang: await getLanguage(), - themeDark: await darkThemeCheck(), + lang: await getLanguage() || 'en', + themeDark: await darkThemeCheck() || false, version: await getVersion(), versionRelease: await getVersionRelease() } diff --git a/package.json b/package.json index 7fd3849..137cc26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obs-timer-controller", - "version": "1.0.4", + "version": "1.0.5", "description": "Handling browser fonts for OBS related to timers and countdowns.", "main": "server.js", "repository": { @@ -23,7 +23,10 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "node": "node . test", - "pkg-win": "electron-packager . OBS-Timer-Controller --platform=win32 --arch=x64 --app-version=1.0.4 --out=dist --overwrite --icon=icon.ico --author=\"XtoManuel\" --description=\"Handling browser fonts for OBS related to timers and countdowns.\"" + "pkg-win": "electron-packager . OBS-Timer-Controller --platform=win32 --arch=x64 --app-version=1.0.5 --out=dist --overwrite --icon=icon.ico --author=\"XtoManuel\" --description=\"Handling browser fonts for OBS related to timers and countdowns.\"", + "pkg-linux": "electron-packager . OBS-Timer-Controller --platform=linux --arch=x64 --app-version=1.0.5 --out=dist --overwrite --icon=icon.ico --author=\"XtoManuel\" --description=\"Handling browser fonts for OBS related to timers and countdowns.\"", + "pkg-mac": "electron-packager . OBS-Timer-Controller --platform=darwin --arch=x64 --app-version=1.0.5 --out=dist --overwrite --icon=icon.ico --author=\"XtoManuel\" --description=\"Handling browser fonts for OBS related to timers and countdowns.\"", + "pkg-all": "npm run pkg-win && npm run pkg-linux && npm run pkg-mac" }, "author": "XtoManuel", "license": "ISC", diff --git a/server.js b/server.js index d0d7863..20a23fd 100644 --- a/server.js +++ b/server.js @@ -66,271 +66,229 @@ let Config; saveConfig(Config) })() +const actions = { + reloadPage: () => sendToAllClients({ action: 'reload' }), + themeChange: (ws, data) => { + Config.themeDark = data.themeDark + saveConfig(Config) + sendToAllClients({ action: 'reload' }) + }, + langChange: (ws, data) => { + Config.lang = data.lang + saveConfig(Config) + sendToAllClients({ action: 'reload' }) + }, + startTimer: (ws, data) => startTimer(wss, GlobalVariables, data.classElement), + pauseCrono: (ws, data) => pauseCrono(wss, GlobalVariables, data.classElement), + pauseCdown: (ws, data) => pauseCdown(wss, GlobalVariables, data.classElement), + pauseExtensible: (ws, data) => pauseExtensible(wss, GlobalVariables, data.classElement), + resetCrono: (ws, data) => resetCrono(wss, GlobalVariables, data.classElement), + resetCdown: (ws, data) => resetCdown(wss, GlobalVariables, data.classElement), + resetExtensible: (ws, data) => resetExtensible(wss, GlobalVariables, data.classElement), + stopCdown: (ws, data) => stopCdown(wss, GlobalVariables, data.classElement), + stopExtensible: (ws, data) => stopExtensible(wss, GlobalVariables, data.classElement), + changeTimeCdown: (ws, data) => handleChangeTimeCdown(data), + changeTimeCdownTime: (ws, data) => handleChangeTimeCdownTime(data), + changeTimeExtensible: (ws, data) => handleChangeTimeExtensible(data), + changeTimezoneTime: (ws, data) => handleChangeTimezoneTime(data), + editMsg: (ws, data) => editMsg(data), + checkboxStopAdd: (ws, data) => changeCheckboxStopAdd(data), + checkboxPauseAdd: (ws, data) => changeCheckboxPauseAdd(data), + changeFormat: (ws, data) => changeFormat(data), + changeFormatExtCrono: (ws, data) => changeFormatExtCrono(data), + changeFormatExtCdown: (ws, data) => changeFormatExtCdown(data), + editTime: (ws, data) => editTime(data), + changeFont: (ws, data) => changeFont(data), + changeSize: (ws, data) => changeSize(data), + textFormat: (ws, data) => changeTextFormat(data), + align: (ws, data) => changeAlign(data), + changeColor: (ws, data) => changeColor(data), + getVariables: (ws, data) => sendVariableData(ws, GlobalVariables, Config, data.classElement), + createData: (ws, data) => createData(data), + removeData: (ws, data) => removeData(data), + stopCode: () => process.exit() +} + +function sendToAllClients (data) { + wss.clients.forEach((client) => { + if (client.readyState === WebSocket.OPEN) { + client.send(JSON.stringify(data)) + } + }) +} + +function handleChangeTimeCdown (data) { + GlobalVariables[data.classElement].textMilliseconds = data.time + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) + if (GlobalVariables[data.classElement].status === 'ended') { + resetCdown(wss, GlobalVariables, data.classElement) + } +} + +function handleChangeTimeCdownTime (data) { + GlobalVariables[data.classElement].endDatetime = data.time + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function handleChangeTimeExtensible (data) { + GlobalVariables[data.classElement].textMilliseconds = data.time + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) + if (GlobalVariables[data.classElement].status === 'ended') { + resetExtensible(wss, GlobalVariables, data.classElement) + } +} + +function handleChangeTimezoneTime (data) { + GlobalVariables[data.classElement].timezone = data.timezone + saveVariablesToYAML(GlobalVariables) + sendToAllClients({ action: 'reload' }) +} + +function editMsg (data) { + GlobalVariables[data.classElement].msgEnd = data.msg + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeCheckboxStopAdd (data) { + GlobalVariables[data.classElement].enableStopAdd = data.value + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeCheckboxPauseAdd (data) { + GlobalVariables[data.classElement].enablePauseAdd = data.value + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeFormat (data) { + const classElement = data.classElement + GlobalVariables[classElement].formatTime = data.format + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeFormatExtCrono (data) { + const classElement = data.classElement + GlobalVariables[classElement].formatTimeCrono = data.format + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeFormatExtCdown (data) { + const classElement = data.classElement + GlobalVariables[classElement].formatTimeCdown = data.format + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function editTime (data) { + const classElement = data.classElement + const status = GlobalVariables[classElement].status + const enablePauseAdd = GlobalVariables[classElement].enablePauseAdd + const enableStopAdd = GlobalVariables[classElement].enableStopAdd + + if ( + classElement.startsWith('crono') || + (classElement.startsWith('cdown') && !classElement.startsWith('cdowntime') && status !== 'ended') || + (classElement.startsWith('extensible') && ( + status === 'started' || + (status === 'paused' && enablePauseAdd) || + (status === 'stopped' && enableStopAdd) + )) + ) { + editTimeTimer(wss, GlobalVariables, data.time, classElement) + } else if (classElement.startsWith('cdowntime')) { + editTimeCdowntime(wss, GlobalVariables, data.time, classElement) + } +} + +function changeFont (data) { + GlobalVariables[data.classElement].font = data.font + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeSize (data) { + GlobalVariables[data.classElement].size = data.size + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeTextFormat (data) { + const classElement = data.classElement + switch (data.format) { + case 'bold': + GlobalVariables[classElement].bold = !GlobalVariables[classElement].bold + break + case 'italic': + GlobalVariables[classElement].italic = !GlobalVariables[classElement].italic + break + case 'underline': + GlobalVariables[classElement].underline = !GlobalVariables[classElement].underline + break + } + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeAlign (data) { + GlobalVariables[data.classElement].align = data.align + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function changeColor (data) { + GlobalVariables[data.classElement].colorText = data.color + sendToAllClients(GlobalVariables) + saveVariablesToYAML(GlobalVariables) +} + +function createData (data) { + const page = createDataYAML(GlobalVariables, data.classType) + fs.copy( + argsv[0] === 'test' ? `./core/template/${data.classType}` : `./resources/app/core/template/${data.classType}`, + argsv[0] === 'test' ? `./core/${page}` : `./resources/app/core/${page}` + ) + .then(() => { + console.log('Folder copied successfully.') + }) + .catch((err) => { + console.error('Error copying the folder:', err) + }) +} + +function removeData (data) { + delete GlobalVariables[data.remove] + fs.remove(argsv[0] === 'test' ? `./core/${data.remove}` : `./resources/app/core/${data.remove}`) + .then(() => { + // console.log(`Folder deleted successfully: ${folderToDelete}`); + }) + .catch((error) => { + console.error(`Error deleting the folder: ${error}`) + }) + saveVariablesToYAML(GlobalVariables) +} + // WebSocket connections handling wss.on('connection', (ws) => { - // Send the list of fonts to the client upon connection ws.send(JSON.stringify({ fonts: fontOptions })) ws.on('message', (message) => { const data = JSON.parse(message) - GlobalVariables = loadDataFromYAML((argsv[0] === 'test') ? './core/db.yaml' : './resources/app/core/db.yaml') - - if (data.action === 'reloadPage') { - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify({ action: 'reload' })) - } - }) - } else if (data.action === 'themeChange') { - Config.themeDark = data.themeDark - saveConfig(Config) - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify({ action: 'reload' })) - } - }) - } else if (data.action === 'langChange') { - Config.lang = data.lang - saveConfig(Config) - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify({ action: 'reload' })) - } - }) - } else if (data.action === 'startCrono' || data.action === 'startCdown' || data.action === 'startExtensible') { - // Start the timer when receiving the "start" action - startTimer(wss, GlobalVariables, data.classElement) - } else if (data.action === 'pauseCrono') { - // Pause the timer when receiving the "pause" action - pauseCrono(wss, GlobalVariables, data.classElement) - } else if (data.action === 'pauseCdown') { - // Pause the countdown when receiving the "pause" action - pauseCdown(wss, GlobalVariables, data.classElement) - } else if (data.action === 'pauseExtensible') { - // Pause the countdown when receiving the "pause" action - pauseExtensible(wss, GlobalVariables, data.classElement) - } else if (data.action === 'resetCrono') { - // Reset the timer when receiving the "reset" action - resetCrono(wss, GlobalVariables, data.classElement) - } else if (data.action === 'resetCdown') { - // Reset the countdown when receiving the "reset" action - resetCdown(wss, GlobalVariables, data.classElement) - } else if (data.action === 'resetExtensible') { - // Reset the countdown when receiving the "reset" action - resetExtensible(wss, GlobalVariables, data.classElement) - } else if (data.action === 'stopCdown') { - // Stop the countdown when receiving the "stop" action - stopCdown(wss, GlobalVariables, data.classElement) - } else if (data.action === 'stopExtensible') { - // Stop the countdown when receiving the "stop" action - stopExtensible(wss, GlobalVariables, data.classElement) - } else if (data.action === 'changeTimeCdown' && data.time) { - // Change the time format when receiving the "changeFormat" action - GlobalVariables[data.classElement].textMilliseconds = data.time - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - if (GlobalVariables[data.classElement].status === 'ended') { - resetCdown(wss, GlobalVariables, data.classElement) - } - } else if (data.action === 'changeTimeCdownTime' && data.time) { - // Change the end time format when receiving the "changeFormat" action - GlobalVariables[data.classElement].endDatetime = data.time - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'changeTimeExtensible' && data.time) { - // Change the time format when receiving the "changeFormat" action - GlobalVariables[data.classElement].textMilliseconds = data.time - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - if (GlobalVariables[data.classElement].status === 'ended') { - resetExtensible(wss, GlobalVariables, data.classElement) - } - } else if ( - data.action === 'changeTimezoneCdownTime' || - data.action === 'changeTimezoneTime' - ) { - // Change the time format when receiving the "changeFormat" action - GlobalVariables[data.classElement].timezone = data.timezone - // Transmit the updated format to all WebSocket clients - saveVariablesToYAML(GlobalVariables) - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify({ action: 'reload' })) - } - }) - } else if (data.action === 'editMsgCdown' || - data.action === 'editMsgCdownTime' || - data.action === 'editMsgExtensible') { - GlobalVariables[data.classElement].msgEnd = data.msg - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'checkboxStopAdd') { - // Change the text format when receiving the "changeFormat" - GlobalVariables[data.classElement].enableStopAdd = data.value - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'checkboxPauseAdd') { - // Change the text format when receiving the "changeFormat" - GlobalVariables[data.classElement].enablePauseAdd = data.value - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if ((data.action === 'changeFormat' || - data.action === 'changeFormatExtCrono' || - data.action === 'changeFormatExtCdown' - ) && data.format) { - const classElement = data.classElement - if (data.action === 'changeFormatExtCrono') { - GlobalVariables[classElement].formatTimeCrono = data.format - } else if (data.action === 'changeFormatExtCdown') { - GlobalVariables[classElement].formatTimeCdown = data.format - } else { - GlobalVariables[classElement].formatTime = data.format - } - - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - - saveVariablesToYAML(GlobalVariables) - } else if ( - data.action === 'editTimeCrono' || - (data.action === 'editTimeCdown' && GlobalVariables[data.classElement].status === 'ended') - ) { - editTimeTimer(wss, GlobalVariables, data.time, data.classElement) - } else if (data.action === 'editTimeCdownTime') { - editTimeCdowntime(wss, GlobalVariables, data.time, data.classElement) - } else if (data.action === 'editTimeExtensible' && ( - GlobalVariables[data.classElement].status === 'started' || - (GlobalVariables[data.classElement].status === 'paused' && GlobalVariables[data.classElement].enablePauseAdd) || - (GlobalVariables[data.classElement].status === 'stopped' && GlobalVariables[data.classElement].enableStopAdd)) - ) { - editTimeTimer(wss, GlobalVariables, data.time, data.classElement) - } else if (data.action === 'changeFont' && data.font) { - // Change the font format when receiving the "changeFormatr" - GlobalVariables[data.classElement].font = data.font - - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'changeSize' && data.size) { - // Change the font size when receiving the "changeFormat" - GlobalVariables[data.classElement].size = data.size - - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'textFormat') { - // Change the text format when receiving the "changeFormat" - switch (data.format) { - case 'bold': - GlobalVariables[data.classElement].bold = !GlobalVariables[data.classElement].bold - break - case 'italic': - GlobalVariables[data.classElement].italic = !GlobalVariables[data.classElement].italic - break - case 'underline': - GlobalVariables[data.classElement].underline = !GlobalVariables[data.classElement].underline - break - } - // Transmit the updated format to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'align') { - // Change the text alignment when receiving the "changeFormat" - GlobalVariables[data.classElement].align = data.align - - // Transmit the updated alignment to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'changeColor') { - // Change the text color when receiving the "changeFormat" - GlobalVariables[data.classElement].colorText = data.color - - // Transmit the updated color to all WebSocket clients - wss.clients.forEach((client) => { - if (client.readyState === WebSocket.OPEN) { - client.send(JSON.stringify(GlobalVariables)) - } - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'getVariables' && data.classElement) { - GlobalVariables = loadDataFromYAML((argsv[0] === 'test') ? './core/db.yaml' : './resources/app/core/db.yaml') - // The client requests variable data - sendVariableData(ws, GlobalVariables, Config, data.classElement) - } else if (data.action === 'createData' && data.classType) { - const page = createDataYAML(GlobalVariables, data.classType) - fs.copy((argsv[0] === 'test') ? `./core/template/${data.classType}` : `./resources/app/core/template/${data.classType}`, (argsv[0] === 'test') ? `./core/${page}` : `./resources/app/core/${page}`) - .then(() => { - console.log('Folder copied successfully.') - }) - .catch((err) => { - console.error('Error copying the folder:', err) - }) - } else if (data.action === 'removeData' && data.remove) { - delete GlobalVariables[data.remove] - fs.remove((argsv[0] === 'test') ? `./core/${data.remove}` : `./resources/app/core/${data.remove}`) - .then(() => { - // console.log(`Folder deleted successfully: ${folderToDelete}`); - }) - .catch((error) => { - console.error(`Error deleting the folder: ${error}`) - }) - saveVariablesToYAML(GlobalVariables) - } else if (data.action === 'stop-code') { - // Stop the Node.js server - process.exit() + GlobalVariables = loadDataFromYAML(argsv[0] === 'test' ? './core/db.yaml' : './resources/app/core/db.yaml') + + if (actions[data.action]) { + actions[data.action](ws, data) } + + ws.send(JSON.stringify(GlobalVariables)) }) - ws.send(JSON.stringify(GlobalVariables)) }) // Configure static routes for HTML files @@ -429,11 +387,20 @@ app.get('/:classElement/control&:request', (req, res) => { // Other server configuration (port, etc.) +function openUrlWhenConfigExists () { + if (Config) { + open.open(`http://localhost:${PORT}`) + } else { + // Config todavĂ­a no existe, espera y vuelve a verificar en un momento + setTimeout(openUrlWhenConfigExists, 1000) // Espera 1 segundo antes de verificar nuevamente + } +} + // Start the server on port 3000 const PORT = process.env.PORT || 3000 server.listen(PORT, () => { console.log(`Server is running on port ${PORT}`) // Open the default browser at 'http://localhost:3000' after starting the server using 'open'. - open.open(`http://localhost:${PORT}`) + openUrlWhenConfigExists() })