2022-12-21 13:55:24 +00:00
|
|
|
import {
|
|
|
|
CompletionContext,
|
|
|
|
CompletionResult,
|
|
|
|
useEffect,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from "../deps.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { FilterOption } from "../types.ts";
|
2022-12-08 16:04:07 +00:00
|
|
|
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";
|
2022-12-21 13:55:24 +00:00
|
|
|
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
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
export function FilterList({
|
|
|
|
placeholder,
|
|
|
|
options,
|
|
|
|
label,
|
|
|
|
onSelect,
|
|
|
|
onKeyPress,
|
2022-12-21 13:55:24 +00:00
|
|
|
completer,
|
|
|
|
vimMode,
|
|
|
|
darkMode,
|
2022-03-20 08:56:28 +00:00
|
|
|
allowNew = false,
|
|
|
|
helpText = "",
|
2022-04-01 13:02:35 +00:00
|
|
|
completePrefix,
|
2022-12-08 16:04:07 +00:00
|
|
|
icon: Icon,
|
2022-03-20 08:56:28 +00:00
|
|
|
newHint,
|
|
|
|
}: {
|
|
|
|
placeholder: string;
|
2022-04-13 12:46:52 +00:00
|
|
|
options: FilterOption[];
|
2022-03-20 08:56:28 +00:00
|
|
|
label: string;
|
|
|
|
onKeyPress?: (key: string, currentText: string) => void;
|
2022-04-13 12:46:52 +00:00
|
|
|
onSelect: (option: FilterOption | undefined) => void;
|
2022-12-21 13:55:24 +00:00
|
|
|
vimMode: boolean;
|
|
|
|
darkMode: boolean;
|
|
|
|
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
|
2022-03-20 08:56:28 +00:00
|
|
|
allowNew?: boolean;
|
2022-04-01 13:02:35 +00:00
|
|
|
completePrefix?: string;
|
2022-03-20 08:56:28 +00:00
|
|
|
helpText: string;
|
|
|
|
newHint?: string;
|
2022-12-08 16:04:07 +00:00
|
|
|
icon?: FunctionalComponent<FeatherProps>;
|
2022-03-20 08:56:28 +00:00
|
|
|
}) {
|
|
|
|
const [text, setText] = useState("");
|
|
|
|
const [matchingOptions, setMatchingOptions] = useState(
|
2023-05-23 18:53:53 +00:00
|
|
|
fuzzySearchAndSort(options, ""),
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|
|
|
|
const [selectedOption, setSelectionOption] = useState(0);
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
const selectedElementRef = useRef<HTMLDivElement>(null);
|
2022-03-31 12:28:07 +00:00
|
|
|
|
|
|
|
function updateFilter(originalPhrase: string) {
|
2023-05-23 18:53:53 +00:00
|
|
|
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) {
|
2022-10-29 07:27:18 +00:00
|
|
|
results.splice(1, 0, {
|
2022-05-16 13:09:36 +00:00
|
|
|
name: originalPhrase,
|
|
|
|
hint: newHint,
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
updateFilter(text);
|
2023-05-23 18:53:53 +00:00
|
|
|
}, [options, text]);
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function closer() {
|
2023-08-20 16:02:13 +00:00
|
|
|
// console.log("Invoking closer");
|
2022-03-20 08:56:28 +00:00
|
|
|
onSelect(undefined);
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
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>
|
2022-12-21 13:55:24 +00:00
|
|
|
<MiniEditor
|
|
|
|
text={text}
|
|
|
|
vimMode={vimMode}
|
|
|
|
vimStartInInsertMode={true}
|
|
|
|
focus={true}
|
|
|
|
darkMode={darkMode}
|
|
|
|
completer={completer}
|
|
|
|
placeholderText={placeholder}
|
2023-08-01 19:37:52 +00:00
|
|
|
onEnter={(_newText, shiftDown) => {
|
|
|
|
onSelect(
|
|
|
|
shiftDown ? { name: text } : matchingOptions[selectedOption],
|
|
|
|
);
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
|
|
|
}}
|
|
|
|
onEscape={() => {
|
|
|
|
onSelect(undefined);
|
2022-08-01 11:05:30 +00:00
|
|
|
}}
|
2022-12-21 13:55:24 +00:00
|
|
|
onChange={(text) => {
|
2023-05-23 18:53:53 +00:00
|
|
|
setText(text);
|
2022-12-21 13:55:24 +00:00
|
|
|
}}
|
|
|
|
onKeyUp={(view, e) => {
|
2023-01-03 20:27:46 +00:00
|
|
|
// This event is triggered after the key has been processed by CM already
|
2022-08-01 11:05:30 +00:00
|
|
|
if (onKeyPress) {
|
2022-12-21 13:55:24 +00:00
|
|
|
onKeyPress(e.key, view.state.sliceDoc());
|
2022-08-01 11:05:30 +00:00
|
|
|
}
|
2023-01-03 20:27:46 +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));
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
2022-08-01 11:05:30 +00:00
|
|
|
case "ArrowDown":
|
|
|
|
setSelectionOption(
|
2022-10-10 12:50:21 +00:00
|
|
|
Math.min(matchingOptions.length - 1, selectedOption + 1),
|
2022-08-01 11:05:30 +00:00
|
|
|
);
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
2022-08-01 15:06:17 +00:00
|
|
|
case "PageUp":
|
|
|
|
setSelectionOption(Math.max(0, selectedOption - 5));
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
2022-08-01 15:06:17 +00:00
|
|
|
case "PageDown":
|
|
|
|
setSelectionOption(Math.max(0, selectedOption + 5));
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
2022-08-01 15:06:17 +00:00
|
|
|
case "Home":
|
|
|
|
setSelectionOption(0);
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
2022-08-01 15:06:17 +00:00
|
|
|
case "End":
|
|
|
|
setSelectionOption(matchingOptions.length - 1);
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
|
|
|
case " ": {
|
|
|
|
const text = view.state.sliceDoc();
|
2023-01-03 20:27:46 +00:00
|
|
|
if (completePrefix && text === "") {
|
2022-12-21 13:55:24 +00:00
|
|
|
setText(completePrefix);
|
2023-05-23 18:53:53 +00:00
|
|
|
// updateFilter(completePrefix);
|
2022-12-21 13:55:24 +00:00
|
|
|
return true;
|
2022-08-01 11:05:30 +00:00
|
|
|
}
|
|
|
|
break;
|
2022-12-21 13:55:24 +00:00
|
|
|
}
|
2022-08-01 11:05:30 +00:00
|
|
|
}
|
2022-12-21 13:55:24 +00:00
|
|
|
return false;
|
2022-11-18 15:04:37 +00:00
|
|
|
}}
|
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 }}
|
2022-10-10 12:50:21 +00:00
|
|
|
>
|
|
|
|
</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) => (
|
2022-10-10 12:50:21 +00:00
|
|
|
<div
|
|
|
|
key={"" + idx}
|
|
|
|
ref={selectedOption === idx ? selectedElementRef : undefined}
|
|
|
|
className={selectedOption === idx
|
|
|
|
? "sb-selected-option"
|
|
|
|
: "sb-option"}
|
|
|
|
onMouseOver={(e) => {
|
|
|
|
setSelectionOption(idx);
|
|
|
|
}}
|
|
|
|
onClick={(e) => {
|
2022-12-21 13:55:24 +00:00
|
|
|
e.stopPropagation();
|
2022-10-10 12:50:21 +00:00
|
|
|
onSelect(option);
|
|
|
|
}}
|
|
|
|
>
|
2022-12-17 08:07:09 +00:00
|
|
|
{Icon && (
|
|
|
|
<span className="sb-icon">
|
|
|
|
<Icon width={16} height={16} />
|
|
|
|
</span>
|
|
|
|
)}
|
2023-05-23 18:53:53 +00:00
|
|
|
<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
|
|
|
>
|
2023-05-23 18:53:53 +00:00
|
|
|
{option.name}
|
2022-10-10 12:50:21 +00:00
|
|
|
</span>
|
|
|
|
{option.hint && <span className="sb-hint">{option.hint}</span>}
|
|
|
|
</div>
|
|
|
|
))
|
2022-08-01 11:05:30 +00:00
|
|
|
: null}
|
|
|
|
</div>
|
2022-03-20 08:56:28 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
selectedElementRef.current?.scrollIntoView({
|
|
|
|
block: "nearest",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnEl;
|
|
|
|
}
|