Allow to have value as html or json

This commit is contained in:
2025-10-02 14:05:47 +02:00
parent d0488e98e0
commit 980048b6ab

View File

@@ -70,14 +70,6 @@ class TpRichTextBox extends FormElement(LitElement) {
]; ];
} }
constructor() {
super();
this.extensions = [];
this.menuMode = 'hidden'; // 'hidden', 'selection', 'suggestion'
this.activeSuggestionType = null; // 'emoji', 'user', null
this._slotObserver = new MutationObserver(this._processSlotChanges.bind(this));
}
render() { render() {
return html` return html`
<!-- Selection-based floating menu with all controls --> <!-- Selection-based floating menu with all controls -->
@@ -99,10 +91,20 @@ class TpRichTextBox extends FormElement(LitElement) {
editor: { type: Object }, editor: { type: Object },
extensions: { type: Array }, extensions: { type: Array },
menuMode: { type: String, reflect: true }, menuMode: { type: String, reflect: true },
activeSuggestionType: { type: String } activeSuggestionType: { type: String },
asHtml: { type: Boolean }
}; };
} }
constructor() {
super();
this.extensions = [];
this.menuMode = 'hidden'; // 'hidden', 'selection', 'suggestion'
this.activeSuggestionType = null; // 'emoji', 'user', null
this.asHtml = false;
this._slotObserver = new MutationObserver(this._processSlotChanges.bind(this));
}
firstUpdated() { firstUpdated() {
// Get initial extensions from slot // Get initial extensions from slot
this._processChildExtensions(); this._processChildExtensions();
@@ -328,18 +330,40 @@ class TpRichTextBox extends FormElement(LitElement) {
get value() { get value() {
if (this.editor) { if (this.editor) {
return this.editor.getJSON(); return this.asHtml ? this.editor.getHTML() : this.editor.getJSON();
} }
return this.asHtml ? '' : {};
}
set value(content) {
this.updateComplete.then(() => {
if (this.editor) {
this.editor.commands.setContent(content);
}
});
}
/**
* Get the editor content as JSON
* @returns {Object} The editor content as JSON object
*/
getJson() {
if (this.editor) {
return this.editor.getJSON();
}
return {}; return {};
} }
set value(jsonContent) { /**
this.updateComplete.then(() => { * Get the editor content as HTML
if (this.editor) { * @returns {string} The editor content as HTML string
this.editor.commands.setContent(jsonContent); */
} getHtml() {
}); if (this.editor) {
return this.editor.getHTML();
}
return '';
} }
} }