| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
/* eslint-disable @typescript-eslint/explicit-function-return-type */ |
| 24 |
|
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
// --- ToolBars |
| 27 |
// -------------------------------------------------------------------------- |
| 28 |
|
| 29 |
/** |
| 30 |
@packageDocumentation |
| 31 |
@module dome/frame/toolbars |
| 32 |
*/ |
| 33 |
|
| 34 |
import React from 'react'; |
| 35 |
import { Event, find, usePromise } from 'dome'; |
| 36 |
import { SVG } from 'dome/controls/icons'; |
| 37 |
import { Label } from 'dome/controls/labels'; |
| 38 |
import { classes } from 'dome/misc/utils'; |
| 39 |
import './style.css'; |
| 40 |
|
| 41 |
// -------------------------------------------------------------------------- |
| 42 |
// --- ToolBar Container |
| 43 |
// -------------------------------------------------------------------------- |
| 44 |
|
| 45 |
export interface ToolBarProps { |
| 46 |
className?: string; |
| 47 |
style?: React.CSSProperties; |
| 48 |
children?: React.ReactNode; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
@class |
| 53 |
@summary Container for toolbar items. |
| 54 |
*/ |
| 55 |
export function ToolBar(props: ToolBarProps) { |
| 56 |
const { children } = props; |
| 57 |
const n = React.Children.count(children); |
| 58 |
if (n === 0) return null; |
| 59 |
const className = classes( |
| 60 |
'dome-xToolBar', |
| 61 |
'dome-color-frame', |
| 62 |
props.className, |
| 63 |
); |
| 64 |
return ( |
| 65 |
<div className={className} style={props.style}> |
| 66 |
<div className="dome-xToolBar-inset" /> |
| 67 |
{children} |
| 68 |
<div className="dome-xToolBar-inset" /> |
| 69 |
</div> |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
// -------------------------------------------------------------------------- |
| 74 |
// --- ToolBar Spaces |
| 75 |
// -------------------------------------------------------------------------- |
| 76 |
|
| 77 |
/** Fixed (tiny) space. */ |
| 78 |
export const Inset = (() => <div className="dome-xToolBar-inset" />); |
| 79 |
|
| 80 |
/** Fixed space. */ |
| 81 |
export const Space = (() => <div className="dome-xToolBar-space" />); |
| 82 |
|
| 83 |
/** Auto-extensible space. */ |
| 84 |
export const Filler = (() => <div className="dome-xToolBar-filler" />); |
| 85 |
|
| 86 |
/** Fixed space with vertical rule. */ |
| 87 |
export const Separator = () => ( |
| 88 |
<div className="dome-xToolBar-separator"> |
| 89 |
<div className="dome-xToolBar-vrule" /> |
| 90 |
</div> |
| 91 |
); |
| 92 |
|
| 93 |
const SELECT = 'dome-xToolBar-control dome-selected'; |
| 94 |
const BUTTON = 'dome-xToolBar-control dome-color-frame'; |
| 95 |
const KIND = (kind: undefined | string) => ( |
| 96 |
kind ? ` dome-xToolBar-${kind}` : '' |
| 97 |
); |
| 98 |
|
| 99 |
export type ButtonKind = |
| 100 |
| 'default' | 'cancel' | 'warning' | 'positive' | 'negative'; |
| 101 |
|
| 102 |
export interface ButtonProps<A> { |
| 103 |
/** Button icon, Cf. [gallery](gallery-icons.html). */ |
| 104 |
icon?: string; |
| 105 |
/** Button label. */ |
| 106 |
label?: string; |
| 107 |
/** Button tooltip text. */ |
| 108 |
title?: string; |
| 109 |
/** Button kind. */ |
| 110 |
kind?: ButtonKind; |
| 111 |
/** Button is displayed (default `true`). */ |
| 112 |
visible?: boolean; |
| 113 |
/** Button is hidden (default `false`). */ |
| 114 |
hidden?: boolean; |
| 115 |
/** Enabled State (default `true`). */ |
| 116 |
enabled?: boolean; |
| 117 |
/** Disabled State (default `false`). */ |
| 118 |
disabled?: boolean; |
| 119 |
/** Selection State (defaults to `false` or `selection` equal to `value`). */ |
| 120 |
selected?: boolean; |
| 121 |
/** Button's value. */ |
| 122 |
value?: A; |
| 123 |
/** Currently selected value. */ |
| 124 |
selection?: A; |
| 125 |
/** Selection callback. Receives the button's value. */ |
| 126 |
onClick?: (value: A | undefined) => void; |
| 127 |
/** Button contents */ |
| 128 |
children?: React.ReactNode; |
| 129 |
} |
| 130 |
|
| 131 |
/** Toolbar Button. */ |
| 132 |
export function Button<A = undefined>(props: ButtonProps<A>) { |
| 133 |
const { visible = true, hidden = false } = props; |
| 134 |
if (!visible || hidden) return null; |
| 135 |
const { enabled = true, disabled = false } = props; |
| 136 |
const { selected, value, selection, onClick } = props; |
| 137 |
const isSelected = selected !== undefined |
| 138 |
? selected |
| 139 |
: (value !== undefined && value === selection); |
| 140 |
return ( |
| 141 |
<button |
| 142 |
type="button" |
| 143 |
disabled={disabled || !enabled} |
| 144 |
className={isSelected ? SELECT : (BUTTON + KIND(props.kind))} |
| 145 |
onClick={onClick && (() => onClick(value))} |
| 146 |
title={props.title} |
| 147 |
> |
| 148 |
{props.icon && <SVG id={props.icon} />} |
| 149 |
{props.label && <label>{props.label}</label>} |
| 150 |
{props.children} |
| 151 |
</button> |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
export interface SwitchProps { |
| 156 |
/** Switch tooltip. */ |
| 157 |
title?: string; |
| 158 |
/** Additional class. */ |
| 159 |
className?: string; |
| 160 |
/** Additional style. */ |
| 161 |
style?: React.CSSProperties; |
| 162 |
/** Defaults to `true`. */ |
| 163 |
enabled?: boolean; |
| 164 |
/** Defaults to `false`. */ |
| 165 |
disabled?: boolean; |
| 166 |
/** Switch position. Defaults to 'left'. */ |
| 167 |
position?: 'left' | 'right'; |
| 168 |
/** Click callback. */ |
| 169 |
onChange?: (newPosition: 'left' | 'right') => void; |
| 170 |
} |
| 171 |
|
| 172 |
/** Toolbar Left/Right Switch. */ |
| 173 |
export function Switch(props: SwitchProps) { |
| 174 |
const { position, onChange } = props; |
| 175 |
const checked = position === 'right'; |
| 176 |
const { title, className, style } = props; |
| 177 |
const { enabled = true, disabled = false } = props; |
| 178 |
const callback = onChange && (() => onChange(checked ? 'left' : 'right')); |
| 179 |
if (disabled || !enabled) return null; |
| 180 |
return ( |
| 181 |
<label className={classes('dome-xSwitch', className)} style={style}> |
| 182 |
<input type={'checkbox'} checked={checked} onChange={callback} /> |
| 183 |
<span className={'dome-xSwitch-slider'} title={title} /> |
| 184 |
</label > |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
// -------------------------------------------------------------------------- |
| 189 |
// --- Selection Props |
| 190 |
// -------------------------------------------------------------------------- |
| 191 |
|
| 192 |
export interface SelectionProps<A> { |
| 193 |
/** Enabled Group (default `true`). */ |
| 194 |
enabled?: boolean; |
| 195 |
/** Disabled Group (default `false`). */ |
| 196 |
disabled?: boolean; |
| 197 |
/** Currently selected button. */ |
| 198 |
value?: A; |
| 199 |
/** Callback on clicked buttons. */ |
| 200 |
onChange?: (value: undefined | A) => void; |
| 201 |
/** Buttons array. */ |
| 202 |
children: React.ReactElement[]; |
| 203 |
} |
| 204 |
|
| 205 |
// -------------------------------------------------------------------------- |
| 206 |
// --- ToolBar Button Group |
| 207 |
// -------------------------------------------------------------------------- |
| 208 |
|
| 209 |
export interface ButtonGroupProps<A> extends SelectionProps<A> { |
| 210 |
className?: string; |
| 211 |
style?: React.CSSProperties; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
Toolbar Button Group. |
| 216 |
|
| 217 |
Properties of the button group are passed down the buttons of the group |
| 218 |
as appropriate defaults. |
| 219 |
*/ |
| 220 |
export function ButtonGroup<A>(props: ButtonGroupProps<A>) { |
| 221 |
const { children, value, onChange, enabled, disabled } = props; |
| 222 |
const baseProps: ButtonProps<A> = { |
| 223 |
enabled, |
| 224 |
disabled, |
| 225 |
selection: value, |
| 226 |
onClick: onChange, |
| 227 |
}; |
| 228 |
const className = classes('dome-xToolBar-group', props.className); |
| 229 |
return ( |
| 230 |
<div className={className} style={props.style}> |
| 231 |
{React.Children.map(children, (elt) => React.cloneElement( |
| 232 |
elt, |
| 233 |
{ ...baseProps, ...elt.props }, |
| 234 |
))} |
| 235 |
</div> |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
// -------------------------------------------------------------------------- |
| 240 |
// --- ToolBar Menu |
| 241 |
// -------------------------------------------------------------------------- |
| 242 |
|
| 243 |
/** Toolbar Selector Menu. |
| 244 |
|
| 245 |
Behaves likes a standard `<select>` element, except that callback directly |
| 246 |
receives the select value, not the entire event. |
| 247 |
The list of options shall be given with standard |
| 248 |
`<option value={...} label={...}>` elements. |
| 249 |
*/ |
| 250 |
export function Select(props: SelectionProps<string>) { |
| 251 |
const { enabled = true, disabled = false, onChange } = props; |
| 252 |
const callback = (evt: React.ChangeEvent<HTMLSelectElement>) => { |
| 253 |
if (onChange) onChange(evt.target.value); |
| 254 |
}; |
| 255 |
return ( |
| 256 |
<select |
| 257 |
className="dome-xToolBar-control dome-color-frame" |
| 258 |
value={props.value} |
| 259 |
disabled={disabled || !enabled} |
| 260 |
onChange={callback} |
| 261 |
> |
| 262 |
{props.children} |
| 263 |
</select> |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
// -------------------------------------------------------------------------- |
| 268 |
// --- ModalActionField necessary types |
| 269 |
// -------------------------------------------------------------------------- |
| 270 |
|
| 271 |
/** Description of a hint used to populate the suggestions. */ |
| 272 |
export interface Hint { |
| 273 |
id: string | number; |
| 274 |
icon?: string; |
| 275 |
label: string; |
| 276 |
title?: string; |
| 277 |
value(): void; |
| 278 |
rank?: number; |
| 279 |
} |
| 280 |
|
| 281 |
/** Total order on hints. */ |
| 282 |
export function byHint(a: Hint, b: Hint) { |
| 283 |
return (a.rank ?? 0) - (b.rank ?? 0); |
| 284 |
} |
| 285 |
|
| 286 |
/** Type alias for functions that build hints list from a pattern. */ |
| 287 |
export type HintsEvaluator = (pattern: string) => Promise<Hint[]>; |
| 288 |
|
| 289 |
/** Description of an action mode. */ |
| 290 |
export interface ActionMode { |
| 291 |
/** Mode tooltip title. */ |
| 292 |
title?: string; |
| 293 |
/** Mode tooltip label. */ |
| 294 |
label: string; |
| 295 |
/** Mode placeholder text. */ |
| 296 |
placeholder?: string; |
| 297 |
/** Icon displayed when the mode is selected. */ |
| 298 |
icon: string; |
| 299 |
/** CSS class for the mode section. */ |
| 300 |
className: string; |
| 301 |
/** Hints provider. */ |
| 302 |
hints: HintsEvaluator; |
| 303 |
/** Hint selection callback. Defaults to evaluate the <value> field. */ |
| 304 |
onHint?: (hint: Hint) => void; |
| 305 |
/** Action to perform when Enter is hit. Useful for modes without hints. */ |
| 306 |
onEnter?: (pattern: string) => void; |
| 307 |
/** Event that triggers a focus request. */ |
| 308 |
event?: Event<void>; |
| 309 |
} |
| 310 |
|
| 311 |
// -------------------------------------------------------------------------- |
| 312 |
// --- ModalActionField default mode: Search |
| 313 |
// -------------------------------------------------------------------------- |
| 314 |
|
| 315 |
const searchEvaluators = new Map<string, HintsEvaluator>(); |
| 316 |
|
| 317 |
// Updates to the new evaluator if the id is already registered |
| 318 |
export function registerSearchHints(id: string, search: HintsEvaluator) { |
| 319 |
searchEvaluators.set(id, search); |
| 320 |
} |
| 321 |
|
| 322 |
export function unregisterSearchHints(id: string) { |
| 323 |
searchEvaluators.delete(id); |
| 324 |
} |
| 325 |
|
| 326 |
async function searchHints(pattern: string) { |
| 327 |
if (pattern === '') return []; |
| 328 |
const promises = Array.from(searchEvaluators).map(([_id, E]) => E(pattern)); |
| 329 |
const hints = await Promise.all(promises); |
| 330 |
return hints.flat().sort(byHint); |
| 331 |
} |
| 332 |
|
| 333 |
const searchMode: ActionMode = { |
| 334 |
label: "Search", |
| 335 |
title: 'Search through the global definitions', |
| 336 |
placeholder: "Search…", |
| 337 |
icon: "SEARCH", |
| 338 |
className: 'dome-xToolBar-searchMode', |
| 339 |
hints: searchHints, |
| 340 |
event: find, |
| 341 |
}; |
| 342 |
|
| 343 |
// -------------------------------------------------------------------------- |
| 344 |
// --- ModalActionField mode button component |
| 345 |
// -------------------------------------------------------------------------- |
| 346 |
|
| 347 |
interface ModeButtonComponentProps { |
| 348 |
current: ActionMode; |
| 349 |
onClick: () => void; |
| 350 |
} |
| 351 |
|
| 352 |
function ModeButton(props: ModeButtonComponentProps) { |
| 353 |
const { current, onClick } = props; |
| 354 |
return ( |
| 355 |
<div |
| 356 |
className={classes("dome-xToolBar-modeSelection", current.className)} |
| 357 |
onClick={onClick} |
| 358 |
> |
| 359 |
<SVG |
| 360 |
className="dome-xToolBar-modeIcon" |
| 361 |
id={current.icon} |
| 362 |
offset={-1} |
| 363 |
/> |
| 364 |
</div> |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
// -------------------------------------------------------------------------- |
| 369 |
// --- ModalActionField suggestions component |
| 370 |
// -------------------------------------------------------------------------- |
| 371 |
|
| 372 |
interface SuggestionsProps { |
| 373 |
hints: Hint[]; |
| 374 |
onHint: (hint: Hint) => void; |
| 375 |
index: number; |
| 376 |
} |
| 377 |
|
| 378 |
function scrollToRef (r: null | HTMLLabelElement) { |
| 379 |
if (r) r.scrollIntoView({ block: 'nearest' }); |
| 380 |
} |
| 381 |
|
| 382 |
function Suggestions(props: SuggestionsProps) { |
| 383 |
const { hints, onHint, index } = props; |
| 384 |
|
| 385 |
// Computing the relevant suggestions. */ |
| 386 |
const suggestions = hints.map((h, k) => { |
| 387 |
const selected = k === index || hints.length === 1; |
| 388 |
const classSelected = selected && 'dome-xToolBar-searchindex'; |
| 389 |
const className = classes('dome-xToolBar-searchitem', classSelected); |
| 390 |
return ( |
| 391 |
<Label |
| 392 |
ref={selected ? scrollToRef : undefined} |
| 393 |
key={h.id} |
| 394 |
icon={h.icon} |
| 395 |
title={h.title} |
| 396 |
className={className} |
| 397 |
onClick={() => onHint(h)} |
| 398 |
> |
| 399 |
{h.label} |
| 400 |
</Label> |
| 401 |
); |
| 402 |
}); |
| 403 |
|
| 404 |
// Rendering the component. |
| 405 |
return ( |
| 406 |
<div |
| 407 |
style={{ visibility: suggestions.length > 0 ? 'visible' : 'hidden' }} |
| 408 |
className='dome-xToolBar-suggestions' |
| 409 |
onMouseDown={ (event) => event.preventDefault() } |
| 410 |
> |
| 411 |
{suggestions} |
| 412 |
</div> |
| 413 |
); |
| 414 |
} |
| 415 |
|
| 416 |
// -------------------------------------------------------------------------- |
| 417 |
// --- ModalActionField input field component |
| 418 |
// -------------------------------------------------------------------------- |
| 419 |
|
| 420 |
interface ActionInputProps { |
| 421 |
title?: string; |
| 422 |
placeholder?: string; |
| 423 |
hints: Hint[]; |
| 424 |
onHint: (hint: Hint) => void; |
| 425 |
onEnter?: (pattern: string) => void; |
| 426 |
index: number; |
| 427 |
setIndex: (n: number) => void; |
| 428 |
pattern: string; |
| 429 |
setPattern: (p: string) => void; |
| 430 |
inputRef: React.MutableRefObject<HTMLInputElement | null>; |
| 431 |
} |
| 432 |
|
| 433 |
function ActionInput(props: ActionInputProps) { |
| 434 |
const { title, placeholder, hints, onHint, onEnter } = props; |
| 435 |
const { index, setIndex, pattern, setPattern, inputRef } = props; |
| 436 |
|
| 437 |
// Blur Event |
| 438 |
const onBlur = () => { setPattern(''); setIndex(-1); }; |
| 439 |
|
| 440 |
// Key Up Events |
| 441 |
const onKeyUp = (evt: React.KeyboardEvent) => { |
| 442 |
const blur = () => inputRef.current?.blur(); |
| 443 |
switch (evt.key) { |
| 444 |
case 'Escape': |
| 445 |
blur(); |
| 446 |
break; |
| 447 |
case 'Enter': |
| 448 |
blur(); |
| 449 |
if (index >= 0 && index < hints.length) onHint(hints[index]); |
| 450 |
else if (hints.length === 1) onHint(hints[0]); |
| 451 |
else if (onEnter) onEnter(pattern); |
| 452 |
break; |
| 453 |
case 'ArrowUp': |
| 454 |
if (index < 0) setIndex(hints.length - 1); |
| 455 |
if (index > 0) setIndex(index - 1); |
| 456 |
break; |
| 457 |
case 'ArrowDown': |
| 458 |
if (index < 0 && 0 < hints.length) setIndex(0); |
| 459 |
if (0 <= index && index < hints.length - 1) setIndex(index + 1); |
| 460 |
break; |
| 461 |
} |
| 462 |
}; |
| 463 |
|
| 464 |
// Key Down Events. Disables the default behavior on ArrowUp and ArrowDown. |
| 465 |
const onKeyDown = (evt: React.KeyboardEvent) => { |
| 466 |
if (evt.key === 'ArrowUp' || evt.key === 'ArrowDown') evt.preventDefault(); |
| 467 |
}; |
| 468 |
|
| 469 |
// // Input Events |
| 470 |
const onChange = (evt: React.ChangeEvent<HTMLInputElement>) => { |
| 471 |
setIndex(-1); |
| 472 |
setPattern(evt.target.value); |
| 473 |
}; |
| 474 |
|
| 475 |
return ( |
| 476 |
<input |
| 477 |
type="search" |
| 478 |
placeholder={placeholder} |
| 479 |
ref={inputRef} |
| 480 |
title={title} |
| 481 |
value={pattern} |
| 482 |
onKeyUp={onKeyUp} |
| 483 |
onKeyDown={onKeyDown} |
| 484 |
onChange={onChange} |
| 485 |
onBlur={onBlur} |
| 486 |
/> |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
// -------------------------------------------------------------------------- |
| 491 |
// --- ModalActionField component |
| 492 |
// -------------------------------------------------------------------------- |
| 493 |
|
| 494 |
export const RegisterMode: Event<ActionMode> = |
| 495 |
new Event('dome.actionmode.register'); |
| 496 |
|
| 497 |
export const UnregisterMode: Event<ActionMode> = |
| 498 |
new Event('dome.actionmode.unregister'); |
| 499 |
|
| 500 |
export function ModalActionField() { |
| 501 |
|
| 502 |
// Internal state of the component along with useful functions acting on it. |
| 503 |
const inputRef = React.useRef<HTMLInputElement | null>(null); |
| 504 |
const [index, setIndex] = React.useState(-1); |
| 505 |
const [pattern, setPattern] = React.useState(''); |
| 506 |
const [current, onModeChange] = React.useState<ActionMode>(searchMode); |
| 507 |
const focus = () => inputRef.current?.focus(); |
| 508 |
const changeMode = (m: ActionMode) => () => { onModeChange(m); focus(); }; |
| 509 |
const toDefault = () => onModeChange(searchMode); |
| 510 |
const reset = (m: ActionMode) => { if (current === m) toDefault(); }; |
| 511 |
|
| 512 |
// Set of all modes currently active. We populate it by reacting to |
| 513 |
// RegisterMode and UnregisterMode events. We also activate the mode event if |
| 514 |
// available. Everything is cleaned when the component is unmounted. |
| 515 |
const [allModes] = React.useState<Set<ActionMode>>(new Set()); |
| 516 |
React.useEffect(() => { |
| 517 |
const on = (m: ActionMode) => m.event?.on(changeMode(m)); |
| 518 |
const register = (m: ActionMode) => { allModes.add(m); on(m); }; |
| 519 |
const off = (m: ActionMode) => m.event?.off(changeMode(m)); |
| 520 |
const remove = (m: ActionMode) => { allModes.delete(m); off(m); reset(m); }; |
| 521 |
RegisterMode.on(register); UnregisterMode.on(remove); |
| 522 |
return () => { RegisterMode.off(register); UnregisterMode.off(remove); }; |
| 523 |
}); |
| 524 |
|
| 525 |
// Register the search mode. |
| 526 |
React.useEffect(() => RegisterMode.emit(searchMode)); |
| 527 |
|
| 528 |
// Compute the hints for the current mode. |
| 529 |
const { hints, onHint = (h) => h.value(), onEnter } = current; |
| 530 |
const hintsPromise = React.useMemo(() => hints(pattern), [pattern, hints]); |
| 531 |
const { result = [] } = usePromise(hintsPromise); |
| 532 |
|
| 533 |
// Auxiliary function that build a Hint from an ActionMode. |
| 534 |
const modeToHint = (mode: ActionMode) => { |
| 535 |
const { label, title = '', icon } = mode; |
| 536 |
const id = 'ActionMode-' + title + '-' + icon; |
| 537 |
const value = () => { onModeChange(mode); }; |
| 538 |
return { id, icon, label, title, value, rank: -1000 }; |
| 539 |
}; |
| 540 |
|
| 541 |
// Hints provider for the mode of all modes. |
| 542 |
const modesHints = React.useCallback((input: string) => { |
| 543 |
const p = input.toLowerCase(); |
| 544 |
const fit = (m: ActionMode) => m.label.toLowerCase().includes(p); |
| 545 |
return Promise.resolve(Array.from(allModes).filter(fit).map(modeToHint)); |
| 546 |
}, [allModes]); |
| 547 |
|
| 548 |
// Build the mode of all modes. This special mode is activated when clicking |
| 549 |
// on the current mode icon and allows to change the current mode, displaying |
| 550 |
// a list of all available modes as hints. |
| 551 |
const modesMode = React.useMemo(() => { |
| 552 |
const label = "Mode selection"; |
| 553 |
const placeholder = "Search mode"; |
| 554 |
const icon = "TUNINGS"; |
| 555 |
const className = 'dome-xToolBar-modeOfModes'; |
| 556 |
return { label, placeholder, icon, className, hints: modesHints }; |
| 557 |
}, [modesHints]); |
| 558 |
|
| 559 |
// Build a new search engine for the search mode, adding available modes to |
| 560 |
// the possible search hints. |
| 561 |
const searchModeHints = React.useCallback(async (input: string) => { |
| 562 |
const hs = await modesMode.hints(input); |
| 563 |
const notCurrent = (h: Hint) => !(h.label.includes(current.label)); |
| 564 |
return hs.filter(notCurrent); |
| 565 |
}, [current.label, modesMode]); |
| 566 |
|
| 567 |
// Register the new search engine. |
| 568 |
React.useEffect(() => { |
| 569 |
registerSearchHints('ModesMode', searchModeHints); |
| 570 |
return () => unregisterSearchHints('ModesMode'); |
| 571 |
}, [searchModeHints]); |
| 572 |
|
| 573 |
// Build the component. |
| 574 |
const { title, placeholder } = current; |
| 575 |
const handleModeClick = changeMode(modesMode); |
| 576 |
const onBlur = () => reset(modesMode); |
| 577 |
return ( |
| 578 |
<div className="dome-xToolBar-actionComponent" onBlur={onBlur}> |
| 579 |
<div className="dome-xToolBar-actionField"> |
| 580 |
<ModeButton current={current} onClick={handleModeClick} /> |
| 581 |
<ActionInput |
| 582 |
title={title} |
| 583 |
placeholder={placeholder} |
| 584 |
hints={result} |
| 585 |
onHint={onHint} |
| 586 |
onEnter={onEnter} |
| 587 |
index={index} |
| 588 |
setIndex={setIndex} |
| 589 |
pattern={pattern} |
| 590 |
setPattern={setPattern} |
| 591 |
inputRef={inputRef} |
| 592 |
/> |
| 593 |
</div> |
| 594 |
<Suggestions hints={result} onHint={onHint} index={index} /> |
| 595 |
</div> |
| 596 |
); |
| 597 |
} |
| 598 |
|
| 599 |
// -------------------------------------------------------------------------- |