Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(editor): simplify ConfigSidebar with extracted components

Richie McIlroy9ba81b0a76aa
diff --git a/apps/desktop/src/routes/editor/ConfigSidebar.tsx b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
index 876531371..0bdf2c84b 100644
--- a/apps/desktop/src/routes/editor/ConfigSidebar.tsx
+++ b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
@@ -65,7 +65,9 @@ import IconLucideTimer from "~icons/lucide/timer";
import IconLucideType from "~icons/lucide/type";
import IconLucideWind from "~icons/lucide/wind";
import { CaptionsTab } from "./CaptionsTab";
+import { hexToRgb, RgbInput, rgbToHex } from "./color-utils";
import { type CornerRoundingType, useEditorContext } from "./context";
+import { GradientEditor } from "./GradientEditor";
import { evaluateMask, type MaskKind, type MaskSegment } from "./masks";
import {
DEFAULT_GRADIENT_FROM,
@@ -128,27 +130,6 @@ const BACKGROUND_COLORS = [
"#00000000", // Transparent
];
-const BACKGROUND_GRADIENTS = [
- { from: [15, 52, 67], to: [52, 232, 158] }, // Dark Blue to Teal
- { from: [34, 193, 195], to: [253, 187, 45] }, // Turquoise to Golden Yellow
- { from: [29, 253, 251], to: [195, 29, 253] }, // Cyan to Purple
- { from: [69, 104, 220], to: [176, 106, 179] }, // Blue to Violet
- { from: [106, 130, 251], to: [252, 92, 125] }, // Soft Blue to Pinkish Red
- { from: [131, 58, 180], to: [253, 29, 29] }, // Purple to Red
- { from: [249, 212, 35], to: [255, 78, 80] }, // Yellow to Coral Red
- { from: [255, 94, 0], to: [255, 42, 104] }, // Orange to Reddish Pink
- { from: [255, 0, 150], to: [0, 204, 255] }, // Pink to Sky Blue
- { from: [0, 242, 96], to: [5, 117, 230] }, // Green to Blue
- { from: [238, 205, 163], to: [239, 98, 159] }, // Peach to Soft Pink
- { from: [44, 62, 80], to: [52, 152, 219] }, // Dark Gray Blue to Light Blue
- { from: [168, 239, 255], to: [238, 205, 163] }, // Light Blue to Peach
- { from: [74, 0, 224], to: [143, 0, 255] }, // Deep Blue to Bright Purple
- { from: [252, 74, 26], to: [247, 183, 51] }, // Deep Orange to Soft Yellow
- { from: [0, 255, 255], to: [255, 20, 147] }, // Cyan to Deep Pink
- { from: [255, 127, 0], to: [255, 255, 0] }, // Orange to Yellow
- { from: [255, 0, 255], to: [0, 255, 0] }, // Magenta to Green
-] satisfies Array<{ from: RGBColor; to: RGBColor }>;
-
const WALLPAPER_NAMES = [
// macOS wallpapers
"macOS/tahoe-dusk-min",
@@ -1357,8 +1338,6 @@ function BackgroundConfig(props: { scrollRef: HTMLDivElement }) {
},
};
- const hapticsEnabled = ostype() === "macos";
-
return (
<KTabs.Content value={TAB_IDS.background} class="flex flex-col gap-6 p-4">
<Field icon={<IconCapImage class="size-4" />} name="Background Image">
@@ -1834,128 +1813,8 @@ function BackgroundConfig(props: { scrollRef: HTMLDivElement }) {
</div>
</Show>
</KTabs.Content>
- <KTabs.Content value="gradient" class="flex flex-row justify-between">
- <Show
- when={
- project.background.source.type === "gradient" &&
- project.background.source
- }
- >
- {(source) => {
- const max = 360;
-
- const { projectHistory } = useEditorContext();
-
- const angle = () => source().angle ?? 90;
-
- return (
- <div class="flex flex-col gap-3">
- <div class="flex gap-5 h-10">
- <RgbInput
- value={source().from}
- onChange={(from) => {
- backgrounds.gradient.from = from;
- setProject("background", "source", {
- type: "gradient",
- from,
- });
- }}
- />
- <RgbInput
- value={source().to}
- onChange={(to) => {
- backgrounds.gradient.to = to;
- setProject("background", "source", {
- type: "gradient",
- to,
- });
- }}
- />
- <div
- class="flex relative flex-col items-center p-1 ml-auto rounded-full border bg-gray-1 border-gray-3 size-10 cursor-ns-resize shrink-0"
- style={{ transform: `rotate(${angle()}deg)` }}
- onMouseDown={(downEvent) => {
- const start = angle();
- const _resumeHistory = projectHistory.pause();
-
- createRoot((dispose) =>
- createEventListenerMap(window, {
- mouseup: () => dispose(),
- mousemove: (moveEvent) => {
- const rawNewAngle =
- Math.round(
- start +
- (downEvent.clientY - moveEvent.clientY),
- ) % max;
- const newAngle = moveEvent.shiftKey
- ? rawNewAngle
- : Math.round(rawNewAngle / 45) * 45;
-
- if (
- !moveEvent.shiftKey &&
- hapticsEnabled &&
- project.background.source.type ===
- "gradient" &&
- project.background.source.angle !== newAngle
- ) {
- commands.performHapticFeedback(
- "alignment",
- "now",
- );
- }
-
- setProject("background", "source", {
- type: "gradient",
- angle:
- newAngle < 0 ? newAngle + max : newAngle,
- });
- },
- }),
- );
- }}
- >
- <div class="bg-blue-9 rounded-full size-1.5" />
- </div>
- </div>
- <div class="flex flex-wrap gap-2">
- <For each={BACKGROUND_GRADIENTS}>
- {(gradient) => (
- <label class="relative">
- <input
- type="radio"
- class="sr-only peer"
- name="colorPicker"
- onChange={(e) => {
- if (e.target.checked) {
- backgrounds.gradient = {
- type: "gradient",
- from: gradient.from,
- to: gradient.to,
- };
- setProject(
- "background",
- "source",
- backgrounds.gradient,
- );
- }
- }}
- />
- <div
- class="rounded-lg transition-all duration-200 cursor-pointer size-8 peer-checked:hover:opacity-100 peer-hover:opacity-70 peer-checked:ring-2 peer-checked:ring-gray-500 peer-checked:ring-offset-2 peer-checked:ring-offset-gray-200"
- style={{
- background: `linear-gradient(${angle()}deg, rgb(${gradient.from.join(
- ",",
- )}), rgb(${gradient.to.join(",")}))`,
- }}
- />
- </label>
- )}
- </For>
- </div>
- </div>
- );
- }}
- </Show>
+ <KTabs.Content value="gradient">
+ <GradientEditor />
</KTabs.Content>
</KTabs>
</Field>
@@ -2815,6 +2674,7 @@ function MaskSegmentConfig(props: {
segment.opacity = 1;
} else {
segment.feather = 0.1;
+ segment.fadeDuration = 0;
}
})
}
@@ -2890,20 +2750,22 @@ function MaskSegmentConfig(props: {
/>
</Field>
</Show>
- <Field name="Fade Duration" icon={<IconLucideTimer class="size-4" />}>
- <Slider
- value={[props.segment.fadeDuration ?? 0.15]}
- onChange={([v]) =>
- updateSegment((segment) => {
- segment.fadeDuration = v;
- })
- }
- minValue={0}
- maxValue={1}
- step={0.01}
- formatTooltip="s"
- />
- </Field>
+ <Show when={props.segment.maskType === "highlight"}>
+ <Field name="Fade Duration" icon={<IconLucideTimer class="size-4" />}>
+ <Slider
+ value={[props.segment.fadeDuration ?? 0.15]}
+ onChange={([v]) =>
+ updateSegment((segment) => {
+ segment.fadeDuration = v;
+ })
+ }
+ minValue={0}
+ maxValue={1}
+ step={0.01}
+ formatTooltip="s"
+ />
+ </Field>
+ </Show>
</div>
);
}
@@ -3632,101 +3494,4 @@ function SceneSegmentConfig(props: {
);
}
-function RgbInput(props: {
- value: [number, number, number];
- onChange: (value: [number, number, number]) => void;
-}) {
- const [text, setText] = createWritableMemo(() => rgbToHex(props.value));
- let prevHex = rgbToHex(props.value);
-
- let colorInput!: HTMLInputElement;
-
- return (
- <div class="flex flex-row items-center gap-[0.75rem] relative">
- <button
- type="button"
- class="size-[2rem] rounded-[0.5rem]"
- style={{
- "background-color": rgbToHex(props.value),
- }}
- onClick={() => colorInput.click()}
- />
- <input
- ref={colorInput}
- type="color"
- class="absolute left-0 bottom-0 w-[3rem] opacity-0"
- value={rgbToHex(props.value)}
- onChange={(e) => {
- const value = hexToRgb(e.target.value);
- if (!value) return;
-
- // RgbInput only handles RGB values, so extract RGB part if RGBA is returned
- const [r, g, b] = value;
- props.onChange([r, g, b]);
- }}
- />
- <TextInput
- class="w-[4.60rem] p-[0.375rem] text-gray-12 text-[13px] border rounded-[0.5rem] bg-gray-1 outline-none focus:ring-1 transition-shadows duration-200 focus:ring-gray-500 focus:ring-offset-1 focus:ring-offset-gray-200"
- value={text()}
- onFocus={() => {
- prevHex = rgbToHex(props.value);
- }}
- onInput={(e) => {
- setText(e.currentTarget.value);
-
- const value = hexToRgb(e.target.value);
- if (!value) return;
-
- const [r, g, b] = value;
- props.onChange([r, g, b]);
- }}
- onBlur={(e) => {
- const value = hexToRgb(e.target.value);
- if (value) {
- const [r, g, b] = value;
- // RgbInput only handles RGB values, so extract RGB part if RGBA is returned
- props.onChange([r, g, b]);
- } else {
- setText(prevHex);
- const fallbackValue = hexToRgb(text());
- if (!fallbackValue) return;
-
- const [r, g, b] = fallbackValue;
- props.onChange([r, g, b]);
- }
- }}
- />
- </div>
- );
-}
-
-function rgbToHex(rgb: [number, number, number]) {
- return `#${rgb
- .map((c) => c.toString(16).padStart(2, "0"))
- .join("")
- .toUpperCase()}`;
-}
-
-function hexToRgb(hex: string): [number, number, number, number] | null {
- // Support both 6-digit (RGB) and 8-digit (RGBA) hex colors
- const match = hex.match(
- /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i,
- );
- if (!match) return null;
-
- const [, r, g, b, a] = match;
- const rgb = [
- Number.parseInt(r, 16),
- Number.parseInt(g, 16),
- Number.parseInt(b, 16),
- ] as const;
-
- // If alpha is provided, return RGBA tuple
- if (a) {
- return [...rgb, Number.parseInt(a, 16)];
- }
-
- return [...rgb, 255];
-}
-
const CHECKERED_BUTTON_BACKGROUND = `url("data:image/svg+xml,%3Csvg width='16' height='16' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='8' height='8' fill='%23a0a0a0'/%3E%3Crect x='8' y='8' width='8' height='8' fill='%23a0a0a0'/%3E%3C/svg%3E")`;