@asteby/metacore-runtime-react 49.11.0 → 49.13.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +40 -0
  3. package/dist/dialogs/dynamic-record.d.ts.map +1 -1
  4. package/dist/dialogs/dynamic-record.js +8 -1
  5. package/dist/dynamic-columns.d.ts.map +1 -1
  6. package/dist/dynamic-columns.js +11 -1
  7. package/dist/dynamic-icon.d.ts +16 -1
  8. package/dist/dynamic-icon.d.ts.map +1 -1
  9. package/dist/dynamic-icon.js +36 -5
  10. package/dist/dynamic-table.d.ts +17 -1
  11. package/dist/dynamic-table.d.ts.map +1 -1
  12. package/dist/dynamic-table.js +66 -9
  13. package/dist/icon-aliases.d.ts +2 -0
  14. package/dist/icon-aliases.d.ts.map +1 -0
  15. package/dist/icon-aliases.js +594 -0
  16. package/dist/icon-resolvers.d.ts +13 -0
  17. package/dist/icon-resolvers.d.ts.map +1 -0
  18. package/dist/icon-resolvers.js +36 -0
  19. package/dist/icon-spec.d.ts +17 -0
  20. package/dist/icon-spec.d.ts.map +1 -0
  21. package/dist/icon-spec.js +165 -0
  22. package/dist/index.d.ts +3 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +3 -0
  25. package/dist/types.d.ts +1 -1
  26. package/dist/types.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/__tests__/dynamic-table-bulk-delete-reasons.test.tsx +224 -0
  29. package/src/__tests__/icon-component.test.tsx +145 -0
  30. package/src/__tests__/icon-spec.test.ts +155 -0
  31. package/src/dialogs/dynamic-record.tsx +17 -1
  32. package/src/dynamic-columns.tsx +22 -1
  33. package/src/dynamic-icon.tsx +68 -3
  34. package/src/dynamic-table.tsx +84 -7
  35. package/src/icon-aliases.ts +602 -0
  36. package/src/icon-resolvers.ts +59 -0
  37. package/src/icon-spec.ts +169 -0
  38. package/src/index.ts +3 -0
  39. package/src/types.ts +3 -0
@@ -0,0 +1,169 @@
1
+ // parseIconSpec — pure, deterministic classifier for "whatever the backend
2
+ // stored in an icon column". No React, no DOM. A string becomes one of:
3
+ //
4
+ // { kind: 'lucide', name } PascalCase Lucide glyph ("Brain", "CreditCard")
5
+ // { kind: 'emoji', value } a single emoji grapheme ("🩺", "👨‍⚕️", "🇲🇽")
6
+ // { kind: 'image', src } http(s) URL, /rooted, ./relative or safe data:image
7
+ // { kind: 'unknown', raw } anything else (never rendered as raw text)
8
+ //
9
+ // Lucide covers: PascalCase, kebab, snake_case, optional `Icon` suffix, and
10
+ // FontAwesome 5/6 classes ("fas fa-brain", "fa-solid fa-brain", "far fa-heart",
11
+ // "fa fa-user-md", a bare "fa-brain") through FONT_AWESOME_ALIASES plus a
12
+ // generic try of the FA name against Lucide. Other libraries' classes
13
+ // ("mdi mdi-home", "bi bi-house", "ti ti-home", "ri-home-line") and Iconify ids
14
+ // ("mdi:home") only get the generic normalisation against Lucide; no tables.
15
+ import dynamicIconImports from 'lucide-react/dynamicIconImports'
16
+ import { FONT_AWESOME_ALIASES } from './icon-aliases'
17
+
18
+ export type IconSpec =
19
+ | { kind: 'lucide'; name: string }
20
+ | { kind: 'emoji'; value: string }
21
+ | { kind: 'image'; src: string }
22
+ | { kind: 'unknown'; raw: string }
23
+
24
+ const LUCIDE_KEYS = dynamicIconImports as Record<string, unknown>
25
+
26
+ function kebabToPascal(kebab: string): string {
27
+ return kebab
28
+ .split('-')
29
+ .map((part) => (part ? part.charAt(0).toUpperCase() + part.slice(1) : ''))
30
+ .join('')
31
+ }
32
+
33
+ /** Lucide kebab key for any name spelling, or null when Lucide lacks it. */
34
+ function lucideKeyFor(raw: string): string | null {
35
+ const tryKey = (s: string): string | null => {
36
+ if (!s) return null
37
+ const key = s
38
+ .replace(/[\s_]+/g, '-')
39
+ .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
40
+ .replace(/([A-Z])([A-Z][a-z])/g, '$1-$2')
41
+ .toLowerCase()
42
+ if (key in LUCIDE_KEYS) return key
43
+ // Lucide splits digits ("building-2"); "building2" is not a key.
44
+ const split = key.replace(/([a-z])(\d)/g, '$1-$2')
45
+ return split in LUCIDE_KEYS ? split : null
46
+ }
47
+ if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(raw)) return null
48
+ if (raw === 'Icon' || raw === 'icon') return null
49
+ return tryKey(raw) ?? (/Icon$/.test(raw) ? tryKey(raw.replace(/Icon$/, '')) : null)
50
+ }
51
+
52
+ const lucide = (key: string): IconSpec => ({ kind: 'lucide', name: kebabToPascal(key) })
53
+
54
+ // ---------- FontAwesome ----------
55
+ const FA_STYLE = /^(fa|fas|far|fab|fal|fad|fat|fass|fasr|fasl|fa-solid|fa-regular|fa-brands|fa-light|fa-thin|fa-duotone|fa-sharp|fa-classic)$/
56
+ const FA_MODIFIER =
57
+ /^fa-(fw|lg|xs|sm|1x|2x|3x|4x|5x|6x|7x|8x|9x|10x|2xs|xl|2xl|spin|spin-pulse|spin-reverse|pulse|beat|beat-fade|bounce|fade|flip|shake|ul|li|border|inverse|stack|stack-1x|stack-2x|pull-left|pull-right|rotate-\d+|rotate-by|flip-horizontal|flip-vertical|flip-both)$/
58
+
59
+ function parseFontAwesome(tokens: string[]): IconSpec | null {
60
+ const names = tokens
61
+ .filter((t) => /^fa-[a-z0-9-]+$/.test(t) && !FA_STYLE.test(t) && !FA_MODIFIER.test(t))
62
+ .map((t) => t.slice(3))
63
+ const name = names[0]
64
+ // "fas brain" without any `fa-` token is not FontAwesome syntax.
65
+ if (!name) return null
66
+ const alias = FONT_AWESOME_ALIASES[name]
67
+ const key = (alias && lucideKeyFor(alias)) ?? lucideKeyFor(name)
68
+ return key ? lucide(key) : { kind: 'unknown', raw: tokens.join(' ') }
69
+ }
70
+
71
+ // ---------- Other icon fonts / Iconify ----------
72
+ // "mdi mdi-home", "bi bi-house", "ti ti-home", "ri-home-line", "lucide-home".
73
+ const FONT_PREFIXES = ['mdi', 'bi', 'ti', 'ri', 'bx', 'la', 'fi', 'lucide', 'glyphicon', 'icon', 'pi', 'el', 'uil']
74
+
75
+ function parseOtherFont(tokens: string[]): IconSpec | null {
76
+ for (const prefix of FONT_PREFIXES) {
77
+ const tag = `${prefix}-`
78
+ const token = tokens.find((t) => t.startsWith(tag) && t.length > tag.length)
79
+ if (!token) continue
80
+ // Only a font-class shape: "mdi mdi-home" (family token + class) or a
81
+ // lone "mdi-home"; never "mdi-home extra words".
82
+ if (!tokens.every((t) => t === prefix || t === token)) continue
83
+ let rest = token.slice(tag.length).toLowerCase()
84
+ const direct = lucideKeyFor(rest)
85
+ if (direct) return lucide(direct)
86
+ // Variant suffixes: "ri-home-line", "bi-house-fill", "ti-home-2".
87
+ rest = rest.replace(/-(line|fill|outline|filled|solid|regular|thin)$/, '')
88
+ const key = lucideKeyFor(rest)
89
+ if (key) return lucide(key)
90
+ return { kind: 'unknown', raw: tokens.join(' ') }
91
+ }
92
+ return null
93
+ }
94
+
95
+ // ---------- Emoji ----------
96
+ const EMOJI_FULL =
97
+ /^(?:\p{Regional_Indicator}{2}|[0-9#*]️?⃣|\p{Extended_Pictographic}[\u{E0020}-\u{E007F}]+|\p{Extended_Pictographic}(?:️|\p{Emoji_Modifier})?(?:‍\p{Extended_Pictographic}(?:️|\p{Emoji_Modifier})?)*️?)$/u
98
+ const EMOJI_START = /^(?:\p{Regional_Indicator}|[0-9#*]️?⃣|\p{Extended_Pictographic})/u
99
+
100
+ function graphemeCount(s: string): number | null {
101
+ const Seg = (Intl as unknown as { Segmenter?: new (l?: string, o?: { granularity: 'grapheme' }) => { segment(s: string): Iterable<unknown> } }).Segmenter
102
+ if (!Seg) return null
103
+ let n = 0
104
+ for (const _ of new Seg(undefined, { granularity: 'grapheme' }).segment(s)) {
105
+ void _
106
+ n++
107
+ if (n > 1) break
108
+ }
109
+ return n
110
+ }
111
+
112
+ export function isSingleEmoji(value: string): boolean {
113
+ if (!value || value.length > 64) return false
114
+ const count = graphemeCount(value)
115
+ // With Intl.Segmenter: one grapheme that starts like an emoji. Without it
116
+ // the strict sequence grammar above does the same job.
117
+ return count !== null ? count === 1 && EMOJI_START.test(value) : EMOJI_FULL.test(value)
118
+ }
119
+
120
+ // ---------- Images ----------
121
+ const DATA_IMAGE = /^data:image\/(?:png|jpeg|webp|gif|svg\+xml)(?:;[a-z0-9=.+-]+)*,[^\s"'<>\\]*$/i
122
+ const HTTP_URL = /^https?:\/\/[^\s"'<>\\]+$/i
123
+ // "/uploads/a.png", "./a.png", "../a.png" ("//host" is protocol-relative: no).
124
+ const ROOTED_PATH = /^(?:\/(?!\/)|\.\.?\/)[^\s"'<>\\]*$/
125
+ // "uploads/a.png": a bare relative path is an image only with an image extension.
126
+ const BARE_IMAGE_PATH = /^[A-Za-z0-9_][^\s"'<>\\:]*\.(?:png|jpe?g|webp|gif|svg|avif|ico)(?:[?#][^\s"'<>\\]*)?$/i
127
+
128
+ function isImageSource(v: string): boolean {
129
+ return DATA_IMAGE.test(v) || HTTP_URL.test(v) || ROOTED_PATH.test(v) || BARE_IMAGE_PATH.test(v)
130
+ }
131
+
132
+ /** Classifies an icon value. Pure and deterministic. */
133
+ export function parseIconSpec(spec: unknown): IconSpec {
134
+ if (typeof spec !== 'string') return { kind: 'unknown', raw: spec == null ? '' : String(spec) }
135
+ const raw = spec
136
+ const value = spec.trim()
137
+ if (!value || value.length > 2048 || /[\u0000-\u001F]/.test(value)) return { kind: 'unknown', raw }
138
+
139
+ if (isImageSource(value)) return { kind: 'image', src: value }
140
+ // javascript:, vbscript:, data:text/html, file:, blob: ... never an icon.
141
+ if (/^(?:javascript|vbscript|data|file|blob|about):/i.test(value)) return { kind: 'unknown', raw }
142
+ if (/^[a-z][a-z0-9+.-]*:/i.test(value) && !/^[a-z0-9-]+:[a-z0-9-]+$/i.test(value)) {
143
+ return { kind: 'unknown', raw }
144
+ }
145
+ if (isSingleEmoji(value)) return { kind: 'emoji', value }
146
+
147
+ // Iconify id: "mdi:home", "lucide:brain", "ph:house-bold".
148
+ const iconify = value.match(/^([a-z0-9-]+):([a-z0-9-]+)$/i)
149
+ if (iconify) {
150
+ const key = lucideKeyFor(iconify[2]!) ?? lucideKeyFor(iconify[2]!.replace(/-(line|fill|bold|outline|solid)$/, ''))
151
+ return key ? lucide(key) : { kind: 'unknown', raw }
152
+ }
153
+
154
+ const tokens = value.split(/\s+/)
155
+ if (tokens.length === 1) {
156
+ const direct = lucideKeyFor(value)
157
+ if (direct) return lucide(direct)
158
+ }
159
+ const fa = parseFontAwesome(tokens)
160
+ if (fa) return fa.kind === 'unknown' ? { kind: 'unknown', raw } : fa
161
+ const other = parseOtherFont(tokens)
162
+ if (other) return other.kind === 'unknown' ? { kind: 'unknown', raw } : other
163
+ // Material Icons ligatures: "material-icons home" / "material-symbols-outlined home".
164
+ if (tokens.length === 2 && /^material-(icons|symbols)(-[a-z]+)?$/.test(tokens[0]!)) {
165
+ const key = lucideKeyFor(tokens[1]!)
166
+ if (key) return lucide(key)
167
+ }
168
+ return { kind: 'unknown', raw }
169
+ }
package/src/index.ts CHANGED
@@ -422,6 +422,9 @@ export {
422
422
  type UseHotSwapReloadResult,
423
423
  } from './hotswap-reload-policy'
424
424
  export * from './dynamic-icon'
425
+ export * from './icon-spec'
426
+ export * from './icon-resolvers'
427
+ export { FONT_AWESOME_ALIASES } from './icon-aliases'
425
428
  export type {
426
429
  ColumnFilterConfig,
427
430
  FilterOption as DynamicColumnFilterOption,
package/src/types.ts CHANGED
@@ -451,6 +451,9 @@ export interface ColumnDefinition {
451
451
  | 'phone'
452
452
  | 'media-gallery'
453
453
  | 'image'
454
+ // Any icon value (Lucide, FontAwesome classes, emoji, image URL, host
455
+ // resolvers): rendered through the universal icon resolver.
456
+ | 'icon'
454
457
  // Landscape stack: wide image on top, label underneath (logos/photos).
455
458
  | 'image_stack'
456
459
  // Declarative pro cell renderers (resolved via `cellStyle ?? type`).