@atlaskit/editor-plugin-media
Version:
Media plugin for @atlaskit/editor-core
113 lines (112 loc) • 3.24 kB
JavaScript
import { SetAttrsStep } from '@atlaskit/adf-schema/steps';
import { stateKey as mediaPluginKey } from '../pm-plugins/plugin-key';
/**
* Note that Media Inline is inserted like a media single node into the media plugin state.
* Though it is not of type mediaSingle, it shares the same `findMediaSingleNode` method
*
*/
export const findMediaSingleNode = (mediaPluginState, id) => {
const {
mediaNodes
} = mediaPluginState;
// Array#find... no IE support
return mediaNodes.reduce((memo, nodeWithPos) => {
if (memo) {
return memo;
}
const {
node
} = nodeWithPos;
if (node.attrs.id === id) {
return nodeWithPos;
}
return memo;
}, null);
};
export const findAllMediaSingleNodes = (mediaPluginState, id) => {
const {
mediaNodes
} = mediaPluginState;
return mediaNodes.filter(nodeWithHandler => nodeWithHandler.node.attrs.id === id);
};
export const isMediaNode = (pos, state) => {
const node = state.doc.nodeAt(pos);
return node && ['media', 'mediaInline'].includes(node.type.name);
};
export const updateAllMediaSingleNodesAttrs = (id, attrs) => (state, dispatch) => {
const mediaPluginState = mediaPluginKey.getState(state);
if (!mediaPluginState) {
return false;
}
let mediaNodes;
mediaNodes = findAllMediaSingleNodes(mediaPluginState, id);
const validMediaNodePositions = mediaNodes.reduce((acc, {
getPos
}) => {
const pos = getPos();
if (typeof pos !== 'number' || isNaN(pos) || typeof pos === 'number' && !isMediaNode(pos, state)) {
return acc;
}
acc.push(pos);
return acc;
}, []);
if (validMediaNodePositions.length === 0) {
return false;
}
const tr = state.tr;
validMediaNodePositions.forEach(pos => tr.step(new SetAttrsStep(pos, attrs)));
tr.setMeta('addToHistory', false);
if (dispatch) {
dispatch(tr);
}
return true;
};
export const updateCurrentMediaNodeAttrs = (attrs, mediaNode) => (state, dispatch) => {
const pos = mediaNode.getPos();
if (typeof pos !== 'number' || isNaN(pos) || typeof pos === 'number' && !isMediaNode(pos, state)) {
return false;
}
const tr = state.tr;
tr.step(new SetAttrsStep(pos, attrs));
tr.setMeta('addToHistory', false);
if (dispatch) {
dispatch(tr);
}
return true;
};
export const updateMediaSingleNodeAttrs = (id, attrs) => (state, dispatch) => {
const mediaPluginState = mediaPluginKey.getState(state);
if (!mediaPluginState) {
return false;
}
const mediaNodeWithPos = findMediaSingleNode(mediaPluginState, id);
if (!mediaNodeWithPos) {
return false;
}
const tr = state.tr;
const pos = mediaNodeWithPos.getPos();
if (typeof pos !== 'number' || !isMediaNode(pos, state)) {
return false;
}
tr.step(new SetAttrsStep(pos, attrs)).setMeta('addToHistory', false);
if (dispatch) {
dispatch(tr);
}
return true;
};
export const replaceExternalMedia = (pos, attrs) => (state, dispatch) => {
const tr = state.tr;
const node = tr.doc.nodeAt(pos);
if (!node || node.type.name !== 'media') {
return false;
}
tr.step(new SetAttrsStep(pos, {
type: 'file',
url: null,
...attrs
})).setMeta('addToHistory', false);
if (dispatch) {
dispatch(tr);
}
return true;
};