|
1 | 1 | <script> |
2 | 2 | import { page } from '$app/stores'; |
| 3 | + import { onDestroy } from 'svelte'; |
3 | 4 |
|
| 5 | + /** |
| 6 | + * @typedef {unknown | Promise<unknown>} EncodeableData |
| 7 | + * @typedef {import('svelte/store').Readable<EncodeableData>} EncodeableDataStore |
| 8 | + * |
| 9 | + * @typedef {EncodeableData | EncodeableDataStore} DebugData |
| 10 | + */ |
| 11 | +
|
| 12 | + /** |
| 13 | + * Data to be displayed as pretty JSON. |
| 14 | + * |
| 15 | + * @type {DebugData} |
| 16 | + */ |
| 17 | + export let data; |
| 18 | + /** |
| 19 | + * Controls when the component should be displayed. |
| 20 | + * |
| 21 | + * Default: `true`. |
| 22 | + */ |
4 | 23 | export let display = true; |
| 24 | + /** |
| 25 | + * Controls when to show the http status code of the current page (reflecs the status code of the last request). |
| 26 | + * |
| 27 | + * Default is `true`. |
| 28 | + */ |
5 | 29 | export let status = true; |
6 | | - /** @type {any} */ |
7 | | - export let data; |
| 30 | + /** |
| 31 | + * Optional label to identify the component easily. |
| 32 | + */ |
| 33 | + export let label = ''; |
| 34 | + /** |
| 35 | + * Controls the maximum length of a string field of the data prop. |
| 36 | + * |
| 37 | + * Default is `120` characters. |
| 38 | + */ |
8 | 39 | export let stringTruncate = 120; |
9 | | - /** @type {HTMLPreElement | undefined} */ |
| 40 | + /** |
| 41 | + * Reference to the pre element that contains the shown d |
| 42 | + * |
| 43 | + * @type {HTMLPreElement | undefined} |
| 44 | + */ |
10 | 45 | export let ref = undefined; |
11 | | - export let label = ''; |
| 46 | + /** |
| 47 | + * Controls if the data prop should be treated as a promise (skips promise detection when true). |
| 48 | + * |
| 49 | + * Default is `false`. |
| 50 | + */ |
12 | 51 | export let promise = false; |
| 52 | + /** |
| 53 | + * Controls if the data prop should be treated as a plain object (skips promise and store detection when true, prevails over promise prop). |
| 54 | + * |
| 55 | + * Default is `false`. |
| 56 | + */ |
| 57 | + export let raw = false; |
| 58 | + /** |
| 59 | + * Enables the display of fields of the data prop that are functions. |
| 60 | + * |
| 61 | + * Default is `false`. |
| 62 | + */ |
| 63 | + export let functions = false; |
13 | 64 |
|
14 | 65 | /** |
15 | | - * @param {any} json |
| 66 | + * @param {unknown} json |
16 | 67 | * @returns {string} |
17 | 68 | */ |
18 | 69 | function syntaxHighlight(json) { |
|
31 | 82 | if (typeof value === 'bigint') { |
32 | 83 | return '#}BI#' + value; |
33 | 84 | } |
| 85 | + if (typeof value === 'function' && functions) { |
| 86 | + return '#}F#' + `[function ${value.name}]`; |
| 87 | + } |
34 | 88 | return value; |
35 | 89 | }, |
36 | 90 | 2 |
37 | 91 | ) |
38 | 92 | .replace(/&/g, '&') |
39 | 93 | .replace(/</g, '<') |
40 | 94 | .replace(/>/g, '>'); |
| 95 | +
|
41 | 96 | return encodedString.replace( |
42 | 97 | /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, |
43 | 98 | function (match) { |
|
67 | 122 | } else if (match.startsWith('"#}BI#')) { |
68 | 123 | cls = 'bigint'; |
69 | 124 | match = match.slice(6, -1) + 'n'; |
| 125 | + } else if (match.startsWith('"#}F#')) { |
| 126 | + cls = 'function'; |
| 127 | + match = match.slice(5, -1); |
70 | 128 | } |
71 | 129 | } |
72 | 130 | } else if (/true|false/.test(match)) { |
|
80 | 138 | } |
81 | 139 |
|
82 | 140 | /** |
83 | | - * @param {any} json |
| 141 | + * @param {EncodeableData} json |
84 | 142 | * @returns {Promise<string>} |
85 | 143 | */ |
86 | 144 | async function promiseSyntaxHighlight(json) { |
87 | 145 | json = await json; |
88 | 146 | return syntaxHighlight(json); |
89 | 147 | } |
| 148 | +
|
| 149 | + /** |
| 150 | + * @param {EncodeableData} data |
| 151 | + * @param {boolean} raw |
| 152 | + * @param {boolean} promise |
| 153 | + * @returns {data is Promise<unknown>} |
| 154 | + */ |
| 155 | + function assertPromise(data, raw, promise) { |
| 156 | + if (raw) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + return ( |
| 160 | + promise || |
| 161 | + (typeof data === 'object' && |
| 162 | + data !== null && |
| 163 | + 'then' in data && |
| 164 | + typeof data['then'] === 'function') |
| 165 | + ); |
| 166 | + } |
| 167 | +
|
| 168 | + /** |
| 169 | + * @param {DebugData} data |
| 170 | + * @param {boolean} raw |
| 171 | + * @returns {data is EncodeableDataStore} |
| 172 | + */ |
| 173 | + function assertStore(data, raw) { |
| 174 | + if (raw) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + return ( |
| 178 | + typeof data === 'object' && |
| 179 | + data !== null && |
| 180 | + 'subscribe' in data && |
| 181 | + typeof data['subscribe'] === 'function' |
| 182 | + ); |
| 183 | + } |
| 184 | +
|
| 185 | + /** @type {EncodeableData} */ |
| 186 | + let debugData; |
| 187 | +
|
| 188 | + let dataIsStore = false; |
| 189 | + if (assertStore(data, raw)) { |
| 190 | + dataIsStore = true; |
| 191 | + const unsubscribe = data.subscribe((value) => { |
| 192 | + debugData = value; |
| 193 | + }); |
| 194 | + onDestroy(unsubscribe); |
| 195 | + } |
| 196 | +
|
| 197 | + $: if (!dataIsStore) { |
| 198 | + debugData = data; |
| 199 | + } |
90 | 200 | </script> |
91 | 201 |
|
92 | 202 | {#if display} |
|
112 | 222 | class="super-debug--pre {label === '' ? 'pt-4' : 'pt-0'}" |
113 | 223 | bind:this={ref}><code class="super-debug--code" |
114 | 224 | ><slot |
115 | | - >{#if promise}{#await promiseSyntaxHighlight(data)}<div>Loading data</div>{:then result}{@html result}{/await}{:else}{@html syntaxHighlight( |
116 | | - data |
| 225 | + >{#if assertPromise(debugData, raw, promise)}{#await promiseSyntaxHighlight(debugData)}<div>Loading data...</div>{:then result}{@html result}{/await}{:else}{@html syntaxHighlight( |
| 226 | + debugData |
117 | 227 | )}{/if}</slot |
118 | 228 | ></code |
119 | 229 | ></pre> |
120 | 230 | </div> |
121 | 231 | {/if} |
122 | 232 |
|
| 233 | +<!-- |
| 234 | + @component |
| 235 | +
|
| 236 | + SuperDebug is a debugging component that gives you colorized and nicely formatted output for any data structure, usually $form. |
| 237 | + |
| 238 | + Other use cases includes debugging plain objects, promises, stores and more. |
| 239 | +
|
| 240 | + More info: |
| 241 | + - [API](https://superforms.rocks/api#superdebug) |
| 242 | + - [Examples](https://github.com/ciscoheat/sveltekit-superforms/tree/main/src/routes/super-debug) |
| 243 | + |
| 244 | + **Short example:** |
| 245 | +
|
| 246 | + ```svelte |
| 247 | + <script> |
| 248 | + import SuperDebug from 'sveltekit-superforms/client/SuperDebug.svelte'; |
| 249 | +
|
| 250 | + export let data; |
| 251 | + |
| 252 | + const { errors, form, enhance } = superForm(data.form); |
| 253 | + </script> |
| 254 | + |
| 255 | + <form method="POST" use:enhance> |
| 256 | + <label> |
| 257 | + Name<br /> |
| 258 | + <input |
| 259 | + name="name" |
| 260 | + type="text" |
| 261 | + aria-invalid={$errors.name ? 'true' : undefined} |
| 262 | + bind:value={$form.name} |
| 263 | + /> |
| 264 | + {#if $errors.name}<span class="invalid">{$errors.name}</span>{/if} |
| 265 | + </label> |
| 266 | + <label> |
| 267 | + Email<br /> |
| 268 | + <input |
| 269 | + name="email" |
| 270 | + type="email" |
| 271 | + aria-invalid={$errors.email ? 'true' : undefined} |
| 272 | + bind:value={$form.email} |
| 273 | + /> |
| 274 | + {#if $errors.email}<span class="invalid">{$errors.email}</span>{/if} |
| 275 | + </label> |
| 276 | + <button>Submit</button> |
| 277 | + </form> |
| 278 | + <SuperDebug data={$form} label="My form data" /> |
| 279 | + ``` |
| 280 | +--> |
| 281 | + |
123 | 282 | <style> |
124 | 283 | .absolute { |
125 | 284 | position: absolute; |
|
0 commit comments