click_extra.sphinx.crosshair module

Highlight the row and the column under the pointer in a two-axis table.

A reader of a two-axis table traces each cell back to its row label and its column header. On a wide grid, like a feature comparison across a dozen projects, that means counting columns. This module ships a script that highlights the row and the column of the cell under the pointer, headers included, and the cell itself more strongly.

The script also makes a cell whose whole content is one link clickable over its full area, like a cell holding only a check mark that links to its evidence.

Both apply to the tables click_extra.sphinx.tables marks with CORNER_TABLE_CLASS: the ones whose top-left cell names both axes. Other tables are left as they are. The stylesheet and the script are written into the build rather than packaged as data files.

click_extra.sphinx.crosshair.CROSSHAIR_CSS = '/* Hovered row and column. The row rule matches the `tr` the script marks, and\n * keeps the specificity of the column rule: one notch higher, it would outrank\n * the rule of the hovered cell below. */\ntable td.crosshair-col,\ntable th.crosshair-col,\ntr.crosshair-row > td,\ntr.crosshair-row > th {\n  background-color: var(--color-background-hover, rgba(128, 128, 128, 0.12));\n}\n\n/* The hovered cell, outlined to mark the intersection. */\ntable td.crosshair-cell,\ntable th.crosshair-cell {\n  background-color: var(--color-highlight-on-target, rgba(255, 209, 0, 0.4));\n  outline: 2px solid var(--color-brand-primary, #336790);\n  outline-offset: -2px;\n}\n\n/* A cell whose whole content is one link: the link\'s hit area covers the\n * cell. The link itself is untouched, so keyboard focus and middle-click keep\n * working, and the overlay is transparent, so the highlight shows through. */\ntable td.linked-cell,\ntable th.linked-cell {\n  position: relative;\n  cursor: pointer;\n}\ntable td.linked-cell a[href]::after,\ntable th.linked-cell a[href]::after {\n  content: "";\n  position: absolute;\n  inset: 0;\n}\n'

Stylesheet of the classes the crosshair script sets.

The colors come from Furo’s CSS variables, so the highlight follows Furo’s light and dark modes. Each variable carries a fallback for other themes.

click_extra.sphinx.crosshair.CROSSHAIR_JS = '(function (selector) {\n  function ready(fn) {\n    if (document.readyState !== "loading") {\n      fn();\n    } else {\n      document.addEventListener("DOMContentLoaded", fn);\n    }\n  }\n\n  ready(function () {\n    Array.prototype.forEach.call(document.querySelectorAll(selector), function (table) {\n      // Mark each cell whose whole content is one link. A cell mixing a link\n      // with other text, like a header with a footnote marker, is left alone.\n      Array.prototype.forEach.call(table.querySelectorAll("td, th"), function (cell) {\n        var links = cell.querySelectorAll("a[href]");\n        if (\n          links.length === 1 &&\n          cell.textContent.trim() === links[0].textContent.trim()\n        ) {\n          cell.classList.add("linked-cell");\n        }\n      });\n\n      // A merged cell shifts the cells after it in its row, so a cell index no\n      // longer names a visual column.\n      if (table.querySelector("[colspan], [rowspan]")) {\n        return;\n      }\n\n      function clear() {\n        var marked = table.querySelectorAll(\n          ".crosshair-col, .crosshair-row, .crosshair-cell"\n        );\n        Array.prototype.forEach.call(marked, function (element) {\n          element.classList.remove("crosshair-col", "crosshair-row", "crosshair-cell");\n        });\n      }\n\n      table.addEventListener("mouseover", function (event) {\n        var cell = event.target.closest("td, th");\n        if (!cell || !table.contains(cell)) {\n          return;\n        }\n        var column = cell.cellIndex;\n        clear();\n        // The column: the cell at the same index in every row, header included.\n        Array.prototype.forEach.call(table.rows, function (row) {\n          var columnCell = row.cells[column];\n          if (columnCell) {\n            columnCell.classList.add("crosshair-col");\n          }\n        });\n        cell.parentElement.classList.add("crosshair-row");\n        cell.classList.add("crosshair-cell");\n      });\n\n      table.addEventListener("mouseleave", clear);\n    });\n  });\n})("table.corner-table");\n'

Script setting the crosshair and full-cell link classes on two-axis tables.

The selector of the tables is the argument of the outer function, so the class click_extra.sphinx.tables sets is named in one place.

click_extra.sphinx.crosshair.STYLESHEET = 'click-extra-crosshair.css'

File name of the stylesheet, served from the _static folder.

click_extra.sphinx.crosshair.SCRIPT = 'click-extra-crosshair.js'

File name of the script, served from the _static folder.

click_extra.sphinx.crosshair.add_crosshair(app)[source]

Write CROSSHAIR_CSS and CROSSHAIR_JS into the build.

Both files go to the static folder of the extension (see click_extra.sphinx.tables._write_static_file), and every page links them. Only an HTML builder serves them, so other builders skip it.

Return type:

None

click_extra.sphinx.crosshair.setup(app)[source]

Register the crosshair stylesheet and script on app.

Called from click_extra.sphinx.setup() so projects only need to list "click_extra.sphinx" in their extensions. The tables it applies to are marked by click_extra.sphinx.tables.mark_corner_tables().

Return type:

None