Files
ThreadNet-Web/src/components/views/settings/SpellCheckSettings.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-11-26 13:51:03 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2021-02-18 19:41:19 +01:00
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
2020-11-26 13:51:03 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2020-11-26 13:51:03 +01:00
*/
import React from "react";
2021-10-22 17:23:32 -05:00
2020-12-01 16:59:21 +01:00
import SpellCheckLanguagesDropdown from "../../../components/views/elements/SpellCheckLanguagesDropdown";
2023-02-13 11:39:16 +00:00
import AccessibleButton, { ButtonEvent } from "../../../components/views/elements/AccessibleButton";
import { _t, getUserLanguage } from "../../../languageHandler";
2020-11-26 13:51:03 +01:00
interface ExistingSpellCheckLanguageIProps {
2021-07-01 23:23:03 +01:00
language: string;
/**
* The label to render on the component. If not provided, the language code will be used.
*/
label?: string;
2023-02-13 11:39:16 +00:00
onRemoved(language: string): void;
2020-12-01 20:36:25 +01:00
}
2020-11-26 13:51:03 +01:00
interface SpellCheckLanguagesIProps {
2021-07-01 23:23:03 +01:00
languages: Array<string>;
2023-02-13 11:39:16 +00:00
onLanguagesChange(languages: Array<string>): void;
2020-12-01 20:36:25 +01:00
}
2020-11-26 13:51:03 +01:00
interface SpellCheckLanguagesIState {
2021-07-01 23:23:03 +01:00
newLanguage: string;
2020-11-26 13:51:03 +01:00
}
export class ExistingSpellCheckLanguage extends React.Component<ExistingSpellCheckLanguageIProps> {
2023-02-13 11:39:16 +00:00
private onRemove = (e: ButtonEvent): void => {
2020-11-26 13:51:03 +01:00
e.stopPropagation();
e.preventDefault();
return this.props.onRemoved(this.props.language);
};
public render(): React.ReactNode {
2020-11-26 13:51:03 +01:00
return (
2020-11-29 12:29:05 +01:00
<div className="mx_ExistingSpellCheckLanguage">
<span className="mx_ExistingSpellCheckLanguage_language">
{this.props.label ?? this.props.language}
</span>
<AccessibleButton onClick={this.onRemove} kind="danger_sm">
{_t("action|remove")}
2020-11-26 13:51:03 +01:00
</AccessibleButton>
</div>
);
}
}
export default class SpellCheckLanguages extends React.Component<SpellCheckLanguagesIProps, SpellCheckLanguagesIState> {
2023-02-13 11:39:16 +00:00
public constructor(props: SpellCheckLanguagesIProps) {
2020-11-26 13:51:03 +01:00
super(props);
this.state = {
newLanguage: "",
2021-06-29 13:11:58 +01:00
};
2020-11-26 13:51:03 +01:00
}
private onRemoved = (language: string): void => {
2020-11-26 13:51:03 +01:00
const languages = this.props.languages.filter((e) => e !== language);
this.props.onLanguagesChange(languages);
};
2023-02-13 11:39:16 +00:00
private onAddClick = (e: ButtonEvent): void => {
2020-11-26 13:51:03 +01:00
e.stopPropagation();
e.preventDefault();
const language = this.state.newLanguage;
this.setState({ newLanguage: "" });
2020-12-01 20:36:25 +01:00
2020-11-26 13:51:03 +01:00
if (!language) return;
if (this.props.languages.includes(language)) return;
2021-06-29 13:11:58 +01:00
this.props.languages.push(language);
2020-11-26 13:51:03 +01:00
this.props.onLanguagesChange(this.props.languages);
};
private onNewLanguageChange = (language: string): void => {
2020-11-26 13:51:03 +01:00
if (this.state.newLanguage === language) return;
2021-06-29 13:11:58 +01:00
this.setState({ newLanguage: language });
2020-11-26 13:51:03 +01:00
};
public render(): React.ReactNode {
const intl = new Intl.DisplayNames([getUserLanguage()], { type: "language", style: "short" });
2020-11-26 13:51:03 +01:00
const existingSpellCheckLanguages = this.props.languages.map((e) => {
return <ExistingSpellCheckLanguage language={e} label={intl.of(e)} onRemoved={this.onRemoved} key={e} />;
2020-11-26 13:51:03 +01:00
});
2020-12-01 20:36:25 +01:00
const addButton = (
<AccessibleButton onClick={this.onAddClick} kind="primary">
{_t("action|add")}
2020-11-26 13:51:03 +01:00
</AccessibleButton>
);
return (
<>
{existingSpellCheckLanguages}
<form onSubmit={this.onAddClick} noValidate={true}>
2020-12-01 20:36:25 +01:00
<SpellCheckLanguagesDropdown
className="mx_GeneralUserSettingsTab_spellCheckLanguageInput"
value={this.state.newLanguage}
onOptionChange={this.onNewLanguageChange}
/>
{addButton}
2020-11-26 13:51:03 +01:00
</form>
</>
2020-11-26 13:51:03 +01:00
);
2020-12-01 20:36:25 +01:00
}
2020-11-26 13:51:03 +01:00
}