From 3746f83fc150302d63d2322dc706dd831ae86ab0 Mon Sep 17 00:00:00 2001 From: Jesse Date: Thu, 22 Aug 2019 14:42:26 -0600 Subject: [PATCH] minimize changes to priority --- CHANGELOG.md | 3 +++ lib/repository.js | 47 ++++++++++++++++++++++++++++++++--------------- package-lock.json | 2 +- package.json | 2 +- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21b7dc23..5147d8e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.5.31 +- When moving a task, only change the order of the current task unless it's moving to the top of a list with more than one task, then we update the task that was previously first as well. + # 1.5.28 - Implement Repository.modifyTaskFromContent diff --git a/lib/repository.js b/lib/repository.js index e8dc528f..525904ad 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -1027,23 +1027,40 @@ Repository.prototype.moveTasks = function(tasks, newList, newPos, noEmit, cb) { return _task.equals(task); }); } - // Modify the tasks in current list - var index = 0; - var taskMapper = function(_task, cb) { - self.setTaskPriority(_task, index, function() { - index++; - self.modifyTask(_task, cb); - }); - }; - async.eachSeries(toListTasks, taskMapper, function(err) { - if (err) return cb([new Error("moveTasks: Error while modifying tasks in the current list"), err]); + // Modify the task order + const tasksToModify = [] + if (newPos === 0) { // is first + task.order = 0 + if (toListTasks.length > 1) { + const nextTask = toListTasks[1] + if (toListTasks.length > 2) { + const nextOrder = toListTasks[2].order + nextTask.order = nextOrder ? nextOrder/2 : 10 + } else { + nextTask.order = 10 + } + tasksToModify.push(nextTask) + } + } else if (newPos === toListTasks.length-1) { // is last + const oldLastTask = toListTasks[toListTasks.length-2] + const oldLastTaskOrder = oldLastTask.order || (toListTasks.length-2)*10 + task.order = oldLastTaskOrder + 10 + } else { + const prevTask = toListTasks[newPos-1] + const nextTask = toListTasks[newPos+1] + const prevTaskOrder = prevTask.order || 0 + const nextTaskOrder = nextTask.order || 100 + task.order = prevTaskOrder + (nextTaskOrder - prevTaskOrder)/2 + } + tasksToModify.push(task) - // Remove the task from the old list - if (!sameList) { - async.eachSeries(fromListTasks, taskMapper, cb); - } else cb(null, task); - }); + async.eachSeries(tasksToModify, (task, cb) => { + self.modifyTask(task, cb); + }, err => { + if (err) return cb([new Error("moveTasks: Error while modifying tasks in the current list"), err]) + cb(null, task) + }) }; async.series(_.map(tasks, function(task, i) { diff --git a/package-lock.json b/package-lock.json index 37f95678..b262cb9f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "imdone-core", - "version": "1.5.29", + "version": "1.5.31", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7af60dcb..4da1c6ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "imdone-core", - "version": "1.5.30", + "version": "1.5.31", "description": "imdone-core", "main": "index.js", "scripts": {