@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.
- package/README.md +5 -0
- package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +24 -0
- package/android/src/main/java/com/g9lynx/g9native/blurhash/G9BlurhashUI.kt +76 -0
- package/android/src/main/java/com/g9lynx/g9native/blurhash/core/G9Blurhash.kt +100 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +94 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +192 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasUI.kt +162 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +1053 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9PictureSpan.kt +99 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichEditText.kt +458 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichStyle.kt +343 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichTextUI.kt +94 -0
- package/catalog/catalog.json +1 -1
- package/catalog/index.md +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/ios/g9lynx-native-swift.podspec +20 -0
- package/ios/g9lynx-native.podspec +6 -2
- package/ios/src/G9LCBlurhashUI.h +14 -0
- package/ios/src/G9LCBlurhashUI.m +28 -0
- package/ios/src/G9LCCanvasUI.h +18 -0
- package/ios/src/G9LCCanvasUI.m +81 -0
- package/ios/src/G9LCRichTextUI.h +16 -0
- package/ios/src/G9LCRichTextUI.m +71 -0
- package/ios/swift/blurhash/G9LCBlurhashView.swift +165 -0
- package/ios/swift/canvas/G9LCCanvasCore.swift +848 -0
- package/ios/swift/canvas/G9LCCanvasView.swift +482 -0
- package/ios/swift/richtext/G9LCRichStyle.swift +238 -0
- package/ios/swift/richtext/G9LCRichTextView.swift +647 -0
- package/ios/swift/shared/G9LCColors.swift +78 -0
- package/ios/swift/shared/G9LCFonts.swift +38 -0
- package/ios/swift/shared/G9LCImages.swift +28 -0
- package/ios/swift/shared/G9LCJson.swift +73 -0
- package/package.json +6 -4
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
package com.g9lynx.g9native.richtext
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.Color
|
|
6
|
+
import android.graphics.Paint
|
|
7
|
+
import android.graphics.RectF
|
|
8
|
+
import android.graphics.Typeface
|
|
9
|
+
import android.text.Layout
|
|
10
|
+
import android.text.Spannable
|
|
11
|
+
import android.text.Spanned
|
|
12
|
+
import android.text.TextPaint
|
|
13
|
+
import android.text.style.ForegroundColorSpan
|
|
14
|
+
import android.text.style.LeadingMarginSpan
|
|
15
|
+
import android.text.style.LineBackgroundSpan
|
|
16
|
+
import android.text.style.MetricAffectingSpan
|
|
17
|
+
import android.text.style.ReplacementSpan
|
|
18
|
+
import android.text.style.StrikethroughSpan
|
|
19
|
+
import android.text.style.StyleSpan
|
|
20
|
+
import android.text.style.UnderlineSpan
|
|
21
|
+
import android.util.TypedValue
|
|
22
|
+
import com.lynx.tasm.behavior.shadow.text.TypefaceCache
|
|
23
|
+
import org.json.JSONArray
|
|
24
|
+
import org.json.JSONObject
|
|
25
|
+
import kotlin.math.max
|
|
26
|
+
import kotlin.math.min
|
|
27
|
+
|
|
28
|
+
/** Every span the element sets (the ones it removes when a line is restyled). */
|
|
29
|
+
interface G9Span
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* How a line style (`RtLine` of client/app `editor/protocol.ts`) becomes spans: paragraph decorations (list markers,
|
|
33
|
+
* checkboxes, quote bars, code and callout boxes, table cells) in one [G9ParagraphSpan], heading sizes and the marks of
|
|
34
|
+
* each run as character spans, inline objects (mentions, chips, dates) and block objects (rules, pictures) as
|
|
35
|
+
* replacement spans drawn here.
|
|
36
|
+
*/
|
|
37
|
+
class G9RichStyle(private val context: Context) {
|
|
38
|
+
var text = Color.parseColor("#e8ecf4")
|
|
39
|
+
var text2 = Color.parseColor("#b4bccb")
|
|
40
|
+
var text3 = Color.parseColor("#7d8698")
|
|
41
|
+
var accent = Color.parseColor("#4f7cff")
|
|
42
|
+
var link = Color.parseColor("#7cb4ff")
|
|
43
|
+
var border = Color.parseColor("#3a4152")
|
|
44
|
+
var surface2 = Color.parseColor("#1c2230")
|
|
45
|
+
var selection = Color.parseColor("#554f7cff")
|
|
46
|
+
var info = Color.parseColor("#3b82f6")
|
|
47
|
+
var success = Color.parseColor("#16a34a")
|
|
48
|
+
var warning = Color.parseColor("#d97706")
|
|
49
|
+
var danger = Color.parseColor("#dc2626")
|
|
50
|
+
var pageRtl = false
|
|
51
|
+
/** A picture finished loading: the editor restyles. */
|
|
52
|
+
var onAsset: (() -> Unit)? = null
|
|
53
|
+
|
|
54
|
+
private fun dp(value: Float): Float = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.resources.displayMetrics)
|
|
55
|
+
|
|
56
|
+
val indentStep get() = dp(24f)
|
|
57
|
+
val quoteStep get() = dp(16f)
|
|
58
|
+
|
|
59
|
+
fun setPalette(p: JSONObject) {
|
|
60
|
+
text = color(p.optString("text"), text)
|
|
61
|
+
text2 = color(p.optString("text2"), text2)
|
|
62
|
+
text3 = color(p.optString("text3"), text3)
|
|
63
|
+
accent = color(p.optString("accent"), accent)
|
|
64
|
+
link = color(p.optString("link"), link)
|
|
65
|
+
border = color(p.optString("border"), border)
|
|
66
|
+
surface2 = color(p.optString("surface2"), surface2)
|
|
67
|
+
selection = color(p.optString("selection"), selection)
|
|
68
|
+
info = color(p.optString("info"), info)
|
|
69
|
+
success = color(p.optString("success"), success)
|
|
70
|
+
warning = color(p.optString("warning"), warning)
|
|
71
|
+
danger = color(p.optString("danger"), danger)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The start margin of a line (list nesting, quotes, boxes). */
|
|
75
|
+
fun marginOf(line: JSONObject): Int {
|
|
76
|
+
val ind = line.optInt("ind")
|
|
77
|
+
val q = line.optInt("q")
|
|
78
|
+
var margin = ind * indentStep + q * quoteStep
|
|
79
|
+
val t = line.optString("t")
|
|
80
|
+
if (t == "code" || line.has("co") || line.has("cell")) margin += dp(10f)
|
|
81
|
+
return margin.toInt()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Sets the spans of one line `[start, end)` (end excludes the line's "\n"). */
|
|
85
|
+
fun apply(text: Spannable, line: JSONObject, start: Int, end: Int, width: Int) {
|
|
86
|
+
val paragraphEnd = if (end < text.length) end + 1 else end
|
|
87
|
+
val t = line.optString("t", "p")
|
|
88
|
+
val margin = marginOf(line)
|
|
89
|
+
val decorated = margin > 0 || line.has("mk") || t == "code" || line.has("co") || line.has("cell") || line.has("q")
|
|
90
|
+
if (decorated && paragraphEnd > start) text.setSpan(G9ParagraphSpan(this, line, margin), start, paragraphEnd, Spanned.SPAN_PARAGRAPH)
|
|
91
|
+
if (end <= start) return
|
|
92
|
+
when (t) {
|
|
93
|
+
"h1" -> heading(text, start, end, 1.6f)
|
|
94
|
+
"h2" -> heading(text, start, end, 1.3f)
|
|
95
|
+
"h3" -> heading(text, start, end, 1.12f)
|
|
96
|
+
"code" -> text.setSpan(G9FontSpan(Typeface.MONOSPACE, 0.88f, null), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
97
|
+
"hr" -> {
|
|
98
|
+
text.setSpan(G9RuleSpan(this, width), start, min(end, start + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
"img" -> {
|
|
102
|
+
val img = line.optJSONObject("img")
|
|
103
|
+
text.setSpan(G9PictureSpan(this, img?.optString("src") ?: "", img?.optString("alt") ?: "", width), start, min(end, start + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (line.optBoolean("done")) {
|
|
108
|
+
text.setSpan(G9Strike(), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
109
|
+
text.setSpan(G9Color(text3), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
110
|
+
} else if (line.optInt("q") > 0) text.setSpan(G9Color(text2), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
111
|
+
runs(text, line.optJSONArray("r"), start, end)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private fun heading(text: Spannable, start: Int, end: Int, scale: Float) {
|
|
115
|
+
val bold = TypefaceCache.getCachedTypeface("G9Bold", Typeface.NORMAL) ?: Typeface.DEFAULT_BOLD
|
|
116
|
+
text.setSpan(G9FontSpan(bold, scale, null), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private fun runs(text: Spannable, runs: JSONArray?, start: Int, end: Int) {
|
|
120
|
+
if (runs == null) return
|
|
121
|
+
var at = start
|
|
122
|
+
for (i in 0 until runs.length()) {
|
|
123
|
+
val run = runs.getJSONObject(i)
|
|
124
|
+
val n = run.optInt("n")
|
|
125
|
+
val s = at
|
|
126
|
+
val e = min(end, at + n)
|
|
127
|
+
at += n
|
|
128
|
+
if (e <= s) continue
|
|
129
|
+
val atom = run.optJSONObject("a")
|
|
130
|
+
if (atom != null) {
|
|
131
|
+
val chipColor = if (atom.has("c")) color(atom.optString("c"), accent) else accent
|
|
132
|
+
text.setSpan(G9ChipSpan(this, atom.optString("t"), atom.optString("k"), if (atom.optBoolean("off")) text3 else chipColor), s, s + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
val m = run.optInt("m")
|
|
136
|
+
if (m and 1 != 0) text.setSpan(G9FontSpan(TypefaceCache.getCachedTypeface("G9Bold", Typeface.NORMAL) ?: Typeface.DEFAULT_BOLD, 1f, null), s, e, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
137
|
+
if (m and 2 != 0) text.setSpan(G9Italic(), s, e, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
138
|
+
if (m and 4 != 0 || m and 32 != 0) text.setSpan(G9Underline(), s, e, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
139
|
+
if (m and 8 != 0) text.setSpan(G9Strike(), s, e, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
140
|
+
if (m and 16 != 0) text.setSpan(G9FontSpan(Typeface.MONOSPACE, 0.9f, surface2), s, e, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
141
|
+
if (m and 32 != 0) text.setSpan(G9Color(link), s, e, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
fun toneColor(tone: String): Int = when (tone) {
|
|
146
|
+
"success" -> success
|
|
147
|
+
"warning" -> warning
|
|
148
|
+
"danger" -> danger
|
|
149
|
+
else -> info
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
companion object {
|
|
153
|
+
/** `#rgb`, `#rrggbb` or `#rrggbbaa` (CSS order) → an Android colour. */
|
|
154
|
+
fun color(value: String?, fallback: Int): Int {
|
|
155
|
+
if (value.isNullOrEmpty() || !value.startsWith("#")) return fallback
|
|
156
|
+
return runCatching {
|
|
157
|
+
when (value.length) {
|
|
158
|
+
4 -> Color.parseColor("#${value[1]}${value[1]}${value[2]}${value[2]}${value[3]}${value[3]}")
|
|
159
|
+
7 -> Color.parseColor(value)
|
|
160
|
+
9 -> Color.parseColor("#${value.substring(7, 9)}${value.substring(1, 7)}")
|
|
161
|
+
else -> fallback
|
|
162
|
+
}
|
|
163
|
+
}.getOrDefault(fallback)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
fun withAlpha(color: Int, alpha: Int): Int = Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color))
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
class G9Italic : StyleSpan(Typeface.ITALIC), G9Span
|
|
171
|
+
class G9Underline : UnderlineSpan(), G9Span
|
|
172
|
+
class G9Strike : StrikethroughSpan(), G9Span
|
|
173
|
+
class G9Color(color: Int) : ForegroundColorSpan(color), G9Span
|
|
174
|
+
|
|
175
|
+
/** A typeface (G9's weights, monospace), a size factor and an optional background. */
|
|
176
|
+
class G9FontSpan(private val face: Typeface, private val scale: Float, private val background: Int?) : MetricAffectingSpan(), G9Span {
|
|
177
|
+
override fun updateDrawState(paint: TextPaint) {
|
|
178
|
+
update(paint)
|
|
179
|
+
if (background != null) paint.bgColor = background
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
override fun updateMeasureState(paint: TextPaint) = update(paint)
|
|
183
|
+
|
|
184
|
+
private fun update(paint: TextPaint) {
|
|
185
|
+
val italic = paint.typeface?.isItalic == true
|
|
186
|
+
paint.typeface = if (italic) Typeface.create(face, Typeface.ITALIC) else face
|
|
187
|
+
if (scale != 1f) paint.textSize = paint.textSize * scale
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** List markers, checkboxes, quote bars, code / callout / cell boxes of one paragraph (UpdateLayout: adding it re-wraps the paragraph — DynamicLayout reflows only for such spans). */
|
|
192
|
+
class G9ParagraphSpan(private val style: G9RichStyle, private val line: JSONObject, private val margin: Int) : LeadingMarginSpan, LineBackgroundSpan, android.text.style.UpdateLayout, G9Span {
|
|
193
|
+
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
194
|
+
|
|
195
|
+
override fun getLeadingMargin(first: Boolean): Int = margin
|
|
196
|
+
|
|
197
|
+
override fun drawLeadingMargin(c: Canvas, p: Paint, x: Int, dir: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence, start: Int, end: Int, first: Boolean, layout: Layout?) {
|
|
198
|
+
val q = line.optInt("q")
|
|
199
|
+
val density = style.indentStep / 24f
|
|
200
|
+
// Quote bars at the start side.
|
|
201
|
+
if (q > 0) {
|
|
202
|
+
paint.color = style.border
|
|
203
|
+
for (i in 0 until q) {
|
|
204
|
+
val bx = x + dir * (i * style.quoteStep + density * 2)
|
|
205
|
+
c.drawRect(min(bx, bx + dir * density * 3), top.toFloat(), max(bx, bx + dir * density * 3), bottom.toFloat(), paint)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
val marker = line.optJSONObject("mk") ?: return
|
|
209
|
+
if (!first || layout == null) return
|
|
210
|
+
val ind = max(1, line.optInt("ind"))
|
|
211
|
+
// The marker sits in the last indent step, before the text.
|
|
212
|
+
val center = x + dir * (q * style.quoteStep + (ind - 0.5f) * style.indentStep)
|
|
213
|
+
val textCenter = baseline + (p.ascent() + p.descent()) / 2
|
|
214
|
+
when (marker.optString("k")) {
|
|
215
|
+
"t" -> {
|
|
216
|
+
val size = density * 16
|
|
217
|
+
val rect = RectF(center - size / 2, textCenter - size / 2, center + size / 2, textCenter + size / 2)
|
|
218
|
+
if (marker.optBoolean("c")) {
|
|
219
|
+
paint.style = Paint.Style.FILL
|
|
220
|
+
paint.color = style.accent
|
|
221
|
+
c.drawRoundRect(rect, density * 4, density * 4, paint)
|
|
222
|
+
paint.color = Color.WHITE
|
|
223
|
+
paint.style = Paint.Style.STROKE
|
|
224
|
+
paint.strokeWidth = density * 2
|
|
225
|
+
val path = android.graphics.Path().apply {
|
|
226
|
+
moveTo(rect.left + size * 0.22f, rect.centerY())
|
|
227
|
+
lineTo(rect.left + size * 0.42f, rect.bottom - size * 0.28f)
|
|
228
|
+
lineTo(rect.right - size * 0.2f, rect.top + size * 0.28f)
|
|
229
|
+
}
|
|
230
|
+
c.drawPath(path, paint)
|
|
231
|
+
} else {
|
|
232
|
+
paint.style = Paint.Style.STROKE
|
|
233
|
+
paint.strokeWidth = density * 1.5f
|
|
234
|
+
paint.color = style.text3
|
|
235
|
+
c.drawRoundRect(rect, density * 4, density * 4, paint)
|
|
236
|
+
}
|
|
237
|
+
paint.style = Paint.Style.FILL
|
|
238
|
+
}
|
|
239
|
+
"n" -> {
|
|
240
|
+
val label = marker.optString("n")
|
|
241
|
+
val old = p.color
|
|
242
|
+
p.color = style.text2
|
|
243
|
+
val w = p.measureText(label)
|
|
244
|
+
c.drawText(label, center - w / 2, baseline.toFloat(), p)
|
|
245
|
+
p.color = old
|
|
246
|
+
}
|
|
247
|
+
else -> {
|
|
248
|
+
paint.color = style.text2
|
|
249
|
+
val r = density * 2.6f
|
|
250
|
+
when ((ind - 1) % 3) {
|
|
251
|
+
0 -> c.drawCircle(center, textCenter, r, paint)
|
|
252
|
+
1 -> {
|
|
253
|
+
paint.style = Paint.Style.STROKE
|
|
254
|
+
paint.strokeWidth = density * 1.2f
|
|
255
|
+
c.drawCircle(center, textCenter, r, paint)
|
|
256
|
+
paint.style = Paint.Style.FILL
|
|
257
|
+
}
|
|
258
|
+
else -> c.drawRect(center - r, textCenter - r, center + r, textCenter + r, paint)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
override fun drawBackground(c: Canvas, p: Paint, left: Int, right: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence, start: Int, end: Int, lineNumber: Int) {
|
|
265
|
+
val t = line.optString("t")
|
|
266
|
+
val density = style.indentStep / 24f
|
|
267
|
+
val inset = (line.optInt("ind") * style.indentStep + line.optInt("q") * style.quoteStep).toInt()
|
|
268
|
+
// The start side: an explicit direction, else the paragraph's first strong letter (like the layout).
|
|
269
|
+
val dir = line.optString("dir")
|
|
270
|
+
val rtl = when (dir) {
|
|
271
|
+
"rtl" -> true
|
|
272
|
+
"ltr" -> false
|
|
273
|
+
else -> start < end && android.text.TextDirectionHeuristics.FIRSTSTRONG_LTR.isRtl(text, start, end - start)
|
|
274
|
+
}
|
|
275
|
+
val l = if (rtl) left else left + inset
|
|
276
|
+
val r = if (rtl) right - inset else right
|
|
277
|
+
if (t == "code") {
|
|
278
|
+
paint.color = style.surface2
|
|
279
|
+
c.drawRect(l.toFloat(), top.toFloat(), r.toFloat(), bottom.toFloat(), paint)
|
|
280
|
+
}
|
|
281
|
+
val tone = line.optString("co")
|
|
282
|
+
if (tone.isNotEmpty()) {
|
|
283
|
+
paint.color = G9RichStyle.withAlpha(style.toneColor(tone), 30)
|
|
284
|
+
c.drawRect(l.toFloat(), top.toFloat(), r.toFloat(), bottom.toFloat(), paint)
|
|
285
|
+
paint.color = style.toneColor(tone)
|
|
286
|
+
if (rtl) c.drawRect(r - density * 3, top.toFloat(), r.toFloat(), bottom.toFloat(), paint)
|
|
287
|
+
else c.drawRect(l.toFloat(), top.toFloat(), l + density * 3, bottom.toFloat(), paint)
|
|
288
|
+
}
|
|
289
|
+
val cell = line.optJSONObject("cell")
|
|
290
|
+
if (cell != null) {
|
|
291
|
+
if (cell.optBoolean("h")) {
|
|
292
|
+
paint.color = style.surface2
|
|
293
|
+
c.drawRect(l.toFloat(), top.toFloat(), r.toFloat(), bottom.toFloat(), paint)
|
|
294
|
+
}
|
|
295
|
+
paint.color = style.border
|
|
296
|
+
if (rtl) c.drawRect(r - density * 2, top.toFloat(), r.toFloat(), bottom.toFloat(), paint)
|
|
297
|
+
else c.drawRect(l.toFloat(), top.toFloat(), l + density * 2, bottom.toFloat(), paint)
|
|
298
|
+
if (cell.optBoolean("top")) c.drawRect(l.toFloat(), top.toFloat(), r.toFloat(), top + density, paint)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** An inline object (mention, chip, date): its label in a tinted pill, one character wide in the text. */
|
|
304
|
+
class G9ChipSpan(private val style: G9RichStyle, private val label: String, private val kind: String, private val color: Int) : ReplacementSpan(), G9Span {
|
|
305
|
+
private val pad get() = style.indentStep / 24f * 6
|
|
306
|
+
|
|
307
|
+
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
|
|
308
|
+
if (fm != null) paint.getFontMetricsInt(fm)
|
|
309
|
+
return (paint.measureText(label) + pad * 2 + pad).toInt()
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
|
|
313
|
+
val width = paint.measureText(label) + pad * 2
|
|
314
|
+
val rect = RectF(x + pad / 2, y + paint.ascent() - pad / 3, x + pad / 2 + width, y + paint.descent() + pad / 3)
|
|
315
|
+
val old = paint.color
|
|
316
|
+
val oldStyle = paint.style
|
|
317
|
+
if (kind != "mention") {
|
|
318
|
+
paint.color = G9RichStyle.withAlpha(color, 38)
|
|
319
|
+
paint.style = Paint.Style.FILL
|
|
320
|
+
canvas.drawRoundRect(rect, pad * 1.4f, pad * 1.4f, paint)
|
|
321
|
+
}
|
|
322
|
+
paint.color = color
|
|
323
|
+
canvas.drawText(label, x + pad / 2 + pad, y.toFloat(), paint)
|
|
324
|
+
paint.color = old
|
|
325
|
+
paint.style = oldStyle
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** A horizontal rule: the object character drawn as a line across the text width. */
|
|
330
|
+
class G9RuleSpan(private val style: G9RichStyle, private val width: Int) : ReplacementSpan(), G9Span {
|
|
331
|
+
override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
|
|
332
|
+
if (fm != null) paint.getFontMetricsInt(fm)
|
|
333
|
+
return max(1, width - 2)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
override fun draw(canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
|
|
337
|
+
val old = paint.color
|
|
338
|
+
paint.color = style.border
|
|
339
|
+
val middle = (top + bottom) / 2f
|
|
340
|
+
canvas.drawRect(x, middle - 1, x + max(1, width - 2), middle + 1, paint)
|
|
341
|
+
paint.color = old
|
|
342
|
+
}
|
|
343
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
package com.g9lynx.g9native.richtext
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import com.lynx.react.bridge.Callback
|
|
5
|
+
import com.lynx.react.bridge.ReadableMap
|
|
6
|
+
import com.lynx.tasm.behavior.Behavior
|
|
7
|
+
import com.lynx.tasm.behavior.LynxContext
|
|
8
|
+
import com.lynx.tasm.behavior.LynxProp
|
|
9
|
+
import com.lynx.tasm.behavior.LynxUIMethod
|
|
10
|
+
import com.lynx.tasm.behavior.LynxUIMethodConstants
|
|
11
|
+
import com.lynx.tasm.behavior.ui.LynxUI
|
|
12
|
+
import com.lynx.tasm.event.LynxDetailEvent
|
|
13
|
+
import org.json.JSONObject
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `<g9-richtext editable placeholder palette page-dir bottom-inset bindrtedit bindrtselect bindrtapplied bindrtaction>`
|
|
17
|
+
* (Plan-Lynx §6, W10): the editable surface of a G9 document. The document model (Yjs, TipTap's schema) is the app's
|
|
18
|
+
* (client/app `features/docs/editor`); this element shows the flat text with its line styles in an [G9RichEditText]
|
|
19
|
+
* (an `EditText` with spans — the IME, selection handles, spell checking, Persian and mixed directions are Android's)
|
|
20
|
+
* and reports every change. Protocol: client/app `editor/protocol.ts`; the element's side is
|
|
21
|
+
* `editor/element-core.ts`, which [G9RichEditText] follows.
|
|
22
|
+
*
|
|
23
|
+
* The app sends messages with the UI method `apply({ json })`; the element answers with events.
|
|
24
|
+
*/
|
|
25
|
+
class G9RichTextUI(context: LynxContext) : LynxUI<G9RichEditText>(context) {
|
|
26
|
+
// LynxUI's constructor calls createView before this class's initialisers run: no initialiser here.
|
|
27
|
+
private lateinit var editor: G9RichEditText
|
|
28
|
+
|
|
29
|
+
override fun createView(context: Context): G9RichEditText {
|
|
30
|
+
val view = G9RichEditText(context)
|
|
31
|
+
view.emitter = { name, detail -> emit(name, detail) }
|
|
32
|
+
editor = view
|
|
33
|
+
return view
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@LynxProp(name = "editable", defaultBoolean = false)
|
|
37
|
+
fun setEditable(value: Boolean) {
|
|
38
|
+
mView?.setEditableMode(value)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@LynxProp(name = "placeholder")
|
|
42
|
+
fun setPlaceholder(value: String?) {
|
|
43
|
+
mView?.hint = value ?: ""
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@LynxProp(name = "palette")
|
|
47
|
+
fun setPalette(value: String?) {
|
|
48
|
+
if (value.isNullOrEmpty()) return
|
|
49
|
+
runCatching { mView?.setPalette(JSONObject(value)) }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@LynxProp(name = "page-dir")
|
|
53
|
+
fun setPageDir(value: String?) {
|
|
54
|
+
mView?.setPageDirection(value == "rtl")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@LynxProp(name = "bottom-inset", defaultDouble = 0.0)
|
|
58
|
+
fun setBottomInset(value: Double) {
|
|
59
|
+
mView?.setBottomInset(value)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The app's message (`invoke({ method: 'apply', params: { json } })`). */
|
|
63
|
+
@LynxUIMethod
|
|
64
|
+
fun apply(params: ReadableMap, callback: Callback) {
|
|
65
|
+
val json = params.getString("json")
|
|
66
|
+
if (!json.isNullOrEmpty()) runCatching { mView?.applyMessage(JSONObject(json)) }.onFailure { android.util.Log.w(TAG, "apply failed", it) }
|
|
67
|
+
callback.invoke(LynxUIMethodConstants.SUCCESS)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private fun emit(name: String, detail: Map<String, Any?>) {
|
|
71
|
+
// The event adds its own fields to the map: a mutable copy.
|
|
72
|
+
lynxContext.eventEmitter.sendCustomEvent(LynxDetailEvent(sign, name, HashMap(detail)))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
override fun destroy() {
|
|
76
|
+
if (::editor.isInitialized) editor.emitter = null
|
|
77
|
+
super.destroy()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private const val TAG = "G9RichText"
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The behaviour of `<g9-richtext>` (`@g9tm/lynxcontrols-native`, 2.5.8). Add it with the host's own:
|
|
85
|
+
* `LynxViewBuilder().addBehaviors(hostBehaviors + G9RichTextBehaviors.create())`.
|
|
86
|
+
*/
|
|
87
|
+
object G9RichTextBehaviors {
|
|
88
|
+
fun create(): List<Behavior> =
|
|
89
|
+
listOf(
|
|
90
|
+
object : Behavior("g9-richtext", false, false, false) {
|
|
91
|
+
override fun createUI(context: LynxContext): LynxUI<*> = G9RichTextUI(context)
|
|
92
|
+
},
|
|
93
|
+
)
|
|
94
|
+
}
|
package/catalog/catalog.json
CHANGED
package/catalog/index.md
CHANGED