forked from wikimedia/wikipedia-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/wikimedia/wikipedia-ios int…
…o native-editor-reference
- Loading branch information
Showing
22 changed files
with
820 additions
and
28 deletions.
There are no files selected for viewing
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
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
107 changes: 107 additions & 0 deletions
107
...ditors/Source Editor/Formatter Extensions/WKSourceEditorFormatterLink+ButtonActions.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,107 @@ | ||
import Foundation | ||
import ComponentsObjC | ||
|
||
enum WKSourceEditorFormatterLinkButtonAction { | ||
case edit | ||
case insert | ||
} | ||
|
||
public struct WKSourceEditorFormatterLinkWizardParameters { | ||
public let editPageTitle: String? | ||
public let editPageLabel: String? | ||
public let insertSearchTerm: String? | ||
let preselectedTextRange: UITextRange | ||
} | ||
|
||
extension WKSourceEditorFormatterLink { | ||
|
||
func linkWizardParameters(action: WKSourceEditorFormatterLinkButtonAction, in textView: UITextView) -> WKSourceEditorFormatterLinkWizardParameters? { | ||
|
||
switch action { | ||
case .edit: | ||
expandSelectedRangeUpToNearestFormattingStrings(startingFormattingString: "[[", endingFormattingString: "]]", in: textView) | ||
|
||
guard let selectedTextRange = textView.selectedTextRange, | ||
let selectedText = textView.text(in: selectedTextRange) else { | ||
return nil | ||
} | ||
|
||
let splitText = selectedText.split(separator: "|").map {String($0)} | ||
|
||
switch splitText.count { | ||
case 1: | ||
return WKSourceEditorFormatterLinkWizardParameters(editPageTitle: splitText[0], editPageLabel: nil, insertSearchTerm: nil, preselectedTextRange: selectedTextRange) | ||
case 2: | ||
return WKSourceEditorFormatterLinkWizardParameters(editPageTitle: splitText[0], editPageLabel: splitText[1], insertSearchTerm: nil, preselectedTextRange: selectedTextRange) | ||
default: | ||
return nil | ||
} | ||
|
||
case .insert: | ||
|
||
guard let selectedTextRange = textView.selectedTextRange, | ||
let selectedText = textView.text(in: selectedTextRange) else { | ||
return nil | ||
} | ||
|
||
return WKSourceEditorFormatterLinkWizardParameters(editPageTitle: nil, editPageLabel: nil, insertSearchTerm: selectedText, preselectedTextRange: selectedTextRange) | ||
} | ||
|
||
|
||
} | ||
|
||
func insertLink(in textView: UITextView, pageTitle: String, preselectedTextRange: UITextRange) { | ||
var content = "[[\(pageTitle)]]" | ||
|
||
guard let selectedText = textView.text(in: preselectedTextRange) else { | ||
return | ||
} | ||
|
||
if pageTitle != selectedText { | ||
content = "[[\(pageTitle)|\(selectedText)]]" | ||
} | ||
|
||
textView.replace(preselectedTextRange, withText: content) | ||
|
||
if let newStartPosition = textView.position(from: preselectedTextRange.start, offset: 2), | ||
let newEndPosition = textView.position(from: preselectedTextRange.start, offset: content.count-2) { | ||
textView.selectedTextRange = textView.textRange(from: newStartPosition, to: newEndPosition) | ||
} | ||
|
||
} | ||
|
||
func editLink(in textView: UITextView, newPageTitle: String, newPageLabel: String?, preselectedTextRange: UITextRange) { | ||
if let newPageLabel, !newPageLabel.isEmpty { | ||
textView.replace(preselectedTextRange, withText: "\(newPageTitle)|\(newPageLabel)") | ||
} else { | ||
textView.replace(preselectedTextRange, withText: "\(newPageTitle)") | ||
} | ||
} | ||
|
||
func removeLink(in textView: UITextView, preselectedTextRange: UITextRange) { | ||
|
||
guard let selectedText = textView.text(in: preselectedTextRange) else { | ||
return | ||
} | ||
|
||
if let markupStartPosition = textView.position(from: preselectedTextRange.start, offset: -2), | ||
let markupEndPosition = textView.position(from: preselectedTextRange.end, offset: 2), | ||
let newSelectedRange = textView.textRange(from: markupStartPosition, to: markupEndPosition) { | ||
textView.replace(newSelectedRange, withText: selectedText) | ||
|
||
if let newStartPosition = textView.position(from: preselectedTextRange.start, offset: -2), | ||
let newEndPosition = textView.position(from: preselectedTextRange.end, offset: -2) { | ||
textView.selectedTextRange = textView.textRange(from: newStartPosition, to: newEndPosition) | ||
} | ||
} | ||
} | ||
|
||
func insertImage(wikitext: String, in textView: UITextView) { | ||
|
||
guard let selectedTextRange = textView.selectedTextRange else { | ||
return | ||
} | ||
|
||
textView.replace(selectedTextRange, withText: wikitext) | ||
} | ||
} |
Oops, something went wrong.