@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
120 lines • 4.75 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import memoizeOne from 'memoize-one';
import { getAnchorAttrName } from '../../ui/utils/dom-attr-name';
export var isAnchorSupported = memoizeOne(function () {
// directly use CSS would cause failed SSR tests.
if (window.CSS && window.CSS.supports) {
return window.CSS.supports('anchor-name: --a');
}
return false;
});
export var AnchorRectCache = /*#__PURE__*/function () {
function AnchorRectCache() {
_classCallCheck(this, AnchorRectCache);
_defineProperty(this, "anchorRectMap", {});
_defineProperty(this, "isAnchorSupported", isAnchorSupported());
_defineProperty(this, "isDirty", true);
_defineProperty(this, "view", null);
}
return _createClass(AnchorRectCache, [{
key: "clear",
value: function clear() {
this.isDirty = true;
this.anchorRectMap = {};
}
}, {
key: "getRects",
value: function getRects() {
if (this.isDirty) {
var _this$view;
var anchorElements = ((_this$view = this.view) === null || _this$view === void 0 ? void 0 : _this$view.dom.querySelectorAll("[".concat(getAnchorAttrName(), "]"))) || [];
this.anchorRectMap = Array.from(anchorElements).reduce(function (prev, curr) {
var anchorName = curr.getAttribute(getAnchorAttrName());
if (anchorName) {
return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, anchorName, {
height: curr.clientHeight,
top: curr.offsetTop,
left: curr.offsetLeft,
right: curr.offsetLeft + curr.clientWidth,
width: curr.clientWidth,
bottom: curr.offsetTop + curr.clientHeight
}));
}
return prev;
}, {});
this.isDirty = false;
}
return this.anchorRectMap;
}
}, {
key: "setEditorView",
value: function setEditorView(view) {
if (this.view !== view) {
this.view = view;
}
}
}, {
key: "getHeight",
value: function getHeight(anchorName) {
var _rects$anchorName;
if (this.isAnchorSupported) {
return null;
}
var rects = this.getRects();
return (_rects$anchorName = rects[anchorName]) === null || _rects$anchorName === void 0 ? void 0 : _rects$anchorName.height;
}
}, {
key: "getWidth",
value: function getWidth(anchorName) {
var _rects$anchorName2;
if (this.isAnchorSupported) {
return null;
}
var rects = this.getRects();
return (_rects$anchorName2 = rects[anchorName]) === null || _rects$anchorName2 === void 0 ? void 0 : _rects$anchorName2.width;
}
}, {
key: "getLeft",
value: function getLeft(anchorName) {
var _rects$anchorName3;
if (this.isAnchorSupported) {
return null;
}
var rects = this.getRects();
return (_rects$anchorName3 = rects[anchorName]) === null || _rects$anchorName3 === void 0 ? void 0 : _rects$anchorName3.left;
}
}, {
key: "getTop",
value: function getTop(anchorName) {
var _rects$anchorName4;
if (this.isAnchorSupported) {
return null;
}
var rects = this.getRects();
return (_rects$anchorName4 = rects[anchorName]) === null || _rects$anchorName4 === void 0 ? void 0 : _rects$anchorName4.top;
}
}, {
key: "getRight",
value: function getRight(anchorName) {
var _rects$anchorName5;
if (this.isAnchorSupported) {
return null;
}
var rects = this.getRects();
return (_rects$anchorName5 = rects[anchorName]) === null || _rects$anchorName5 === void 0 ? void 0 : _rects$anchorName5.right;
}
}, {
key: "getRect",
value: function getRect(anchorName) {
if (this.isAnchorSupported) {
return null;
}
var rects = this.getRects();
return rects[anchorName];
}
}]);
}();