-
Notifications
You must be signed in to change notification settings - Fork 2
/
Keyins.ts
70 lines (66 loc) · 2.32 KB
/
Keyins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tools
*/
import type { ToolList } from "@itwin/core-frontend";
import type { IMatch } from "../utils/matchesWords";
/**
* Defines a keyin entry to show/filter in UI
* @public
*/
export interface KeyinEntry {
/** string that matched a filter string */
value: string;
/** define array of start and end positions of filter matches. */
matches?: IMatch[];
/** true if entry was loaded from key-in history */
isHistory?: boolean;
}
/**
* Controls whether localized and/or non-localized key-in strings appear in a KeyinField's auto-completion list.
* @public
*/
export enum KeyinFieldLocalization {
/** Include only non-localized key-in strings. */
NonLocalized,
/** Include only localized key-in strings. */
Localized,
/** Include localized and non-localized strings for each key-in. */
Both,
}
/**
* Parses a given ToolList for its tools keyins, filtering based on a KeyinFieldLocalization value.
* @internal
*/
export function getKeyinsFromToolList(
toolList: ToolList,
localizedKeyinPreference: KeyinFieldLocalization = KeyinFieldLocalization.NonLocalized
): KeyinEntry[] {
const toolKeyins: KeyinEntry[] = [];
for (const tool of toolList) {
switch (localizedKeyinPreference) {
case KeyinFieldLocalization.Localized:
toolKeyins.push({ value: tool.keyin });
break;
case KeyinFieldLocalization.Both:
toolKeyins.push({ value: tool.keyin });
// istanbul ignore else
if (tool.keyin === tool.englishKeyin) break;
// istanbul ignore next
default: // eslint-disable-line no-fallthrough
case KeyinFieldLocalization.NonLocalized:
// istanbul ignore next
if (
KeyinFieldLocalization.NonLocalized === localizedKeyinPreference ||
(KeyinFieldLocalization.Both === localizedKeyinPreference &&
tool.englishKeyin !== tool.keyin)
)
toolKeyins.push({ value: tool.englishKeyin });
break;
}
}
return toolKeyins;
}