@atlaskit/editor-plugin-find-replace
Version:
find replace plugin for @atlaskit/editor-core
84 lines (82 loc) • 3.15 kB
JavaScript
import { ACTION, ACTION_SUBJECT, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics/types/enums';
import { withAnalytics } from '@atlaskit/editor-common/withAnalytics';
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
import { activate, cancelSearch, find, findNext, findPrevious, replace, replaceAll } from './commands';
/**
* Hosts that do not install `editorViewMode` are always editing, so the attribute
* falls back to `edit` when the api is absent.
*/
export const activateWithAnalytics = (editorAnalyticsAPI, editorViewModeAPI) => ({
triggerMethod
}) => withAnalytics(editorAnalyticsAPI, state => {
var _editorViewModeAPI$sh, _editorViewModeAPI$sh2;
return {
eventType: EVENT_TYPE.UI,
action: ACTION.ACTIVATED,
actionSubject: ACTION_SUBJECT.FIND_REPLACE_DIALOG,
attributes: {
inputMethod: state.selection instanceof TextSelection && !state.selection.empty ? INPUT_METHOD.PREFILL : INPUT_METHOD.KEYBOARD,
triggerMethod,
editorViewMode: (_editorViewModeAPI$sh = editorViewModeAPI === null || editorViewModeAPI === void 0 ? void 0 : (_editorViewModeAPI$sh2 = editorViewModeAPI.sharedState.currentState()) === null || _editorViewModeAPI$sh2 === void 0 ? void 0 : _editorViewModeAPI$sh2.mode) !== null && _editorViewModeAPI$sh !== void 0 ? _editorViewModeAPI$sh : 'edit'
}
};
})(activate());
export const findWithAnalytics = editorAnalyticsAPI => ({
editorView,
containerElement,
keyword
}) => withAnalytics(editorAnalyticsAPI, {
eventType: EVENT_TYPE.TRACK,
action: ACTION.FIND_PERFORMED,
actionSubject: ACTION_SUBJECT.TEXT
})(find(editorView, containerElement, keyword));
export const findNextWithAnalytics = (editorAnalyticsAPI, editorView) => ({
triggerMethod
}) => {
return withAnalytics(editorAnalyticsAPI, {
eventType: EVENT_TYPE.TRACK,
action: ACTION.FIND_NEXT_PERFORMED,
actionSubject: ACTION_SUBJECT.TEXT,
attributes: {
triggerMethod
}
})(findNext(editorView));
};
export const findPrevWithAnalytics = (editorAnalyticsAPI, editorView) => ({
triggerMethod
}) => withAnalytics(editorAnalyticsAPI, {
eventType: EVENT_TYPE.TRACK,
action: ACTION.FIND_PREV_PERFORMED,
actionSubject: ACTION_SUBJECT.TEXT,
attributes: {
triggerMethod
}
})(findPrevious(editorView));
export const replaceWithAnalytics = editorAnalyticsAPI => ({
triggerMethod,
replaceText
}) => withAnalytics(editorAnalyticsAPI, {
eventType: EVENT_TYPE.TRACK,
action: ACTION.REPLACED_ONE,
actionSubject: ACTION_SUBJECT.TEXT,
attributes: {
triggerMethod
}
})(replace(replaceText));
export const replaceAllWithAnalytics = editorAnalyticsAPI => ({
replaceText
}) => withAnalytics(editorAnalyticsAPI, {
eventType: EVENT_TYPE.TRACK,
action: ACTION.REPLACED_ALL,
actionSubject: ACTION_SUBJECT.TEXT
})(replaceAll(replaceText));
export const cancelSearchWithAnalytics = editorAnalyticsAPI => ({
triggerMethod
}) => withAnalytics(editorAnalyticsAPI, {
eventType: EVENT_TYPE.UI,
action: ACTION.DEACTIVATED,
actionSubject: ACTION_SUBJECT.FIND_REPLACE_DIALOG,
attributes: {
triggerMethod
}
})(cancelSearch());