Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.8 KB
Raw Blame
import type {
  NodeTransformType,
  SelectionData,
} from '@core/component/LexicalMarkdown/plugins';
import type { HotkeyToken } from '@core/hotkey/tokens';
import TextCodeIcon from '@icon/system/regular/code.svg';
import ListBulletsIcon from '@icon/system/regular/list-bullets.svg';
import ListChecksIcon from '@icon/system/regular/list-checks.svg';
import ListNumbersIcon from '@icon/system/regular/list-numbers.svg';
import TextQuoteIcon from '@icon/system/regular/quotes.svg';
import TextBoldIcon from '@icon/system/regular/text-b.svg';
import TextItalicIcon from '@icon/system/regular/text-italic.svg';
import TextStrikethroughIcon from '@icon/system/regular/text-strikethrough.svg';
import { Button } from '@ui';
import type { TextFormatType } from 'lexical';
import type { Accessor, JSX } from 'solid-js';

type FormatButtonsProps = {
  selectionState: Accessor<SelectionData | undefined>;
  onInlineFormat: (format: TextFormatType) => void;
  onNodeFormat: (format: NodeTransformType) => void;
};

export function FormatButtons(props: FormatButtonsProps) {
  const selection = () => props.selectionState();
  const elementsInRange = () => selection()?.elementsInRange;

  const toggleNodeFormat = (format: NodeTransformType) => {
    const isActive = elementsInRange()?.has(format);
    props.onNodeFormat(isActive ? 'paragraph' : format);
  };

  return (
    <>
      <FormatButton
        label="Bold"
        active={selection()?.bold}
        onClick={() => props.onInlineFormat('bold')}
      >
        <TextBoldIcon class="size-4" />
      </FormatButton>
      <FormatButton
        label="Italic"
        active={selection()?.italic}
        onClick={() => props.onInlineFormat('italic')}
      >
        <TextItalicIcon class="size-4" />
      </FormatButton>
      <FormatButton
        label="Strikethrough"
        active={selection()?.strikethrough}
        onClick={() => props.onInlineFormat('strikethrough')}
      >
        <TextStrikethroughIcon class="size-4" />
      </FormatButton>
      <FormatButton
        label="Inline code"
        active={selection()?.code}
        onClick={() => props.onInlineFormat('code')}
      >
        <TextCodeIcon class="size-4" />
      </FormatButton>
      <div class="w-px h-5 shrink-0 bg-edge-muted" />
      <FormatButton
        label="Bulleted list"
        active={elementsInRange()?.has('list-bullet')}
        onClick={() => toggleNodeFormat('list-bullet')}
      >
        <ListBulletsIcon class="size-4" />
      </FormatButton>
      <FormatButton
        label="Numbered list"
        active={elementsInRange()?.has('list-number')}
        onClick={() => toggleNodeFormat('list-number')}
      >
        <ListNumbersIcon class="size-4" />
      </FormatButton>
      <FormatButton
        label="Checklist"
        active={elementsInRange()?.has('list-check')}
        onClick={() => toggleNodeFormat('list-check')}
      >
        <ListChecksIcon class="size-4" />
      </FormatButton>
      <FormatButton
        label="Blockquote"
        active={elementsInRange()?.has('quote')}
        onClick={() => toggleNodeFormat('quote')}
      >
        <TextQuoteIcon class="size-4" />
      </FormatButton>
    </>
  );
}

type FormatButtonProps = {
  label: string;
  hotkeyToken?: HotkeyToken;
  active?: boolean;
  onClick: () => void;
  children: JSX.Element;
};

function FormatButton(props: FormatButtonProps) {
  return (
    <Button
      tooltip={{ content: props.label, hotkey: props.hotkeyToken }}
      aria-label={props.label}
      variant="ghost"
      size="icon-sm"
      class="shrink-0"
      aria-pressed={!!props.active}
      onPointerDown={(event: PointerEvent) => event.preventDefault()}
      onMouseDown={(event: MouseEvent) => event.preventDefault()}
      onClick={() => props.onClick()}
    >
      {props.children}
    </Button>
  );
}