1
0
silverbullet/web/components/filter.tsx

214 lines
6.3 KiB
TypeScript
Raw Normal View History

import {
CompletionContext,
CompletionResult,
useEffect,
useRef,
useState,
} from "../deps.ts";
import { FilterOption } from "../types.ts";
import { FunctionalComponent } from "https://esm.sh/v99/preact@10.11.3/src/index";
import { FeatherProps } from "https://esm.sh/v99/preact-feather@4.2.1/dist/types";
import { MiniEditor } from "./mini_editor.tsx";
2023-07-25 15:33:07 +00:00
import { fuzzySearchAndSort } from "./fuse_search.ts";
2023-08-20 16:02:13 +00:00
import { deepEqual } from "../../common/util.ts";
2022-05-16 13:09:36 +00:00
export function FilterList({
placeholder,
options,
label,
onSelect,
onKeyPress,
completer,
vimMode,
darkMode,
allowNew = false,
helpText = "",
2022-04-01 13:02:35 +00:00
completePrefix,
icon: Icon,
newHint,
}: {
placeholder: string;
options: FilterOption[];
label: string;
onKeyPress?: (key: string, currentText: string) => void;
onSelect: (option: FilterOption | undefined) => void;
vimMode: boolean;
darkMode: boolean;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
allowNew?: boolean;
2022-04-01 13:02:35 +00:00
completePrefix?: string;
helpText: string;
newHint?: string;
icon?: FunctionalComponent<FeatherProps>;
}) {
const [text, setText] = useState("");
const [matchingOptions, setMatchingOptions] = useState(
fuzzySearchAndSort(options, ""),
);
const [selectedOption, setSelectionOption] = useState(0);
const selectedElementRef = useRef<HTMLDivElement>(null);
function updateFilter(originalPhrase: string) {
const results = fuzzySearchAndSort(options, originalPhrase);
const foundExactMatch = !!results.find((result) =>
result.name === originalPhrase
);
2022-08-01 15:06:17 +00:00
if (allowNew && !foundExactMatch && originalPhrase) {
results.splice(1, 0, {
2022-05-16 13:09:36 +00:00
name: originalPhrase,
hint: newHint,
});
}
2023-08-20 16:02:13 +00:00
if (!deepEqual(matchingOptions, results)) {
// Only do this (=> rerender of UI) if the results have changed
setMatchingOptions(results);
setSelectionOption(0);
}
}
useEffect(() => {
updateFilter(text);
}, [options, text]);
useEffect(() => {
function closer() {
2023-08-20 16:02:13 +00:00
// console.log("Invoking closer");
onSelect(undefined);
}
document.addEventListener("click", closer);
return () => {
document.removeEventListener("click", closer);
};
}, []);
const returnEl = (
2022-12-21 15:08:51 +00:00
<div className="sb-modal-wrapper">
<div className="sb-modal-box">
2023-07-24 07:36:10 +00:00
<div
className="sb-header"
onClick={(e) => {
// Allow tapping/clicking the header without closing it
e.stopPropagation();
}}
>
2022-08-01 11:05:30 +00:00
<label>{label}</label>
<MiniEditor
text={text}
vimMode={vimMode}
vimStartInInsertMode={true}
focus={true}
darkMode={darkMode}
completer={completer}
placeholderText={placeholder}
onEnter={(_newText, shiftDown) => {
onSelect(
shiftDown ? { name: text } : matchingOptions[selectedOption],
);
return true;
}}
onEscape={() => {
onSelect(undefined);
2022-08-01 11:05:30 +00:00
}}
onChange={(text) => {
setText(text);
}}
onKeyUp={(view, e) => {
// This event is triggered after the key has been processed by CM already
2022-08-01 11:05:30 +00:00
if (onKeyPress) {
onKeyPress(e.key, view.state.sliceDoc());
2022-08-01 11:05:30 +00:00
}
return false;
}}
onKeyDown={(view, e) => {
2022-08-01 11:05:30 +00:00
switch (e.key) {
case "ArrowUp":
setSelectionOption(Math.max(0, selectedOption - 1));
return true;
2022-08-01 11:05:30 +00:00
case "ArrowDown":
setSelectionOption(
Math.min(matchingOptions.length - 1, selectedOption + 1),
2022-08-01 11:05:30 +00:00
);
return true;
2022-08-01 15:06:17 +00:00
case "PageUp":
setSelectionOption(Math.max(0, selectedOption - 5));
return true;
2022-08-01 15:06:17 +00:00
case "PageDown":
setSelectionOption(Math.max(0, selectedOption + 5));
return true;
2022-08-01 15:06:17 +00:00
case "Home":
setSelectionOption(0);
return true;
2022-08-01 15:06:17 +00:00
case "End":
setSelectionOption(matchingOptions.length - 1);
return true;
case " ": {
const text = view.state.sliceDoc();
if (completePrefix && text === "") {
setText(completePrefix);
// updateFilter(completePrefix);
return true;
2022-08-01 11:05:30 +00:00
}
break;
}
2022-08-01 11:05:30 +00:00
}
return false;
}}
2022-08-01 11:05:30 +00:00
/>
</div>
<div
2022-08-02 12:40:04 +00:00
className="sb-help-text"
2022-08-01 11:05:30 +00:00
dangerouslySetInnerHTML={{ __html: helpText }}
>
</div>
2022-08-02 12:40:04 +00:00
<div className="sb-result-list">
2022-08-01 11:05:30 +00:00
{matchingOptions && matchingOptions.length > 0
? matchingOptions.map((option, idx) => (
<div
key={"" + idx}
ref={selectedOption === idx ? selectedElementRef : undefined}
className={selectedOption === idx
? "sb-selected-option"
: "sb-option"}
onMouseOver={(e) => {
setSelectionOption(idx);
}}
onClick={(e) => {
e.stopPropagation();
onSelect(option);
}}
>
2022-12-17 08:07:09 +00:00
{Icon && (
<span className="sb-icon">
<Icon width={16} height={16} />
</span>
)}
<span className="sb-name" // dangerouslySetInnerHTML={{
// __html: option?.result?.indexes
// ? fuzzysort.highlight(option.result, "<b>", "</b>")!
// : escapeHtml(option.name),
// }}
2022-08-01 11:05:30 +00:00
>
{option.name}
</span>
{option.hint && <span className="sb-hint">{option.hint}</span>}
</div>
))
2022-08-01 11:05:30 +00:00
: null}
</div>
</div>
</div>
);
useEffect(() => {
selectedElementRef.current?.scrollIntoView({
block: "nearest",
});
});
return returnEl;
}