@writedocs/generator 0.7.1 → 0.7.3

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,17 +1,33 @@
1
1
  // SearchModal.astro's own trigger button (rendered inside TopBar.astro) +
2
2
  // overlay/modal. Owns opening/closing the modal, the ⌘K/Ctrl K shortcut,
3
3
  // lazy-loading Pagefind on first open, and rendering results.
4
+
5
+ // The current page's modal, for the ⌘K/Ctrl K shortcut. ClientRouter swaps
6
+ // in a new modal on every navigation and initSearch() runs again for it; the
7
+ // shortcut is one document listener, attached once, that always acts on
8
+ // this. (Attached per page, it piled up one listener per visited page, each
9
+ // still driving that page's detached modal - on a page without a modal of
10
+ // its own, Ctrl K then locked the page's scrolling.)
11
+ let active: { toggle(): void; closeIfOpen(): void } | null = null;
12
+ let shortcutBound = false;
13
+
4
14
  export function initSearch(root: ParentNode) {
15
+ // No trigger button on a `blank` page (no topbar) - the modal and its
16
+ // shortcut still work there, as BaseLayout.astro intends.
5
17
  const trigger = root.querySelector<HTMLButtonElement>('[data-search-trigger]');
6
18
  const overlay = root.querySelector<HTMLElement>('#wd-search-overlay');
7
19
  const input = root.querySelector<HTMLInputElement>('#wd-search-input');
8
20
  const resultsEl = root.querySelector<HTMLElement>('#wd-search-results');
9
- if (!trigger || !overlay || !input || !resultsEl || trigger.dataset.wdInit) return;
10
- trigger.dataset.wdInit = 'true';
21
+ if (!overlay || !input || !resultsEl) {
22
+ active = null;
23
+ return;
24
+ }
25
+ if (overlay.dataset.wdInit) return;
26
+ overlay.dataset.wdInit = 'true';
11
27
 
12
28
  // Most keyboards outside macOS/iOS don't have a command key - "Ctrl K"
13
29
  // reads more naturally there than the ⌘ glyph.
14
- const kbd = trigger.querySelector('.wd-search-kbd');
30
+ const kbd = trigger?.querySelector('.wd-search-kbd');
15
31
  if (kbd && !/Mac|iPhone|iPad/.test(navigator.userAgent)) {
16
32
  kbd.textContent = 'Ctrl K';
17
33
  }
@@ -122,10 +138,10 @@ export function initSearch(root: ParentNode) {
122
138
  function close() {
123
139
  overlay.hidden = true;
124
140
  document.body.style.overflow = '';
125
- trigger.focus();
141
+ trigger?.focus();
126
142
  }
127
143
 
128
- trigger.addEventListener('click', open);
144
+ trigger?.addEventListener('click', open);
129
145
  overlay.addEventListener('click', (e) => {
130
146
  if (e.target === overlay) close();
131
147
  });
@@ -143,13 +159,22 @@ export function initSearch(root: ParentNode) {
143
159
  close();
144
160
  }
145
161
  });
146
- document.addEventListener('keydown', (e) => {
147
- if ((e.key === 'k' || e.key === 'K') && (e.metaKey || e.ctrlKey)) {
148
- e.preventDefault();
149
- if (overlay.hidden) open();
150
- else close();
151
- } else if (e.key === 'Escape' && !overlay.hidden) {
152
- close();
153
- }
154
- });
162
+ active = {
163
+ toggle: () => (overlay.hidden ? open() : close()),
164
+ closeIfOpen: () => {
165
+ if (!overlay.hidden) close();
166
+ },
167
+ };
168
+ if (!shortcutBound) {
169
+ shortcutBound = true;
170
+ document.addEventListener('keydown', (e) => {
171
+ if (!active) return;
172
+ if ((e.key === 'k' || e.key === 'K') && (e.metaKey || e.ctrlKey)) {
173
+ e.preventDefault();
174
+ active.toggle();
175
+ } else if (e.key === 'Escape') {
176
+ active.closeIfOpen();
177
+ }
178
+ });
179
+ }
155
180
  }
@@ -692,8 +692,8 @@
692
692
  "proxy": {
693
693
  "default": true,
694
694
  "type": "boolean",
695
- "description": "Send \"Try it\" requests through writedocs' proxy, for APIs that don't allow calls from other sites (CORS). Default true.",
696
- "markdownDescription": "Send \"Try it\" requests through writedocs' proxy, for APIs that don't allow calls from other sites (CORS). Default true."
695
+ "description": "When the browser blocks a \"Try it\" request (the API doesn't allow calls from other sites - CORS), retry it through writedocs' proxy. Requests always go to the API directly first. Default true.",
696
+ "markdownDescription": "When the browser blocks a \"Try it\" request (the API doesn't allow calls from other sites - CORS), retry it through writedocs' proxy. Requests always go to the API directly first. Default true."
697
697
  }
698
698
  },
699
699
  "additionalProperties": false,