mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Show size and a draft indicator in snippet-library rows
This commit is contained in:
@@ -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`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user