@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
180 lines • 5.27 kB
JavaScript
import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
import { getQuickInsertMenuItemParents } from '@atlaskit/editor-common/quick-insert/get-menu-item-parents';
import { BLOCK_TEMPLATES_SECTION, DATA_AND_CHARTS_SECTION, EMBED_SECTION, MEDIA_SECTION, OTHER_SECTION, ROVO_SECTION, STRUCTURE_SECTION, TEXT_FORMATTING_SECTION } from '@atlaskit/editor-common/quick-insert/keys';
export const QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS = 10_000;
const sectionCategoryKeys = {
[BLOCK_TEMPLATES_SECTION.key]: 'blockTemplates',
[DATA_AND_CHARTS_SECTION.key]: 'dataAndCharts',
[EMBED_SECTION.key]: 'embed',
[MEDIA_SECTION.key]: 'media',
[OTHER_SECTION.key]: 'other',
[ROVO_SECTION.key]: 'rovo',
[STRUCTURE_SECTION.key]: 'structure',
[TEXT_FORMATTING_SECTION.key]: 'textFormatting'
};
const legacyCategoryKeys = new Set(['admin', 'ai', 'block-templates', 'communication', 'confluence-content', 'data-and-charts', 'development', 'embed', 'external-content', 'formatting', 'media', 'navigation', 'other', 'reporting', 'skills', 'structure', 'text-formatting', 'visuals']);
const isLegacyCategoryKey = category => legacyCategoryKeys.has(category);
export const calculateQuickInsertItemsAttributes = items => {
const attributes = {
category: {
blockTemplates: {
appCount: 0
},
dataAndCharts: {
appCount: 0
},
embed: {
appCount: 0
},
media: {
appCount: 0
},
other: {
appCount: 0
},
rovo: {
appCount: 0
},
structure: {
appCount: 0
},
textFormatting: {
appCount: 0
}
},
legacyCategory: {
admin: {
appCount: 0
},
ai: {
appCount: 0
},
'block-templates': {
appCount: 0
},
communication: {
appCount: 0
},
'confluence-content': {
appCount: 0
},
'data-and-charts': {
appCount: 0
},
development: {
appCount: 0
},
embed: {
appCount: 0
},
'external-content': {
appCount: 0
},
formatting: {
appCount: 0
},
media: {
appCount: 0
},
navigation: {
appCount: 0
},
other: {
appCount: 0
},
reporting: {
appCount: 0
},
skills: {
appCount: 0
},
structure: {
appCount: 0
},
'text-formatting': {
appCount: 0
},
uncategorized: {
appCount: 0
},
visuals: {
appCount: 0
}
}
};
for (const item of items) {
var _item$categories$map$, _item$categories;
const normalizedCategories = new Set((_item$categories$map$ = (_item$categories = item.categories) === null || _item$categories === void 0 ? void 0 : _item$categories.map(category => category.trim().toLowerCase()).filter(Boolean)) !== null && _item$categories$map$ !== void 0 ? _item$categories$map$ : []);
let hasUnknownCategory = normalizedCategories.size === 0;
for (const category of normalizedCategories) {
if (isLegacyCategoryKey(category)) {
attributes.legacyCategory[category].appCount += 1;
} else {
hasUnknownCategory = true;
}
}
if (hasUnknownCategory) {
attributes.legacyCategory.uncategorized.appCount += 1;
}
for (const {
key
} of getQuickInsertMenuItemParents({
categories: item.categories,
rank: 0
})) {
const categoryKey = sectionCategoryKeys[key];
if (categoryKey) {
attributes.category[categoryKey].appCount += 1;
}
}
}
return attributes;
};
export const createQuickInsertItemsAnalyticsScheduler = dispatchAnalyticsEvent => {
let timeoutId;
let animationFrameId;
let idleCallbackId;
let destroyed = false;
let scheduled = false;
const emit = items => {
if (destroyed) {
return;
}
dispatchAnalyticsEvent({
action: ACTION.QUICK_INSERT_INFORMATION,
actionSubject: ACTION_SUBJECT.QUICK_INSERT,
attributes: calculateQuickInsertItemsAttributes(items),
eventType: EVENT_TYPE.OPERATIONAL
});
};
return {
destroy: () => {
destroyed = true;
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
if (animationFrameId !== undefined && typeof window !== 'undefined') {
window.cancelAnimationFrame(animationFrameId);
}
if (idleCallbackId !== undefined && typeof window !== 'undefined' && typeof window.cancelIdleCallback === 'function') {
window.cancelIdleCallback(idleCallbackId);
}
},
schedule: items => {
if (scheduled || destroyed) {
return;
}
scheduled = true;
timeoutId = setTimeout(() => {
if (destroyed || typeof window === 'undefined') {
return;
}
if (typeof window.requestIdleCallback === 'function') {
idleCallbackId = window.requestIdleCallback(() => emit(items));
} else if (typeof window.requestAnimationFrame === 'function') {
animationFrameId = window.requestAnimationFrame(() => emit(items));
}
}, QUICK_INSERT_ITEMS_ANALYTICS_DELAY_MS);
}
};
};