Skip to content

Commit

Permalink
Merge branch 'main' into feat/block
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Nov 5, 2024
2 parents 78836cb + b37ea84 commit bd3f63f
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/docx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @udecode/plate-docx-serializer

## 39.3.2

## 39.3.1

## 39.3.0

## 39.2.13
Expand Down
4 changes: 2 additions & 2 deletions packages/docx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@udecode/plate-docx",
"version": "39.3.0",
"version": "39.3.2",
"description": "Docx serializer plugin for Plate",
"keywords": [
"docx",
Expand Down Expand Up @@ -49,7 +49,7 @@
"@udecode/plate-heading": "39.0.0",
"@udecode/plate-indent": "39.0.0",
"@udecode/plate-indent-list": "39.1.10",
"@udecode/plate-media": "39.3.0",
"@udecode/plate-media": "39.3.2",
"@udecode/plate-table": "39.1.6",
"validator": "^13.12.0"
},
Expand Down
12 changes: 12 additions & 0 deletions packages/media/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @udecode/plate-media

## 39.3.2

### Patch Changes

- [#3725](https://github.com/udecode/plate/pull/3725) by [@felixfeng33](https://github.com/felixfeng33)`insertMedia`: Should insert in the current block if it is empty.

## 39.3.1

### Patch Changes

- [#3723](https://github.com/udecode/plate/pull/3723) by [@felixfeng33](https://github.com/felixfeng33) – Add `at` in `insertMedia` api.

## 39.3.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/media/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@udecode/plate-media",
"version": "39.3.0",
"version": "39.3.2",
"description": "Plate Media plugin",
"keywords": [
"plate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type props = {
type: string;
url: string;
id?: string;
initialHeight?: number;
initialWidth?: number;
isUpload?: boolean;
Expand Down
28 changes: 24 additions & 4 deletions packages/media/src/react/placeholder/PlaceholderPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type ExtendConfig, bindFirst } from '@udecode/plate-common';
import {
type ExtendConfig,
bindFirst,
getAncestorNode,
getNodeString,
} from '@udecode/plate-common';
import { findEventRange, toTPlatePlugin } from '@udecode/plate-common/react';

import type { AllowedFileType } from './internal/mimes';
Expand Down Expand Up @@ -112,7 +117,6 @@ export const PlaceholderPlugin = toTPlatePlugin<
handlers: {
onDrop: ({ editor, event, tf }) => {
// using DnD plugin by default
if (!getOption('disabledDndPlugin')) return;

const { files } = event.dataTransfer;

Expand All @@ -121,6 +125,9 @@ export const PlaceholderPlugin = toTPlatePlugin<
/** Without this, the dropped file replaces the page */
event.preventDefault();
event.stopPropagation();

if (!getOption('disabledDndPlugin')) return;

/**
* When we drop a file, the selection won't move automatically to the
* drop location. Find the location from the event and upload the files
Expand All @@ -134,15 +141,28 @@ export const PlaceholderPlugin = toTPlatePlugin<

return true;
},
onPaste: ({ event, tf }) => {
onPaste: ({ editor, event, tf }) => {
const { files } = event.clipboardData;

if (files.length === 0) return false;

event.preventDefault();
event.stopPropagation();

tf.insert.media(files);
let inserted = false;
const ancestor = getAncestorNode(editor);

if (ancestor) {
const [node, path] = ancestor;

if (getNodeString(node).length === 0) {
tf.insert.media(files, path);
inserted = true;
}
}
if (!inserted) {
tf.insert.media(files);
}

return true;
},
Expand Down
35 changes: 27 additions & 8 deletions packages/media/src/react/placeholder/transforms/insertMedia.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
Expand Down Expand Up @@ -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<TPlaceholderElement>(editor, {
id,
children: [{ text: '' }],
mediaType: getMediaType(file, uploadConfig)!,
type: editor.getType(BasePlaceholderPlugin),
})
insertNodes<TPlaceholderElement>(
editor,
{
id,
children: [{ text: '' }],
mediaType: getMediaType(file, uploadConfig)!,
type: editor.getType(BasePlaceholderPlugin),
},
{ at: currentAt, nextBlock: false }
)
);

api.placeholder.addUploadingFile(id, file);
Expand Down
4 changes: 4 additions & 0 deletions packages/plate/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @udecode/plate

## 39.3.2

## 39.3.1

## 39.3.0

## 39.2.21
Expand Down
6 changes: 3 additions & 3 deletions packages/plate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@udecode/plate",
"version": "39.3.0",
"version": "39.3.2",
"description": "Plate – a plugin system for slate",
"keywords": [
"contentEditable",
Expand Down Expand Up @@ -68,7 +68,7 @@
"@udecode/plate-common": "39.2.21",
"@udecode/plate-csv": "39.1.6",
"@udecode/plate-diff": "39.0.0",
"@udecode/plate-docx": "39.3.0",
"@udecode/plate-docx": "39.3.2",
"@udecode/plate-find-replace": "39.0.0",
"@udecode/plate-floating": "39.1.6",
"@udecode/plate-font": "39.0.0",
Expand All @@ -84,7 +84,7 @@
"@udecode/plate-link": "39.1.9",
"@udecode/plate-list": "39.0.0",
"@udecode/plate-markdown": "39.2.0",
"@udecode/plate-media": "39.3.0",
"@udecode/plate-media": "39.3.2",
"@udecode/plate-mention": "39.0.0",
"@udecode/plate-node-id": "39.0.0",
"@udecode/plate-normalizers": "39.0.0",
Expand Down

0 comments on commit bd3f63f

Please sign in to comment.