From b69c475ffe09a2088807d714e8a20e07aa2b3b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Wallstr=C3=B6m?= <21986391+FredrikWallstrom@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:07:52 +0100 Subject: [PATCH] feat(text-editor): add insertHtml function to properly parse html string Co-authored-by: Adrian Schmidt --- etc/lime-elements.api.md | 1 + .../examples/text-editor-custom-triggers.tsx | 85 +++++++++++++--- .../trigger/create-html-inserter.spec.ts | 98 +++++++++++++++++++ .../plugins/trigger/create-html-inserter.ts | 25 +++++ .../plugins/trigger/factory.ts | 29 +++++- .../plugins/trigger/inserter.ts | 83 ++++++++++------ .../prosemirror-adapter.tsx | 5 +- .../text-editor/text-editor.types.ts | 7 +- 8 files changed, 279 insertions(+), 54 deletions(-) create mode 100644 src/components/text-editor/prosemirror-adapter/plugins/trigger/create-html-inserter.spec.ts create mode 100644 src/components/text-editor/prosemirror-adapter/plugins/trigger/create-html-inserter.ts diff --git a/etc/lime-elements.api.md b/etc/lime-elements.api.md index 9dd3535e60..dba08c3adf 100644 --- a/etc/lime-elements.api.md +++ b/etc/lime-elements.api.md @@ -2417,6 +2417,7 @@ export interface TabPanelComponent { // @alpha (undocumented) export interface TextEditor { insert: (input: TextEditorNode | string) => void; + insertHtml: (input: string) => Promise; // (undocumented) stopTrigger: () => void; } diff --git a/src/components/text-editor/examples/text-editor-custom-triggers.tsx b/src/components/text-editor/examples/text-editor-custom-triggers.tsx index 38d2a8f979..659fcc4be1 100644 --- a/src/components/text-editor/examples/text-editor-custom-triggers.tsx +++ b/src/components/text-editor/examples/text-editor-custom-triggers.tsx @@ -27,8 +27,11 @@ import { TextEditor, TriggerEventDetail } from '../text-editor.types'; * supplied methods will effectivly replace the trigger content in the text editor with * the content of choice. * - * In this example we pass either a text or a `limel-chip` representing some chosen user - * in a mention like situation. + * In this example we pass either plain text, HTML string, or a `limel-chip` + * representing some chosen user in a mention like situation. + * + * :::note + * Changing the contentType resets the text editor with an empty value. */ @Component({ tag: 'limel-example-text-editor-triggers', @@ -43,13 +46,16 @@ export class TextEditorCustomTriggersExample { private triggerState: string = ''; @State() - private inputText: string = ''; + private tagValue: string = ''; @State() private isPickerOpen: boolean = false; @State() - private insertMode: 'text' | 'chip' = 'text'; + private insertMode: 'text' | 'chip' | 'html' = 'text'; + + @State() + private contentType: 'markdown' | 'html' = 'markdown'; @State() private items: Array> = [ @@ -76,6 +82,22 @@ export class TextEditorCustomTriggersExample { id: '2', title: 'chip', }, + { + id: '3', + title: 'html', + }, + ]; + + private contentTypeButtons: Button[] = [ + { + id: '1', + title: 'markdown', + selected: true, + }, + { + id: '2', + title: 'html', + }, ]; private triggerFunction?: TextEditor; @@ -85,11 +107,11 @@ export class TextEditorCustomTriggersExample { this.setupEventHandlers(); } - @Watch('inputText') - protected watchInputText() { + @Watch('tagValue') + protected watchTagValue() { if (this.isPickerOpen) { this.visibleItems = this.items.filter((item: MenuItem) => - item.text.toLowerCase().includes(this.inputText), + item.text.toLowerCase().includes(this.tagValue), ); } } @@ -180,6 +202,16 @@ export class TextEditorCustomTriggersExample { public render() { return [ + + + , this.renderPicker(), , - Insert mode: - +
, @@ -252,12 +288,12 @@ export class TextEditorCustomTriggersExample { private handleTriggerStop = () => { this.triggerState = 'stop'; - this.inputText = ''; + this.tagValue = ''; this.isPickerOpen = false; }; private handleTriggerChange = (event: CustomEvent) => { - this.inputText = event.detail.value.toLowerCase(); + this.tagValue = event.detail.value.toLowerCase(); }; private handleChange = (event: CustomEvent) => { @@ -274,9 +310,26 @@ export class TextEditorCustomTriggersExample { this.insertMode = event.detail.title as any; }; + private handleContentTypeChange = (event: CustomEvent