class_name DeleteKey extends RefCounted ## The Delete key in Build mode: deletes what's selected (the selected cards ## and groups, else the inspector's thread, result or card), as one undo step. ## It asks "Are you sure?" first unless View ▾ → Ask before deleting is off ## (the setting "confirm_delete", on by default). const SETTING := "confirm_delete" ## Used when saving is off (--sample / --screenshot) and there's no state. static var _asks_mem := true static func asks(m: Main) -> bool: return bool(m.state.get_setting(SETTING, true)) if m.state else _asks_mem static func set_asks(m: Main, on: bool) -> void: _asks_mem = on if m.state: m.state.set_setting(SETTING, on) m.state.save() ## What the key would delete now: {"what": words for the question, "do": ## Callable}, or {} when nothing is selected. static func target(m: Main) -> Dictionary: var cards: Array[String] = [] for t in m.data.tables: if m.selection.has(t.id): cards.append(t.id) var groups: Array = m.selecting.groups.keys() if not cards.is_empty() or not groups.is_empty(): var parts := PackedStringArray() if not cards.is_empty(): parts.append(_name(m, cards[0]) if cards.size() == 1 else "%d cards" % cards.size()) if not groups.is_empty(): parts.append("%d %s and the cards in %s" % [groups.size(), "group" if groups.size() == 1 else "groups", "it" if groups.size() == 1 else "them"]) return {"what": " and ".join(parts), "do": func() -> void: _delete_many(m, cards, groups)} var sel: Dictionary = m.inspector.selection if m.inspector else {} match str(sel.get("kind", "")): "pipe": var key := "%s:%s>%s" % [sel["tid"], sel["eid"], sel["to"]] return {"what": "the thread to %s" % _name(m, str(sel["to"])), "do": m.remove_pipe.bind(key)} "result": var t := m.data.get_table(str(sel["tid"])) var e := t.find_entry(str(sel["eid"])) if t else null if e: return {"what": "“%s”" % Formatter.strip_tags(e.text).strip_edges(), "do": m.delete_entry.bind(t.id, e.id)} "card": if m.data.get_table(str(sel["tid"])): return {"what": _name(m, str(sel["tid"])), "do": m.delete_table.bind(str(sel["tid"]))} return {} ## Handles the key: asks first (unless turned off), then deletes. static func press(m: Main) -> void: var found := target(m) if found.is_empty(): return if not asks(m): (found["do"] as Callable).call() return var dialog := ModalCard.confirm("Delete?", "Delete %s? You can undo it with Ctrl+Z." % found["what"], "Delete", found["do"], true) var dont_ask := InspectorKit.check_box("Don't ask again (View ▾ → Ask before deleting)", false) dont_ask.toggled.connect(func(on: bool) -> void: set_asks(m, not on)) dialog.body.add_child(dont_ask) m._add_modal(dialog) static func _delete_many(m: Main, cards: Array[String], groups: Array) -> void: m.undo_redo._begin_edit() for gid: String in groups: m.grouping.delete_group(gid, false) for tid in cards: if m.data.get_table(tid): m.editing.delete_table(tid, false) m.selecting.clear() m.clear_selection() m.undo_redo._end_edit("Delete") m.toast("Deleted", true) static func _name(m: Main, tid: String) -> String: var t := m.data.get_table(tid) return Formatter.strip_tags(t.name).strip_edges() if t else "the card"