UNPKG

@atlaskit/editor-plugin-text-formatting

Version:

Text-formatting plugin for @atlaskit/editor-core

256 lines 5.66 kB
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { toggleMark } from '@atlaskit/editor-common/mark'; import { nextToggleMark } from './utils/marks'; export const toggleEm = ({ tr }) => { const { em } = tr.doc.type.schema.marks; if (!em) { // No transaction to apply return null; } return toggleMark(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 toggleMark(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 toggleMark(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 toggleMark(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 toggleMark(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 toggleMark(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; };