Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3ComposerEditorModule.swift

132 lines5.3 KB

1import ExpoModulesCore
2import UIKit
3​
4enum T3ComposerClipboard {
5 static let fragmentType = "app.t3.context-fragment"
6​
7 static func write(text: String, fragment: String) {
8 var items: [String: Any] = ["public.utf8-plain-text": text]
9 if let data = fragment.data(using: .utf8),
10 var payload = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
11 let records = payload["records"] as? [[String: Any]] {
12 var selected = records.filter { record in
13 guard let id = record["contextId"] as? String else { return false }
14 return text.contains("/\(id))")
15 }
16 let screenshots = Set(selected.compactMap { $0["screenshotContextId"] as? String })
17 selected.append(contentsOf: records.filter { screenshots.contains($0["contextId"] as? String ?? "") && !text.contains("/\($0["contextId"] as? String ?? ""))") })
18 payload["records"] = selected
19 if !selected.isEmpty, let encoded = try? JSONSerialization.data(withJSONObject: payload), let raw = String(data: encoded, encoding: .utf8) {
20 let attribute = raw.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? ""
21 let escaped = text.replacingOccurrences(of: "&", with: "&amp;").replacingOccurrences(of: "<", with: "&lt;").replacingOccurrences(of: ">", with: "&gt;")
22 items[fragmentType] = encoded
23 items["public.html"] = Data("<pre data-t3-context-fragment=\"\(attribute)\">\(escaped)</pre>".utf8)
24 }
25 }
26 UIPasteboard.general.items = [items]
27 }
28​
29 static func read() -> [String: String] {
30 let board = UIPasteboard.general
31 return [
32 "text": board.string ?? "",
33 "fragment": board.data(forPasteboardType: fragmentType).flatMap { String(data: $0, encoding: .utf8) } ?? "",
34 "html": board.data(forPasteboardType: "public.html").flatMap { String(data: $0, encoding: .utf8) } ?? "",
35 ]
36 }
38​
39@ExpoModule("T3ComposerEditor")
40public class T3ComposerEditorModule: Module {
41 // UIPasteboard is main-thread only, so the member is main-actor isolated.
42 @JS
43 @MainActor
44 func writeContextClipboard(text: String, fragment: String) async {
45 T3ComposerClipboard.write(text: text, fragment: fragment)
46 }
47​
48 public func definition() -> ModuleDefinition {
49 View(T3ComposerEditorView.self) {
50 Prop("controlledDocumentJson") { (view: T3ComposerEditorView, documentJson: String) in
51 view.setControlledDocumentJson(documentJson)
52 }
53 Prop("themeJson") { (view: T3ComposerEditorView, themeJson: String) in
54 view.setThemeJson(themeJson)
55 }
56 Prop("clipboardFragment") { (view: T3ComposerEditorView, fragment: String) in
57 view.setClipboardFragment(fragment)
58 }
59 Prop("placeholder") { (view: T3ComposerEditorView, placeholder: String) in
60 view.setPlaceholder(placeholder)
61 }
62 Prop("fontFamily") { (view: T3ComposerEditorView, fontFamily: String) in
63 view.setFontFamily(fontFamily)
64 }
65 Prop("fontSize") { (view: T3ComposerEditorView, fontSize: Double) in
66 view.setFontSize(CGFloat(fontSize))
67 }
68 Prop("lineHeight") { (view: T3ComposerEditorView, lineHeight: Double) in
69 view.setLineHeight(CGFloat(lineHeight))
70 }
71 Prop("contentInsetVertical") { (view: T3ComposerEditorView, contentInsetVertical: Double) in
72 view.setContentInsetVertical(CGFloat(contentInsetVertical))
73 }
74 Prop("editable") { (view: T3ComposerEditorView, editable: Bool) in
75 view.setEditable(editable)
76 }
77 Prop("readOnly") { (view: T3ComposerEditorView, readOnly: Bool) in
78 view.setReadOnly(readOnly)
79 }
80 Prop("scrollEnabled") { (view: T3ComposerEditorView, scrollEnabled: Bool) in
81 view.setScrollEnabled(scrollEnabled)
82 }
83 Prop("autoFocus") { (view: T3ComposerEditorView, autoFocus: Bool) in
84 view.setAutoFocus(autoFocus)
85 }
86 Prop("autoCorrect") { (view: T3ComposerEditorView, autoCorrect: Bool) in
87 view.setAutoCorrect(autoCorrect)
88 }
89 Prop("spellCheck") { (view: T3ComposerEditorView, spellCheck: Bool) in
90 view.setSpellCheck(spellCheck)
91 }
92 Prop("submitTitle") { (view: T3ComposerEditorView, title: String) in
93 view.setSubmitTitle(title)
94 }
95 Prop("alternateSubmitTitle") { (view: T3ComposerEditorView, title: String) in
96 view.setAlternateSubmitTitle(title)
97 }
98 Prop("enterBehavior") { (view: T3ComposerEditorView, behavior: String) in
99 view.setEnterBehavior(behavior)
100 }
101 Prop("textPasteThresholdBytes") { (view: T3ComposerEditorView, threshold: Int) in
102 view.setTextPasteThresholdBytes(threshold)
103 }
104 Prop("maxInputChars") { (view: T3ComposerEditorView, maxInputChars: Int) in
105 view.setMaxInputChars(maxInputChars)
106 }
107​
108 Events(
109 "onComposerChange",
110 "onComposerSelectionChange",
111 "onComposerFocus",
112 "onComposerBlur",
113 "onComposerSubmit",
114 "onComposerPasteImages",
115 "onComposerContextPress",
116 "onComposerPasteContext",
117 "onComposerPasteText",
118 "onComposerContentSizeChange"
119 )
120​
121 AsyncFunction("focus") { (view: T3ComposerEditorView) in
122 view.focusEditor()
123 }
124 AsyncFunction("blur") { (view: T3ComposerEditorView) in
125 view.blurEditor()
126 }
127 AsyncFunction("setSelection") { (view: T3ComposerEditorView, start: Int, end: Int) in
128 view.setSelection(start: start, end: end)
129 }
130 }
131 }