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.7 KB
Raw Blame
import type { CommentId, ThreadId } from '@core/comments/commentType';
import type { MarkdownEditorErrors } from '@core/component/LexicalMarkdown/constants';
import type {
  Completion,
  GenerateMenuOpen,
  NodekeyOffset,
  PluginManager,
  ProgressStats,
  SelectionData,
  WordcountStats,
} from '@core/component/LexicalMarkdown/plugins';
import type { FloatingStyle } from '@core/component/LexicalMarkdown/plugins/find-and-replace';
import { createParamsState } from '@core/component/ParamsProvider';
import type { NodeIdMappings } from '@macro-inc/lexical-core/plugins/nodeIdPlugin';
import type { LexicalEditor } from 'lexical';
import { createSignal } from 'solid-js';
import { createStore, type Store } from 'solid-js/store';
import type {
  CommentStore,
  MarkStore,
  ThreadStore,
} from '../comments/commentType';

type MdData = {
  editor?: LexicalEditor;
  /** Durable nodeId <-> nodeKey mapping for the main editor (from the nodeId plugin). */
  mapping?: NodeIdMappings;
  titleEditor?: LexicalEditor;
  plugins?: PluginManager;
  selection?: Store<SelectionData>;
  wordcountStats?: Store<WordcountStats>;
  progressStats?: Store<ProgressStats>;
  notebook?: HTMLElement;
  scrollContainer?: HTMLElement;
  commentMargin?: HTMLElement;
  contentRef?: HTMLElement;
  locationReady?: boolean;
};

type FindAndReplaceState = {
  searchIsOpen: boolean;
  isSearching: boolean;
  searchInputText: string;
  replaceInputOpen: boolean;
  replaceInputText: string;
  listOffset: NodekeyOffset[];
  styles: { style: FloatingStyle; idx: number | undefined }[];
  matches: number;
  currentMatch: number;
  currentQuery: string;
};

const initialFindAndReplaceState: FindAndReplaceState = {
  searchIsOpen: false,
  isSearching: false,
  searchInputText: '',
  replaceInputOpen: false,
  replaceInputText: '',
  listOffset: [],
  styles: [],
  matches: 0,
  currentMatch: -1,
  currentQuery: '',
};

type MarkdownCommentsState = {
  marks: MarkStore;
  activeMarkIds: string[];
  activeCommentThread: ThreadId | null;
  highlightedCommentId: CommentId | null;
  comments: CommentStore;
  threads: ThreadStore;
  commentMarksInitialized: boolean;
  highlightedCommentThreads: ThreadId[];
};

export function createMarkdownDocumentState() {
  const params = createParamsState();
  const [md, setMd] = createStore<MdData>({});
  const [error, setError] = createSignal<MarkdownEditorErrors | null>(null);
  const [findAndReplace, setFindAndReplace] = createStore<FindAndReplaceState>(
    structuredClone(initialFindAndReplaceState)
  );

  const [isGenerating, setIsGenerating] = createSignal(false);
  const [generatedAndWaiting, setGeneratedAndWaiting] = createSignal(false);
  const [completion, setCompletion] = createSignal<Completion>();
  const [generateMenuOpen, setGenerateMenuOpen]: GenerateMenuOpen =
    createSignal<boolean>();
  const [generateContext, setGenerateContext] = createSignal<string>();

  const [comments, setCommentState] = createStore<MarkdownCommentsState>({
    marks: {},
    activeMarkIds: [],
    activeCommentThread: null,
    highlightedCommentId: null,
    comments: {},
    threads: {},
    commentMarksInitialized: false,
    highlightedCommentThreads: [],
  });

  return {
    params,
    editor: {
      md,
      setMd,
      error,
      setError,
      findAndReplace,
      setFindAndReplace,
    },
    generation: {
      isGenerating,
      setIsGenerating,
      generatedAndWaiting,
      setGeneratedAndWaiting,
      completion,
      setCompletion,
      generateMenuOpen,
      setGenerateMenuOpen,
      generateContext,
      setGenerateContext,
    },
    comments,
    setCommentState,
  };
}

export type MarkdownDocumentState = ReturnType<
  typeof createMarkdownDocumentState
>;