@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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