-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRenderer.svelte
188 lines (164 loc) · 4.37 KB
/
Renderer.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<script lang="ts" module>
import "svelte-highlight/styles/github-dark.css";
export type ComponentOptions = {
center?: boolean;
package?: string;
extraComponents?: string[];
};
</script>
<script lang="ts">
import { CopyButton } from "$lib";
import Highlight from "./Highlight.svelte";
import type { StorySnippet } from "./Story.svelte";
const defaultOptions = {
center: true,
package: "@nais/ds-svelte-community",
extraComponents: [],
};
let {
componentOptions,
children,
preview,
source,
values,
}: {
values: { [key: string]: unknown };
componentOptions?: ComponentOptions;
children: StorySnippet;
source?: string;
preview?: { width?: string };
} = $props();
let showCode = $state(false);
const options = { ...defaultOptions, ...componentOptions };
const code = $derived.by(() => {
if (!source) {
return "";
}
return source.replace(/(\s*\{docProps\}\s*)/g, (p1) => {
if (Object.keys(values).length === 0) {
return p1.indexOf("\n") >= 0 ? "\n" : "";
}
return (
"\n" +
Object.keys(values)
.sort()
.map((key) => {
const value = values[key];
if (typeof value === "string") {
if (value.indexOf(`"`) === 0) {
return ` ${key}=${value}`;
}
if (value === "true") {
return ` ${key}`;
}
return ` ${key}={${value}}`;
}
return ` ${key}={${JSON.stringify(value)}}`;
})
.join("\n")
);
});
});
const fromText = (t: string): unknown => {
if (t === "undefined" || t === undefined) {
return undefined;
} else if (t === "null" || t === null) {
return null;
}
return JSON.parse(t);
};
const dejsonify = (obj: Record<string, unknown>) => {
const ret: Record<string, unknown> = {};
Object.entries(obj).forEach(([key, value]) => {
if (typeof value === "string") {
try {
ret[key] = fromText(value);
} catch {
ret[key] = value;
}
} else {
ret[key] = value;
}
});
return ret;
};
</script>
<div class="preview" class:center={options.center} class:show-code={code && showCode}>
<div class="preview-wrapper" style="width: {preview?.width}">
{@render children({ docProps: dejsonify(values) })}
<!-- <svelte:component this={component} children={defaultBody} {...restProps} /> -->
</div>
{#if code}
<button class="toggleCode" onclick={() => (showCode = !showCode)}>
{showCode ? "Hide" : "Show"} code
</button>
{/if}
</div>
{#if code && showCode}
<div class="code-preview">
<div class="copy-code">
<CopyButton size="small" copyText={code} text="Copy code" activeText="Code copied" />
</div>
<Highlight {code} />
</div>
{/if}
<style>
.preview {
background-color: var(--ax-bg-default, var(--a-surface-default));
border: 1px solid var(--ax-border-default, var(--a-border-default));
border-radius: var(--ax-border-radius-medium, var(--a-border-radius-medium));
min-height: 250px;
position: relative;
&.show-code {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
}
.preview-wrapper {
margin: 1rem;
}
.preview.center {
display: flex;
justify-content: center;
align-items: center;
}
.code-preview {
font-size: var(--ax-font-size-small, var(--a-font-size-small));
position: relative;
.copy-code {
--ac-copybutton-neutral-text: var(--ax-text-default, var(--a-text-on-inverted));
--ac-copybutton-neutral-hover-text: var(--ax-text-meta-purple, var(--a-surface-alt-1-subtle));
--ac-copybutton-neutral-active-text: var(
--ax-text-meta-purple-strong,
var(--a-surface-alt-1-moderate)
);
display: none;
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
&:hover .copy-code {
display: block;
}
& :global(pre) {
margin-top: 0;
border-bottom-left-radius: var(--ax-border-radius-medium, var(--a-border-radius-medium));
border-bottom-right-radius: var(--ax-border-radius-medium, var(--a-border-radius-medium));
}
}
.toggleCode {
position: absolute;
bottom: -1px;
right: -1px;
font-size: var(--ax-font-size-small, var(--a-font-size-small));
border: 1px solid var(--ax-border-default, var(--a-border-default));
border-radius: 0;
border-top-left-radius: var(--ax-border-radius-medium, var(--a-border-radius-medium));
padding: 0.2rem 0.5rem;
cursor: pointer;
background: var(--ax-bg-default);
&:hover {
background-color: var(--ax-bg-meta-purple-moderate-hover, var(--a-surface-alt-1-subtle));
}
}
</style>