UNPKG

@atlaskit/editor-common

Version:

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

190 lines (183 loc) • 7.72 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } import { getDocument } from '@atlaskit/browser-apis'; import { EXPERIENCE_FAILURE_REASON } from './consts'; import { popupWithNestedElement } from './experience-utils'; var PORTAL_CONTAINER_SELECTOR = 'body > .atlaskit-portal-container'; /** * Popup check type determines how popups are observed based on their DOM location: * - 'inline': Popups appearing in toolbar button-groups (emoji, media, table selector, image) * - 'editorRoot': Popups attached to editor root (e.g., mention popups) * - 'editorContent': Content-level popups or modals in portal containers (e.g., block menu) * - 'portalRoot': Popups in body > .atlaskit-portal-container (e.g., flags, modals) */ export var ExperienceCheckPopupMutation = /*#__PURE__*/function () { function ExperienceCheckPopupMutation(config) { _classCallCheck(this, ExperienceCheckPopupMutation); _defineProperty(this, "observers", []); this.config = config; } /** * Returns the list of DOM elements to observe based on popup type. */ return _createClass(ExperienceCheckPopupMutation, [{ key: "getObserveTargets", value: function getObserveTargets() { var config = this.config; switch (config.type) { case 'inline': return this.getInlineTargets(config); case 'editorRoot': return this.getEditorRootTargets(config); case 'editorContent': return this.getEditorContentTargets(config); case 'portalRoot': return this.getPortalRootTargets(); } } /** * For 'portalRoot' type: observe .atlaskit-portal-container. * Popups like flags and modals render in body > .atlaskit-portal-container. */ }, { key: "getPortalRootTargets", value: function getPortalRootTargets() { var _getDocument; var portalContainer = (_getDocument = getDocument()) === null || _getDocument === void 0 ? void 0 : _getDocument.querySelector(PORTAL_CONTAINER_SELECTOR); return portalContainer ? [portalContainer] : []; } /** * For 'editorContent' type: observe the target (mount point) and any existing * [data-editor-popup] wrappers within it. Content-level popups and modals * appear in portal containers. */ }, { key: "getEditorContentTargets", value: function getEditorContentTargets(config) { var target = config.getTarget(); if (!target) { return []; } var targets = [target]; var wrappers = target.querySelectorAll('[data-editor-popup]'); var _iterator = _createForOfIteratorHelper(wrappers), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var wrapper = _step.value; targets.push(wrapper); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return targets; } /** * For 'inline' type: observe the target element directly. * The caller is responsible for resolving the correct container * (e.g. the toolbar button-group) via the getTarget function. */ }, { key: "getInlineTargets", value: function getInlineTargets(config) { var target = config.getTarget(); if (!target) { return []; } return [target]; } /** * For 'editorRoot' type: observe the actual editor root container. * The editorDom is the ProseMirror element, but popups appear as direct children * of the parent .akEditor container. So we observe the parent of editorDom. */ }, { key: "getEditorRootTargets", value: function getEditorRootTargets(config) { var targets = []; var editorDom = config.getEditorDom(); if (editorDom) { var editorRoot = editorDom.closest('.akEditor') || editorDom.parentElement; if (editorRoot instanceof HTMLElement) { targets.push(editorRoot); } } return targets; } }, { key: "start", value: function start(callback) { var _this = this; var doc = getDocument(); var observeTargets = this.getObserveTargets(); if (!doc || !observeTargets.length) { callback({ status: 'failure', reason: EXPERIENCE_FAILURE_REASON.DOM_MUTATION_TARGET_NOT_FOUND }); return; } var query = this.config.nestedElementQuery; var subtree = this.config.type === 'inline' && this.config.subtree === true; var observe = function observe(el) { var observer = new MutationObserver(onMutation); observer.observe(el, { childList: true, subtree: subtree }); _this.observers.push(observer); }; var onMutation = function onMutation(mutations) { var found = mutations.some(function (_ref) { var type = _ref.type, addedNodes = _ref.addedNodes; return type === 'childList' && _toConsumableArray(addedNodes).some(function (node) { return node instanceof HTMLElement && (popupWithNestedElement(node, query) || node.matches(query) || node.querySelector(query) !== null); }); }); if (found) { callback({ status: 'success' }); return; } }; var _iterator2 = _createForOfIteratorHelper(observeTargets), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var observeTarget = _step2.value; observe(observeTarget); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } }, { key: "stop", value: function stop() { var _iterator3 = _createForOfIteratorHelper(this.observers), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var observer = _step3.value; observer.disconnect(); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } this.observers = []; } }]); }();