@rokkit/ui 1.0.0-next.148 → 1.0.0-next.150
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/package.json +4 -2
- package/src/MarkdownRenderer.svelte +43 -0
- package/src/components/AlertList.svelte +8 -9
- package/src/components/List.svelte +27 -21
- package/src/components/Swatch.svelte +17 -11
- package/src/index.ts +3 -0
- package/src/markdown-plugin.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/ui",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.150",
|
|
4
4
|
"description": "Data driven UI components for Rokkit applications",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -46,7 +46,9 @@
|
|
|
46
46
|
"@rokkit/core": "latest",
|
|
47
47
|
"@rokkit/data": "latest",
|
|
48
48
|
"@rokkit/states": "latest",
|
|
49
|
-
"@rokkit/actions": "latest"
|
|
49
|
+
"@rokkit/actions": "latest",
|
|
50
|
+
"marked": "^15.0.0",
|
|
51
|
+
"isomorphic-dompurify": "^2.0.0"
|
|
50
52
|
},
|
|
51
53
|
"peerDependencies": {
|
|
52
54
|
"shiki": "^3.23.0",
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { marked } from 'marked'
|
|
3
|
+
import type { Token, TokensList } from 'marked'
|
|
4
|
+
import DOMPurify from 'isomorphic-dompurify'
|
|
5
|
+
import type { MarkdownPlugin } from './markdown-plugin.js'
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
markdown: string
|
|
9
|
+
plugins?: MarkdownPlugin[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { markdown, plugins = [] }: Props = $props()
|
|
13
|
+
|
|
14
|
+
const pluginMap = $derived(
|
|
15
|
+
Object.fromEntries(plugins.map((p) => [p.language.toLowerCase(), p.component]))
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
const tokens = $derived(marked.lexer(markdown))
|
|
19
|
+
|
|
20
|
+
function tokenToSafeHtml(token: Token): string {
|
|
21
|
+
const tokenList = Object.assign([token], { links: (tokens as TokensList).links ?? {} }) as TokensList
|
|
22
|
+
const raw = marked.parser(tokenList)
|
|
23
|
+
return DOMPurify.sanitize(raw)
|
|
24
|
+
}
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<div class="markdown-renderer" data-markdown>
|
|
28
|
+
{#each tokens as token, i (i)}
|
|
29
|
+
{#if token.type === 'code'}
|
|
30
|
+
{@const lang = (token.lang ?? '').toLowerCase()}
|
|
31
|
+
{@const Plugin = pluginMap[lang]}
|
|
32
|
+
{#if Plugin}
|
|
33
|
+
<Plugin code={token.text} />
|
|
34
|
+
{:else}
|
|
35
|
+
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
|
36
|
+
{@html tokenToSafeHtml(token)}
|
|
37
|
+
{/if}
|
|
38
|
+
{:else}
|
|
39
|
+
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
|
40
|
+
{@html tokenToSafeHtml(token)}
|
|
41
|
+
{/if}
|
|
42
|
+
{/each}
|
|
43
|
+
</div>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte'
|
|
2
3
|
import { SvelteSet } from 'svelte/reactivity'
|
|
3
4
|
import { alerts } from '@rokkit/states'
|
|
4
5
|
import Message from './Message.svelte'
|
|
@@ -18,17 +19,15 @@
|
|
|
18
19
|
|
|
19
20
|
const { position = 'top-right', class: className = '' }: AlertListProps = $props()
|
|
20
21
|
|
|
21
|
-
let el: HTMLElement | undefined = $state()
|
|
22
22
|
const dismissing = new SvelteSet<string>()
|
|
23
23
|
|
|
24
24
|
// Portal to document.body so position:fixed is relative to the viewport,
|
|
25
25
|
// not clipped by any overflow:auto ancestor (e.g. the docs main column).
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
26
|
+
|
|
27
|
+
function mountPortal(node: HTMLElement) {
|
|
28
|
+
document.body.appendChild(node)
|
|
29
|
+
return { destroy: () => node.remove() }
|
|
30
|
+
}
|
|
32
31
|
|
|
33
32
|
function startDismiss(id: string) {
|
|
34
33
|
dismissing.add(id)
|
|
@@ -42,7 +41,7 @@
|
|
|
42
41
|
}
|
|
43
42
|
</script>
|
|
44
43
|
|
|
45
|
-
<div
|
|
44
|
+
<div use:mountPortal data-alert-list data-position={position} class={className || undefined}>
|
|
46
45
|
{#each alerts.current as alert (alert.id)}
|
|
47
46
|
<div
|
|
48
47
|
data-dismissing={dismissing.has(alert.id) || undefined}
|
|
@@ -52,7 +51,7 @@
|
|
|
52
51
|
type={alert.type as 'error' | 'info' | 'success' | 'warning'}
|
|
53
52
|
text={alert.text}
|
|
54
53
|
dismissible={alert.dismissible}
|
|
55
|
-
actions={alert.actions as
|
|
54
|
+
actions={alert.actions as Snippet}
|
|
56
55
|
ondismiss={() => startDismiss(alert.id)}
|
|
57
56
|
/>
|
|
58
57
|
</div>
|
|
@@ -86,28 +86,34 @@
|
|
|
86
86
|
// collapsible=false: groups are fixed section headers — always expanded.
|
|
87
87
|
// collapsible=true: only expand the ancestor group of the active value;
|
|
88
88
|
// all other groups start collapsed (user can toggle them).
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (proxy.hasChildren) proxy.expanded = true
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
let activeKey: string | null = null
|
|
96
|
-
for (const [key, proxy] of wrapper.lookup) {
|
|
97
|
-
if (proxy.value === value) {
|
|
98
|
-
activeKey = key
|
|
99
|
-
break
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
for (const [key, proxy] of wrapper.lookup) {
|
|
103
|
-
if (proxy.hasChildren) {
|
|
104
|
-
proxy.expanded =
|
|
105
|
-
activeKey !== null &&
|
|
106
|
-
(activeKey === key || activeKey.startsWith(`${key }-`))
|
|
107
|
-
}
|
|
108
|
-
}
|
|
89
|
+
function expandAllGroups() {
|
|
90
|
+
for (const [, proxy] of wrapper.lookup) {
|
|
91
|
+
if (proxy.hasChildren) proxy.expanded = true
|
|
109
92
|
}
|
|
110
|
-
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function findActiveKey(): string | null {
|
|
96
|
+
for (const [key, proxy] of wrapper.lookup) {
|
|
97
|
+
if (proxy.value === value) return key
|
|
98
|
+
}
|
|
99
|
+
return null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function expandAncestorGroups(activeKey: string | null) {
|
|
103
|
+
for (const [key, proxy] of wrapper.lookup) {
|
|
104
|
+
if (!proxy.hasChildren) continue
|
|
105
|
+
proxy.expanded =
|
|
106
|
+
activeKey !== null &&
|
|
107
|
+
(activeKey === key || activeKey.startsWith(`${key }-`))
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function syncExpandedGroups() {
|
|
112
|
+
if (!collapsible) { expandAllGroups(); return }
|
|
113
|
+
expandAncestorGroups(findActiveKey())
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
$effect(syncExpandedGroups)
|
|
111
117
|
|
|
112
118
|
// ─── Sync external value → focused key ────────────────────────────────────
|
|
113
119
|
|
|
@@ -38,19 +38,25 @@
|
|
|
38
38
|
return proxy.value === value
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
function toggleMultiValue(extracted, original) {
|
|
42
|
+
const arr = Array.isArray(value) ? [...value] : []
|
|
43
|
+
const idx = arr.indexOf(extracted)
|
|
44
|
+
if (idx >= 0) arr.splice(idx, 1)
|
|
45
|
+
else arr.push(extracted)
|
|
46
|
+
value = arr
|
|
47
|
+
onchange?.(value, original)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function selectSingleValue(extracted, original) {
|
|
51
|
+
if (extracted === value) return
|
|
52
|
+
value = extracted
|
|
53
|
+
onchange?.(extracted, original)
|
|
54
|
+
}
|
|
55
|
+
|
|
41
56
|
function handleSelect(extracted, proxy) {
|
|
42
57
|
if (proxy.disabled || disabled) return
|
|
43
|
-
if (multiple)
|
|
44
|
-
|
|
45
|
-
const idx = arr.indexOf(extracted)
|
|
46
|
-
if (idx >= 0) arr.splice(idx, 1)
|
|
47
|
-
else arr.push(extracted)
|
|
48
|
-
value = arr
|
|
49
|
-
onchange?.(value, proxy.original)
|
|
50
|
-
} else if (extracted !== value) {
|
|
51
|
-
value = extracted
|
|
52
|
-
onchange?.(extracted, proxy.original)
|
|
53
|
-
}
|
|
58
|
+
if (multiple) toggleMultiValue(extracted, proxy.original)
|
|
59
|
+
else selectSingleValue(extracted, proxy.original)
|
|
54
60
|
}
|
|
55
61
|
</script>
|
|
56
62
|
|
package/src/index.ts
CHANGED
|
@@ -43,6 +43,9 @@ export {
|
|
|
43
43
|
AlertList
|
|
44
44
|
} from './components/index.js'
|
|
45
45
|
|
|
46
|
+
export { default as MarkdownRenderer } from './MarkdownRenderer.svelte'
|
|
47
|
+
export type { MarkdownPlugin } from './markdown-plugin.js'
|
|
48
|
+
|
|
46
49
|
// Utilities
|
|
47
50
|
export { highlightCode, preloadHighlighter, getSupportedLanguages } from './utils/shiki.js'
|
|
48
51
|
export * from './utils/palette.js'
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Component } from 'svelte'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A plugin that renders a fenced code block as a Svelte component.
|
|
5
|
+
* The component receives { code: string } as props.
|
|
6
|
+
*/
|
|
7
|
+
export interface MarkdownPlugin {
|
|
8
|
+
/** Fenced code block language to match (e.g. 'plot', 'table', 'sparkline') */
|
|
9
|
+
language: string
|
|
10
|
+
/** Svelte component to render the block. Receives { code: string } */
|
|
11
|
+
component: Component<{ code: string }>
|
|
12
|
+
}
|