@atlaskit/editor-plugin-find-replace
Version:
find replace plugin for @atlaskit/editor-core
184 lines (182 loc) • 9.46 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _analytics = require("@atlaskit/editor-common/analytics");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _commands = require("../pm-plugins/commands");
var _commandsWithAnalytics = require("../pm-plugins/commands-with-analytics");
var _FindReplaceDropdown = _interopRequireDefault(require("./FindReplaceDropdown"));
var _FindReplaceToolbarButton = _interopRequireDefault(require("./FindReplaceToolbarButton"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
// light implementation of useSharedPluginState(). This is due to findreplace
// being the only plugin that previously used WithPluginState with
// debounce=false. That was implemented because of text sync issues
// between editor & findreplace dialog.
// To address under ENGHEALTH-5853
var useSharedPluginStateNoDebounce = function useSharedPluginStateNoDebounce(api) {
var _api$findReplace;
var _useState = (0, _react.useState)(api === null || api === void 0 || (_api$findReplace = api.findReplace) === null || _api$findReplace === void 0 ? void 0 : _api$findReplace.sharedState.currentState()),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
(0, _react.useLayoutEffect)(function () {
var _api$findReplace2;
var unsub = api === null || api === void 0 || (_api$findReplace2 = api.findReplace) === null || _api$findReplace2 === void 0 ? void 0 : _api$findReplace2.sharedState.onChange(function (_ref) {
var nextSharedState = _ref.nextSharedState;
setState(nextSharedState);
});
return function () {
unsub === null || unsub === void 0 || unsub();
};
}, [api]);
return {
findReplaceState: state
};
};
var FindReplaceToolbarButtonWithState = function FindReplaceToolbarButtonWithState(_ref2) {
var _api$analytics, _matches$index;
var popupsBoundariesElement = _ref2.popupsBoundariesElement,
popupsMountPoint = _ref2.popupsMountPoint,
popupsScrollableElement = _ref2.popupsScrollableElement,
isToolbarReducedSpacing = _ref2.isToolbarReducedSpacing,
editorView = _ref2.editorView,
containerElement = _ref2.containerElement,
dispatchAnalyticsEvent = _ref2.dispatchAnalyticsEvent,
takeFullWidth = _ref2.takeFullWidth,
api = _ref2.api,
isButtonHidden = _ref2.isButtonHidden,
doesNotHaveButton = _ref2.doesNotHaveButton;
var editorAnalyticsAPI = api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions;
var _useSharedPluginState = useSharedPluginStateNoDebounce(api),
findReplaceState = _useSharedPluginState.findReplaceState;
var shouldMatchCase = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.shouldMatchCase;
var isActive = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.isActive;
var findText = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.findText;
var replaceText = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.replaceText;
var index = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.index;
var matches = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.matches;
var shouldFocus = findReplaceState === null || findReplaceState === void 0 ? void 0 : findReplaceState.shouldFocus;
if (!editorView) {
return null;
}
// we need the editor to be in focus for scrollIntoView() to work
// so we focus it while we run the command, then put focus back into
// whatever element was previously focused in find replace component
var runWithEditorFocused = function runWithEditorFocused(fn) {
var activeElement = document.activeElement;
editorView.focus();
fn();
activeElement === null || activeElement === void 0 || activeElement.focus();
};
var dispatchCommand = function dispatchCommand(cmd) {
var state = editorView.state,
dispatch = editorView.dispatch;
cmd(state, dispatch, editorView);
};
var handleActivate = function handleActivate() {
runWithEditorFocused(function () {
return dispatchCommand((0, _commandsWithAnalytics.activateWithAnalytics)(editorAnalyticsAPI)({
triggerMethod: _analytics.TRIGGER_METHOD.TOOLBAR
}));
});
};
var handleFind = function handleFind(keyword) {
runWithEditorFocused(function () {
return dispatchCommand((0, _commandsWithAnalytics.findWithAnalytics)(editorAnalyticsAPI)({
editorView: editorView,
containerElement: containerElement,
keyword: keyword
}));
});
};
var handleFindNext = function handleFindNext(_ref3) {
var triggerMethod = _ref3.triggerMethod;
runWithEditorFocused(function () {
return dispatchCommand((0, _commandsWithAnalytics.findNextWithAnalytics)(editorAnalyticsAPI, editorView)({
triggerMethod: triggerMethod
}));
});
};
var handleFindPrev = function handleFindPrev(_ref4) {
var triggerMethod = _ref4.triggerMethod;
runWithEditorFocused(function () {
return dispatchCommand((0, _commandsWithAnalytics.findPrevWithAnalytics)(editorAnalyticsAPI, editorView)({
triggerMethod: triggerMethod
}));
});
};
var handleReplace = function handleReplace(_ref5) {
var triggerMethod = _ref5.triggerMethod,
replaceText = _ref5.replaceText;
runWithEditorFocused(function () {
return dispatchCommand((0, _commandsWithAnalytics.replaceWithAnalytics)(editorAnalyticsAPI)({
triggerMethod: triggerMethod,
replaceText: replaceText
}));
});
};
var handleReplaceAll = function handleReplaceAll(_ref6) {
var replaceText = _ref6.replaceText;
runWithEditorFocused(function () {
return dispatchCommand((0, _commandsWithAnalytics.replaceAllWithAnalytics)(editorAnalyticsAPI)({
replaceText: replaceText
}));
});
};
var handleFindBlur = function handleFindBlur() {
dispatchCommand((0, _commands.blur)());
};
var handleCancel = function handleCancel(_ref7) {
var triggerMethod = _ref7.triggerMethod;
dispatchCommand((0, _commandsWithAnalytics.cancelSearchWithAnalytics)(editorAnalyticsAPI)({
triggerMethod: triggerMethod
}));
editorView.focus();
};
var handleToggleMatchCase = function handleToggleMatchCase() {
dispatchCommand((0, _commands.toggleMatchCase)());
};
if (shouldMatchCase === undefined || isActive === undefined || findText === undefined || replaceText === undefined || index === undefined || matches === undefined || shouldFocus === undefined) {
return null;
}
var DropDownComponent = doesNotHaveButton ? _FindReplaceDropdown.default : _FindReplaceToolbarButton.default;
return /*#__PURE__*/_react.default.createElement(DropDownComponent, {
shouldMatchCase: shouldMatchCase,
onToggleMatchCase: handleToggleMatchCase,
isActive: isActive,
findText: findText,
index: index,
numMatches: matches.length,
isReplaceable: (0, _expValEquals.expValEquals)('platform_editor_find_and_replace_improvements', 'isEnabled', true) ? (_matches$index = matches[index]) === null || _matches$index === void 0 ? void 0 : _matches$index.canReplace : undefined,
numReplaceable: (0, _expValEquals.expValEquals)('platform_editor_find_and_replace_improvements', 'isEnabled', true) ?
// eslint-disable-next-line @atlassian/perf-linting/no-expensive-computations-in-render -- Ignored via go/ees017 (to be fixed)
matches.filter(function (match) {
return match.canReplace === true;
}).length : undefined,
replaceText: replaceText,
shouldFocus: shouldFocus,
popupsBoundariesElement: popupsBoundariesElement,
popupsMountPoint: popupsMountPoint,
popupsScrollableElement: popupsScrollableElement,
isReducedSpacing: !!isToolbarReducedSpacing,
dispatchAnalyticsEvent: dispatchAnalyticsEvent,
onFindBlur: handleFindBlur,
onCancel: handleCancel,
onActivate: handleActivate,
onFind: handleFind,
onFindNext: handleFindNext,
onFindPrev: handleFindPrev,
onReplace: handleReplace,
onReplaceAll: handleReplaceAll,
takeFullWidth: !!takeFullWidth,
isButtonHidden: isButtonHidden
});
};
var _default_1 = /*#__PURE__*/_react.default.memo(FindReplaceToolbarButtonWithState);
var _default = exports.default = _default_1;