UNPKG

@atlaskit/editor-plugin-annotation

Version:

Annotation plugin for @atlaskit/editor-core

115 lines (114 loc) 6.42 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _view = require("@atlaskit/editor-prosemirror/view"); var _types = require("./types"); var _utils = require("./utils"); 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) { (0, _defineProperty2.default)(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; } var _default = exports.default = function _default(pluginState, action) { switch (action.type) { case _types.ACTIONS.SET_INLINE_COMMENTS_FETCHED: return _objectSpread(_objectSpread({}, pluginState), {}, { annotationsLoaded: true }); case _types.ACTIONS.UPDATE_INLINE_COMMENT_STATE: return _objectSpread(_objectSpread({}, pluginState), {}, { annotationsLoaded: true, annotations: _objectSpread(_objectSpread({}, pluginState.annotations), action.data) }); case _types.ACTIONS.INLINE_COMMENT_UPDATE_MOUSE_STATE: var mouseData = Object.assign({}, pluginState.mouseData, action.data.mouseData); return _objectSpread(_objectSpread({}, pluginState), {}, { mouseData: mouseData }); case _types.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 _types.ACTIONS.INLINE_COMMENT_CLEAR_DIRTY_MARK: return _objectSpread(_objectSpread({}, pluginState), {}, { dirtyAnnotations: false, annotations: {} }); case _types.ACTIONS.CLOSE_COMPONENT: return _objectSpread(_objectSpread({}, pluginState), {}, { isInlineCommentViewClosed: true, isDrafting: false, isOpeningMediaCommentFromToolbar: false, selectedAnnotations: [] }); case _types.ACTIONS.ADD_INLINE_COMMENT: var updatedPluginState = getNewDraftState(pluginState, action.data.drafting, action.data.editorState); return _objectSpread(_objectSpread({}, updatedPluginState), {}, { selectedAnnotations: [].concat((0, _toConsumableArray2.default)(updatedPluginState.selectedAnnotations), (0, _toConsumableArray2.default)(action.data.selectedAnnotations)), annotations: _objectSpread(_objectSpread({}, pluginState.annotations), action.data.inlineComments), isInlineCommentViewClosed: false, selectAnnotationMethod: undefined }, pluginState.isAnnotationManagerEnabled && { skipSelectionHandling: true }); case _types.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 _types.ACTIONS.SET_SELECTED_ANNOTATION: return _objectSpread(_objectSpread({}, pluginState), {}, { selectedAnnotations: (0, _toConsumableArray2.default)(action.data.selectedAnnotations), selectAnnotationMethod: action.data.selectAnnotationMethod, skipSelectionHandling: true, isInlineCommentViewClosed: false, isOpeningMediaCommentFromToolbar: action.data.isOpeningMediaCommentFromToolbar }); case _types.ACTIONS.SET_HOVERED_ANNOTATION: return _objectSpread(_objectSpread({}, pluginState), {}, { hoveredAnnotations: (0, _toConsumableArray2.default)(action.data.hoveredAnnotations), skipSelectionHandling: true, isInlineCommentViewClosed: false }); case _types.ACTIONS.FLUSH_PENDING_SELECTIONS: return _objectSpread(_objectSpread({}, pluginState), {}, { selectedAnnotations: action.data.canSetAsSelectedAnnotations ? (0, _toConsumableArray2.default)(pluginState.pendingSelectedAnnotations) : pluginState.selectedAnnotations, pendingSelectedAnnotations: [], isInlineCommentViewClosed: false }); case _types.ACTIONS.SET_PENDING_SELECTIONS: return _objectSpread(_objectSpread({}, pluginState), {}, { pendingSelectedAnnotations: (0, _toConsumableArray2.default)(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 = _view.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 = (0, _utils.resolveDraftBookmark)(editorState, newState.bookmark, supportedBlockNodes), from = _resolveDraftBookmark.from, to = _resolveDraftBookmark.to; var draftDecoration = (0, _utils.addDraftDecoration)(from, to, targetType); newState.draftDecorationSet = draftDecorationSet.add(editorState.doc, [draftDecoration]); } newState.isOpeningMediaCommentFromToolbar = isOpeningMediaCommentFromToolbar; return newState; }