Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/implement mentions in gutenberg #13942

Merged
merged 44 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
613996b
Implement mentions in Gutenberg
SergioEstevao Apr 17, 2020
22300e0
Fix reload of suggestions to remember current search.
SergioEstevao Apr 17, 2020
b971053
Make text field overlap block toolbar.
SergioEstevao Apr 17, 2020
eeab894
Reactive block text view after mention is done.
SergioEstevao Apr 17, 2020
d8bcfc4
Refactor code to separate function when dispatching to main thread.
SergioEstevao Apr 17, 2020
a029580
Do a full width overlap view for search bar.
SergioEstevao Apr 17, 2020
2d7ac29
Update release notes for mentions.
SergioEstevao Apr 18, 2020
9a73700
Fix lint errors
SergioEstevao Apr 18, 2020
1d486aa
Insert suggestions on return if only one is available.
SergioEstevao Apr 18, 2020
6ba77c2
Update project packages
SergioEstevao Apr 18, 2020
c8f9f96
Refactor mention trigger string to a constant
SergioEstevao Apr 18, 2020
1f3c3b4
Refactor error building for mention cancelation.
SergioEstevao Apr 18, 2020
4ca0e3b
Localize placeholder for mentions.
SergioEstevao Apr 18, 2020
5bbf34f
Refactor error handling to specific mention error type.
SergioEstevao Apr 18, 2020
3274249
Remove unnecessary code.
SergioEstevao Apr 21, 2020
c464f5e
Use the correct array for suggestions counting.
SergioEstevao Apr 21, 2020
49938fb
Add comment to explain the keyboard size issues.
SergioEstevao Apr 21, 2020
0eca5a2
Update GB reference.
SergioEstevao Apr 21, 2020
aeff564
Fix suggestion cell image load on reuse.
SergioEstevao Apr 22, 2020
6b543cc
Don't animate suggestions view with the keyboard animations.
SergioEstevao Apr 22, 2020
c7daecf
Reduce margins for suggestion table view.
SergioEstevao Apr 22, 2020
47ff6d6
Dismiss suggestion interface if tapped outside results or search.
SergioEstevao Apr 22, 2020
1b37e93
Add separator view between search field and table of suggestions.
SergioEstevao Apr 23, 2020
aa44a25
Make sure that at least half cell is show when there is more suggesti…
SergioEstevao Apr 23, 2020
270d60c
Show loading cell while waiting for users to be fetched.
SergioEstevao Apr 23, 2020
2dc0ff0
Merge branch 'develop' into issue/implement_mentions_in_gutenberg
SergioEstevao Apr 23, 2020
b3fd98f
Allow configuration of show loading off suggestions.
SergioEstevao Apr 23, 2020
8bbc640
Proper save of the text that was used to search.
SergioEstevao Apr 23, 2020
be43905
Update GB reference.
SergioEstevao Apr 23, 2020
0dfc825
Show results when only 1 suggestion exists.
SergioEstevao Apr 23, 2020
f7d59d3
Update gutenberg version.
SergioEstevao Apr 24, 2020
c063199
Fix backspace being deleted.
SergioEstevao Apr 27, 2020
4d66d12
Add comments for the new properties and methods
SergioEstevao Apr 28, 2020
c845d98
Update GB reference.
SergioEstevao Apr 28, 2020
527c662
Use list foreground color for the suggestions table.
SergioEstevao Apr 29, 2020
3ceee89
Merge branch 'develop' into issue/implement_mentions_in_gutenberg
SergioEstevao Apr 29, 2020
f1a572e
Make background semi-transparent.
SergioEstevao May 7, 2020
ee15749
Merge branch 'develop' into issue/implement_mentions_in_gutenberg
SergioEstevao May 11, 2020
1650519
Make suggestion header smoke color adapt to traits.
SergioEstevao May 11, 2020
e12f871
Set max visible cells to 7.
SergioEstevao May 14, 2020
e56e488
Show 0.4 % of next cell if available.
SergioEstevao May 14, 2020
563cf28
Update colors and size of text labels.
SergioEstevao May 14, 2020
5beafbd
Fix separator view color between the header and the tableview.
SergioEstevao May 19, 2020
022bdab
Merge branch 'develop' into issue/implement_mentions_in_gutenberg
SergioEstevao May 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ target 'WordPress' do
## Gutenberg (React Native)
## =====================
##
gutenberg :tag => 'v1.26.0'
gutenberg :commit => 'ce88a8ae7052c3fff3292ee2b6382750c90fe7ee'

## Third party libraries
## =====================
Expand Down
146 changes: 73 additions & 73 deletions Podfile.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
14.8
-----
* Block Editor: Mentions insertion are now available for WP.com and Jetpack sites.

14.7
-----
* Classic Editor: Fixed action sheet position for additional Media sources picker on iPad
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import Foundation
import UIKit

public class GutenbergMentionsViewController: UIViewController {

static let mentionTriggerText = String("@")

public lazy var backgroundView: UIView = {
let view = UIView(frame: .zero)
view.backgroundColor = .basicBackground
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

public lazy var searchView: UITextField = {
let textField = UITextField(frame: CGRect.zero)
textField.placeholder = NSLocalizedString("Search users...", comment: "Placeholder message when showing mentions search field")
textField.text = Self.mentionTriggerText
textField.clearButtonMode = .whileEditing
textField.translatesAutoresizingMaskIntoConstraints = false
textField.delegate = self
return textField
}()

public lazy var suggestionsView: SuggestionsTableView = {
let suggestionsView = SuggestionsTableView()
suggestionsView.enabled = true
suggestionsView.showSuggestions(forWord: Self.mentionTriggerText)
suggestionsView.suggestionsDelegate = self
suggestionsView.translatesAutoresizingMaskIntoConstraints = false
suggestionsView.siteID = siteID
suggestionsView.useTransparentHeader = true
return suggestionsView
}()

private let siteID: NSNumber
public var onCompletion: ((Result<String, NSError>) -> Void)?

public init(siteID: NSNumber) {
self.siteID = siteID
super.init(nibName: nil, bundle: nil)
}

public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved
self.siteID = 0
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override public func viewDidLoad() {
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .clear

view.addSubview(backgroundView)
NSLayoutConstraint.activate([
backgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
backgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
backgroundView.bottomAnchor.constraint(equalTo: view.safeBottomAnchor),
backgroundView.heightAnchor.constraint(equalToConstant: 50.0)
])

let margin = CGFloat(10)
view.addSubview(searchView)
searchView.becomeFirstResponder()
NSLayoutConstraint.activate([
searchView.leadingAnchor.constraint(equalTo: view.safeLeadingAnchor, constant: margin),
searchView.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor, constant: -margin),
searchView.bottomAnchor.constraint(equalTo: view.safeBottomAnchor),
searchView.heightAnchor.constraint(equalToConstant: 50.0)
])

view.addSubview(suggestionsView)
NSLayoutConstraint.activate([
suggestionsView.leadingAnchor.constraint(equalTo: view.safeLeadingAnchor, constant: margin),
suggestionsView.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor, constant: -margin),
suggestionsView.bottomAnchor.constraint(equalTo: searchView.topAnchor),
suggestionsView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
])

view.setNeedsUpdateConstraints()
}

override public func viewDidAppear(_ animated: Bool) {
suggestionsView.showSuggestions(forWord: Self.mentionTriggerText)
}
}

extension GutenbergMentionsViewController: UITextFieldDelegate {

public func textFieldDidEndEditing(_ textField: UITextField) {
guard let text = textField.text else {
return
}
suggestionsView.showSuggestions(forWord: text)
}

public func textFieldShouldClear(_ textField: UITextField) -> Bool {
onCompletion?(.failure(buildErrorForCancelation()))
return true
}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let nsString = textField.text as NSString? else {
return true
}
let searchWord = nsString.replacingCharacters(in: range, with: string)
if searchWord.hasPrefix(Self.mentionTriggerText) {
suggestionsView.showSuggestions(forWord: searchWord)
} else {
onCompletion?(.failure(buildErrorForCancelation()))
}
return true
}

public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if suggestionsView.numberOfSuggestions() == 1 {
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved
suggestionsView.selectSuggestion(atPosition: 0)
}
return true
}
}

extension GutenbergMentionsViewController: SuggestionsTableViewDelegate {

public func suggestionsTableView(_ suggestionsTableView: SuggestionsTableView, didSelectSuggestion suggestion: String?, forSearchText text: String) {
suggestionsTableView.showSuggestions(forWord: String())
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved
if let suggestion = suggestion {
onCompletion?(.success(suggestion))
}
}

public func suggestionsTableView(_ suggestionsTableView: SuggestionsTableView, didChangeTableBounds bounds: CGRect) {

}

public func suggestionsTableViewMaxDisplayedRows(_ suggestionsTableView: SuggestionsTableView) -> Int {
return 3
}

}

extension GutenbergMentionsViewController {

enum MentionError: CustomNSError {
case canceled
case notAvailable

static var errorDomain: String = "MentionErrorDomain"

var errorCode: Int {
switch self {
case .canceled:
return 1
case .notAvailable:
return 2
}
}

var errorUserInfo: [String: Any] {
return [:]
}
}

private func buildErrorForCancelation() -> NSError {
return MentionError.canceled as NSError
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class GutenbergViewController: UIViewController, PostEditor {
}

deinit {
tearDownKeyboardObservers()
removeObservers(fromPost: post)
gutenberg.invalidate()
attachmentDelegate.cancelAllPendingMediaRequests()
Expand All @@ -260,6 +261,7 @@ class GutenbergViewController: UIViewController, PostEditor {

override func viewDidLoad() {
super.viewDidLoad()
setupKeyboardObservers()
WPFontManager.loadNotoFontFamily()
createRevisionOfPost(loadAutosaveRevision: loadAutosaveRevision)
setupGutenbergView()
Expand All @@ -277,6 +279,36 @@ class GutenbergViewController: UIViewController, PostEditor {

// MARK: - Functions

private var keyboardShowObserver: Any?
private var keyboardHideObserver: Any?
private var keyboardFrame = CGRect.zero
private var mentionsBottomConstraint: NSLayoutConstraint?
private var previousFirstResponder: UIView?

private func setupKeyboardObservers() {
keyboardShowObserver = NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { (notification) in
if let keyboardRect = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
self.keyboardFrame = keyboardRect
self.updateConstraintsToAvoidKeyboard(frame: keyboardRect)
}
}
keyboardHideObserver = NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { (notification) in
if let keyboardRect = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
self.keyboardFrame = keyboardRect
self.updateConstraintsToAvoidKeyboard(frame: keyboardRect)
}
}
}

private func tearDownKeyboardObservers() {
if let keyboardShowObserver = keyboardShowObserver {
NotificationCenter.default.removeObserver(keyboardShowObserver)
}
if let keyboardHideObserver = keyboardHideObserver {
NotificationCenter.default.removeObserver(keyboardHideObserver)
}
}

private func configureNavigationBar() {
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.accessibilityIdentifier = "Gutenberg Editor Navigation Bar"
Expand Down Expand Up @@ -605,6 +637,63 @@ extension GutenbergViewController: GutenbergBridgeDelegate {
controller.modalPresentationStyle = .overCurrentContext
self.present(controller, animated: true)
}

func updateConstraintsToAvoidKeyboard(frame: CGRect) {
keyboardFrame = frame
let minimumKeyboardHeight = CGFloat(50)
guard let mentionsBottomConstraint = mentionsBottomConstraint else {
return
}

if keyboardFrame.height < minimumKeyboardHeight {
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved
mentionsBottomConstraint.constant = -self.view.safeAreaInsets.bottom
}
else {
mentionsBottomConstraint.constant = -self.keyboardFrame.height
}
}

func gutenbergDidRequestMention(callback: @escaping (Swift.Result<String, NSError>) -> Void) {
DispatchQueue.main.async(execute: { [weak self] in
self?.mentionShow(callback: callback)
})
}
}

// MARK: - Mention implementation

extension GutenbergViewController {

private func mentionShow(callback: @escaping (Swift.Result<String, NSError>) -> Void) {
guard let siteID = post.blog.dotComID else {
callback(.failure(GutenbergMentionsViewController.MentionError.notAvailable as NSError))
return
}

previousFirstResponder = view.findFirstResponder()
let mentionsController = GutenbergMentionsViewController(siteID: siteID)
mentionsController.onCompletion = { (result) in
callback(result)
mentionsController.view.removeFromSuperview()
mentionsController.removeFromParent()
if let previousFirstResponder = self.previousFirstResponder {
previousFirstResponder.becomeFirstResponder()
}
}
addChild(mentionsController)
view.addSubview(mentionsController.view)
let mentionsBottomConstraint = mentionsController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
NSLayoutConstraint.activate([
mentionsController.view.leadingAnchor.constraint(equalTo: view.safeLeadingAnchor, constant: 0),
mentionsController.view.trailingAnchor.constraint(equalTo: view.safeTrailingAnchor, constant: 0),
mentionsBottomConstraint,
mentionsController.view.topAnchor.constraint(equalTo: view.safeTopAnchor)
])
self.mentionsBottomConstraint = mentionsBottomConstraint
updateConstraintsToAvoidKeyboard(frame: keyboardFrame)
mentionsController.didMove(toParent: self)
}

}

// MARK: - GutenbergBridgeDataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
- (BOOL)showSuggestionsForWord:(nonnull NSString *)word;

- (void)hideSuggestions;

- (NSInteger)numberOfSuggestions;

- (void)selectSuggestionAtPosition:(NSInteger)position;

@end

@protocol SuggestionsTableViewDelegate <NSObject>
Expand Down
15 changes: 14 additions & 1 deletion WordPress/Classes/ViewRelated/Suggestions/SuggestionsTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ - (void)hideSuggestions
[self showSuggestionsForWord:@""];
}

- (NSInteger)numberOfSuggestions {
return self.suggestions.count;
}

- (void)selectSuggestionAtPosition:(NSInteger)position {
if ([self.suggestions count] == 0) {
return;
}
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0]];
}


#pragma mark - UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Expand Down Expand Up @@ -302,7 +314,8 @@ - (void)suggestionListUpdated:(NSNotification *)notification
// only reload if the suggestion list is updated for the current site
if (self.siteID && [notification.object isEqualToNumber:self.siteID]) {
self.suggestions = [[SuggestionService sharedInstance] suggestionsForSiteID:self.siteID];
[self showSuggestionsForWord:self.searchText];
NSString * text = [NSString stringWithFormat:@"@%@", self.searchText];
[self showSuggestionsForWord:text];
}
}

Expand Down
Loading