Skip to content

Commit 1a8cdb8

Browse files
committed
Row page no longer collapses long values replaced by plugin JavaScript
row-fields.js now marks a long value as .is-rich when it contains anything other than marked-up text, such as a map or chart drawn by a plugin, and CSS then shows all of it without the "Show all" toggle. A MutationObserver catches plugins that make the change after page load.
1 parent b98e1a2 commit 1a8cdb8

5 files changed

Lines changed: 125 additions & 1 deletion

File tree

‎datasette/static/app.css‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6294,6 +6294,20 @@ button.column-menu-trigger[aria-expanded="true"] {
62946294
.row-field-long.fits > .row-field-expand {
62956295
display: none;
62966296
}
6297+
/* row-fields.js adds .is-rich when the value holds more than text, such as a
6298+
map drawn by a plugin: show all of it, and drop the toggle */
6299+
.row-field-long.is-rich {
6300+
white-space: normal;
6301+
}
6302+
.row-field-long.is-rich > .row-field-value {
6303+
max-height: none;
6304+
overflow: visible;
6305+
-webkit-mask-image: none;
6306+
mask-image: none;
6307+
}
6308+
.row-field-long.is-rich > .row-field-expand {
6309+
display: none;
6310+
}
62976311
.row-field-expand {
62986312
margin-top: 0.35rem;
62996313
padding: 0;

‎datasette/static/row-fields.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,58 @@
88
// Re-checked whenever a value's size changes (window resize, font load).
99
function updateFits(value) {
1010
var wrap = value.parentElement;
11-
if (wrap.classList.contains("is-expanded")) {
11+
if (
12+
wrap.classList.contains("is-expanded") ||
13+
wrap.classList.contains("is-rich")
14+
) {
1215
return;
1316
}
1417
wrap.classList.toggle("fits", value.scrollHeight <= value.clientHeight + 1);
1518
}
19+
20+
// A long value is clamped because it is a lot of text. If a plugin has
21+
// replaced that text with something else - a map, a chart, an image - then
22+
// its length no longer says anything about how much room it needs, so
23+
// mark it as rich and let CSS show all of it without the toggle. Elements
24+
// that are just marked-up text, such as links, do not count.
25+
var TEXT_ELEMENTS =
26+
"a, abbr, b, br, code, em, i, kbd, mark, small, span, strong, sub, sup, wbr";
27+
function updateRich(value) {
28+
var rich = Array.prototype.some.call(
29+
value.querySelectorAll("*"),
30+
function (el) {
31+
return !el.matches(TEXT_ELEMENTS);
32+
},
33+
);
34+
value.parentElement.classList.toggle("is-rich", rich);
35+
if (!rich) {
36+
updateFits(value);
37+
}
38+
}
39+
1640
var longValues = document.querySelectorAll(
1741
".row-field-long > .row-field-value",
1842
);
43+
longValues.forEach(updateRich);
44+
// Plugin JavaScript usually runs after this script, so keep watching
45+
if (window.MutationObserver) {
46+
var richObserver = new MutationObserver(function (mutations) {
47+
var seen = [];
48+
mutations.forEach(function (mutation) {
49+
// childList mutations always have an element as their target
50+
var value = mutation.target.closest(
51+
".row-field-long > .row-field-value",
52+
);
53+
if (value && seen.indexOf(value) === -1) {
54+
seen.push(value);
55+
updateRich(value);
56+
}
57+
});
58+
});
59+
longValues.forEach(function (value) {
60+
richObserver.observe(value, { childList: true, subtree: true });
61+
});
62+
}
1963
if (window.ResizeObserver) {
2064
var observer = new ResizeObserver(function (entries) {
2165
entries.forEach(function (entry) {

‎docs/changelog.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Unreleased
2323
- Output renderers registered with :ref:`plugin_register_output_renderer` can now list the :ref:`extras <json_api_extra>` they need, such as ``count`` or ``human_description_en``, using a new ``"extras"`` key. Datasette calculates those and passes them to the ``render`` function in a new ``page`` argument, a dictionary which also includes ``next`` and ``next_url`` on table pages. (:issue:`2770`, :issue:`2186`)
2424
- The total time spent calculating suggested facets for a page is now capped at ten times the :ref:`setting_facet_suggest_time_limit_ms` setting, so tables with a large number of columns can no longer spend several seconds suggesting facets. (:issue:`862`)
2525
- The default :ref:`setting_max_post_body_bytes` limit has been increased from 2MB to 10MB (``10485760`` bytes), allowing larger JSON API requests.
26+
- Long values on the row page are no longer collapsed behind a "Show all 2,099 characters" toggle if plugin JavaScript has replaced the text with something else, such as a map. See :ref:`javascript_plugins_row_values`.
2627
- Fixed a bug where a database, table or saved query with a name such as ``count``, ``wrapper`` or ``tag`` could break the layout of its pages, because the class added to the ``<body>`` element for that name matched a class used by Datasette's own CSS and JavaScript.
2728

2829
.. _v1_0_a42:

‎docs/javascript_plugins.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,19 @@ You can customize layout and sizing without adding extra classes. For example, t
685685
width: min(720px, calc(100vw - 32px));
686686
}
687687
688+
.. _javascript_plugins_row_values:
689+
690+
Replacing values on the row page
691+
--------------------------------
692+
693+
The row page shows each value in an element with the class ``row-field-value``, inside a ``<div class="row-field" data-column="column_name">``.
694+
695+
Text values of more than 400 characters, or more than six lines, are collapsed to their first few lines with a "Show all 2,099 characters" toggle beneath them.
696+
697+
If your plugin replaces the contents of a ``row-field-value`` element with something other than text - a map or a chart, for example - Datasette notices and displays all of it, without the toggle. This works however long after the page has loaded your plugin makes the change.
698+
699+
Elements that are marked-up text do not count: a value containing only text and ``<a>``, ``<abbr>``, ``<b>``, ``<br>``, ``<code>``, ``<em>``, ``<i>``, ``<kbd>``, ``<mark>``, ``<small>``, ``<span>``, ``<strong>``, ``<sub>``, ``<sup>`` or ``<wbr>`` elements is still treated as text, and is still collapsed if it is long.
700+
688701
.. _javascript_plugins_copy_button:
689702
690703
Copy to clipboard buttons

‎tests/test_playwright.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,3 +4679,55 @@ def is_expanded():
46794679
toggle.click()
46804680
toggle.click()
46814681
assert is_expanded()
4682+
4683+
4684+
@pytest.mark.playwright
4685+
def test_row_page_long_value_replaced_by_plugin_is_not_clamped(
4686+
page, datasette_server, tmp_path
4687+
):
4688+
from playwright.sync_api import expect
4689+
4690+
# datasette_server shares this tmp_path, so this writes to its data.db
4691+
conn = sqlite3.connect(tmp_path / "data.db")
4692+
try:
4693+
conn.execute("create table long_text (id integer primary key, body text)")
4694+
conn.execute("insert into long_text values (1, ?)", ["word " * 2000])
4695+
conn.commit()
4696+
finally:
4697+
conn.close()
4698+
page.goto(f"{datasette_server}data/long_text/1")
4699+
field = page.locator('.row-field[data-column="body"]')
4700+
wrap = field.locator(".row-field-long")
4701+
value = field.locator(".row-field-value")
4702+
expand = field.locator(".row-field-expand")
4703+
4704+
# A long text value is clamped, with a toggle to show the rest
4705+
expect(wrap).not_to_have_class(re.compile(r"\bis-rich\b"))
4706+
expect(expand).to_be_visible()
4707+
clamped_height = value.bounding_box()["height"]
4708+
assert clamped_height < 300
4709+
4710+
# Marked-up text is still text
4711+
value.evaluate(
4712+
"el => { el.innerHTML = 'word '.repeat(2000) + '<a href=\"/\">link</a>'; }"
4713+
)
4714+
expect(expand).to_be_visible()
4715+
expect(wrap).not_to_have_class(re.compile(r"\bis-rich\b"))
4716+
4717+
# A plugin replaces the text with something that is not text, as
4718+
# a plugin that draws a map or a chart would
4719+
value.evaluate("""el => {
4720+
const div = document.createElement("div");
4721+
div.style.height = "400px";
4722+
el.replaceChildren(div);
4723+
}""")
4724+
expect(wrap).to_have_class(re.compile(r"\bis-rich\b"))
4725+
expect(expand).to_be_hidden()
4726+
assert value.bounding_box()["height"] == 400
4727+
assert value.evaluate("el => getComputedStyle(el).overflowY") == "visible"
4728+
4729+
# Back to text, back to being clamped
4730+
value.evaluate("el => { el.textContent = 'word '.repeat(2000); }")
4731+
expect(wrap).not_to_have_class(re.compile(r"\bis-rich\b"))
4732+
expect(expand).to_be_visible()
4733+
assert value.bounding_box()["height"] == clamped_height

0 commit comments

Comments
 (0)