diff --git a/apps/client/src/widgets/react/FormTextBox.tsx b/apps/client/src/widgets/react/FormTextBox.tsx index 99697d11f..de9e67dce 100644 --- a/apps/client/src/widgets/react/FormTextBox.tsx +++ b/apps/client/src/widgets/react/FormTextBox.tsx @@ -3,7 +3,7 @@ import type { InputHTMLAttributes, RefObject } from "preact/compat"; interface FormTextBoxProps extends Omit, "onChange" | "value"> { id?: string; currentValue?: string; - onChange?(newValue: string, validity: ValidityState): void | false; + onChange?(newValue: string, validity: ValidityState): void; inputRef?: RefObject; } diff --git a/apps/client/src/widgets/type_widgets/options/other.tsx b/apps/client/src/widgets/type_widgets/options/other.tsx index 12bed4ee4..95774ba53 100644 --- a/apps/client/src/widgets/type_widgets/options/other.tsx +++ b/apps/client/src/widgets/type_widgets/options/other.tsx @@ -12,11 +12,14 @@ import { SANITIZER_DEFAULT_ALLOWED_TAGS } from "@triliumnext/commons"; import FormCheckbox from "../../react/FormCheckbox"; import FormGroup from "../../react/FormGroup"; import search from "../../../services/search"; -import { FormTextBoxWithUnit } from "../../react/FormTextBox"; +import FormTextBox, { FormTextBoxWithUnit } from "../../react/FormTextBox"; +import FormSelect from "../../react/FormSelect"; +import { isElectron } from "../../../services/utils"; export default function OtherSettings() { return ( <> + {isElectron() && } @@ -28,6 +31,57 @@ export default function OtherSettings() { ) } +function SearchEngineSettings() { + const [ customSearchEngineName, setCustomSearchEngineName ] = useTriliumOption("customSearchEngineName"); + const [ customSearchEngineUrl, setCustomSearchEngineUrl ] = useTriliumOption("customSearchEngineUrl"); + + const searchEngines = useMemo(() => { + return [ + { url: "https://www.bing.com/search?q={keyword}", name: t("search_engine.bing") }, + { url: "https://www.baidu.com/s?wd={keyword}", name: t("search_engine.baidu") }, + { url: "https://duckduckgo.com/?q={keyword}", name: t("search_engine.duckduckgo") }, + { url: "https://www.google.com/search?q={keyword}", name: t("search_engine.google") } + ]; + }, []); + + return ( + + {t("search_engine.custom_search_engine_info")} + + + { + const searchEngine = searchEngines.find(e => e.url === newValue); + if (!searchEngine) { + return; + } + + setCustomSearchEngineName(searchEngine.name); + setCustomSearchEngineUrl(searchEngine.url); + }} + /> + + + + + + + + + + + ) +} + function NoteErasureTimeout() { return ( diff --git a/apps/client/src/widgets/type_widgets/options/other/search_engine.ts b/apps/client/src/widgets/type_widgets/options/other/search_engine.ts deleted file mode 100644 index b512a3cae..000000000 --- a/apps/client/src/widgets/type_widgets/options/other/search_engine.ts +++ /dev/null @@ -1,84 +0,0 @@ -import OptionsWidget from "../options_widget.js"; -import utils from "../../../../services/utils.js"; -import { t } from "../../../../services/i18n.js"; -import type { OptionMap } from "@triliumnext/commons"; - -const TPL = /*html*/` -
-

${t("search_engine.title")}

- -

${t("search_engine.custom_search_engine_info")}

- -
-
- - -
- -
- - -
- -
- - -
- -
- -
-
-
`; - -const SEARCH_ENGINES: Record = { - Bing: "https://www.bing.com/search?q={keyword}", - Baidu: "https://www.baidu.com/s?wd={keyword}", - DuckDuckGo: "https://duckduckgo.com/?q={keyword}", - Google: "https://www.google.com/search?q={keyword}" -}; - -export default class SearchEngineOptions extends OptionsWidget { - - private $form!: JQuery; - private $predefinedSearchEngineSelect!: JQuery; - private $customSearchEngineName!: JQuery; - private $customSearchEngineUrl!: JQuery; - - isEnabled() { - return super.isEnabled() && utils.isElectron(); - } - - doRender() { - this.$widget = $(TPL); - - this.$form = this.$widget.find(".sync-setup-form"); - this.$predefinedSearchEngineSelect = this.$widget.find(".predefined-search-engine-select"); - this.$customSearchEngineName = this.$widget.find(".custom-search-engine-name"); - this.$customSearchEngineUrl = this.$widget.find(".custom-search-engine-url"); - - this.$predefinedSearchEngineSelect.on("change", () => { - const predefinedSearchEngine = String(this.$predefinedSearchEngineSelect.val()); - this.$customSearchEngineName[0].value = predefinedSearchEngine; - this.$customSearchEngineUrl[0].value = SEARCH_ENGINES[predefinedSearchEngine]; - }); - - this.$form.on("submit", () => { - this.updateMultipleOptions({ - customSearchEngineName: this.$customSearchEngineName.val(), - customSearchEngineUrl: this.$customSearchEngineUrl.val() - }); - }); - } - - async optionsLoaded(options: OptionMap) { - this.$predefinedSearchEngineSelect.val(""); - this.$customSearchEngineName[0].value = options.customSearchEngineName; - this.$customSearchEngineUrl[0].value = options.customSearchEngineUrl; - } -}