2024-08-14 02:36:18 +08:00
|
|
|
import {
|
|
|
|
EditorFormDefinition,
|
|
|
|
EditorFormField,
|
|
|
|
EditorFormTabs,
|
|
|
|
EditorSelectFormFieldDefinition
|
|
|
|
} from "../../framework/forms";
|
2024-08-02 22:28:54 +08:00
|
|
|
import {EditorUiContext} from "../../framework/core";
|
2024-08-20 21:54:53 +08:00
|
|
|
import {$createNodeSelection, $createTextNode, $getSelection, $insertNodes, $setSelection} from "lexical";
|
2024-08-14 02:36:18 +08:00
|
|
|
import {$isImageNode, ImageNode} from "../../../nodes/image";
|
2024-08-20 21:54:53 +08:00
|
|
|
import {$createLinkNode, $isLinkNode, LinkNode} from "@lexical/link";
|
2024-08-02 22:28:54 +08:00
|
|
|
import {$createMediaNodeFromHtml, $createMediaNodeFromSrc, $isMediaNode, MediaNode} from "../../../nodes/media";
|
2024-07-28 00:25:30 +08:00
|
|
|
import {$insertNodeToNearestRoot} from "@lexical/utils";
|
2024-08-20 20:07:33 +08:00
|
|
|
import {$getNodeFromSelection, getLastSelection} from "../../../utils/selection";
|
2024-08-14 02:36:18 +08:00
|
|
|
import {EditorFormModal} from "../../framework/modals";
|
|
|
|
import {EditorActionField} from "../../framework/blocks/action-field";
|
|
|
|
import {EditorButton} from "../../framework/buttons";
|
|
|
|
import {showImageManager} from "../../../utils/images";
|
|
|
|
import searchImageIcon from "@icons/editor/image-search.svg";
|
2024-08-16 18:22:12 +08:00
|
|
|
import searchIcon from "@icons/search.svg";
|
|
|
|
import {showLinkSelector} from "../../../utils/links";
|
2024-08-16 19:29:40 +08:00
|
|
|
import {LinkField} from "../../framework/blocks/link-field";
|
2024-08-20 21:54:53 +08:00
|
|
|
import {insertOrUpdateLink} from "../../../utils/formats";
|
2024-08-14 02:36:18 +08:00
|
|
|
|
|
|
|
export function $showImageForm(image: ImageNode, context: EditorUiContext) {
|
|
|
|
const imageModal: EditorFormModal = context.manager.createModal('image');
|
|
|
|
const height = image.getHeight();
|
|
|
|
const width = image.getWidth();
|
|
|
|
|
|
|
|
const formData = {
|
|
|
|
src: image.getSrc(),
|
|
|
|
alt: image.getAltText(),
|
|
|
|
height: height === 0 ? '' : String(height),
|
|
|
|
width: width === 0 ? '' : String(width),
|
|
|
|
};
|
|
|
|
|
|
|
|
imageModal.show(formData);
|
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-08-02 22:28:54 +08:00
|
|
|
export const image: EditorFormDefinition = {
|
|
|
|
submitText: 'Apply',
|
|
|
|
async action(formData, context: EditorUiContext) {
|
|
|
|
context.editor.update(() => {
|
2024-08-20 20:07:33 +08:00
|
|
|
const selection = getLastSelection(context.editor);
|
|
|
|
const selectedImage = $getNodeFromSelection(selection, $isImageNode);
|
2024-08-14 02:36:18 +08:00
|
|
|
if ($isImageNode(selectedImage)) {
|
|
|
|
selectedImage.setSrc(formData.get('src')?.toString() || '');
|
|
|
|
selectedImage.setAltText(formData.get('alt')?.toString() || '');
|
|
|
|
|
|
|
|
selectedImage.setWidth(Number(formData.get('width')?.toString() || '0'));
|
|
|
|
selectedImage.setHeight(Number(formData.get('height')?.toString() || '0'));
|
|
|
|
}
|
2024-08-02 22:28:54 +08:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
2024-08-14 02:36:18 +08:00
|
|
|
build() {
|
|
|
|
return new EditorActionField(
|
|
|
|
new EditorFormField({
|
|
|
|
label: 'Source',
|
|
|
|
name: 'src',
|
|
|
|
type: 'text',
|
|
|
|
}),
|
|
|
|
new EditorButton({
|
|
|
|
label: 'Browse files',
|
|
|
|
icon: searchImageIcon,
|
|
|
|
action(context: EditorUiContext) {
|
|
|
|
showImageManager((image) => {
|
|
|
|
const modal = context.manager.getActiveModal('image');
|
|
|
|
if (modal) {
|
|
|
|
modal.getForm().setValues({
|
|
|
|
src: image.thumbs?.display || image.url,
|
|
|
|
alt: image.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
2024-08-02 22:28:54 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Alternative description',
|
|
|
|
name: 'alt',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Width',
|
|
|
|
name: 'width',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Height',
|
|
|
|
name: 'height',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-08-20 21:54:53 +08:00
|
|
|
export function $showLinkForm(link: LinkNode|null, context: EditorUiContext) {
|
|
|
|
const linkModal = context.manager.createModal('link');
|
2024-06-01 23:49:47 +08:00
|
|
|
|
2024-08-20 21:54:53 +08:00
|
|
|
let formDefaults = {};
|
|
|
|
if (link) {
|
|
|
|
formDefaults = {
|
|
|
|
url: link.getURL(),
|
|
|
|
text: link.getTextContent(),
|
|
|
|
title: link.getTitle(),
|
|
|
|
target: link.getTarget(),
|
|
|
|
}
|
2024-08-16 18:22:12 +08:00
|
|
|
|
2024-08-20 21:54:53 +08:00
|
|
|
context.editor.update(() => {
|
|
|
|
const selection = $createNodeSelection();
|
|
|
|
selection.add(link.getKey());
|
|
|
|
$setSelection(selection);
|
|
|
|
});
|
|
|
|
}
|
2024-06-01 23:49:47 +08:00
|
|
|
|
2024-08-20 21:54:53 +08:00
|
|
|
linkModal.show(formDefaults);
|
|
|
|
}
|
2024-06-01 23:49:47 +08:00
|
|
|
|
2024-08-20 21:54:53 +08:00
|
|
|
export const link: EditorFormDefinition = {
|
|
|
|
submitText: 'Apply',
|
|
|
|
async action(formData, context: EditorUiContext) {
|
|
|
|
insertOrUpdateLink(context.editor, {
|
|
|
|
url: formData.get('url')?.toString() || '',
|
|
|
|
title: formData.get('title')?.toString() || '',
|
|
|
|
target: formData.get('target')?.toString() || '',
|
|
|
|
text: formData.get('text')?.toString() || '',
|
2024-06-01 23:49:47 +08:00
|
|
|
});
|
2024-05-30 23:50:55 +08:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
2024-08-16 18:22:12 +08:00
|
|
|
build() {
|
|
|
|
return new EditorActionField(
|
2024-08-16 19:29:40 +08:00
|
|
|
new LinkField(new EditorFormField({
|
2024-08-16 18:22:12 +08:00
|
|
|
label: 'URL',
|
|
|
|
name: 'url',
|
|
|
|
type: 'text',
|
2024-08-16 19:29:40 +08:00
|
|
|
})),
|
2024-08-16 18:22:12 +08:00
|
|
|
new EditorButton({
|
|
|
|
label: 'Browse links',
|
|
|
|
icon: searchIcon,
|
|
|
|
action(context: EditorUiContext) {
|
|
|
|
showLinkSelector(entity => {
|
|
|
|
const modal = context.manager.getActiveModal('link');
|
|
|
|
if (modal) {
|
|
|
|
modal.getForm().setValues({
|
|
|
|
url: entity.link,
|
|
|
|
text: entity.name,
|
|
|
|
title: entity.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
2024-05-30 23:50:55 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Text to display',
|
|
|
|
name: 'text',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Title',
|
|
|
|
name: 'title',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Open link in...',
|
|
|
|
name: 'target',
|
|
|
|
type: 'select',
|
|
|
|
valuesByLabel: {
|
|
|
|
'Current window': '',
|
|
|
|
'New window': '_blank',
|
|
|
|
}
|
|
|
|
} as EditorSelectFormFieldDefinition,
|
|
|
|
],
|
2024-06-06 01:43:42 +08:00
|
|
|
};
|
|
|
|
|
2024-07-25 23:25:08 +08:00
|
|
|
export const media: EditorFormDefinition = {
|
|
|
|
submitText: 'Save',
|
2024-07-28 00:25:30 +08:00
|
|
|
async action(formData, context: EditorUiContext) {
|
|
|
|
const selectedNode: MediaNode|null = await (new Promise((res, rej) => {
|
|
|
|
context.editor.getEditorState().read(() => {
|
|
|
|
const node = $getNodeFromSelection($getSelection(), $isMediaNode);
|
|
|
|
res(node as MediaNode|null);
|
|
|
|
});
|
|
|
|
}));
|
2024-07-25 23:25:08 +08:00
|
|
|
|
|
|
|
const embedCode = (formData.get('embed') || '').toString().trim();
|
|
|
|
if (embedCode) {
|
|
|
|
context.editor.update(() => {
|
|
|
|
const node = $createMediaNodeFromHtml(embedCode);
|
2024-07-28 00:25:30 +08:00
|
|
|
if (selectedNode && node) {
|
|
|
|
selectedNode.replace(node)
|
|
|
|
} else if (node) {
|
2024-09-10 19:14:26 +08:00
|
|
|
$insertNodes([node]);
|
2024-07-28 00:25:30 +08:00
|
|
|
}
|
2024-07-25 23:25:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-07-28 00:25:30 +08:00
|
|
|
context.editor.update(() => {
|
|
|
|
const src = (formData.get('src') || '').toString().trim();
|
|
|
|
const height = (formData.get('height') || '').toString().trim();
|
|
|
|
const width = (formData.get('width') || '').toString().trim();
|
2024-07-25 23:25:08 +08:00
|
|
|
|
2024-07-28 00:25:30 +08:00
|
|
|
const updateNode = selectedNode || $createMediaNodeFromSrc(src);
|
|
|
|
updateNode.setSrc(src);
|
|
|
|
updateNode.setWidthAndHeight(width, height);
|
|
|
|
if (!selectedNode) {
|
2024-09-10 19:14:26 +08:00
|
|
|
$insertNodes([updateNode]);
|
2024-07-28 00:25:30 +08:00
|
|
|
}
|
|
|
|
});
|
2024-07-25 23:25:08 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
2024-07-28 19:48:58 +08:00
|
|
|
build() {
|
|
|
|
return new EditorFormTabs([
|
|
|
|
{
|
|
|
|
label: 'General',
|
|
|
|
contents: [
|
|
|
|
{
|
|
|
|
label: 'Source',
|
|
|
|
name: 'src',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Width',
|
|
|
|
name: 'width',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Height',
|
|
|
|
name: 'height',
|
|
|
|
type: 'text',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Embed',
|
|
|
|
contents: [
|
|
|
|
{
|
|
|
|
label: 'Paste your embed code below:',
|
|
|
|
name: 'embed',
|
|
|
|
type: 'textarea',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}
|
2024-07-25 23:25:08 +08:00
|
|
|
},
|
|
|
|
],
|
2024-05-30 23:50:55 +08:00
|
|
|
};
|