From 2fb008d19969aed88841e5c9e46e32ce8fb6b0d2 Mon Sep 17 00:00:00 2001 From: Chhekur <820121223505e@gmail.com> Date: Fri, 8 Mar 2019 22:06:34 +0530 Subject: [PATCH] fixed: remember only one unsaved file & add: remember your last working project & fixed: auto close when all files are closed & fixed: auto focus when create new file --- CHANGELOG.md | 6 +- assets/js/code_sharing.js | 118 ++++++++++++++++ assets/js/editor.js | 274 ++++++-------------------------------- assets/js/last_session.js | 123 +++++++++++++++++ main.js | 2 +- menu/functions.js | 42 ++++-- package-lock.json | 2 +- views/editor.html | 4 +- 8 files changed, 319 insertions(+), 252 deletions(-) create mode 100644 assets/js/code_sharing.js create mode 100644 assets/js/last_session.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 4842599..793c26d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ ## Changelog -- Fixed: Now you can stop running process, in case you run infinite process -- Improved: Buffer size increased \ No newline at end of file +- Fixed: Colon only able to remember one unsaved file +- Add: Colon remember you last working project +- Fixed: Auto close when all tabs are closed +- Fixed: Editor will auto focus when you create a new file \ No newline at end of file diff --git a/assets/js/code_sharing.js b/assets/js/code_sharing.js new file mode 100644 index 0000000..5853a98 --- /dev/null +++ b/assets/js/code_sharing.js @@ -0,0 +1,118 @@ +var server = require('http').createServer(); +var io = require('socket.io')(server); +var localtunnel = require('localtunnel'); +const io_client = require('socket.io-client'); +var socket; +var clients = []; + +function shareCode(){ + if(isCurrentFileSaved()){ + $('#code-sharing-box').html('
Waiting...
'); + server.listen(4000, function(){ + // console.log('listening at 4000'); + var tunnel = localtunnel(4000, function(err, tunnel) { + if (err) { + // console.log(err); + $('#code-sharing-box').html(err); + } + + // the assigned public url for your tunnel + // i.e. https://abcdefgjhij.localtunnel.me + $('#code-sharing-box').html('
Share this url with whome you want to share

'); + $('#code-sharing-box').append('
' + tunnel.url + '

'); + $('#code-sharing-box').append('

'); + $('#code-sharing-box').append(''); + // console.log(tunnel.url); + }); + tunnel.on('error', function(err) { + $('#code-sharing-box').html(err); + }); + }); + // console.log(file); + io.on('connection', function (socket) { + clients.push(socket); + socket.emit('file', { id: file.id, name:file.name, remote_path:file.path, data:file.editor.getValue() }); + socket.on('commit-changes', function (data) { + // console.log('hello'); + if(file.id == data.id && file.path == data.path){ + file.editor.setValue(data.data); + editor.setValue(data.data); + } + files['#' + data.id].editor.setValue(data.data); + }); + }); + } +} + +function connect(flag){ + if(!flag){ + $('#code-sharing-box').html('

'); + }else{ + let url = $('#url').val(); + $('#code-sharing-box').html('
Waiting...
'); + socket = io_client(url); + socket.on('file', function (data) { + remoteFile = data; + // newFileCount++; + // newTab(undefined, newFileCount, path.basename(data.remote_path), data.data); + openFile(data.data, undefined, data.name, data.remote_path); + // console.log(data); + // socket.emit('my other event', { my: 'data' }); + $('#code-sharing-box').html('

'); + $('#code-sharing-box').append(''); + }); + socket.on('commit-changes', function(data){ + // console.log('data recieved'); + // console.log(file.remote_path, remoteFile.remote_path); + if(file.remote_path == remoteFile.remote_path){ + file.editor.setValue(data.data); + editor.setValue(data.data); + // console.log('if executed'); + } + // console.log('after if'); + for(let i = 0; i < files.length; i++){ + if(files[i].remote_path == remoteFile.remote_path){ + // console.log('file found'); + files[i].editor.setValue(data.data); + } + } + }); + socket.on('error', function(error){ + $('#code-sharing-box').html(error); + }); + } +} + +function commitChangesSender(){ + for(let i = 0; i < clients.length; i++){ + clients[i].emit('commit-changes', {id:file.id, name:file.name, path:file.path, data:file.editor.getValue()}); + } +} + +function commitChanges(){ + socket.emit('commit-changes', {id:remoteFile.id, name:remoteFile.name, path:remoteFile.remote_path, data:file.editor.getValue()}); +} + +function backAllButtons(){ + $('#code-sharing-box').html('

'); +} + +function closeSocket(){ + socket.destroy(); + backAllButtons(); +} + +function disconnect(){ + server.close(); + backAllButtons(); +} + +module.exports = { + shareCode : shareCode, + connect : connect, + commitChangesSender : commitChangesSender, + commitChanges : commitChanges, + backAllButtons : backAllButtons, + closeSocket : closeSocket, + disconnect : disconnect +} \ No newline at end of file diff --git a/assets/js/editor.js b/assets/js/editor.js index d072c78..d9b565d 100644 --- a/assets/js/editor.js +++ b/assets/js/editor.js @@ -1,10 +1,5 @@ -var app = require('http').createServer(); -var io = require('socket.io')(app); -var localtunnel = require('localtunnel'); -const io_client = require('socket.io-client'); -var socket; -var clients = []; -// var socket = require('socket.io-client'); +const code_sharing = require('../assets/js/code_sharing.js'); +const last_session = require('../assets/js/last_session.js'); const ipc = require('electron').ipcRenderer; const {dialog} = require('electron').remote; @@ -83,39 +78,10 @@ CodeMirror.modeURL = "../src/editor/mode/%N/%N.js"; // var editor = ""; // var file = ""; var editor; -// checking is last session already there ? -// console.log(getUserDataPath()); -let last_session = fs.readFileSync(path.join(getUserDataPath(), '/last_session/info.json')); -last_session = JSON.parse(last_session); -// console.log(last_session,Object.keys(last_session).length); -if(Object.keys(last_session).length > 0){ - fs.writeFileSync(path.join(getUserDataPath() , '/last_session/info.json'), '{}'); - for (let i in last_session){ - // console.log('i - ', i); - let data = fs.readFileSync(path.join(getUserDataPath(), last_session[i].current_path), 'utf-8'); - openFile(data, last_session[i].original_path); - // console.log('file_id - ', '#' + file.id); - // console.log('#' + file.id, i); - if('#' + file.id != i){ - fs.unlinkSync(path.join(getUserDataPath(), last_session[i].current_path)); - // let temp_last_session = fs.readFileSync(path.join(getUserDataPath() , '/last_session/info.json')); - // temp_last_session = JSON.parse(temp_last_session); - // delete temp_last_session[i]; - // temp_last_session = JSON.stringify(temp_last_session, null, 2); - } - } - // for(i in last_session){ - // let data = fs.readFileSync(path.join(getUserDataPath(), last_session[i].current_path), 'utf-8'); - // openFile(data, last_session[i].original_path); - // } - // fs.writeFileSync('last_session/info.json','{}'); -}else{ - newFile(); - // editor = newTab(undefined , newFileCount , 'untitled', ''); - // var file = files['#new' + newFileCount]; - // $('#filename_new1').click(); -} +// checking is last session already there ? +last_session.checkLastSessionProjectAndOpen() +last_session.checkLastSessionFilesAndOpen(); // checking is last session already there end here @@ -150,7 +116,7 @@ function newTab(filepath, filecount ,filename, data, remote_path){ // file_id = filepath; // } $('#code_mirror_editors').append('
  • ' + filename + '
  • '); - $('#editors').append('
    '); + $('#editors').append('
    '); let temp = initEditor(document.getElementById('file_' + file_id)); temp.setValue(data); chagneEditorMode(filename,temp); @@ -165,11 +131,7 @@ function newTab(filepath, filecount ,filename, data, remote_path){ editor: temp } temp.on("change", function() { - let last_session = fs.readFileSync(path.join(getUserDataPath(), '/last_session/info.json')); - last_session = JSON.parse(last_session); - fs.writeFile(path.join(getUserDataPath(), last_session['#' + file.id].current_path), editor.getValue(), function(error){ - if(error) throw error; - }); + last_session.changeLastSessionFilesContent(file.id, editor.getValue()); closeToDot(); // // clearTimeout(delay); // updateHtmlPreview(); @@ -235,7 +197,7 @@ ipc.on('themeChanged', function(event){ function isFileAlreadyOpened(filepath){ for(i in files){ - if(files[i].path == filepath) return true; + if(files[i].path == filepath && filepath != undefined) return true; } return false; } @@ -266,19 +228,8 @@ function openFile(data, filepath, filename, remote_path){ //last session - let last_session = fs.readFileSync(path.join(getUserDataPath(), '/last_session/info.json')); - last_session = JSON.parse(last_session); - // console.log(last_session); - last_session['#new' + newFileCount] = { - original_path : filepath, - current_path : path.join('last_session','#new' + newFileCount) - } - fs.writeFile(path.join(getUserDataPath(), last_session['#new' + newFileCount].current_path), data, function(error){ - if(error) throw error; - }); - last_session = JSON.stringify(last_session, null, 2); - fs.writeFileSync(path.join(getUserDataPath(), '/last_session/info.json'),last_session); - + last_session.makeNewLastSessionFile(newFileCount, filepath, data); + // last session end here $('#filename_new'+newFileCount).click(); @@ -325,17 +276,7 @@ function closeCurrentFile(file){ // delete file from last session - let last_session = fs.readFileSync(path.join(getUserDataPath(), '/last_session/info.json')); - last_session = JSON.parse(last_session); - if(last_session['#new' + filecount]){ - fs.unlinkSync(path.join(getUserDataPath(), last_session['#new' + filecount].current_path)); - } - // fs.unlinkSync(path.join(__dirname, '..', last_session['#new' + filecount].current_path), function(error){ - // if(error) throw error; - // }); - delete last_session['#new' + filecount]; - last_session = JSON.stringify(last_session, null, 2); - fs.writeFileSync(path.join(getUserDataPath(), '/last_session/info.json'), last_session); + last_session.deleteLastSessionFile(file.id); // delete file from last session end here @@ -352,8 +293,14 @@ function closeCurrentFile(file){ // console.log($('#filename_' + nextFile.split('#')[1]).parent()); $('#filename_' + nextFile.split('#')[1]).click(); }else{ - - ipc.send('close-app'); + // console.log(files['#' + file.id].filepath, files); + if(files['#' + file.id].path != undefined){ + $('#file_tab_' + file.id).remove(); + $('#'+ file.id).remove(); + delete files['#'+file.id]; + newFile(); + } + // ipc.send('close-app'); } // console.log(files.hasOwnProperty(nextFile)); } @@ -379,14 +326,21 @@ function pathToId(filepath){ // open folder -ipc.on('openFolder', function(event,structure){ - let response = '