1 import {EditorFormDefinition, EditorSelectFormFieldDefinition} from "../framework/forms";
2 import {EditorUiContext} from "../framework/core";
3 import {$createLinkNode} from "@lexical/link";
4 import {$createTextNode, $getSelection} from "lexical";
5 import {$createImageNode} from "../../nodes/image";
6 import {setEditorContentFromHtml} from "../../actions";
7 import {$createMediaNodeFromHtml} from "../../nodes/media";
10 export const link: EditorFormDefinition = {
12 action(formData, context: EditorUiContext) {
13 context.editor.update(() => {
15 const selection = $getSelection();
17 const linkNode = $createLinkNode(formData.get('url')?.toString() || '', {
18 title: formData.get('title')?.toString() || '',
19 target: formData.get('target')?.toString() || '',
21 linkNode.append($createTextNode(formData.get('text')?.toString() || ''));
23 selection?.insertNodes([linkNode]);
34 label: 'Text to display',
44 label: 'Open link in...',
49 'New window': '_blank',
51 } as EditorSelectFormFieldDefinition,
55 export const image: EditorFormDefinition = {
57 action(formData, context: EditorUiContext) {
58 context.editor.update(() => {
59 const selection = $getSelection();
60 const imageNode = $createImageNode(formData.get('src')?.toString() || '', {
61 alt: formData.get('alt')?.toString() || '',
62 height: Number(formData.get('height')?.toString() || '0'),
63 width: Number(formData.get('width')?.toString() || '0'),
65 selection?.insertNodes([imageNode]);
76 label: 'Alternative description',
93 export const media: EditorFormDefinition = {
95 action(formData, context: EditorUiContext) {
97 // TODO - Get media from selection
99 const embedCode = (formData.get('embed') || '').toString().trim();
101 context.editor.update(() => {
102 const node = $createMediaNodeFromHtml(embedCode);
103 // TODO - Replace existing or insert new
109 const src = (formData.get('src') || '').toString().trim();
110 const height = (formData.get('height') || '').toString().trim();
111 const width = (formData.get('width') || '').toString().trim();
113 // TODO - Update existing or insert new
133 // TODO - Tabbed interface to separate this option
135 label: 'Paste your embed code below:',
142 export const source: EditorFormDefinition = {
144 action(formData, context: EditorUiContext) {
145 setEditorContentFromHtml(context.editor, formData.get('source')?.toString() || '');