UNPKG

@atlaskit/editor-common

Version:

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

48 lines (46 loc) 2.07 kB
// Disable no-re-export rule for entry point files /* eslint-disable @atlaskit/editor/no-re-export */ import { Node as PMNode } from '@atlaskit/editor-prosemirror/model'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; const isPMNode = nodeOrMark => { return nodeOrMark instanceof PMNode || Array.isArray(nodeOrMark.marks); }; /** * 🧱 Internal Helper Function: Editor FE Platform * * Creates a set of generic metadata attributes for a ProseMirror node or mark. * These attributes are used to annotate the DOM representation with information * about the node or mark type. * * @param {PMNode | PMMark} nodeOrMark - The ProseMirror node or mark to create metadata for. * @returns {Record<string, string>} An object containing metadata attributes. * - `data-prosemirror-content-type`: Specifies if the content is a node or mark. * - `data-prosemirror-node-name` (if applicable): The name of the node. * - `data-prosemirror-node-block` (if applicable): Indicates if the node is a block. * - `data-prosemirror-node-inline` (if applicable): Indicates if the node is inline. * - `data-prosemirror-mark-name` (if applicable): The name of the mark. */ export const createProseMirrorMetadata = (nodeOrMark, options) => { const name = nodeOrMark.type.name; const isNode = isPMNode(nodeOrMark); const commonAttributes = { 'data-prosemirror-content-type': isNode ? 'node' : 'mark' }; if (!isNode) { return { ...commonAttributes, ['data-prosemirror-mark-name']: name }; } commonAttributes['data-prosemirror-node-name'] = name; if (nodeOrMark.type.isBlock) { commonAttributes['data-prosemirror-node-block'] = 'true'; } if (nodeOrMark.type.isInline) { commonAttributes['data-prosemirror-node-inline'] = 'true'; } if ((options === null || options === void 0 ? void 0 : options.anchrorId) !== undefined && expValEquals('platform_editor_native_anchor_with_dnd', 'isEnabled', true)) { commonAttributes['data-node-anchor'] = options.anchrorId; } return commonAttributes; };