Fix language configuration UI (#388)

This commit is contained in:
sua yoo 2022-12-01 10:02:13 -08:00 committed by GitHub
parent aabb0b2a92
commit 5d18ffa938
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import { LitElement, html, css } from "lit"; import { LitElement, html, css } from "lit";
import { state, property } from "lit/decorators.js"; import { state, property } from "lit/decorators.js";
import { ifDefined } from "lit/directives/if-defined.js";
import { localized, msg } from "@lit/localize"; import { localized, msg } from "@lit/localize";
import sortBy from "lodash/fp/sortBy"; import sortBy from "lodash/fp/sortBy";
import ISO6391 from "iso-639-1"; import ISO6391 from "iso-639-1";
@ -14,11 +15,12 @@ const languages = sortBy("name")(
}>; }>;
/** /**
* Choose language from dropdown * Choose language from dropdown.
* Uses ISO 639-1 codes (2 letters representing macrolanguages.)
* *
* Usage: * Usage:
* ```ts * ```ts
* <btrix-language-select @sl-select=${console.debug}> * <btrix-language-select value=${defaultValue} @sl-select=${console.debug}>
* <span slot="label">Label</span> * <span slot="label">Label</span>
* </btrix-language-select> * </btrix-language-select>
* ``` * ```
@ -35,13 +37,21 @@ export class LanguageSelect extends LitElement {
} }
`; `;
@property({ type: String })
value?: LanguageCode;
@property({ type: Boolean }) @property({ type: Boolean })
hoist = false; hoist = false;
render() { render() {
return html` return html`
<sl-select clearable placeholder=${msg("Default")} ?hoist=${this.hoist}> <sl-select
<div slot="label"><slot name="label"></slot></div> clearable
placeholder=${msg("Browser Default")}
value=${ifDefined(this.value)}
?hoist=${this.hoist}
>
<div slot="label"><slot name="label">${msg("Language")}</slot></div>
${languages.map( ${languages.map(
({ code, name, nativeName }) => html` ({ code, name, nativeName }) => html`
<sl-menu-item value=${code}> <sl-menu-item value=${code}>

View File

@ -104,6 +104,7 @@ export class CrawlTemplatesDetail extends LiteElement {
this.isConfigCodeView = true; this.isConfigCodeView = true;
} }
this.configCode = jsonToYaml(this.crawlTemplate.config); this.configCode = jsonToYaml(this.crawlTemplate.config);
this.browserLanguage = this.crawlTemplate.config.lang;
if (this.crawlTemplate.config.exclude?.length) { if (this.crawlTemplate.config.exclude?.length) {
this.exclusions = this.crawlTemplate.config.exclude; this.exclusions = this.crawlTemplate.config.exclude;
} }
@ -725,6 +726,7 @@ export class CrawlTemplatesDetail extends LiteElement {
</div> </div>
<div> <div>
<btrix-language-select <btrix-language-select
.value=${this.browserLanguage}
@sl-select=${(e: CustomEvent) => @sl-select=${(e: CustomEvent) =>
(this.browserLanguage = e.detail.item.value)} (this.browserLanguage = e.detail.item.value)}
@sl-clear=${() => (this.browserLanguage = null)} @sl-clear=${() => (this.browserLanguage = null)}

View File

@ -27,7 +27,6 @@ type NewCrawlTemplate = {
scale: number; scale: number;
config: CrawlConfig; config: CrawlConfig;
profileid: string | null; profileid: string | null;
lang: string | null;
}; };
export type InitialCrawlTemplate = Pick< export type InitialCrawlTemplate = Pick<
@ -141,6 +140,15 @@ export class CrawlTemplatesNew extends LiteElement {
this.exclusions = this.initialCrawlTemplate.config.exclude; this.exclusions = this.initialCrawlTemplate.config.exclude;
} }
this.browserProfileId = this.initialCrawlTemplate.profileid; this.browserProfileId = this.initialCrawlTemplate.profileid;
// Default to current user browser language
const browserLanguage = window.navigator.language;
if (browserLanguage) {
this.browserLanguage = browserLanguage.slice(
0,
browserLanguage.indexOf("-")
);
}
super.connectedCallback(); super.connectedCallback();
} }
@ -395,6 +403,7 @@ export class CrawlTemplatesNew extends LiteElement {
</div> </div>
<div class="col-span-1"> <div class="col-span-1">
<btrix-language-select <btrix-language-select
.value=${this.browserLanguage}
@sl-select=${(e: CustomEvent) => @sl-select=${(e: CustomEvent) =>
(this.browserLanguage = e.detail.item.value)} (this.browserLanguage = e.detail.item.value)}
@sl-clear=${() => (this.browserLanguage = null)} @sl-clear=${() => (this.browserLanguage = null)}
@ -553,7 +562,6 @@ export class CrawlTemplatesNew extends LiteElement {
crawlTimeout: crawlTimeoutMinutes ? +crawlTimeoutMinutes * 60 : 0, crawlTimeout: crawlTimeoutMinutes ? +crawlTimeoutMinutes * 60 : 0,
scale: +scale, scale: +scale,
profileid: this.browserProfileId, profileid: this.browserProfileId,
lang: this.browserLanguage || null,
}; };
if (this.isConfigCodeView) { if (this.isConfigCodeView) {
@ -565,6 +573,7 @@ export class CrawlTemplatesNew extends LiteElement {
limit: pageLimit ? +pageLimit : 0, limit: pageLimit ? +pageLimit : 0,
extraHops: formData.get("extraHopsOne") ? 1 : 0, extraHops: formData.get("extraHopsOne") ? 1 : 0,
exclude: trimExclusions(this.exclusions), exclude: trimExclusions(this.exclusions),
lang: this.browserLanguage || null,
}; };
} }