UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

65 lines (62 loc) 4.34 kB
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 { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view'; export var TYPE_SURFACE_ANCHOR_DEC = 'block-controls-surface-anchor'; export var SURFACE_ANCHOR_ATTR = 'data-block-controls-surface-anchor'; export var SURFACE_NODE_TYPE_ATTR = 'data-block-controls-surface-node-type'; export var SURFACE_ANCHOR_VARIABLE = '--block-controls-surface-anchor-name'; /** * These node views expose the box used by the measured placement fallback rather than their * ProseMirror node wrapper. The marker carries the name through a custom property so sparse CSS * can name that inner box without adding a widget or changing the node view. */ var INNER_CONTENT_NODE_TYPES = new Set(['blockCard', 'bodiedExtension', 'embedCard', 'extension', 'mediaSingle', 'multiBodiedExtension', 'table']); var getMarkerStyle = function getMarkerStyle(nodeType, anchorName) { var inheritedName = "".concat(SURFACE_ANCHOR_VARIABLE, ": ").concat(anchorName, ";"); // Inner boxes get their anchor from the sparse global selector. Naming only the node wrapper // would position media/table/extension controls against the wrapper instead of visible content. return INNER_CONTENT_NODE_TYPES.has(nodeType) ? inheritedName : "".concat(inheritedName, " anchor-name: ").concat(anchorName, ";"); }; /** * Builds sparse CSS anchor decorations from already-qualified cached positions. Anchor identity is * supplied by the editor's native node-anchor provider so sparse surfaces and consumers share the * same `data-node-anchor`. */ export var createSurfaceAnchorDecorations = function createSurfaceAnchorDecorations(state, positions, resolveAnchorName, previousAnchors) { var anchors = new Map(); var decorations = []; var _iterator = _createForOfIteratorHelper(positions), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var position = _step.value; if (anchors.has(position)) { continue; } var node = state.doc.nodeAt(position); if (!(node !== null && node !== void 0 && node.isBlock)) { continue; } var anchorName = resolveAnchorName(node, position, previousAnchors === null || previousAnchors === void 0 ? void 0 : previousAnchors.get(position)); if (!anchorName) { continue; } anchors.set(position, anchorName); decorations.push(Decoration.node(position, position + node.nodeSize, _defineProperty(_defineProperty(_defineProperty({}, SURFACE_ANCHOR_ATTR, anchorName), SURFACE_NODE_TYPE_ATTR, node.type.name), "style", getMarkerStyle(node.type.name, anchorName)), { type: TYPE_SURFACE_ANCHOR_DEC, anchorName: anchorName, nodeType: node.type.name })); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return { anchors: anchors, decorations: DecorationSet.create(state.doc, decorations) }; };