Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(editor): refresh text presets and look matching
Richie McIlroy39a305a618aa
diff --git a/apps/desktop/src/routes/editor/text-presets.test.ts b/apps/desktop/src/routes/editor/text-presets.test.tsnew file mode 100644index 000000000..9f56d4433--- /dev/null+++ b/apps/desktop/src/routes/editor/text-presets.test.ts@@ -0,0 +1,80 @@+import { describe, expect, it } from "vitest";+import { defaultTextSegment } from "./text";+import {+ applyTextPreset,+ matchTextPreset,+ TEXT_PRESET_GROUPS,+ TEXT_PRESETS,+} from "./text-presets";++const INSTALLED = ["Helvetica Neue", "Georgia", "Menlo"];++describe("text presets", () => {+ it("every preset belongs to a listed group and ends its stack in a generic", () => {+ for (const preset of TEXT_PRESETS) {+ expect(TEXT_PRESET_GROUPS).toContain(preset.group);+ expect(["sans-serif", "serif", "monospace"]).toContain(+ preset.style.fontStack.at(-1),+ );+ }+ expect(TEXT_PRESETS.map((preset) => preset.id)).toHaveLength(+ new Set(TEXT_PRESETS.map((preset) => preset.id)).size,+ );+ });++ it("applying a preset is recognised as that preset", () => {+ for (const preset of TEXT_PRESETS) {+ const segment = defaultTextSegment(0, 2);+ applyTextPreset(segment, preset, INSTALLED);+ expect(matchTextPreset(segment, INSTALLED)).toBe(preset.id);+ }+ });++ it("adopts the sample only while the content is untouched", () => {+ const fresh = defaultTextSegment(0, 2);+ applyTextPreset(fresh, TEXT_PRESETS[0], INSTALLED);+ expect(fresh.content).toBe(TEXT_PRESETS[0].sample);++ const written = defaultTextSegment(0, 2);+ written.content = "Hello there";+ applyTextPreset(written, TEXT_PRESETS[0], INSTALLED);+ expect(written.content).toBe("Hello there");+ });++ it("keeps the segment colour unless the preset sets one and clears leftover looks", () => {+ const segment = defaultTextSegment(0, 2);+ segment.color = "#123456";+ const sticker = TEXT_PRESETS.find((preset) => preset.id === "sticker");+ const title = TEXT_PRESETS.find((preset) => preset.id === "title");+ if (!sticker || !title) throw new Error("presets missing");++ applyTextPreset(segment, sticker, INSTALLED);+ expect(segment.color).toBe("#ffffff");+ expect(segment.strokeWidth).toBe(8);++ segment.color = "#123456";+ applyTextPreset(segment, title, INSTALLED);+ expect(segment.color).toBe("#123456");+ expect(segment.strokeWidth).toBe(0);+ expect(segment.backgroundColor).toBeNull();+ expect(segment.gradientColor).toBeNull();+ expect(segment.glow).toBe(0);+ });++ it("scales the box about its top edge and moves it only for placed presets", () => {+ const segment = defaultTextSegment(0, 2);+ const topEdge = segment.center.y - segment.size.y / 2;+ const title = TEXT_PRESETS.find((preset) => preset.id === "title");+ const lowerThird = TEXT_PRESETS.find(+ (preset) => preset.id === "lower-third",+ );+ if (!title || !lowerThird) throw new Error("presets missing");++ applyTextPreset(segment, title, INSTALLED);+ expect(segment.center.x).toBe(0.5);+ expect(segment.center.y - segment.size.y / 2).toBeCloseTo(topEdge, 6);++ applyTextPreset(segment, lowerThird, INSTALLED);+ expect(segment.center).toEqual(lowerThird.center);+ });+});diff --git a/apps/desktop/src/routes/editor/text-presets.ts b/apps/desktop/src/routes/editor/text-presets.tsindex 9405683e9..5b08c7919 100644--- a/apps/desktop/src/routes/editor/text-presets.ts+++ b/apps/desktop/src/routes/editor/text-presets.ts@@ -1,4 +1,9 @@-import type { TextAlign, TextAnimation, TextSegment } from "./text";+import type {+ TextAlign,+ TextAnimation,+ TextBackgroundStyle,+ TextSegment,+} from "./text";export type TextPresetStyle = {fontStack: string[];@@ -10,6 +15,13 @@ export type TextPresetStyle = {letterSpacing: number;lineHeight: number;shadow: number;+ glow: number;+ strokeWidth: number;+ strokeColor: string;+ backgroundStyle: TextBackgroundStyle;+ backgroundColor: string | null;+ color: string | null;+ gradientColor: string | null;animationIn: TextAnimation;animationInDuration: number;animationOut: TextAnimation;@@ -18,6 +30,7 @@ export type TextPresetStyle = {export type TextPreset = {id: string;+ group: string;name: string;sample: string;style: TextPresetStyle;@@ -25,13 +38,27 @@ export type TextPreset = {center?: { x: number; y: number };};+export const TEXT_PRESET_GROUPS = [+ "All",+ "Titles",+ "Lower thirds",+ "Callouts",+ "Statements",+ "Code",+];++const SANS = ["Helvetica Neue", "Segoe UI", "Inter", "sans-serif"];+const SERIF = ["Georgia", "Times New Roman", "serif"];+const MONO = ["Menlo", "Consolas", "monospace"];+export const TEXT_PRESETS: TextPreset[] = [{id: "title",+ group: "Titles",name: "Title",- sample: "Big Title",+ sample: "Introducing Cap",style: {- fontStack: ["Helvetica Neue", "Segoe UI", "Inter", "sans-serif"],+ fontStack: SANS,fontSize: 96,fontWeight: 700,italic: false,@@ -40,6 +67,13 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: -1,lineHeight: 1.1,shadow: 0.35,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,animationIn: "slideUp",animationInDuration: 0.35,animationOut: "fade",@@ -47,32 +81,97 @@ export const TEXT_PRESETS: TextPreset[] = [},},{- id: "subtitle",- name: "Subtitle",- sample: "A calmer supporting line",+ id: "headline",+ group: "Titles",+ name: "Headline",+ sample: "Ship faster",style: {- fontStack: ["Helvetica Neue", "Segoe UI", "Inter", "sans-serif"],- fontSize: 44,- fontWeight: 500,+ fontStack: SANS,+ fontSize: 112,+ fontWeight: 800,italic: false,uppercase: false,align: "center",- letterSpacing: 0,- lineHeight: 1.3,- shadow: 0.25,- animationIn: "fade",- animationInDuration: 0.3,+ letterSpacing: -3,+ lineHeight: 1,+ shadow: 0.3,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,+ animationIn: "words",+ animationInDuration: 0.6,animationOut: "fade",animationOutDuration: 0.25,},},+ {+ id: "cinematic",+ group: "Titles",+ name: "Cinematic",+ sample: "Chapter one",+ style: {+ fontStack: SERIF,+ fontSize: 64,+ fontWeight: 400,+ italic: false,+ uppercase: true,+ align: "center",+ letterSpacing: 12,+ lineHeight: 1.2,+ shadow: 0.25,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,+ animationIn: "tracking",+ animationInDuration: 0.9,+ animationOut: "tracking",+ animationOutDuration: 0.6,+ },+ },+ {+ id: "gradient",+ group: "Titles",+ name: "Gradient",+ sample: "Beautiful text",+ style: {+ fontStack: SANS,+ fontSize: 104,+ fontWeight: 800,+ italic: false,+ uppercase: false,+ align: "center",+ letterSpacing: -2,+ lineHeight: 1.05,+ shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: "#ffffff",+ gradientColor: "#b388ff",+ animationIn: "zoom",+ animationInDuration: 0.45,+ animationOut: "fade",+ animationOutDuration: 0.3,+ },+ },{id: "lower-third",- name: "Lower Third",- sample: "Name / Context",+ group: "Lower thirds",+ name: "Lower third",+ sample: "Richie McIlroy",center: { x: 0.22, y: 0.85 },style: {- fontStack: ["Helvetica Neue", "Segoe UI", "Inter", "sans-serif"],+ fontStack: SANS,fontSize: 40,fontWeight: 600,italic: false,@@ -81,18 +180,84 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: 0,lineHeight: 1.25,shadow: 0.4,- animationIn: "slideUp",+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,+ animationIn: "slideRight",+ animationInDuration: 0.35,+ animationOut: "fade",+ animationOutDuration: 0.25,+ },+ },+ {+ id: "name-tag",+ group: "Lower thirds",+ name: "Name tag",+ sample: "Richie · Founder",+ center: { x: 0.2, y: 0.86 },+ style: {+ fontStack: SANS,+ fontSize: 32,+ fontWeight: 600,+ italic: false,+ uppercase: false,+ align: "left",+ letterSpacing: 0.5,+ lineHeight: 1.2,+ shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "pill",+ backgroundColor: "#000000",+ color: "#ffffff",+ gradientColor: null,+ animationIn: "slideRight",animationInDuration: 0.3,- animationOut: "slideDown",+ animationOut: "fade",+ animationOutDuration: 0.2,+ },+ },+ {+ id: "caption",+ group: "Lower thirds",+ name: "Caption",+ sample: "Recorded with Cap",+ center: { x: 0.5, y: 0.88 },+ style: {+ fontStack: SANS,+ fontSize: 34,+ fontWeight: 500,+ italic: false,+ uppercase: false,+ align: "center",+ letterSpacing: 0,+ lineHeight: 1.3,+ shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: "#000000",+ color: "#ffffff",+ gradientColor: null,+ animationIn: "fade",+ animationInDuration: 0.25,+ animationOut: "fade",animationOutDuration: 0.25,},},{id: "kicker",+ group: "Callouts",name: "Kicker",- sample: "NEW FEATURE",+ sample: "New feature",style: {- fontStack: ["Helvetica Neue", "Segoe UI", "Inter", "sans-serif"],+ fontStack: SANS,fontSize: 26,fontWeight: 700,italic: false,@@ -101,18 +266,138 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: 6,lineHeight: 1.2,shadow: 0.2,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,animationIn: "fade",animationInDuration: 0.2,animationOut: "fade",animationOutDuration: 0.2,},},+ {+ id: "label",+ group: "Callouts",+ name: "Label",+ sample: "Pro tip",+ style: {+ fontStack: SANS,+ fontSize: 28,+ fontWeight: 600,+ italic: false,+ uppercase: false,+ align: "center",+ letterSpacing: 0.3,+ lineHeight: 1.2,+ shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "pill",+ backgroundColor: "#007aff",+ color: "#ffffff",+ gradientColor: null,+ animationIn: "pop",+ animationInDuration: 0.3,+ animationOut: "fade",+ animationOutDuration: 0.2,+ },+ },+ {+ id: "highlight",+ group: "Callouts",+ name: "Highlight",+ sample: "the important part",+ style: {+ fontStack: SANS,+ fontSize: 56,+ fontWeight: 700,+ italic: false,+ uppercase: false,+ align: "center",+ letterSpacing: 0,+ lineHeight: 1.25,+ shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "highlight",+ backgroundColor: "#ffe14d",+ color: "#111111",+ gradientColor: null,+ animationIn: "wipe",+ animationInDuration: 0.5,+ animationOut: "fade",+ animationOutDuration: 0.25,+ },+ },+ {+ id: "sticker",+ group: "Callouts",+ name: "Sticker",+ sample: "Boom!",+ style: {+ fontStack: SANS,+ fontSize: 88,+ fontWeight: 900,+ italic: false,+ uppercase: false,+ align: "center",+ letterSpacing: -1,+ lineHeight: 1.1,+ shadow: 0.3,+ glow: 0,+ strokeWidth: 8,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: "#ffffff",+ gradientColor: null,+ animationIn: "bounce",+ animationInDuration: 0.5,+ animationOut: "pop",+ animationOutDuration: 0.25,+ },+ },+ {+ id: "neon",+ group: "Callouts",+ name: "Neon",+ sample: "Glow up",+ style: {+ fontStack: SANS,+ fontSize: 84,+ fontWeight: 700,+ italic: false,+ uppercase: false,+ align: "center",+ letterSpacing: 1,+ lineHeight: 1.1,+ shadow: 0,+ glow: 1,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: "#7df9ff",+ gradientColor: null,+ animationIn: "fade",+ animationInDuration: 0.5,+ animationOut: "fade",+ animationOutDuration: 0.4,+ },+ },{id: "stat",- name: "Big Stat",+ group: "Statements",+ name: "Big stat",sample: "128%",style: {- fontStack: ["Helvetica Neue", "Segoe UI", "Inter", "sans-serif"],+ fontStack: SANS,fontSize: 160,fontWeight: 800,italic: false,@@ -121,6 +406,13 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: -2,lineHeight: 1,shadow: 0.3,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,animationIn: "pop",animationInDuration: 0.4,animationOut: "fade",@@ -129,10 +421,11 @@ export const TEXT_PRESETS: TextPreset[] = [},{id: "quote",+ group: "Statements",name: "Quote",sample: "“Make it feel effortless”",style: {- fontStack: ["Georgia", "Times New Roman", "serif"],+ fontStack: SERIF,fontSize: 56,fontWeight: 500,italic: true,@@ -141,18 +434,26 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: 0,lineHeight: 1.35,shadow: 0.2,- animationIn: "fade",- animationInDuration: 0.4,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,+ animationIn: "words",+ animationInDuration: 0.8,animationOut: "fade",animationOutDuration: 0.3,},},{id: "code",+ group: "Code",name: "Code",sample: "$ cap record",style: {- fontStack: ["Menlo", "Consolas", "monospace"],+ fontStack: MONO,fontSize: 36,fontWeight: 400,italic: false,@@ -161,6 +462,13 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: 0,lineHeight: 1.4,shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: "#0f1115",+ color: "#e6edf3",+ gradientColor: null,animationIn: "fade",animationInDuration: 0.2,animationOut: "fade",@@ -169,10 +477,11 @@ export const TEXT_PRESETS: TextPreset[] = [},{id: "typewriter",+ group: "Code",name: "Typewriter",sample: "typing it out…",style: {- fontStack: ["Menlo", "Consolas", "monospace"],+ fontStack: MONO,fontSize: 44,fontWeight: 500,italic: false,@@ -181,6 +490,13 @@ export const TEXT_PRESETS: TextPreset[] = [letterSpacing: 0,lineHeight: 1.3,shadow: 0,+ glow: 0,+ strokeWidth: 0,+ strokeColor: "#000000",+ backgroundStyle: "box",+ backgroundColor: null,+ color: null,+ gradientColor: null,animationIn: "typewriter",animationInDuration: 0.8,animationOut: "fade",@@ -206,9 +522,9 @@ export function pickFontFamily(stack: string[], installedFonts: string[]) {return stack[stack.length - 1] ?? "sans-serif";}-// Everything the preset styles is applied; content, timing, color (kept from-// the auto black/white pick) and — unless the preset implies placement —-// position stay the user's.+// Everything the preset styles is applied, including clearing the background,+// gradient, stroke and glow a previous preset left behind; content, timing and+// — unless the preset implies placement — position stay the user's.export function applyTextPreset(segment: TextSegment,preset: TextPreset,@@ -228,11 +544,19 @@ export function applyTextPreset(segment.fontSize = style.fontSize;segment.fontWeight = style.fontWeight;segment.italic = style.italic;+ segment.uppercase = style.uppercase;segment.align = style.align;segment.letterSpacing = style.letterSpacing;segment.lineHeight = style.lineHeight;segment.opacity = 1;segment.shadow = style.shadow;+ segment.glow = style.glow;+ segment.strokeWidth = style.strokeWidth;+ segment.strokeColor = style.strokeColor;+ segment.backgroundStyle = style.backgroundStyle;+ segment.backgroundColor = style.backgroundColor;+ segment.gradientColor = style.gradientColor;+ if (style.color !== null) segment.color = style.color;segment.animationIn = style.animationIn;segment.animationOut = style.animationOut;segment.animationInDuration = style.animationInDuration;@@ -241,6 +565,9 @@ export function applyTextPreset(style.animationInDuration,style.animationOutDuration,);+ const content = segment.content ?? "";+ if (content.trim() === "" || content === "Text")+ segment.content = preset.sample;if (preset.center) {segment.center = { ...preset.center };}@@ -248,6 +575,11 @@ export function applyTextPreset(const near = (a: number, b: number, eps = 0.011) => Math.abs(a - b) < eps;+const sameOptionalColor = (+ a: string | null | undefined,+ b: string | null | undefined,+) => (a ?? null) === (b ?? null);+export function matchTextPreset(segment: TextSegment,installedFonts: string[],@@ -258,10 +590,18 @@ export function matchTextPreset(segment.fontFamily === pickFontFamily(style.fontStack, installedFonts) &&segment.fontWeight === style.fontWeight &&segment.italic === style.italic &&+ (segment.uppercase ?? false) === style.uppercase &&segment.align === style.align &&near(segment.letterSpacing, style.letterSpacing) &&near(segment.lineHeight, style.lineHeight) &&near(segment.shadow, style.shadow) &&+ near(segment.glow ?? 0, style.glow) &&+ near(segment.strokeWidth ?? 0, style.strokeWidth) &&+ (style.strokeWidth <= 0 ||+ (segment.strokeColor ?? "#000000") === style.strokeColor) &&+ (segment.backgroundStyle ?? "box") === style.backgroundStyle &&+ sameOptionalColor(segment.backgroundColor, style.backgroundColor) &&+ sameOptionalColor(segment.gradientColor, style.gradientColor) &&segment.animationIn === style.animationIn &&segment.animationOut === style.animationOut &&near(segment.animationInDuration, style.animationInDuration) &&diff --git a/apps/desktop/src/routes/editor/text.ts b/apps/desktop/src/routes/editor/text.tsindex 808d87881..8a52ba1e5 100644--- a/apps/desktop/src/routes/editor/text.ts+++ b/apps/desktop/src/routes/editor/text.ts@@ -14,9 +14,19 @@ export type TextAnimation =| "fade"| "slideUp"| "slideDown"+ | "slideLeft"+ | "slideRight"| "pop"+ | "zoom"+ | "bounce"+ | "wipe"+ | "words"+ | "letters"+ | "tracking"| "typewriter";+export type TextBackgroundStyle = "box" | "pill" | "highlight";+export type TextLayout = "overlay" | "fullscreen" | "splitLeft" | "splitRight";export type TextSegment = {@@ -34,6 +44,11 @@ export type TextSegment = {uppercase: boolean;color: string;backgroundColor?: string | null;+ backgroundStyle: TextBackgroundStyle;+ gradientColor?: string | null;+ strokeWidth: number;+ strokeColor: string;+ glow: number;fadeDuration: number;align: TextAlign;letterSpacing: number;@@ -124,6 +139,11 @@ export const defaultTextSegment = (uppercase: false,color: "#ffffff",backgroundColor: undefined,+ backgroundStyle: "box",+ gradientColor: undefined,+ strokeWidth: 0,+ strokeColor: "#000000",+ glow: 0,fadeDuration: 0.15,align: "center",letterSpacing: 0,