@nativue/core 0.15.4 → 0.17.0
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/AGENTS.md +18 -4
- package/android/nativue.settings.gradle.kts +29 -9
- package/android/src/main/java/dev/nativue/BackgroundGradients.kt +111 -0
- package/android/src/main/java/dev/nativue/BoxDrawable.kt +49 -9
- package/android/src/main/java/dev/nativue/Bridge.kt +6 -16
- package/android/src/main/java/dev/nativue/NativeNode.kt +4 -0
- package/android/src/main/java/dev/nativue/Navigation.kt +31 -37
- package/android/src/main/java/dev/nativue/NavigationIcons.kt +24 -0
- package/android/src/main/java/dev/nativue/Plugins.kt +13 -1
- package/android/src/main/java/dev/nativue/Style.kt +81 -3
- package/android/src/main/java/dev/nativue/Views.kt +70 -33
- package/android/src/main/res/drawable/nativue_arrow_back.xml +14 -0
- package/android/src/main/res/drawable/nativue_close.xml +14 -0
- package/cli/ios-project.mjs +1 -1
- package/cli/link.mjs +25 -6
- package/cli/nativue.mjs +0 -1
- package/ios/BackgroundGradients.swift +191 -0
- package/ios/Bridge.swift +14 -20
- package/ios/ImageView.swift +32 -10
- package/ios/NativeNode.swift +19 -2
- package/ios/NavigationIcons.swift +49 -0
- package/ios/Plugins.swift +13 -1
- package/ios/PressableView.swift +3 -1
- package/ios/ScreenController.swift +6 -3
- package/ios/Style.swift +76 -14
- package/ios/VueScrollView.swift +1 -0
- package/package.json +2 -2
- package/src/colors.ts +10 -6
- package/src/gradients.ts +304 -0
- package/src/native-components.ts +0 -14
- package/src/renderer.ts +203 -12
- package/src/router.ts +20 -7
- package/src/types.ts +18 -0
- package/android/src/main/java/dev/nativue/SvgView.kt +0 -443
- package/ios/SvgView.swift +0 -464
package/src/gradients.ts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// backgroundImage, as CSS writes it: linear-gradient() and radial-gradient(), one or several (the first
|
|
2
|
+
// on top), parsed into what native draws over backgroundColor. The geometry that depends on the view's
|
|
3
|
+
// size (an angle's line, a corner, a radius) is worked out natively, at layout.
|
|
4
|
+
import { parseColor } from './colors'
|
|
5
|
+
|
|
6
|
+
/** A length: a fraction of the view's side plus points, [fraction, points]. */
|
|
7
|
+
type Length = [fraction: number, points: number]
|
|
8
|
+
|
|
9
|
+
export type BackgroundGradient = {
|
|
10
|
+
type: 'linear' | 'radial'
|
|
11
|
+
/** linear: degrees, 0 pointing up and 90 right; or the side or corner it goes to, e.g. ['top', 'right']. */
|
|
12
|
+
angle?: number
|
|
13
|
+
to?: string[]
|
|
14
|
+
/** radial: a circle or an ellipse, sized by an extent keyword or by radii, around `center`. */
|
|
15
|
+
shape?: 'circle' | 'ellipse'
|
|
16
|
+
extent?: 'closest-side' | 'closest-corner' | 'farthest-side' | 'farthest-corner'
|
|
17
|
+
radii?: [Length, Length]
|
|
18
|
+
center?: [Length, Length]
|
|
19
|
+
colors: number[]
|
|
20
|
+
/** Fractions of the gradient line, one per color. */
|
|
21
|
+
positions: number[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const EXTENTS = new Set(['closest-side', 'closest-corner', 'farthest-side', 'farthest-corner'])
|
|
25
|
+
const SIDE_FRACTIONS: Record<string, number> = { left: 0, top: 0, center: 0.5, right: 1, bottom: 1 }
|
|
26
|
+
const DEGREES_PER_UNIT: Record<string, number> = {
|
|
27
|
+
deg: 1,
|
|
28
|
+
grad: 0.9,
|
|
29
|
+
rad: 180 / Math.PI,
|
|
30
|
+
turn: 360,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 'linear-gradient(135deg, #4af2ae, #10b39b)' as gradients native draws; [] for what it can't. */
|
|
34
|
+
export function processBackgroundImage(value: unknown): BackgroundGradient[] {
|
|
35
|
+
if (typeof value !== 'string') return []
|
|
36
|
+
|
|
37
|
+
return splitTopLevel(value, ',').flatMap((layer) => {
|
|
38
|
+
const gradient = parseGradient(layer.trim())
|
|
39
|
+
|
|
40
|
+
return gradient ? [gradient] : []
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseGradient(text: string): BackgroundGradient | null {
|
|
45
|
+
const match = /^(linear|radial)-gradient\(([\s\S]*)\)$/i.exec(text)
|
|
46
|
+
if (!match) return null
|
|
47
|
+
|
|
48
|
+
const type = match[1].toLowerCase() as BackgroundGradient['type']
|
|
49
|
+
const [first, ...rest] = splitTopLevel(match[2], ',').map((part) => part.trim())
|
|
50
|
+
const firstTokens = splitTopLevel(first ?? '', ' ')
|
|
51
|
+
const hasSettings = firstTokens.length > 0 && parseColor(firstTokens[0]) == null
|
|
52
|
+
const stops = colorStops(hasSettings ? rest : [first, ...rest])
|
|
53
|
+
if (stops.colors.length === 0) return null
|
|
54
|
+
|
|
55
|
+
const settings = hasSettings
|
|
56
|
+
? withoutColorSpace(firstTokens.map((token) => token.toLowerCase()))
|
|
57
|
+
: []
|
|
58
|
+
|
|
59
|
+
return type === 'linear'
|
|
60
|
+
? { type, ...linearSettings(settings), ...stops }
|
|
61
|
+
: { type, ...radialSettings(settings), ...stops }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const HUE_METHODS = new Set(['shorter', 'longer', 'increasing', 'decreasing'])
|
|
65
|
+
|
|
66
|
+
/** "in oklab", "in hsl longer hue", before or after the direction: left out, colors mix in sRGB. */
|
|
67
|
+
function withoutColorSpace(tokens: string[]): string[] {
|
|
68
|
+
const start = tokens.indexOf('in')
|
|
69
|
+
if (start < 0) return tokens
|
|
70
|
+
|
|
71
|
+
const hasHueMethod = HUE_METHODS.has(tokens[start + 2]) && tokens[start + 3] === 'hue'
|
|
72
|
+
|
|
73
|
+
return [...tokens.slice(0, start), ...tokens.slice(start + (hasHueMethod ? 4 : 2))]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** "135deg", "to top right". */
|
|
77
|
+
function linearSettings(tokens: string[]): Pick<BackgroundGradient, 'angle' | 'to'> {
|
|
78
|
+
if (tokens[0] === 'to') return { to: tokens.slice(1) }
|
|
79
|
+
|
|
80
|
+
const angle = /^(-?[\d.]+)(deg|grad|rad|turn)?$/.exec(tokens[0] ?? '')
|
|
81
|
+
|
|
82
|
+
return { angle: angle ? Number(angle[1]) * DEGREES_PER_UNIT[angle[2] ?? 'deg'] : 180 }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** "circle at 30% 40%", "ellipse farthest-corner", "40px 20px at top left". */
|
|
86
|
+
function radialSettings(tokens: string[]): Partial<BackgroundGradient> {
|
|
87
|
+
const atIndex = tokens.indexOf('at')
|
|
88
|
+
const before = atIndex < 0 ? tokens : tokens.slice(0, atIndex)
|
|
89
|
+
const sizes = before.filter((token) => /^-?[\d.]/.test(token)).map(length)
|
|
90
|
+
const shape = before.includes('circle') || (sizes.length === 1 && !before.includes('ellipse'))
|
|
91
|
+
const extent = before.find((token) => EXTENTS.has(token)) as BackgroundGradient['extent']
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
shape: shape ? 'circle' : 'ellipse',
|
|
95
|
+
...(sizes.length > 0
|
|
96
|
+
? { radii: [sizes[0], sizes[1] ?? sizes[0]] }
|
|
97
|
+
: { extent: extent ?? 'farthest-corner' }),
|
|
98
|
+
center: position(atIndex < 0 ? [] : tokens.slice(atIndex + 1)),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const HORIZONTAL = new Set(['left', 'right'])
|
|
103
|
+
const VERTICAL = new Set(['top', 'bottom'])
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* "at top", "at center left", "at 30% 40px", "at right 10px top 20%": x and y, a keyword naming its
|
|
107
|
+
* axis whatever the order, the one left out centered.
|
|
108
|
+
*/
|
|
109
|
+
function position(tokens: string[]): [Length, Length] {
|
|
110
|
+
const isOffset = (token: string | undefined) => token !== undefined && /^-?[\d.]/.test(token)
|
|
111
|
+
const hasEdgeOffsets = tokens.some(
|
|
112
|
+
(token, index) => !isOffset(token) && token !== 'center' && isOffset(tokens[index + 1]),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
// Edges and offsets from them: "right 10px top 20%".
|
|
116
|
+
if (hasEdgeOffsets) {
|
|
117
|
+
let x: Length = [0.5, 0]
|
|
118
|
+
let y: Length = [0.5, 0]
|
|
119
|
+
|
|
120
|
+
tokens.forEach((token, index) => {
|
|
121
|
+
if (isOffset(token)) return
|
|
122
|
+
|
|
123
|
+
const [fraction, points] = isOffset(tokens[index + 1]) ? length(tokens[index + 1]) : [0, 0]
|
|
124
|
+
const fromEnd = token === 'right' || token === 'bottom'
|
|
125
|
+
const edge: Length = fromEnd ? [1 - fraction, -points] : [fraction, points]
|
|
126
|
+
|
|
127
|
+
if (HORIZONTAL.has(token)) x = edge
|
|
128
|
+
else if (VERTICAL.has(token)) y = edge
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
return [x, y]
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const [first, second = 'center'] = tokens
|
|
135
|
+
if (first === undefined)
|
|
136
|
+
return [
|
|
137
|
+
[0.5, 0],
|
|
138
|
+
[0.5, 0],
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
const isSwapped = VERTICAL.has(first) || HORIZONTAL.has(second)
|
|
142
|
+
|
|
143
|
+
return isSwapped ? [length(second), length(first)] : [length(first), length(second)]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function length(token: string): Length {
|
|
147
|
+
if (token in SIDE_FRACTIONS) return [SIDE_FRACTIONS[token], 0]
|
|
148
|
+
if (token.endsWith('%')) return [Number.parseFloat(token) / 100, 0]
|
|
149
|
+
|
|
150
|
+
return [0, Number.parseFloat(token) || 0]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* "red", "red 20%", "red 20% 40%" (two stops): colors and their positions, missing ones spread evenly
|
|
155
|
+
* between their neighbours, as CSS places them. Positions are percentages; a length counts as missing.
|
|
156
|
+
*/
|
|
157
|
+
function colorStops(parts: string[]): Pick<BackgroundGradient, 'colors' | 'positions'> {
|
|
158
|
+
const colors: number[] = []
|
|
159
|
+
const known: (number | undefined)[] = []
|
|
160
|
+
|
|
161
|
+
for (const part of parts) {
|
|
162
|
+
const [colorText, ...positions] = splitTopLevel(part, ' ')
|
|
163
|
+
// A color hint ("red, 30%, blue") is left out: colors mix evenly.
|
|
164
|
+
const color = parseColor(colorText)
|
|
165
|
+
if (color == null) continue
|
|
166
|
+
|
|
167
|
+
const fractions = positions.map((text) =>
|
|
168
|
+
text.endsWith('%') ? Number.parseFloat(text) / 100 : undefined,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
for (const fraction of fractions.length > 0 ? fractions : [undefined]) {
|
|
172
|
+
colors.push(color)
|
|
173
|
+
known.push(fraction)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (colors.length === 1) {
|
|
177
|
+
colors.push(colors[0])
|
|
178
|
+
known.push(undefined)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return withoutTransparentBlack(...withinLine(colors, spreadPositions(known)))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Stops before 0% or past 100%, which CSS allows, cut at the ends of the line, the colors there mixed
|
|
186
|
+
* from their neighbours: native gradients take positions from 0 to 1.
|
|
187
|
+
*/
|
|
188
|
+
function withinLine(colors: number[], positions: number[]): [number[], number[]] {
|
|
189
|
+
if (positions[0] >= 0 && positions[positions.length - 1] <= 1) return [colors, positions]
|
|
190
|
+
|
|
191
|
+
const colorAt = (position: number) => {
|
|
192
|
+
const after = positions.findIndex((stop) => stop >= position)
|
|
193
|
+
if (after <= 0) return colors[after < 0 ? colors.length - 1 : 0]
|
|
194
|
+
|
|
195
|
+
const span = positions[after] - positions[after - 1]
|
|
196
|
+
|
|
197
|
+
return mixColors(
|
|
198
|
+
colors[after - 1],
|
|
199
|
+
colors[after],
|
|
200
|
+
span === 0 ? 1 : (position - positions[after - 1]) / span,
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
const inside = positions.flatMap((position, index) =>
|
|
204
|
+
position > 0 && position < 1 ? [index] : [],
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
return [
|
|
208
|
+
[colorAt(0), ...inside.map((index) => colors[index]), colorAt(1)],
|
|
209
|
+
[0, ...inside.map((index) => positions[index]), 1],
|
|
210
|
+
]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Two 0xRRGGBBAA colors mixed, `amount` of the way from the first. */
|
|
214
|
+
function mixColors(first: number, second: number, amount: number): number {
|
|
215
|
+
const channel = (color: number, shift: number) => (color >>> shift) & 0xff
|
|
216
|
+
const mixed = [24, 16, 8, 0].map((shift) =>
|
|
217
|
+
Math.round(channel(first, shift) + (channel(second, shift) - channel(first, shift)) * amount),
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
return ((mixed[0] << 24) | (mixed[1] << 16) | (mixed[2] << 8) | mixed[3]) >>> 0
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* CSS mixes a gradient's colors premultiplied: orange to transparent fades the orange out. Native mixes
|
|
225
|
+
* them as they are, through transparent's black, so a fully transparent stop takes its neighbours' color
|
|
226
|
+
* (twice, at the same place, when they differ).
|
|
227
|
+
*/
|
|
228
|
+
function withoutTransparentBlack(
|
|
229
|
+
colors: number[],
|
|
230
|
+
positions: number[],
|
|
231
|
+
): Pick<BackgroundGradient, 'colors' | 'positions'> {
|
|
232
|
+
const result: Pick<BackgroundGradient, 'colors' | 'positions'> = { colors: [], positions: [] }
|
|
233
|
+
const isClear = (color: number) => (color & 0xff) === 0
|
|
234
|
+
const add = (color: number, position: number) => {
|
|
235
|
+
result.colors.push(color >>> 0)
|
|
236
|
+
result.positions.push(position)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
colors.forEach((color, index) => {
|
|
240
|
+
if (!isClear(color)) return add(color, positions[index])
|
|
241
|
+
|
|
242
|
+
const neighbours = [colors[index - 1], colors[index + 1]].filter(
|
|
243
|
+
(neighbour) => neighbour !== undefined && !isClear(neighbour),
|
|
244
|
+
)
|
|
245
|
+
if (neighbours.length === 0) return add(color, positions[index])
|
|
246
|
+
|
|
247
|
+
for (const neighbour of new Set(neighbours)) add(neighbour & 0xffffff00, positions[index])
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
return result
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function spreadPositions(known: (number | undefined)[]): number[] {
|
|
254
|
+
const positions = [...known]
|
|
255
|
+
|
|
256
|
+
positions[0] ??= 0
|
|
257
|
+
positions[positions.length - 1] ??= 1
|
|
258
|
+
|
|
259
|
+
// Never before an earlier stop.
|
|
260
|
+
let latest = positions[0]!
|
|
261
|
+
|
|
262
|
+
for (let index = 1; index < positions.length; index++) {
|
|
263
|
+
if (positions[index] === undefined) continue
|
|
264
|
+
|
|
265
|
+
positions[index] = Math.max(positions[index]!, latest)
|
|
266
|
+
latest = positions[index]!
|
|
267
|
+
}
|
|
268
|
+
for (let index = 1; index < positions.length; index++) {
|
|
269
|
+
if (positions[index] !== undefined) continue
|
|
270
|
+
|
|
271
|
+
const next = positions.findIndex((position, after) => after > index && position !== undefined)
|
|
272
|
+
const start = positions[index - 1]!
|
|
273
|
+
const step = (positions[next]! - start) / (next - index + 1)
|
|
274
|
+
|
|
275
|
+
for (let missing = index; missing < next; missing++)
|
|
276
|
+
positions[missing] = start + step * (missing - index + 1)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return positions as number[]
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Splits on `separator` outside parentheses: "rgb(0, 0, 0) 10%, red" by ",". */
|
|
283
|
+
function splitTopLevel(text: string, separator: ',' | ' '): string[] {
|
|
284
|
+
const parts: string[] = []
|
|
285
|
+
let depth = 0
|
|
286
|
+
let current = ''
|
|
287
|
+
|
|
288
|
+
for (const character of text) {
|
|
289
|
+
if (character === '(') depth++
|
|
290
|
+
if (character === ')') depth--
|
|
291
|
+
|
|
292
|
+
const separates = separator === ' ' ? /\s/.test(character) : character === separator
|
|
293
|
+
|
|
294
|
+
if (separates && depth === 0) {
|
|
295
|
+
if (current.trim()) parts.push(current.trim())
|
|
296
|
+
current = ''
|
|
297
|
+
} else {
|
|
298
|
+
current += character
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (current.trim()) parts.push(current.trim())
|
|
302
|
+
|
|
303
|
+
return parts
|
|
304
|
+
}
|
package/src/native-components.ts
CHANGED
|
@@ -24,13 +24,6 @@ export interface AccessibilityProps {
|
|
|
24
24
|
'aria-checked'?: BooleanAttribute
|
|
25
25
|
}
|
|
26
26
|
type EventHandler<Payload> = ((payload: Payload) => void) | AnimatedEvent<Payload>
|
|
27
|
-
// [tag, attributes], e.g. ['circle', { cx: 12, cy: 12, r: 10 }]: path, circle, ellipse, rect, line,
|
|
28
|
-
// polyline and polygon. Stroked with `color`; filled too when `fill` isn't 'none'.
|
|
29
|
-
export type SvgElement = [tag: string, attributes: Record<string, string | number>]
|
|
30
|
-
// An icon component that carries its drawing, so it can also be a tab bar icon (see @nativue/icons).
|
|
31
|
-
export interface SvgIcon {
|
|
32
|
-
svg: { viewBox: [number, number, number, number]; elements: SvgElement[] }
|
|
33
|
-
}
|
|
34
27
|
type ModelProps<Value> = { modelValue?: Value; 'onUpdate:modelValue'?: (value: Value) => void }
|
|
35
28
|
|
|
36
29
|
declare module '@vue/runtime-core' {
|
|
@@ -91,13 +84,6 @@ declare module '@vue/runtime-core' {
|
|
|
91
84
|
color?: string
|
|
92
85
|
size?: 'small' | 'large'
|
|
93
86
|
}>
|
|
94
|
-
// Low level: draws SVG elements, e.g. an icon set's nodes (see @nativue/icons).
|
|
95
|
-
Svg: NativeComponent<{
|
|
96
|
-
viewBox?: [number, number, number, number]
|
|
97
|
-
elements: SvgElement[]
|
|
98
|
-
color?: string
|
|
99
|
-
strokeWidth?: number
|
|
100
|
-
}>
|
|
101
87
|
Modal: NativeComponent<{
|
|
102
88
|
visible?: boolean
|
|
103
89
|
animationType?: 'none' | 'slide' | 'fade'
|
package/src/renderer.ts
CHANGED
|
@@ -22,9 +22,12 @@ import { animatedEventMapping, animatedStyleBinding, isAnimatedEvent } from './a
|
|
|
22
22
|
import { Appearance } from './appearance'
|
|
23
23
|
import { COLOR_PROPERTIES, processColor } from './colors'
|
|
24
24
|
import { FlatListComponent, ImageBackground, Pressable, Swipeable } from './components'
|
|
25
|
+
import { processBackgroundImage } from './gradients'
|
|
25
26
|
import type { LayoutAnimationConfig, Style } from './types'
|
|
26
27
|
export * from '@vue/runtime-core'
|
|
27
28
|
export { AccessibilityInfo } from './accessibility'
|
|
29
|
+
export type { AccessibilityProps } from './native-components'
|
|
30
|
+
export { processColor } from './colors'
|
|
28
31
|
export { Animated } from './animated'
|
|
29
32
|
export {
|
|
30
33
|
Appearance,
|
|
@@ -43,7 +46,6 @@ export { openSettings, usePermission } from './permissions'
|
|
|
43
46
|
export type { PermissionResponse, PermissionResponseWith } from './permissions'
|
|
44
47
|
export { AsyncStorage } from './storage'
|
|
45
48
|
export type * from './types'
|
|
46
|
-
export type { SvgElement, SvgIcon } from './native-components'
|
|
47
49
|
|
|
48
50
|
interface Node {
|
|
49
51
|
id: number
|
|
@@ -64,6 +66,44 @@ interface Node {
|
|
|
64
66
|
/** <TextInput v-model.trim>: its modifiers, and the text typed last. */
|
|
65
67
|
modelModifiers?: ModelModifiers
|
|
66
68
|
typedText?: string
|
|
69
|
+
/** A drawn view's style from its own props (<svg width height>): under its class and style. */
|
|
70
|
+
baseStyle?: Style
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** An element inside a drawn view, or the view itself, as its package sees it. */
|
|
74
|
+
export interface DrawnElement {
|
|
75
|
+
readonly id: number
|
|
76
|
+
readonly type: string
|
|
77
|
+
readonly parent: DrawnElement | null
|
|
78
|
+
readonly children: DrawnElement[]
|
|
79
|
+
readonly text: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A native view that draws what's inside it, like @nativue/svg's <svg>: its elements stay in JS and
|
|
84
|
+
* never become views. After each batch of changes, the view gets them as one prop, `drawing`.
|
|
85
|
+
*/
|
|
86
|
+
export interface DrawnView {
|
|
87
|
+
/** The native view's type, e.g. 'Svg', which a package's plugin registers natively. */
|
|
88
|
+
type: string
|
|
89
|
+
/** The tags that create it in a template, e.g. ['svg']. */
|
|
90
|
+
tags: string[]
|
|
91
|
+
/** The tags of the elements drawn inside it, e.g. 'path'. */
|
|
92
|
+
partTags: string[]
|
|
93
|
+
/** A part's prop (an @event's handler excepted, which nativue keeps). */
|
|
94
|
+
setPartProp(element: DrawnElement, key: string, value: unknown): void
|
|
95
|
+
/**
|
|
96
|
+
* The view's own prop: true when only the drawing reads it, false when it also goes to the native view.
|
|
97
|
+
* `setBaseStyle` styles the view under its class and style.
|
|
98
|
+
*/
|
|
99
|
+
setViewProp(
|
|
100
|
+
element: DrawnElement,
|
|
101
|
+
key: string,
|
|
102
|
+
value: unknown,
|
|
103
|
+
setBaseStyle: (style: Style) => void,
|
|
104
|
+
): boolean
|
|
105
|
+
/** The view's `drawing`, once per batch of changes; undefined leaves it as it is. */
|
|
106
|
+
drawing(view: DrawnElement, isPressable: (element: DrawnElement) => boolean): unknown
|
|
67
107
|
}
|
|
68
108
|
|
|
69
109
|
type ModelModifiers = { trim?: boolean; number?: boolean; lazy?: boolean }
|
|
@@ -90,6 +130,19 @@ export function setClassStyleResolver(resolver: ClassStyleResolver): void {
|
|
|
90
130
|
nodesWithClass.forEach((node) => updateStyle(node, {}))
|
|
91
131
|
}
|
|
92
132
|
|
|
133
|
+
const warnedTags = new Set<string>()
|
|
134
|
+
|
|
135
|
+
// Native components are capitalized: a lowercase tag is HTML (<div>), or a drawn view's (<svg>) whose
|
|
136
|
+
// package isn't installed. Either way it would show nothing.
|
|
137
|
+
function warnIfUnknown(tag: string) {
|
|
138
|
+
if (!/^[a-z]/.test(tag) || drawnViewsByTag.has(tag) || warnedTags.has(tag)) return
|
|
139
|
+
|
|
140
|
+
warnedTags.add(tag)
|
|
141
|
+
console.warn(
|
|
142
|
+
`<${tag}> isn't a nativue element, so it shows nothing: use nativue's components (<View>, <Text>...), or install the package that provides <${tag}>.`,
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
93
146
|
function createNode(type: string): Node {
|
|
94
147
|
return { id: nextNodeId++, type, parent: null, children: [], text: '' }
|
|
95
148
|
}
|
|
@@ -101,9 +154,76 @@ function nativeIdsIn(node: Node): number[] {
|
|
|
101
154
|
return [...ownId, ...node.children.flatMap(nativeIdsIn)]
|
|
102
155
|
}
|
|
103
156
|
|
|
104
|
-
// '#text' and '#comment' nodes only exist in JS (Vue anchors / raw text)
|
|
157
|
+
// '#text' and '#comment' nodes only exist in JS (Vue anchors / raw text), and so do a drawn view's
|
|
158
|
+
// elements, which the view draws: native never sees them.
|
|
105
159
|
function isNativeNode(node: Node): boolean {
|
|
106
|
-
return !node.type.startsWith('#')
|
|
160
|
+
return !node.type.startsWith('#') && !drawnViewsByPartTag.has(node.type)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const drawnViewsByTag = new Map<string, DrawnView>()
|
|
164
|
+
const drawnViewsByPartTag = new Map<string, DrawnView>()
|
|
165
|
+
const drawnViewsByType = new Map<string, DrawnView>()
|
|
166
|
+
const drawnViewsToRedraw = new Set<Node>()
|
|
167
|
+
|
|
168
|
+
/** For a package's runtime module, before the app starts: see DrawnView. */
|
|
169
|
+
export function registerDrawnView(view: DrawnView): void {
|
|
170
|
+
for (const tag of [...view.tags, view.type]) drawnViewsByTag.set(tag, view)
|
|
171
|
+
for (const tag of view.partTags) drawnViewsByPartTag.set(tag, view)
|
|
172
|
+
drawnViewsByType.set(view.type, view)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function drawnViewOf(node: Node | null): Node | null {
|
|
176
|
+
let current = node
|
|
177
|
+
|
|
178
|
+
while (current && !drawnViewsByType.has(current.type)) current = current.parent
|
|
179
|
+
|
|
180
|
+
return current
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Every change inside a drawn view in one batch: the drawing is sent once, after them. */
|
|
184
|
+
function redraw(node: Node | null) {
|
|
185
|
+
const view = drawnViewOf(node)
|
|
186
|
+
if (!view) return
|
|
187
|
+
|
|
188
|
+
if (drawnViewsToRedraw.size === 0) Promise.resolve().then(sendDrawings)
|
|
189
|
+
drawnViewsToRedraw.add(view)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function sendDrawings() {
|
|
193
|
+
const isPressable = (element: DrawnElement) => Boolean(eventHandlersByNodeId[element.id]?.press)
|
|
194
|
+
// Copied first: a drawing that fails doesn't keep the others from being sent next time.
|
|
195
|
+
const views = [...drawnViewsToRedraw]
|
|
196
|
+
|
|
197
|
+
drawnViewsToRedraw.clear()
|
|
198
|
+
for (const view of views) {
|
|
199
|
+
const drawing = drawnViewsByType.get(view.type)?.drawing(view, isPressable)
|
|
200
|
+
|
|
201
|
+
if (drawing !== undefined) native.setProp(view.id, 'drawing', drawing)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A drawn view's own props: false for those that go on as for any view. */
|
|
206
|
+
function patchDrawnViewProp(element: Node, key: string, value: unknown): boolean {
|
|
207
|
+
const name = camelize(key)
|
|
208
|
+
|
|
209
|
+
if (isEventProp(name)) redraw(element)
|
|
210
|
+
if (key === 'style' || key === 'class' || isEventProp(name) || isAccessibilityAttribute(key)) {
|
|
211
|
+
return false
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const setBaseStyle = (style: Style) => {
|
|
215
|
+
const previousStyle = combinedStyle(element)
|
|
216
|
+
|
|
217
|
+
element.baseStyle = style
|
|
218
|
+
patchStyle(element, previousStyle, combinedStyle(element))
|
|
219
|
+
}
|
|
220
|
+
const isDrawingOnly = drawnViewsByType
|
|
221
|
+
.get(element.type)!
|
|
222
|
+
.setViewProp(element, key, value, setBaseStyle)
|
|
223
|
+
|
|
224
|
+
redraw(element)
|
|
225
|
+
|
|
226
|
+
return isDrawingOnly
|
|
107
227
|
}
|
|
108
228
|
|
|
109
229
|
// Vue removes only the root of an unmounted subtree: its descendants go with it.
|
|
@@ -140,7 +260,25 @@ function syncTextContent(parent: Node | null) {
|
|
|
140
260
|
.map((child) => child.text)
|
|
141
261
|
.join('')
|
|
142
262
|
.trim()
|
|
143
|
-
native.setText(parent.id, text)
|
|
263
|
+
native.setText(parent.id, transformedText(text, combinedStyle(parent)?.textTransform))
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** textTransform, applied to what's sent: the same on both platforms. */
|
|
267
|
+
function transformedText(text: string, textTransform: Style['textTransform']): string {
|
|
268
|
+
switch (textTransform) {
|
|
269
|
+
case 'uppercase':
|
|
270
|
+
return text.toUpperCase()
|
|
271
|
+
case 'lowercase':
|
|
272
|
+
return text.toLowerCase()
|
|
273
|
+
case 'capitalize':
|
|
274
|
+
// A word starts after anything but a letter, a digit or an apostrophe: "(hello)" is "(Hello)".
|
|
275
|
+
return text.replace(
|
|
276
|
+
/(^|[^\p{L}\p{N}'’])(\p{L})/gu,
|
|
277
|
+
(_, before: string, letter: string) => before + letter.toUpperCase(),
|
|
278
|
+
)
|
|
279
|
+
default:
|
|
280
|
+
return text
|
|
281
|
+
}
|
|
144
282
|
}
|
|
145
283
|
|
|
146
284
|
function detachFromParent(node: Node): Node | null {
|
|
@@ -158,6 +296,8 @@ function nativeIndexBefore(parent: Node, childIndex: number): number {
|
|
|
158
296
|
}
|
|
159
297
|
|
|
160
298
|
function nativeStyleValue(property: string, value: unknown): unknown {
|
|
299
|
+
if (property === 'backgroundImage') return processBackgroundImage(value)
|
|
300
|
+
|
|
161
301
|
return COLOR_PROPERTIES.has(property) ? processColor(value) : value
|
|
162
302
|
}
|
|
163
303
|
|
|
@@ -198,9 +338,9 @@ function withoutAnimatedProperties(
|
|
|
198
338
|
// The class's styles come first, so the style prop overrides them, as inline styles do on the web.
|
|
199
339
|
function combinedStyle(element: Node): Style | null {
|
|
200
340
|
const focusedStyle = element.focused ? element.focusedStyle : null
|
|
201
|
-
if (!element.classStyle && !focusedStyle) return element.style ?? null
|
|
341
|
+
if (!element.classStyle && !focusedStyle && !element.baseStyle) return element.style ?? null
|
|
202
342
|
|
|
203
|
-
return { ...element.classStyle, ...element.style, ...focusedStyle }
|
|
343
|
+
return { ...element.baseStyle, ...element.classStyle, ...element.style, ...focusedStyle }
|
|
204
344
|
}
|
|
205
345
|
|
|
206
346
|
function classStyleOf(element: Node): Style | undefined {
|
|
@@ -226,7 +366,10 @@ function updateStyle(
|
|
|
226
366
|
if (element.className) nodesWithClass.add(element)
|
|
227
367
|
else nodesWithClass.delete(element)
|
|
228
368
|
|
|
229
|
-
|
|
369
|
+
const nextStyle = combinedStyle(element)
|
|
370
|
+
|
|
371
|
+
patchStyle(element, previousStyle, nextStyle)
|
|
372
|
+
if (previousStyle?.textTransform !== nextStyle?.textTransform) syncTextContent(element)
|
|
230
373
|
}
|
|
231
374
|
|
|
232
375
|
Appearance.addChangeListener(() => nodesWithClass.forEach((node) => updateStyle(node, {})))
|
|
@@ -264,6 +407,23 @@ function reportsToJavaScript(eventName: string, handler: unknown): boolean {
|
|
|
264
407
|
return eventName !== 'scroll' || handler.listener != null
|
|
265
408
|
}
|
|
266
409
|
|
|
410
|
+
/** Inside a drawn view there's no native view to tell: the drawing says which elements take presses. */
|
|
411
|
+
function patchDrawnEventHandler(
|
|
412
|
+
element: Node,
|
|
413
|
+
key: string,
|
|
414
|
+
handler: unknown,
|
|
415
|
+
component: ComponentInternalInstance | null,
|
|
416
|
+
) {
|
|
417
|
+
const listener = handler as Function | undefined
|
|
418
|
+
|
|
419
|
+
eventHandlersByNodeId[element.id] ??= {}
|
|
420
|
+
eventHandlersByNodeId[element.id][key.slice(2).toLowerCase()] =
|
|
421
|
+
listener &&
|
|
422
|
+
((payload) =>
|
|
423
|
+
callWithAsyncErrorHandling(listener, component, ErrorCodes.NATIVE_EVENT_HANDLER, [payload]))
|
|
424
|
+
redraw(element)
|
|
425
|
+
}
|
|
426
|
+
|
|
267
427
|
// An Animated.event handler writes into values natively; JS keeps only its listener. Handlers run
|
|
268
428
|
// through Vue's error handling, like on the web, so an error names the component it came from.
|
|
269
429
|
function patchEventHandler(
|
|
@@ -291,8 +451,12 @@ function patchEventHandler(
|
|
|
291
451
|
|
|
292
452
|
const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
293
453
|
createElement(type) {
|
|
294
|
-
|
|
295
|
-
|
|
454
|
+
if (drawnViewsByPartTag.has(type)) return createNode(type)
|
|
455
|
+
|
|
456
|
+
const element = createNode(drawnViewsByTag.get(type)?.type ?? type)
|
|
457
|
+
|
|
458
|
+
warnIfUnknown(type)
|
|
459
|
+
native.create(element.id, element.type)
|
|
296
460
|
if (type === 'TextInput') textInputsById.set(element.id, element)
|
|
297
461
|
|
|
298
462
|
return element
|
|
@@ -312,11 +476,18 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
312
476
|
setText(textNode, text) {
|
|
313
477
|
textNode.text = text
|
|
314
478
|
syncTextContent(textNode.parent)
|
|
479
|
+
redraw(textNode.parent)
|
|
315
480
|
},
|
|
316
481
|
|
|
317
482
|
setElementText(element, text) {
|
|
318
|
-
element.
|
|
319
|
-
|
|
483
|
+
const isText = element.type === 'Text'
|
|
484
|
+
const isDrawnPart = drawnViewsByPartTag.has(element.type)
|
|
485
|
+
|
|
486
|
+
// Kept as a child: a later textTransform, or a drawing, reads it again.
|
|
487
|
+
element.children =
|
|
488
|
+
isText || isDrawnPart ? [{ ...createNode('#text'), text, parent: element }] : []
|
|
489
|
+
if (isText) syncTextContent(element)
|
|
490
|
+
if (isDrawnPart) redraw(element)
|
|
320
491
|
},
|
|
321
492
|
|
|
322
493
|
insert(child, parent, anchor) {
|
|
@@ -328,6 +499,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
328
499
|
|
|
329
500
|
if (isNativeNode(child)) native.insert(parent.id, child.id, nativeIndexBefore(parent, index))
|
|
330
501
|
else syncTextContent(parent)
|
|
502
|
+
redraw(parent)
|
|
331
503
|
},
|
|
332
504
|
|
|
333
505
|
remove(child) {
|
|
@@ -336,6 +508,7 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
336
508
|
|
|
337
509
|
if (isNativeNode(child)) native.remove(child.id, nativeIdsIn(child))
|
|
338
510
|
else syncTextContent(parent)
|
|
511
|
+
redraw(parent)
|
|
339
512
|
},
|
|
340
513
|
|
|
341
514
|
parentNode(node) {
|
|
@@ -349,7 +522,18 @@ const { createApp: createBaseApp } = createRenderer<Node, Node>({
|
|
|
349
522
|
},
|
|
350
523
|
|
|
351
524
|
patchProp(element, key, previousValue, nextValue, _namespace, component) {
|
|
352
|
-
|
|
525
|
+
const drawnView = drawnViewsByPartTag.get(element.type)
|
|
526
|
+
|
|
527
|
+
if (drawnView) {
|
|
528
|
+
if (isEventProp(camelize(key))) {
|
|
529
|
+
patchDrawnEventHandler(element, camelize(key), nextValue, component ?? null)
|
|
530
|
+
} else {
|
|
531
|
+
drawnView.setPartProp(element, key, nextValue)
|
|
532
|
+
redraw(element)
|
|
533
|
+
}
|
|
534
|
+
} else if (drawnViewsByType.has(element.type) && patchDrawnViewProp(element, key, nextValue)) {
|
|
535
|
+
return
|
|
536
|
+
} else if (key === 'style') updateStyle(element, { style: nextValue })
|
|
353
537
|
else if (key === 'class') updateStyle(element, { className: nextValue ?? undefined })
|
|
354
538
|
else if (key === 'pressed') updateStyle(element, { pressed: nextValue })
|
|
355
539
|
else if (isAccessibilityAttribute(key)) updateAccessibility(element, key, nextValue)
|
|
@@ -373,6 +557,13 @@ const MODEL_EVENT_BY_NATIVE_EVENT: Record<string, string> = {
|
|
|
373
557
|
}
|
|
374
558
|
|
|
375
559
|
globalThis.__dispatch = (nodeId, eventName, payload) => {
|
|
560
|
+
// A press on a drawn view's elements: the innermost first, then the groups around it.
|
|
561
|
+
if (eventName === 'elementpress') {
|
|
562
|
+
const { nodeIds, ...location } = payload as { nodeIds: number[] }
|
|
563
|
+
|
|
564
|
+
return nodeIds.forEach((id) => eventHandlersByNodeId[id]?.press?.(location))
|
|
565
|
+
}
|
|
566
|
+
|
|
376
567
|
updateFocus(nodeId, eventName)
|
|
377
568
|
|
|
378
569
|
const handlers = eventHandlersByNodeId[nodeId]
|