2020-11-26 11:41:13 +01:00
|
|
|
import { UPLOAD_MEDIA } from "@/graphql/upload";
|
2020-02-18 08:57:00 +01:00
|
|
|
import apolloProvider from "@/vue-apollo";
|
2021-05-12 18:10:07 +02:00
|
|
|
import { ApolloClient } from "@apollo/client/core/ApolloClient";
|
2021-04-30 12:20:31 +02:00
|
|
|
import { Plugin } from "prosemirror-state";
|
2020-02-18 08:57:00 +01:00
|
|
|
import { EditorView } from "prosemirror-view";
|
2021-04-30 12:20:31 +02:00
|
|
|
import Image from "@tiptap/extension-image";
|
2021-05-12 18:10:07 +02:00
|
|
|
import { NormalizedCacheObject } from "@apollo/client/cache";
|
2019-05-29 17:47:52 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
/* eslint-disable class-methods-use-this */
|
2019-05-29 17:47:52 +02:00
|
|
|
|
2021-04-30 12:20:31 +02:00
|
|
|
const CustomImage = Image.extend({
|
|
|
|
name: "image",
|
|
|
|
addAttributes() {
|
2019-05-29 17:47:52 +02:00
|
|
|
return {
|
2021-04-30 12:20:31 +02:00
|
|
|
src: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
alt: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
"data-media-id": {
|
|
|
|
default: null,
|
2019-05-29 17:47:52 +02:00
|
|
|
},
|
|
|
|
};
|
2021-04-30 12:20:31 +02:00
|
|
|
},
|
|
|
|
addProseMirrorPlugins() {
|
2019-05-29 17:47:52 +02:00
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
props: {
|
|
|
|
handleDOMEvents: {
|
2020-02-18 08:57:00 +01:00
|
|
|
drop(view: EditorView<any>, event: Event) {
|
|
|
|
const realEvent = event as DragEvent;
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
realEvent.dataTransfer &&
|
|
|
|
realEvent.dataTransfer.files &&
|
|
|
|
realEvent.dataTransfer.files.length
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false;
|
2019-05-29 17:47:52 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:29:12 +02:00
|
|
|
const images = Array.from(realEvent.dataTransfer.files).filter(
|
2020-11-30 10:24:11 +01:00
|
|
|
(file: any) =>
|
|
|
|
/image/i.test(file.type) && !/svg/i.test(file.type)
|
2020-02-18 08:57:00 +01:00
|
|
|
);
|
2019-05-29 17:47:52 +02:00
|
|
|
|
|
|
|
if (images.length === 0) {
|
2020-02-18 08:57:00 +01:00
|
|
|
return false;
|
2019-05-29 17:47:52 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
realEvent.preventDefault();
|
2019-05-29 17:47:52 +02:00
|
|
|
|
|
|
|
const { schema } = view.state;
|
2020-02-18 08:57:00 +01:00
|
|
|
const coordinates = view.posAtCoords({
|
|
|
|
left: realEvent.clientX,
|
|
|
|
top: realEvent.clientY,
|
|
|
|
});
|
|
|
|
if (!coordinates) return false;
|
2021-05-17 19:01:08 +02:00
|
|
|
const client =
|
|
|
|
apolloProvider.defaultClient as ApolloClient<NormalizedCacheObject>;
|
2019-05-29 17:47:52 +02:00
|
|
|
|
2020-10-09 19:29:12 +02:00
|
|
|
try {
|
|
|
|
images.forEach(async (image) => {
|
|
|
|
const { data } = await client.mutate({
|
2020-11-26 11:41:13 +01:00
|
|
|
mutation: UPLOAD_MEDIA,
|
2020-10-09 19:29:12 +02:00
|
|
|
variables: {
|
|
|
|
file: image,
|
|
|
|
name: image.name,
|
|
|
|
},
|
|
|
|
});
|
2020-11-26 11:41:13 +01:00
|
|
|
const node = schema.nodes.image.create({
|
|
|
|
src: data.uploadMedia.url,
|
|
|
|
"data-media-id": data.uploadMedia.id,
|
|
|
|
});
|
2020-11-30 10:24:11 +01:00
|
|
|
const transaction = view.state.tr.insert(
|
|
|
|
coordinates.pos,
|
|
|
|
node
|
|
|
|
);
|
2020-10-09 19:29:12 +02:00
|
|
|
view.dispatch(transaction);
|
2019-05-29 17:47:52 +02:00
|
|
|
});
|
2020-10-09 19:29:12 +02:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 17:47:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
2021-04-30 12:20:31 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default CustomImage;
|