deck drag&drop undo

This commit is contained in:
Damien Elmes 2021-03-11 19:54:29 +10:00
parent 984e2c2666
commit e20c5ed9c5
3 changed files with 26 additions and 12 deletions

View File

@ -287,14 +287,22 @@ class DeckBrowser:
self._renderPage(reuse=True)
def _handle_drag_and_drop(self, source: int, target: int) -> None:
try:
def process() -> None:
self.mw.col.decks.drag_drop_decks([source], target)
def on_done(fut: Future) -> None:
try:
fut.result()
except Exception as e:
showWarning(str(e))
return
self.mw.update_undo_actions()
gui_hooks.sidebar_should_refresh_decks()
self.show()
self.mw.taskman.with_progress(process, on_done)
def ask_delete_deck(self, did: int) -> bool:
deck = self.mw.col.decks.get(did)
if deck["dyn"]:

View File

@ -480,9 +480,9 @@ class SidebarTreeView(QTreeView):
return
self.refresh()
self.mw.deckBrowser.refresh()
self.mw.update_undo_actions()
def on_save() -> None:
self.mw.checkpoint(tr(TR.ACTIONS_RENAME_DECK))
self.browser.model.beginReset()
self.mw.taskman.with_progress(
lambda: self.col.decks.drag_drop_decks(source_ids, target.id), on_done

View File

@ -617,9 +617,8 @@ impl Collection {
source_decks: &[DeckID],
target: Option<DeckID>,
) -> Result<()> {
self.state.deck_cache.clear();
let usn = self.usn()?;
self.transact(None, |col| {
self.transact(Some(UndoableOpKind::RenameDeck), |col| {
let target_deck;
let mut target_name = None;
if let Some(target) = target {
@ -634,18 +633,25 @@ impl Collection {
for source in source_decks {
if let Some(mut source) = col.storage.get_deck(*source)? {
let orig = source.clone();
let new_name = drag_drop_deck_name(&source.name, target_name);
if new_name == source.name {
continue;
}
let orig = source.clone();
// this is basically update_deck_inner(), except:
// - we skip the normalization in prepare_for_update()
// - we skip the match_or_create_parents() step
source.set_modified(usn);
source.name = new_name;
col.ensure_deck_name_unique(&mut source, usn)?;
col.rename_child_decks(&orig, &source.name, usn)?;
source.set_modified(usn);
col.storage.update_deck(&source)?;
col.update_single_deck_undoable(&mut source, orig)?;
// after updating, we need to ensure all grandparents exist, which may not be the case
// in the parent->child case
// FIXME: maybe we only need to do this once at the end of the loop?
col.create_missing_parents(&source.name, usn)?;
}
}