@atlaskit/editor-plugin-status
Version:
Status plugin for @atlaskit/editor-core
123 lines (122 loc) • 6.08 kB
JavaScript
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 React, { useCallback, useMemo } from 'react';
import { useSharedPluginState } from '@atlaskit/editor-common/hooks';
import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { commitStatusPicker, updateStatus } from '../pm-plugins/actions';
import { getSuggestedStatuses } from './getSuggestedStatuses';
import StatusPicker from './statusPicker';
/** Renders the status picker popup for the currently selected status node. */
export function ContentComponent(_ref) {
var api = _ref.api,
popupsMountPoint = _ref.popupsMountPoint,
popupsBoundariesElement = _ref.popupsBoundariesElement,
popupsScrollableElement = _ref.popupsScrollableElement,
editorView = _ref.editorView,
domAtPos = _ref.domAtPos;
var _useSharedPluginState = useSharedPluginState(api, ['status']),
statusState = _useSharedPluginState.statusState;
var _ref2 = statusState !== null && statusState !== void 0 ? statusState : {},
showStatusPickerAt = _ref2.showStatusPickerAt;
var target = useMemo(function () {
return (
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
showStatusPickerAt ? findDomRefAtPos(showStatusPickerAt, domAtPos) : null
);
}, [showStatusPickerAt, domAtPos]);
var statusNode = useMemo(function () {
return showStatusPickerAt ? editorView.state.doc.nodeAt(showStatusPickerAt) : undefined;
},
// eslint-disable-next-line react-hooks/exhaustive-deps -- preserve original recompute cadence for the ungated path
[showStatusPickerAt, editorView]);
var onSelect = useCallback(function (status) {
updateStatus(status)(editorView.state, editorView.dispatch);
}, [editorView]);
var onTextChanged = useCallback(function (status) {
updateStatus(status)(editorView.state, editorView.dispatch);
}, [editorView]);
var closeStatusPicker = useCallback(function (closingPayload) {
commitStatusPicker(closingPayload)(editorView);
}, [editorView]);
var onEnter = useCallback(function () {
commitStatusPicker()(editorView);
}, [editorView]);
var onSuggestedStatusClick = useCallback(function (status) {
var _editorView$state$doc;
var currentLocalId = typeof showStatusPickerAt === 'number' ? (_editorView$state$doc = editorView.state.doc.nodeAt(showStatusPickerAt)) === null || _editorView$state$doc === void 0 ? void 0 : _editorView$state$doc.attrs.localId : undefined;
var suggestedStatus = _objectSpread(_objectSpread(_objectSpread({
color: status.color
}, currentLocalId ? {
localId: currentLocalId
} : {}), status.style ? {
style: status.style
} : {}), {}, {
text: status.text
});
var didUpdateStatus = updateStatus(suggestedStatus)(editorView.state, editorView.dispatch);
if (didUpdateStatus) {
commitStatusPicker()(editorView);
}
}, [editorView, showStatusPickerAt]);
var suggestedStatuses = useMemo(function () {
if (typeof showStatusPickerAt !== 'number' || !expValEquals('platform_editor_status_popup_suggestions', 'isEnabled', true)) {
return [];
}
// Read the node directly from the current doc so suggestions reflect the latest
// document state (the memoized `statusNode` intentionally preserves the original
// recompute cadence for the ungated path).
var currentNode = editorView.state.doc.nodeAt(showStatusPickerAt);
if (!currentNode || currentNode.type.name !== 'status') {
return [];
}
var _currentNode$attrs = currentNode.attrs,
color = _currentNode$attrs.color,
localId = _currentNode$attrs.localId,
text = _currentNode$attrs.text;
return getSuggestedStatuses({
currentPos: showStatusPickerAt,
currentStatus: {
color: color,
localId: localId,
text: text
},
doc: editorView.state.doc,
shouldUppercaseText: fg('platform-dst-lozenge-tag-badge-visual-uplifts')
});
}, [showStatusPickerAt, editorView.state.doc]);
if (typeof showStatusPickerAt !== 'number') {
return null;
}
if (!statusNode || statusNode.type.name !== 'status') {
return null;
}
var _statusNode$attrs = statusNode.attrs,
text = _statusNode$attrs.text,
color = _statusNode$attrs.color,
localId = _statusNode$attrs.localId,
style = _statusNode$attrs.style;
var displayText = style !== 'mixedCase' && fg('platform-dst-lozenge-tag-badge-visual-uplifts') ? text.toUpperCase() : text;
return /*#__PURE__*/React.createElement(StatusPicker, {
isNew: statusState === null || statusState === void 0 ? void 0 : statusState.isNew,
focusStatusInput: statusState === null || statusState === void 0 ? void 0 : statusState.focusStatusInput,
target: target,
defaultText: displayText,
defaultColor: color,
defaultLocalId: localId,
mountTo: popupsMountPoint,
boundariesElement: popupsBoundariesElement,
scrollableElement: popupsScrollableElement,
onSelect: onSelect,
onTextChanged: onTextChanged,
closeStatusPicker: closeStatusPicker,
onEnter: onEnter,
onSuggestedStatusClick: onSuggestedStatusClick,
editorView: editorView,
api: api,
suggestedStatuses: suggestedStatuses
});
}