Show size and a draft indicator in snippet-library rows

This commit is contained in:
2026-06-05 14:11:00 +03:00
parent da20475d2f
commit ad239d7d49
4 changed files with 138 additions and 50 deletions
+26
View File
@@ -121,3 +121,29 @@ export function createSnippet(options: CreateSnippetOptions = {}): Snippet {
export function hasUnpublishedChanges(snippet: Snippet): boolean {
return snippet.draftSpec !== snippet.spec;
}
/**
* Approximate payload size of a snippet, in bytes (spec §09 `size`). Measured on
* the working draft — the version the editor and preview currently show — as its
* UTF-8 byte length, so the figure tracks what the user is actually editing.
* `TextEncoder` is a platform global (like `crypto.randomUUID` above), not a
* browser/DOM API, so it stays within the portable core.
*/
export function snippetSizeBytes(snippet: Snippet): number {
return new TextEncoder().encode(snippet.draftSpec).length;
}
/** Below this we omit the size in the library to reduce clutter (spec §02). */
const SIZE_DISPLAY_THRESHOLD = 1024;
/**
* Human-readable size for the library row, or `null` when the snippet is small
* enough that the spec says to omit it (under ~1 KB). Rounded to whole KB/MB —
* a list hint, not a precise measure.
*/
export function formatSnippetSize(bytes: number): string | null {
if (bytes < SIZE_DISPLAY_THRESHOLD) return null;
const kb = bytes / 1024;
if (kb < 1024) return `${Math.round(kb)} KB`;
return `${Math.round(kb / 1024)} MB`;
}