Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new customized paste options #315

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/commands/command-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class CommandOrchestrator {
return this.executeCommand(
this.pasteOptions.command || getDefaultSaveImageCommandName(),
{
saveImage: this.pasteOptions.saveImage,
pasteOptions: this.pasteOptions,
event: event
} as PasteCommandContext
);
Expand All @@ -158,7 +158,7 @@ export class CommandOrchestrator {
return this.executeCommand(
this.pasteOptions.command || getDefaultSaveImageCommandName(),
{
saveImage: this.pasteOptions.saveImage,
pasteOptions: this.pasteOptions,
event: event
} as PasteCommandContext
);
Expand All @@ -173,7 +173,7 @@ export class CommandOrchestrator {
return this.executeCommand(
this.pasteOptions.command || getDefaultSaveImageCommandName(),
{
saveImage: this.pasteOptions.saveImage,
pasteOptions: this.pasteOptions,
event: event
} as PasteCommandContext
);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface CommandContext {
export interface PasteCommandContext extends CommandContext {
type: "paste";
event: React.ClipboardEvent | React.DragEvent | React.ChangeEvent;
saveImage: SaveImageHandler;
pasteOptions: PasteOptions;
}

export type ToolbarCommands = string[][];
Expand Down
48 changes: 45 additions & 3 deletions src/commands/default-commands/save-image-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
Command,
CommandContext,
ExecuteOptions,
PasteCommandContext
PasteCommandContext,
PasteOptions
} from "../command";
import { readFileAsync } from "../../util/files";
import { getBreaksNeededForEmptyLineBefore } from "../../util/MarkdownUtil";
Expand All @@ -26,6 +27,42 @@ function fileListToArray(list: FileList): Array<File> {
return result;
}

function filterItems(
items: File[],
{ multiple, accept }: Pick<PasteOptions, "multiple" | "accept">
): File[] {
let filteredItems = items;

if (!multiple) {
filteredItems = filteredItems.slice(0, 1);
}

if (accept) {
//https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#unique_file_type_specifiers
const acceptedTypes = accept.split(",");
const fileExtensions = new Set(
acceptedTypes.filter(t => /^\.\w+/.test(t)).map(t => t.split(".")[1])
);
const mimeTypes = new Set(
acceptedTypes.filter(t => /^[-\w.]+\/[-\w.]+$/.test(t))
);
const anyTypes = new Set(
acceptedTypes
.filter(t => /(audio|video|image)\/\*/.test(t))
.map(t => t.split("/")[0])
);

filteredItems = filteredItems.filter(
f =>
fileExtensions.has(f.name.split(".")[1]) ||
mimeTypes.has(f.type) ||
anyTypes.has(f.type.split("/")[0])
);
}

return filteredItems;
}

export const saveImageCommand: Command = {
async execute({
initialState,
Expand All @@ -37,7 +74,10 @@ export const saveImageCommand: Command = {
throw new Error("wrong context");
}
const pasteContext = context as PasteCommandContext;
const { event, saveImage } = pasteContext;
const {
event,
pasteOptions: { saveImage, multiple, accept }
} = pasteContext;

const items = isPasteEvent(context)
? dataTransferToArray((event as React.ClipboardEvent).clipboardData.items)
Expand All @@ -47,7 +87,9 @@ export const saveImageCommand: Command = {
(event as React.ChangeEvent<HTMLInputElement>).target.files
);

for (const index in items) {
const filteredItems = filterItems(items, { multiple, accept });

for (const index in filteredItems) {
const initialState = textApi.getState();
const breaksBeforeCount = getBreaksNeededForEmptyLineBefore(
initialState.text,
Expand Down
16 changes: 14 additions & 2 deletions src/components/ReactMde.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export interface ReactMdeState {
editorHeight: number;
}

const pasteOptionDefaults: Required<Omit<
PasteOptions,
"saveImage" | "command"
>> = {
accept: "image/*",
multiple: false
};

export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
/**
* "finalRefs" is a clone of "props.refs" except that undefined refs are set to default values
Expand Down Expand Up @@ -110,6 +118,8 @@ export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
this.finalRefs.textarea,
this.props.l18n,
this.props.paste
? { ...pasteOptionDefaults, ...this.props.paste }
: undefined
);
const minEditorHeight = Math.min(
props.maxEditorHeight,
Expand Down Expand Up @@ -243,8 +253,10 @@ export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
<input
className={classNames("image-input")}
type="file"
accept={this.props.paste.accept || "image/*"}
multiple={this.props.paste.multiple || true}
accept={this.props.paste.accept ?? pasteOptionDefaults.accept}
multiple={
this.props.paste.multiple ?? pasteOptionDefaults.multiple
}
onChange={this.handleImageSelection}
/>
<span>{l18n.pasteDropSelect}</span>
Expand Down