Skip to content

Commit

Permalink
Merge pull request #315 from ustacryptact/master
Browse files Browse the repository at this point in the history
Fix new customized paste options
  • Loading branch information
andrerpena authored Apr 29, 2021
2 parents 57eba72 + 07a4156 commit f6d982a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
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

0 comments on commit f6d982a

Please sign in to comment.