UNPKG

@atlaskit/editor-plugin-annotation

Version:

Annotation plugin for @atlaskit/editor-core

108 lines 5.98 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; 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 { DecorationSet } from '@atlaskit/editor-prosemirror/view'; import { ACTIONS } from './types'; import { addDraftDecoration, resolveDraftBookmark } from './utils'; export default (function (pluginState, action) { switch (action.type) { case ACTIONS.SET_INLINE_COMMENTS_FETCHED: return _objectSpread(_objectSpread({}, pluginState), {}, { annotationsLoaded: true }); case ACTIONS.UPDATE_INLINE_COMMENT_STATE: return _objectSpread(_objectSpread({}, pluginState), {}, { annotationsLoaded: true, annotations: _objectSpread(_objectSpread({}, pluginState.annotations), action.data) }); case ACTIONS.INLINE_COMMENT_UPDATE_MOUSE_STATE: var mouseData = Object.assign({}, pluginState.mouseData, action.data.mouseData); return _objectSpread(_objectSpread({}, pluginState), {}, { mouseData: mouseData }); case ACTIONS.SET_INLINE_COMMENT_DRAFT_STATE: return getNewDraftState(pluginState, action.data.drafting, action.data.editorState, action.data.targetType, action.data.supportedBlockNodes, action.data.targetNodeId, action.data.isOpeningMediaCommentFromToolbar); case ACTIONS.INLINE_COMMENT_CLEAR_DIRTY_MARK: return _objectSpread(_objectSpread({}, pluginState), {}, { dirtyAnnotations: false, annotations: {} }); case ACTIONS.CLOSE_COMPONENT: return _objectSpread(_objectSpread({}, pluginState), {}, { isInlineCommentViewClosed: true, isDrafting: false, isOpeningMediaCommentFromToolbar: false, selectedAnnotations: [] }); case ACTIONS.ADD_INLINE_COMMENT: var updatedPluginState = getNewDraftState(pluginState, action.data.drafting, action.data.editorState); return _objectSpread(_objectSpread({}, updatedPluginState), {}, { selectedAnnotations: [].concat(_toConsumableArray(updatedPluginState.selectedAnnotations), _toConsumableArray(action.data.selectedAnnotations)), annotations: _objectSpread(_objectSpread({}, pluginState.annotations), action.data.inlineComments), isInlineCommentViewClosed: false, selectAnnotationMethod: undefined }, pluginState.isAnnotationManagerEnabled && { skipSelectionHandling: true }); case ACTIONS.INLINE_COMMENT_SET_VISIBLE: var isVisible = action.data.isVisible; if (isVisible === pluginState.isVisible) { return pluginState; } return _objectSpread(_objectSpread({}, isVisible ? pluginState : getNewDraftState(pluginState, false)), {}, { isVisible: isVisible }); case ACTIONS.SET_SELECTED_ANNOTATION: return _objectSpread(_objectSpread({}, pluginState), {}, { selectedAnnotations: _toConsumableArray(action.data.selectedAnnotations), selectAnnotationMethod: action.data.selectAnnotationMethod, skipSelectionHandling: true, isInlineCommentViewClosed: false, isOpeningMediaCommentFromToolbar: action.data.isOpeningMediaCommentFromToolbar }); case ACTIONS.SET_HOVERED_ANNOTATION: return _objectSpread(_objectSpread({}, pluginState), {}, { hoveredAnnotations: _toConsumableArray(action.data.hoveredAnnotations), skipSelectionHandling: true, isInlineCommentViewClosed: false }); case ACTIONS.FLUSH_PENDING_SELECTIONS: return _objectSpread(_objectSpread({}, pluginState), {}, { selectedAnnotations: action.data.canSetAsSelectedAnnotations ? _toConsumableArray(pluginState.pendingSelectedAnnotations) : pluginState.selectedAnnotations, pendingSelectedAnnotations: [], isInlineCommentViewClosed: false }); case ACTIONS.SET_PENDING_SELECTIONS: return _objectSpread(_objectSpread({}, pluginState), {}, { pendingSelectedAnnotations: _toConsumableArray(action.data.selectedAnnotations), pendingSelectedAnnotationsUpdateCount: pluginState.pendingSelectedAnnotationsUpdateCount + 1, skipSelectionHandling: true, isInlineCommentViewClosed: false }); default: return pluginState; } }); function getNewDraftState(pluginState, drafting, editorState, targetType, supportedBlockNodes, targetNodeId, isOpeningMediaCommentFromToolbar) { var draftDecorationSet = pluginState.draftDecorationSet; if (!draftDecorationSet || !drafting) { draftDecorationSet = DecorationSet.empty; } var newState = _objectSpread(_objectSpread({}, pluginState), {}, { draftDecorationSet: draftDecorationSet, isDrafting: drafting, targetNodeId: targetNodeId }); newState.bookmark = undefined; if (drafting && editorState) { newState.bookmark = editorState.selection.getBookmark(); var _resolveDraftBookmark = resolveDraftBookmark(editorState, newState.bookmark, supportedBlockNodes), from = _resolveDraftBookmark.from, to = _resolveDraftBookmark.to; var draftDecoration = addDraftDecoration(from, to, targetType); newState.draftDecorationSet = draftDecorationSet.add(editorState.doc, [draftDecoration]); } newState.isOpeningMediaCommentFromToolbar = isOpeningMediaCommentFromToolbar; return newState; }