hyperbook 0.46.0 → 0.47.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.
@@ -1,33 +1,83 @@
1
1
  var hyperbook = (function () {
2
- const collapsibles = document.getElementsByClassName("collapsible");
2
+ /**
3
+ * Initialize elements within the given root element.
4
+ * @param {HTMLElement} root - The root element to initialize.
5
+ */
6
+ const initCollapsibles = (root) => {
7
+ const collapsibleEls = root.getElementsByClassName("collapsible");
8
+ for (let collapsible of collapsibleEls) {
9
+ collapsible.addEventListener("click", () => {
10
+ collapsible.classList.toggle("expanded");
11
+ const id = collapsible.parentElement.getAttribute("data-id");
12
+ if (id) {
13
+ store.collapsibles.get(id).then((result) => {
14
+ if (!result) {
15
+ store.collapsibles.put({ id }).then(() => {
16
+ updateCollapsibles(root);
17
+ });
18
+ } else {
19
+ store.collapsibles.delete(id).then(() => {
20
+ updateCollapsibles(root);
21
+ });
22
+ }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ updateCollapsibles(root);
28
+ };
3
29
 
4
- for (let collapsible of collapsibles) {
5
- collapsible.addEventListener("click", () => {
6
- collapsible.classList.toggle("expanded");
30
+ /**
31
+ * @param {HTMLElement} root
32
+ */
33
+ const updateCollapsibles = (root) => {
34
+ store.collapsibles.toArray().then((collapsibles) => {
35
+ const collapsibleEls = root.getElementsByClassName("collapsible");
36
+ for (let collapsibleEl of collapsibleEls) {
37
+ const id = collapsibleEl.parentElement.getAttribute("data-id");
38
+ if (id) {
39
+ const expanded = collapsibles.some((c) => c.id === id);
40
+ if (expanded) {
41
+ collapsibleEl.classList.add("expanded");
42
+ } else {
43
+ collapsibleEl.classList.remove("expanded");
44
+ }
45
+ }
46
+ }
7
47
  });
8
- }
48
+ };
49
+
50
+ const initSearch = (root) => {
51
+ const searchInputEl = root.querySelector("#search-input");
52
+ if (searchInputEl) {
53
+ searchInputEl.addEventListener("keypress", (event) => {
54
+ if (event.key === "Enter") {
55
+ event.preventDefault();
56
+ search();
57
+ }
58
+ });
59
+ }
60
+ };
9
61
 
62
+ /**
63
+ * Toggle the table of contents drawer.
64
+ */
10
65
  function tocToggle() {
11
66
  const tocDrawerEl = document.getElementById("toc-drawer");
12
67
  tocDrawerEl.open = !tocDrawerEl.open;
13
68
  }
14
- // search
15
-
16
- const searchInputEl = document.getElementById("search-input");
17
- if (searchInputEl) {
18
- searchInputEl.addEventListener("keypress", (event) => {
19
- if (event.key === "Enter") {
20
- event.preventDefault();
21
- search();
22
- }
23
- });
24
- }
25
69
 
70
+ /**
71
+ * Toggle the search drawer.
72
+ */
26
73
  function searchToggle() {
27
74
  const searchDrawerEl = document.getElementById("search-drawer");
28
75
  searchDrawerEl.open = !searchDrawerEl.open;
29
76
  }
30
77
 
78
+ /**
79
+ * Perform a search and display the results.
80
+ */
31
81
  function search() {
32
82
  const resultsEl = document.getElementById("search-results");
33
83
  resultsEl.innerHTML = "";
@@ -83,6 +133,9 @@ var hyperbook = (function () {
83
133
  }
84
134
  }
85
135
 
136
+ /**
137
+ * Open the QR code dialog.
138
+ */
86
139
  function qrcodeOpen() {
87
140
  const qrCodeDialog = document.getElementById("qrcode-dialog");
88
141
  const qrcodeEls = qrCodeDialog.getElementsByClassName("make-qrcode");
@@ -106,56 +159,94 @@ var hyperbook = (function () {
106
159
  qrCodeDialog.showModal();
107
160
  }
108
161
 
162
+ /**
163
+ * Close the QR code dialog.
164
+ */
109
165
  function qrcodeClose() {
110
166
  const qrCodeDialog = document.getElementById("qrcode-dialog");
111
167
  qrCodeDialog.close();
112
168
  }
113
169
 
170
+ /**
171
+ * Toggle the navigation drawer.
172
+ */
114
173
  function navToggle() {
115
174
  const navDrawerEl = document.getElementById("nav-drawer");
116
175
  navDrawerEl.open = !navDrawerEl.open;
117
176
  }
118
177
 
119
178
  /**
120
- * @param {string} key
121
- * @param {string} label
179
+ * Toggle a bookmark.
180
+ * @param {string} key - The key of the bookmark.
181
+ * @param {string} label - The label of the bookmark.
122
182
  */
123
183
  function toggleBookmark(key, label) {
124
- const bookmarks = JSON.parse(localStorage.getItem("bookmarks") || "{}");
125
184
  const el = document.querySelectorAll(`.bookmark[data-key="${key}"]`);
126
- if (bookmarks[key]) {
127
- delete bookmarks[key];
128
- el.forEach((e) => e.classList.remove("active"));
129
- } else {
130
- bookmarks[key] = label;
131
- el.forEach((e) => e.classList.add("active"));
132
- }
133
- localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
134
-
135
- if (hyperbook.bookmarks) {
136
- hyperbook.bookmarks.update();
137
- }
185
+ store.bookmarks.get(key).then((bookmark) => {
186
+ if (!bookmark) {
187
+ store.bookmarks.add({ path: key, label }).then(() => {
188
+ el.forEach((e) => e.classList.add("active"));
189
+ hyperbook.bookmarks.update();
190
+ });
191
+ } else {
192
+ store.bookmarks.delete(key).then(() => {
193
+ el.forEach((e) => e.classList.remove("active"));
194
+ hyperbook.bookmarks.update();
195
+ });
196
+ }
197
+ });
138
198
  }
139
199
 
140
- function initBookmarks() {
141
- const bookmarkEls = document.getElementsByClassName("bookmark");
142
- const bookmarks = JSON.parse(localStorage.getItem("bookmarks") || "{}");
200
+ /**
201
+ * Initialize bookmarks within the given root element.
202
+ * @param {HTMLElement} [root=document] - The root element to initialize.
203
+ */
204
+ function initBookmarks(root = document) {
205
+ const bookmarkEls = root.getElementsByClassName("bookmark");
143
206
  for (let bookmarkEl of bookmarkEls) {
144
207
  const key = bookmarkEl.getAttribute("data-key");
145
- if (bookmarks[key]) {
146
- bookmarkEl.classList.add("active");
147
- }
208
+ store.bookmarks.get(key).then((bookmark) => {
209
+ if (bookmark) {
210
+ bookmarkEl.classList.add("active");
211
+ }
212
+ });
148
213
  }
149
214
  }
150
- initBookmarks();
151
215
 
152
216
  /**
153
- * @param {HTMLElement} el
217
+ * Toggle the lightbox view of an element.
218
+ * @param {HTMLElement} el - The element to toggle.
154
219
  */
155
220
  function toggleLightbox(el) {
156
221
  el.parentElement.classList.toggle("lightbox");
157
222
  el.parentElement.classList.toggle("normal");
158
223
  }
224
+
225
+ function init(root) {
226
+ initCollapsibles(root);
227
+ initSearch(root);
228
+ initBookmarks(root);
229
+ }
230
+
231
+ // Initialize existing elements on document load
232
+ document.addEventListener("DOMContentLoaded", () => {
233
+ init(document);
234
+ });
235
+
236
+ // Observe for new elements added to the DOM
237
+ const observer = new MutationObserver((mutations) => {
238
+ mutations.forEach((mutation) => {
239
+ mutation.addedNodes.forEach((node) => {
240
+ if (node.nodeType === 1) {
241
+ // Element node
242
+ init(node);
243
+ }
244
+ });
245
+ });
246
+ });
247
+
248
+ observer.observe(document.body, { childList: true, subtree: true });
249
+
159
250
  return {
160
251
  toggleLightbox,
161
252
  toggleBookmark,
@@ -165,5 +256,6 @@ var hyperbook = (function () {
165
256
  search,
166
257
  qrcodeOpen,
167
258
  qrcodeClose,
259
+ init,
168
260
  };
169
261
  })();