@atlaskit/editor-plugin-rule
Version:
Rule plugin for @atlaskit/editor-core
133 lines • 4.59 kB
JavaScript
import { ACTION, ACTION_SUBJECT, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics/types/enums';
import { findSelectedNodeOfType, removeSelectedNode } from '@atlaskit/editor-prosemirror/utils';
import { DEFAULT_DIVIDER_STYLE, DEFAULT_DIVIDER_WEIGHT } from '../ui/constants';
import { createHorizontalRule } from './input-rule';
export const insertHorizontalRule = editorAnalyticsAPI => inputMethod => (state, dispatch) => {
const tr = createHorizontalRule(state, state.selection.from, state.selection.to, inputMethod, editorAnalyticsAPI);
if (tr) {
if (dispatch) {
dispatch(tr);
}
return true;
}
return false;
};
export const removeHorizontalRule = editorAnalyticsAPI => (state, dispatch) => {
const {
rule
} = state.schema.nodes;
if (!findSelectedNodeOfType(rule)(state.selection)) {
return false;
}
if (dispatch) {
const deleteTr = removeSelectedNode(state.tr);
const payload = {
action: ACTION.DELETED,
actionSubject: ACTION_SUBJECT.DIVIDER,
attributes: {
inputMethod: INPUT_METHOD.FLOATING_TB
},
eventType: EVENT_TYPE.TRACK
};
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent(payload)(deleteTr);
dispatch(deleteTr);
}
return true;
};
export const updateHorizontalRuleWeight = (weight, editorAnalyticsAPI) => (state, dispatch) => {
var _selectedRule$node$at;
const {
rule
} = state.schema.nodes;
const selectedRule = findSelectedNodeOfType(rule)(state.selection);
if (!selectedRule) {
return false;
}
const previousWeight = (_selectedRule$node$at = selectedRule.node.attrs.weight) !== null && _selectedRule$node$at !== void 0 ? _selectedRule$node$at : DEFAULT_DIVIDER_WEIGHT;
if (previousWeight === weight) {
return true;
}
if (dispatch) {
const updateTr = state.tr.setNodeMarkup(selectedRule.pos, undefined, {
...selectedRule.node.attrs,
weight
});
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
action: ACTION.UPDATED,
actionSubject: ACTION_SUBJECT.DIVIDER,
attributes: {
inputMethod: INPUT_METHOD.FLOATING_TB,
previousWeight,
weight
},
eventType: EVENT_TYPE.TRACK
})(updateTr);
dispatch(updateTr);
}
return true;
};
export const updateHorizontalRuleStyle = (style, editorAnalyticsAPI) => (state, dispatch) => {
var _selectedRule$node$at2;
const {
rule
} = state.schema.nodes;
const selectedRule = findSelectedNodeOfType(rule)(state.selection);
if (!selectedRule) {
return false;
}
const previousStyle = (_selectedRule$node$at2 = selectedRule.node.attrs.style) !== null && _selectedRule$node$at2 !== void 0 ? _selectedRule$node$at2 : DEFAULT_DIVIDER_STYLE;
if (previousStyle === style) {
return true;
}
if (dispatch) {
const updateTr = state.tr.setNodeMarkup(selectedRule.pos, undefined, {
...selectedRule.node.attrs,
style
});
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
action: ACTION.UPDATED,
actionSubject: ACTION_SUBJECT.DIVIDER,
attributes: {
inputMethod: INPUT_METHOD.FLOATING_TB,
previousStyle,
style
},
eventType: EVENT_TYPE.TRACK
})(updateTr);
dispatch(updateTr);
}
return true;
};
export const updateHorizontalRuleColor = (color, editorAnalyticsAPI) => (state, dispatch) => {
const {
rule
} = state.schema.nodes;
const selectedRule = findSelectedNodeOfType(rule)(state.selection);
if (!selectedRule) {
return false;
}
const defaultColor = "var(--ds-border, #0B120E24)";
const previousColor = typeof selectedRule.node.attrs.color === 'string' ? selectedRule.node.attrs.color : null;
const updatedColor = color === defaultColor ? null : color;
if (previousColor === updatedColor) {
return true;
}
if (dispatch) {
const updateTr = state.tr.setNodeMarkup(selectedRule.pos, undefined, {
...selectedRule.node.attrs,
color: updatedColor
});
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({
action: ACTION.UPDATED,
actionSubject: ACTION_SUBJECT.DIVIDER,
attributes: {
inputMethod: INPUT_METHOD.FLOATING_TB,
previousColor,
color: updatedColor
},
eventType: EVENT_TYPE.TRACK
})(updateTr);
dispatch(updateTr);
}
return true;
};