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 arbitrary file upload on drop to markdown block (#426)

Seamuse67c33fc66f8
diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsx
index a77a2060f6..5cd4fe06cc 100644
--- a/js/app/packages/block-md/component/MarkdownEditor.tsx
+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx
@@ -99,7 +99,6 @@ import { ScopedPortal } from '@core/component/ScopedPortal';
import { toast } from '@core/component/Toast/Toast';
import {
blockNameToFileExtensions,
- blockNameToMimeTypes,
fileTypeToBlockName,
} from '@core/constant/allBlocks';
import {
@@ -109,7 +108,7 @@ import {
LOCAL_ONLY,
} from '@core/constant/featureFlags';
import { fileDrop } from '@core/directive/fileDrop';
-import { HEIC_EXTENSIONS, HEIC_MIME_TYPES } from '@core/heic/constants';
+import { HEIC_EXTENSIONS } from '@core/heic/constants';
import { blockElementSignal } from '@core/signal/blockElement';
import {
blockFileSignal,
@@ -122,6 +121,7 @@ import { useCanComment, useCanEdit } from '@core/signal/permissions';
import { useBlockDocumentName } from '@core/util/currentBlockDocumentName';
import { isSourceDSS, isSourceSyncService } from '@core/util/source';
import { bufToString } from '@core/util/string';
+import { uploadFile } from '@core/util/upload';
import WarningIcon from '@icon/regular/warning.svg';
import {
$createDocumentMentionNode,
@@ -142,10 +142,16 @@ import { normalizeEnterPlugin } from 'core/component/LexicalMarkdown/plugins/nor
import {
autoRegister,
lazyRegister,
+ registerInternalLayoutShiftListener,
registerRootEventListener,
} from 'core/component/LexicalMarkdown/plugins/shared/utils';
import { createMethodRegistration } from 'core/orchestrator';
-import { $getRoot, $isElementNode, type EditorState } from 'lexical';
+import {
+ $getRoot,
+ $getSelection,
+ $isElementNode,
+ type EditorState,
+} from 'lexical';
import {
type Accessor,
createEffect,
@@ -166,7 +172,6 @@ import {
} from '../signal/generateSignal';
import { blockDataSignal, mdStore } from '../signal/markdownBlockData';
import { useBlockSave, useSaveMarkdownDocument } from '../signal/save';
-
import { MarkdownCollabProvider } from './MarkdownCollabProvider';
import { MarkdownPopup } from './MarkdownPopup';
@@ -202,12 +207,7 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
...blockNameToFileExtensions.image,
...HEIC_EXTENSIONS,
];
- const IMAGE_MIME_TYPES_HEIC = [
- ...blockNameToMimeTypes.image,
- ...HEIC_MIME_TYPES,
- ];
const VIDEO_EXTENSIONS = blockNameToFileExtensions.video;
- const VIDEO_MIME_TYPES = blockNameToMimeTypes.video;
const blockHandle = blockHandleSignal.get;
createMethodRegistration(blockHandle, {
@@ -277,12 +277,13 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
const { editor, plugins, cleanup: cleanupPlugins } = lexicalWrapper;
- let [state, setState] = createSignal<EditorState>(editor.getEditorState());
+ const [state, setState] = createSignal<EditorState>(editor.getEditorState());
setMdStore('editor', editor);
setMdStore('plugins', plugins);
+
const [editorFocus, setEditorFocus] = createSignal(false);
- editorFocusSignal(editor, setEditorFocus);
+ autoRegister(editorFocusSignal(editor, setEditorFocus));
const mentionsMenuOperations = createMenuOperations();
const emojiMenuOperations = createMenuOperations();
@@ -304,7 +305,10 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
};
// handler for the file paste directive
- const onPasteFiles = async (files: File[]) => {
+ const onPasteFiles = async (
+ files: File[],
+ position?: ReturnType<typeof getDragDropPosition>
+ ) => {
for (const file of files) {
const ext = fileExtension(file.name);
@@ -316,18 +320,45 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
} else if (ext != null && VIDEO_EXTENSIONS.includes(ext)) {
const res = await addMediaFromFile(editor, file, 'video');
success = res.success;
+ } else {
+ const res = await uploadFile(file, 'dss');
+ if (!res.failed && !res.pending && res.type === 'document') {
+ success = true;
+ const mentionId = await trackMention(
+ blockId,
+ 'document',
+ res.documentId
+ );
+ editor.update(() => {
+ const mention = $createDocumentMentionNode({
+ documentId: res.documentId,
+ blockName: fileTypeToBlockName(res.fileType),
+ mentionUuid: mentionId,
+ documentName: file.name,
+ });
+ if (position && position.key) {
+ if (position.position === 'before') {
+ $insertWrappedBefore(position.key, mention);
+ } else {
+ $insertWrappedAfter(position.key, mention);
+ }
+ } else {
+ $getSelection()?.insertNodes([mention]);
+ }
+ mention.selectEnd();
+ });
+ }
}
if (!success) {
- toast.failure('Invalid attachment file(s)');
+ toast.failure('Invalid file for upload');
}
}
};
// handler for the file drop directive - file paste plus calc drop pos.
const onDropFiles = (files: File[], e: DragEvent) => {
- getDragDropPosition(e, true);
- onPasteFiles(files);
+ onPasteFiles(files, getDragDropPosition(e, true));
};
// store for the drag insert pluign.
@@ -631,6 +662,18 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
})
);
+ const observeClickTargetHeight = () => {
+ const blockEl = blockElement();
+ if (!blockEl) {
+ setClickTargetHeight(EDITOR_PADDING_BOTTOM);
+ return;
+ }
+ const blockBottom = blockEl.getBoundingClientRect().bottom;
+ const targetHeight =
+ blockBottom - mountRef.getBoundingClientRect().bottom - 40;
+ setClickTargetHeight(Math.max(targetHeight, EDITOR_PADDING_BOTTOM));
+ };
+
onMount(() => {
setMdStore('selection', lexicalWrapper.selection);
editor.setRootElement(mountRef);
@@ -643,21 +686,7 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
})
);
- // watch the height of the content editable to set the height of
- // the focus target
- const editorRefObserver = new ResizeObserver(() => {
- const blockEl = blockElement();
- if (!blockEl) {
- setClickTargetHeight(EDITOR_PADDING_BOTTOM);
- return;
- }
- const blockBottom =
- blockEl?.getBoundingClientRect().bottom ?? window.innerHeight;
-
- const targetHeight =
- blockBottom - mountRef.getBoundingClientRect().bottom - 40;
- setClickTargetHeight(Math.max(targetHeight, EDITOR_PADDING_BOTTOM));
- });
+ const editorRefObserver = new ResizeObserver(observeClickTargetHeight);
editorRefObserver.observe(mountRef);
onCleanup(() => {
@@ -681,7 +710,9 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
if (clickFocusFlag) return;
e.preventDefault();
editor.focus(undefined, { defaultSelection: 'rootStart' });
- })
+ }),
+ // adjust click target height on layout shift
+ registerInternalLayoutShiftListener(editor, observeClickTargetHeight)
);
const additionalCleanups: Array<() => void> = [];
@@ -905,11 +936,6 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
class="relative"
ref={editorContainerRef}
use:fileDrop={{
- acceptedMimeTypes: [...IMAGE_MIME_TYPES_HEIC, ...VIDEO_MIME_TYPES],
- acceptedFileExtensions: [
- ...IMAGE_EXTENSIONS_HEIC,
- ...VIDEO_EXTENSIONS,
- ],
onDrop: (files, event) => {
onDropFiles(files, event);
},
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/drag-insert/dragInsertPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/drag-insert/dragInsertPlugin.ts
index 39d3e94e0b..4cf7ab438b 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/drag-insert/dragInsertPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/drag-insert/dragInsertPlugin.ts
@@ -79,7 +79,7 @@ function elementKeyToDomRect(editor: LexicalEditor, key: NodeKey) {
}
/**
- * Get an instersion point from a drag event.DOMRect
+ * Get an insertion point from a drag event.DOMRect
*/
export function calculateInsertPoint(
editor: LexicalEditor,