Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(md): add inline media upload to lexical actions (#1438)

Seamusaf76447c45f1
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/actions/actions.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/actions/actions.ts
index f6c7b61d98..275cf942e8 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/actions/actions.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/actions/actions.ts
@@ -1,6 +1,8 @@
import CodeBlock from '@icon/regular/code-block.svg';
+import VideoIcon from '@icon/regular/file-video.svg';
import MathIcon from '@icon/regular/function.svg';
import TableIcon from '@icon/regular/grid-four.svg';
+import ImageIcon from '@icon/regular/image.svg';
import LinkIcon from '@icon/regular/link.svg';
import ListBullets from '@icon/regular/list-bullets.svg';
import ListChecks from '@icon/regular/list-checks.svg';
@@ -17,6 +19,7 @@ import type { Component } from 'solid-js';
import { INSERT_HORIZONTAL_RULE_COMMAND } from '..';
import { TRY_INSERT_EQUATION_COMMAND } from '../katex';
import { TRY_INSERT_LINK_COMMAND } from '../links';
+import { TRY_INSERT_MEDIA_UPLOAD_COMMAND } from '../media';
import { NODE_TRANSFORM } from '../node-transform';
export type ActionIcon = string;
@@ -139,16 +142,30 @@ export const ACTIONS: Action[] = [
editor.dispatchCommand(NODE_TRANSFORM, 'list-check');
},
},
- // {
- // id: 'image',
- // name: 'Image',
- // keywords: ['picture', 'photo', 'img'],
- // category: ActionCategory.MEDIA,
- // icon: Image,
- // action: (editor: LexicalEditor) => {
- // console.log('Insert image');
- // },
- // },
+ {
+ id: 'image',
+ name: 'Image',
+ keywords: ['picture', 'photo', 'img', 'upload'],
+ category: ActionCategory.MEDIA,
+ icon: ImageIcon,
+ action: (editor: LexicalEditor) => {
+ queueMicrotask(() => {
+ editor.dispatchCommand(TRY_INSERT_MEDIA_UPLOAD_COMMAND, 'image');
+ });
+ },
+ },
+ {
+ id: 'video',
+ name: 'Video',
+ keywords: ['video', 'movie', 'film', 'upload'],
+ category: ActionCategory.MEDIA,
+ icon: VideoIcon,
+ action: (editor: LexicalEditor) => {
+ queueMicrotask(() => {
+ editor.dispatchCommand(TRY_INSERT_MEDIA_UPLOAD_COMMAND, 'video');
+ });
+ },
+ },
{
id: 'link',
name: 'Link',
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts
index bf6843a84a..28a58f47fe 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts
@@ -88,6 +88,9 @@ export const UPDATE_MEDIA_SIZE_COMMAND: LexicalCommand<
[NodeKey, { width: number; height: number }, MediaType]
> = createCommand('UPDATE_MEDIA_SIZE_COMMAND');
+export const TRY_INSERT_MEDIA_UPLOAD_COMMAND: LexicalCommand<MediaType> =
+ createCommand('TRY_INSERT_MEDIA_UPLOAD_COMMAND');
+
export function validateMediaFile(file: File, mediaType: MediaType): boolean {
const ext = fileExtension(file.name);
return ext != null && blockNameToFileExtensionSet[mediaType].has(ext);
@@ -529,6 +532,44 @@ function registerMediaPlugin(editor: LexicalEditor) {
return true;
},
COMMAND_PRIORITY_NORMAL
+ ),
+
+ editor.registerCommand(
+ TRY_INSERT_MEDIA_UPLOAD_COMMAND,
+ (mediaType) => {
+ const input = document.createElement('input');
+ input.type = 'file';
+ input.accept = mediaType === 'video' ? 'video/*' : 'image/*';
+ input.multiple = true;
+ input.style.display = 'none';
+
+ input.addEventListener('change', () => {
+ const files = input.files;
+ if (!files || files.length === 0) return;
+
+ for (const file of Array.from(files)) {
+ const isImage = file.type.startsWith('image/');
+ const isVideo = file.type.startsWith('video/');
+ const type: MediaType | null = isImage
+ ? 'image'
+ : isVideo
+ ? 'video'
+ : null;
+ if (type) {
+ addMediaFromFile(editor, file, type);
+ }
+ }
+ input.remove();
+ });
+
+ input.addEventListener('cancel', () => {
+ input.remove();
+ });
+ document.body.appendChild(input);
+ input.click();
+ return true;
+ },
+ COMMAND_PRIORITY_NORMAL
)
);
}