# #138 — Block-Based Slash-Command Composer

> Structured document composer where typing / or tapping + inserts Headings, Callouts, Todos, Code, and Quotes as reorderable blocks.

## Metadata

- **Index**: #138
- **Slug**: `block-slash-composer`
- **Kind**: `ui`
- **Family**: Workspace & Editor
- **Theme**: `light`
- **Mobile Strength**: `native-first`
- **Tags**: `block-editor`, `slash-commands`, `composer`, `rich-text`, `document-workspace`, `structured-content`
- **Best For**: Collaborative docs and team wikis, Project briefs and meeting notes, CMS article and newsletter composers, Interactive runbooks and checklists
- **Real-World Inspiration**: [Slite](https://slite.com/)
- **Detail Page**: https://templates.michaelpitts.net/t/block-slash-composer
- **Live Demo**: https://templates.michaelpitts.net/demos/block-slash-composer.html
- **JSON API**: https://templates.michaelpitts.net/api/templates/block-slash-composer

## Why This Aesthetic Is Unique

Combines keyboard-fast slash-command insertion with tactile block handles, interactive checkboxes, and a touch-accessible '+' block picker bar so mobile authors never have to hunt for '/' on soft keyboards.

## Color Palette

| Role | Name | Hex |
|------|------|-----|
| `canvas` | Warm Linen | `#F7F6F3` |
| `surface` | Document Card | `#FFFFFF` |
| `ink` | Graphite Ink | `#1E1D1B` |
| `accent` | Craft Azure | `#2563EB` |
| `muted` | Warm Slate | `#716E67` |

## Typography

- **Display**: `Inter`
- **Body**: `Inter`
- **Mono**: `JetBrains Mono`
- **Scale Notes**: 28px document title, 20px section heading blocks, 15.5px/1.65 paragraph blocks, 13px mono code blocks.
- **Google Fonts**: https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap
  ```html
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap">
  ```

## Layout Architecture

Centered 760px elevated document sheet with subtle per-block action rails on hover/focus and a floating slash-command popover menu.

## Interaction & Micro-Motion Rules

Type '/' inside any block or click '+ Add Block' to filter and insert Heading, Callout, Todo, Code, Quote, or Divider blocks; move blocks up/down, duplicate, or delete in one click.

## Mobile & Responsive Rules

Keeps an always-reachable bottom/top '+ Insert Block' action bar and explicit Up/Down reorder buttons so touch users never depend on hover-only drag handles.

## Agent Guardrails

### DO
- Always pair keyboard '/' slash triggers with a visible '+ Add Block' button for touch devices.
- Support up/down keyboard arrow navigation inside the slash-command picker.
- Provide explicit Up/Down reorder buttons alongside drag handles for accessibility.

### DON'T
- Never hide block insertion or deletion strictly behind desktop hover states.
- Avoid breaking cursor focus when converting or inserting a new block.

## Complete Standalone Starter Code

File: `demos/block-slash-composer.html`

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CanvasDoc — Block Slash-Command Composer</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
  <style>
    :root {
      --canvas: #f4f1ea;
      --sheet: #ffffff;
      --sheet-subtle: #faf8f4;
      --border: #e6e0d4;
      --border-strong: #d3cbbd;
      --ink: #1e1d1a;
      --ink-muted: #666158;
      --ink-faint: #9a9387;
      --accent: #3b5bdb;
      --accent-soft: #edf2ff;
      --callout-bg: #fff8e6;
      --callout-border: #f59e0b;
      --code-bg: #1e2430;
      --code-ink: #e2e8f0;
      --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
      --font-mono: 'JetBrains Mono', ui-monospace, monospace;
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }

    html, body {
      min-height: 100%;
      background: var(--canvas);
      color: var(--ink);
      font-family: var(--font-sans);
      -webkit-font-smoothing: antialiased;
      overflow-x: hidden;
    }

    .topbar {
      position: sticky;
      top: 0;
      z-index: 30;
      background: rgba(244, 241, 234, 0.92);
      backdrop-filter: blur(10px);
      border-bottom: 1px solid var(--border);
      padding: 10px 20px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 12px;
    }

    .crumb {
      display: flex;
      align-items: center;
      gap: 8px;
      font-size: 13px;
      color: var(--ink-muted);
      min-width: 0;
    }

    .crumb-badge {
      width: 26px;
      height: 26px;
      border-radius: 7px;
      background: var(--ink);
      color: #fff;
      display: grid;
      place-items: center;
      font-size: 13px;
      font-weight: 700;
      flex-shrink: 0;
    }

    .crumb strong {
      color: var(--ink);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .top-actions {
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .todo-pill {
      font-size: 12px;
      font-weight: 600;
      color: var(--accent);
      background: var(--accent-soft);
      padding: 6px 11px;
      border-radius: 99px;
      white-space: nowrap;
    }

    .insert-btn {
      appearance: none;
      border: 1px solid var(--border-strong);
      background: var(--sheet);
      color: var(--ink);
      font-family: var(--font-sans);
      font-size: 13px;
      font-weight: 600;
      padding: 0 13px;
      min-height: 38px;
      border-radius: 9px;
      cursor: pointer;
      display: inline-flex;
      align-items: center;
      gap: 6px;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
      transition: background 0.15s, border-color 0.15s;
    }
    .insert-btn:hover {
      background: var(--sheet-subtle);
      border-color: var(--accent);
      color: var(--accent);
    }

    /* Document Card Sheet */
    .stage {
      max-width: 820px;
      margin: 28px auto 96px;
      padding: 0 16px;
    }

    .doc-sheet {
      background: var(--sheet);
      border: 1px solid var(--border);
      border-radius: 18px;
      padding: 40px 44px 48px;
      box-shadow: 0 10px 34px rgba(30, 29, 26, 0.06);
    }

    .doc-cover-meta {
      display: flex;
      align-items: center;
      gap: 8px;
      font-size: 12px;
      font-weight: 600;
      color: var(--ink-faint);
      text-transform: uppercase;
      letter-spacing: 0.06em;
      margin-bottom: 10px;
    }

    .doc-title {
      width: 100%;
      border: none;
      outline: none;
      background: transparent;
      font-family: var(--font-sans);
      font-size: clamp(24px, 3.6vw, 34px);
      font-weight: 700;
      letter-spacing: -0.025em;
      color: var(--ink);
      margin-bottom: 8px;
    }

    .doc-hint {
      font-size: 13px;
      color: var(--ink-faint);
      margin-bottom: 24px;
      padding-bottom: 18px;
      border-bottom: 1px solid var(--border);
    }

    .doc-hint kbd {
      font-family: var(--font-mono);
      font-size: 11.5px;
      background: var(--sheet-subtle);
      border: 1px solid var(--border-strong);
      border-radius: 4px;
      padding: 1px 5px;
      color: var(--ink);
    }

    /* Blocks */
    .blocks-list {
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .block-row {
      display: grid;
      grid-template-columns: 1fr auto;
      align-items: start;
      gap: 8px;
      padding: 6px 8px;
      margin: 0 -8px;
      border-radius: 11px;
      border: 1px solid transparent;
      transition: background 0.15s, border-color 0.15s;
    }

    .block-row:hover,
    .block-row:focus-within {
      background: var(--sheet-subtle);
      border-color: var(--border);
    }

    .block-content {
      min-width: 0;
    }

    .block-actions {
      display: flex;
      align-items: center;
      gap: 3px;
      opacity: 0.25;
      transition: opacity 0.15s;
      flex-shrink: 0;
    }

    .block-row:hover .block-actions,
    .block-row:focus-within .block-actions {
      opacity: 1;
    }

    .act-btn {
      appearance: none;
      border: 1px solid transparent;
      background: transparent;
      color: var(--ink-muted);
      width: 30px;
      height: 30px;
      border-radius: 6px;
      cursor: pointer;
      display: grid;
      place-items: center;
      font-size: 13px;
    }
    .act-btn:hover {
      background: var(--sheet);
      border-color: var(--border);
      color: var(--ink);
    }
    .act-btn.del:hover { color: #dc2626; }

    /* Editable Text Variants */
    .editable {
      outline: none;
      width: 100%;
      word-break: break-word;
      white-space: pre-wrap;
    }

    .editable:empty::before {
      content: attr(data-placeholder);
      color: var(--ink-faint);
      pointer-events: none;
    }

    .b-paragraph {
      font-size: 15.5px;
      line-height: 1.65;
      color: var(--ink);
      padding: 4px 2px;
    }

    .b-heading {
      font-size: 21px;
      font-weight: 700;
      letter-spacing: -0.015em;
      line-height: 1.3;
      color: var(--ink);
      padding: 8px 2px 2px;
    }

    .b-callout {
      background: var(--callout-bg);
      border: 1px solid rgba(245, 158, 11, 0.35);
      border-left: 4px solid var(--callout-border);
      border-radius: 10px;
      padding: 12px 14px;
      display: flex;
      align-items: flex-start;
      gap: 10px;
      font-size: 14.5px;
      line-height: 1.55;
    }

    .callout-icon {
      font-size: 16px;
      line-height: 1.4;
      flex-shrink: 0;
    }

    .b-todo {
      display: flex;
      align-items: flex-start;
      gap: 10px;
      padding: 4px 2px;
      font-size: 15.5px;
      line-height: 1.55;
    }

    .b-todo input[type="checkbox"] {
      width: 18px;
      height: 18px;
      margin-top: 3px;
      accent-color: var(--accent);
      cursor: pointer;
      flex-shrink: 0;
    }

    .b-todo.checked .editable {
      text-decoration: line-through;
      color: var(--ink-faint);
    }

    .b-quote {
      border-left: 3px solid var(--accent);
      padding: 6px 14px;
      font-style: italic;
      font-size: 16px;
      line-height: 1.6;
      color: var(--ink-muted);
    }

    .b-code {
      background: var(--code-bg);
      color: var(--code-ink);
      border-radius: 10px;
      padding: 13px 15px;
      font-family: var(--font-mono);
      font-size: 13px;
      line-height: 1.6;
      overflow-x: auto;
    }

    .b-divider {
      padding: 12px 0;
    }

    .b-divider hr {
      border: none;
      border-top: 1px solid var(--border-strong);
    }

    .add-tail-btn {
      margin-top: 18px;
      width: 100%;
      appearance: none;
      border: 1px dashed var(--border-strong);
      background: transparent;
      color: var(--ink-muted);
      font-family: var(--font-sans);
      font-size: 13.5px;
      font-weight: 500;
      min-height: 44px;
      border-radius: 10px;
      cursor: pointer;
      transition: background 0.15s, border-color 0.15s, color 0.15s;
    }
    .add-tail-btn:hover {
      background: var(--sheet-subtle);
      border-color: var(--accent);
      color: var(--accent);
    }

    /* Slash Command Palette */
    .slash-menu {
      position: fixed;
      z-index: 90;
      width: 280px;
      max-width: calc(100vw - 24px);
      background: var(--sheet);
      border: 1px solid var(--border-strong);
      border-radius: 13px;
      box-shadow: 0 18px 46px rgba(15, 23, 42, 0.18);
      padding: 8px;
      display: none;
    }

    .slash-menu.open { display: block; }

    .slash-search {
      width: 100%;
      border: 1px solid var(--border);
      background: var(--sheet-subtle);
      border-radius: 8px;
      padding: 8px 10px;
      font-family: var(--font-sans);
      font-size: 13px;
      color: var(--ink);
      outline: none;
      margin-bottom: 6px;
    }
    .slash-search:focus { border-color: var(--accent); }

    .slash-list {
      display: flex;
      flex-direction: column;
      gap: 2px;
      max-height: 280px;
      overflow-y: auto;
    }

    .slash-item {
      appearance: none;
      border: none;
      background: transparent;
      width: 100%;
      min-height: 44px;
      padding: 7px 10px;
      border-radius: 8px;
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 11px;
      text-align: left;
      color: var(--ink);
    }

    .slash-item:hover,
    .slash-item.active {
      background: var(--accent-soft);
    }

    .slash-badge {
      width: 30px;
      height: 30px;
      border-radius: 7px;
      background: var(--sheet-subtle);
      border: 1px solid var(--border);
      display: grid;
      place-items: center;
      font-family: var(--font-mono);
      font-size: 12px;
      font-weight: 600;
      color: var(--ink-muted);
      flex-shrink: 0;
    }

    .slash-item.active .slash-badge {
      background: var(--accent);
      border-color: var(--accent);
      color: #fff;
    }

    .slash-meta strong {
      display: block;
      font-size: 13.5px;
      font-weight: 600;
    }
    .slash-meta span {
      font-size: 11.5px;
      color: var(--ink-muted);
    }

    /* Mobile (390px) */
    @media (max-width: 680px) {
      .topbar { padding: 8px 12px; }
      .insert-btn { min-height: 44px; }

      .stage {
        margin: 14px auto 64px;
        padding: 0 10px;
      }

      .doc-sheet {
        padding: 20px 16px 28px;
        border-radius: 14px;
      }

      .block-row {
        grid-template-columns: 1fr;
        gap: 6px;
        padding: 8px;
        margin: 0;
        border-color: var(--border);
        background: var(--sheet-subtle);
      }

      .block-actions {
        opacity: 1;
        justify-content: flex-end;
        border-top: 1px solid var(--border);
        padding-top: 6px;
      }

      .act-btn {
        width: 44px;
        height: 44px;
        background: var(--sheet);
        border-color: var(--border);
      }

      .slash-menu {
        left: 12px !important;
        right: 12px !important;
        bottom: 14px !important;
        top: auto !important;
        width: auto;
      }
    }
  </style>
</head>
<body>
  <header class="topbar">
    <div class="crumb">
      <div class="crumb-badge" aria-hidden="true">C</div>
      <span>Workspace / <strong>Q3 Sync Brief</strong></span>
    </div>
    <div class="top-actions">
      <span id="todoPill" class="todo-pill">0/0 tasks</span>
      <button id="topInsertBtn" class="insert-btn" type="button">+ Insert Block</button>
    </div>
  </header>

  <main class="stage">
    <article class="doc-sheet">
      <div class="doc-cover-meta">Product Spec · Live Block Editor</div>
      <input id="docTitle" class="doc-title" type="text" value="Q3 Launch Brief — Offline Sync Engine" aria-label="Document title">
      <p class="doc-hint">Type <kbd>/</kbd> inside any block or tap <strong>+ Insert Block</strong> to open the slash-command menu.</p>

      <div id="blocksList" class="blocks-list"></div>

      <button id="tailAddBtn" class="add-tail-btn" type="button">+ Add block or type /</button>
    </article>
  </main>

  <div id="slashMenu" class="slash-menu" role="dialog" aria-label="Insert block menu">
    <input id="slashSearch" class="slash-search" type="text" placeholder="Filter blocks (heading, todo, code…)" aria-label="Filter slash commands">
    <div id="slashList" class="slash-list"></div>
  </div>

  <script>
    const COMMANDS = [
      { type: 'heading', icon: 'H2', label: 'Heading', desc: 'Section heading' },
      { type: 'paragraph', icon: '¶', label: 'Paragraph', desc: 'Plain body text' },
      { type: 'callout', icon: '!', label: 'Callout', desc: 'Highlighted amber note' },
      { type: 'todo', icon: '☑', label: 'Todo', desc: 'Interactive checklist item' },
      { type: 'code', icon: '</>', label: 'Code', desc: 'Monospace snippet block' },
      { type: 'quote', icon: '“', label: 'Quote', desc: 'Indented pull quote' },
      { type: 'divider', icon: '—', label: 'Divider', desc: 'Horizontal section rule' }
    ];

    let nextId = 10;
    const blocks = [
      { id: 1, type: 'paragraph', text: 'Our Q3 release replaces polling with a local-first CRDT log so edits survive airplane mode and merge cleanly upon reconnect.' },
      { id: 2, type: 'callout', text: 'Beta cohort opens July 14 for 250 workspace admins. Keep telemetry payload under 12 KB per sync batch.' },
      { id: 3, type: 'heading', text: 'Launch Readiness Checklist' },
      { id: 4, type: 'todo', checked: true, text: 'Finalize IndexedDB compaction benchmark on 50k blocks' },
      { id: 5, type: 'todo', checked: false, text: 'Verify conflict toast when two devices edit the same heading' },
      { id: 6, type: 'todo', checked: false, text: 'Publish migration guide and rollback flag documentation' },
      { id: 7, type: 'quote', text: 'Local-first software should feel instantaneous on a slow train and invisible when the network returns.' },
      { id: 8, type: 'code', text: 'const batch = await syncEngine.flush({ maxBytes: 12_288, retry: "exponential" });' },
      { id: 9, type: 'divider', text: '' }
    ];

    const blocksList = document.getElementById('blocksList');
    const todoPill = document.getElementById('todoPill');
    const slashMenu = document.getElementById('slashMenu');
    const slashSearch = document.getElementById('slashSearch');
    const slashList = document.getElementById('slashList');

    let menuContext = { afterIndex: blocks.length - 1, replaceIndex: null };
    let filteredCmds = [...COMMANDS];
    let activeCmdIdx = 0;

    function updateTodoCount() {
      const todos = blocks.filter(b => b.type === 'todo');
      const done = todos.filter(b => b.checked).length;
      todoPill.textContent = `${done}/${todos.length} tasks`;
    }

    function renderBlocks(focusId = null) {
      blocksList.innerHTML = '';

      blocks.forEach((block, idx) => {
        const row = document.createElement('div');
        row.className = 'block-row';
        row.dataset.id = block.id;

        const content = document.createElement('div');
        content.className = 'block-content';

        if (block.type === 'divider') {
          content.innerHTML = `<div class="b-divider"><hr></div>`;
        } else if (block.type === 'todo') {
          const wrap = document.createElement('div');
          wrap.className = `b-todo ${block.checked ? 'checked' : ''}`;
          const cb = document.createElement('input');
          cb.type = 'checkbox';
          cb.checked = !!block.checked;
          cb.setAttribute('aria-label', 'Toggle task');
          cb.addEventListener('change', () => {
            block.checked = cb.checked;
            wrap.classList.toggle('checked', block.checked);
            updateTodoCount();
          });
          const ed = createEditable(block, idx, 'To-do item…');
          wrap.append(cb, ed);
          content.appendChild(wrap);
        } else if (block.type === 'callout') {
          const wrap = document.createElement('div');
          wrap.className = 'b-callout';
          const icon = document.createElement('span');
          icon.className = 'callout-icon';
          icon.textContent = '✦';
          const ed = createEditable(block, idx, 'Callout note…');
          wrap.append(icon, ed);
          content.appendChild(wrap);
        } else {
          const placeholders = {
            heading: 'Section heading…',
            paragraph: 'Write text, or type / for commands…',
            quote: 'Pull quote…',
            code: '// Code snippet…'
          };
          const ed = createEditable(block, idx, placeholders[block.type] || 'Type / for commands…');
          ed.classList.add(`b-${block.type}`);
          content.appendChild(ed);
        }

        const actions = document.createElement('div');
        actions.className = 'block-actions';
        actions.innerHTML = `
          <button class="act-btn" type="button" data-act="add" title="Insert below" aria-label="Insert block below">+</button>
          <button class="act-btn" type="button" data-act="up" title="Move up" aria-label="Move block up" ${idx === 0 ? 'disabled' : ''}>↑</button>
          <button class="act-btn" type="button" data-act="down" title="Move down" aria-label="Move block down" ${idx === blocks.length - 1 ? 'disabled' : ''}>↓</button>
          <button class="act-btn del" type="button" data-act="del" title="Delete block" aria-label="Delete block">×</button>
        `;

        actions.addEventListener('click', (e) => {
          const btn = e.target.closest('button[data-act]');
          if (!btn) return;
          const act = btn.dataset.act;
          if (act === 'add') {
            openSlashMenu(btn, { afterIndex: idx, replaceIndex: null });
          } else if (act === 'up' && idx > 0) {
            [blocks[idx - 1], blocks[idx]] = [blocks[idx], blocks[idx - 1]];
            renderBlocks(block.id);
          } else if (act === 'down' && idx < blocks.length - 1) {
            [blocks[idx + 1], blocks[idx]] = [blocks[idx], blocks[idx + 1]];
            renderBlocks(block.id);
          } else if (act === 'del') {
            blocks.splice(idx, 1);
            if (!blocks.length) blocks.push({ id: nextId++, type: 'paragraph', text: '' });
            renderBlocks();
          }
        });

        row.append(content, actions);
        blocksList.appendChild(row);
      });

      updateTodoCount();

      if (focusId !== null) {
        const target = blocksList.querySelector(`.block-row[data-id="${focusId}"] .editable`);
        if (target) {
          target.focus();
          placeCaretAtEnd(target);
        }
      }
    }

    function createEditable(block, idx, placeholder) {
      const ed = document.createElement('div');
      ed.className = 'editable';
      ed.contentEditable = 'true';
      ed.dataset.placeholder = placeholder;
      ed.textContent = block.text;

      ed.addEventListener('input', () => {
        block.text = ed.textContent;
        if (block.text === '/') {
          openSlashMenu(ed, { afterIndex: idx, replaceIndex: idx });
        }
      });

      ed.addEventListener('keydown', (e) => {
        if (e.key === 'Enter' && !e.shiftKey && block.type !== 'code') {
          e.preventDefault();
          const newBlock = { id: nextId++, type: block.type === 'todo' ? 'todo' : 'paragraph', checked: false, text: '' };
          blocks.splice(idx + 1, 0, newBlock);
          renderBlocks(newBlock.id);
        }
      });

      return ed;
    }

    function placeCaretAtEnd(el) {
      const range = document.createRange();
      const sel = window.getSelection();
      range.selectNodeContents(el);
      range.collapse(false);
      sel.removeAllRanges();
      sel.addRange(range);
    }

    function renderSlashList() {
      const q = slashSearch.value.trim().toLowerCase();
      filteredCmds = COMMANDS.filter(c =>
        c.label.toLowerCase().includes(q) || c.desc.toLowerCase().includes(q)
      );
      activeCmdIdx = Math.min(activeCmdIdx, Math.max(0, filteredCmds.length - 1));

      if (!filteredCmds.length) {
        slashList.innerHTML = `<div style="padding:12px;font-size:12.5px;color:var(--ink-faint);text-align:center;">No matching block</div>`;
        return;
      }

      slashList.innerHTML = filteredCmds.map((cmd, i) => `
        <button class="slash-item ${i === activeCmdIdx ? 'active' : ''}" type="button" data-cmd="${cmd.type}">
          <span class="slash-badge">${cmd.icon}</span>
          <span class="slash-meta">
            <strong>${cmd.label}</strong>
            <span>${cmd.desc}</span>
          </span>
        </button>
      `).join('');
    }

    function openSlashMenu(anchorEl, ctx) {
      menuContext = ctx;
      slashSearch.value = '';
      activeCmdIdx = 0;
      renderSlashList();

      const rect = anchorEl.getBoundingClientRect();
      const top = Math.min(window.innerHeight - 330, rect.bottom + 8);
      const left = Math.min(window.innerWidth - 296, Math.max(12, rect.left));
      slashMenu.style.top = `${Math.max(60, top)}px`;
      slashMenu.style.left = `${left}px`;
      slashMenu.classList.add('open');
      slashSearch.focus();
    }

    function closeSlashMenu() {
      slashMenu.classList.remove('open');
    }

    function executeCommand(type) {
      closeSlashMenu();
      const newBlock = { id: nextId++, type, checked: false, text: '' };
      if (menuContext.replaceIndex !== null) {
        blocks[menuContext.replaceIndex] = newBlock;
      } else {
        blocks.splice(menuContext.afterIndex + 1, 0, newBlock);
      }
      renderBlocks(type === 'divider' ? null : newBlock.id);
    }

    slashSearch.addEventListener('input', () => {
      activeCmdIdx = 0;
      renderSlashList();
    });

    slashSearch.addEventListener('keydown', (e) => {
      if (e.key === 'ArrowDown') {
        e.preventDefault();
        activeCmdIdx = (activeCmdIdx + 1) % Math.max(1, filteredCmds.length);
        renderSlashList();
      } else if (e.key === 'ArrowUp') {
        e.preventDefault();
        activeCmdIdx = (activeCmdIdx - 1 + filteredCmds.length) % Math.max(1, filteredCmds.length);
        renderSlashList();
      } else if (e.key === 'Enter') {
        e.preventDefault();
        if (filteredCmds[activeCmdIdx]) executeCommand(filteredCmds[activeCmdIdx].type);
      } else if (e.key === 'Escape') {
        closeSlashMenu();
      }
    });

    slashList.addEventListener('click', (e) => {
      const item = e.target.closest('button[data-cmd]');
      if (item) executeCommand(item.dataset.cmd);
    });

    document.getElementById('topInsertBtn').addEventListener('click', (e) => {
      openSlashMenu(e.currentTarget, { afterIndex: blocks.length - 1, replaceIndex: null });
    });

    document.getElementById('tailAddBtn').addEventListener('click', (e) => {
      openSlashMenu(e.currentTarget, { afterIndex: blocks.length - 1, replaceIndex: null });
    });

    document.addEventListener('click', (e) => {
      if (!slashMenu.classList.contains('open')) return;
      if (e.target.closest('#slashMenu') || e.target.closest('#topInsertBtn') || e.target.closest('#tailAddBtn') || e.target.closest('button[data-act="add"]')) return;
      closeSlashMenu();
    });

    renderBlocks();
  </script>
</body>
</html>
```
