@hyperbook/markdown 0.44.0 → 0.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,34 +4,39 @@ var hyperbook = (function () {
4
4
  * @param {HTMLElement} root - The root element to initialize.
5
5
  */
6
6
  const initCollapsibles = (root) => {
7
- const collapsibleEls = root.getElementsByClassName("collapsible");
8
- for (let collapsible of collapsibleEls) {
9
- const id = collapsible.parentElement.getAttribute("data-id");
10
-
11
- if (id.startsWith("_nav:") && !collapsible.classList.contains("empty")) {
12
- const link = collapsible.querySelector("a");
7
+ // Handle both navigation sections and directive-collapsible elements
8
+ const detailsEls = root.querySelectorAll("details.section, details.directive-collapsible");
9
+ for (let details of detailsEls) {
10
+ const id = details.getAttribute("data-id");
11
+
12
+ // Prevent link clicks from toggling the details element in navigation
13
+ if (id && id.startsWith("_nav:") && !details.classList.contains("empty")) {
14
+ const link = details.querySelector("summary a");
13
15
  link?.addEventListener("click", (event) => {
14
16
  event.stopPropagation();
15
17
  });
16
18
  }
17
- collapsible.addEventListener("click", () => {
18
- collapsible.classList.toggle("expanded");
19
+
20
+ // Listen for toggle events to persist state and sync with other elements
21
+ details.addEventListener("toggle", () => {
19
22
  if (id) {
20
- store.collapsibles.get(id).then((result) => {
21
- if (!result) {
22
- store.collapsibles.put({ id }).then(() => {
23
- updateCollapsibles(root);
24
- });
25
- } else {
26
- store.collapsibles.delete(id).then(() => {
27
- updateCollapsibles(root);
28
- });
23
+ if (details.open) {
24
+ store.collapsibles.put({ id });
25
+ } else {
26
+ store.collapsibles.delete(id);
27
+ }
28
+
29
+ // Sync all elements with the same ID
30
+ const allWithSameId = document.querySelectorAll(`[data-id="${id}"]`);
31
+ for (let el of allWithSameId) {
32
+ if (el !== details && el.tagName === "DETAILS") {
33
+ el.open = details.open;
29
34
  }
30
- });
35
+ }
31
36
  }
32
37
 
33
38
  setTimeout(() => {
34
- window.dispatchEvent(new Event("resize")); // geogebra new this in order resize the applet
39
+ window.dispatchEvent(new Event("resize")); // geogebra needs this to resize the applet
35
40
  }, 100);
36
41
  });
37
42
  }
@@ -43,15 +48,15 @@ var hyperbook = (function () {
43
48
  */
44
49
  const updateCollapsibles = (root) => {
45
50
  store.collapsibles.toArray().then((collapsibles) => {
46
- const collapsibleEls = root.getElementsByClassName("collapsible");
47
- for (let collapsibleEl of collapsibleEls) {
48
- const id = collapsibleEl.parentElement.getAttribute("data-id");
51
+ const detailsEls = root.querySelectorAll("details.section, details.directive-collapsible");
52
+ for (let details of detailsEls) {
53
+ const id = details.getAttribute("data-id");
49
54
  if (id) {
50
- const expanded = collapsibles.some((c) => c.id === id);
51
- if (expanded) {
52
- collapsibleEl.classList.add("expanded");
53
- } else if (!id.startsWith("_nav:")) {
54
- collapsibleEl.classList.remove("expanded");
55
+ const shouldBeOpen = collapsibles.some((c) => c.id === id);
56
+ // Only update if state differs and it's not a navigation section
57
+ // (navigation sections are auto-expanded based on current page)
58
+ if (!id.startsWith("_nav:") && details.open !== shouldBeOpen) {
59
+ details.open = shouldBeOpen;
55
60
  }
56
61
  }
57
62
  }