Skip to content
TheChatArchiveThe Chat Archive
chatgpt-4o-latest-20250326· OpenAIHugging Face LMArena Human Preference 140K

typescript, fix (copy text as monospace to clipboard): const copyTextToClipboard = async…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-r1-0528’s answer.
  1. User

    typescript, fix (copy text as monospace to clipboard): const copyTextToClipboard = async (text: string, type: string, isMonospace: boolean = false) => { try { await navigator.clipboard.writeText(text); notify.success(${type} text copied to clipboard!); } catch (err) { console.error(Failed to copy ${type} text: , err); notify.error(Failed to copy ${type} text to clipboard); } };

  2. Assistant

    To make the clipboard content monospaced, you need to preserve the font formatting. However, the clipboard.writeText() method only lets you copy plain text — it cannot control how the pasted content is displayed (like using monospace). For rich text (e.g., HTML or styled), you must use navigator.clipboard.write() with a ClipboardItem.

    Here’s how you can modify your function to support copying as monospace by writing HTML content when isMonospace is true:

    const copyTextToClipboard = async (
      text: string,
      type: string,
      isMonospace: boolean = false
    ) => {
      try {
        if (!navigator.clipboard) {
          throw new Error("Clipboard API not supported");
        }
    
        if (isMonospace && navigator.clipboard.write) {
          const html = `<pre style="font-family: monospace;">${escapeHtml(text)}</pre>`;
          const blob = new Blob([html], { type: "text/html" });
          const item = new ClipboardItem({ "text/html": blob });
          await navigator.clipboard.write([item]);
        } else {
          await navigator.clipboard.writeText(text);
        }
    
        notify.success(`${type} text copied to clipboard!`);
      } catch (err) {
        console.error(`Failed to copy ${type} text: `, err);
        notify.error(`Failed to copy ${type} text to clipboard`);
      }
    };
    
    // Helper to escape HTML characters
    const escapeHtml = (text: string): string =>
      text.replace(/[&<>"']/g, (char) => {
        const escapeMap: { [key: string]: string } = {
          "&": "&amp;",
          "<": "&lt;",
          ">": "&gt;",
          '"': "&quot;",
          "'": "&#39;",
        };
        return escapeMap[char] || char;
      });
    

    🔍 Explanation:

    • Monospace Trick: A <pre> tag with CSS font-family: monospace will render the copied content with monospace font if pasted into a rich-text editor (e.g., Gmail, Word, etc.).
    • Plain Text Fallback: If isMonospace is false, it just copies regular text.
    • HTML Escaping: Prevents HTML injection when copying code.

    ✅ Note: Rich HTML copying only works in contexts that support the ClipboardItem API (modern browsers) and may not behave as expected when pasted into plain-text editors (e.g., Notepad).

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.