Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

3.0 KB
Raw Blame
/**
 * Precedent #19 (clipboard pipeline is mdast-canonical with per-view hook mechanisms) is what these
 * helpers exercise.
 */

import type { Page } from '@playwright/test';
import { selectAllAndWaitForSelection } from './editor-state';

const SOURCE_CONTENT_SELECTOR = '.source-editor .cm-content';

export async function simulateCopyAndRead(
  page: Page,
  view: 'wysiwyg' | 'source' = 'wysiwyg',
): Promise<{ plain: string; html: string }> {
  const selector =
    view === 'source' ? SOURCE_CONTENT_SELECTOR : '.ProseMirror:not(.composer-prosemirror)';
  await selectAllAndWaitForSelection(page, selector);
  return page.evaluate((sel) => {
    const editor = document.querySelector(sel) as HTMLElement | null;
    if (!editor) {
      const editorCount = document.querySelectorAll(
        '.ProseMirror:not(.composer-prosemirror), .cm-content',
      ).length;
      const rootPreview = (document.body?.outerHTML ?? '').slice(0, 400);
      throw new Error(
        `simulateCopyAndRead: editor "${sel}" not found — editor views on page: ${editorCount}. document.body head:\n${rootPreview}`,
      );
    }
    const captured: Record<string, string> = {};
    const dt = new DataTransfer();
    const origSetData = dt.setData.bind(dt);
    dt.setData = (key: string, value: string): void => {
      captured[key] = value;
      origSetData(key, value);
    };
    const event = new ClipboardEvent('copy', {
      clipboardData: dt,
      bubbles: true,
      cancelable: true,
    });
    editor.dispatchEvent(event);
    return {
      plain: captured['text/plain'] ?? '',
      html: captured['text/html'] ?? '',
    };
  }, selector);
}

export async function simulateCutAndRead(
  page: Page,
  view: 'wysiwyg' | 'source' = 'wysiwyg',
): Promise<{ plain: string; html: string; contentAfter: string }> {
  const selector =
    view === 'source' ? SOURCE_CONTENT_SELECTOR : '.ProseMirror:not(.composer-prosemirror)';
  await selectAllAndWaitForSelection(page, selector);
  return page.evaluate((sel) => {
    const editor = document.querySelector(sel) as HTMLElement | null;
    if (!editor) {
      const editorCount = document.querySelectorAll(
        '.ProseMirror:not(.composer-prosemirror), .cm-content',
      ).length;
      const rootPreview = (document.body?.outerHTML ?? '').slice(0, 400);
      throw new Error(
        `simulateCutAndRead: editor "${sel}" not found — editor views on page: ${editorCount}. document.body head:\n${rootPreview}`,
      );
    }
    const captured: Record<string, string> = {};
    const dt = new DataTransfer();
    const origSetData = dt.setData.bind(dt);
    dt.setData = (key: string, value: string): void => {
      captured[key] = value;
      origSetData(key, value);
    };
    const event = new ClipboardEvent('cut', {
      clipboardData: dt,
      bubbles: true,
      cancelable: true,
    });
    editor.dispatchEvent(event);
    return {
      plain: captured['text/plain'] ?? '',
      html: captured['text/html'] ?? '',
      contentAfter: editor.textContent ?? '',
    };
  }, selector);
}