Files
dokuwiki-plugin-prosemirror/script/plugins/Menu/MenuItems/ImageMenuItemDispatcher.js
Michael Große 5d25733619 ♻️ (Menu) Refactor menu to be more modular and adapt to different schemas
This is done in preparations for footnotes which should not have other footnotes and headings as
part of their schema and hence also not part of their menu.

Preparation for #20
2018-07-18 17:03:31 +02:00

56 lines
2.0 KiB
JavaScript

import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher';
import MediaForm from '../../../nodeviews/MediaForm';
import MenuItem from '../MenuItem';
import { svgIcon } from '../MDI';
export default class ImageMenuItemDispatcher extends AbstractMenuItemDispatcher {
static isAvailable(schema) {
return !!schema.nodes.image;
}
static getMenuItem(schema) {
if (!this.isAvailable(schema)) {
throw new Error('Image is missing in provided Schema!');
}
return new MenuItem({
command: (state, dispatch) => {
const { $from } = state.selection;
const index = $from.index();
if (!$from.parent.canReplaceWith(index, index, schema.nodes.image)) {
return false;
}
if (dispatch) {
let textContent = '';
state.selection.content().content.descendants((node) => {
textContent += node.textContent;
return false;
});
const mediaForm = new MediaForm();
if (textContent) {
mediaForm.setCaption(textContent);
mediaForm.setSource(textContent);
}
mediaForm.show();
mediaForm.on('submit', MediaForm.resolveSubmittedLinkData(
{},
mediaForm,
(newAttrs) => {
dispatch(state.tr.replaceSelectionWith(schema.nodes.image.create(newAttrs)));
mediaForm.off('submit');
mediaForm.hide();
mediaForm.resetForm();
},
));
}
return true;
},
icon: svgIcon('image'),
label: 'Insert Image',
});
}
}