Skip to content

Commit

Permalink
fix #47, #48 new gallery field, and started gumlet video
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeFulton committed Jun 24, 2024
1 parent 350db2b commit 3462d68
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 136 deletions.
1 change: 1 addition & 0 deletions components/Editor/CustomExtensions/Figure2/Figure.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ const Figure = Node.create({
.run()
);
} else if (attrs.figureType == "video") {
console.log(attrs)
const content = [{ type: "video", attrs }];

// if (caption) {
Expand Down
112 changes: 65 additions & 47 deletions components/Editor/CustomExtensions/Video/Video.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,98 @@

import { Node, mergeAttributes } from '@tiptap/core'
import { Node, mergeAttributes } from "@tiptap/core";
// https://www.codemzy.com/blog/tiptap-video-embed-extension

const Video = Node.create({
name: 'video', // unique name for the Node
group: 'block', // belongs to the 'block' group of extensions
name: "video", // unique name for the Node
group: "block", // belongs to the 'block' group of extensions
draggable: false, // so we can drag the video
atom: true, // is a single unit

addAttributes() {
return {
"src": {
default: null
src: {
default: null,
},
width:{
original:{
default:null
},
height:{
default:null
gumlet: {
default: null,
},
class:{
default:''
width: {
default: null,
},
autoplay:{
default:'autoplay'
height: {
default: null,
},
loop:{
default:'loop'
class: {
default: "",
},
muted:{
default:''
autoplay: {
default: "autoplay",
},
defaultMuted:{
default:''
loop: {
default: "loop",
},
playsinline:{
default:''
}
}
muted: {
default: "",
},
defaultMuted: {
default: "",
},
playsinline: {
default: "",
},
};
},

parseHTML() {
return [
{
tag: 'video',
tag: "video",
},
]
];
},

renderHTML({ HTMLAttributes }) {
return ['video', mergeAttributes(HTMLAttributes)];
console.log(HTMLAttributes)
if (HTMLAttributes.gumlet) {
let gumletJSON = JSON.parse(HTMLAttributes.gumlet);
console.log(gumletJSON)
// if (gumletJSON.output?.playback_url) {
// HTMLAttributes.src = gumletJSON.output?.playback_url;
// }
}

return ["video", mergeAttributes(HTMLAttributes)];
},

addCommands() {
return {
setVideo: ({ src,position,width,height,classNames, ...attrs }) => ({ chain }) => {
return chain()
.insertContentAt(position,{
type: this.name,
attrs:{src, width, height,class:classNames}
})
// .insertContent({
// type: this.name,
// attrs:{src, width, height,class:classNames}
// })
// set cursor at end of caption field
.command(({ tr, commands }) => {
const { doc, selection } = tr
const position = doc.resolve(selection.to - 2).end()
setVideo:
({ src, position, width, height, classNames, ...attrs }) =>
({ chain }) => {
return (
chain()
.insertContentAt(position, {
type: this.name,
attrs: { src, width, height, class: classNames },
})
// .insertContent({
// type: this.name,
// attrs:{src, width, height,class:classNames}
// })
// set cursor at end of caption field
.command(({ tr, commands }) => {
const { doc, selection } = tr;
const position = doc.resolve(selection.to - 2).end();

return commands.setTextSelection(position)
})
.run()
},
}
}
return commands.setTextSelection(position);
})
.run()
);
},
};
},

// addNodeView() {
// return ({ editor, node }) => {
Expand All @@ -97,4 +115,4 @@ const Video = Node.create({
// },
});

export default Video
export default Video;
68 changes: 43 additions & 25 deletions components/Editor/Menus/FloatingMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react";
import { FloatingMenu } from "@tiptap/react";
import useUser from "@/lib/iron-session/useUser";
import toast from "react-hot-toast";

import { uploadToGumlet } from "@/lib/uploadToGumlet";
import { styled, keyframes } from "@stitches/react";
import { violet, mauve, blackA } from "@radix-ui/colors";
import * as PopoverPrimitive from "@radix-ui/react-popover";
Expand Down Expand Up @@ -141,7 +141,7 @@ const uploadMedia = (event, editor, user, setLoading, setIsOpen) => {

const resp = await fetch(url);
const blob = await resp.blob();
setIsOpen(false)
setIsOpen(false);
let placeholderPos = addPlaceholder(blob, editor);

const file = new File([blob], `${files[0].name || "image.png"}`, {
Expand All @@ -163,26 +163,30 @@ const uploadMedia = (event, editor, user, setLoading, setIsOpen) => {
await axios(configUpload)
.then(async function (response) {
setLoading(false);
setIsOpen(false)
setIsOpen(false);
toast.success("Image Uploaded!", {
duration: 5000,
});
const url = response?.data?.url;
// editor.chain().focus().setFigure({src: url, caption:'enter caption'}).run()
// editor.chain().focus().setImage({ src: url }).run();
editor.chain().setNodeSelection(placeholderPos).setFigure({
figureType: "image",
position: placeholderPos,
src: url,
alt: "",
figcaption: "",
class: "",
}).run();
editor
.chain()
.setNodeSelection(placeholderPos)
.setFigure({
figureType: "image",
position: placeholderPos,
src: url,
alt: "",
figcaption: "",
class: "",
})
.run();
removePlaceholder(editor);
})
.catch(function (error) {
console.log(error);
setIsOpen(false)
setIsOpen(false);
alert("There was an issue with that image. Please try again.");
setTimeout(() => {}, 300);
removePlaceholder(editor);
Expand Down Expand Up @@ -228,7 +232,19 @@ const uploadMedia = (event, editor, user, setLoading, setIsOpen) => {
duration: 5000,
});
let url = response?.data?.url;


// const gumletData = await uploadToGumlet({
// videoUrl: url,
// mediaId: null,
// });

// const gumletAttr = {
// gumletId: gumletData.asset_id,
// source_id: gumletData.source_id,
// collection_id: gumletData.collection_id,
// output: gumletData?.output,
// };

//ensure url has https:// prefix
if (!url.startsWith("https://")) {
url = "https://" + url;
Expand All @@ -239,7 +255,9 @@ const uploadMedia = (event, editor, user, setLoading, setIsOpen) => {
editor.commands.setFigure({
figureType: "video",
position: placeholderPos,
// gumlet: JSON.stringify(gumletAttr),
src: url,
original: url,
class: "",
});
removePlaceholder(editor);
Expand Down Expand Up @@ -327,14 +345,14 @@ const MenuFloating = ({ editor, isSelecting }) => {
setIsOpen(!open);
};

useEffect(()=>{
let editor = document.querySelector('.tiptap.ProseMirror')
if(open){
editor?.classList?.add('menu-open')
}else{
editor?.classList?.remove('menu-open')
useEffect(() => {
let editor = document.querySelector(".tiptap.ProseMirror");
if (open) {
editor?.classList?.add("menu-open");
} else {
editor?.classList?.remove("menu-open");
}
},[open])
}, [open]);

useEffect(() => {
function handleKeyUp(event) {
Expand All @@ -349,7 +367,7 @@ const MenuFloating = ({ editor, isSelecting }) => {
<FloatingMenu
shouldShow={({ editor, view, state, oldState }) => {
if (state?.selection?.$anchor?.parent?.type?.name == "doc") {
setIsOpen(false)
setIsOpen(false);
return false;
}

Expand Down Expand Up @@ -425,9 +443,9 @@ const MenuFloating = ({ editor, isSelecting }) => {
id="img-upload"
accept="image/*,video/*"
className="hidden"
onChange={event =>{
setIsOpen(false)
uploadMedia(event, editor, user, setLoading, setIsOpen)
onChange={event => {
setIsOpen(false);
uploadMedia(event, editor, user, setLoading, setIsOpen);
}}
/>
</>
Expand Down Expand Up @@ -471,7 +489,7 @@ const MenuFloating = ({ editor, isSelecting }) => {
})
.setNodeSelection(pos - 1)
.insertContentAt(pos, "<p></p>")
.setTextSelection(pos+1)
.setTextSelection(pos + 1)
.focus()
.enter()
.run();
Expand Down
Loading

0 comments on commit 3462d68

Please sign in to comment.