mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
A user accidentally dragged a list into another swimlane on mobile (merging it with an existing list) and spent ~15 min recovering - no confirmation, no undo. Undo/Redo: the userPositionHistory collection modelled this but was NOT actually recording (trackChange guarded on an un-imported global) and had no redo or key bindings. Now: - list moves are recorded in updateListSort (import UserPositionHistory so the guard passes); card moves already had the call. - add a symmetric redo() model method + an undone/undoneAt redo stack; add userPositionHistory.undoLast/redoLast methods (a new action clears the redo stack via trackChange). - wire Ctrl+Z / Ctrl+Y (and Cmd variants) in client/lib/keyboard.js; disabled inside inputs/textareas so native text undo is unaffected. - pure selection logic models/lib/undoRedoSelection.js + tests. Confirmation: on touch / small screens, moving a list to a DIFFERENT swimlane (which re-homes every card) now asks for confirmation; a decline reverts the drag. Same-swimlane reordering is unaffected. Thanks to mueschel (report) and xet7.
360 lines
9.5 KiB
JavaScript
360 lines
9.5 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { SimpleSchema } from '/imports/simpleSchema';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Boards from '/models/boards';
|
|
import Cards from '/models/cards';
|
|
import ChecklistItems from '/models/checklistItems';
|
|
import Checklists from '/models/checklists';
|
|
import Lists from '/models/lists';
|
|
import Swimlanes from '/models/swimlanes';
|
|
|
|
/**
|
|
* UserPositionHistory collection - Per-user history of entity movements
|
|
* Similar to Activities but specifically for tracking position changes with undo/redo support
|
|
*/
|
|
const UserPositionHistory = new Mongo.Collection('userPositionHistory');
|
|
|
|
UserPositionHistory.attachSchema(
|
|
new SimpleSchema({
|
|
userId: {
|
|
/**
|
|
* The user who made this change
|
|
*/
|
|
type: String,
|
|
},
|
|
boardId: {
|
|
/**
|
|
* The board where the change occurred
|
|
*/
|
|
type: String,
|
|
},
|
|
entityType: {
|
|
/**
|
|
* Type of entity: 'swimlane', 'list', or 'card'
|
|
*/
|
|
type: String,
|
|
allowedValues: ['swimlane', 'list', 'card', 'checklist', 'checklistItem'],
|
|
},
|
|
entityId: {
|
|
/**
|
|
* The ID of the entity that was moved
|
|
*/
|
|
type: String,
|
|
},
|
|
actionType: {
|
|
/**
|
|
* Type of action performed
|
|
*/
|
|
type: String,
|
|
allowedValues: ['move', 'create', 'delete', 'restore', 'archive'],
|
|
},
|
|
previousState: {
|
|
/**
|
|
* The state before the change
|
|
*/
|
|
type: Object,
|
|
blackbox: true,
|
|
optional: true,
|
|
},
|
|
newState: {
|
|
/**
|
|
* The state after the change
|
|
*/
|
|
type: Object,
|
|
blackbox: true,
|
|
},
|
|
// For easier undo operations, store specific fields
|
|
previousSort: {
|
|
type: Number,
|
|
optional: true,
|
|
},
|
|
newSort: {
|
|
type: Number,
|
|
optional: true,
|
|
},
|
|
previousSwimlaneId: {
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
newSwimlaneId: {
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
previousListId: {
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
newListId: {
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
previousBoardId: {
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
newBoardId: {
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
createdAt: {
|
|
/**
|
|
* When this history entry was created
|
|
*/
|
|
type: Date,
|
|
autoValue() {
|
|
if (this.isInsert) {
|
|
return new Date();
|
|
} else if (this.isUpsert) {
|
|
return { $setOnInsert: new Date() };
|
|
} else {
|
|
this.unset();
|
|
}
|
|
},
|
|
},
|
|
// For savepoint/checkpoint feature
|
|
isCheckpoint: {
|
|
/**
|
|
* Whether this is a user-marked checkpoint/savepoint
|
|
*/
|
|
type: Boolean,
|
|
defaultValue: false,
|
|
optional: true,
|
|
},
|
|
checkpointName: {
|
|
/**
|
|
* User-defined name for the checkpoint
|
|
*/
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
// For grouping related changes
|
|
batchId: {
|
|
/**
|
|
* ID to group related changes (e.g., moving multiple cards at once)
|
|
*/
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
// #6478 undo/redo stack: a change that has been undone can be redone until a
|
|
// NEW change is recorded (which clears the redo stack). undoneAt orders the
|
|
// redo stack (most-recently-undone is redone first).
|
|
undone: {
|
|
type: Boolean,
|
|
defaultValue: false,
|
|
optional: true,
|
|
},
|
|
undoneAt: {
|
|
type: Date,
|
|
optional: true,
|
|
},
|
|
}),
|
|
);
|
|
|
|
UserPositionHistory.helpers({
|
|
/**
|
|
* Get a human-readable description of this change
|
|
*/
|
|
getDescription() {
|
|
const entityName = this.entityType;
|
|
const action = this.actionType;
|
|
|
|
let desc = `${action} ${entityName}`;
|
|
|
|
if (this.actionType === 'move') {
|
|
if (this.previousListId && this.newListId && this.previousListId !== this.newListId) {
|
|
desc += ' to different list';
|
|
} else if (this.previousSwimlaneId && this.newSwimlaneId && this.previousSwimlaneId !== this.newSwimlaneId) {
|
|
desc += ' to different swimlane';
|
|
} else if (this.previousSort !== this.newSort) {
|
|
desc += ' position';
|
|
}
|
|
}
|
|
|
|
return desc;
|
|
},
|
|
|
|
/**
|
|
* Can this change be undone?
|
|
*/
|
|
async canUndo() {
|
|
// Can undo if the entity still exists
|
|
switch (this.entityType) {
|
|
case 'card':
|
|
return !!(await ReactiveCache.getCard(this.entityId));
|
|
case 'list':
|
|
return !!(await ReactiveCache.getList(this.entityId));
|
|
case 'swimlane':
|
|
return !!(await ReactiveCache.getSwimlane(this.entityId));
|
|
case 'checklist':
|
|
return !!(await ReactiveCache.getChecklist(this.entityId));
|
|
case 'checklistItem':
|
|
return !!(await ChecklistItems.findOneAsync(this.entityId));
|
|
default:
|
|
return false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Undo this change
|
|
*/
|
|
async undo() {
|
|
if (!(await this.canUndo())) {
|
|
throw new Meteor.Error('cannot-undo', 'Entity no longer exists');
|
|
}
|
|
|
|
const userId = this.userId;
|
|
|
|
switch (this.entityType) {
|
|
case 'card': {
|
|
const card = await ReactiveCache.getCard(this.entityId);
|
|
if (card) {
|
|
// Restore previous position
|
|
const boardId = this.previousBoardId || card.boardId;
|
|
const swimlaneId = this.previousSwimlaneId || card.swimlaneId;
|
|
const listId = this.previousListId || card.listId;
|
|
const sort = this.previousSort !== undefined ? this.previousSort : card.sort;
|
|
|
|
await Cards.updateAsync(card._id, {
|
|
$set: {
|
|
boardId,
|
|
swimlaneId,
|
|
listId,
|
|
sort,
|
|
},
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'list': {
|
|
const list = await ReactiveCache.getList(this.entityId);
|
|
if (list) {
|
|
const sort = this.previousSort !== undefined ? this.previousSort : list.sort;
|
|
const swimlaneId = this.previousSwimlaneId || list.swimlaneId;
|
|
|
|
await Lists.updateAsync(list._id, {
|
|
$set: {
|
|
sort,
|
|
swimlaneId,
|
|
},
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'swimlane': {
|
|
const swimlane = await ReactiveCache.getSwimlane(this.entityId);
|
|
if (swimlane) {
|
|
const sort = this.previousSort !== undefined ? this.previousSort : swimlane.sort;
|
|
|
|
await Swimlanes.updateAsync(swimlane._id, {
|
|
$set: {
|
|
sort,
|
|
},
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'checklist': {
|
|
const checklist = await ReactiveCache.getChecklist(this.entityId);
|
|
if (checklist) {
|
|
const sort = this.previousSort !== undefined ? this.previousSort : checklist.sort;
|
|
|
|
await Checklists.updateAsync(checklist._id, {
|
|
$set: {
|
|
sort,
|
|
},
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'checklistItem': {
|
|
if (typeof ChecklistItems !== 'undefined') {
|
|
const item = await ChecklistItems.findOneAsync(this.entityId);
|
|
if (item) {
|
|
const sort = this.previousSort !== undefined ? this.previousSort : item.sort;
|
|
const checklistId = this.previousState?.checklistId || item.checklistId;
|
|
|
|
await ChecklistItems.updateAsync(item._id, {
|
|
$set: {
|
|
sort,
|
|
checklistId,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
|
|
/**
|
|
* #6478: Redo this change — re-apply the NEW position after an undo. Mirrors
|
|
* undo() but restores the new* fields instead of the previous* ones.
|
|
*/
|
|
async redo() {
|
|
if (!(await this.canUndo())) {
|
|
throw new Meteor.Error('cannot-redo', 'Entity no longer exists');
|
|
}
|
|
|
|
switch (this.entityType) {
|
|
case 'card': {
|
|
const card = await ReactiveCache.getCard(this.entityId);
|
|
if (card) {
|
|
await Cards.updateAsync(card._id, {
|
|
$set: {
|
|
boardId: this.newBoardId || card.boardId,
|
|
swimlaneId: this.newSwimlaneId || card.swimlaneId,
|
|
listId: this.newListId || card.listId,
|
|
sort: this.newSort !== undefined ? this.newSort : card.sort,
|
|
},
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'list': {
|
|
const list = await ReactiveCache.getList(this.entityId);
|
|
if (list) {
|
|
await Lists.updateAsync(list._id, {
|
|
$set: {
|
|
sort: this.newSort !== undefined ? this.newSort : list.sort,
|
|
swimlaneId: this.newSwimlaneId || list.swimlaneId,
|
|
},
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'swimlane': {
|
|
const swimlane = await ReactiveCache.getSwimlane(this.entityId);
|
|
if (swimlane) {
|
|
await Swimlanes.updateAsync(swimlane._id, {
|
|
$set: { sort: this.newSort !== undefined ? this.newSort : swimlane.sort },
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'checklist': {
|
|
const checklist = await ReactiveCache.getChecklist(this.entityId);
|
|
if (checklist) {
|
|
await Checklists.updateAsync(checklist._id, {
|
|
$set: { sort: this.newSort !== undefined ? this.newSort : checklist.sort },
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case 'checklistItem': {
|
|
if (typeof ChecklistItems !== 'undefined') {
|
|
const item = await ChecklistItems.findOneAsync(this.entityId);
|
|
if (item) {
|
|
await ChecklistItems.updateAsync(item._id, {
|
|
$set: { sort: this.newSort !== undefined ? this.newSort : item.sort },
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
});
|
|
|
|
export default UserPositionHistory;
|