UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

69 lines (66 loc) 2.36 kB
import { JSONTransformer } from '@atlaskit/editor-json-transformer'; import { Node as PMNode } from '@atlaskit/editor-prosemirror/model'; import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '../../analytics'; const transformer = new JSONTransformer(); export function toJSON(node) { return transformer.encode(node); } /** * This throttles the callback with requestIdleCallback. */ function createThrottleSchedule(callback) { let frameId; let lastArgs; const wrapperFn = (...args) => { var _globalThis$requestId; lastArgs = args; if (frameId) { return; } // If `requestIdleCallback` doesn't exist - fallback to `requestAnimationFrame` const delayFunction = (_globalThis$requestId = globalThis.requestIdleCallback) !== null && _globalThis$requestId !== void 0 ? _globalThis$requestId : globalThis.requestAnimationFrame; frameId = delayFunction(() => { frameId = undefined; if (lastArgs) { callback(...lastArgs); } }, { timeout: 100 }); }; return wrapperFn; } export const scheduleDocumentRequest = createThrottleSchedule(returnDocumentRequest); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/max-params function returnDocumentRequest(editorView, callback, transformer, fireAnalyticsEvent) { var _editorView$state; const { doc, schema } = (_editorView$state = editorView === null || editorView === void 0 ? void 0 : editorView.state) !== null && _editorView$state !== void 0 ? _editorView$state : {}; if (!doc || !schema) { return undefined; } try { const json = toJSON(doc); if (typeof transformer === 'undefined') { callback(json); } else { const nodeSanitized = PMNode.fromJSON(schema, json); callback(transformer.encode(nodeSanitized)); } } catch (e) { fireAnalyticsEvent === null || fireAnalyticsEvent === void 0 ? void 0 : fireAnalyticsEvent({ payload: { action: ACTION.DOCUMENT_PROCESSING_ERROR, actionSubject: ACTION_SUBJECT.EDITOR, eventType: EVENT_TYPE.OPERATIONAL, attributes: { errorMessage: `${e instanceof Error && e.name === 'NodeNestingTransformError' ? 'NodeNestingTransformError - Failed to transform one or more nested tables' : undefined}` } } }); throw e; } }