-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reintroduced the langChanged directive
- Loading branch information
Showing
6 changed files
with
156 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {AsyncDirective} from "lit/async-directive.js"; | ||
import {LangChangedEvent, LangChangedSubscription} from "../model"; | ||
import {listenForLangChanged} from "../util"; | ||
|
||
/** | ||
* An abstract lit directive that reacts when the language changes. | ||
*/ | ||
export abstract class LangChangedDirectiveBase extends AsyncDirective { | ||
protected langChangedSubscription: LangChangedSubscription | null = null; | ||
protected getValue: ((e?: LangChangedEvent) => unknown) = (() => ""); | ||
|
||
/** | ||
* Sets up the directive by setting the getValue property and subscribing. | ||
* When subclassing LangChangedDirectiveBase this function should be call in the render function. | ||
* @param getValue | ||
*/ | ||
renderValue(getValue: ((e?: LangChangedEvent) => unknown)): unknown { | ||
this.getValue = getValue; | ||
this.subscribe(); | ||
return this.getValue(); | ||
} | ||
|
||
/** | ||
* Called when the lang changed event is dispatched. | ||
* @param e | ||
*/ | ||
langChanged(e?: LangChangedEvent) { | ||
this.setValue(this.getValue(e)); | ||
} | ||
|
||
/** | ||
* Subscribes to the lang changed event. | ||
*/ | ||
subscribe() { | ||
if (this.langChangedSubscription == null) { | ||
this.langChangedSubscription = listenForLangChanged(this.langChanged.bind(this)); | ||
} | ||
} | ||
|
||
/** | ||
* Unsubscribes from the lang changed event. | ||
*/ | ||
unsubscribe() { | ||
if (this.langChangedSubscription != null) { | ||
this.langChangedSubscription(); | ||
} | ||
} | ||
|
||
/** | ||
* Unsubscribes when disconnected. | ||
*/ | ||
disconnected() { | ||
this.unsubscribe(); | ||
} | ||
|
||
/** | ||
* Subscribes when reconnected. | ||
*/ | ||
reconnected() { | ||
this.subscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {directive} from "lit/directive.js"; | ||
import {LangChangedEvent} from "../model"; | ||
import {LangChangedDirectiveBase} from "./lang-changed-base"; | ||
|
||
/** | ||
* A lit directive that reacts when the language changes and renders the getValue callback. | ||
*/ | ||
export class LangChangedDirective extends LangChangedDirectiveBase { | ||
render(getValue: ((e?: LangChangedEvent) => unknown)): unknown { | ||
return this.renderValue(getValue); | ||
} | ||
} | ||
|
||
export const langChanged = directive(LangChangedDirective); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
import { directive } from "lit/directive.js"; | ||
import { unsafeHTML } from "lit/directives/unsafe-html.js"; | ||
import { ITranslateConfig, Values, ValuesCallback } from "../model"; | ||
import { TranslateDirective } from "./translate"; | ||
import {directive} from "lit/directive.js"; | ||
import {unsafeHTML} from "lit/directives/unsafe-html.js"; | ||
import {ITranslateConfig, Values, ValuesCallback} from "../model"; | ||
import {TranslateDirective} from "./translate"; | ||
import {get} from "../util"; | ||
|
||
/** | ||
* A lit directive that updates the translation as HTML when the language changes. | ||
*/ | ||
export class TranslateUnsafeHTMLDirective extends TranslateDirective { | ||
render (key: string, values?: Values | ValuesCallback, config?: ITranslateConfig) { | ||
super.render(key, values, config); | ||
return unsafeHTML(this.getTranslation()); | ||
} | ||
|
||
updateTranslation () { | ||
const translation = this.getTranslation(); | ||
this.setValue(unsafeHTML(translation)); | ||
} | ||
render(key: string, values?: Values | ValuesCallback, config?: ITranslateConfig) { | ||
return this.renderValue(() => unsafeHTML(get(key, values, config))); | ||
} | ||
} | ||
|
||
export const translateUnsafeHTML = directive(TranslateUnsafeHTMLDirective); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,15 @@ | ||
import { AsyncDirective } from "lit/async-directive.js"; | ||
import { directive } from "lit/directive.js"; | ||
import { ITranslateConfig, LangChangedSubscription, Translation, Values, ValuesCallback } from "../model"; | ||
import { get, listenForLangChanged } from "../util"; | ||
import {directive} from "lit/directive.js"; | ||
import {ITranslateConfig, Values, ValuesCallback} from "../model"; | ||
import {get} from "../util"; | ||
import {LangChangedDirectiveBase} from "./lang-changed-base"; | ||
|
||
/** | ||
* A lit directive that updates the translation when the language changes. | ||
*/ | ||
export class TranslateDirective extends AsyncDirective { | ||
protected langChangedSubscription: LangChangedSubscription | null = null; | ||
protected getTranslation: (() => Translation) = (() => ""); | ||
|
||
render (key: string, values?: Values | ValuesCallback, config?: ITranslateConfig): unknown { | ||
this.getTranslation = () => get(key, values, config); | ||
this.subscribe(); | ||
return this.getTranslation(); | ||
} | ||
|
||
updateTranslation () { | ||
const translation = this.getTranslation(); | ||
this.setValue(translation); | ||
} | ||
|
||
subscribe () { | ||
if (this.langChangedSubscription == null) { | ||
this.langChangedSubscription = listenForLangChanged(this.updateTranslation.bind(this)); | ||
} | ||
} | ||
|
||
unsubscribe () { | ||
if (this.langChangedSubscription != null) { | ||
this.langChangedSubscription(); | ||
} | ||
} | ||
|
||
disconnected () { | ||
this.unsubscribe(); | ||
} | ||
|
||
reconnected () { | ||
this.subscribe(); | ||
} | ||
export class TranslateDirective extends LangChangedDirectiveBase { | ||
render(key: string, values?: Values | ValuesCallback, config?: ITranslateConfig): unknown { | ||
return this.renderValue(() => get(key, values, config)); | ||
} | ||
} | ||
|
||
// Create the directive function | ||
export const translate = directive(TranslateDirective); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters