ios: add markdown format menu to compose

This commit is contained in:
Levitating Pineapple
2024-11-01 13:55:46 +02:00
parent 78510b6fd3
commit c0e673d63f
2 changed files with 50 additions and 1 deletions

View File

@@ -126,7 +126,7 @@ struct ChatItemForwardingView: View {
ChatItemForwardingView(
chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello")],
fromChatInfo: .direct(contact: Contact.sampleData),
composeState: Binding.constant(ComposeState(message: "hello"))
composeState: Binding.constant(ComposeState())
).environmentObject(CurrentColors.toAppTheme())
}

View File

@@ -130,6 +130,7 @@ private class CustomUITextField: UITextView, UITextViewDelegate {
}
func textView(_ textView: UITextView, editMenuForTextIn range: NSRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
let suggestedActions = [formatMenu] + suggestedActions
if !UIPasteboard.general.hasImages { return UIMenu(children: suggestedActions)}
return UIMenu(children: suggestedActions.map { elem in
if let elem = elem as? UIMenu {
@@ -208,6 +209,45 @@ private class CustomUITextField: UITextView, UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
onFocusChanged(false)
}
// MARK: Formatting
private var formatMenu: UIMenu {
func set(_ attribute: Any, for key: NSAttributedString.Key) {
textStorage.setAttributes([key: attribute], range: selectedRange)
delegate?.textViewDidChange?(self)
}
func systemImage(_ systemName: String, color: UIColor? = nil) -> UIImage? {
if let uiImage = UIImage(systemName: systemName) {
if let color {
uiImage.withTintColor(color, renderingMode: .alwaysOriginal)
} else {
uiImage
}
} else { nil }
}
return UIMenu(
title: "Format",
children: [
UIAction(image: systemImage("bold")) { _ in /*TODO*/ },
UIAction(image: systemImage("italic")) { _ in /*TODO*/ },
UIAction(image: systemImage("strikethrough")) { _ in /*TODO*/ },
UIAction(title: "Mono") { _ in /*TODO*/ },
UIMenu(
title: "Color",
children: Array<UIColor>([.red, .green, .blue, .yellow, .cyan, .magenta])
.map { color in
UIAction(image: systemImage("circle.fill", color: color)) { _ in
set(color, for: .foregroundColor)
}
}
),
UIAction(title: "Secret") { test in /*TODO*/ }
]
)
}
}
struct NativeTextEditor_Previews: PreviewProvider{
@@ -222,3 +262,12 @@ struct NativeTextEditor_Previews: PreviewProvider{
.fixedSize(horizontal: false, vertical: true)
}
}
func encodeMarkdown(_ string: String) -> NSAttributedString {
}
func decodeMarkdown(_ attributedString: NSAttributedString) -> String {
}