@nulib/canopy 0.0.0 → 2.0.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.
- package/bin/canopy.js +16 -0
- package/lib/AGENTS.md +107 -0
- package/lib/base-path.js +67 -0
- package/lib/build/appearance.js +76 -0
- package/lib/build/assets.js +30 -0
- package/lib/build/build.js +350 -0
- package/lib/build/dev.js +1355 -0
- package/lib/build/devtoast.config.json +6 -0
- package/lib/build/devtoast.css +14 -0
- package/lib/build/facets.js +19 -0
- package/lib/build/fonts.js +105 -0
- package/lib/build/iiif.js +2990 -0
- package/lib/build/log.js +31 -0
- package/lib/build/mdx.js +1967 -0
- package/lib/build/package-names.js +111 -0
- package/lib/build/pages.js +420 -0
- package/lib/build/rehype-task-list-labels.js +54 -0
- package/lib/build/runtimes.js +141 -0
- package/lib/build/search-index.js +55 -0
- package/lib/build/search-text.js +160 -0
- package/lib/build/search-workflow.js +72 -0
- package/lib/build/search.js +302 -0
- package/lib/build/sitemap.js +197 -0
- package/lib/build/styles.js +140 -0
- package/lib/build/theme-tokens.js +78 -0
- package/lib/build/ui-styles.js +25 -0
- package/lib/build/upgrade-notices.js +121 -0
- package/lib/cli.js +64 -0
- package/lib/common.js +580 -0
- package/lib/components/bibliography.js +197 -0
- package/lib/components/featured.js +237 -0
- package/lib/components/hero-slider-runtime.js +215 -0
- package/lib/components/image-story-runtime-entry.js +249 -0
- package/lib/components/map-runtime.js +114 -0
- package/lib/components/metadata-index.js +32 -0
- package/lib/components/nav-place.js +227 -0
- package/lib/components/navigation.js +381 -0
- package/lib/components/referenced.js +217 -0
- package/lib/components/slider-runtime-entry.js +106 -0
- package/lib/components/timeline-runtime.js +80 -0
- package/lib/components/viewer-runtime-entry.js +88 -0
- package/lib/config-path.js +78 -0
- package/lib/default-locale.js +113 -0
- package/lib/discovery/browser-loader.js +107 -0
- package/lib/discovery/browser.js +115 -0
- package/lib/discovery/config.js +25 -0
- package/lib/discovery/envelope.js +124 -0
- package/lib/discovery/export.js +226 -0
- package/lib/discovery/ingestion.js +62 -0
- package/lib/discovery/pagination.js +66 -0
- package/lib/discovery/provenance.js +79 -0
- package/lib/discovery/search.js +81 -0
- package/lib/discovery/tools.js +155 -0
- package/lib/discovery/validation.js +121 -0
- package/lib/head.js +154 -0
- package/lib/iiif/thumbnail.js +402 -0
- package/lib/index.js +9 -0
- package/lib/locale-runtime.js +69 -0
- package/lib/locales.js +203 -0
- package/lib/orchestrator.js +231 -0
- package/lib/page-context.js +24 -0
- package/lib/search/bundle.js +25 -0
- package/lib/search/data.js +202 -0
- package/lib/search/engine.js +86 -0
- package/lib/search/index-cache.js +95 -0
- package/lib/search/search-app.jsx +835 -0
- package/lib/search/search-engine-runtime.js +44 -0
- package/lib/search/search-form-runtime.js +462 -0
- package/lib/search/search.js +676 -0
- package/package.json +109 -3
- package/types/base-path.d.ts +4 -0
- package/types/orchestrator.d.ts +17 -0
- package/ui/dist/index.mjs +7358 -0
- package/ui/dist/index.mjs.map +7 -0
- package/ui/dist/server.mjs +12731 -0
- package/ui/dist/server.mjs.map +7 -0
- package/ui/fonts/OFL.txt +98 -0
- package/ui/fonts/google-sans-flex-latin-ext.woff2 +0 -0
- package/ui/fonts/google-sans-flex-latin.woff2 +0 -0
- package/ui/fonts/source-serif-4-italic-latin-ext.woff2 +0 -0
- package/ui/fonts/source-serif-4-italic-latin.woff2 +0 -0
- package/ui/fonts/source-serif-4-latin-ext.woff2 +0 -0
- package/ui/fonts/source-serif-4-latin.woff2 +0 -0
- package/ui/styles/base/_common.scss +97 -0
- package/ui/styles/base/_heading.scss +61 -0
- package/ui/styles/base/_markdown.scss +188 -0
- package/ui/styles/base/_type.scss +51 -0
- package/ui/styles/base/_utilities.scss +7 -0
- package/ui/styles/base/index.scss +5 -0
- package/ui/styles/components/_article-card.scss +108 -0
- package/ui/styles/components/_band.scss +104 -0
- package/ui/styles/components/_bibliography.scss +88 -0
- package/ui/styles/components/_buttons.scss +143 -0
- package/ui/styles/components/_card.scss +109 -0
- package/ui/styles/components/_diagram.scss +76 -0
- package/ui/styles/components/_feature.scss +66 -0
- package/ui/styles/components/_footer.scss +136 -0
- package/ui/styles/components/_gallery.scss +476 -0
- package/ui/styles/components/_image-story.scss +56 -0
- package/ui/styles/components/_interstitial-hero.scss +625 -0
- package/ui/styles/components/_layout.scss +83 -0
- package/ui/styles/components/_map.scss +296 -0
- package/ui/styles/components/_metadata-index.scss +68 -0
- package/ui/styles/components/_nav-tree.scss +80 -0
- package/ui/styles/components/_referenced-items.scss +69 -0
- package/ui/styles/components/_sub-navigation.scss +208 -0
- package/ui/styles/components/_swoosh.scss +50 -0
- package/ui/styles/components/_timeline.scss +350 -0
- package/ui/styles/components/_work.scss +71 -0
- package/ui/styles/components/header/_header.scss +294 -0
- package/ui/styles/components/header/_logo.scss +99 -0
- package/ui/styles/components/header/_navbar.scss +143 -0
- package/ui/styles/components/header/index.scss +3 -0
- package/ui/styles/components/iiif/_image.scss +32 -0
- package/ui/styles/components/iiif/_slider.scss +5 -0
- package/ui/styles/components/iiif/index.scss +2 -0
- package/ui/styles/components/index.scss +25 -0
- package/ui/styles/components/modal/_modal.scss +130 -0
- package/ui/styles/components/modal/index.scss +1 -0
- package/ui/styles/components/search/_filters.scss +259 -0
- package/ui/styles/components/search/_form.scss +187 -0
- package/ui/styles/components/search/_panel.scss +7 -0
- package/ui/styles/components/search/_results.scss +241 -0
- package/ui/styles/components/search/_tabs.scss +60 -0
- package/ui/styles/components/search/index.scss +4 -0
- package/ui/styles/index.css +5007 -0
- package/ui/styles/index.scss +11 -0
- package/ui/styles/settings/_breakpoints.scss +7 -0
- package/ui/styles/settings/_focus.scss +15 -0
- package/ui/styles/settings/_tokens.scss +170 -0
- package/ui/styles/settings/_type.scss +126 -0
- package/ui/styles/version-parity.css +116 -0
- package/ui/tailwind-canopy-iiif-plugin.js +39 -0
- package/ui/tailwind-canopy-iiif-preset.js +32 -0
- package/ui/tailwind-config.d.ts +29 -0
- package/ui/tailwind-config.js +159 -0
- package/ui/tailwind-default.config.mjs +4 -0
- package/ui/theme-color.js +130 -0
- package/ui/theme-palette.js +337 -0
- package/ui/theme.js +153 -0
- package/README.md +0 -3
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The search engine for the header's suggestions, as `site/scripts/canopy-search-engine.js`.
|
|
2
|
+
// `search-form-runtime.js` loads it the first time a visitor searches from the header, so
|
|
3
|
+
// no page pays for FlexSearch or the index until then. The search page bundles the same
|
|
4
|
+
// modules into its own runtime (`search-app.jsx`).
|
|
5
|
+
//
|
|
6
|
+
// window.CanopySearchEngine.open() resolves to { records, search(query, limit) }, the
|
|
7
|
+
// records the query finds, best first.
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
const FlexSearch = require('flexsearch');
|
|
12
|
+
const { createEngine } = require('./engine');
|
|
13
|
+
const { loadSearchData } = require('./data');
|
|
14
|
+
const { openIndex } = require('./index-cache');
|
|
15
|
+
|
|
16
|
+
let opened = null;
|
|
17
|
+
|
|
18
|
+
function open() {
|
|
19
|
+
if (!opened) {
|
|
20
|
+
opened = (async () => {
|
|
21
|
+
const engine = createEngine(FlexSearch);
|
|
22
|
+
const { records, version } = await loadSearchData();
|
|
23
|
+
const { index } = await openIndex({ engine, records, version });
|
|
24
|
+
return {
|
|
25
|
+
records,
|
|
26
|
+
search(query, limit) {
|
|
27
|
+
return engine
|
|
28
|
+
.search(index, query, limit)
|
|
29
|
+
.map((position) => records[position])
|
|
30
|
+
.filter(Boolean);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
})();
|
|
34
|
+
// A failure (offline, say) is tried again the next time.
|
|
35
|
+
opened.catch(() => {
|
|
36
|
+
opened = null;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return opened;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof window !== 'undefined') window.CanopySearchEngine = { open };
|
|
43
|
+
|
|
44
|
+
module.exports = { open };
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
// The header's search field (`SearchPanel`): suggestions under it as a visitor types, and
|
|
2
|
+
// on the search page, the page's own query. The suggestions come from the same engine as
|
|
3
|
+
// the search page's results (`search-engine-runtime.js`), loaded the first time the field
|
|
4
|
+
// is used, so a page that is not searched loads neither it nor the index.
|
|
5
|
+
//
|
|
6
|
+
// A visitor reaches the suggestions with the down arrow or Tab, and goes back with the up
|
|
7
|
+
// arrow or Escape. Each group (Works, Pages) is a named group. How many there are is said
|
|
8
|
+
// in a status message, once typing pauses (4.1.3); the words come from `locale.yml`.
|
|
9
|
+
|
|
10
|
+
function ready(fn) {
|
|
11
|
+
if (document.readyState === 'loading') {
|
|
12
|
+
document.addEventListener('DOMContentLoaded', fn, { once: true });
|
|
13
|
+
} else {
|
|
14
|
+
fn();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseProps(el) {
|
|
19
|
+
try {
|
|
20
|
+
const script = el.querySelector('script[type="application/json"]');
|
|
21
|
+
if (script) return JSON.parse(script.textContent || '{}');
|
|
22
|
+
} catch (_) {}
|
|
23
|
+
return {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getBasePath() {
|
|
27
|
+
try {
|
|
28
|
+
const raw = window && window.CANOPY_BASE_PATH ? String(window.CANOPY_BASE_PATH) : '';
|
|
29
|
+
if (!raw) return '';
|
|
30
|
+
return raw.endsWith('/') ? raw.slice(0, -1) : raw;
|
|
31
|
+
} catch (_) {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The engine sits beside this script; its `?v=` changes with every build, as this one's.
|
|
37
|
+
const ENGINE_SRC = (() => {
|
|
38
|
+
try {
|
|
39
|
+
const own = document.currentScript && document.currentScript.src;
|
|
40
|
+
if (!own) return '';
|
|
41
|
+
const url = new URL('canopy-search-engine.js', own);
|
|
42
|
+
const version = new URL(own).searchParams.get('v');
|
|
43
|
+
if (version) url.searchParams.set('v', version);
|
|
44
|
+
return url.href;
|
|
45
|
+
} catch (_) {
|
|
46
|
+
return '';
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
|
|
50
|
+
let enginePromise = null;
|
|
51
|
+
function loadEngine() {
|
|
52
|
+
if (window.CanopySearchEngine) return Promise.resolve(window.CanopySearchEngine);
|
|
53
|
+
if (!enginePromise) {
|
|
54
|
+
enginePromise = new Promise((resolve, reject) => {
|
|
55
|
+
const script = document.createElement('script');
|
|
56
|
+
script.src = ENGINE_SRC || `${getBasePath()}/scripts/canopy-search-engine.js`;
|
|
57
|
+
script.async = true;
|
|
58
|
+
script.onload = () =>
|
|
59
|
+
window.CanopySearchEngine
|
|
60
|
+
? resolve(window.CanopySearchEngine)
|
|
61
|
+
: reject(new Error('the search engine did not start'));
|
|
62
|
+
script.onerror = () => reject(new Error('the search engine did not load'));
|
|
63
|
+
document.head.appendChild(script);
|
|
64
|
+
});
|
|
65
|
+
enginePromise.catch(() => {
|
|
66
|
+
enginePromise = null;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return enginePromise;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function openEngine() {
|
|
73
|
+
return loadEngine().then((api) => api.open());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function message(path, fallback, replacements) {
|
|
77
|
+
let value;
|
|
78
|
+
try {
|
|
79
|
+
value = path.split('.').reduce((node, key) => (node == null ? node : node[key]), window.CANOPY_LOCALE_MESSAGES);
|
|
80
|
+
} catch (_) {
|
|
81
|
+
value = undefined;
|
|
82
|
+
}
|
|
83
|
+
const template = typeof value === 'string' ? value : fallback;
|
|
84
|
+
return template.replace(/\{([^}]+)\}/g, (match, key) =>
|
|
85
|
+
replacements && replacements[key] != null ? String(replacements[key]) : match,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function groupLabel(type) {
|
|
90
|
+
const key = String(type || '').toLowerCase();
|
|
91
|
+
if (key === 'work') return message('common.types.work', 'Works');
|
|
92
|
+
if (key === 'page') return message('common.types.page', 'Pages');
|
|
93
|
+
if (key === 'docs') return message('common.types.docs', 'Docs');
|
|
94
|
+
return key ? key.charAt(0).toUpperCase() + key.slice(1) : '';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function countMessage(count) {
|
|
98
|
+
const noun =
|
|
99
|
+
count === 1
|
|
100
|
+
? message('common.nouns.suggestion', 'suggestion')
|
|
101
|
+
: message('common.nouns.suggestions', 'suggestions');
|
|
102
|
+
if (!count) return message('common.statuses.empty_short', 'No {content}', { content: noun });
|
|
103
|
+
return message('common.phrases.count_content', '{count} {content}', { count, content: noun });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// The search page's runtime marks its page (`search-app.jsx`), whatever its route: there
|
|
107
|
+
// the field drives the page's results rather than suggestions.
|
|
108
|
+
function isOnSearchPage() {
|
|
109
|
+
try {
|
|
110
|
+
if (window.CanopySearchPage) return true;
|
|
111
|
+
const base = getBasePath();
|
|
112
|
+
let path = String(location.pathname || '');
|
|
113
|
+
if (base && path.startsWith(base)) path = path.slice(base.length);
|
|
114
|
+
if (path.endsWith('/')) path = path.slice(0, -1);
|
|
115
|
+
return path === '/search' || path === '/search/index.html';
|
|
116
|
+
} catch (_) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function hidePanel(panel) {
|
|
122
|
+
if (!panel) return;
|
|
123
|
+
panel.classList.add('is-empty');
|
|
124
|
+
panel.setAttribute('hidden', 'hidden');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function showPanel(panel) {
|
|
128
|
+
if (!panel) return;
|
|
129
|
+
panel.classList.remove('is-empty');
|
|
130
|
+
panel.removeAttribute('hidden');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function getItems(list) {
|
|
134
|
+
try {
|
|
135
|
+
return Array.prototype.slice.call(list.querySelectorAll('[data-canopy-item]'));
|
|
136
|
+
} catch (_) {
|
|
137
|
+
return [];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function renderList(list, records, groupOrder, idPrefix) {
|
|
142
|
+
list.innerHTML = '';
|
|
143
|
+
const panel = list.closest('[data-canopy-search-form-panel]');
|
|
144
|
+
if (!records.length) {
|
|
145
|
+
hidePanel(panel);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
showPanel(panel);
|
|
149
|
+
const groups = new Map();
|
|
150
|
+
records.forEach((record) => {
|
|
151
|
+
const type = String((record && record.type) || 'page');
|
|
152
|
+
if (!groups.has(type)) groups.set(type, []);
|
|
153
|
+
groups.get(type).push(record);
|
|
154
|
+
});
|
|
155
|
+
const desiredOrder = Array.isArray(groupOrder) ? groupOrder : [];
|
|
156
|
+
const orderedKeys = [
|
|
157
|
+
...desiredOrder.filter((key) => groups.has(key)),
|
|
158
|
+
...Array.from(groups.keys()).filter((key) => !desiredOrder.includes(key)),
|
|
159
|
+
];
|
|
160
|
+
orderedKeys.forEach((key, groupIndex) => {
|
|
161
|
+
const group = document.createElement('div');
|
|
162
|
+
group.className = 'canopy-search-teaser__group';
|
|
163
|
+
group.setAttribute('role', 'group');
|
|
164
|
+
const header = document.createElement('div');
|
|
165
|
+
header.className = 'canopy-search-teaser__label';
|
|
166
|
+
header.id = `${idPrefix}-group-${groupIndex}`;
|
|
167
|
+
header.textContent = groupLabel(key);
|
|
168
|
+
group.setAttribute('aria-labelledby', header.id);
|
|
169
|
+
group.appendChild(header);
|
|
170
|
+
(groups.get(key) || []).forEach((record) => {
|
|
171
|
+
const item = document.createElement('a');
|
|
172
|
+
item.setAttribute('data-canopy-item', '');
|
|
173
|
+
item.href = String((record && record.href) || '');
|
|
174
|
+
item.className = 'canopy-card canopy-card--teaser canopy-search-teaser__item canopy-teaser-card';
|
|
175
|
+
|
|
176
|
+
if (record && record.thumbnail) {
|
|
177
|
+
const media = document.createElement('div');
|
|
178
|
+
media.className = 'canopy-search-teaser__thumb';
|
|
179
|
+
const img = document.createElement('img');
|
|
180
|
+
img.src = record.thumbnail;
|
|
181
|
+
img.alt = '';
|
|
182
|
+
img.loading = 'lazy';
|
|
183
|
+
img.className = 'canopy-search-teaser__thumb-img';
|
|
184
|
+
media.appendChild(img);
|
|
185
|
+
item.appendChild(media);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const textWrap = document.createElement('div');
|
|
189
|
+
textWrap.className = 'canopy-search-teaser__text';
|
|
190
|
+
const title = document.createElement('span');
|
|
191
|
+
title.textContent = record.title || record.href || '';
|
|
192
|
+
title.className = 'canopy-search-teaser__title';
|
|
193
|
+
textWrap.appendChild(title);
|
|
194
|
+
const meta = Array.isArray(record && record.metadata) ? record.metadata : [];
|
|
195
|
+
if (meta.length) {
|
|
196
|
+
const metaLine = document.createElement('span');
|
|
197
|
+
metaLine.textContent = meta.slice(0, 2).join(' • ');
|
|
198
|
+
metaLine.className = 'canopy-search-teaser__meta';
|
|
199
|
+
textWrap.appendChild(metaLine);
|
|
200
|
+
}
|
|
201
|
+
item.appendChild(textWrap);
|
|
202
|
+
|
|
203
|
+
item.onfocus = () => {
|
|
204
|
+
try {
|
|
205
|
+
item.scrollIntoView({ block: 'nearest' });
|
|
206
|
+
} catch (_) {}
|
|
207
|
+
};
|
|
208
|
+
group.appendChild(item);
|
|
209
|
+
});
|
|
210
|
+
list.appendChild(group);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function focusFirst(list) {
|
|
215
|
+
const items = getItems(list);
|
|
216
|
+
if (items.length) items[0].focus();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function focusLast(list, resetTarget) {
|
|
220
|
+
const items = getItems(list);
|
|
221
|
+
if (!items.length) {
|
|
222
|
+
if (resetTarget && resetTarget.focus) resetTarget.focus();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
items[items.length - 1].focus();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function bindKeyboardNavigation({ input, list, panel, ensureResults }) {
|
|
229
|
+
// The arrows reopen suggestions Escape closed, for the same query, before moving in.
|
|
230
|
+
const ensure = () => {
|
|
231
|
+
if (getItems(list).length && String(input.value || '').trim()) {
|
|
232
|
+
showPanel(panel);
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
if (typeof ensureResults === 'function') ensureResults();
|
|
236
|
+
return false;
|
|
237
|
+
};
|
|
238
|
+
input.addEventListener('keydown', (event) => {
|
|
239
|
+
if (event.key === 'ArrowDown') {
|
|
240
|
+
event.preventDefault();
|
|
241
|
+
if (!ensure()) return;
|
|
242
|
+
focusFirst(list);
|
|
243
|
+
} else if (event.key === 'ArrowUp') {
|
|
244
|
+
event.preventDefault();
|
|
245
|
+
if (!ensure()) return;
|
|
246
|
+
focusLast(list, input);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
list.addEventListener('keydown', (event) => {
|
|
251
|
+
const current = event.target && event.target.closest && event.target.closest('[data-canopy-item]');
|
|
252
|
+
if (!current) return;
|
|
253
|
+
if (event.key === 'ArrowDown') {
|
|
254
|
+
event.preventDefault();
|
|
255
|
+
const items = getItems(list);
|
|
256
|
+
const idx = items.indexOf(current);
|
|
257
|
+
const next = items[Math.min(items.length - 1, idx + 1)] || current;
|
|
258
|
+
next.focus();
|
|
259
|
+
} else if (event.key === 'ArrowUp') {
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
const items = getItems(list);
|
|
262
|
+
const idx = items.indexOf(current);
|
|
263
|
+
if (idx <= 0) {
|
|
264
|
+
if (input && input.focus) input.focus();
|
|
265
|
+
} else {
|
|
266
|
+
(items[idx - 1] || current).focus();
|
|
267
|
+
}
|
|
268
|
+
} else if (event.key === 'Escape') {
|
|
269
|
+
hidePanel(panel);
|
|
270
|
+
try {
|
|
271
|
+
if (input && input.focus) input.focus();
|
|
272
|
+
} catch (_) {}
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// A visually hidden status message beside the field, there from the start so that a
|
|
278
|
+
// screen reader hears it change.
|
|
279
|
+
function ensureStatus(host) {
|
|
280
|
+
let status = host.querySelector('[data-canopy-search-form-status]');
|
|
281
|
+
if (!status) {
|
|
282
|
+
status = document.createElement('div');
|
|
283
|
+
status.setAttribute('data-canopy-search-form-status', '');
|
|
284
|
+
status.setAttribute('role', 'status');
|
|
285
|
+
status.className = 'sr-only';
|
|
286
|
+
host.appendChild(status);
|
|
287
|
+
}
|
|
288
|
+
return status;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Long enough that a screen reader hears the count once typing pauses, not every key.
|
|
292
|
+
const ANNOUNCE_DELAY = 800;
|
|
293
|
+
|
|
294
|
+
let hostCount = 0;
|
|
295
|
+
|
|
296
|
+
async function attachSearchForm(host) {
|
|
297
|
+
const config = parseProps(host) || {};
|
|
298
|
+
const maxResults = Number(config.maxResults || 8) || 8;
|
|
299
|
+
const groupOrder = Array.isArray(config.groupOrder) ? config.groupOrder : ['work', 'page'];
|
|
300
|
+
const hotkey = typeof config.hotkey === 'string' ? config.hotkey : '';
|
|
301
|
+
const onSearchPage = isOnSearchPage();
|
|
302
|
+
hostCount += 1;
|
|
303
|
+
const idPrefix = `canopy-search-suggestions-${hostCount}`;
|
|
304
|
+
|
|
305
|
+
const panel = host.querySelector('[data-canopy-search-form-panel]');
|
|
306
|
+
if (!panel) return;
|
|
307
|
+
|
|
308
|
+
if (!onSearchPage) {
|
|
309
|
+
const wrapper = host.querySelector('.relative');
|
|
310
|
+
if (wrapper) wrapper.setAttribute('data-canopy-search-form-auto', '1');
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (onSearchPage) hidePanel(panel);
|
|
314
|
+
|
|
315
|
+
const list = panel.querySelector('#cplist') || panel.querySelector('.canopy-search-teaser__list');
|
|
316
|
+
if (!list) return;
|
|
317
|
+
|
|
318
|
+
const input = host.querySelector('[data-canopy-search-form-input]');
|
|
319
|
+
if (!input) return;
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
const qp = new URLSearchParams(location.search || '').get('q');
|
|
323
|
+
if (qp) input.value = qp;
|
|
324
|
+
} catch (_) {}
|
|
325
|
+
|
|
326
|
+
const status = onSearchPage ? null : ensureStatus(host);
|
|
327
|
+
let statusTimer = null;
|
|
328
|
+
function announce(text) {
|
|
329
|
+
if (!status) return;
|
|
330
|
+
clearTimeout(statusTimer);
|
|
331
|
+
statusTimer = setTimeout(() => {
|
|
332
|
+
if (status.textContent !== text) status.textContent = text;
|
|
333
|
+
}, ANNOUNCE_DELAY);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
let sequence = 0;
|
|
337
|
+
async function filterAndShow(query) {
|
|
338
|
+
const mine = (sequence += 1);
|
|
339
|
+
const q = String(query || '').trim();
|
|
340
|
+
if (!q) {
|
|
341
|
+
list.innerHTML = '';
|
|
342
|
+
hidePanel(panel);
|
|
343
|
+
announce('');
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
let engine;
|
|
347
|
+
try {
|
|
348
|
+
engine = await openEngine();
|
|
349
|
+
} catch (error) {
|
|
350
|
+
try {
|
|
351
|
+
console.warn('[canopy][search-form] no suggestions:', error && (error.message || error));
|
|
352
|
+
} catch (_) {}
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
// A later keystroke has its own search.
|
|
356
|
+
if (mine !== sequence) return;
|
|
357
|
+
const found = engine.search(q, maxResults);
|
|
358
|
+
renderList(list, found, groupOrder, idPrefix);
|
|
359
|
+
announce(countMessage(found.length));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
input.addEventListener('input', () => {
|
|
363
|
+
if (onSearchPage) {
|
|
364
|
+
try {
|
|
365
|
+
window.dispatchEvent(new CustomEvent('canopy:search:setQuery', { detail: { query: input.value || '' } }));
|
|
366
|
+
} catch (_) {}
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
filterAndShow(input.value || '');
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
bindKeyboardNavigation({
|
|
373
|
+
input,
|
|
374
|
+
list,
|
|
375
|
+
panel,
|
|
376
|
+
ensureResults: () => {
|
|
377
|
+
filterAndShow(input.value || '');
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
document.addEventListener('keydown', (event) => {
|
|
382
|
+
if (event.key === 'Escape') hidePanel(panel);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
document.addEventListener('mousedown', (event) => {
|
|
386
|
+
try {
|
|
387
|
+
if (!panel.contains(event.target) && !host.contains(event.target)) hidePanel(panel);
|
|
388
|
+
} catch (_) {}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
if (hotkey) {
|
|
392
|
+
// The shortcut's hint in the field, in the platform's words, and the same for a reader.
|
|
393
|
+
try {
|
|
394
|
+
const mac = /Mac|iPhone|iPad|iPod/i.test(
|
|
395
|
+
(navigator.userAgentData && navigator.userAgentData.platform) || navigator.platform || navigator.userAgent || '',
|
|
396
|
+
);
|
|
397
|
+
const hint = host.querySelector('[data-canopy-search-hotkey]');
|
|
398
|
+
if (hint) {
|
|
399
|
+
hint.textContent = mac ? '⌘K' : 'Ctrl K';
|
|
400
|
+
hint.hidden = false;
|
|
401
|
+
}
|
|
402
|
+
input.setAttribute('aria-keyshortcuts', mac ? 'Meta+K' : 'Control+K');
|
|
403
|
+
} catch (_) {}
|
|
404
|
+
document.addEventListener('keydown', (event) => {
|
|
405
|
+
try {
|
|
406
|
+
const want = hotkey.toLowerCase();
|
|
407
|
+
if (!want) return;
|
|
408
|
+
const isMod = event.metaKey || event.ctrlKey;
|
|
409
|
+
const isCmd =
|
|
410
|
+
(want === 'mod+k' || want === 'cmd+k' || want === 'ctrl+k') && (event.key === 'k' || event.key === 'K');
|
|
411
|
+
if (isCmd && isMod) {
|
|
412
|
+
event.preventDefault();
|
|
413
|
+
if (onSearchPage) {
|
|
414
|
+
try {
|
|
415
|
+
window.dispatchEvent(new CustomEvent('canopy:search:setQuery', { detail: { hotkey: true } }));
|
|
416
|
+
} catch (_) {}
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (input.focus) input.focus();
|
|
420
|
+
filterAndShow(input.value || '');
|
|
421
|
+
}
|
|
422
|
+
} catch (_) {}
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function openPanel() {
|
|
427
|
+
if (onSearchPage) {
|
|
428
|
+
try {
|
|
429
|
+
window.dispatchEvent(new CustomEvent('canopy:search:setQuery', { detail: {} }));
|
|
430
|
+
} catch (_) {}
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
// Start loading the engine and the index as the field takes focus, ahead of typing.
|
|
434
|
+
openEngine().catch(() => {});
|
|
435
|
+
filterAndShow(input.value || '');
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
host.addEventListener('click', (event) => {
|
|
439
|
+
const trigger = event.target && event.target.closest && event.target.closest('[data-canopy-search-form-trigger]');
|
|
440
|
+
if (!trigger) return;
|
|
441
|
+
const mode = (trigger.dataset && trigger.dataset.canopySearchFormTrigger) || '';
|
|
442
|
+
if (mode === 'submit' || mode === 'form') return;
|
|
443
|
+
event.preventDefault();
|
|
444
|
+
if (input.focus) input.focus();
|
|
445
|
+
openPanel();
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
input.addEventListener('focus', () => {
|
|
449
|
+
openPanel();
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
ready(() => {
|
|
454
|
+
const hosts = Array.from(document.querySelectorAll('[data-canopy-search-form]'));
|
|
455
|
+
hosts.forEach((host) => {
|
|
456
|
+
attachSearchForm(host).catch((err) => {
|
|
457
|
+
try {
|
|
458
|
+
console.warn('[canopy][search-form] failed to initialise', err && (err.message || err));
|
|
459
|
+
} catch (_) {}
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
});
|