mirror of
https://github.com/cosmocode/dokuwiki-plugin-prosemirror.git
synced 2025-07-29 21:06:13 +00:00

Labels and icons suggested that only image links were possible. Now it should be clear that file links are possible as well.
56 lines
2.0 KiB
JavaScript
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'],
|
|
});
|
|
}
|
|
}
|