-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0418daa
commit 52b47cb
Showing
6 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
VinOutlineKit/Sources/VinOutlineKit/Commands/SortRowsCommand.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Created by Maurice Parker on 9/14/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class SortRowsCommand: OutlineCommand { | ||
|
||
var rowMoves = [Outline.RowMove]() | ||
var restoreMoves = [Outline.RowMove]() | ||
|
||
public init(actionName: String, | ||
undoManager: UndoManager, | ||
delegate: OutlineCommandDelegate, | ||
outline: Outline, | ||
rows: [Row]) { | ||
|
||
let displayOrderRows = rows.sortedByDisplayOrder() | ||
|
||
for row in displayOrderRows { | ||
guard let parent = row.parent, let index = parent.firstIndexOfRow(row) else { continue } | ||
restoreMoves.append(Outline.RowMove(row: row, toParent: parent, toChildIndex: index)) | ||
} | ||
|
||
let sortedRows = rows.sorted { left, right in | ||
return (left.topic?.string ?? "").caseInsensitiveCompare(right.topic?.string ?? "") == .orderedAscending | ||
} | ||
|
||
let startIndex = displayOrderRows.first!.parent!.firstIndexOfRow(displayOrderRows.first!) ?? 0 | ||
|
||
for i in 0..<sortedRows.count { | ||
let row = sortedRows[i] | ||
guard let parent = row.parent else { continue } | ||
rowMoves.append(Outline.RowMove(row: row, toParent: parent, toChildIndex: startIndex + i)) | ||
} | ||
|
||
super.init(actionName: actionName, undoManager: undoManager, delegate: delegate, outline: outline) | ||
} | ||
|
||
public override func perform() { | ||
outline.moveRows(rowMoves, rowStrings: nil) | ||
} | ||
|
||
public override func undo() { | ||
outline.moveRows(restoreMoves, rowStrings: nil) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters