Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots 📖

feat(editor): format text with numeric size, line height and bold italic toggles

Luis Ortizdaaf790b3c8e
diff --git a/src/core/screenshot/draw.ts b/src/core/screenshot/draw.ts
index f3ac945..4a70a0d 100644
--- a/src/core/screenshot/draw.ts
+++ b/src/core/screenshot/draw.ts
@@ -1,5 +1,5 @@
import type { Annotation, ArrowEnd } from './types';
-import { FONT_FAMILIES, FONT_SIZES, LINE_HEIGHTS, LINE_WIDTHS } from './types';
+import { DEFAULT_LINE_HEIGHT, FONT_FAMILIES, LINE_WIDTHS } from './types';
export type Ctx = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
@@ -111,13 +111,14 @@ export function drawAnnotation(ctx: Ctx, a: Annotation, originX: number, originY
break;
}
case 'text': {
- const px = a.fontSize ? FONT_SIZES[a.fontSize] : a.size;
+ const px = a.size;
const family = FONT_FAMILIES[a.fontFamily ?? 'sans-serif'];
- const style = a.fontStyle === 'italic' ? 'italic ' : '';
- const weight = a.fontStyle === 'bold' ? 700 : 500;
+ const style = a.italic ? 'italic ' : '';
+ const weight = a.bold ? 700 : 500;
ctx.fillStyle = a.color;
ctx.font = `${style}${weight} ${px}px ${family}`;
- const lh = px * LINE_HEIGHTS[a.lineHeight ?? 'md'];
+ const ratio = typeof a.lineHeight === 'number' ? a.lineHeight : DEFAULT_LINE_HEIGHT;
+ const lh = px * ratio;
a.text.split('\n').forEach((line, i) => {
ctx.fillText(line, a.x, a.y + i * lh);
});
diff --git a/src/core/screenshot/types.ts b/src/core/screenshot/types.ts
index 2009aa9..5dfb947 100644
--- a/src/core/screenshot/types.ts
+++ b/src/core/screenshot/types.ts
@@ -36,23 +36,20 @@ export const LINE_WIDTHS: Record<LineWidth, number> = {
export const LINE_WIDTH_ORDER: LineWidth[] = ['none', 'xs', 'sm', 'ms', 'md', 'ml', 'lg', 'xl'];
export type FontFamily = 'sans-serif' | 'serif' | 'monospace';
-export type FontStyleName = 'normal' | 'bold' | 'italic';
-export type FontSizeName = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
-export type LineHeightName = 'sm' | 'md' | 'lg';
-
export const FONT_FAMILIES: Record<FontFamily, string> = {
'sans-serif': 'Poppins, sans-serif',
serif: 'Georgia, serif',
monospace: 'ui-monospace, monospace',
};
-export const FONT_SIZES: Record<FontSizeName, number> = { xs: 16, sm: 22, md: 30, lg: 42, xl: 60 };
-export const LINE_HEIGHTS: Record<LineHeightName, number> = { sm: 1.1, md: 1.4, lg: 1.9 };
-
export const FONT_FAMILY_ORDER: FontFamily[] = ['sans-serif', 'serif', 'monospace'];
-export const FONT_STYLE_ORDER: FontStyleName[] = ['normal', 'bold', 'italic'];
-export const FONT_SIZE_ORDER: FontSizeName[] = ['xs', 'sm', 'md', 'lg', 'xl'];
-export const LINE_HEIGHT_ORDER: LineHeightName[] = ['sm', 'md', 'lg'];
+
+export const DEFAULT_FONT_SIZE = 32;
+export const MIN_FONT_SIZE = 8;
+export const MAX_FONT_SIZE = 200;
+export const DEFAULT_LINE_HEIGHT = 1.4;
+export const MIN_LINE_HEIGHT = 0.8;
+export const MAX_LINE_HEIGHT = 3;
export type ArrowEnd = 'none' | 'bar' | 'arrow' | 'arrow-solid' | 'circle' | 'circle-solid' | 'square' | 'square-solid';
@@ -132,10 +129,10 @@ export type Annotation =
size: number;
w?: number;
h?: number;
- fontSize?: FontSizeName;
fontFamily?: FontFamily;
- fontStyle?: FontStyleName;
- lineHeight?: LineHeightName;
+ bold?: boolean;
+ italic?: boolean;
+ lineHeight?: number;
}
| { id: string; type: 'freehand'; points: number[]; color: string; lineWidth?: LineWidth }
| { id: string; type: 'redact'; x: number; y: number; w: number; h: number; style: 'blur' | 'solid' }
diff --git a/src/locales/en.yml b/src/locales/en.yml
index 7f577b7..1b5e4d2 100644
--- a/src/locales/en.yml
+++ b/src/locales/en.yml
@@ -204,19 +204,9 @@ annotationEditor:
lineWidth: "line width"
cornerRadius: "corner radius"
font: "font"
- fontStyle: "font style"
+ bold: "Bold"
+ italic: "Italic"
lineHeight: "line height"
- style_normal: "Normal"
- style_bold: "Bold"
- style_italic: "Italic"
- size_xs: "Extra small"
- size_sm: "Small"
- size_md: "Medium"
- size_lg: "Large"
- size_xl: "Extra large"
- height_sm: "Small"
- height_md: "Medium"
- height_lg: "Large"
width_none: "No outline"
width_xs: "Extra small"
width_sm: "Small"
diff --git a/src/locales/es.yml b/src/locales/es.yml
index 7446396..ff6399e 100644
--- a/src/locales/es.yml
+++ b/src/locales/es.yml
@@ -204,19 +204,9 @@ annotationEditor:
lineWidth: "grosor"
cornerRadius: "radio"
font: "fuente"
- fontStyle: "estilo"
+ bold: "Negrita"
+ italic: "Cursiva"
lineHeight: "interlineado"
- style_normal: "Normal"
- style_bold: "Negrita"
- style_italic: "Cursiva"
- size_xs: "Muy pequeño"
- size_sm: "Pequeño"
- size_md: "Mediano"
- size_lg: "Grande"
- size_xl: "Muy grande"
- height_sm: "Pequeño"
- height_md: "Mediano"
- height_lg: "Grande"
width_none: "Sin contorno"
width_xs: "Muy fino"
width_sm: "Fino"
diff --git a/src/locales/fr.yml b/src/locales/fr.yml
index ad94115..9a0c62b 100644
--- a/src/locales/fr.yml
+++ b/src/locales/fr.yml
@@ -204,19 +204,9 @@ annotationEditor:
lineWidth: "épaisseur"
cornerRadius: "rayon"
font: "police"
- fontStyle: "style"
+ bold: "Gras"
+ italic: "Italique"
lineHeight: "interligne"
- style_normal: "Normal"
- style_bold: "Gras"
- style_italic: "Italique"
- size_xs: "Très petit"
- size_sm: "Petit"
- size_md: "Moyen"
- size_lg: "Grand"
- size_xl: "Très grand"
- height_sm: "Petit"
- height_md: "Moyen"
- height_lg: "Grand"
width_none: "Sans contour"
width_xs: "Très fin"
width_sm: "Fin"
diff --git a/src/locales/pt-BR.yml b/src/locales/pt-BR.yml
index 71f4fde..e73d81b 100644
--- a/src/locales/pt-BR.yml
+++ b/src/locales/pt-BR.yml
@@ -204,19 +204,9 @@ annotationEditor:
lineWidth: "espessura"
cornerRadius: "raio"
font: "fonte"
- fontStyle: "estilo"
+ bold: "Negrito"
+ italic: "Itálico"
lineHeight: "entrelinha"
- style_normal: "Normal"
- style_bold: "Negrito"
- style_italic: "Itálico"
- size_xs: "Muito pequeno"
- size_sm: "Pequeno"
- size_md: "Médio"
- size_lg: "Grande"
- size_xl: "Muito grande"
- height_sm: "Pequeno"
- height_md: "Médio"
- height_lg: "Grande"
width_none: "Sem contorno"
width_xs: "Muito fina"
width_sm: "Fina"
diff --git a/src/ui/shared/AnnotationEditor.tsx b/src/ui/shared/AnnotationEditor.tsx
index 5403e91..ec7cf01 100644
--- a/src/ui/shared/AnnotationEditor.tsx
+++ b/src/ui/shared/AnnotationEditor.tsx
@@ -7,6 +7,7 @@ import {
Eraser,
EyeOff,
Minus,
+ MoveVertical,
Pencil,
PenTool,
Redo2,
@@ -37,22 +38,21 @@ import type {
Annotation,
ArrowEnd,
FontFamily,
- FontSizeName,
- FontStyleName,
- LineHeightName,
LineWidth,
ScreenshotEdits,
TargetBorder,
} from '@/core/screenshot/types';
import {
ARROW_ENDS,
+ DEFAULT_FONT_SIZE,
+ DEFAULT_LINE_HEIGHT,
FONT_FAMILIES,
FONT_FAMILY_ORDER,
- FONT_SIZE_ORDER,
- FONT_SIZES,
- FONT_STYLE_ORDER,
- LINE_HEIGHT_ORDER,
LINE_WIDTH_ORDER,
+ MAX_FONT_SIZE,
+ MAX_LINE_HEIGHT,
+ MIN_FONT_SIZE,
+ MIN_LINE_HEIGHT,
SHAPE_COLORS,
TARGET_COLORS,
} from '@/core/screenshot/types';
@@ -88,6 +88,12 @@ const MIN_CROP_SIZE = 20;
const HANDLE_DISPLAY_SIZE = 10;
const HANDLE_HIT_PX = 10;
const HISTORY_LIMIT = 49;
+
+function clampNumber(raw: string, min: number, max: number): number {
+ const n = Number(raw);
+ if (!Number.isFinite(n)) return min;
+ return Math.min(max, Math.max(min, n));
+}
const SELECTION_GAP = 6;
const DRAFT_ID = 'draft';
const TARGET_ID = 'click-target';
@@ -178,9 +184,10 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
const [radius, setRadius] = useState(0);
const [arrowEnd, setArrowEnd] = useState<ArrowEnd>('arrow-solid');
const [fontFamily, setFontFamily] = useState<FontFamily>('sans-serif');
- const [fontStyle, setFontStyle] = useState<FontStyleName>('normal');
- const [fontSize, setFontSize] = useState<FontSizeName>('md');
- const [lineHeight, setLineHeight] = useState<LineHeightName>('md');
+ const [bold, setBold] = useState(false);
+ const [italic, setItalic] = useState(false);
+ const [fontSize, setFontSize] = useState(DEFAULT_FONT_SIZE);
+ const [lineHeight, setLineHeight] = useState(DEFAULT_LINE_HEIGHT);
const [past, setPast] = useState<Annotation[][]>([]);
const [future, setFuture] = useState<Annotation[][]>([]);
@@ -401,7 +408,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
const rect = canvasRef.current?.getBoundingClientRect();
setTextEditor({
x: p.x,
- y: p.y + FONT_SIZES[fontSize],
+ y: p.y + fontSize,
left: rect ? e.clientX - rect.left : 0,
top: rect ? e.clientY - rect.top : 0,
});
@@ -565,23 +572,25 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
const setTextProp = (patch: {
fontFamily?: FontFamily;
- fontStyle?: FontStyleName;
- fontSize?: FontSizeName;
- lineHeight?: LineHeightName;
+ bold?: boolean;
+ italic?: boolean;
+ size?: number;
+ lineHeight?: number;
}) => {
if (patch.fontFamily) setFontFamily(patch.fontFamily);
- if (patch.fontStyle) setFontStyle(patch.fontStyle);
- if (patch.fontSize) setFontSize(patch.fontSize);
- if (patch.lineHeight) setLineHeight(patch.lineHeight);
+ if (patch.bold !== undefined) setBold(patch.bold);
+ if (patch.italic !== undefined) setItalic(patch.italic);
+ if (patch.size !== undefined) setFontSize(patch.size);
+ if (patch.lineHeight !== undefined) setLineHeight(patch.lineHeight);
if (!selected || selected.type !== 'text') return;
const next = {
fontFamily: patch.fontFamily ?? selected.fontFamily ?? 'sans-serif',
- fontStyle: patch.fontStyle ?? selected.fontStyle ?? 'normal',
- fontSize: patch.fontSize ?? selected.fontSize ?? 'md',
- lineHeight: patch.lineHeight ?? selected.lineHeight ?? 'md',
+ bold: patch.bold ?? selected.bold ?? false,
+ italic: patch.italic ?? selected.italic ?? false,
+ size: patch.size ?? selected.size,
+ lineHeight: patch.lineHeight ?? selected.lineHeight ?? DEFAULT_LINE_HEIGHT,
};
- const px = FONT_SIZES[next.fontSize];
- patchSelected({ ...next, size: px, ...measureText(selected.text, px, next.fontFamily, next.fontStyle) });
+ patchSelected({ ...next, ...measureText(selected.text, next.size, next.fontFamily, next.bold, next.italic) });
};
const handleColorSelect = (c: string) => {
@@ -589,13 +598,11 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
setAnnotations((prev) => prev.map((a) => (a.id === selectedId && a.type !== 'redact' ? { ...a, color: c } : a)));
};
- const measureText = (value: string, px: number, family: FontFamily, style: FontStyleName) => {
+ const measureText = (value: string, px: number, family: FontFamily, isBold: boolean, isItalic: boolean) => {
const ctx = canvasRef.current?.getContext('2d');
if (!ctx) return { w: value.length * px * 0.6, h: px * 1.4 };
ctx.save();
- const italic = style === 'italic' ? 'italic ' : '';
- const weight = style === 'bold' ? 700 : 500;
- ctx.font = `${italic}${weight} ${px}px ${FONT_FAMILIES[family]}`;
+ ctx.font = `${isItalic ? 'italic ' : ''}${isBold ? 700 : 500} ${px}px ${FONT_FAMILIES[family]}`;
const w = ctx.measureText(value).width;
ctx.restore();
return { w, h: px * 1.4 };
@@ -614,11 +621,11 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
y: textEditor.y,
text: textValue.trim(),
color,
- size: FONT_SIZES[fontSize],
- ...measureText(textValue.trim(), FONT_SIZES[fontSize], fontFamily, fontStyle),
- fontSize,
+ size: fontSize,
+ ...measureText(textValue.trim(), fontSize, fontFamily, bold, italic),
fontFamily,
- fontStyle,
+ bold,
+ italic,
lineHeight,
},
]);
@@ -773,47 +780,68 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
<select
value={selected.fontFamily ?? 'sans-serif'}
onChange={(e) => setTextProp({ fontFamily: e.target.value as FontFamily })}
- className="h-6 rounded-md bg-primary-foreground/10 px-1 text-[10px] text-primary-foreground outline-none"
+ title={i18n.t('annotationEditor.font')}
+ className="h-6 rounded-md bg-primary-foreground/10 px-1.5 text-[11px] text-primary-foreground outline-none"
+ style={{ fontFamily: FONT_FAMILIES[selected.fontFamily ?? 'sans-serif'] }}
>
{FONT_FAMILY_ORDER.map((f) => (
- <option key={f} value={f}>
- {f}
+ <option key={f} value={f} style={{ fontFamily: FONT_FAMILIES[f] }}>
+ Ag — {f}
</option>
))}
</select>
- <select
- value={selected.fontStyle ?? 'normal'}
- onChange={(e) => setTextProp({ fontStyle: e.target.value as FontStyleName })}
- className="h-6 rounded-md bg-primary-foreground/10 px-1 text-[10px] text-primary-foreground outline-none"
+ <div className="flex items-center gap-0.5 rounded-md bg-primary-foreground/10 p-0.5">
+ <button
+ type="button"
+ title={i18n.t('annotationEditor.bold')}
+ onClick={() => setTextProp({ bold: !selected.bold })}
+ className={`w-5 h-5 rounded text-[12px] font-extrabold ${selected.bold ? 'bg-accent text-primary-foreground' : 'text-primary-foreground hover:bg-primary-foreground/15'}`}
+ >
+ B
+ </button>
+ <button
+ type="button"
+ title={i18n.t('annotationEditor.italic')}
+ onClick={() => setTextProp({ italic: !selected.italic })}
+ className={`w-5 h-5 rounded text-[12px] italic font-serif ${selected.italic ? 'bg-accent text-primary-foreground' : 'text-primary-foreground hover:bg-primary-foreground/15'}`}
+ >
+ I
+ </button>
+ </div>
+ <div
+ className="flex items-center rounded-md bg-primary-foreground/10 h-6"
+ title={i18n.t('annotationEditor.fontSize')}
>
- {FONT_STYLE_ORDER.map((f) => (
- <option key={f} value={f}>
- {i18n.t(`annotationEditor.style_${f}`)}
- </option>
- ))}
- </select>
- <select
- value={selected.fontSize ?? 'md'}
- onChange={(e) => setTextProp({ fontSize: e.target.value as FontSizeName })}
- className="h-6 rounded-md bg-primary-foreground/10 px-1 text-[10px] text-primary-foreground outline-none"
+ <span className="pl-1.5 text-[9px] text-primary-foreground/60">A</span>
+ <input
+ type="number"
+ min={MIN_FONT_SIZE}
+ max={MAX_FONT_SIZE}
+ value={Math.round(selected.size)}
+ onChange={(e) =>
+ setTextProp({ size: clampNumber(e.target.value, MIN_FONT_SIZE, MAX_FONT_SIZE) })
+ }
+ className="w-9 bg-transparent text-center text-[11px] tabular-nums text-primary-foreground outline-none"
+ />
+ <span className="pr-1.5 text-[13px] text-primary-foreground/60">A</span>
+ </div>
+ <div
+ className="flex items-center gap-1 rounded-md bg-primary-foreground/10 h-6 px-1.5"
+ title={i18n.t('annotationEditor.lineHeight')}
>
- {FONT_SIZE_ORDER.map((f) => (
- <option key={f} value={f}>
- {i18n.t(`annotationEditor.size_${f}`)}
- </option>
- ))}
- </select>
- <select
- value={selected.lineHeight ?? 'md'}
- onChange={(e) => setTextProp({ lineHeight: e.target.value as LineHeightName })}
- className="h-6 rounded-md bg-primary-foreground/10 px-1 text-[10px] text-primary-foreground outline-none"
- >
- {LINE_HEIGHT_ORDER.map((f) => (
- <option key={f} value={f}>
- {i18n.t(`annotationEditor.height_${f}`)}
- </option>
- ))}
- </select>
+ <MoveVertical size={11} className="text-primary-foreground/60" />
+ <input
+ type="number"
+ step={0.1}
+ min={MIN_LINE_HEIGHT}
+ max={MAX_LINE_HEIGHT}
+ value={selected.lineHeight ?? DEFAULT_LINE_HEIGHT}
+ onChange={(e) =>
+ setTextProp({ lineHeight: clampNumber(e.target.value, MIN_LINE_HEIGHT, MAX_LINE_HEIGHT) })
+ }
+ className="w-8 bg-transparent text-center text-[11px] tabular-nums text-primary-foreground outline-none"
+ />
+ </div>
</>
)}
<button