@atlaskit/editor-plugin-indentation
Version:
Indentation plugin for @atlaskit/editor-core
88 lines (81 loc) • 2.28 kB
JavaScript
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INDENT_TYPE } from '@atlaskit/editor-common/analytics';
// Analytics GAS v3 Utils
const indentTypes = {
paragraph: INDENT_TYPE.PARAGRAPH,
heading: INDENT_TYPE.HEADING
};
/**
* Get the current indentation level given prev and new attributes
* @param prevAttrs - Previous attributes from indentation
* @param newAttrs - New attributes from indentation
*/
export function getNewIndentLevel(prevAttrs, newAttrs) {
if (newAttrs === undefined) {
return getPrevIndentLevel(prevAttrs);
} else if (newAttrs === false) {
return 0;
}
return newAttrs.level;
}
/**
* Get the previous indentation level prev attributes
* @param prevAttrs - Previous attributes from indentation
*/
export function getPrevIndentLevel(prevAttrs) {
if (prevAttrs === undefined) {
return 0;
}
return prevAttrs.level;
}
/**
* Create a new dispatch function who add analytics events given a list of attributes changes
*
* @export
* @param {*} getAttrsChanges
* @param {*} state
* @param dispatch
* @returns
*/
export function createAnalyticsDispatch({
getAttrsChanges,
inputMethod,
editorAnalyticsAPI,
state,
dispatch
}) {
return tr => {
const currentTr = tr;
const changes = getAttrsChanges(); // Get all attributes changes
// Add analytics event for each change stored.
changes.forEach(({
node,
prevAttrs,
newAttrs,
options: {
direction
}
}) => {
const indentType = indentTypes[node.type.name];
if (!indentType) {
return; // If no valid indent type continue
}
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
action: ACTION.FORMATTED,
actionSubject: ACTION_SUBJECT.TEXT,
actionSubjectId: ACTION_SUBJECT_ID.FORMAT_INDENT,
eventType: EVENT_TYPE.TRACK,
attributes: {
inputMethod,
previousIndentationLevel: getPrevIndentLevel(prevAttrs),
newIndentLevel: getNewIndentLevel(prevAttrs, newAttrs),
direction,
indentType
}
})(currentTr);
});
// Dispatch analytics if exist
if (dispatch) {
dispatch(tr);
}
};
}