Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
[iOS] Edit link with text + url
Browse files Browse the repository at this point in the history
  • Loading branch information
aringenbach committed Oct 12, 2023
1 parent 6c42f29 commit 7c14c8a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
29 changes: 20 additions & 9 deletions platforms/ios/example/Wysiwyg/Views/ComposerActionToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ struct ComposerActionToolbar: View {
func makeAlertConfig() -> AlertConfig {
var actions: [AlertConfig.Action] = [.cancel(title: "Cancel")]
let createLinkTitle = "Create Link"
let singleTextAction: ([String]) -> Void = { strings in
let urlString = strings[0]
viewModel.select(range: linkAttributedRange)
viewModel.applyLinkOperation(.setLink(urlString: urlString))
}
switch linkAction {
case .create:
let singleTextAction: ([String]) -> Void = { strings in
let urlString = strings[0]
viewModel.select(range: linkAttributedRange)
viewModel.applyLinkOperation(.setLink(urlString: urlString))
}
actions.append(createAction(singleTextAction: singleTextAction))
return AlertConfig(title: createLinkTitle, actions: actions)
case .createWithText:
Expand All @@ -70,9 +70,15 @@ struct ComposerActionToolbar: View {
}
actions.append(createWithTextAction(doubleTextAction: doubleTextAction))
return AlertConfig(title: createLinkTitle, actions: actions)
case let .edit(url):
case let .edit(url, text):
let editLinktitle = "Edit Link URL"
actions.append(editTextAction(singleTextAction: singleTextAction, url: url))
let doubleTextAction: ([String]) -> Void = { strings in
let urlString = strings[0]
let text = strings[1]
viewModel.select(range: linkAttributedRange)
viewModel.applyLinkOperation(.editLink(urlString: urlString, text: text))
}
actions.append(editTextAction(doubleTextAction: doubleTextAction, url: url, text: text))
let removeAction = {
viewModel.select(range: linkAttributedRange)
viewModel.applyLinkOperation(.removeLinks)
Expand Down Expand Up @@ -119,7 +125,7 @@ private extension ComposerActionToolbar {
)
}

private func editTextAction(singleTextAction: @escaping ([String]) -> Void, url: String) -> AlertConfig.Action {
private func editTextAction(doubleTextAction: @escaping ([String]) -> Void, url: String, text: String) -> AlertConfig.Action {
.textAction(
title: "Ok",
textFieldsData: [
Expand All @@ -128,8 +134,13 @@ private extension ComposerActionToolbar {
placeholder: "URL",
defaultValue: url
),
.init(
accessibilityIdentifier: .linkTextTextField,
placeholder: "Text",
defaultValue: text
),
],
action: singleTextAction
action: doubleTextAction
)
}
}
11 changes: 7 additions & 4 deletions platforms/ios/example/WysiwygUITests/WysiwygUITests+Links.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,27 @@ extension WysiwygUITests {

// Edit
button(.linkButton).tap()
XCTAssertFalse(textField(.linkTextTextField).exists)
XCTAssertTrue(textField(.linkUrlTextField).exists)
XCTAssertTrue(textField(.linkTextTextField).exists)
textField(.linkUrlTextField).doubleTap()
textField(.linkUrlTextField).typeTextCharByChar("new_url")
textField(.linkTextTextField).doubleTap()
textField(.linkTextTextField).typeTextCharByChar("new text")
app.buttons["Ok"].tap()
assertTreeEquals(
"""
└>a "https://new_url"
└>"text"
└>"new text"
"""
)

// Remove
button(.linkButton).tap()
XCTAssertFalse(textField(.linkTextTextField).exists)
XCTAssertTrue(textField(.linkTextTextField).exists)
app.buttons["Remove"].tap()
assertTreeEquals(
"""
└>"text"
└>"new text"
"""
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protocol ComposerModelWrapperProtocol {
func enter() -> ComposerUpdate
func setLink(url: String, attributes: [Attribute]) -> ComposerUpdate
func setLinkWithText(url: String, text: String, attributes: [Attribute]) -> ComposerUpdate
func editLinkWithText(url: String, text: String, attributes: [Attribute]) -> ComposerUpdate
func insertMention(url: String, text: String, attributes: [Attribute]) -> ComposerUpdate
func insertMentionAtSuggestion(url: String, text: String, suggestion: SuggestionPattern, attributes: [Attribute]) -> ComposerUpdate
func removeLinks() -> ComposerUpdate
Expand Down Expand Up @@ -131,6 +132,10 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol {
func setLinkWithText(url: String, text: String, attributes: [Attribute]) -> ComposerUpdate {
execute { try $0.setLinkWithText(url: url, text: text, attributes: attributes) }
}

func editLinkWithText(url: String, text: String, attributes: [Attribute]) -> ComposerUpdate {
execute { try $0.editLinkWithText(url: url, text: text, attributes: attributes) }
}

func insertMention(url: String, text: String, attributes: [Attribute]) -> ComposerUpdate {
execute { try $0.insertMention(url: url, text: text, attributes: attributes) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ public extension WysiwygComposerViewModel {
switch linkOperation {
case let .createLink(urlString, text):
update = model.setLinkWithText(url: urlString, text: text, attributes: [])
case let .editLink(urlString, text):
update = model.editLinkWithText(url: urlString, text: text, attributes: [])
case let .setLink(urlString):
update = model.setLink(url: urlString, attributes: [])
case .removeLinks:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation

public enum WysiwygLinkOperation: Equatable {
case setLink(urlString: String)
case editLink(urlString: String, text: String)
case createLink(urlString: String, text: String)
case removeLinks
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension WysiwygComposerTests {
let url = "test_url"
ComposerModelWrapper()
.action { $0.setLinkWithText(url: url, text: "test", attributes: []) }
.assertLinkAction(.edit(url: "https://\(url)"))
.assertLinkAction(.edit(url: "https://\(url)", text: "test"))
}

func testSetLinkWithText() {
Expand Down

0 comments on commit 7c14c8a

Please sign in to comment.