UNPKG

@atlaskit/editor-plugin-extension

Version:

editor-plugin-extension plugin for @atlaskit/editor-core

167 lines (165 loc) 6.81 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, TARGET_SELECTION_SOURCE } from '@atlaskit/editor-common/analytics'; import { removeConnectedNodes } from '@atlaskit/editor-common/utils'; import { NodeSelection } from '@atlaskit/editor-prosemirror/state'; import { findParentNodeOfType, removeParentNodeOfType, removeSelectedNode } from '@atlaskit/editor-prosemirror/utils'; import { createCommand } from '../pm-plugins/plugin-factory'; import { getSelectedExtension } from '../pm-plugins/utils'; // AFP-2532 TODO: Fix automatic suppressions below // eslint-disable-next-line @atlassian/tangerine/import/entry-points export function updateState(state) { return createCommand({ type: 'UPDATE_STATE', data: state }); } export function setEditingContextToContextPanel(processParametersBefore, processParametersAfter, applyChangeToContextPanel) { return createCommand({ type: 'UPDATE_STATE', data: { showContextPanel: true, processParametersBefore: processParametersBefore, processParametersAfter: processParametersAfter } }, applyChangeToContextPanel); } export var clearEditingContext = function clearEditingContext(applyChangeToContextPanel) { return createCommand({ type: 'UPDATE_STATE', data: { showContextPanel: false, processParametersBefore: undefined, processParametersAfter: undefined } }, applyChangeToContextPanel); }; export var forceAutoSave = function forceAutoSave(applyChangeToContextPanel) { return function (resolve, reject) { return createCommand({ type: 'UPDATE_STATE', data: { autoSaveResolve: resolve, autoSaveReject: reject } }, applyChangeToContextPanel); }; }; export var updateExtensionLayout = function updateExtensionLayout(layout, analyticsApi) { return createCommand({ type: 'UPDATE_STATE', data: { layout: layout } }, function (tr, state) { var selectedExtension = getSelectedExtension(state, true); if (selectedExtension) { var trWithNewNodeMarkup = tr.setNodeMarkup(selectedExtension.pos, undefined, _objectSpread(_objectSpread({}, selectedExtension.node.attrs), {}, { layout: layout })); trWithNewNodeMarkup.setMeta('scrollIntoView', false); if (analyticsApi) { analyticsApi.attachAnalyticsEvent({ action: ACTION.UPDATED, actionSubject: ACTION_SUBJECT.EXTENSION, actionSubjectId: ACTION_SUBJECT_ID.EXTENSION, eventType: EVENT_TYPE.TRACK, attributes: { extensionType: selectedExtension.node.attrs.extensionType, extensionKey: selectedExtension.node.attrs.extensionKey, localId: selectedExtension.node.attrs.localId, layout: layout, selection: tr.selection.toJSON(), targetSelectionSource: TARGET_SELECTION_SOURCE.CURRENT_SELECTION } })(tr); } return trWithNewNodeMarkup; } return tr; }); }; export var removeExtension = function removeExtension(editorAnalyticsAPI, inputMethod) { return createCommand({ type: 'UPDATE_STATE', data: { element: undefined } }, function (tr, state) { if (getSelectedExtension(state)) { return removeSelectedNodeWithAnalytics(state, tr, editorAnalyticsAPI, inputMethod); } else { return checkAndRemoveExtensionNode(state, tr, editorAnalyticsAPI, inputMethod); } }); }; export var removeDescendantNodes = function removeDescendantNodes(sourceNode) { return createCommand({ type: 'UPDATE_STATE', data: { element: undefined } }, function (tr, state) { return sourceNode ? removeConnectedNodes(state, sourceNode) : tr; }); }; export var removeSelectedNodeWithAnalytics = function removeSelectedNodeWithAnalytics(state, tr, analyticsApi, inputMethod) { if (state.selection instanceof NodeSelection) { var node = state.selection.node; if (analyticsApi) { analyticsApi.attachAnalyticsEvent({ action: ACTION.DELETED, actionSubject: ACTION_SUBJECT.EXTENSION, actionSubjectId: ACTION_SUBJECT_ID.EXTENSION, eventType: EVENT_TYPE.TRACK, attributes: { extensionType: node.attrs.extensionType, extensionKey: node.attrs.extensionKey, localId: node.attrs.localId, inputMethod: inputMethod } })(tr); } } return removeSelectedNode(tr); }; export var checkAndRemoveExtensionNode = function checkAndRemoveExtensionNode(state, tr, analyticsApi, inputMethod) { var nodeType = state.schema.nodes.bodiedExtension; var maybeMBENode = findParentNodeOfType(state.schema.nodes.multiBodiedExtension)(state.selection); if (maybeMBENode) { nodeType = state.schema.nodes.multiBodiedExtension; if (analyticsApi) { analyticsApi.attachAnalyticsEvent({ action: ACTION.DELETED, actionSubject: ACTION_SUBJECT.MULTI_BODIED_EXTENSION, eventType: EVENT_TYPE.TRACK, attributes: { extensionType: maybeMBENode.node.attrs.extensionType, extensionKey: maybeMBENode.node.attrs.extensionKey, localId: maybeMBENode.node.attrs.localId, currentFramesCount: maybeMBENode.node.content.childCount, inputMethod: inputMethod } })(tr); } } var bodiedExtensionNode = findParentNodeOfType(state.schema.nodes.bodiedExtension)(state.selection); if (bodiedExtensionNode) { if (analyticsApi) { analyticsApi.attachAnalyticsEvent({ action: ACTION.DELETED, actionSubject: ACTION_SUBJECT.EXTENSION, actionSubjectId: ACTION_SUBJECT_ID.EXTENSION_BODIED, eventType: EVENT_TYPE.TRACK, attributes: { extensionType: bodiedExtensionNode.node.attrs.extensionType, extensionKey: bodiedExtensionNode.node.attrs.extensionKey, localId: bodiedExtensionNode.node.attrs.localId, inputMethod: inputMethod } })(tr); } } return removeParentNodeOfType(nodeType)(tr); };