@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
52 lines (51 loc) • 2.03 kB
JavaScript
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD, JOIN_SCENARIOS_WHEN_TYPING_TO_INSERT_LIST } from '@atlaskit/editor-common/analytics';
import { inputRuleWithAnalytics as ruleWithAnalytics } from '@atlaskit/editor-common/utils';
import { createWrappingJoinRule } from './wrapping-join-rule';
const getOrder = matchResult => Number(matchResult[1]);
export function createRuleForListType({
listType,
expression,
editorAnalyticsApi
}) {
let joinScenario = JOIN_SCENARIOS_WHEN_TYPING_TO_INSERT_LIST.NO_JOIN;
const isBulletList = listType.name === 'bulletList';
const actionSubjectId = isBulletList ? ACTION_SUBJECT_ID.FORMAT_LIST_BULLET : ACTION_SUBJECT_ID.FORMAT_LIST_NUMBER;
const getAnalyticsPayload = (state, matchResult) => {
const analyticsPayload = {
action: ACTION.INSERTED,
actionSubject: ACTION_SUBJECT.LIST,
actionSubjectId,
eventType: EVENT_TYPE.TRACK,
attributes: {
inputMethod: INPUT_METHOD.FORMATTING
}
};
if (listType === state.schema.nodes.orderedList && analyticsPayload.attributes) {
analyticsPayload.attributes.listStartNumber = getOrder(matchResult);
analyticsPayload.attributes.joinScenario = joinScenario;
// we reset the tracked joinScenario after storing it in the event payload
joinScenario = JOIN_SCENARIOS_WHEN_TYPING_TO_INSERT_LIST.NO_JOIN;
}
return analyticsPayload;
};
const joinToNeighbourIfSameListType = (_, node, scenario) => {
const shouldJoin = node.type === listType;
if (shouldJoin) {
joinScenario = scenario;
}
return shouldJoin;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getAttrs = matchResult => {
return {
order: getOrder(matchResult)
};
};
const inputRule = createWrappingJoinRule({
match: expression,
nodeType: listType,
getAttrs,
joinPredicate: joinToNeighbourIfSameListType
});
return ruleWithAnalytics(getAnalyticsPayload, editorAnalyticsApi)(inputRule);
}