Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit c948438

Browse files
committed
QuickJump: use JSX syntax
1 parent 0e094b8 commit c948438

File tree

6 files changed

+108
-121
lines changed

6 files changed

+108
-121
lines changed

haddock-api/resources/html/gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function buildJS(targetFileName, files) {
1717
}
1818

1919
gulp.task('build-js', function() {
20-
buildJS('quick-jump.min.js', ['./js-src/quick-jump.ts']);
20+
buildJS('quick-jump.min.js', ['./js-src/quick-jump.tsx']);
2121
buildJS('haddock-bundle.min.js', ['./js-src/init.ts']);
2222
});
2323

haddock-api/resources/html/haddock-bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

haddock-api/resources/html/js-src/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ function onDomReady(callback: () => void) {
55
if (document.readyState === 'interactive') {
66
callback();
77
} else {
8-
document.addEventListener('readystatechange', function () {
8+
document.addEventListener('readystatechange', () => {
99
if (document.readyState === 'interactive') {
1010
callback();
1111
}
1212
});
1313
}
1414
}
1515

16-
onDomReady(function() {
16+
onDomReady(() => {
1717
util.addStyleMenu();
1818
util.resetStyle();
1919
util.restoreCollapsed();

haddock-api/resources/html/js-src/quick-jump.ts renamed to haddock-api/resources/html/js-src/quick-jump.tsx

Lines changed: 102 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,16 @@ class PageMenuButton extends Component<any, any> {
4040
e.preventDefault();
4141
props.onClick();
4242
}
43-
44-
return h('li', {},
45-
h('a', { href: '#', onClick: onClick }, props.title)
46-
);
43+
return <li><a href="#" onClick={onClick}>{props.title}</a></li>;
4744
}
4845

4946
}
5047

5148
function addSearchPageMenuButton(action: () => void) {
52-
var pageMenu = document.querySelector('#page-menu') as HTMLUListElement;
53-
var dummy = document.createElement('li');
49+
const pageMenu = document.querySelector('#page-menu') as HTMLUListElement;
50+
const dummy = document.createElement('li');
5451
pageMenu.insertBefore(dummy, pageMenu.firstChild);
55-
preact.render(h(PageMenuButton, { onClick: action, title: "Quick Jump" }), pageMenu, dummy);
52+
preact.render(<PageMenuButton onClick={action} title="Quick Jump" />, pageMenu, dummy);
5653
}
5754

5855
// -------------------------------------------------------------------------- //
@@ -165,7 +162,7 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
165162
}
166163

167164
navigateLinks(change: number) {
168-
var newActiveLinkIndex = Math.max(-1, Math.min(this.linkIndex-1, this.state.activeLinkIndex + change));
165+
const newActiveLinkIndex = Math.max(-1, Math.min(this.linkIndex-1, this.state.activeLinkIndex + change));
169166
this.navigatedByKeyboard = true;
170167
this.setState({ activeLinkIndex: newActiveLinkIndex });
171168
}
@@ -176,19 +173,19 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
176173
}
177174

178175
updateResults() {
179-
var searchString = (this.input && this.input.value) || '';
176+
const searchString = (this.input && this.input.value) || '';
180177
const results: FuseResult<DocItem>[] = this.state.fuse.search(searchString);
181178

182-
var resultsByModule: { [name: string]: FuseResult<DocItem>[] } = {};
179+
const resultsByModule: { [name: string]: FuseResult<DocItem>[] } = {};
183180

184181
results.forEach((result) => {
185182
const moduleName = result.item.module;
186183
const resultsInModule = resultsByModule[moduleName] || (resultsByModule[moduleName] = []);
187184
resultsInModule.push(result);
188185
});
189186

190-
var moduleResults: ResultsInModule[] = [];
191-
for (var moduleName in resultsByModule) {
187+
const moduleResults: ResultsInModule[] = [];
188+
for (const moduleName in resultsByModule) {
192189
const items = resultsByModule[moduleName];
193190
let sumOfInverseScores = 0;
194191
items.forEach((item) => { sumOfInverseScores += 1/item.score; });
@@ -202,8 +199,8 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
202199

203200
componentDidUpdate() {
204201
if (this.searchResults && this.activeLink && this.navigatedByKeyboard) {
205-
var rect = this.activeLink.getClientRects()[0];
206-
var searchResultsTop = this.searchResults.getClientRects()[0].top;
202+
const rect = this.activeLink.getClientRects()[0];
203+
const searchResultsTop = this.searchResults.getClientRects()[0].top;
207204
if (rect.bottom > window.innerHeight) {
208205
this.searchResults.scrollTop += rect.bottom - window.innerHeight + 80;
209206
} else if (rect.top < searchResultsTop) {
@@ -229,7 +226,7 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
229226
const stopPropagation = (e: Event) => { e.stopPropagation(); };
230227

231228
const onMouseOver = (e: MouseEvent) => {
232-
var target: null | Element = e.target as Element;
229+
let target: null | Element = e.target as Element;
233230
while (target && typeof target.getAttribute === 'function') {
234231
const linkIndexString = target.getAttribute('data-link-index');
235232
if (typeof linkIndexString == 'string') {
@@ -243,37 +240,32 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
243240

244241
const items = take(10, state.moduleResults).map((r) => this.renderResultsInModule(r));
245242

246-
return (
247-
h('div', { id: 'search', class: state.isVisible ? '' : 'hidden' },
248-
h('div', { id: 'search-form', onMouseDown: stopPropagation },
249-
h('input', {
250-
placeholder: "Search in package by name",
251-
ref: (input) => { this.input = input as undefined | HTMLInputElement; },
252-
onFocus: this.show.bind(this),
253-
onClick: this.show.bind(this),
254-
onInput: this.updateResults.bind(this)
255-
})
256-
),
257-
h('div', {
258-
id: 'search-results',
259-
ref: (el) => { this.searchResults = el; },
260-
onMouseDown: stopPropagation, onMouseOver: onMouseOver
261-
},
262-
state.searchString === ''
263-
? [h(IntroMsg, {}), h(KeyboardShortcuts, {})]
264-
: items.length == 0
265-
? h(NoResultsMsg, { searchString: state.searchString })
266-
: h('ul', {}, items)
267-
)
268-
)
269-
);
243+
return <div id="search" class={state.isVisible ? '' : 'hidden'}>
244+
<div id="search-form" onMouseDown={stopPropagation}>
245+
<input
246+
placeholder="Search in package by name"
247+
ref={(input) => { this.input = input as undefined | HTMLInputElement; }}
248+
onFocus={this.show.bind(this)}
249+
onClick={this.show.bind(this)}
250+
onInput={this.updateResults.bind(this)}
251+
/>
252+
</div>
253+
<div id="search-results" ref={(el) => { this.searchResults = el; }}
254+
onMouseDown={stopPropagation} onMouseOver={onMouseOver}>
255+
{state.searchString === ''
256+
? [<IntroMsg />, <KeyboardShortcuts />]
257+
: items.length == 0
258+
? <NoResultsMsg searchString={state.searchString} />
259+
: <ul>{items}</ul>}
260+
</div>
261+
</div>;
270262
}
271263

272264
renderResultsInModule(resultsInModule: ResultsInModule): JSX.Element {
273-
var items = resultsInModule.items;
274-
var moduleName = resultsInModule.module;
275-
var showAll = this.state.expanded[moduleName] || items.length <= 10;
276-
var visibleItems = showAll ? items : take(8, items);
265+
const items = resultsInModule.items;
266+
const moduleName = resultsInModule.module;
267+
const showAll = this.state.expanded[moduleName] || items.length <= 10;
268+
const visibleItems = showAll ? items : take(8, items);
277269

278270
const expand = () => {
279271
const newExpanded = Object.assign({}, this.state.expanded);
@@ -282,24 +274,24 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
282274
};
283275

284276
const renderItem = (item: DocItem) => {
285-
return h('li', { class: 'search-result' },
286-
this.navigationLink(this.props.baseUrl + "/" + item.link, {},
287-
h(DocHtml, { html: item.display_html })
288-
)
289-
);
277+
return <li class="search-result">
278+
{this.navigationLink(this.props.baseUrl + "/" + item.link, {},
279+
<DocHtml html={item.display_html} />
280+
)}
281+
</li>;
290282
};
291283

292-
return h('li', { class: 'search-module' },
293-
h('h4', {}, moduleName),
294-
h('ul', {},
295-
visibleItems.map((item) => renderItem(item.item)),
296-
showAll
284+
return <li class="search-module">
285+
<h4>{moduleName}</h4>
286+
<ul>
287+
{visibleItems.map((item) => renderItem(item.item))}
288+
{showAll
297289
? []
298-
: h('li', { class: 'more-results' },
299-
this.actionLink(expand, {}, "show " + (items.length - visibleItems.length) + " more results from this module")
300-
)
301-
)
302-
);
290+
: <li class="more-results">
291+
{this.actionLink(expand, {}, "show " + (items.length - visibleItems.length) + " more results from this module")}
292+
</li>}
293+
</ul>
294+
</li>;
303295
}
304296

305297
navigationLink(href: string, attrs: JSX.HTMLAttributes&JSX.SVGAttributes&{[propName: string]: any}, ...children: (JSX.Element|JSX.Element[]|string)[]) {
@@ -315,13 +307,13 @@ class QuickJump extends Component<QuickJumpProps, QuickJumpState> {
315307
}
316308

317309
menuLink(attrs: JSX.HTMLAttributes&JSX.SVGAttributes&{[propName: string]: any}, action: () => void, ...children: (JSX.Element|JSX.Element[]|string)[]) {
318-
var linkIndex = this.linkIndex;
310+
const linkIndex = this.linkIndex;
319311
if (linkIndex === this.state.activeLinkIndex) {
320312
attrs['class'] = (attrs['class'] ? attrs['class'] + ' ' : '') + 'active-link';
321313
attrs.ref = (link?: Element) => { if (link) this.activeLink = link as HTMLAnchorElement; };
322314
this.activeLinkAction = action;
323315
}
324-
var newAttrs = Object.assign({ 'data-link-index': linkIndex }, attrs);
316+
const newAttrs = Object.assign({ 'data-link-index': linkIndex }, attrs);
325317
this.linkIndex += 1;
326318
return h('a', newAttrs, ...children);
327319
}
@@ -335,79 +327,73 @@ class DocHtml extends Component<{ html: string }, {}> {
335327
}
336328

337329
render(props: { html: string }) {
338-
return h('div', {dangerouslySetInnerHTML: {__html: props.html}});
330+
return <div dangerouslySetInnerHTML={{__html: props.html}} />;
339331
}
340332

341333
};
342334

343335
function KeyboardShortcuts() {
344-
return h('table', { class: 'keyboard-shortcuts' },
345-
h('tr', {},
346-
h('th', {}, "Key"),
347-
h('th', {}, "Shortcut")
348-
),
349-
h('tr', {},
350-
h('td', {}, h('span', { class: 'key' }, "s")),
351-
h('td', {}, "Open this search box")
352-
),
353-
h('tr', {},
354-
h('td', {}, h('span', { class: 'key' }, "esc")),
355-
h('td', {}, "Close this search box")
356-
),
357-
h('tr', {},
358-
h('td', {},
359-
h('span', { class: 'key' }, "↓"), ", ",
360-
h('span', { class: 'key' }, "ctrl"), "+",
361-
h('span', { class: 'key' }, "j")
362-
),
363-
h('td', {}, "Move down in search results")
364-
),
365-
h('tr', {},
366-
h('td', {},
367-
h('span', { class: 'key' }, "↑"), ", ",
368-
h('span', { class: 'key' }, "ctrl"), "+",
369-
h('span', { class: 'key' }, "k")
370-
),
371-
h('td', {}, "Move up in search results")
372-
),
373-
h('tr', {},
374-
h('td', {}, h('span', { class: 'key' }, "↵")),
375-
h('td', {}, "Go to active search result")
376-
)
377-
);
336+
return <table class="keyboard-shortcuts">
337+
<tr>
338+
<th>Key</th>
339+
<th>Shortcut</th>
340+
</tr>
341+
<tr>
342+
<td><span class="key">s</span></td>
343+
<td>Open this search box</td>
344+
</tr>
345+
<tr>
346+
<td><span class="key">esc</span></td>
347+
<td>Close this search box</td>
348+
</tr>
349+
<tr>
350+
<td>
351+
<span class="key"></span>,
352+
<span class="key">ctrl</span> + <span class="key">j</span>
353+
</td>
354+
<td>Move down in search results</td>
355+
</tr>
356+
<tr>
357+
<td>
358+
<span class="key"></span>,
359+
<span class="key">ctrl</span> + <span class="key">k</span>
360+
</td>
361+
<td>Move up in search results</td>
362+
</tr>
363+
<tr>
364+
<td><span class="key"></span></td>
365+
<td>Go to active search result</td>
366+
</tr>
367+
</table>;
378368
}
379369

380370
function IntroMsg() {
381-
return h('p', {},
382-
"You can find any exported type, constructor, class, function or pattern defined in this package by (approximate) name."
383-
);
371+
return <p>You can find any exported type, constructor, class, function or pattern defined in this package by (approximate) name.</p>;
384372
}
385373

386374
function NoResultsMsg(props: { searchString: string }) {
387-
var messages = [
388-
h('p', {},
389-
"Your search for '" + props.searchString + "' produced the following list of results: ",
390-
h('code', {}, '[]'),
391-
"."
392-
),
393-
h('p', {},
394-
h('code', {}, 'Nothing'),
395-
" matches your query for '" + props.searchString + "'."
396-
),
397-
h('p', {},
398-
h('code', {}, 'Left "no matches for \'' + props.searchString + '\'" :: Either String (NonEmpty SearchResult)')
399-
)
375+
const messages = [
376+
<p>
377+
Your search for '{props.searchString}' produced the following list of results: <code>[]</code>.
378+
</p>,
379+
<p>
380+
<code>Nothing</code> matches your query for '{props.searchString}'.
381+
</p>,
382+
<p>
383+
<code>
384+
Left "no matches for '{props.searchString}'" :: Either String (NonEmpty SearchResult)
385+
</code>
386+
</p>
400387
];
401388

402389
return messages[(props.searchString || 'a').charCodeAt(0) % messages.length];
403390
}
404391

405392
export function init(docBaseUrl?: string, showHide?: (action: () => void) => void) {
406-
const props = {
407-
baseUrl: docBaseUrl || ".",
408-
showHideTrigger: showHide || addSearchPageMenuButton
409-
};
410-
preact.render(h(QuickJump, props), document.body);
393+
preact.render(
394+
<QuickJump baseUrl={docBaseUrl || "."} showHideTrigger={showHide || addSearchPageMenuButton} />,
395+
document.body
396+
);
411397
}
412398

413399
// export to global object

haddock-api/resources/html/quick-jump.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

haddock-api/resources/html/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"module": "commonjs",
99
"target": "es5",
1010
"jsx": "react",
11+
"jsxFactory": "h",
1112
"lib": ["es2015", "dom"]
1213
},
1314
"include": [

0 commit comments

Comments
 (0)