Skip to content

Commit

Permalink
feat: add attachments from clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Sep 8, 2023
1 parent 56c7d2b commit d924198
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/components/messaging/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Channel from "../../stores/objects/Channel";
import { MessageType, RESTPostAPIChannelMessageJSONBody } from "@spacebarchat/spacebar-api-types/v9";
import { observer } from "mobx-react-lite";
import React, { useMemo } from "react";
import { BaseEditor, Descendant, Node, createEditor } from "slate";
import { HistoryEditor, withHistory } from "slate-history";
import { Editable, ReactEditor, Slate, withReact } from "slate-react";
import { Descendant, Node, createEditor } from "slate";
import { withHistory } from "slate-history";
import { Editable, Slate, withReact } from "slate-react";
import Guild from "../../stores/objects/Guild";
import { Permissions } from "../../utils/Permissions";
import Snowflake from "../../utils/Snowflake";
Expand All @@ -18,17 +18,6 @@ import IconButton from "../IconButton";
import AttachmentUploadList from "./AttachmentUploadList";
import TypingStatus from "./TypingStatus";

type CustomElement = { type: "paragraph"; children: CustomText[] };
type CustomText = { text: string; bold?: true };

declare module "slate" {
interface CustomTypes {
Editor: BaseEditor & ReactEditor & HistoryEditor;
Element: CustomElement;
Text: CustomText;
}
}

const Container = styled.div`
margin-top: -8px;
padding-left: 16px;
Expand Down Expand Up @@ -106,6 +95,19 @@ function MessageInput(props: Props) {
const uploadRef = React.useRef<HTMLInputElement>(null);
const [attachments, setAttachments] = React.useState<File[]>([]);

editor.insertData = (data) => {
console.log("insert data", data);
const text = data.getData("text/plain");
const { files } = data;

if (files && files.length > 0) {
const newAttachments = [...attachments, ...files];
setAttachments(newAttachments);
} else {
editor.insertText(text);
}
};

React.useEffect(() => {
const permission = Permissions.getPermission(app.account!.id, props.guild, props.channel);
setCanSendMessages(permission.has("SEND_MESSAGES"));
Expand Down
38 changes: 38 additions & 0 deletions src/components/messaging/Slate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BaseEditor, BaseRange, Descendant } from "slate";
import { HistoryEditor } from "slate-history";
import { ReactEditor } from "slate-react";

export type EmptyText = {
text: string;
};

export type CustomText = {
bold?: boolean;
italic?: boolean;
code?: boolean;
text: string;
};

export type ParagraphElement = {
type: "paragraph";
align?: string;
children: Descendant[];
};

type CustomElement = ParagraphElement;
export type CustomEditor = BaseEditor &
ReactEditor &
HistoryEditor & {
nodeToDecorations?: Map<Element, Range[]>;
};

declare module "slate" {
interface CustomTypes {
Editor: CustomEditor;
Element: CustomElement;
Text: CustomText | EmptyText;
Range: BaseRange & {
[key: string]: unknown;
};
}
}

0 comments on commit d924198

Please sign in to comment.