Files
dokuwiki-plugin-prosemirror/script/plugins/Menu/MenuItems/ImageMenuItemDispatcher.js
Anna Dabrowska abb3a0e47c More accurate icons and labels in media wizard
Labels and icons suggested that only image links were possible. Now it should be clear that file links are possible as well.
2020-07-21 21:04:36 +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('file-image-outline'),
label: LANG.plugins.prosemirror['label:image'],
});
}
}