hyperbook 0.46.1 → 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.
@@ -3,27 +3,51 @@ var hyperbook = (function () {
3
3
  * Initialize elements within the given root element.
4
4
  * @param {HTMLElement} root - The root element to initialize.
5
5
  */
6
- const init = (root) => {
7
- const collapsibles = root.getElementsByClassName("collapsible");
8
- for (let collapsible of collapsibles) {
6
+ const initCollapsibles = (root) => {
7
+ const collapsibleEls = root.getElementsByClassName("collapsible");
8
+ for (let collapsible of collapsibleEls) {
9
9
  collapsible.addEventListener("click", () => {
10
- const id = collapsible.parentElement.getAttribute("data-id");
11
10
  collapsible.classList.toggle("expanded");
12
- const expanded = collapsible.classList.contains("expanded");
11
+ const id = collapsible.parentElement.getAttribute("data-id");
13
12
  if (id) {
14
- [...collapsibles]
15
- .filter((c) => c.parentElement.getAttribute("data-id") === id)
16
- .forEach((c) => {
17
- if (expanded) {
18
- c.classList.add("expanded");
19
- } else {
20
- c.classList.remove("expanded");
21
- }
22
- });
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
+ });
23
24
  }
24
25
  });
25
26
  }
27
+ updateCollapsibles(root);
28
+ };
29
+
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
+ }
47
+ });
48
+ };
26
49
 
50
+ const initSearch = (root) => {
27
51
  const searchInputEl = root.querySelector("#search-input");
28
52
  if (searchInputEl) {
29
53
  searchInputEl.addEventListener("keypress", (event) => {
@@ -33,8 +57,6 @@ var hyperbook = (function () {
33
57
  }
34
58
  });
35
59
  }
36
-
37
- initBookmarks(root);
38
60
  };
39
61
 
40
62
  /**
@@ -159,20 +181,20 @@ var hyperbook = (function () {
159
181
  * @param {string} label - The label of the bookmark.
160
182
  */
161
183
  function toggleBookmark(key, label) {
162
- const bookmarks = JSON.parse(localStorage.getItem("bookmarks") || "{}");
163
184
  const el = document.querySelectorAll(`.bookmark[data-key="${key}"]`);
164
- if (bookmarks[key]) {
165
- delete bookmarks[key];
166
- el.forEach((e) => e.classList.remove("active"));
167
- } else {
168
- bookmarks[key] = label;
169
- el.forEach((e) => e.classList.add("active"));
170
- }
171
- localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
172
-
173
- if (hyperbook.bookmarks) {
174
- hyperbook.bookmarks.update();
175
- }
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
+ });
176
198
  }
177
199
 
178
200
  /**
@@ -181,12 +203,13 @@ var hyperbook = (function () {
181
203
  */
182
204
  function initBookmarks(root = document) {
183
205
  const bookmarkEls = root.getElementsByClassName("bookmark");
184
- const bookmarks = JSON.parse(localStorage.getItem("bookmarks") || "{}");
185
206
  for (let bookmarkEl of bookmarkEls) {
186
207
  const key = bookmarkEl.getAttribute("data-key");
187
- if (bookmarks[key]) {
188
- bookmarkEl.classList.add("active");
189
- }
208
+ store.bookmarks.get(key).then((bookmark) => {
209
+ if (bookmark) {
210
+ bookmarkEl.classList.add("active");
211
+ }
212
+ });
190
213
  }
191
214
  }
192
215
 
@@ -199,6 +222,12 @@ var hyperbook = (function () {
199
222
  el.parentElement.classList.toggle("normal");
200
223
  }
201
224
 
225
+ function init(root) {
226
+ initCollapsibles(root);
227
+ initSearch(root);
228
+ initBookmarks(root);
229
+ }
230
+
202
231
  // Initialize existing elements on document load
203
232
  document.addEventListener("DOMContentLoaded", () => {
204
233
  init(document);