Lexical: Added UI translation support

This commit is contained in:
Dan Brown 2024-09-22 12:29:06 +01:00
parent 2add15bd72
commit ef3de1050f
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
9 changed files with 20 additions and 14 deletions

View File

@ -10,6 +10,12 @@ export class WysiwygEditor extends Component {
/** @var {SimpleWysiwygEditorInterface|null} */ /** @var {SimpleWysiwygEditorInterface|null} */
this.editor = null; this.editor = null;
const translations = {
...window.editor_translations,
imageUploadErrorText: this.$opts.imageUploadErrorText,
serverUploadLimitText: this.$opts.serverUploadLimitText,
};
window.importVersioned('wysiwyg').then(wysiwyg => { window.importVersioned('wysiwyg').then(wysiwyg => {
const editorContent = this.input.value; const editorContent = this.input.value;
this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent, { this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent, {
@ -17,10 +23,7 @@ export class WysiwygEditor extends Component {
pageId: Number(this.$opts.pageId), pageId: Number(this.$opts.pageId),
darkMode: document.documentElement.classList.contains('dark-mode'), darkMode: document.documentElement.classList.contains('dark-mode'),
textDirection: this.$opts.textDirection, textDirection: this.$opts.textDirection,
translations: { translations,
imageUploadErrorText: this.$opts.imageUploadErrorText,
serverUploadLimitText: this.$opts.serverUploadLimitText,
},
}); });
}); });

View File

@ -84,7 +84,7 @@ export const alignRight: EditorButtonDefinition = {
}; };
export const alignJustify: EditorButtonDefinition = { export const alignJustify: EditorButtonDefinition = {
label: 'Align justify', label: 'Justify',
icon: alignJustifyIcon, icon: alignJustifyIcon,
action(context: EditorUiContext) { action(context: EditorUiContext) {
context.editor.update(() => setAlignmentForSelection(context, 'justify')); context.editor.update(() => setAlignmentForSelection(context, 'justify'));

View File

@ -17,7 +17,7 @@ import {
function buildCalloutButton(category: CalloutCategory, name: string): EditorButtonDefinition { function buildCalloutButton(category: CalloutCategory, name: string): EditorButtonDefinition {
return { return {
label: `${name} Callout`, label: name,
action(context: EditorUiContext) { action(context: EditorUiContext) {
context.editor.update(() => { context.editor.update(() => {
$toggleSelectionBlockNodeType( $toggleSelectionBlockNodeType(

View File

@ -56,7 +56,7 @@ export const redo: EditorButtonDefinition = {
export const source: EditorButtonDefinition = { export const source: EditorButtonDefinition = {
label: 'Source code', label: 'Source',
icon: sourceIcon, icon: sourceIcon,
async action(context: EditorUiContext) { async action(context: EditorUiContext) {
const modal = context.manager.createModal('source'); const modal = context.manager.createModal('source');

View File

@ -30,12 +30,12 @@ export const bold: EditorButtonDefinition = buildFormatButton('Bold', 'bold', bo
export const italic: EditorButtonDefinition = buildFormatButton('Italic', 'italic', italicIcon); export const italic: EditorButtonDefinition = buildFormatButton('Italic', 'italic', italicIcon);
export const underline: EditorButtonDefinition = buildFormatButton('Underline', 'underline', underlinedIcon); export const underline: EditorButtonDefinition = buildFormatButton('Underline', 'underline', underlinedIcon);
export const textColor: EditorBasicButtonDefinition = {label: 'Text color', icon: textColorIcon}; export const textColor: EditorBasicButtonDefinition = {label: 'Text color', icon: textColorIcon};
export const highlightColor: EditorBasicButtonDefinition = {label: 'Highlight color', icon: highlightIcon}; export const highlightColor: EditorBasicButtonDefinition = {label: 'Background color', icon: highlightIcon};
export const strikethrough: EditorButtonDefinition = buildFormatButton('Strikethrough', 'strikethrough', strikethroughIcon); export const strikethrough: EditorButtonDefinition = buildFormatButton('Strikethrough', 'strikethrough', strikethroughIcon);
export const superscript: EditorButtonDefinition = buildFormatButton('Superscript', 'superscript', superscriptIcon); export const superscript: EditorButtonDefinition = buildFormatButton('Superscript', 'superscript', superscriptIcon);
export const subscript: EditorButtonDefinition = buildFormatButton('Subscript', 'subscript', subscriptIcon); export const subscript: EditorButtonDefinition = buildFormatButton('Subscript', 'subscript', subscriptIcon);
export const code: EditorButtonDefinition = buildFormatButton('Inline Code', 'code', codeIcon); export const code: EditorButtonDefinition = buildFormatButton('Inline code', 'code', codeIcon);
export const clearFormating: EditorButtonDefinition = { export const clearFormating: EditorButtonDefinition = {
label: 'Clear formatting', label: 'Clear formatting',
icon: formatClearIcon, icon: formatClearIcon,

View File

@ -5,7 +5,7 @@ import {cellProperties, rowProperties, tableProperties} from "./forms/tables";
export const modals: Record<string, EditorFormModalDefinition> = { export const modals: Record<string, EditorFormModalDefinition> = {
link: { link: {
title: 'Insert/Edit link', title: 'Insert/Edit Link',
form: link, form: link,
}, },
image: { image: {

View File

@ -54,7 +54,7 @@ export class EditorFormField extends EditorUiElement {
if (this.definition.type === 'select') { if (this.definition.type === 'select') {
const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel
const labels = Object.keys(options); const labels = Object.keys(options);
const optionElems = labels.map(label => el('option', {value: options[label]}, [label])); const optionElems = labels.map(label => el('option', {value: options[label]}, [this.trans(label)]));
input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems); input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
} else if (this.definition.type === 'textarea') { } else if (this.definition.type === 'textarea') {
input = el('textarea', {id, name: this.definition.name, class: 'editor-form-field-input'}); input = el('textarea', {id, name: this.definition.name, class: 'editor-form-field-input'});

View File

@ -19,7 +19,10 @@ export function buildEditorUI(container: HTMLElement, element: HTMLElement, scro
editorDOM: element, editorDOM: element,
scrollDOM: scrollContainer, scrollDOM: scrollContainer,
manager, manager,
translate: (text: string): string => text, // TODO - Implement translate(text: string): string {
const translations = options.translations;
return translations[text] || text;
},
error(error: string|Error): void { error(error: string|Error): void {
const message = error instanceof Error ? error.message : error; const message = error instanceof Error ? error.message : error;
window.$events.error(message); // TODO - Translate window.$events.error(message); // TODO - Translate

View File

@ -9,7 +9,7 @@
<div class="editor-container flex-container-column flex-fill flex" refs="wysiwyg-editor@edit-container"> <div class="editor-container flex-container-column flex-fill flex" refs="wysiwyg-editor@edit-container">
</div> </div>
<div id="lexical-debug" style="white-space: pre-wrap; font-size: 12px; height: 200px; overflow-y: scroll; background-color: #000; padding: 1rem; border-radius: 4px; color: #FFF;"></div> {{-- <div id="lexical-debug" style="white-space: pre-wrap; font-size: 12px; height: 200px; overflow-y: scroll; background-color: #000; padding: 1rem; border-radius: 4px; color: #FFF;"></div>--}}
<textarea refs="wysiwyg-editor@input" id="html-editor" hidden="hidden" name="html" rows="5">{{ old('html') ?? $model->html ?? '' }}</textarea> <textarea refs="wysiwyg-editor@input" id="html-editor" hidden="hidden" name="html" rows="5">{{ old('html') ?? $model->html ?? '' }}</textarea>
</div> </div>
@ -18,4 +18,4 @@
<div class="text-neg text-small">{{ $errors->first('html') }}</div> <div class="text-neg text-small">{{ $errors->first('html') }}</div>
@endif @endif
{{--TODO - @include('form.editor-translations')--}} @include('form.editor-translations')