Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: add MiddleEllipsis component for improved text truncation and update asset and folder item components to utilize it

konstantin-paulusfb8a924a7a81
diff --git a/apps/web/src/components/sidebar-left/asset-item.tsx b/apps/web/src/components/sidebar-left/asset-item.tsx
index 4dd24361..8dbec3c9 100644
--- a/apps/web/src/components/sidebar-left/asset-item.tsx
+++ b/apps/web/src/components/sidebar-left/asset-item.tsx
@@ -17,6 +17,7 @@ import {
ContextMenuTrigger,
} from "../ui/context-menu";
import { AssetThumbnail } from "../ui/asset-thumbnail";
+import { MiddleEllipsis } from "../ui/middle-ellipsis";
import { formatAssetDuration } from "@/utils";
import { useLibrary } from "@/engine/library";
import { ASSET_DRAG_TYPE } from "./folder-item";
@@ -114,10 +115,10 @@ export function LazyAssetItem(props: LazyAssetItemProps) {
<div
ref={ref}
data-selected={props.selected}
- class="relative aspect-video w-full overflow-clip rounded bg-muted after:pointer-events-none after:absolute after:inset-0 after:rounded after:opacity-0 after:ring-2 after:ring-inset after:ring-ring after:z-10 data-[selected=true]:after:opacity-100"
+ class="relative aspect-video w-full rounded bg-muted after:pointer-events-none after:absolute after:-inset-px after:rounded-[5px] after:opacity-0 after:ring-2 after:ring-inset after:ring-ring after:z-10 data-[selected=true]:after:opacity-100"
>
<Show when={isVisible()}>
- <AssetThumbnail asset={props.asset} class="absolute inset-0" cache={library()?.cache} />
+ <AssetThumbnail asset={props.asset} class="absolute inset-0 overflow-clip rounded" cache={library()?.cache} />
</Show>
<Show when={assetDuration()}>
{(duration) => (
@@ -132,9 +133,12 @@ export function LazyAssetItem(props: LazyAssetItemProps) {
<Show
when={isRenaming()}
fallback={
- <div class="text-xs text-foreground truncate select-none" title={props.asset.source}>
- {name()}
- </div>
+ <MiddleEllipsis
+ text={name()}
+ data-selected={props.selected}
+ class="text-xs text-center text-muted-foreground select-none data-[selected=true]:text-foreground"
+ title={props.asset.source}
+ />
}
>
<input
@@ -148,7 +152,7 @@ export function LazyAssetItem(props: LazyAssetItemProps) {
onClick={(e) => e.stopPropagation()}
onContextMenu={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
- class="text-xs text-foreground truncate bg-transparent rounded-sm outline-none ring-1 ring-primary px-0.5"
+ class="text-xs text-center text-foreground truncate bg-transparent rounded-sm outline-none ring-1 ring-primary px-0.5"
/>
</Show>
</ContextMenuTrigger>
diff --git a/apps/web/src/components/sidebar-left/folder-item.tsx b/apps/web/src/components/sidebar-left/folder-item.tsx
index 3b5efad4..2bc354d7 100644
--- a/apps/web/src/components/sidebar-left/folder-item.tsx
+++ b/apps/web/src/components/sidebar-left/folder-item.tsx
@@ -14,6 +14,7 @@ import {
ContextMenuTrigger,
} from "../ui/context-menu";
import { Icon } from "../ui/icon";
+import { MiddleEllipsis } from "../ui/middle-ellipsis";
import { useLibrary } from "@/engine/library";
import type { AssetLibrary } from "@diffusionstudio/assets";
@@ -138,7 +139,7 @@ export function FolderItem(props: FolderItemProps) {
>
<div
data-drop-target={isDropTarget()}
- class="relative aspect-video w-full overflow-clip rounded bg-muted flex items-center justify-center after:pointer-events-none after:absolute after:inset-0 after:rounded after:opacity-0 after:ring-2 after:ring-inset after:ring-ring after:z-10 data-[drop-target=true]:after:opacity-100"
+ class="relative aspect-video w-full rounded bg-muted flex items-center justify-center after:pointer-events-none after:absolute after:-inset-px after:rounded-[5px] after:opacity-0 after:ring-2 after:ring-inset after:ring-ring after:z-10 data-[drop-target=true]:after:opacity-100"
>
<Icon name="folder-thumbnail" class="w-8 h-6 text-muted-foreground pointer-events-none" />
<Show when={childCount() > 0}>
@@ -149,7 +150,7 @@ export function FolderItem(props: FolderItemProps) {
</div>
<Show
when={props.renaming}
- fallback={<div class="text-xs text-foreground truncate select-none">{name()}</div>}
+ fallback={<MiddleEllipsis text={name()} class="text-xs text-center text-muted-foreground select-none" />}
>
<input
ref={handleFocusElement}
@@ -162,7 +163,7 @@ export function FolderItem(props: FolderItemProps) {
onClick={(e) => e.stopPropagation()}
onContextMenu={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
- class="text-xs text-foreground truncate bg-transparent rounded-sm outline-none ring-1 ring-primary px-0.5"
+ class="text-xs text-center text-foreground truncate bg-transparent rounded-sm outline-none ring-1 ring-primary px-0.5"
/>
</Show>
</ContextMenuTrigger>
diff --git a/apps/web/src/components/ui/middle-ellipsis.tsx b/apps/web/src/components/ui/middle-ellipsis.tsx
new file mode 100644
index 00000000..8101295f
--- /dev/null
+++ b/apps/web/src/components/ui/middle-ellipsis.tsx
@@ -0,0 +1,78 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { createMemo, createSignal, splitProps, type ComponentProps } from "solid-js";
+import { createElementSize } from "@solid-primitives/resize-observer";
+import { cx } from "@/lib/cva";
+
+const ELLIPSIS = "…";
+
+let context: CanvasRenderingContext2D | null | undefined;
+
+/**
+ * Bumped whenever the document finishes loading fonts: text measured before
+ * the web font arrived was measured in the fallback font.
+ */
+const [fontVersion, setFontVersion] = createSignal(0);
+const bumpFontVersion = () => setFontVersion((version) => version + 1);
+document.fonts.ready.then(bumpFontVersion);
+document.fonts.addEventListener("loadingdone", bumpFontVersion);
+
+/**
+ * `text` shortened in the middle, keeping its start and end, so that it fits
+ * `width` CSS pixels in `font` (a CSS font shorthand).
+ */
+function middleEllipsis(text: string, width: number, font: string): string {
+ context ??= document.createElement("canvas").getContext("2d");
+ if (!context) return text;
+
+ context.font = font;
+ const fits = (candidate: string) => context!.measureText(candidate).width <= width;
+ if (fits(text)) return text;
+
+ // Code points, so a surrogate pair is never split
+ const chars = Array.from(text);
+ const shorten = (kept: number) =>
+ chars.slice(0, Math.ceil(kept / 2)).join("") + ELLIPSIS + chars.slice(chars.length - Math.floor(kept / 2)).join("");
+
+ // The most characters that still fit beside the ellipsis
+ let low = 0;
+ let high = chars.length - 1;
+ while (low < high) {
+ const mid = Math.ceil((low + high) / 2);
+ if (fits(shorten(mid))) low = mid;
+ else high = mid - 1;
+ }
+
+ return shorten(low);
+}
+
+export type MiddleEllipsisProps = ComponentProps<"div"> & {
+ text: string;
+};
+
+/**
+ * Single-line text that is shortened in the middle rather than at the end
+ * when it does not fit its width, so both the start and the end of a name
+ * (such as its extension) stay visible.
+ */
+export function MiddleEllipsis(props: MiddleEllipsisProps) {
+ const [local, rest] = splitProps(props, ["text", "class"]);
+ const [element, setElement] = createSignal<HTMLDivElement>();
+ const size = createElementSize(element);
+
+ const shown = createMemo(() => {
+ const el = element();
+ const width = size.width;
+ fontVersion();
+ if (!el || !width) return local.text;
+ return middleEllipsis(local.text, width, getComputedStyle(el).font);
+ });
+
+ return (
+ <div ref={setElement} class={cx("overflow-hidden whitespace-nowrap", local.class)} {...rest}>
+ {shown()}
+ </div>
+ );
+}