Skip to content

Commit

Permalink
Merge pull request #4340 from serlo/feat/expose-language-in-editor-we…
Browse files Browse the repository at this point in the history
…b-component

feat(editor-web-component): Expose language prop
  • Loading branch information
CodingDive authored Dec 6, 2024
2 parents a3339ea + 32511fe commit 33a230c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/editor-web-component/src/editor-web-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type BaseEditor,
defaultPlugins,
EditorPluginType,
type SupportedLanguage,
} from '@serlo/editor'
import styles from '@serlo/editor/dist/style.css?raw'
import React, { Suspense, lazy } from 'react'
Expand Down Expand Up @@ -43,6 +44,8 @@ export class EditorWebComponent extends HTMLElement {

private _isProductionEnvironment: boolean = false

private _language: SupportedLanguage = 'de' as const

constructor() {
super()

Expand All @@ -57,6 +60,7 @@ export class EditorWebComponent extends HTMLElement {
'editor-variant',
'plugins',
'is-production-environment',
'language',
]
}

Expand All @@ -75,6 +79,11 @@ export class EditorWebComponent extends HTMLElement {
this.plugins = JSON.parse(newValue) as EditorPluginType[]
} else if (name === 'is-production-environment') {
this.isProductionEnvironment = newValue === 'true'
} else if (name === 'language' && oldValue !== newValue) {
// Validates the language attribute. Will need to keep this in sync with
// the SupportedLanguage type, if we add more language support!
const validatedLanguage = newValue === 'en' ? 'en' : 'de'
this.language = validatedLanguage
}
}

Expand Down Expand Up @@ -160,6 +169,23 @@ export class EditorWebComponent extends HTMLElement {
this.mountReactComponent()
}

get language(): SupportedLanguage {
return this._language
}

set language(value: SupportedLanguage) {
if (value !== 'en' && value !== 'de') {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Invalid language value: ${value}. Supported values are 'en' or 'de'.`
)
}

this._language = value
this.setAttribute('language', value)
this.mountReactComponent()
}

connectedCallback() {
this.appendChild(this.container)
this.loadAndApplyStyles()
Expand Down Expand Up @@ -218,6 +244,7 @@ export class EditorWebComponent extends HTMLElement {
this._currentState = newState
this.broadcastNewState(newState)
}}
language={this.language}
>
{(editor) => {
this._history = editor.history
Expand All @@ -229,6 +256,7 @@ export class EditorWebComponent extends HTMLElement {
<SerloRenderer
state={this.initialState}
editorVariant={this.editorVariant}
language={this.language}
/>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/editor-web-component/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export {
pluginMenuEn,
EditorPluginType,
defaultPlugins,
type SupportedLanguage,
} from '@serlo/editor'
export { EditorWebComponent } from './editor-web-component'
1 change: 1 addition & 0 deletions packages/editor/src/package/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { SerloEditor, type SerloEditorProps } from './editor'
export { SerloRenderer, type SerloRendererProps } from './serlo-renderer'

export type { SupportedLanguage } from '@editor/types/language-data'
export type { BaseEditor } from '@editor/core'

// We need to make a distinction between entires on our menu and technical
Expand Down

0 comments on commit 33a230c

Please sign in to comment.