Commit 581e80e
committed
mcp(docs[_static]): re-attach prompt-admonition copy buttons after gp-sphinx SPA swap
``copybutton_selector`` in ``docs/conf.py`` is
``"div.highlight pre, div.admonition.prompt > p:last-child"`` — we copy
*prompt text* as well as code. On full-page load,
``sphinx-copybutton`` iterates that selector and inserts a ``.copybtn``
after every match, then binds ``new ClipboardJS('.copybtn', ...)``;
ClipboardJS uses delegated listening on ``document.body``, so those
clicks keep working across SPA DOM swaps.
On SPA navigation, however, gp-sphinx's
``spa-nav.js::addCopyButtons`` iterates ``"div.highlight pre"`` only —
it does NOT re-attach buttons to ``.admonition.prompt > p:last-child``.
After an SPA swap, prompt-heavy pages like ``/recipes/`` (no code
blocks, eight ``.admonition.prompt``) render naked: no copy
affordance at all. This is the user-visible regression introduced by
the gp-sphinx migration (``c822f02``).
This shim fills the gap:
* Capture the first live ``.copybtn`` that appears anywhere in the
document as a reusable template, so ``sphinx-copybutton``'s
locale-specific tooltip and tabler-copy SVG are preserved exactly.
``FALLBACK_COPYBTN_HTML`` covers the rare case where the user's
first page has no ``.copybtn`` anywhere (landing with no code and
no prompts) before any SPA nav.
* On every SPA swap, re-insert ``.copybtn`` siblings after any
``.admonition.prompt > p:last-child`` lacking one, assigning the
``<p>`` a ``mcp-promptcell-N`` id and wiring the button's
``data-clipboard-target``. The inserted element plugs into
ClipboardJS's existing body delegation *and* the project's
capture-phase ``prompt-copy.js`` delegation transparently — no
extra click handlers required.
Initial-load ordering: at deferred-script execution time
``document.readyState`` is ``"interactive"``, not ``"loading"``, so
a naive readyState check would run our initial pass eagerly —
*before* sphinx-copybutton's DOMContentLoaded handler fires, letting
our fallback template beat sphinx-copybutton's proper one. We gate
on ``"complete"`` instead, so we always register a DOMContentLoaded
listener (which fires after sphinx-copybutton's, registered earlier
when ``readyState`` was ``"loading"``) unless the document is already
fully loaded.
Verified end-to-end in real Chromium via Playwright:
* Fresh load of ``/recipes/``: 8 buttons, all with
sphinx-copybutton's ``#codecell{N}`` targets and tabler-copy SVG
— shim is inert.
* SPA-nav away then back to ``/recipes/``: 8 buttons re-inserted
with ``#mcp-promptcell-{N}`` targets.
* Click on a shim-inserted button: ``prompt-copy.js``'s
capture-phase delegation fires, markdown-preserving
``navigator.clipboard.writeText`` succeeds, ``.success`` class +
``"Copied!"`` tooltip + tabler-check SVG swap — identical
feedback to a fresh-load click.
* ``/topics/prompting/`` (code-only, no prompts): 4 buttons
re-added by gp-sphinx, clicks still fire through ClipboardJS
delegation.
* Multiple SPA navs in a row: idempotent, no double-insertion.
The correct upstream fix is in gp-sphinx — its ``addCopyButtons``
should iterate the full ``copybutton_selector`` (or dispatch a
``spa-nav-complete`` event that consumers like ``sphinx-copybutton``
can hook). Until then, this project-local shim keeps the docs
behaving.1 parent bd0c1ac commit 581e80e
2 files changed
Lines changed: 138 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| 132 | + | |
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
| |||
0 commit comments