From eb50e3a4ac0e4df0dcb047744e7bdefc3439699a Mon Sep 17 00:00:00 2001 From: Felix Feng Date: Tue, 5 Nov 2024 15:45:39 +0800 Subject: [PATCH 1/2] fix --- .../placeholder/transforms/insertMedia.ts | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/packages/media/src/react/placeholder/transforms/insertMedia.ts b/packages/media/src/react/placeholder/transforms/insertMedia.ts index 9c88405b7f..4dc76a873a 100644 --- a/packages/media/src/react/placeholder/transforms/insertMedia.ts +++ b/packages/media/src/react/placeholder/transforms/insertMedia.ts @@ -1,6 +1,7 @@ import type { PlateEditor } from '@udecode/plate-common/react'; import { insertNodes, nanoid, withoutNormalizing } from '@udecode/plate-common'; +import { Path } from 'slate'; import { type TPlaceholderElement, BasePlaceholderPlugin } from '../../../lib'; import { PlaceholderPlugin } from '../PlaceholderPlugin'; @@ -9,7 +10,11 @@ import { createUploadError, isUploadError } from '../utils/createUploadError'; import { getMediaType } from '../utils/getMediaType'; import { validateFiles } from '../utils/validateFiles'; -export const insertMedia = (editor: PlateEditor, files: FileList): any => { +export const insertMedia = ( + editor: PlateEditor, + files: FileList, + at?: Path +): any => { const api = editor.getApi(PlaceholderPlugin); const uploadConfig = editor.getOption(PlaceholderPlugin, 'uploadConfig'); const multiple = editor.getOption(PlaceholderPlugin, 'multiple'); @@ -48,16 +53,30 @@ export const insertMedia = (editor: PlateEditor, files: FileList): any => { ); } - Array.from(files).forEach((file) => { + let currentAt: Path | undefined; + + Array.from(files).forEach((file, index) => { + if (index === 0) { + if (at) { + currentAt = at; + } + } else { + currentAt = currentAt ? Path.next(currentAt) : undefined; + } + const id = nanoid(); withoutNormalizing(editor, () => - insertNodes(editor, { - id, - children: [{ text: '' }], - mediaType: getMediaType(file, uploadConfig)!, - type: editor.getType(BasePlaceholderPlugin), - }) + insertNodes( + editor, + { + id, + children: [{ text: '' }], + mediaType: getMediaType(file, uploadConfig)!, + type: editor.getType(BasePlaceholderPlugin), + }, + { at: currentAt, nextBlock: false } + ) ); api.placeholder.addUploadingFile(id, file); From d0720cf28bea38d541c514ef18022504b38442b7 Mon Sep 17 00:00:00 2001 From: Felix Feng Date: Tue, 5 Nov 2024 15:46:03 +0800 Subject: [PATCH 2/2] docs --- .changeset/tame-socks-admire.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tame-socks-admire.md diff --git a/.changeset/tame-socks-admire.md b/.changeset/tame-socks-admire.md new file mode 100644 index 0000000000..c2e37beb53 --- /dev/null +++ b/.changeset/tame-socks-admire.md @@ -0,0 +1,5 @@ +--- +'@udecode/plate-media': patch +--- + +Add `at` in `insertMedia` api.