-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathGutenbergViewController+MoreActions.swift
104 lines (82 loc) · 4.22 KB
/
GutenbergViewController+MoreActions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Foundation
/// This extension handles the "more" actions triggered by the top right
/// navigation bar button of Gutenberg editor.
extension GutenbergViewController {
private enum ErrorCode: Int {
case expectedSecondaryAction = 1
}
func displayMoreSheet() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//TODO: Comment in when bridge is ready
/*if mode == .richText {
// NB : This is a candidate for plurality via .stringsdict, but is limited by https://github.com/wordpress-mobile/WordPress-iOS/issues/6327
let textCounterTitle = String(format: NSLocalizedString("%li words, %li characters", comment: "Displays the number of words and characters in text"), richTextView.wordCount, richTextView.characterCount)
alert.title = textCounterTitle
}*/
if postEditorStateContext.isSecondaryPublishButtonShown,
let buttonTitle = postEditorStateContext.secondaryPublishButtonText {
alert.addDefaultActionWithTitle(buttonTitle) { _ in
self.secondaryPublishButtonTapped()
}
}
alert.addDefaultActionWithTitle(MoreSheetAlert.classicTitle) { [unowned self] _ in
self.savePostEditsAndSwitchToAztec()
}
let toggleModeTitle: String = {
if mode == .richText {
return MoreSheetAlert.htmlTitle
} else {
return MoreSheetAlert.richTitle
}
}()
alert.addDefaultActionWithTitle(toggleModeTitle) { [unowned self] _ in
self.toggleEditingMode()
}
alert.addDefaultActionWithTitle(MoreSheetAlert.previewTitle) { [weak self] _ in
self?.displayPreview()
}
if (post.revisions ?? []).count > 0 {
alert.addDefaultActionWithTitle(MoreSheetAlert.historyTitle) { [weak self] _ in
self?.displayHistory()
}
}
alert.addDefaultActionWithTitle(MoreSheetAlert.postSettingsTitle) { [weak self] _ in
self?.displayPostSettings()
}
alert.addCancelActionWithTitle(MoreSheetAlert.keepEditingTitle)
alert.popoverPresentationController?.barButtonItem = navigationBarManager.moreBarButtonItem
present(alert, animated: true)
}
func secondaryPublishButtonTapped() {
guard let action = self.postEditorStateContext.secondaryPublishButtonAction else {
// If the user tapped on the secondary publish action button, it means we should have a secondary publish action.
let error = NSError(domain: errorDomain, code: ErrorCode.expectedSecondaryAction.rawValue, userInfo: nil)
WPCrashLogging.logError(error)
return
}
let secondaryStat = self.postEditorStateContext.secondaryPublishActionAnalyticsStat
let publishPostClosure = { [unowned self] in
self.publishPost(
action: action,
dismissWhenDone: action.dismissesEditor,
analyticsStat: secondaryStat)
}
if presentedViewController != nil {
dismiss(animated: true, completion: publishPostClosure)
} else {
publishPostClosure()
}
}
}
// MARK: - Constants
extension GutenbergViewController {
private struct MoreSheetAlert {
static let classicTitle = NSLocalizedString("Switch to Classic Editor", comment: "Switches from Gutenberg mobile to the Classic editor")
static let htmlTitle = NSLocalizedString("Switch to HTML Mode", comment: "Switches the Editor to HTML Mode")
static let richTitle = NSLocalizedString("Switch to Visual Mode", comment: "Switches the Editor to Rich Text Mode")
static let previewTitle = NSLocalizedString("Preview", comment: "Displays the Post Preview Interface")
static let historyTitle = NSLocalizedString("History", comment: "Displays the History screen from the editor's alert sheet")
static let postSettingsTitle = NSLocalizedString("Post Settings", comment: "Name of the button to open the post settings")
static let keepEditingTitle = NSLocalizedString("Keep Editing", comment: "Goes back to editing the post.")
}
}