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: move highlight rendering from capture to UI display layer

Luis Ortiz92b16a31a43a
diff --git a/src/core/capture/events/handlers.ts b/src/core/capture/events/handlers.ts
index 19af28b..89b1039 100644
--- a/src/core/capture/events/handlers.ts
+++ b/src/core/capture/events/handlers.ts
@@ -2,14 +2,7 @@ import PQueue from 'p-queue';
import { sendMessage } from '@/lib/messaging';
import { extractDOMContext } from '../dom/context';
import { extractElementMeta } from '../dom/element-meta';
-import {
- findFocusableAncestor,
- isMimikElement,
- isNavigatingClick,
- isTextField,
- isTooLarge,
-} from '../dom/element-utils';
-import { HighlightManager } from './highlight';
+import { findFocusableAncestor, isMimikElement, isNavigatingClick, isTextField } from '../dom/element-utils';
import { InputSession } from './input-session';
const DEDUP_MS = 300;
@@ -21,77 +14,53 @@ let lastClickTime = 0;
export interface CaptureHandle {
stop: () => void;
- hideOverlay: () => void;
- showOverlay: () => void;
}
+const PASSIVE_CAPTURE = { capture: true, passive: true } as const;
+const ACTIVE_CAPTURE = { capture: true } as const;
+
class CaptureController {
- private hl = new HighlightManager();
private input: InputSession;
private queue = new PQueue({ concurrency: 1 });
- private listeners: [string, EventListener][] = [];
+ private listeners: [string, EventListener, AddEventListenerOptions][] = [];
private dragStartX: number | null = null;
private dragStartY: number | null = null;
private dragStartElement: Element | null = null;
- constructor(private guideId: string) {
- this.input = new InputSession(guideId, this.hl);
+ constructor(
+ private guideId: string,
+ isTopFrame: boolean,
+ ) {
+ this.input = new InputSession(guideId);
this.listeners = [
- ['click', this.onClick.bind(this)],
- ['auxclick', this.onAuxClick.bind(this)],
- ['keydown', this.onKeydown.bind(this)],
- ['input', this.onInput.bind(this)],
- ['copy', this.onClipboard.bind(this)],
- ['paste', this.onClipboard.bind(this)],
- ['cut', this.onClipboard.bind(this)],
- ['pointerdown', this.onPointerDown.bind(this)],
- ['pointerup', this.onPointerUp.bind(this)],
- ['dragend', this.onDragEnd.bind(this)],
- ['mouseover', this.onMouseOver.bind(this)],
- ['mouseout', this.onMouseOut.bind(this)],
- ['focusin', this.onFocusIn.bind(this)],
- ['focusout', this.onFocusOut.bind(this)],
+ ['click', this.onClick.bind(this), ACTIVE_CAPTURE],
+ ['auxclick', this.onAuxClick.bind(this), ACTIVE_CAPTURE],
+ ['keydown', this.onKeydown.bind(this), ACTIVE_CAPTURE],
+ ['input', this.onInput.bind(this), PASSIVE_CAPTURE],
+ ['focusout', this.onFocusOut.bind(this), PASSIVE_CAPTURE],
];
- for (const [event, handler] of this.listeners) {
- window.addEventListener(event, handler, { capture: true });
+ if (isTopFrame) {
+ this.listeners.push(
+ ['copy', this.onClipboard.bind(this), PASSIVE_CAPTURE],
+ ['paste', this.onClipboard.bind(this), PASSIVE_CAPTURE],
+ ['cut', this.onClipboard.bind(this), PASSIVE_CAPTURE],
+ ['pointerdown', this.onPointerDown.bind(this), PASSIVE_CAPTURE],
+ ['pointerup', this.onPointerUp.bind(this), PASSIVE_CAPTURE],
+ ['dragend', this.onDragEnd.bind(this), PASSIVE_CAPTURE],
+ );
}
- }
-
- private async captureAction(action: string, target: HTMLElement) {
- await this.hl.hideInstant();
- try {
- await sendMessage('captureStep', {
- guideId: this.guideId,
- action,
- elementMeta: extractElementMeta(target),
- domContext: extractDOMContext(target, action),
- });
- } finally {
- this.hl.showInstant();
+ for (const [event, handler, opts] of this.listeners) {
+ window.addEventListener(event, handler, opts);
}
}
- private resolveTarget(raw: EventTarget | null): HTMLElement | null {
- if (!raw || !(raw instanceof Element) || isMimikElement(raw)) return null;
- const target = findFocusableAncestor(raw);
- if (target === document.body || target === document.documentElement || isTooLarge(target)) return null;
- return target;
- }
-
- private onMouseOver(e: Event) {
- const target = this.resolveTarget((e as MouseEvent).target);
- if (target) this.hl.scheduleShow(target);
- }
-
- private onMouseOut(e: Event) {
- const related = (e as MouseEvent).relatedTarget;
- if (related instanceof Element && this.hl.hoveredElement?.contains(related)) return;
- this.hl.scheduleHide();
- }
-
- private onFocusIn(e: Event) {
- const target = this.resolveTarget((e as FocusEvent).target);
- if (target) this.hl.scheduleShow(target);
+ private async captureAction(action: string, target: HTMLElement) {
+ await sendMessage('captureStep', {
+ guideId: this.guideId,
+ action,
+ elementMeta: extractElementMeta(target),
+ domContext: extractDOMContext(target, action),
+ });
}
private onClick(e: Event) {
@@ -233,26 +202,16 @@ class CaptureController {
}
stop() {
- for (const [event, handler] of this.listeners) {
- window.removeEventListener(event, handler, { capture: true });
+ for (const [event, handler, opts] of this.listeners) {
+ window.removeEventListener(event, handler, opts);
}
this.queue.add(() => this.input.finalize());
- this.hl.dispose();
- }
-
- hideOverlay() {
- this.hl.hideInstant();
- }
- showOverlay() {
- this.hl.showInstant();
}
}
-export function startCapture(guideId: string): CaptureHandle {
- const controller = new CaptureController(guideId);
+export function startCapture(guideId: string, isTopFrame = true): CaptureHandle {
+ const controller = new CaptureController(guideId, isTopFrame);
return {
stop: () => controller.stop(),
- hideOverlay: () => controller.hideOverlay(),
- showOverlay: () => controller.showOverlay(),
};
}
diff --git a/src/core/capture/events/highlight.ts b/src/core/capture/events/highlight.ts
deleted file mode 100644
index 19e9795..0000000
--- a/src/core/capture/events/highlight.ts
+++ /dev/null
@@ -1,106 +0,0 @@
-export interface Highlight {
- host: HTMLElement;
- overlay: HTMLDivElement;
-}
-
-export function createHighlight(): Highlight {
- const host = document.createElement('mimik-highlight');
- host.setAttribute('data-mimik-ignore', '');
- host.style.cssText = 'position:fixed;top:0;left:0;z-index:2147483647;pointer-events:none;';
- const shadow = host.attachShadow({ mode: 'closed' });
- const overlay = document.createElement('div');
- overlay.style.cssText = `
- position: fixed; pointer-events: none;
- outline: 2.5px dashed #F59E0B; outline-offset: 3px;
- border-radius: 4px; box-sizing: border-box;
- opacity: 0; transition: opacity 0.15s ease;
- `;
- shadow.appendChild(overlay);
- document.documentElement.appendChild(host);
- return { host, overlay };
-}
-
-const MIN_RECT_PX = 5;
-const STICKY_MS = 350;
-
-function resolveRect(target: Element): DOMRect {
- const rect = target.getBoundingClientRect();
- if (rect.width >= MIN_RECT_PX && rect.height >= MIN_RECT_PX) return rect;
- const parent = target.parentElement;
- if (parent && parent !== document.body) {
- const parentRect = parent.getBoundingClientRect();
- if (parentRect.width >= MIN_RECT_PX && parentRect.height >= MIN_RECT_PX) return parentRect;
- }
- return rect;
-}
-
-function waitForRepaint(): Promise<void> {
- return new Promise((resolve) => {
- requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
- });
-}
-
-export class HighlightManager {
- private highlight: Highlight | null = null;
- private hideTimer: ReturnType<typeof setTimeout> | null = null;
- private rafId: number | null = null;
- private stickyUntil = 0;
- hoveredElement: Element | null = null;
-
- show(target: Element) {
- if (!this.highlight) this.highlight = createHighlight();
- if (this.hideTimer) {
- clearTimeout(this.hideTimer);
- this.hideTimer = null;
- }
- const rect = resolveRect(target);
- this.highlight.overlay.style.top = `${rect.top}px`;
- this.highlight.overlay.style.left = `${rect.left}px`;
- this.highlight.overlay.style.width = `${rect.width}px`;
- this.highlight.overlay.style.height = `${rect.height}px`;
- this.highlight.overlay.style.opacity = '1';
- this.stickyUntil = Date.now() + STICKY_MS;
- }
-
- hide() {
- if (this.highlight) this.highlight.overlay.style.opacity = '0';
- }
-
- async hideInstant(): Promise<void> {
- if (this.highlight) {
- this.highlight.host.style.display = 'none';
- await waitForRepaint();
- }
- }
-
- showInstant() {
- if (this.highlight) this.highlight.host.style.display = '';
- }
-
- scheduleShow(target: Element) {
- if (target === this.hoveredElement) return;
- if (this.hoveredElement && Date.now() < this.stickyUntil && this.hoveredElement.contains(target)) {
- return;
- }
- this.hoveredElement = target;
- if (this.rafId) cancelAnimationFrame(this.rafId);
- this.rafId = requestAnimationFrame(() => this.show(target));
- }
-
- scheduleHide() {
- this.hoveredElement = null;
- if (this.hideTimer) clearTimeout(this.hideTimer);
- this.hideTimer = setTimeout(() => this.hide(), 80);
- }
-
- dispose() {
- if (this.hideTimer) clearTimeout(this.hideTimer);
- if (this.rafId) cancelAnimationFrame(this.rafId);
- if (this.highlight) {
- this.highlight.host.remove();
- this.highlight = null;
- }
- this.hoveredElement = null;
- this.stickyUntil = 0;
- }
-}
diff --git a/src/core/capture/events/input-session.ts b/src/core/capture/events/input-session.ts
index 453c2a0..a6a942b 100644
--- a/src/core/capture/events/input-session.ts
+++ b/src/core/capture/events/input-session.ts
@@ -3,18 +3,15 @@ import { sendMessage } from '@/lib/messaging';
import { extractDOMContext } from '../dom/context';
import { extractElementMeta } from '../dom/element-meta';
import { getFieldLabel, getFieldValue } from '../dom/element-utils';
-import type { HighlightManager } from './highlight';
export class InputSession {
stepId: string | null = null;
target: HTMLElement | null = null;
private guideId: string;
- private hl: HighlightManager;
- constructor(guideId: string, hl: HighlightManager) {
+ constructor(guideId: string) {
this.guideId = guideId;
- this.hl = hl;
}
get active() {
@@ -22,20 +19,15 @@ export class InputSession {
}
async start(target: HTMLElement) {
- await this.hl.hideInstant();
- try {
- const res = await sendMessage('captureStep', {
- guideId: this.guideId,
- action: 'input',
- elementMeta: extractElementMeta(target),
- domContext: extractDOMContext(target, 'input'),
- });
- if ('stepId' in res) {
- this.stepId = res.stepId;
- this.target = target;
- }
- } finally {
- this.hl.showInstant();
+ const res = await sendMessage('captureStep', {
+ guideId: this.guideId,
+ action: 'input',
+ elementMeta: extractElementMeta(target),
+ domContext: extractDOMContext(target, 'input'),
+ });
+ if ('stepId' in res) {
+ this.stepId = res.stepId;
+ this.target = target;
}
}
@@ -54,15 +46,10 @@ export class InputSession {
const stepId = this.stepId;
this.stepId = null;
this.target = null;
- await this.hl.hideInstant();
- try {
- await sendMessage('finalizeInputStep', {
- stepId,
- elementMeta: extractElementMeta(target),
- domContext: extractDOMContext(target, 'input'),
- });
- } finally {
- this.hl.showInstant();
- }
+ await sendMessage('finalizeInputStep', {
+ stepId,
+ elementMeta: extractElementMeta(target),
+ domContext: extractDOMContext(target, 'input'),
+ });
}
}
diff --git a/src/core/capture/rrweb-recorder.ts b/src/core/capture/rrweb-recorder.ts
index 39dd156..43b994e 100644
--- a/src/core/capture/rrweb-recorder.ts
+++ b/src/core/capture/rrweb-recorder.ts
@@ -1,4 +1,3 @@
-// @ts-expect-error rrweb 1.1.3 type resolution
import { record } from 'rrweb';
import { sendMessage } from '@/lib/messaging';
@@ -25,6 +24,13 @@ export function startRrwebRecording(guideId: string): () => void {
}
},
checkoutEveryNms: 30_000,
+ recordCanvas: false,
+ sampling: {
+ mousemove: 200,
+ mouseInteraction: true,
+ scroll: 200,
+ input: 'last',
+ },
});
flushTimer = setInterval(flushChunk, 30_000);
diff --git a/src/core/capture/session.ts b/src/core/capture/session.ts
index 6f8dc26..d1fa2f5 100644
--- a/src/core/capture/session.ts
+++ b/src/core/capture/session.ts
@@ -28,8 +28,11 @@ export class CaptureSession {
logger.info('Capture started β†’ guideId:', guideId);
this.activeGuideId = guideId;
- this.capture = startCapture(guideId);
- this.stopReplay = startRrwebRecording(guideId);
+ const isTopFrame = window.self === window.top;
+ this.capture = startCapture(guideId, isTopFrame);
+ if (isTopFrame) {
+ this.stopReplay = startRrwebRecording(guideId);
+ }
}
stop(): void {
@@ -43,16 +46,6 @@ export class CaptureSession {
this.activeGuideId = null;
}
- hideOverlay(): void {
- logger.debug('CaptureSession.hideOverlay, capture exists:', !!this.capture);
- this.capture?.hideOverlay();
- }
-
- showOverlay(): void {
- logger.debug('CaptureSession.showOverlay');
- this.capture?.showOverlay();
- }
-
dispose(): void {
this.stop();
this.disabled = true;
diff --git a/src/entrypoints/content.ts b/src/entrypoints/content.ts
index 126f130..3c29b70 100644
--- a/src/entrypoints/content.ts
+++ b/src/entrypoints/content.ts
@@ -49,18 +49,6 @@ function createTabMessageHandler(session: CaptureSession) {
});
return true;
- case TabMessage.HIDE_OVERLAY:
- logger.debug('Received HIDE_OVERLAY message');
- session.hideOverlay();
- sendResponse({ hidden: true });
- return true;
-
- case TabMessage.SHOW_OVERLAY:
- logger.debug('Received SHOW_OVERLAY message');
- session.showOverlay();
- sendResponse({ shown: true });
- return true;
-
default:
return false;
}