debian-mirror-gitlab/app/assets/javascripts/content_editor/services/track_input_rules_and_shortcuts.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import { mapValues } from 'lodash';
2021-12-11 22:18:48 +05:30
import { InputRule } from '@tiptap/core';
2021-06-08 01:23:25 +05:30
import { ENTER_KEY, BACKSPACE_KEY } from '~/lib/utils/keys';
import Tracking from '~/tracking';
import {
CONTENT_EDITOR_TRACKING_LABEL,
KEYBOARD_SHORTCUT_TRACKING_ACTION,
INPUT_RULE_TRACKING_ACTION,
} from '../constants';
const trackKeyboardShortcut = (contentType, commandFn, shortcut) => () => {
Tracking.event(undefined, KEYBOARD_SHORTCUT_TRACKING_ACTION, {
label: CONTENT_EDITOR_TRACKING_LABEL,
property: `${contentType}.${shortcut}`,
});
return commandFn();
};
const trackInputRule = (contentType, inputRule) => {
2021-12-11 22:18:48 +05:30
return new InputRule({
find: inputRule.find,
handler: (...args) => {
const result = inputRule.handler(...args);
2021-06-08 01:23:25 +05:30
2021-12-11 22:18:48 +05:30
if (result !== null) {
Tracking.event(undefined, INPUT_RULE_TRACKING_ACTION, {
label: CONTENT_EDITOR_TRACKING_LABEL,
property: contentType,
});
}
2021-06-08 01:23:25 +05:30
2021-12-11 22:18:48 +05:30
return result;
},
2021-06-08 01:23:25 +05:30
});
};
const trackInputRulesAndShortcuts = (tiptapExtension) => {
2022-03-02 08:16:31 +05:30
return tiptapExtension
.extend({
addKeyboardShortcuts() {
const shortcuts = this.parent?.() || {};
const { name } = this;
/**
* We dont want to track keyboard shortcuts
* that are not deliberately executed to create
* new types of content
*/
const dotNotTrackKeys = [ENTER_KEY, BACKSPACE_KEY];
const decorated = mapValues(shortcuts, (commandFn, shortcut) =>
dotNotTrackKeys.includes(shortcut)
? commandFn
: trackKeyboardShortcut(name, commandFn, shortcut),
);
2021-06-08 01:23:25 +05:30
2022-03-02 08:16:31 +05:30
return decorated;
},
addInputRules() {
const inputRules = this.parent?.() || [];
const { name } = this;
2021-06-08 01:23:25 +05:30
2022-03-02 08:16:31 +05:30
return inputRules.map((inputRule) => trackInputRule(name, inputRule));
},
})
.configure(tiptapExtension.options);
2021-06-08 01:23:25 +05:30
};
export default trackInputRulesAndShortcuts;