2022-03-04 15:16:11 +00:00
|
|
|
import { Extension } from '@tiptap/core';
|
|
|
|
import { Plugin, PluginKey } from 'prosemirror-state';
|
|
|
|
import { __ } from '~/locale';
|
|
|
|
import { VARIANT_DANGER } from '~/flash';
|
2022-04-22 12:08:38 +00:00
|
|
|
import createMarkdownDeserializer from '../services/gl_api_markdown_deserializer';
|
2022-03-04 15:16:11 +00:00
|
|
|
import {
|
|
|
|
ALERT_EVENT,
|
|
|
|
LOADING_CONTENT_EVENT,
|
|
|
|
LOADING_SUCCESS_EVENT,
|
|
|
|
LOADING_ERROR_EVENT,
|
|
|
|
EXTENSION_PRIORITY_HIGHEST,
|
|
|
|
} from '../constants';
|
2022-04-22 03:08:49 +00:00
|
|
|
import CodeBlockHighlight from './code_block_highlight';
|
|
|
|
import Diagram from './diagram';
|
|
|
|
import Frontmatter from './frontmatter';
|
2022-03-04 15:16:11 +00:00
|
|
|
|
|
|
|
const TEXT_FORMAT = 'text/plain';
|
|
|
|
const HTML_FORMAT = 'text/html';
|
|
|
|
const VS_CODE_FORMAT = 'vscode-editor-data';
|
2022-04-22 03:08:49 +00:00
|
|
|
const CODE_BLOCK_NODE_TYPES = [CodeBlockHighlight.name, Diagram.name, Frontmatter.name];
|
2022-03-04 15:16:11 +00:00
|
|
|
|
|
|
|
export default Extension.create({
|
|
|
|
name: 'pasteMarkdown',
|
|
|
|
priority: EXTENSION_PRIORITY_HIGHEST,
|
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
renderMarkdown: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
pasteMarkdown: (markdown) => () => {
|
|
|
|
const { editor, options } = this;
|
|
|
|
const { renderMarkdown, eventHub } = options;
|
|
|
|
const deserializer = createMarkdownDeserializer({ render: renderMarkdown });
|
|
|
|
|
|
|
|
eventHub.$emit(LOADING_CONTENT_EVENT);
|
|
|
|
|
|
|
|
deserializer
|
|
|
|
.deserialize({ schema: editor.schema, content: markdown })
|
2022-03-14 18:07:46 +00:00
|
|
|
.then(({ document }) => {
|
|
|
|
if (!document) {
|
2022-03-04 15:16:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { state, view } = editor;
|
|
|
|
const { tr, selection } = state;
|
|
|
|
|
2022-03-14 18:07:46 +00:00
|
|
|
tr.replaceWith(selection.from - 1, selection.to, document.content);
|
2022-03-04 15:16:11 +00:00
|
|
|
view.dispatch(tr);
|
|
|
|
eventHub.$emit(LOADING_SUCCESS_EVENT);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
eventHub.$emit(ALERT_EVENT, {
|
|
|
|
message: __('An error occurred while pasting text in the editor. Please try again.'),
|
|
|
|
variant: VARIANT_DANGER,
|
|
|
|
});
|
|
|
|
eventHub.$emit(LOADING_ERROR_EVENT);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
addProseMirrorPlugins() {
|
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
key: new PluginKey('pasteMarkdown'),
|
|
|
|
props: {
|
|
|
|
handlePaste: (_, event) => {
|
|
|
|
const { clipboardData } = event;
|
|
|
|
const content = clipboardData.getData(TEXT_FORMAT);
|
|
|
|
const hasHTML = clipboardData.types.some((type) => type === HTML_FORMAT);
|
|
|
|
const hasVsCode = clipboardData.types.some((type) => type === VS_CODE_FORMAT);
|
|
|
|
const vsCodeMeta = hasVsCode ? JSON.parse(clipboardData.getData(VS_CODE_FORMAT)) : {};
|
|
|
|
const language = vsCodeMeta.mode;
|
|
|
|
|
|
|
|
if (!content || (hasHTML && !hasVsCode) || (hasVsCode && language !== 'markdown')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-22 03:08:49 +00:00
|
|
|
// if a code block is active, paste as plain text
|
|
|
|
if (CODE_BLOCK_NODE_TYPES.some((type) => this.editor.isActive(type))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-04 15:16:11 +00:00
|
|
|
this.editor.commands.pasteMarkdown(content);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
});
|