UNPKG

@atlaskit/editor-common

Version:

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

141 lines (133 loc) • 7.48 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.attachGenericProseMirrorMetadata = exports.SafePlugin = void 0; var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _state = require("@atlaskit/editor-prosemirror/state"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _isSsr = require("../core-utils/is-ssr"); var _nodeAnchorProvider = require("../node-anchor/node-anchor-provider"); var _prosemirrorDomMetadata = require("../prosemirror-dom-metadata"); var _nativeAnchor = require("../styles/shared/native-anchor"); function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /** * 🧱 Internal Helper Function: Editor FE Platform * * Attaches generic ProseMirror metadata attributes to a given DOM element based on the properties of a ProseMirror node. * This function is useful for annotating DOM elements with metadata that describes the type and characteristics of the ProseMirror node. * * * @param {object} params - The parameters for the function. * @param {PMNode} params.node - The ProseMirror node from which to derive metadata. * @param {HTMLElement} params.dom - The DOM element to which the metadata attributes will be attached. */ var attachGenericProseMirrorMetadata = exports.attachGenericProseMirrorMetadata = function attachGenericProseMirrorMetadata(_ref) { var nodeOrMark = _ref.nodeOrMark, dom = _ref.dom, options = _ref.options; var metadata = (0, _prosemirrorDomMetadata.createProseMirrorMetadata)(nodeOrMark, options); Object.entries(metadata).forEach(function (_ref2) { var _ref3 = (0, _slicedToArray2.default)(_ref2, 2), name = _ref3[0], value = _ref3[1]; dom.setAttribute(name, value); if (name === 'data-node-anchor' && (0, _expValEquals.expValEquals)('platform_editor_native_anchor_with_dnd', 'isEnabled', true)) { // if browser doesn't support CSS anchor, won't need the style // Or if it supports CSS attr() function as the value of anchor-name, // We won't need set the style if (!(0, _nativeAnchor.isCSSAnchorSupported)() || (0, _nativeAnchor.isCSSAttrAnchorSupported)()) { return; } // otherwise, we set the CSS variable for anchor-name dom.style.setProperty(_nativeAnchor.ANCHOR_VARIABLE_NAME, "".concat(value)); return; } }); }; /** Type guard to check if a Node is an HTMLElement in a safe way. */ var isHTMLElement = function isHTMLElement(element) { if (element === null || element === undefined) { return false; } // In SSR `HTMLElement` is not defined, so we need to use duck typing here return 'innerHTML' in element && 'style' in element && 'classList' in element; }; // Wraper to avoid any exception during the get pos operation // See this https://hello.atlassian.net/wiki/spaces/EDITOR/pages/2849713193/ED-19672+Extensions+Regression // And this https://discuss.prosemirror.net/t/possible-bug-on-viewdesc-posbeforechild/5783 var wrapGetPosExceptions = function wrapGetPosExceptions(spec) { var _spec$props; if (!(spec !== null && spec !== void 0 && (_spec$props = spec.props) !== null && _spec$props !== void 0 && _spec$props.nodeViews)) { return spec; } var unsafeNodeViews = spec.props.nodeViews; var nodeIdProvider; var safeNodeViews = new Proxy(unsafeNodeViews, { get: function get(target, prop, receiver) { var safeNodeView = new Proxy(Reflect.get(target, prop, receiver), { apply: function apply(target, thisArg, argumentsList) { var _argumentsList = (0, _toArray2.default)(argumentsList), node = _argumentsList[0], view = _argumentsList[1], unsafeGetPos = _argumentsList[2], more = _argumentsList.slice(3); if (!nodeIdProvider && (0, _expValEquals.expValEquals)('platform_editor_native_anchor_with_dnd', 'isEnabled', true)) { nodeIdProvider = (0, _nodeAnchorProvider.getNodeIdProvider)(view); } var safeGetPos = function () { try { return unsafeGetPos(); } catch (e) { return; } // eslint-disable-next-line no-extra-bind }.bind(thisArg); var result = Reflect.apply(target, thisArg, [node, view, safeGetPos].concat((0, _toConsumableArray2.default)(more))); if ((result === null || result === void 0 ? void 0 : result.dom) instanceof HTMLElement || // SSR result?.dom is not an instance of HTMLElement, but we still want to attach metadata to it (0, _isSsr.isSSR)() && isHTMLElement(result === null || result === void 0 ? void 0 : result.dom) && (0, _platformFeatureFlags.fg)('platform_editor_native_anchor_patch_3')) { var _nodeIdProvider; // we only attach metadata to the dom if its position is known var pos = safeGetPos(); var options = pos !== undefined && (0, _expValEquals.expValEquals)('platform_editor_native_anchor_with_dnd', 'isEnabled', true) ? { anchrorId: (_nodeIdProvider = nodeIdProvider) === null || _nodeIdProvider === void 0 ? void 0 : _nodeIdProvider.getOrGenerateId(node, pos) } : undefined; attachGenericProseMirrorMetadata({ nodeOrMark: node, dom: result.dom, options: options }); } return result; } }); return safeNodeView; } }); spec.props.nodeViews = safeNodeViews; return spec; }; // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any var SafePlugin = exports.SafePlugin = /*#__PURE__*/function (_Plugin) { // This variable isn't (and shouldn't) be used anywhere. Its purpose is // to distinguish Plugin from SafePlugin, thus ensuring that an 'unsafe' // Plugin cannot be assigned as an item in EditorPlugin → pmPlugins. function SafePlugin(spec) { (0, _classCallCheck2.default)(this, SafePlugin); return _callSuper(this, SafePlugin, [wrapGetPosExceptions(spec)]); } (0, _inherits2.default)(SafePlugin, _Plugin); return (0, _createClass2.default)(SafePlugin); }(_state.Plugin);