debian-mirror-gitlab/app/assets/javascripts/content_editor/extensions/attachment.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { Extension } from '@tiptap/core';
import { Plugin, PluginKey } from 'prosemirror-state';
import { handleFileEvent } from '../services/upload_helpers';
export default Extension.create({
name: 'attachment',
2022-01-26 12:08:38 +05:30
addOptions() {
return {
uploadsPath: null,
renderMarkdown: null,
2022-05-07 20:08:51 +05:30
eventHub: null,
2022-01-26 12:08:38 +05:30
};
2021-10-27 15:23:28 +05:30
},
addCommands() {
return {
uploadAttachment: ({ file }) => () => {
2022-05-07 20:08:51 +05:30
const { uploadsPath, renderMarkdown, eventHub } = this.options;
2021-10-27 15:23:28 +05:30
2022-05-07 20:08:51 +05:30
return handleFileEvent({
file,
uploadsPath,
renderMarkdown,
editor: this.editor,
eventHub,
});
2021-10-27 15:23:28 +05:30
},
};
},
addProseMirrorPlugins() {
const { editor } = this;
return [
new Plugin({
key: new PluginKey('attachment'),
props: {
handlePaste: (_, event) => {
2022-05-07 20:08:51 +05:30
const { uploadsPath, renderMarkdown, eventHub } = this.options;
2021-10-27 15:23:28 +05:30
return handleFileEvent({
editor,
file: event.clipboardData.files[0],
uploadsPath,
renderMarkdown,
2022-05-07 20:08:51 +05:30
eventHub,
2021-10-27 15:23:28 +05:30
});
},
handleDrop: (_, event) => {
2022-05-07 20:08:51 +05:30
const { uploadsPath, renderMarkdown, eventHub } = this.options;
2021-10-27 15:23:28 +05:30
return handleFileEvent({
editor,
file: event.dataTransfer.files[0],
uploadsPath,
renderMarkdown,
2022-05-07 20:08:51 +05:30
eventHub,
2021-10-27 15:23:28 +05:30
});
},
},
}),
];
},
});