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 πŸ“–

refactor(editor): float the rewrite panel and make Ask AI always available

Luis Ortizfb7986eda431
diff --git a/src/ui/fullview/GuideContent.tsx b/src/ui/fullview/GuideContent.tsx
index 29f37c8..66008fd 100644
--- a/src/ui/fullview/GuideContent.tsx
+++ b/src/ui/fullview/GuideContent.tsx
@@ -375,32 +375,30 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
</div>
) : editing ? (
<div className="flex items-start gap-2 max-w-[720px]">
- <div className="flex-1 min-w-0">
- <textarea
- ref={(el) => {
- if (el) {
- el.style.height = '0';
- el.style.height = `${el.scrollHeight}px`;
- }
- }}
- value={description}
- rows={2}
- onChange={(e) => {
- setDescription(e.target.value);
- const el = e.target;
+ <textarea
+ ref={(el) => {
+ if (el) {
el.style.height = '0';
el.style.height = `${el.scrollHeight}px`;
- }}
- onFocus={() => {
- editingDescriptionRef.current = true;
- }}
- onSelect={askAi.onSelect}
- onBlur={handleGuideDescriptionBlur}
- placeholder={i18n.t('editor.descriptionPlaceholder')}
- className="w-full resize-none overflow-hidden bg-transparent p-0 text-[15px] leading-relaxed text-muted-foreground placeholder:text-muted-foreground/60 border-b-2 border-transparent hover:border-border focus:outline-none focus:border-accent"
- />
- {askAi.panel}
- </div>
+ }
+ }}
+ value={description}
+ rows={2}
+ onChange={(e) => {
+ setDescription(e.target.value);
+ const el = e.target;
+ el.style.height = '0';
+ el.style.height = `${el.scrollHeight}px`;
+ }}
+ onFocus={() => {
+ editingDescriptionRef.current = true;
+ }}
+ onSelect={askAi.onSelect}
+ onBlur={handleGuideDescriptionBlur}
+ placeholder={i18n.t('editor.descriptionPlaceholder')}
+ className="flex-1 resize-none overflow-hidden bg-transparent p-0 text-[15px] leading-relaxed text-muted-foreground placeholder:text-muted-foreground/60 border-b-2 border-transparent hover:border-border focus:outline-none focus:border-accent"
+ />
+ {hasApiKey && <span className="shrink-0 mt-0.5">{askAi.trigger}</span>}
{hasApiKey && (
<button
type="button"
diff --git a/src/ui/shared/AskAi.tsx b/src/ui/shared/AskAi.tsx
index 8112a91..10d37f1 100644
--- a/src/ui/shared/AskAi.tsx
+++ b/src/ui/shared/AskAi.tsx
@@ -1,5 +1,6 @@
-import { Loader2, Sparkles } from 'lucide-react';
+import { Loader2, Sparkles, Wand2 } from 'lucide-react';
import { useCallback, useEffect, useRef, useState } from 'react';
+import { createPortal } from 'react-dom';
import { i18n } from '#imports';
import { REWRITE_PRESETS, type RewritePreset } from '@/core/capture/ai/prompts';
import { sendMessage } from '@/lib/messaging';
@@ -16,6 +17,10 @@ const PRESET_LABEL_KEYS = {
const PRESET_ORDER = Object.keys(PRESET_LABEL_KEYS) as RewritePreset[];
+const PANEL_WIDTH = 320;
+const PANEL_GAP = 6;
+const VIEWPORT_MARGIN = 8;
+
interface Span {
start: number;
end: number;
@@ -35,28 +40,78 @@ function readSpan(value: string, start: number, end: number): Span | null {
export function useAskAi(value: string, onReplace: (next: string) => void, enabled = true) {
const [span, setSpan] = useState<Span | null>(null);
const [active, setActive] = useState<Span | null>(null);
+ const [anchor, setAnchor] = useState<{ top: number; left: number } | null>(null);
const [instruction, setInstruction] = useState('');
const [busy, setBusy] = useState(false);
const [result, setResult] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
+ const triggerRef = useRef<HTMLButtonElement>(null);
+ const panelRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
- useEffect(() => {
- if (active && result === null) inputRef.current?.focus();
- }, [active, result]);
-
const reset = useCallback(() => {
setActive(null);
- setSpan(null);
setInstruction('');
setResult(null);
}, []);
+ const place = useCallback(() => {
+ const rect = triggerRef.current?.getBoundingClientRect();
+ if (!rect) return;
+ const maxLeft = window.innerWidth - PANEL_WIDTH - VIEWPORT_MARGIN;
+ setAnchor({
+ top: rect.bottom + PANEL_GAP,
+ left: Math.min(Math.max(VIEWPORT_MARGIN, rect.left), Math.max(VIEWPORT_MARGIN, maxLeft)),
+ });
+ }, []);
+
+ useEffect(() => {
+ if (!active) return;
+ place();
+ const reposition = () => place();
+ window.addEventListener('scroll', reposition, true);
+ window.addEventListener('resize', reposition);
+ return () => {
+ window.removeEventListener('scroll', reposition, true);
+ window.removeEventListener('resize', reposition);
+ };
+ }, [active, place]);
+
+ useEffect(() => {
+ if (!active) return;
+ const onPointerDown = (e: MouseEvent) => {
+ const target = e.target as Node;
+ if (panelRef.current?.contains(target) || triggerRef.current?.contains(target)) return;
+ reset();
+ };
+ const onKeyDown = (e: KeyboardEvent) => {
+ if (e.key === 'Escape') reset();
+ };
+ document.addEventListener('mousedown', onPointerDown);
+ document.addEventListener('keydown', onKeyDown);
+ return () => {
+ document.removeEventListener('mousedown', onPointerDown);
+ document.removeEventListener('keydown', onKeyDown);
+ };
+ }, [active, reset]);
+
+ useEffect(() => {
+ if (active && result === null) inputRef.current?.focus();
+ }, [active, result]);
+
const onSelect = useCallback((e: React.SyntheticEvent<HTMLTextAreaElement>) => {
const el = e.currentTarget;
setSpan(readSpan(el.value, el.selectionStart, el.selectionEnd));
}, []);
+ const open = useCallback(() => {
+ const target = span ?? readSpan(value, 0, value.length);
+ if (!target) return;
+ setResult(null);
+ setInstruction('');
+ setActive(target);
+ }, [span, value]);
+
const run = useCallback(
async (target: Span, prompt: string) => {
if (!prompt.trim() || busy) return;
@@ -86,92 +141,104 @@ export function useAskAi(value: string, onReplace: (next: string) => void, enabl
return;
}
onReplace(value.slice(0, active.start) + active.lead + result + active.trail + value.slice(active.end));
+ setSpan(null);
reset();
}, [active, result, value, onReplace, reset]);
- const panel = !enabled ? null : (
- <>
- {span && !active && (
- <button
- type="button"
- onClick={() => setActive(span)}
- className="mt-1 flex items-center gap-1 text-[11px] font-medium text-accent hover:text-deep"
- >
- <Sparkles size={11} />
- {i18n.t('editor.rewriteAskAi')}
- </button>
- )}
-
- {active && (
- <div className="mt-1.5 rounded-lg border border-border bg-card overflow-hidden">
- {result === null ? (
- <>
- <div className="flex items-center gap-1.5 px-2 py-1.5 border-b border-border">
- <Sparkles size={12} className="shrink-0 text-accent" />
- <input
- ref={inputRef}
- value={instruction}
- disabled={busy}
- onChange={(e) => setInstruction(e.target.value)}
- onKeyDown={(e) => {
- if (e.key === 'Enter') {
- e.preventDefault();
- void run(active, instruction);
- }
- if (e.key === 'Escape') reset();
- }}
- placeholder={i18n.t('editor.rewritePlaceholder')}
- className="flex-1 min-w-0 bg-transparent p-0 text-[12px] text-foreground placeholder:text-muted-foreground/60 focus:outline-none disabled:opacity-50"
- />
- {busy ? (
- <Loader2 size={12} className="shrink-0 animate-spin text-accent" />
- ) : (
- <span className="shrink-0 text-[10px] text-muted-foreground">{i18n.t('editor.rewriteEnter')}</span>
- )}
- </div>
- <div className="py-1">
- {PRESET_ORDER.map((preset) => (
+ const panel =
+ active && anchor
+ ? createPortal(
+ <div
+ ref={panelRef}
+ style={{ top: anchor.top, left: anchor.left, width: PANEL_WIDTH }}
+ className="fixed z-[2147483000] rounded-lg border border-border bg-card shadow-lg overflow-hidden"
+ >
+ <p className="px-2.5 pt-2 pb-1.5 text-[11px] leading-snug text-muted-foreground border-b border-border line-clamp-2">
+ {active.core}
+ </p>
+ {result === null ? (
+ <>
+ <div className="flex items-center gap-1.5 px-2 py-1.5 border-b border-border">
+ <Sparkles size={12} className="shrink-0 text-accent" />
+ <input
+ ref={inputRef}
+ value={instruction}
+ disabled={busy}
+ onChange={(e) => setInstruction(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ void run(active, instruction);
+ }
+ }}
+ placeholder={i18n.t('editor.rewritePlaceholder')}
+ className="flex-1 min-w-0 bg-transparent p-0 text-[12px] text-foreground placeholder:text-muted-foreground/60 focus:outline-none disabled:opacity-50"
+ />
+ {busy ? (
+ <Loader2 size={12} className="shrink-0 animate-spin text-accent" />
+ ) : (
+ <span className="shrink-0 text-[10px] text-muted-foreground">{i18n.t('editor.rewriteEnter')}</span>
+ )}
+ </div>
+ <div className="py-1">
+ {PRESET_ORDER.map((preset) => (
+ <button
+ key={preset}
+ type="button"
+ disabled={busy}
+ onClick={() => void run(active, REWRITE_PRESETS[preset])}
+ className="w-full px-2.5 py-1.5 text-left text-[12px] text-foreground hover:bg-secondary disabled:opacity-50"
+ >
+ {i18n.t(PRESET_LABEL_KEYS[preset])}
+ </button>
+ ))}
+ </div>
+ </>
+ ) : (
+ <>
+ <p className="px-2.5 py-2 text-[12px] leading-snug text-foreground whitespace-pre-wrap border-b border-border">
+ {result}
+ </p>
+ <div className="flex items-center gap-1 px-2 py-1.5">
<button
- key={preset}
type="button"
- disabled={busy}
- onClick={() => void run(active, REWRITE_PRESETS[preset])}
- className="w-full px-2.5 py-1 text-left text-[12px] text-foreground hover:bg-secondary disabled:opacity-50"
+ onClick={replace}
+ className="rounded-md bg-accent px-2 py-1 text-[11px] font-medium text-primary-foreground hover:bg-deep"
>
- {i18n.t(PRESET_LABEL_KEYS[preset])}
+ {i18n.t('editor.rewriteReplace')}
</button>
- ))}
- </div>
- </>
- ) : (
- <>
- <p className="px-2.5 py-2 text-[12px] leading-snug text-foreground whitespace-pre-wrap border-b border-border">
- {result}
- </p>
- <div className="flex items-center gap-1 px-2 py-1.5">
- <button
- type="button"
- onClick={replace}
- className="rounded-md bg-accent px-2 py-1 text-[11px] font-medium text-primary-foreground hover:bg-deep"
- >
- {i18n.t('editor.rewriteReplace')}
- </button>
- <button
- type="button"
- onClick={reset}
- className="rounded-md px-2 py-1 text-[11px] font-medium text-muted-foreground hover:bg-secondary"
- >
- {i18n.t('editor.rewriteDiscard')}
- </button>
- </div>
- </>
- )}
- </div>
- )}
+ <button
+ type="button"
+ onClick={reset}
+ className="rounded-md px-2 py-1 text-[11px] font-medium text-muted-foreground hover:bg-secondary"
+ >
+ {i18n.t('editor.rewriteDiscard')}
+ </button>
+ </div>
+ </>
+ )}
+ </div>,
+ document.body,
+ )
+ : null;
+ const trigger = !enabled ? null : (
+ <>
+ <button
+ ref={triggerRef}
+ type="button"
+ onClick={open}
+ disabled={!value.trim()}
+ title={i18n.t('editor.rewriteAskAi')}
+ aria-label={i18n.t('editor.rewriteAskAi')}
+ className={`p-1 rounded-md transition-colors disabled:opacity-40 ${active ? 'text-accent' : 'text-border hover:text-accent'}`}
+ >
+ <Wand2 size={14} />
+ </button>
+ {panel}
<Toast message={error} onDismiss={() => setError(null)} />
</>
);
- return { onSelect, panel };
+ return { onSelect, trigger };
}
diff --git a/src/ui/sidepanel/GuideEditor.tsx b/src/ui/sidepanel/GuideEditor.tsx
index 87835b3..0800999 100644
--- a/src/ui/sidepanel/GuideEditor.tsx
+++ b/src/ui/sidepanel/GuideEditor.tsx
@@ -282,22 +282,24 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
className="w-full resize-none overflow-hidden bg-transparent p-0 text-xs leading-snug text-muted-foreground placeholder:text-muted-foreground/60 border-b border-transparent hover:border-border focus:outline-none focus:border-accent"
/>
{hasApiKey && (
- <button
- type="button"
- onClick={handleGenerateDescription}
- disabled={generating || metaGenerating}
- title={description ? i18n.t('editor.regenerateDescription') : i18n.t('editor.generateDescription')}
- className="mt-1 flex items-center gap-1 text-[11px] font-medium text-accent hover:text-deep disabled:opacity-50 disabled:cursor-not-allowed"
- >
- {generating || metaGenerating ? <Loader2 size={11} className="animate-spin" /> : <Sparkles size={11} />}
- {generating || metaGenerating
- ? i18n.t('editor.generatingDescription')
- : description
- ? i18n.t('editor.regenerateDescription')
- : i18n.t('editor.generateDescription')}
- </button>
+ <div className="mt-1 flex items-center gap-1.5">
+ {askAi.trigger}
+ <button
+ type="button"
+ onClick={handleGenerateDescription}
+ disabled={generating || metaGenerating}
+ title={description ? i18n.t('editor.regenerateDescription') : i18n.t('editor.generateDescription')}
+ className="flex items-center gap-1 text-[11px] font-medium text-accent hover:text-deep disabled:opacity-50 disabled:cursor-not-allowed"
+ >
+ {generating || metaGenerating ? <Loader2 size={11} className="animate-spin" /> : <Sparkles size={11} />}
+ {generating || metaGenerating
+ ? i18n.t('editor.generatingDescription')
+ : description
+ ? i18n.t('editor.regenerateDescription')
+ : i18n.t('editor.generateDescription')}
+ </button>
+ </div>
)}
- {askAi.panel}
<Toast message={descriptionError} onDismiss={() => setDescriptionError(null)} />
</div>
<div className="text-[11px] flex items-center gap-2 text-muted-foreground" style={{ marginLeft: '34px' }}>
diff --git a/src/ui/sidepanel/StepCard.tsx b/src/ui/sidepanel/StepCard.tsx
index 4bd2e72..57de166 100644
--- a/src/ui/sidepanel/StepCard.tsx
+++ b/src/ui/sidepanel/StepCard.tsx
@@ -163,9 +163,9 @@ export default function StepCard({
/>
)}
</div>
- {askAi.panel}
<div className="flex items-center justify-end mt-1">
<div className="flex items-center gap-0.5">
+ {askAi.trigger}
{screenshot && (
<button
onClick={handleCopy}