mirror of
https://github.com/hanzoai/dev.git
synced 2026-11-21 14:22:49 +00:00
141 lines
5.8 KiB
Swift
141 lines
5.8 KiB
Swift
import ExpoModulesCore
|
|
import UIKit
|
|
|
|
enum HanzoComposerClipboard {
|
|
static let fragmentType = "app.hanzo.context-fragment"
|
|
|
|
// Pasted thread links become chips in JS; packages/shared/src/threadLinks.ts parses them.
|
|
static func hasThreadLink(_ text: String) -> Bool {
|
|
text.contains("hanzo-thread://v2/") || text.contains("https://hanzo.bot/threads/")
|
|
}
|
|
|
|
static func write(text: String, fragment: String) {
|
|
var items: [String: Any] = ["public.utf8-plain-text": text]
|
|
if let data = fragment.data(using: .utf8),
|
|
var payload = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
let records = payload["records"] as? [[String: Any]] {
|
|
var selected = records.filter { record in
|
|
guard let id = record["contextId"] as? String else { return false }
|
|
return text.contains("/\(id))")
|
|
}
|
|
let screenshots = Set(selected.compactMap { $0["screenshotContextId"] as? String })
|
|
selected.append(contentsOf: records.filter { screenshots.contains($0["contextId"] as? String ?? "") && !text.contains("/\($0["contextId"] as? String ?? ""))") })
|
|
payload["records"] = selected
|
|
if !selected.isEmpty, let encoded = try? JSONSerialization.data(withJSONObject: payload), let raw = String(data: encoded, encoding: .utf8) {
|
|
let attribute = raw.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? ""
|
|
let escaped = text.replacingOccurrences(of: "&", with: "&").replacingOccurrences(of: "<", with: "<").replacingOccurrences(of: ">", with: ">")
|
|
items[fragmentType] = encoded
|
|
items["public.html"] = Data("<pre data-hanzo-context-fragment=\"\(attribute)\">\(escaped)</pre>".utf8)
|
|
}
|
|
}
|
|
UIPasteboard.general.items = [items]
|
|
}
|
|
|
|
static func read() -> [String: String] {
|
|
let board = UIPasteboard.general
|
|
return [
|
|
"text": board.string ?? "",
|
|
"fragment": board.data(forPasteboardType: fragmentType).flatMap { String(data: $0, encoding: .utf8) } ?? "",
|
|
"html": board.data(forPasteboardType: "public.html").flatMap { String(data: $0, encoding: .utf8) } ?? "",
|
|
]
|
|
}
|
|
}
|
|
|
|
@ExpoModule("HanzoComposerEditor")
|
|
public class HanzoComposerEditorModule: Module {
|
|
// UIPasteboard is main-thread only, so the member is main-actor isolated.
|
|
@JS
|
|
@MainActor
|
|
func writeContextClipboard(text: String, fragment: String) async {
|
|
HanzoComposerClipboard.write(text: text, fragment: fragment)
|
|
}
|
|
|
|
public func definition() -> ModuleDefinition {
|
|
View(HanzoComposerEditorView.self) {
|
|
Prop("controlledDocumentJson") { (view: HanzoComposerEditorView, documentJson: String) in
|
|
view.setControlledDocumentJson(documentJson)
|
|
}
|
|
Prop("themeJson") { (view: HanzoComposerEditorView, themeJson: String) in
|
|
view.setThemeJson(themeJson)
|
|
}
|
|
Prop("clipboardFragment") { (view: HanzoComposerEditorView, fragment: String) in
|
|
view.setClipboardFragment(fragment)
|
|
}
|
|
Prop("placeholder") { (view: HanzoComposerEditorView, placeholder: String) in
|
|
view.setPlaceholder(placeholder)
|
|
}
|
|
Prop("fontFamily") { (view: HanzoComposerEditorView, fontFamily: String) in
|
|
view.setFontFamily(fontFamily)
|
|
}
|
|
Prop("fontSize") { (view: HanzoComposerEditorView, fontSize: Double) in
|
|
view.setFontSize(CGFloat(fontSize))
|
|
}
|
|
Prop("lineHeight") { (view: HanzoComposerEditorView, lineHeight: Double) in
|
|
view.setLineHeight(CGFloat(lineHeight))
|
|
}
|
|
Prop("contentInsetVertical") { (view: HanzoComposerEditorView, contentInsetVertical: Double) in
|
|
view.setContentInsetVertical(CGFloat(contentInsetVertical))
|
|
}
|
|
Prop("editable") { (view: HanzoComposerEditorView, editable: Bool) in
|
|
view.setEditable(editable)
|
|
}
|
|
Prop("readOnly") { (view: HanzoComposerEditorView, readOnly: Bool) in
|
|
view.setReadOnly(readOnly)
|
|
}
|
|
Prop("scrollEnabled") { (view: HanzoComposerEditorView, scrollEnabled: Bool) in
|
|
view.setScrollEnabled(scrollEnabled)
|
|
}
|
|
Prop("autoFocus") { (view: HanzoComposerEditorView, autoFocus: Bool) in
|
|
view.setAutoFocus(autoFocus)
|
|
}
|
|
Prop("autoCorrect") { (view: HanzoComposerEditorView, autoCorrect: Bool) in
|
|
view.setAutoCorrect(autoCorrect)
|
|
}
|
|
Prop("spellCheck") { (view: HanzoComposerEditorView, spellCheck: Bool) in
|
|
view.setSpellCheck(spellCheck)
|
|
}
|
|
Prop("submitTitle") { (view: HanzoComposerEditorView, title: String) in
|
|
view.setSubmitTitle(title)
|
|
}
|
|
Prop("alternateSubmitTitle") { (view: HanzoComposerEditorView, title: String) in
|
|
view.setAlternateSubmitTitle(title)
|
|
}
|
|
Prop("enterBehavior") { (view: HanzoComposerEditorView, behavior: String) in
|
|
view.setEnterBehavior(behavior)
|
|
}
|
|
Prop("textPasteThresholdBytes") { (view: HanzoComposerEditorView, threshold: Int) in
|
|
view.setTextPasteThresholdBytes(threshold)
|
|
}
|
|
Prop("maxInputChars") { (view: HanzoComposerEditorView, maxInputChars: Int) in
|
|
view.setMaxInputChars(maxInputChars)
|
|
}
|
|
|
|
Events(
|
|
"onComposerChange",
|
|
"onComposerSelectionChange",
|
|
"onComposerFocus",
|
|
"onComposerBlur",
|
|
"onComposerSubmit",
|
|
"onComposerPasteImages",
|
|
"onComposerContextPress",
|
|
"onComposerPasteContext",
|
|
"onComposerPasteText",
|
|
"onComposerContentSizeChange"
|
|
)
|
|
|
|
AsyncFunction("focus") { (view: HanzoComposerEditorView) in
|
|
view.focusEditor()
|
|
}
|
|
AsyncFunction("blur") { (view: HanzoComposerEditorView) in
|
|
view.blurEditor()
|
|
}
|
|
AsyncFunction("setSelection") { (view: HanzoComposerEditorView, start: Int, end: Int) in
|
|
view.setSelection(start: start, end: end)
|
|
}
|
|
AsyncFunction("readState") { (view: HanzoComposerEditorView) -> [String: Any] in
|
|
view.readState()
|
|
}
|
|
}
|
|
}
|
|
}
|