|
| 1 | +import React, { Fragment } from "react"; |
| 2 | +import { |
| 3 | + actionReferences, |
| 4 | + modifierReferences, |
| 5 | + scopeReferences, |
| 6 | +} from "@cursorless/lib-common"; |
| 7 | +import type { ReferenceEntry } from "@cursorless/lib-common"; |
| 8 | + |
| 9 | +export function ActionReference({ id }: { id: keyof typeof actionReferences }) { |
| 10 | + return <ReferenceEntryComponent id={id} entry={actionReferences[id]} />; |
| 11 | +} |
| 12 | + |
| 13 | +export function ModifierReference({ |
| 14 | + id, |
| 15 | +}: { |
| 16 | + id: keyof typeof modifierReferences; |
| 17 | +}) { |
| 18 | + return <ReferenceEntryComponent id={id} entry={modifierReferences[id]} />; |
| 19 | +} |
| 20 | + |
| 21 | +export function ScopeReference({ id }: { id: keyof typeof scopeReferences }) { |
| 22 | + return <ReferenceEntryComponent id={id} entry={scopeReferences[id]} />; |
| 23 | +} |
| 24 | + |
| 25 | +function ReferenceEntryComponent({ |
| 26 | + id, |
| 27 | + entry, |
| 28 | +}: { |
| 29 | + id: string; |
| 30 | + entry: ReferenceEntry; |
| 31 | +}) { |
| 32 | + return ( |
| 33 | + <> |
| 34 | + {entry.description != null && <p>{entry.description}</p>} |
| 35 | + |
| 36 | + <p> |
| 37 | + Cursorless ID: <code>{id}</code> |
| 38 | + </p> |
| 39 | + |
| 40 | + {entry.defaultSpokenForm != null && ( |
| 41 | + <div className="mb-3"> |
| 42 | + <div> |
| 43 | + Default spoken form: <code>{entry.defaultSpokenForm}</code> |
| 44 | + </div> |
| 45 | + |
| 46 | + {entry.legacySpokenForms != null && ( |
| 47 | + <div> |
| 48 | + Legacy spoken forms:{" "} |
| 49 | + {entry.legacySpokenForms.map((s, i) => ( |
| 50 | + <Fragment key={s}> |
| 51 | + {i > 0 && ", "} |
| 52 | + <code>{s}</code> |
| 53 | + </Fragment> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + )} |
| 57 | + |
| 58 | + {entry.disabledByDefault && ( |
| 59 | + <div> |
| 60 | + <strong>Disabled by default</strong> |
| 61 | + </div> |
| 62 | + )} |
| 63 | + </div> |
| 64 | + )} |
| 65 | + |
| 66 | + {entry.syntaxes.length > 0 && ( |
| 67 | + <div> |
| 68 | + Syntax: |
| 69 | + <ul className="list-unstyled"> |
| 70 | + {entry.syntaxes.map((syntax, index) => ( |
| 71 | + <li key={index}> |
| 72 | + {formatPattern(syntax.pattern, entry.defaultSpokenForm)}:{" "} |
| 73 | + {formatPattern(syntax.description)} |
| 74 | + </li> |
| 75 | + ))} |
| 76 | + </ul> |
| 77 | + </div> |
| 78 | + )} |
| 79 | + |
| 80 | + {entry.examples.length > 0 && ( |
| 81 | + <div> |
| 82 | + Examples: |
| 83 | + <ul className="list-unstyled"> |
| 84 | + {entry.examples.map((example, index) => ( |
| 85 | + <li key={index}> |
| 86 | + <code> |
| 87 | + " |
| 88 | + {formatPattern(example.spokenForm, entry.defaultSpokenForm)} |
| 89 | + " |
| 90 | + </code> |
| 91 | + : {example.description} |
| 92 | + </li> |
| 93 | + ))} |
| 94 | + </ul> |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +function formatPattern( |
| 102 | + pattern: string, |
| 103 | + defaultSpokenForm?: string, |
| 104 | +): React.ReactNode[] { |
| 105 | + return pattern.split(/(<[\w ]+>)/gu).map((part, index) => { |
| 106 | + if (part.startsWith("<") && part.endsWith(">")) { |
| 107 | + if (part === "<spokenForm>") { |
| 108 | + return defaultSpokenForm ?? "???"; |
| 109 | + } |
| 110 | + return <code key={index}>{part}</code>; |
| 111 | + } |
| 112 | + return part; |
| 113 | + }); |
| 114 | +} |
0 commit comments