@g9tm/lynxcontrols-native 2.5.7 → 2.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +5 -0
  2. package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +24 -0
  3. package/android/src/main/java/com/g9lynx/g9native/blurhash/G9BlurhashUI.kt +76 -0
  4. package/android/src/main/java/com/g9lynx/g9native/blurhash/core/G9Blurhash.kt +100 -0
  5. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +94 -0
  6. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +192 -0
  7. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasUI.kt +162 -0
  8. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +1053 -0
  9. package/android/src/main/java/com/g9lynx/g9native/richtext/G9PictureSpan.kt +99 -0
  10. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichEditText.kt +458 -0
  11. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichStyle.kt +343 -0
  12. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichTextUI.kt +94 -0
  13. package/catalog/catalog.json +1 -1
  14. package/catalog/index.md +1 -1
  15. package/dist/.tsbuildinfo +1 -1
  16. package/ios/g9lynx-native-swift.podspec +20 -0
  17. package/ios/g9lynx-native.podspec +6 -2
  18. package/ios/src/G9LCBlurhashUI.h +14 -0
  19. package/ios/src/G9LCBlurhashUI.m +28 -0
  20. package/ios/src/G9LCCanvasUI.h +18 -0
  21. package/ios/src/G9LCCanvasUI.m +81 -0
  22. package/ios/src/G9LCRichTextUI.h +16 -0
  23. package/ios/src/G9LCRichTextUI.m +71 -0
  24. package/ios/swift/blurhash/G9LCBlurhashView.swift +165 -0
  25. package/ios/swift/canvas/G9LCCanvasCore.swift +848 -0
  26. package/ios/swift/canvas/G9LCCanvasView.swift +482 -0
  27. package/ios/swift/richtext/G9LCRichStyle.swift +238 -0
  28. package/ios/swift/richtext/G9LCRichTextView.swift +647 -0
  29. package/ios/swift/shared/G9LCColors.swift +78 -0
  30. package/ios/swift/shared/G9LCFonts.swift +38 -0
  31. package/ios/swift/shared/G9LCImages.swift +28 -0
  32. package/ios/swift/shared/G9LCJson.swift +73 -0
  33. package/package.json +6 -4
@@ -0,0 +1,238 @@
1
+ import UIKit
2
+
3
+ /// How a line style (`RtLine` of client/app `editor/protocol.ts`) becomes text attributes (the Android host's
4
+ /// G9LCRichStyle): paragraph indents (list nesting, quotes, boxes; the markers and boxes themselves are drawn by the
5
+ /// element's overlays), heading sizes, the marks of each run, inline objects (mentions, chips, dates) and block objects
6
+ /// (rules, pictures) as text attachments.
7
+ struct G9LCRichStyle {
8
+ static let indentStep: CGFloat = 24
9
+ static let quoteStep: CGFloat = 16
10
+
11
+ var text = UIColor(red: 0.91, green: 0.93, blue: 0.96, alpha: 1)
12
+ var text2 = UIColor(red: 0.71, green: 0.74, blue: 0.80, alpha: 1)
13
+ var text3 = UIColor(red: 0.49, green: 0.53, blue: 0.60, alpha: 1)
14
+ var accent = UIColor(red: 0.31, green: 0.49, blue: 1, alpha: 1)
15
+ var link = UIColor(red: 0.49, green: 0.71, blue: 1, alpha: 1)
16
+ var border = UIColor(red: 0.23, green: 0.25, blue: 0.32, alpha: 1)
17
+ var surface2 = UIColor(red: 0.11, green: 0.13, blue: 0.19, alpha: 1)
18
+ var info = UIColor(red: 0.23, green: 0.51, blue: 0.96, alpha: 1)
19
+ var success = UIColor(red: 0.09, green: 0.64, blue: 0.29, alpha: 1)
20
+ var warning = UIColor(red: 0.85, green: 0.47, blue: 0.02, alpha: 1)
21
+ var danger = UIColor(red: 0.86, green: 0.15, blue: 0.15, alpha: 1)
22
+ var pageRtl = false
23
+
24
+ mutating func setPalette(_ p: [String: Any]) {
25
+ func pick(_ key: String, _ fallback: UIColor) -> UIColor { G9LCColors.color(G9LCJson.string(p, key)) ?? fallback }
26
+ text = pick("text", text)
27
+ text2 = pick("text2", text2)
28
+ text3 = pick("text3", text3)
29
+ accent = pick("accent", accent)
30
+ link = pick("link", link)
31
+ border = pick("border", border)
32
+ surface2 = pick("surface2", surface2)
33
+ info = pick("info", info)
34
+ success = pick("success", success)
35
+ warning = pick("warning", warning)
36
+ danger = pick("danger", danger)
37
+ }
38
+
39
+ func font(size: CGFloat, weight: CGFloat) -> UIFont { G9LCFonts.text(size: size, weight: weight) }
40
+
41
+ func mono(size: CGFloat) -> UIFont { UIFont.monospacedSystemFont(ofSize: size, weight: .regular) }
42
+
43
+ func tone(_ name: String) -> UIColor {
44
+ switch name {
45
+ case "success": return success
46
+ case "warning": return warning
47
+ case "danger": return danger
48
+ default: return info
49
+ }
50
+ }
51
+
52
+ /// The start margin of a line (list nesting, quotes, boxes).
53
+ func margin(_ line: [String: Any]) -> CGFloat {
54
+ let ind = CGFloat(G9LCJson.int(line, "ind") ?? 0)
55
+ let q = CGFloat(G9LCJson.int(line, "q") ?? 0)
56
+ var margin = ind * G9LCRichStyle.indentStep + q * G9LCRichStyle.quoteStep
57
+ if G9LCJson.string(line, "t") == "code" || line["co"] != nil || line["cell"] != nil { margin += 10 }
58
+ return margin
59
+ }
60
+
61
+ /// Right-to-left: the explicit `dir`, else the first strong letter (Persian/Arabic/Hebrew), else the page's.
62
+ func isRtl(_ line: [String: Any], _ lineText: String) -> Bool {
63
+ switch G9LCJson.string(line, "dir") {
64
+ case "rtl": return true
65
+ case "ltr": return false
66
+ default: break
67
+ }
68
+ for scalar in lineText.unicodeScalars {
69
+ let v = scalar.value
70
+ if (0x0590...0x08FF).contains(v) || (0xFB1D...0xFDFF).contains(v) || (0xFE70...0xFEFF).contains(v) { return true }
71
+ if CharacterSet.letters.contains(scalar) { return false }
72
+ }
73
+ return pageRtl
74
+ }
75
+
76
+ /// The line's paragraph and base character attributes.
77
+ func base(_ line: [String: Any], lineText: String) -> [NSAttributedString.Key: Any] {
78
+ let t = G9LCJson.string(line, "t") ?? "p"
79
+ let font: UIFont
80
+ switch t {
81
+ case "h1": font = self.font(size: 25.6, weight: 700)
82
+ case "h2": font = self.font(size: 20.8, weight: 700)
83
+ case "h3": font = self.font(size: 17.9, weight: 700)
84
+ case "code": font = mono(size: 14)
85
+ default: font = self.font(size: 16, weight: 400)
86
+ }
87
+ let paragraph = NSMutableParagraphStyle()
88
+ let margin = self.margin(line)
89
+ paragraph.firstLineHeadIndent = margin
90
+ paragraph.headIndent = margin
91
+ paragraph.lineSpacing = 4
92
+ paragraph.paragraphSpacingBefore = t.hasPrefix("h") ? 8 : 0
93
+ paragraph.alignment = .natural
94
+ paragraph.baseWritingDirection = isRtl(line, lineText) ? .rightToLeft : .leftToRight
95
+ var color = text
96
+ if G9LCJson.bool(line, "done") == true { color = text3 } else if (G9LCJson.int(line, "q") ?? 0) > 0 { color = text2 }
97
+ var attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color, .paragraphStyle: paragraph]
98
+ if G9LCJson.bool(line, "done") == true { attributes[.strikethroughStyle] = NSUnderlineStyle.single.rawValue }
99
+ return attributes
100
+ }
101
+
102
+ /// Marks, inline objects, rules and pictures of the line `[start, end)` (end excludes its "\n").
103
+ func decorate(_ storage: NSTextStorage, line: [String: Any], start: Int, end: Int, width: CGFloat, reload: @escaping () -> Void) {
104
+ guard end > start else { return }
105
+ let t = G9LCJson.string(line, "t") ?? "p"
106
+ if t == "hr" {
107
+ storage.addAttribute(.attachment, value: rule(width: width - margin(line)), range: NSRange(location: start, length: 1))
108
+ return
109
+ }
110
+ if t == "img" {
111
+ let img = line["img"] as? [String: Any]
112
+ let attachment = picture(src: G9LCJson.string(img, "src") ?? "", alt: G9LCJson.string(img, "alt") ?? "", width: width - margin(line), reload: reload)
113
+ storage.addAttribute(.attachment, value: attachment, range: NSRange(location: start, length: 1))
114
+ return
115
+ }
116
+ guard let runs = line["r"] as? [Any] else { return }
117
+ let baseFont = (storage.attribute(.font, at: start, effectiveRange: nil) as? UIFont) ?? font(size: 16, weight: 400)
118
+ let heading = t.hasPrefix("h")
119
+ var at = start
120
+ for raw in runs {
121
+ guard let run = raw as? [String: Any] else { continue }
122
+ let n = G9LCJson.int(run, "n") ?? 0
123
+ let s = at
124
+ let e = min(end, at + n)
125
+ at += n
126
+ guard e > s else { continue }
127
+ let range = NSRange(location: s, length: e - s)
128
+ if let atom = run["a"] as? [String: Any] {
129
+ let off = G9LCJson.bool(atom, "off") == true
130
+ let color = off ? text3 : (G9LCColors.color(G9LCJson.string(atom, "c")) ?? accent)
131
+ let pill = chip(G9LCJson.string(atom, "t") ?? "", color: color, font: baseFont)
132
+ storage.addAttributes([.attachment: pill, .g9Atom: G9LCJson.string(atom, "k") ?? "chip"], range: NSRange(location: s, length: 1))
133
+ continue
134
+ }
135
+ let m = G9LCJson.int(run, "m") ?? 0
136
+ if m & 1 != 0 && !heading { storage.addAttribute(.font, value: font(size: baseFont.pointSize, weight: 700), range: range) }
137
+ if m & 2 != 0 { storage.addAttribute(.obliqueness, value: 0.18, range: range) }
138
+ if m & 4 != 0 || m & 32 != 0 { storage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range) }
139
+ if m & 8 != 0 { storage.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: range) }
140
+ if m & 16 != 0 {
141
+ storage.addAttributes([.font: mono(size: baseFont.pointSize * 0.9), .backgroundColor: surface2], range: range)
142
+ }
143
+ if m & 32 != 0 { storage.addAttribute(.foregroundColor, value: link, range: range) }
144
+ }
145
+ }
146
+
147
+ // MARK: - attachments
148
+
149
+ /// A mention / chip / date drawn as a tinted pill in the line.
150
+ private func chip(_ label: String, color: UIColor, font: UIFont) -> NSTextAttachment {
151
+ let labelFont = self.font(size: max(12, font.pointSize * 0.875), weight: 500)
152
+ let attributes: [NSAttributedString.Key: Any] = [.font: labelFont, .foregroundColor: color]
153
+ let textSize = (label as NSString).size(withAttributes: attributes)
154
+ let size = CGSize(width: ceil(textSize.width) + 12, height: ceil(textSize.height) + 4)
155
+ let image = UIGraphicsImageRenderer(size: size).image { _ in
156
+ let pill = UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), cornerRadius: size.height / 2)
157
+ color.withAlphaComponent(0.16).setFill()
158
+ pill.fill()
159
+ (label as NSString).draw(at: CGPoint(x: 6, y: 2), withAttributes: attributes)
160
+ }
161
+ let attachment = NSTextAttachment()
162
+ attachment.image = image
163
+ attachment.bounds = CGRect(x: 0, y: font.descender - 1, width: size.width, height: size.height)
164
+ return attachment
165
+ }
166
+
167
+ /// A horizontal rule across the line.
168
+ private func rule(width: CGFloat) -> NSTextAttachment {
169
+ let size = CGSize(width: max(20, width - 8), height: 18)
170
+ let color = border
171
+ let image = UIGraphicsImageRenderer(size: size).image { context in
172
+ color.setFill()
173
+ context.fill(CGRect(x: 0, y: size.height / 2, width: size.width, height: 1))
174
+ }
175
+ let attachment = NSTextAttachment()
176
+ attachment.image = image
177
+ attachment.bounds = CGRect(origin: .zero, size: size)
178
+ return attachment
179
+ }
180
+
181
+ /// A picture (loaded through the pinned session, cached by address); a box with its description until then.
182
+ private func picture(src: String, alt: String, width: CGFloat, reload: @escaping () -> Void) -> NSTextAttachment {
183
+ let maxWidth = max(40, width - 8)
184
+ let attachment = NSTextAttachment()
185
+ if let image = G9LCRichPictures.shared.image(src, loaded: reload) {
186
+ let scale = min(1, maxWidth / max(image.size.width, 1), 320 / max(image.size.height, 1))
187
+ attachment.image = image
188
+ attachment.bounds = CGRect(x: 0, y: 0, width: image.size.width * scale, height: image.size.height * scale)
189
+ return attachment
190
+ }
191
+ let size = CGSize(width: maxWidth, height: 120)
192
+ let fill = surface2
193
+ let color = text3
194
+ let caption = alt.isEmpty ? "…" : alt
195
+ let attributes: [NSAttributedString.Key: Any] = [.font: font(size: 13, weight: 400), .foregroundColor: color]
196
+ attachment.image = UIGraphicsImageRenderer(size: size).image { _ in
197
+ fill.setFill()
198
+ UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), cornerRadius: 8).fill()
199
+ let textSize = (caption as NSString).size(withAttributes: attributes)
200
+ (caption as NSString).draw(at: CGPoint(x: max(8, (size.width - textSize.width) / 2), y: (size.height - textSize.height) / 2), withAttributes: attributes)
201
+ }
202
+ attachment.bounds = CGRect(origin: .zero, size: size)
203
+ return attachment
204
+ }
205
+ }
206
+
207
+ /// Pictures of documents, downloaded once (2.5.8, from G9Hub) through `G9LCImages.session` (the host's to set); plain
208
+ /// http only on the local network. Main thread.
209
+ @objc(G9LCRichPictures)
210
+ final class G9LCRichPictures: NSObject {
211
+ static let shared = G9LCRichPictures()
212
+
213
+
214
+ private let cache = NSCache<NSString, UIImage>()
215
+ private var loading = Set<String>()
216
+ private var waiting: [String: [() -> Void]] = [:]
217
+
218
+ /// The picture when it is here; otherwise nil, and `loaded` runs once it arrived.
219
+ func image(_ src: String, loaded: @escaping () -> Void) -> UIImage? {
220
+ guard !src.isEmpty else { return nil }
221
+ if let image = cache.object(forKey: src as NSString) { return image }
222
+ waiting[src, default: []].append(loaded)
223
+ guard !loading.contains(src), let url = URL(string: src) else { return nil }
224
+ if url.scheme?.lowercased() == "http", !G9LCImages.isLocal(url.host) { return nil }
225
+ loading.insert(src)
226
+ G9LCImages.session.dataTask(with: url) { data, _, _ in
227
+ let image = data.flatMap { UIImage(data: $0) }
228
+ DispatchQueue.main.async {
229
+ self.loading.remove(src)
230
+ let callbacks = self.waiting.removeValue(forKey: src) ?? []
231
+ guard let image = image else { return }
232
+ self.cache.setObject(image, forKey: src as NSString)
233
+ callbacks.forEach { $0() }
234
+ }
235
+ }.resume()
236
+ return nil
237
+ }
238
+ }