feat: add pdf extension to editor

This commit is contained in:
swve 2023-02-19 19:28:38 +01:00
parent 429a95c50a
commit fdcee29a0d
7 changed files with 297 additions and 6 deletions

View file

@ -20,6 +20,8 @@ import Youtube from "@tiptap/extension-youtube";
import VideoBlock from "./Extensions/Video/VideoBlock";
import { Save } from "lucide-react";
import MathEquationBlock from "./Extensions/MathEquation/MathEquationBlock";
import PDFBlockComponent from "./Extensions/PDF/PDFBlockComponent";
import PDFBlock from "./Extensions/PDF/PDFBlock";
interface Editor {
content: string;
@ -59,6 +61,10 @@ function Editor(props: Editor) {
editable: true,
lecture: props.lecture,
}),
PDFBlock.configure({
editable: true,
lecture: props.lecture,
}),
Youtube.configure({
controls: true,
modestBranding: true,

View file

@ -0,0 +1,35 @@
import { mergeAttributes, Node } from "@tiptap/core";
import { ReactNodeViewRenderer } from "@tiptap/react";
import PDFBlockComponent from "./PDFBlockComponent";
export default Node.create({
name: "blockPDF",
group: "block",
atom: true,
addAttributes() {
return {
fileObject: {
default: null,
},
};
},
parseHTML() {
return [
{
tag: "block-pdf",
},
];
},
renderHTML({ HTMLAttributes }) {
return ["block-pdf", mergeAttributes(HTMLAttributes), 0];
},
addNodeView() {
return ReactNodeViewRenderer(PDFBlockComponent);
},
});

View file

@ -0,0 +1,88 @@
import { NodeViewWrapper } from "@tiptap/react";
import React from "react";
import styled from "styled-components";
import { AlertCircle, AlertTriangle, FileText, Image, ImagePlus, Info } from "lucide-react";
import { getPDFFile, uploadNewPDFFile } from "../../../../services/files/documents";
import { getBackendUrl } from "../../../../services/config";
function PDFBlockComponent(props: any) {
const [pdf, setPDF] = React.useState(null);
const [isLoading, setIsLoading] = React.useState(false);
const [fileObject, setfileObject] = React.useState(props.node.attrs.fileObject);
const handlePDFChange = (event: React.ChangeEvent<any>) => {
setPDF(event.target.files[0]);
};
const handleSubmit = async (e: any) => {
e.preventDefault();
setIsLoading(true);
let object = await uploadNewPDFFile(pdf, props.extension.options.lecture.lecture_id);
setIsLoading(false);
setfileObject(object);
props.updateAttributes({
fileObject: object,
});
};
return (
<NodeViewWrapper className="block-pdf">
{!fileObject && (
<BlockPDFWrapper contentEditable={props.extension.options.editable}>
<div>
<FileText color="#e1e0e0" size={50} />
<br />
</div>
<input onChange={handlePDFChange} type="file" name="" id="" />
<br />
<button onClick={handleSubmit}>Submit</button>
</BlockPDFWrapper>
)}
{fileObject && (
<BlockPDF>
<iframe
src={`${getBackendUrl()}content/uploads/files/documents/${props.extension.options.lecture.lecture_id}/${fileObject.file_id}.${
fileObject.file_format
}`}
/>
</BlockPDF>
)}
{isLoading && (
<div>
<AlertTriangle color="#e1e0e0" size={50} />
</div>
)}
</NodeViewWrapper>
);
}
export default PDFBlockComponent;
const BlockPDFWrapper = styled.div`
display: flex;
flex-direction: column;
background: #f9f9f9;
border-radius: 3px;
padding: 30px;
min-height: 74px;
border: ${(props) => (props.contentEditable ? "2px dashed #713f1117" : "none")};
// center
align-items: center;
justify-content: center;
text-align: center;
font-size: 14px;
`;
const BlockPDF = styled.div`
display: flex;
flex-direction: column;
img {
width: 100%;
border-radius: 6px;
height: 300px;
// cover
object-fit: cover;
}
`;
const PDFNotFound = styled.div``;

View file

@ -1,6 +1,6 @@
import styled from "styled-components";
import { FontBoldIcon, FontItalicIcon, StrikethroughIcon, ArrowLeftIcon, ArrowRightIcon, OpacityIcon } from "@radix-ui/react-icons";
import { AlertCircle, AlertTriangle, ImagePlus, Info, Sigma, Video, Youtube } from "lucide-react";
import { AlertCircle, AlertTriangle, FileText, ImagePlus, Info, Sigma, Video, Youtube } from "lucide-react";
export const ToolbarButtons = ({ editor }: any) => {
if (!editor) {
@ -103,6 +103,19 @@ export const ToolbarButtons = ({ editor }: any) => {
>
<Sigma size={15} />
</ToolBtn>
<ToolBtn
onClick={() =>
editor
.chain()
.focus()
.insertContent({
type: "blockPDF",
})
.run()
}
>
<FileText size={15} />
</ToolBtn>
</ToolButtonsWrapper>
);
};