2020-11-26 11:41:13 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2020-08-27 14:40:35 +02:00
|
|
|
// @ts-nocheck
|
2020-02-18 08:57:00 +01:00
|
|
|
import { Node } from "tiptap";
|
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";
|
|
|
|
import ApolloClient from "apollo-client";
|
|
|
|
import { NormalizedCacheObject } from "apollo-cache-inmemory";
|
|
|
|
import { NodeType, NodeSpec } from "prosemirror-model";
|
|
|
|
import { EditorState, Plugin, TextSelection } from "prosemirror-state";
|
|
|
|
import { DispatchFn } from "tiptap-commands";
|
|
|
|
import { EditorView } from "prosemirror-view";
|
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
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
export default class Image extends Node {
|
2019-05-29 17:47:52 +02:00
|
|
|
get name() {
|
2020-02-18 08:57:00 +01:00
|
|
|
return "image";
|
2019-05-29 17:47:52 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
get schema(): NodeSpec {
|
2019-05-29 17:47:52 +02:00
|
|
|
return {
|
|
|
|
inline: true,
|
|
|
|
attrs: {
|
|
|
|
src: {},
|
|
|
|
alt: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
default: null,
|
|
|
|
},
|
2020-11-26 11:41:13 +01:00
|
|
|
"data-media-id": {},
|
2019-05-29 17:47:52 +02:00
|
|
|
},
|
2020-02-18 08:57:00 +01:00
|
|
|
group: "inline",
|
2019-05-29 17:47:52 +02:00
|
|
|
draggable: true,
|
|
|
|
parseDOM: [
|
|
|
|
{
|
2020-11-26 11:41:13 +01:00
|
|
|
tag: "img",
|
2020-02-18 08:57:00 +01:00
|
|
|
getAttrs: (dom: any) => ({
|
|
|
|
src: dom.getAttribute("src"),
|
|
|
|
title: dom.getAttribute("title"),
|
|
|
|
alt: dom.getAttribute("alt"),
|
2020-11-26 11:41:13 +01:00
|
|
|
"data-media-id": dom.getAttribute("data-media-id"),
|
2019-05-29 17:47:52 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
2020-02-18 08:57:00 +01:00
|
|
|
toDOM: (node: any) => ["img", node.attrs],
|
2019-05-29 17:47:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
commands({ type }: { type: NodeType }): any {
|
|
|
|
return (attrs: { [key: string]: string }) => (state: EditorState, dispatch: DispatchFn) => {
|
|
|
|
const { selection }: { selection: TextSelection } = state;
|
2019-05-29 17:47:52 +02:00
|
|
|
const position = selection.$cursor ? selection.$cursor.pos : selection.$to.pos;
|
|
|
|
const node = type.create(attrs);
|
|
|
|
const transaction = state.tr.insert(position, node);
|
|
|
|
dispatch(transaction);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get plugins() {
|
|
|
|
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(
|
|
|
|
(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;
|
|
|
|
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-10-09 19:29:12 +02:00
|
|
|
const transaction = view.state.tr.insert(coordinates.pos, node);
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|