From 796b7f3f1eeaee548a9df67e9a72bd849bc97afb Mon Sep 17 00:00:00 2001 From: Kumar Priyansh Date: Sat, 18 Jul 2020 01:02:26 +0530 Subject: Rename Chapter Function: Event Handling --- src/main.js | 20 +++++----- src/views/system/chapterRename.html | 49 +++++++++++++++++++++++++ src/views/templates/blank/template.html | 31 +++++++++++++++- src/views/templates/blank/template.js | 65 +++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 src/views/system/chapterRename.html diff --git a/src/main.js b/src/main.js index 9cc58cb..720f1c6 100644 --- a/src/main.js +++ b/src/main.js @@ -1,37 +1,30 @@ -const {app, BrowserWindow} = require('electron') +const {app, BrowserWindow, ipcMain} = require('electron') const path = require('path') const url = require('url') let win function createWindow () { - // Create the browser window. win = new BrowserWindow({ width: 800, height: 600, frame: false, - // transparent: true, titleBarStyle: 'hidden', webPreferences: { - nodeIntegration: true + nodeIntegration: true, + enableRemoteModule: true } }) - // and load the index.html of the app. win.loadURL(url.format({ pathname: path.join(__dirname, 'views/index.html'), protocol: 'file:', slashes: true })) - // Open the DevTools. win.webContents.openDevTools() - // Emitted when the window is closed. win.on('closed', () => { - // Dereference the window object, usually you would store windows - // in an array if your app supports multi windows, this is the time - // when you should delete the corresponding element. win = null }) } @@ -43,6 +36,13 @@ app.on('window-all-closed', () => { } }) +ipcMain.on('renamedChapter', (event, message) => { + let focusedWindow = BrowserWindow.getFocusedWindow(); + focusedWindow.close(); + let parentWindow = BrowserWindow.getFocusedWindow(); + parentWindow.webContents.send('renamedChapter', message); +}); + app.on('activate', () => { if (win === null) { createWindow() diff --git a/src/views/system/chapterRename.html b/src/views/system/chapterRename.html new file mode 100644 index 0000000..28a6ee8 --- /dev/null +++ b/src/views/system/chapterRename.html @@ -0,0 +1,49 @@ + + + + + + + Document + + + + +
+
+

Change Chapter Name

+
+
+ +
+
+
+ + +
+
+
+ + + + + \ No newline at end of file diff --git a/src/views/templates/blank/template.html b/src/views/templates/blank/template.html index 7a07207..a1d8477 100644 --- a/src/views/templates/blank/template.html +++ b/src/views/templates/blank/template.html @@ -1,11 +1,40 @@ + Document + + + -

The Blank Template

+
+
+

Blank Template

+
+ + + +
+
+
+
+ +
+
+
+
+ + + \ No newline at end of file diff --git a/src/views/templates/blank/template.js b/src/views/templates/blank/template.js index e69de29..d20f8ea 100644 --- a/src/views/templates/blank/template.js +++ b/src/views/templates/blank/template.js @@ -0,0 +1,65 @@ +const remote = require('electron').remote +const Menu = remote.Menu; +const MenuItem = remote.MenuItem; +const { ipcRenderer } = require('electron'); + +const chapterList = ['Chapterfl1']; +const chapterCount = chapterList.length; + +$(document).ready(function () { + updateChapters(); +}); + +function updateChapters() { + $('#chapters').empty(); + chapterList.forEach(chapter => { + const chapterName = chapter.replace(/fl/g, ' '); + $('#chapters').append(`${chapterName}`) + }); +} + +let currentSelectedChapterForContextAction = null; +let rightClickPosition = null; +const chapterMenu = new Menu() +const chapterContextItems = new MenuItem({ + label: 'Rename Chapter', + click: (e) => { + const currentChapter = currentSelectedChapterForContextAction.replace(/fl/g, ' '); + let child = new remote.BrowserWindow({ + parent: remote.getCurrentWindow(), + modal: true, + width: 360, + height: 135, + frame: false, + show: false, + titleBarStyle: 'hidden', + webPreferences: { + nodeIntegration: true, + enableRemoteModule: true + } + }) + child.loadURL('file://' + __dirname + '/../../system/chapterRename.html') + child.once('show', function () { + child.webContents.send('currentChapterName', currentChapter); + }); + child.once('ready-to-show', () => { + child.show() + }); + ipcRenderer.on('renamedChapter', (event, message) => { + const newChapterName = message; + const index = chapterList.findIndex(chapter => chapter === currentSelectedChapterForContextAction); + const newChapterId = newChapterName.replace(/ /g, 'fl'); + chapterList[index] = newChapterId; + currentSelectedChapterForContextAction = null; + updateChapters(); + }); + } +}) +chapterMenu.append(chapterContextItems); + +$(document).on('contextmenu', '.chapter_navigation', (e) => { + e.preventDefault(); + currentSelectedChapterForContextAction = e.target.id; + rightClickPosition = { x: e.x, y: e.y }; + chapterMenu.popup(remote.getCurrentWindow()); +}); -- cgit v1.2.3