Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

15.4 KB
Raw Blame
import { useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { ClipboardCopy, TextSelect } from 'lucide-react';
import * as monaco from 'monaco-editor';
import { DiffEditor, Editor, loader, type DiffOnMount, type OnChange, type OnMount } from '@monaco-editor/react';

import {
  getEditorFontSize,
  subscribeEditorFontSize,
} from '../../lib/editorFontSize';

import editorWorker from 'monaco-editor/editor/editor.worker?worker';
import jsonWorker from 'monaco-editor/language/json/json.worker?worker';
import tsWorker from 'monaco-editor/language/typescript/ts.worker?worker';
import cssWorker from 'monaco-editor/language/css/css.worker?worker';
import htmlWorker from 'monaco-editor/language/html/html.worker?worker';

// The VS Code kernel. ALL monaco imports live in THIS module so that
// `React.lazy(() => import('./MonacoEditor'))` keeps monaco-editor + its language
// workers out of the main entry chunk — they load only when an editor first opens
// (verified: monaco is its own content-hashed chunk in `npm run build`).
//
// Self-hosted: Avibe serves the UI from the user's own machine, so the workers are
// emitted as local chunks (no CDN). We register the bundled instance with
// @monaco-editor/react via loader.config() instead of letting it fetch the AMD
// build remotely. Worker set is trimmed to the languages that ship a worker —
// editor service + json + ts/js + css + html; everything else (markdown, python,
// go, rust, yaml, …) uses Monaco's worker-less Monarch tokenizer and still
// highlights without a dedicated worker.
self.MonacoEnvironment = {
  getWorker(_workerId: string, label: string): Worker {
    switch (label) {
      case 'json':
        return new jsonWorker();
      case 'css':
      case 'scss':
      case 'less':
        return new cssWorker();
      case 'html':
      case 'handlebars':
      case 'razor':
        return new htmlWorker();
      case 'typescript':
      case 'javascript':
        return new tsWorker();
      default:
        return new editorWorker();
    }
  },
};

loader.config({ monaco });

// One-time global setup, run on first mount of any editor.
let configured = false;
function setupMonaco(): void {
  if (configured) return;
  configured = true;

  // An avibe-flavoured dark theme: vs-dark, but with the editor surface matched to
  // the window's --surface-2 so Monaco blends into the AppWindow chrome instead of
  // sitting on its own near-black slab.
  monaco.editor.defineTheme('avibe-dark', {
    base: 'vs-dark',
    inherit: true,
    rules: [],
    colors: {
      'editor.background': '#11111c',
      'editorGutter.background': '#11111c',
      'minimap.background': '#11111c',
      'editorLineNumber.foreground': '#4b5163',
      'editorLineNumber.activeForeground': '#9ba3b8',
      'editorWidget.background': '#0e0e18',
      'editor.lineHighlightBackground': '#ffffff0a',
      'editor.selectionBackground': '#3fe0e533',
      'editorIndentGuide.background1': '#ffffff12',
    },
  });

  // This is a whole-machine file editor, not a project IDE: a lone file would
  // otherwise light up with false "cannot find module" / "duplicate identifier"
  // semantic errors. Keep syntax checking, drop semantic validation for ts/js.
  // (0.55 moved the language namespaces to the top level — monaco.typescript.)
  for (const defaults of [monaco.typescript.typescriptDefaults, monaco.typescript.javascriptDefaults]) {
    defaults.setDiagnosticsOptions({ noSemanticValidation: true, noSyntaxValidation: false });
  }
}

// Symbol keys phones can't easily reach (mirrors the Terminal accessory bar).
const SYMBOL_KEYS = ['(', ')', '{', '}', '[', ']', ';', ':', '=', '"', "'", '`', '|', '/', '\\', '-', '_'];

export interface MonacoEditorProps {
  value: string;
  /** Monaco language id (e.g. `typescript`); falls back to plaintext when omitted. */
  language?: string;
  /**
   * Model path/URI for the file. Monaco's TS/JS worker keys JSX/TSX script kind off
   * the model URI's extension, so `.tsx`/`.jsx` files need a path here or they parse as
   * plain TS/JS and show bogus syntax errors. Should be unique per open editor.
   */
  path?: string;
  readOnly?: boolean;
  /** Resolved app theme — drives the dark VS Code theme vs the light one. */
  dark?: boolean;
  onChange?: (value: string) => void;
  /** Save the file — bound to ⌘S / Ctrl+S inside the editor (the IDE has no visible save button). */
  onSave?: () => void;
  /**
   * Live status for an IDE status bar: the 1-based cursor position (`Ln x, Col y`) plus the model's
   * RESOLVED indentation (`insertSpaces` / `tabSize`) after Monaco's `detectIndentation` runs. Fires
   * on mount, on cursor moves, and when the model's indentation is (re)detected.
   */
  onCursorChange?: (line: number, column: number, indent: { insertSpaces: boolean; tabSize: number }) => void;
  /**
   * Jump to + select a match (from cross-file search). `line` is 1-based; `column`/`endColumn`
   * are 0-based offsets within the line (the search backend's convention). `nonce` makes a
   * repeated jump to the same spot re-fire.
   */
  reveal?: { line: number; column: number; endColumn: number; nonce: number } | null;
}

// The model's RESOLVED indentation (post-`detectIndentation`), for the IDE status bar. Reads the
// model's own options rather than the construction defaults, so a tabs file reports tabs and a
// 4-space file reports 4. The `?? 2` guards only the unreachable no-model case (mount / cursor /
// options events always have a model); it mirrors the editor's own 2-space fallback below.
function readIndent(editor: monaco.editor.IStandaloneCodeEditor): { insertSpaces: boolean; tabSize: number } {
  const opts = editor.getModel()?.getOptions();
  return { insertSpaces: opts?.insertSpaces ?? true, tabSize: opts?.tabSize ?? 2 };
}

function applyReveal(editor: monaco.editor.IStandaloneCodeEditor, reveal: { line: number; column: number; endColumn: number }) {
  const line = Math.max(1, reveal.line);
  const startColumn = Math.max(1, reveal.column + 1);
  const endColumn = Math.max(startColumn, reveal.endColumn + 1);
  const range = { startLineNumber: line, startColumn, endLineNumber: line, endColumn };
  editor.revealRangeInCenter(range);
  editor.setSelection(range);
  editor.focus();
}

export default function MonacoEditor({ value, language, path, readOnly, dark = true, onChange, onSave, onCursorChange, reveal }: MonacoEditorProps) {
  const { t } = useTranslation();
  const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
  // Keep the latest callbacks in refs: the ⌘S command + cursor listener are bound
  // once on mount (Monaco can't cleanly rebind a command), so they must read live
  // closures rather than capture the first render's.
  const onSaveRef = useRef(onSave);
  const onCursorRef = useRef(onCursorChange);
  // Hold the latest reveal so handleMount can apply one that arrived before the editor existed
  // (opening a not-yet-open file from a search result mounts the editor with reveal already set).
  const revealRef = useRef(reveal);
  useEffect(() => {
    onSaveRef.current = onSave;
    onCursorRef.current = onCursorChange;
    revealRef.current = reveal;
  });

  useEffect(
    () => subscribeEditorFontSize((fontSize) => editorRef.current?.updateOptions({ fontSize })),
    [],
  );

  const handleMount: OnMount = (editor) => {
    editorRef.current = editor;
    editor.updateOptions({ fontSize: getEditorFontSize() });
    editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => onSaveRef.current?.());
    // Report the initial cursor + detected indentation, then keep both live: the cursor on every
    // move, and indentation whenever Monaco (re)detects it (detectIndentation resolves the model's
    // options at creation, and a manual indentation change fires onDidChangeModelOptions too).
    const pos = editor.getPosition();
    if (pos) onCursorRef.current?.(pos.lineNumber, pos.column, readIndent(editor));
    editor.onDidChangeCursorPosition((e) => onCursorRef.current?.(e.position.lineNumber, e.position.column, readIndent(editor)));
    editor.onDidChangeModelOptions(() => {
      const p = editor.getPosition();
      onCursorRef.current?.(p?.lineNumber ?? 1, p?.column ?? 1, readIndent(editor));
    });
    if (revealRef.current) applyReveal(editor, revealRef.current);
  };

  // Already-open file: a new reveal (its nonce changes) jumps + selects the match in place.
  useEffect(() => {
    if (editorRef.current && reveal) applyReveal(editorRef.current, reveal);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [reveal?.nonce]);
  const handleChange: OnChange = (next) => onChange?.(next ?? '');

  // Accessory actions operate on the live editor instance.
  const insert = (text: string) => {
    const editor = editorRef.current;
    if (!editor || readOnly) return;
    editor.focus();
    const selection = editor.getSelection();
    if (selection) editor.executeEdits('accessory', [{ range: selection, text, forceMoveMarkers: true }]);
  };
  const indent = () => {
    const editor = editorRef.current;
    if (!editor || readOnly) return;
    editor.focus();
    // The `tab` command honours the model's insertSpaces / tabSize settings.
    editor.trigger('accessory', 'tab', null);
  };
  const dismissKeyboard = () => {
    // On phones Esc has no real editor meaning; use it to drop focus and hide the
    // soft keyboard so the user can read the file.
    (document.activeElement as HTMLElement | null)?.blur();
  };
  const selectAll = () => {
    const editor = editorRef.current;
    if (!editor) return;
    editor.focus();
    editor.getAction('editor.action.selectAll')?.run();
  };
  const copyAll = () => {
    const editor = editorRef.current;
    if (!editor) return;
    void navigator.clipboard?.writeText(editor.getValue());
  };

  const accessoryBtn =
    'shrink-0 rounded-md border border-border-strong px-2.5 py-1.5 font-mono text-[12px] text-foreground active:bg-foreground/[0.08]';

  return (
    <div className="flex h-full min-h-0 flex-col">
      <div className="min-h-0 flex-1">
        <Editor
          theme={dark ? 'avibe-dark' : 'light'}
          path={path}
          language={language}
          value={value}
          onChange={handleChange}
          onMount={handleMount}
          beforeMount={setupMonaco}
          loading={
            <div className="grid h-full w-full place-items-center bg-surface-2 text-[12px] text-muted">
              {t('common.loading')}
            </div>
          }
          options={{
            readOnly,
            fontSize: getEditorFontSize(),
            fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
            minimap: { enabled: false },
            scrollBeyondLastLine: false,
            automaticLayout: true,
            // Detect indentation from the file's own content so the status bar reports the model's
            // real resolved insertSpaces/tabSize. `tabSize: 2` is only the FALLBACK for content with
            // no detectable indentation (new/empty buffers) — detectIndentation always overrides it
            // when the file has real indentation, so this keeps the 2-space default without lying.
            detectIndentation: true,
            tabSize: 2,
            renderWhitespace: 'selection',
            smoothScrolling: true,
            cursorBlinking: 'smooth',
            padding: { top: 10, bottom: 10 },
            scrollbar: { useShadows: false },
          }}
        />
      </div>

      {/* Touch accessory bar — Monaco has no touch affordances, so coarse-pointer devices get the
          symbol keys + select/copy helpers. Keyed off pointer type rather than the md: viewport
          breakpoint so tablets keep it and desktops (a hardware keyboard already has these keys)
          don't — the same rule TerminalView's key bar uses (PR #796). */}
      <div className="hidden items-center gap-1 overflow-x-auto border-t border-border bg-surface px-2 py-1.5 pointer-coarse:flex">
        {!readOnly &&
          SYMBOL_KEYS.map((key) => (
            <button key={key} type="button" onClick={() => insert(key)} className={accessoryBtn}>
              {key}
            </button>
          ))}
        {!readOnly && (
          <button type="button" onClick={indent} className={accessoryBtn}>
            {t('apps.terminal.keys.tab')}
          </button>
        )}
        <button type="button" onClick={dismissKeyboard} className={accessoryBtn}>
          {t('apps.terminal.keys.esc')}
        </button>
        <span className="mx-0.5 h-5 w-px shrink-0 bg-border-strong" />
        <button
          type="button"
          onClick={selectAll}
          className="flex shrink-0 items-center gap-1 rounded-md border border-border-strong px-2.5 py-1.5 text-[12px] text-foreground active:bg-foreground/[0.08]"
        >
          <TextSelect className="size-3.5" />
          {t('apps.editor.selectAll')}
        </button>
        <button
          type="button"
          onClick={copyAll}
          className="flex shrink-0 items-center gap-1 rounded-md border border-border-strong px-2.5 py-1.5 text-[12px] text-foreground active:bg-foreground/[0.08]"
        >
          <ClipboardCopy className="size-3.5" />
          {t('apps.editor.copyAll')}
        </button>
      </div>
    </div>
  );
}

export interface MonacoDiffEditorProps {
  /** Left side — the on-disk content (read-only baseline). */
  original: string;
  /** Right side — the local editor buffer. */
  modified: string;
  /** Monaco language id, applied to both sides; falls back to plaintext when omitted. */
  language?: string;
  /** Resolved app theme — drives the dark VS Code theme vs the light one. */
  dark?: boolean;
}

// Read-only side-by-side diff, used by the save-conflict Compare view (disk vs the local buffer).
// Lives in THIS module so it rides the same lazy monaco chunk + `setupMonaco` theme registration as
// the editor — opening it from an already-open file loads no extra chunk. Both sides are read-only:
// this is a comparison, not a merge surface; the conflict is resolved via Reload / Overwrite instead.
export function MonacoDiffEditor({ original, modified, language, dark = true }: MonacoDiffEditorProps) {
  const { t } = useTranslation();
  const editorRef = useRef<monaco.editor.IStandaloneDiffEditor | null>(null);
  useEffect(
    () => subscribeEditorFontSize((fontSize) => editorRef.current?.updateOptions({ fontSize })),
    [],
  );
  const handleMount: DiffOnMount = (editor) => {
    editorRef.current = editor;
    editor.updateOptions({ fontSize: getEditorFontSize() });
  };
  return (
    <DiffEditor
      theme={dark ? 'avibe-dark' : 'light'}
      language={language}
      original={original}
      modified={modified}
      beforeMount={setupMonaco}
      onMount={handleMount}
      loading={
        <div className="grid h-full w-full place-items-center bg-surface-2 text-[12px] text-muted">
          {t('common.loading')}
        </div>
      }
      options={{
        readOnly: true,
        originalEditable: false,
        // Side-by-side per the design; Monaco auto-collapses to inline when the pane is too narrow
        // (its default useInlineViewWhenSpaceIsLimited), so the mobile single-file page stays legible.
        renderSideBySide: true,
        fontSize: getEditorFontSize(),
        fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
        minimap: { enabled: false },
        scrollBeyondLastLine: false,
        automaticLayout: true,
        renderOverviewRuler: false,
        scrollbar: { useShadows: false },
      }}
    />
  );
}