UNPKG

@atlaskit/editor-plugin-text-formatting

Version:

Text-formatting plugin for @atlaskit/editor-core

269 lines (268 loc) • 6.21 kB
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics/types/enums'; import { toggleMark } from '@atlaskit/editor-common/mark/commands'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { nextToggleMark } from './utils/marks'; /** * Toggles a text style mark. Behind `platform_editor_style_around_inline_code` it uses the * package's own toggle, which can remove a style from a selection that also contains text * excluding it (such as inline code). */ const toggleStyleMark = (markType, attrs) => { if (fg('platform_editor_style_around_inline_code')) { return nextToggleMark(markType, undefined, attrs); } return toggleMark(markType, attrs); }; export const toggleEm = ({ tr }) => { const { em } = tr.doc.type.schema.marks; if (!em) { // No transaction to apply return null; } return toggleStyleMark(em)({ tr }); }; export const toggleEmWithAnalytics = editorAnalyticsApi => inputMethod => ({ tr }) => { const newTr = toggleEm({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_ITALIC, attributes: { inputMethod } })(newTr); return newTr; }; export const toggleStrike = ({ tr }) => { const { strike } = tr.doc.type.schema.marks; if (!strike) { // No transaction to apply return null; } return toggleStyleMark(strike)({ tr }); }; export const toggleStrikeWithAnalytics = editorAnalyticsApi => inputMethod => ({ tr }) => { const newTr = toggleStrike({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_STRIKE, attributes: { inputMethod } })(newTr); return newTr; }; export const toggleStrong = ({ tr }) => { const { strong } = tr.doc.type.schema.marks; if (!strong) { // No transaction to apply return null; } return toggleStyleMark(strong)({ tr }); }; export const toggleStrongWithAnalytics = editorAnalyticsApi => inputMethod => ({ tr }) => { const newTr = toggleStrong({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_STRONG, attributes: { inputMethod } })(newTr); return newTr; }; export const toggleUnderline = ({ tr }) => { const { underline } = tr.doc.type.schema.marks; if (!underline) { // No transaction to apply return null; } return toggleStyleMark(underline)({ tr }); }; export const toggleUnderlineWithAnalytics = editorAnalyticsApi => inputMethod => ({ tr }) => { const newTr = toggleUnderline({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_UNDERLINE, attributes: { inputMethod } })(newTr); return newTr; }; export const toggleSuperscript = ({ tr }) => { const { subsup } = tr.doc.type.schema.marks; if (!subsup) { // No transaction to apply return null; } return toggleStyleMark(subsup, { type: 'sup' })({ tr }); }; export const toggleSuperscriptWithAnalytics = editorAnalyticsApi => inputMethod => ({ tr }) => { const newTr = toggleSuperscript({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_SUPER, attributes: { inputMethod } })(newTr); return newTr; }; export const toggleSubscript = ({ tr }) => { const { subsup } = tr.doc.type.schema.marks; if (!subsup) { // No transaction to apply return null; } return toggleStyleMark(subsup, { type: 'sub' })({ tr }); }; export const toggleSubscriptWithAnalytics = editorAnalyticsApi => inputMethod => ({ tr }) => { const newTr = toggleSubscript({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_SUB, attributes: { inputMethod } })(newTr); return newTr; }; export const toggleCode = ({ api }) => ({ tr }) => { const { code } = tr.doc.type.schema.marks; if (!code) { // No transaction to apply return null; } return nextToggleMark(code, api)({ tr }); }; export const toggleCodeWithAnalytics = (editorAnalyticsApi, api) => inputMethod => ({ tr }) => { const newTr = toggleCode({ api })({ tr }); if (!newTr) { // No transaction to apply return null; } editorAnalyticsApi === null || editorAnalyticsApi === void 0 ? void 0 : editorAnalyticsApi.attachAnalyticsEvent({ action: ACTION.FORMATTED, actionSubject: ACTION_SUBJECT.TEXT, eventType: EVENT_TYPE.TRACK, actionSubjectId: ACTION_SUBJECT_ID.FORMAT_CODE, attributes: { inputMethod } })(newTr); return newTr; };