From f1ebc93367bd135d8bb59e93742cd468733076a5 Mon Sep 17 00:00:00 2001 From: Samuel Hutchinson <34013162+flightmansam@users.noreply.github.com> Date: Wed, 8 Jun 2022 13:06:47 +1000 Subject: [PATCH 1/2] Initial functionality task-cmd-line-args Now calling min with the -t argument and a query will try and add the new tab into a specific task (or create one) Todos: - Refactor search and sort tasks and the move/switch to task to a more general location of the souce code as it is no longer just used for customBangs NB: this commit includes the electron API change to app.requestSingleInstanceLock(process.argv) in main which fixes a bug with chromium scrambling of second instance commandline args. --- js/menuRenderer.js | 13 +++++++++++++ js/searchbar/customBangs.js | 20 ++++++++++++-------- main/main.js | 25 ++++++++++++++++++------- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/js/menuRenderer.js b/js/menuRenderer.js index 78b06df4e..688407125 100644 --- a/js/menuRenderer.js +++ b/js/menuRenderer.js @@ -10,6 +10,7 @@ var PDFViewer = require('pdfViewer.js') var tabEditor = require('navbar/tabEditor.js') var readerView = require('readerView.js') var taskOverlay = require('taskOverlay/taskOverlay.js') +var { searchAndSortTasks, moveToTaskCommand } = require('searchbar/customBangs.js') module.exports = { initialize: function () { @@ -94,6 +95,18 @@ module.exports = { browserUI.addTab(newTab, { enterEditMode: !data.url // only enter edit mode if the new tab is empty }) + + if (data.taskQuery) { + // use the first search result + // if there is no result, need to create a new task + let task = searchAndSortTasks(data.taskQuery, excludeSelected=false)[0]?.task + if (!task) { + task = tasks.get(tasks.add(undefined, tasks.getIndex(tasks.getSelected().id) + 1)) + task.name = data.taskQuery + } + + moveToTaskCommand(task.id) + } }) ipc.on('saveCurrentPage', async function () { diff --git a/js/searchbar/customBangs.js b/js/searchbar/customBangs.js index a668954e8..d7ae8f050 100644 --- a/js/searchbar/customBangs.js +++ b/js/searchbar/customBangs.js @@ -67,14 +67,19 @@ function getTaskByNameOrNumber (text) { // return an array of tasks sorted by last activity // if a search string is present, filter the results with a basic fuzzy search -function searchAndSortTasks (text) { +function searchAndSortTasks (text, excludeSelected=true) { + let taskResults = tasks - .filter(t => t.id !== tasks.getSelected().id) - .map(t => Object.assign({}, { task: t }, { lastActivity: tasks.getLastActivity(t.id) })) + + if (excludeSelected === true){ + taskResults = tasks.filter(t => t.id !== tasks.getSelected().id) + } - taskResults = taskResults.sort(function (a, b) { - return b.lastActivity - a.lastActivity - }) + taskResults = taskResults + .map(t => Object.assign({}, { task: t }, { lastActivity: tasks.getLastActivity(t.id) })) + .sort(function (a, b) { + return b.lastActivity - a.lastActivity + }) if (text !== '') { // fuzzy search @@ -89,7 +94,6 @@ function searchAndSortTasks (text) { return (exactMatch || fuzzyTitleScore > 0.4) }) } - return taskResults } @@ -364,4 +368,4 @@ function initialize () { }) } -module.exports = { initialize } +module.exports = { initialize, searchAndSortTasks, moveToTaskCommand } diff --git a/main/main.js b/main/main.js index ac3b07407..ab8ea5c5b 100644 --- a/main/main.js +++ b/main/main.js @@ -69,7 +69,7 @@ var secondaryMenu = null var isFocusMode = false var appIsReady = false -const isFirstInstance = app.requestSingleInstanceLock() +const isFirstInstance = app.requestSingleInstanceLock(process.argv) if (!isFirstInstance) { app.quit() @@ -104,23 +104,34 @@ function openTabInWindow (url) { function handleCommandLineArguments (argv) { // the "ready" event must occur before this function can be used + var initTaskQuery = undefined if (argv) { + + // check for -t task query + if (argv.includes('-t') && argv.indexOf('-t') > 0){ + // query for specific task to add search to + initTaskQuery = argv[argv.indexOf('-t') + 1] + } + argv.forEach(function (arg, idx) { if (arg && arg.toLowerCase() !== __dirname.toLowerCase()) { - // URL if (arg.indexOf('://') !== -1) { + // URL sendIPCToWindow(mainWindow, 'addTab', { - url: arg + url: arg, + taskQuery: initTaskQuery }) } else if (idx > 0 && argv[idx - 1] === '-s') { // search sendIPCToWindow(mainWindow, 'addTab', { - url: arg + url: arg, + taskQuery: initTaskQuery }) } else if (/\.(m?ht(ml)?|pdf)$/.test(arg) && fs.existsSync(arg)) { // local files (.html, .mht, mhtml, .pdf) sendIPCToWindow(mainWindow, 'addTab', { - url: 'file://' + path.resolve(arg) + url: 'file://' + path.resolve(arg), + taskQuery: initTaskQuery }) } } @@ -353,14 +364,14 @@ app.on('open-url', function (e, url) { } }) -app.on('second-instance', function (e, argv, workingDir) { +app.on('second-instance', function (e, argv, workingDir, additionalData) { if (mainWindow) { if (mainWindow.isMinimized()) { mainWindow.restore() } mainWindow.focus() // add a tab with the new URL - handleCommandLineArguments(argv) + handleCommandLineArguments(additionalData) } }) From 1f5e7c712da602ee942ec957e5b08a57d24ff58f Mon Sep 17 00:00:00 2001 From: Samuel Hutchinson <34013162+flightmansam@users.noreply.github.com> Date: Thu, 9 Jun 2022 14:00:43 +1000 Subject: [PATCH 2/2] API improvements Added ability to pass in the task ID to the query Added a switch to task IPC --- js/menuRenderer.js | 54 ++++++++++++++++++++++++++++++++++++++++------ main/main.js | 4 ++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/js/menuRenderer.js b/js/menuRenderer.js index 688407125..d11f387da 100644 --- a/js/menuRenderer.js +++ b/js/menuRenderer.js @@ -97,15 +97,55 @@ module.exports = { }) if (data.taskQuery) { - // use the first search result - // if there is no result, need to create a new task - let task = searchAndSortTasks(data.taskQuery, excludeSelected=false)[0]?.task - if (!task) { - task = tasks.get(tasks.add(undefined, tasks.getIndex(tasks.getSelected().id) + 1)) - task.name = data.taskQuery + // use the first search result + // if there is no result, need to create a new task + let task + + if (/^\d+$/.test(data.taskQuery)) { + task = tasks.get(data.taskQuery) + } else { + task = searchAndSortTasks(data.taskQuery, excludeSelected=false)[0]?.task + } + + if (!task) { + task = tasks.get(tasks.add(undefined, tasks.getIndex(tasks.getSelected().id) + 1)) + task.name = data.taskQuery + } + + moveToTaskCommand(task.id) } + + }) + + ipc.on('switchToTask', function (e, data) { + /* new tabs can't be created in modal mode */ + if (modalMode.enabled()) { + return + } + + /* new tabs can't be created in focus mode */ + if (focusMode.enabled()) { + focusMode.warn() + return + } + + if (data.taskQuery) { + // use the first search result + // if there is no result, need to create a new task + let task + + if (/^\d+$/.test(data.taskQuery)) { + task = tasks.get(data.taskQuery) + } else { + task = searchAndSortTasks(data.taskQuery, excludeSelected=false)[0]?.task + } + + if (!task) { + task = tasks.get(tasks.add(undefined, tasks.getIndex(tasks.getSelected().id) + 1)) + task.name = data.taskQuery + } - moveToTaskCommand(task.id) + browserUI.switchToTask(task.id) } }) diff --git a/main/main.js b/main/main.js index ab8ea5c5b..1094ed9aa 100644 --- a/main/main.js +++ b/main/main.js @@ -133,6 +133,10 @@ function handleCommandLineArguments (argv) { url: 'file://' + path.resolve(arg), taskQuery: initTaskQuery }) + } else if (initTaskQuery) { + sendIPCToWindow(mainWindow, 'switchToTask', { + taskQuery: initTaskQuery + }) } } })