2024-07-30 23:33:33 +08:00
|
|
|
import type { FeatherProps } from "preact-feather/types";
|
|
|
|
import type {
|
|
|
|
CompletionContext,
|
|
|
|
CompletionResult,
|
|
|
|
} from "@codemirror/autocomplete";
|
|
|
|
import type { FunctionalComponent } from "preact";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { useEffect, useRef, useState } from "preact/hooks";
|
2024-08-07 02:11:38 +08:00
|
|
|
import type { FilterOption } from "@silverbulletmd/silverbullet/type/client";
|
2022-12-21 21:55:24 +08:00
|
|
|
import { MiniEditor } from "./mini_editor.tsx";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { fuzzySearchAndSort } from "../fuse_search.ts";
|
2024-02-29 22:23:05 +08:00
|
|
|
import { deepEqual } from "../../plug-api/lib/json.ts";
|
2024-04-20 22:22:02 +08:00
|
|
|
import { AlwaysShownModal } from "./basic_modals.tsx";
|
2022-05-16 21:09:36 +08:00
|
|
|
|
2022-03-20 16:56:28 +08:00
|
|
|
export function FilterList({
|
|
|
|
placeholder,
|
|
|
|
options,
|
|
|
|
label,
|
|
|
|
onSelect,
|
|
|
|
onKeyPress,
|
2022-12-21 21:55:24 +08:00
|
|
|
completer,
|
|
|
|
vimMode,
|
|
|
|
darkMode,
|
2023-12-22 02:49:25 +08:00
|
|
|
preFilter,
|
|
|
|
phrasePreprocessor,
|
2022-03-20 16:56:28 +08:00
|
|
|
allowNew = false,
|
|
|
|
helpText = "",
|
2022-04-01 21:02:35 +08:00
|
|
|
completePrefix,
|
2022-12-09 00:04:07 +08:00
|
|
|
icon: Icon,
|
2022-03-20 16:56:28 +08:00
|
|
|
newHint,
|
|
|
|
}: {
|
|
|
|
placeholder: string;
|
2022-04-13 20:46:52 +08:00
|
|
|
options: FilterOption[];
|
2022-03-20 16:56:28 +08:00
|
|
|
label: string;
|
|
|
|
onKeyPress?: (key: string, currentText: string) => void;
|
2022-04-13 20:46:52 +08:00
|
|
|
onSelect: (option: FilterOption | undefined) => void;
|
2023-12-22 02:49:25 +08:00
|
|
|
preFilter?: (options: FilterOption[], phrase: string) => FilterOption[];
|
|
|
|
phrasePreprocessor?: (phrase: string) => string;
|
2022-12-21 21:55:24 +08:00
|
|
|
vimMode: boolean;
|
|
|
|
darkMode: boolean;
|
|
|
|
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
|
2022-03-20 16:56:28 +08:00
|
|
|
allowNew?: boolean;
|
2022-04-01 21:02:35 +08:00
|
|
|
completePrefix?: string;
|
2022-03-20 16:56:28 +08:00
|
|
|
helpText: string;
|
|
|
|
newHint?: string;
|
2022-12-09 00:04:07 +08:00
|
|
|
icon?: FunctionalComponent<FeatherProps>;
|
2022-03-20 16:56:28 +08:00
|
|
|
}) {
|
|
|
|
const [text, setText] = useState("");
|
|
|
|
const [matchingOptions, setMatchingOptions] = useState(
|
2024-01-21 02:16:07 +08:00
|
|
|
fuzzySearchAndSort(
|
|
|
|
preFilter ? preFilter(options, "") : options,
|
|
|
|
"",
|
|
|
|
),
|
2022-03-20 16:56:28 +08:00
|
|
|
);
|
|
|
|
const [selectedOption, setSelectionOption] = useState(0);
|
|
|
|
|
2022-10-10 20:50:21 +08:00
|
|
|
const selectedElementRef = useRef<HTMLDivElement>(null);
|
2022-03-31 20:28:07 +08:00
|
|
|
|
|
|
|
function updateFilter(originalPhrase: string) {
|
2023-12-22 02:49:25 +08:00
|
|
|
const prefilteredOptions = preFilter
|
|
|
|
? preFilter(options, originalPhrase)
|
|
|
|
: options;
|
|
|
|
if (phrasePreprocessor) {
|
|
|
|
originalPhrase = phrasePreprocessor(originalPhrase);
|
|
|
|
}
|
|
|
|
const results = fuzzySearchAndSort(prefilteredOptions, originalPhrase);
|
2023-05-24 02:53:53 +08:00
|
|
|
const foundExactMatch = !!results.find((result) =>
|
|
|
|
result.name === originalPhrase
|
|
|
|
);
|
2022-08-01 23:06:17 +08:00
|
|
|
if (allowNew && !foundExactMatch && originalPhrase) {
|
2022-10-29 15:27:18 +08:00
|
|
|
results.splice(1, 0, {
|
2022-05-16 21:09:36 +08:00
|
|
|
name: originalPhrase,
|
|
|
|
hint: newHint,
|
|
|
|
});
|
2022-03-20 16:56:28 +08:00
|
|
|
}
|
|
|
|
|
2023-08-21 00:02:13 +08:00
|
|
|
if (!deepEqual(matchingOptions, results)) {
|
|
|
|
// Only do this (=> rerender of UI) if the results have changed
|
|
|
|
setMatchingOptions(results);
|
|
|
|
setSelectionOption(0);
|
|
|
|
}
|
2022-03-31 20:28:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
updateFilter(text);
|
2023-05-24 02:53:53 +08:00
|
|
|
}, [options, text]);
|
2022-03-20 16:56:28 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function closer() {
|
|
|
|
onSelect(undefined);
|
|
|
|
}
|
2022-03-31 20:28:07 +08:00
|
|
|
|
2022-03-20 16:56:28 +08:00
|
|
|
document.addEventListener("click", closer);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("click", closer);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const returnEl = (
|
2024-04-20 22:22:02 +08:00
|
|
|
<AlwaysShownModal
|
|
|
|
onCancel={() => {
|
|
|
|
onSelect(undefined);
|
|
|
|
}}
|
|
|
|
>
|
2024-01-15 23:43:12 +08:00
|
|
|
<div
|
|
|
|
className="sb-header"
|
|
|
|
onClick={(e) => {
|
|
|
|
// Allow tapping/clicking the header without closing it
|
|
|
|
e.stopPropagation();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<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;
|
2023-07-24 15:36:10 +08:00
|
|
|
}}
|
2024-01-15 23:43:12 +08:00
|
|
|
onEscape={() => {
|
|
|
|
onSelect(undefined);
|
|
|
|
}}
|
|
|
|
onChange={(text) => {
|
|
|
|
setText(text);
|
|
|
|
}}
|
|
|
|
onKeyUp={(view, e) => {
|
|
|
|
// This event is triggered after the key has been processed by CM already
|
|
|
|
if (onKeyPress) {
|
|
|
|
onKeyPress(e.key, view.state.sliceDoc());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}}
|
|
|
|
onKeyDown={(view, e) => {
|
|
|
|
switch (e.key) {
|
|
|
|
case "ArrowUp":
|
|
|
|
setSelectionOption(Math.max(0, selectedOption - 1));
|
|
|
|
return true;
|
|
|
|
case "ArrowDown":
|
|
|
|
setSelectionOption(
|
|
|
|
Math.min(matchingOptions.length - 1, selectedOption + 1),
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
case "PageUp":
|
|
|
|
setSelectionOption(Math.max(0, selectedOption - 5));
|
|
|
|
return true;
|
|
|
|
case "PageDown":
|
2024-04-20 22:22:02 +08:00
|
|
|
setSelectionOption(
|
|
|
|
Math.min(matchingOptions.length - 1, selectedOption + 5),
|
|
|
|
);
|
2024-01-15 23:43:12 +08:00
|
|
|
return true;
|
|
|
|
case "Home":
|
|
|
|
setSelectionOption(0);
|
|
|
|
return true;
|
|
|
|
case "End":
|
|
|
|
setSelectionOption(matchingOptions.length - 1);
|
|
|
|
return true;
|
|
|
|
case " ": {
|
|
|
|
const text = view.state.sliceDoc();
|
2024-07-19 23:10:39 +08:00
|
|
|
if (e.shiftKey) {
|
|
|
|
// Operate on the highlighted option, ignoring prompt
|
|
|
|
const option = matchingOptions[selectedOption].name;
|
|
|
|
// Get the folder it's nested in, keeping the trailing /
|
2024-07-30 21:17:34 +08:00
|
|
|
const folderPath = option.slice(
|
|
|
|
0,
|
|
|
|
option.lastIndexOf("/") + 1,
|
|
|
|
);
|
2024-07-19 23:10:39 +08:00
|
|
|
// If the option wasn't in a folder, make it a folder
|
|
|
|
setText(folderPath !== "" ? folderPath : option + "/");
|
|
|
|
return true;
|
|
|
|
} else if (completePrefix && text === "") {
|
2024-01-15 23:43:12 +08:00
|
|
|
setText(completePrefix);
|
|
|
|
// updateFilter(completePrefix);
|
2022-12-21 21:55:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
break;
|
2022-08-01 19:05:30 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
}
|
2024-08-15 22:41:15 +08:00
|
|
|
if (e.ctrlKey && e.key === "n") {
|
|
|
|
setSelectionOption(
|
|
|
|
Math.min(matchingOptions.length - 1, selectedOption + 1),
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (e.ctrlKey && e.key === "p") {
|
|
|
|
setSelectionOption(Math.max(0, selectedOption - 1));
|
|
|
|
return true;
|
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
return false;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="sb-help-text"
|
|
|
|
dangerouslySetInnerHTML={{ __html: helpText }}
|
|
|
|
>
|
|
|
|
</div>
|
2024-04-20 22:22:02 +08:00
|
|
|
<div className="sb-result-list" tabIndex={-1}>
|
2024-01-15 23:43:12 +08:00
|
|
|
{matchingOptions && matchingOptions.length > 0
|
|
|
|
? matchingOptions.map((option, idx) => (
|
|
|
|
<div
|
|
|
|
key={"" + idx}
|
|
|
|
ref={selectedOption === idx ? selectedElementRef : undefined}
|
2024-07-31 17:28:31 +08:00
|
|
|
className={(selectedOption === idx
|
|
|
|
? "sb-option sb-selected-option"
|
|
|
|
: "sb-option") +
|
2024-08-02 23:14:40 +08:00
|
|
|
(option.cssClass
|
|
|
|
? " sb-decorated-object " + option.cssClass
|
|
|
|
: "")}
|
2024-07-30 21:17:34 +08:00
|
|
|
onMouseMove={() => {
|
2024-01-15 23:43:12 +08:00
|
|
|
if (selectedOption !== idx) {
|
2022-10-10 20:50:21 +08:00
|
|
|
setSelectionOption(idx);
|
2024-01-15 23:43:12 +08:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
onSelect(option);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{Icon && (
|
|
|
|
<span className="sb-icon">
|
|
|
|
<Icon width={16} height={16} />
|
2022-10-10 20:50:21 +08:00
|
|
|
</span>
|
2024-01-15 23:43:12 +08:00
|
|
|
)}
|
|
|
|
<span className="sb-name">
|
|
|
|
{option.name}
|
|
|
|
</span>
|
|
|
|
{option.hint && <span className="sb-hint">{option.hint}</span>}
|
|
|
|
<div className="sb-description">{option.description}</div>
|
|
|
|
</div>
|
|
|
|
))
|
|
|
|
: null}
|
2022-03-20 16:56:28 +08:00
|
|
|
</div>
|
2024-04-20 22:22:02 +08:00
|
|
|
</AlwaysShownModal>
|
2022-03-20 16:56:28 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
selectedElementRef.current?.scrollIntoView({
|
|
|
|
block: "nearest",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnEl;
|
|
|
|
}
|