Use the proper index sets and so on to make deleting work from the sidebar again. Fix #192.

This commit is contained in:
Brent Simmons 2017-11-25 17:44:54 -08:00
parent 7d579733f0
commit 1047fa5a02
2 changed files with 64 additions and 8 deletions

View File

@ -111,11 +111,14 @@ import RSCore
return return
} }
let selectedRows = outlineView.selectedRowIndexes
animatingChanges = true animatingChanges = true
outlineView.beginUpdates() outlineView.beginUpdates()
outlineView.removeItems(at: selectedRows, inParent: nil, withAnimation: [.slideDown])
let indexSetsGroupedByParent = Node.indexSetsGroupedByParent(nodesToDelete)
for (parent, indexSet) in indexSetsGroupedByParent {
outlineView.removeItems(at: indexSet, inParent: parent.isRoot ? nil : parent, withAnimation: [.slideDown])
}
outlineView.endUpdates() outlineView.endUpdates()
runCommand(deleteCommand) runCommand(deleteCommand)
@ -466,5 +469,21 @@ private extension SidebarViewController {
} }
} }
func commonParentItemForNodes(_ nodes: [Node]) -> Node? {
if nodes.isEmpty {
return nil
}
guard let parent = nodes.first!.parent else {
return nil
}
for node in nodes {
if node.parent !== parent {
return nil
}
}
return parent
}
} }

View File

@ -8,13 +8,16 @@
import Foundation import Foundation
public final class Node: Equatable { public final class Node: Hashable {
public weak var parent: Node? public weak var parent: Node?
public let representedObject: AnyObject public let representedObject: AnyObject
public var canHaveChildNodes = false public var canHaveChildNodes = false
public var isGroupItem = false public var isGroupItem = false
public var childNodes: [Node]? public var childNodes: [Node]?
public let hashValue: Int
private static var incrementingID = 0
private static var incrementingIDLock = NSLock()
public var isRoot: Bool { public var isRoot: Bool {
get { get {
@ -63,6 +66,11 @@ public final class Node: Equatable {
self.representedObject = representedObject self.representedObject = representedObject
self.parent = parent self.parent = parent
Node.incrementingIDLock.lock()
self.hashValue = Node.incrementingID
Node.incrementingID += 1
Node.incrementingIDLock.unlock()
} }
public class func genericRootNode() -> Node { public class func genericRootNode() -> Node {
@ -137,6 +145,35 @@ public final class Node: Equatable {
nomad = parent nomad = parent
} }
} }
public class func nodesOrganizedByParent(_ nodes: [Node]) -> [Node: [Node]] {
let nodesWithParents = nodes.filter { $0.parent != nil }
return Dictionary(grouping: nodesWithParents, by: { $0.parent! })
}
public class func indexSetsGroupedByParent(_ nodes: [Node]) -> [Node: IndexSet] {
let d = nodesOrganizedByParent(nodes)
let indexSetDictionary = d.mapValues { (nodes) -> IndexSet in
var indexSet = IndexSet()
if nodes.isEmpty {
return indexSet
}
let parent = nodes.first!.parent!
for node in nodes {
if let index = parent.indexOfChild(node) {
indexSet.insert(index)
}
}
return indexSet
}
return indexSetDictionary
}
} }