UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

47 lines 1.89 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import ReactDOM from 'react-dom'; // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import uuid from 'uuid'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { isFontSizeMarkActive } from './utils/validation'; export const TYPE_DROP_TARGET_DEC = 'drop-target-decoration'; export const TYPE_HANDLE_DEC = 'drag-handle'; export const TYPE_NODE_DEC = 'node-decoration'; export const NESTED_DEPTH = 100; export const getNodeAnchor = node => { const handleId = ObjHash.getForNode(node); return `--node-anchor-${node.type.name}-${handleId}`; }; const getSubType = node => { if (node.attrs.level) { return `-${node.attrs.level}`; } if (isFontSizeMarkActive(node)) { return '-small'; } return ''; }; export const getNodeTypeWithLevel = node => { const subType = expValEquals('platform_editor_small_font_size', 'isEnabled', true) ? getSubType(node) : node.attrs.level ? `-${node.attrs.level}` : ''; return node.type.name + subType; }; class ObjHash { static getForNode(node) { if (this.caching.has(node)) { return this.caching.get(node); } // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead const uniqueId = uuid(); this.caching.set(node, uniqueId); return uniqueId; } } _defineProperty(ObjHash, "caching", new WeakMap()); export const unmountDecorations = (nodeViewPortalProviderAPI, selector, key) => { // Removing decorations manually instead of using native destroy function in prosemirror API // as it was more responsive and causes less re-rendering const decorationsToRemove = document.querySelectorAll(`[${selector}="true"]`); decorationsToRemove.forEach(el => { ReactDOM.unmountComponentAtNode(el); }); };