UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

36 lines 1.5 kB
import { ACTION, ACTION_SUBJECT, CONTENT_COMPONENT, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { logException } from '@atlaskit/editor-common/monitoring'; import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils'; export const getDomRefFromSelection = (view, dispatchAnalyticsEvent) => { try { const domRef = findDomRefAtPos(view.state.selection.from, view.domAtPos.bind(view)); if (domRef instanceof HTMLElement) { return domRef; } throw new Error('Invalid DOM reference'); } catch (error) { if (dispatchAnalyticsEvent) { const payload = { action: ACTION.ERRORED, actionSubject: ACTION_SUBJECT.CONTENT_COMPONENT, eventType: EVENT_TYPE.OPERATIONAL, attributes: { component: CONTENT_COMPONENT.SELECTION_TOOLBAR, selection: view.state.selection.toJSON(), position: view.state.selection.from, docSize: view.state.doc.nodeSize, error: error instanceof Error ? error.toString() : String(error), // @ts-expect-error - Object literal may only specify known properties, 'errorStack' does not exist in type // This error was introduced after upgrading to TypeScript 5 errorStack: error instanceof Error ? error.stack : undefined } }; dispatchAnalyticsEvent(payload); } if (error instanceof Error) { logException(error, { location: 'editor-plugin-toolbar/selectionToolbar' }); } } };