@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
58 lines (54 loc) • 1.92 kB
JavaScript
import { insertSelectedItem } from '@atlaskit/editor-common/insert';
import { pluginKey } from '../plugin-key';
export const openElementBrowser = ({
category
} = {}) => ({
tr
}) => tr.setMeta(pluginKey, {
elementBrowserInitialCategory: category,
isElementBrowserOpen: true
});
export const closeElementBrowser = () => (state, dispatch) => {
if (dispatch) {
dispatch(state.tr.setMeta(pluginKey, {
elementBrowserInitialCategory: undefined,
isElementBrowserOpen: false
}));
}
return true;
};
/**
* @private
* @deprecated {@link https://hello.atlassian.net/browse/ENGHEALTH-62138 Internal documentation for deprecation (no external access)} Tracked by EDITOR-8422. Use `openElementBrowser` instead.
*/
export const openElementBrowserModal = ({
tr
}) => tr.setMeta(pluginKey, {
isElementBrowserModalOpen: true
});
/**
* @private
* @deprecated {@link https://hello.atlassian.net/browse/ENGHEALTH-62138 Internal documentation for deprecation (no external access)} Tracked by EDITOR-8422. Use `closeElementBrowser` instead.
*/
export const closeElementBrowserModal = () => (state, dispatch) => {
if (dispatch) {
dispatch(state.tr.setMeta(pluginKey, {
isElementBrowserModalOpen: false
}));
}
return true;
};
export const createInsertItem = onInsert => insertItem(onInsert);
// this method was adapted from the typeahead plugin so we respect the API for quick insert items
const insertItem = onInsert => (item, source) => (state, dispatch) => {
const insert = (maybeNode, opts = {}) => {
return insertSelectedItem(maybeNode, opts)(state, state.tr, state.selection.head);
};
const tr = item.action(insert, state, source);
/** @note There is no transaction when called without a search currently (different insert) */
if (tr && dispatch) {
dispatch(tr);
}
onInsert === null || onInsert === void 0 ? void 0 : onInsert(item);
return true;
};