@atlaskit/editor-plugin-block-controls 1.4.9 → 1.4.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/cjs/plugin.js +40 -2
  3. package/dist/cjs/pm-plugins/decorations.js +60 -25
  4. package/dist/cjs/pm-plugins/main.js +82 -60
  5. package/dist/cjs/ui/drag-handle.js +51 -29
  6. package/dist/cjs/ui/drag-preview.js +4 -4
  7. package/dist/cjs/ui/drop-target.js +1 -1
  8. package/dist/cjs/ui/mouse-move-wrapper.js +92 -0
  9. package/dist/cjs/utils/drag-handle-positions.js +23 -0
  10. package/dist/cjs/utils/getSelection.js +11 -1
  11. package/dist/cjs/utils/index.js +6 -0
  12. package/dist/es2019/plugin.js +38 -2
  13. package/dist/es2019/pm-plugins/decorations.js +60 -25
  14. package/dist/es2019/pm-plugins/main.js +74 -55
  15. package/dist/es2019/ui/drag-handle.js +56 -33
  16. package/dist/es2019/ui/drag-preview.js +4 -4
  17. package/dist/es2019/ui/drop-target.js +1 -1
  18. package/dist/es2019/ui/mouse-move-wrapper.js +77 -0
  19. package/dist/es2019/utils/drag-handle-positions.js +17 -0
  20. package/dist/es2019/utils/getSelection.js +10 -0
  21. package/dist/es2019/utils/index.js +1 -1
  22. package/dist/esm/plugin.js +40 -2
  23. package/dist/esm/pm-plugins/decorations.js +60 -25
  24. package/dist/esm/pm-plugins/main.js +82 -61
  25. package/dist/esm/ui/drag-handle.js +53 -31
  26. package/dist/esm/ui/drag-preview.js +4 -4
  27. package/dist/esm/ui/drop-target.js +1 -1
  28. package/dist/esm/ui/mouse-move-wrapper.js +84 -0
  29. package/dist/esm/utils/drag-handle-positions.js +17 -0
  30. package/dist/esm/utils/getSelection.js +10 -0
  31. package/dist/esm/utils/index.js +1 -1
  32. package/dist/types/pm-plugins/decorations.d.ts +10 -3
  33. package/dist/types/types.d.ts +13 -1
  34. package/dist/types/ui/drag-handle.d.ts +6 -3
  35. package/dist/types/ui/drag-preview.d.ts +1 -1
  36. package/dist/types/ui/mouse-move-wrapper.d.ts +11 -0
  37. package/dist/types/utils/drag-handle-positions.d.ts +2 -0
  38. package/dist/types/utils/getSelection.d.ts +1 -0
  39. package/dist/types/utils/index.d.ts +1 -1
  40. package/dist/types-ts4.5/pm-plugins/decorations.d.ts +10 -3
  41. package/dist/types-ts4.5/types.d.ts +13 -1
  42. package/dist/types-ts4.5/ui/drag-handle.d.ts +6 -3
  43. package/dist/types-ts4.5/ui/drag-preview.d.ts +1 -1
  44. package/dist/types-ts4.5/ui/mouse-move-wrapper.d.ts +11 -0
  45. package/dist/types-ts4.5/utils/drag-handle-positions.d.ts +2 -0
  46. package/dist/types-ts4.5/utils/getSelection.d.ts +1 -0
  47. package/dist/types-ts4.5/utils/index.d.ts +1 -1
  48. package/package.json +5 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @atlaskit/editor-plugin-block-controls
2
2
 
3
+ ## 1.4.11
4
+
5
+ ### Patch Changes
6
+
7
+ - [#108763](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/108763)
8
+ [`f97149e66b556`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/f97149e66b556) -
9
+ Fix table scroll when table selected by drag handle
10
+ - Updated dependencies
11
+
12
+ ## 1.4.10
13
+
14
+ ### Patch Changes
15
+
16
+ - [#105071](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/105071)
17
+ [`e70e9b713e1e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/e70e9b713e1e) -
18
+ ED23246 - Refactor decorations to render wrapper around each node to track mouseenter events and
19
+ show drag handle when active node is set
20
+
3
21
  ## 1.4.9
4
22
 
5
23
  ### Patch Changes
@@ -9,7 +9,7 @@ var _react = _interopRequireDefault(require("react"));
9
9
  var _main = require("./pm-plugins/main");
10
10
  var _dragHandleMenu = require("./ui/drag-handle-menu");
11
11
  var _globalStyles = require("./ui/global-styles");
12
- var _getSelection = require("./utils/getSelection");
12
+ var _utils = require("./utils");
13
13
  var blockControlsPlugin = exports.blockControlsPlugin = function blockControlsPlugin(_ref) {
14
14
  var api = _ref.api;
15
15
  return {
@@ -28,16 +28,54 @@ var blockControlsPlugin = exports.blockControlsPlugin = function blockControlsPl
28
28
  var _node$nodeSize;
29
29
  var tr = _ref2.tr;
30
30
  var node = tr.doc.nodeAt(start);
31
+ if (!node) {
32
+ return tr;
33
+ }
31
34
  var size = (_node$nodeSize = node === null || node === void 0 ? void 0 : node.nodeSize) !== null && _node$nodeSize !== void 0 ? _node$nodeSize : 1;
32
35
  var end = start + size;
33
36
  var nodeCopy = tr.doc.content.cut(start, end); // cut the content
34
37
  tr.delete(start, end); // delete the content from the original position
35
38
  var mappedTo = tr.mapping.map(to);
36
39
  tr.insert(mappedTo, nodeCopy); // insert the content at the new position
37
- tr.setSelection((0, _getSelection.getSelection)(tr, mappedTo));
40
+ tr = (0, _utils.selectNode)(tr, mappedTo, node.type.name);
41
+ tr.setMeta(_main.key, {
42
+ nodeMoved: true
43
+ });
38
44
  api === null || api === void 0 || api.core.actions.focus();
39
45
  return tr;
40
46
  };
47
+ },
48
+ showDragHandleAt: function showDragHandleAt(pos, anchorName, nodeType) {
49
+ return function (_ref3) {
50
+ var tr = _ref3.tr;
51
+ tr.setMeta(_main.key, {
52
+ activeNode: {
53
+ pos: pos,
54
+ anchorName: anchorName,
55
+ nodeType: nodeType
56
+ }
57
+ });
58
+ return tr;
59
+ };
60
+ },
61
+ setNodeDragged: function setNodeDragged(pos, anchorName, nodeType) {
62
+ return function (_ref4) {
63
+ var tr = _ref4.tr;
64
+ if (pos === undefined) {
65
+ return tr;
66
+ }
67
+ var newTr = tr;
68
+ newTr = (0, _utils.selectNode)(newTr, pos, nodeType);
69
+ newTr.setMeta(_main.key, {
70
+ isDragging: true,
71
+ activeNode: {
72
+ pos: pos,
73
+ anchorName: anchorName,
74
+ nodeType: nodeType
75
+ }
76
+ });
77
+ return newTr;
78
+ };
41
79
  }
42
80
  },
43
81
  getSharedState: function getSharedState(editorState) {
@@ -4,13 +4,14 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.dropTargetDecorations = exports.dragHandleDecoration = void 0;
7
+ exports.nodeDecorations = exports.mouseMoveWrapperDecorations = exports.dropTargetDecorations = exports.dragHandleDecoration = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
8
9
  var _react = require("react");
9
10
  var _reactDom = _interopRequireDefault(require("react-dom"));
10
11
  var _view = require("@atlaskit/editor-prosemirror/view");
11
- var _consts = require("../ui/consts");
12
12
  var _dragHandle = require("../ui/drag-handle");
13
13
  var _dropTarget = require("../ui/drop-target");
14
+ var _mouseMoveWrapper = require("../ui/mouse-move-wrapper");
14
15
  var dropTargetDecorations = exports.dropTargetDecorations = function dropTargetDecorations(oldState, newState, api) {
15
16
  var decs = [];
16
17
  // Decoration state is used to keep track of the position of the drop targets
@@ -28,6 +29,8 @@ var dropTargetDecorations = exports.dropTargetDecorations = function dropTargetD
28
29
  index: index
29
30
  }), element);
30
31
  return element;
32
+ }, {
33
+ type: 'drop-target-decoration'
31
34
  }));
32
35
  return false;
33
36
  });
@@ -50,39 +53,71 @@ var dropTargetDecorations = exports.dropTargetDecorations = function dropTargetD
50
53
  index: decorationState.length
51
54
  }), element);
52
55
  return element;
56
+ }, {
57
+ type: 'drop-target-decoration'
53
58
  }));
54
59
  return {
55
60
  decs: decs,
56
61
  decorationState: decorationState
57
62
  };
58
63
  };
59
- var dragHandleDecoration = exports.dragHandleDecoration = function dragHandleDecoration(oldState, meta, api) {
60
- return _view.DecorationSet.create(oldState.doc, [_view.Decoration.widget(meta.pos, function (view, getPos) {
64
+ var nodeDecorations = exports.nodeDecorations = function nodeDecorations(newState) {
65
+ var decs = [];
66
+ newState.doc.descendants(function (node, pos, _parent, index) {
67
+ var anchorName = "--node-anchor-".concat(node.type.name, "-").concat(index);
68
+ var style = "anchor-name: ".concat(anchorName, "; ").concat(pos === 0 ? 'margin-top: 0px;' : '');
69
+ decs.push(_view.Decoration.node(pos, pos + node.nodeSize, (0, _defineProperty2.default)({
70
+ style: style
71
+ }, 'data-drag-handler-anchor-name', anchorName)));
72
+ return false;
73
+ });
74
+ return decs;
75
+ };
76
+ /**
77
+ * Setting up decorations around each node to track mousemove events into each node
78
+ * When a mouseenter event is triggered on the node, we will set the activeNode to the node
79
+ * And show the drag handle
80
+ */
81
+ var mouseMoveWrapperDecorations = exports.mouseMoveWrapperDecorations = function mouseMoveWrapperDecorations(newState, api) {
82
+ var decs = [];
83
+ newState.doc.descendants(function (node, pos, _parent, index) {
84
+ var anchorName = "--node-anchor-".concat(node.type.name, "-").concat(index);
85
+ decs.push(_view.Decoration.widget(pos, function (view, getPos) {
86
+ var element = document.createElement('div');
87
+ _reactDom.default.render( /*#__PURE__*/(0, _react.createElement)(_mouseMoveWrapper.MouseMoveWrapper, {
88
+ view: view,
89
+ api: api,
90
+ anchorName: anchorName,
91
+ nodeType: node.type.name,
92
+ getPos: getPos
93
+ }), element);
94
+ return element;
95
+ }, {
96
+ type: 'mouse-move-wrapper',
97
+ side: -1,
98
+ ignoreSelection: true,
99
+ stopEvent: function stopEvent(e) {
100
+ return true;
101
+ }
102
+ }));
103
+ return false;
104
+ });
105
+ return decs;
106
+ };
107
+ var dragHandleDecoration = exports.dragHandleDecoration = function dragHandleDecoration(pos, anchorName, nodeType, api) {
108
+ return _view.Decoration.widget(pos, function (view, getPos) {
61
109
  var element = document.createElement('div');
110
+ element.setAttribute('data-testid', 'block-ctrl-decorator-widget');
62
111
  _reactDom.default.render( /*#__PURE__*/(0, _react.createElement)(_dragHandle.DragHandle, {
63
- dom: meta.dom,
112
+ view: view,
64
113
  api: api,
65
- start: meta.pos
114
+ getPos: getPos,
115
+ anchorName: anchorName,
116
+ nodeType: nodeType
66
117
  }), element);
67
- element.style.position = 'absolute';
68
- element.style.zIndex = '1';
69
-
70
- // If the selected node is a table or mediaSingle with resizer, we need to adjust the position of the drag handle
71
- var resizer = ['table', 'mediaSingle'].includes(meta.type) ? meta.dom.querySelector('.resizer-item') : null;
72
- if (resizer) {
73
- element.style.left = getComputedStyle(resizer).transform === 'none' ? "".concat(resizer.offsetLeft - (0, _consts.dragHandleGap)(meta.type) - _consts.DRAG_HANDLE_WIDTH, "px") : "".concat(resizer.offsetLeft - resizer.offsetWidth / 2 - (0, _consts.dragHandleGap)(meta.type) - _consts.DRAG_HANDLE_WIDTH, "px");
74
- } else {
75
- element.style.left = "".concat(meta.dom.offsetLeft - (0, _consts.dragHandleGap)(meta.type) - _consts.DRAG_HANDLE_WIDTH, "px");
76
- }
77
- if (meta.type === 'table') {
78
- var table = meta.dom.querySelector('table');
79
- element.style.top = "".concat(meta.dom.offsetTop + ((table === null || table === void 0 ? void 0 : table.offsetTop) || 0), "px");
80
- } else {
81
- element.style.top = "".concat(meta.dom.offsetTop, "px");
82
- }
83
- element.setAttribute('data-testid', 'block-ctrl-decorator-widget');
84
118
  return element;
85
119
  }, {
86
- side: -1
87
- })]);
120
+ side: -1,
121
+ id: 'drag-handle'
122
+ });
88
123
  };
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.key = exports.createPlugin = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _rafSchd = _interopRequireDefault(require("raf-schd"));
7
10
  var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
11
  var _state = require("@atlaskit/editor-prosemirror/state");
9
12
  var _view = require("@atlaskit/editor-prosemirror/view");
@@ -21,47 +24,67 @@ var createPlugin = exports.createPlugin = function createPlugin(api) {
21
24
  isDragging: false,
22
25
  isMenuOpen: false,
23
26
  start: null,
24
- end: null
27
+ end: null,
28
+ editorHeight: 0
25
29
  };
26
30
  },
27
31
  apply: function apply(tr, currentState, oldState, newState) {
28
- var _decorationState, _meta$pos, _meta$isDragging;
32
+ var _decorationState, _meta$activeNode, _meta$isDragging, _meta$editorHeight;
29
33
  // return currentState;
30
34
  var activeNode = currentState.activeNode,
31
35
  decorations = currentState.decorations,
32
36
  isMenuOpen = currentState.isMenuOpen,
33
- decorationState = currentState.decorationState;
37
+ decorationState = currentState.decorationState,
38
+ editorHeight = currentState.editorHeight;
34
39
  var meta = tr.getMeta(key);
35
40
 
36
- // Drag handle decoration
37
- if (meta && meta.pos !== (activeNode === null || activeNode === void 0 ? void 0 : activeNode.pos) && api) {
38
- decorations = (0, _decorations.dragHandleDecoration)(newState, meta, api);
39
- }
40
- // Drop target decorations
41
- if (meta !== null && meta !== void 0 && meta.isDragging && api) {
41
+ // Draw node and mouseWrapper decorations at top level node if decorations is empty, editor height changes or node is moved
42
+ var redrawDecorations = decorations === _view.DecorationSet.empty || (meta === null || meta === void 0 ? void 0 : meta.editorHeight) !== undefined && (meta === null || meta === void 0 ? void 0 : meta.editorHeight) !== editorHeight || tr.docChanged && tr.doc.childCount === newState.doc.childCount || (meta === null || meta === void 0 ? void 0 : meta.nodeMoved) && tr.docChanged;
43
+ if (redrawDecorations && api) {
42
44
  decorations = _view.DecorationSet.create(newState.doc, []);
45
+ var nodeDecs = (0, _decorations.nodeDecorations)(newState);
46
+ var mouseWrapperDecs = (0, _decorations.mouseMoveWrapperDecorations)(newState, api);
47
+ decorations = decorations.add(newState.doc, [].concat((0, _toConsumableArray2.default)(nodeDecs), (0, _toConsumableArray2.default)(mouseWrapperDecs)));
48
+ if (activeNode) {
49
+ var draghandleDec = (0, _decorations.dragHandleDecoration)(activeNode.pos, activeNode.anchorName, activeNode.nodeType, api);
50
+ decorations = decorations.add(newState.doc, [draghandleDec]);
51
+ }
52
+ }
53
+
54
+ // Remove previous drag handle widget and draw new drag handle widget when activeNode changes
55
+ if (meta !== null && meta !== void 0 && meta.activeNode && (meta === null || meta === void 0 ? void 0 : meta.activeNode.pos) !== (activeNode === null || activeNode === void 0 ? void 0 : activeNode.pos) && (meta === null || meta === void 0 ? void 0 : meta.activeNode.anchorName) !== (activeNode === null || activeNode === void 0 ? void 0 : activeNode.anchorName) && api) {
56
+ var oldHandle = decorations.find().filter(function (_ref) {
57
+ var spec = _ref.spec;
58
+ return spec.id === 'drag-handle';
59
+ });
60
+ decorations = decorations.remove(oldHandle);
61
+ var decs = (0, _decorations.dragHandleDecoration)(meta.activeNode.pos, meta.activeNode.anchorName, meta.activeNode.nodeType, api);
62
+ decorations = decorations.add(newState.doc, [decs]);
63
+ }
64
+
65
+ // Add drop targets when node is being dragged
66
+ if (meta !== null && meta !== void 0 && meta.isDragging && !tr.docChanged && api) {
43
67
  var _dropTargetDecoration = (0, _decorations.dropTargetDecorations)(oldState, newState, api),
44
- decs = _dropTargetDecoration.decs,
68
+ _decs = _dropTargetDecoration.decs,
45
69
  updatedDecorationState = _dropTargetDecoration.decorationState;
46
70
  decorationState = updatedDecorationState;
47
- decorations = decorations.add(newState.doc, decs);
71
+ decorations = decorations.add(newState.doc, _decs);
48
72
  }
49
73
 
50
- // Remove drop target decorations when dragging is stopped
51
- if ((meta === null || meta === void 0 ? void 0 : meta.isDragging) === false) {
52
- decorations = _view.DecorationSet.create(newState.doc, []);
53
- }
54
-
55
- // Map decorations when the document changes
56
- if (tr.docChanged && decorations !== _view.DecorationSet.empty) {
57
- decorations = decorations.map(tr.mapping, tr.doc);
74
+ // Remove drop target decoration when dragging stops
75
+ if ((meta === null || meta === void 0 ? void 0 : meta.isDragging) === false && !tr.docChanged) {
76
+ var dropTargetDecs = decorations.find().filter(function (_ref2) {
77
+ var spec = _ref2.spec;
78
+ return spec.type === 'drop-target-decoration';
79
+ });
80
+ decorations = decorations.remove(dropTargetDecs);
58
81
  }
59
82
 
60
83
  // Map drop target decoration positions when the document changes
61
84
  if (tr.docChanged && currentState.isDragging) {
62
- decorationState = decorationState.map(function (_ref) {
63
- var index = _ref.index,
64
- pos = _ref.pos;
85
+ decorationState = decorationState.map(function (_ref3) {
86
+ var index = _ref3.index,
87
+ pos = _ref3.pos;
65
88
  return {
66
89
  index: index,
67
90
  pos: tr.mapping.map(pos)
@@ -69,16 +92,24 @@ var createPlugin = exports.createPlugin = function createPlugin(api) {
69
92
  });
70
93
  }
71
94
 
95
+ // Map decorations if document changes and node decorations do not need to be redrawn
96
+ if (tr.docChanged && !redrawDecorations) {
97
+ decorations = decorations.map(tr.mapping, tr.doc);
98
+ }
99
+
72
100
  // Map active node position when the document changes
73
- var mappedActiveNodePos = tr.docChanged && activeNode ? tr.mapping.map(activeNode.pos) : activeNode === null || activeNode === void 0 ? void 0 : activeNode.pos;
101
+ var mappedActiveNodePos = tr.docChanged && activeNode ? {
102
+ pos: tr.mapping.map(activeNode.pos),
103
+ anchorName: activeNode.anchorName,
104
+ nodeType: activeNode.nodeType
105
+ } : activeNode;
74
106
  return {
75
107
  decorations: decorations,
76
108
  decorationState: (_decorationState = decorationState) !== null && _decorationState !== void 0 ? _decorationState : currentState.decorationState,
77
- activeNode: {
78
- pos: (_meta$pos = meta === null || meta === void 0 ? void 0 : meta.pos) !== null && _meta$pos !== void 0 ? _meta$pos : mappedActiveNodePos
79
- },
109
+ activeNode: (_meta$activeNode = meta === null || meta === void 0 ? void 0 : meta.activeNode) !== null && _meta$activeNode !== void 0 ? _meta$activeNode : mappedActiveNodePos,
80
110
  isDragging: (_meta$isDragging = meta === null || meta === void 0 ? void 0 : meta.isDragging) !== null && _meta$isDragging !== void 0 ? _meta$isDragging : currentState.isDragging,
81
- isMenuOpen: meta !== null && meta !== void 0 && meta.toggleMenu ? !isMenuOpen : isMenuOpen
111
+ isMenuOpen: meta !== null && meta !== void 0 && meta.toggleMenu ? !isMenuOpen : isMenuOpen,
112
+ editorHeight: (_meta$editorHeight = meta === null || meta === void 0 ? void 0 : meta.editorHeight) !== null && _meta$editorHeight !== void 0 ? _meta$editorHeight : currentState.editorHeight
82
113
  };
83
114
  }
84
115
  },
@@ -86,40 +117,31 @@ var createPlugin = exports.createPlugin = function createPlugin(api) {
86
117
  decorations: function decorations(state) {
87
118
  var _key$getState;
88
119
  return (_key$getState = key.getState(state)) === null || _key$getState === void 0 ? void 0 : _key$getState.decorations;
89
- },
90
- handleDOMEvents: {
91
- mousemove: function mousemove(view, event) {
92
- var pos = view.posAtCoords({
93
- left: event.clientX,
94
- top: event.clientY
95
- });
96
- if ((pos === null || pos === void 0 ? void 0 : pos.inside) !== undefined && pos.inside >= 0) {
97
- var _api$core;
98
- var node = view.state.doc.nodeAt(pos.inside);
99
- if (!node) {
100
- return;
101
- }
102
- var resolvedPos = view.state.doc.resolve(pos.pos);
103
- var topLevelPos = resolvedPos.before(1); // 1 here restricts the depth to the root level
104
- var topLevelNode = view.state.doc.nodeAt(topLevelPos);
105
- if (!topLevelNode) {
106
- return;
107
- }
108
- var dom = view.nodeDOM(topLevelPos);
109
- if (!dom) {
110
- return;
111
- }
112
- api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(function (_ref2) {
113
- var tr = _ref2.tr;
114
- return tr.setMeta(key, {
115
- pos: topLevelPos,
116
- dom: dom,
117
- type: topLevelNode.type.name
118
- });
119
- });
120
- }
121
- }
122
120
  }
121
+ },
122
+ view: function view(editorView) {
123
+ var dom = editorView.dom;
124
+
125
+ // Use ResizeObserver to observe height changes
126
+ var resizeObserver = new ResizeObserver((0, _rafSchd.default)(function () {
127
+ var editorHeight = dom.offsetHeight;
128
+
129
+ // Update the plugin state when the height changes
130
+ var pluginState = key.getState(editorView.state);
131
+ if (!(pluginState !== null && pluginState !== void 0 && pluginState.isDragging)) {
132
+ editorView.dispatch(editorView.state.tr.setMeta(key, {
133
+ editorHeight: editorHeight
134
+ }));
135
+ }
136
+ }));
137
+
138
+ // Start observing the editor DOM element
139
+ resizeObserver.observe(dom);
140
+ return {
141
+ destroy: function destroy() {
142
+ resizeObserver.unobserve(dom);
143
+ }
144
+ };
123
145
  }
124
146
  });
125
147
  };
@@ -12,12 +12,14 @@ var _dragHandler = _interopRequireDefault(require("@atlaskit/icon/glyph/drag-han
12
12
  var _adapter = require("@atlaskit/pragmatic-drag-and-drop/element/adapter");
13
13
  var _setCustomNativeDragPreview = require("@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview");
14
14
  var _main = require("../pm-plugins/main");
15
- var _getSelection = require("../utils/getSelection");
15
+ var _utils = require("../utils");
16
+ var _dragHandlePositions = require("../utils/drag-handle-positions");
16
17
  var _consts = require("./consts");
17
18
  var _dragPreview = require("./drag-preview");
18
19
  /** @jsx jsx */
19
20
 
20
21
  var dragHandleButtonStyles = (0, _react2.css)({
22
+ position: 'absolute',
21
23
  padding: "var(--ds-space-025, 2px)".concat(" 0"),
22
24
  boxSizing: 'border-box',
23
25
  display: 'flex',
@@ -31,10 +33,11 @@ var dragHandleButtonStyles = (0, _react2.css)({
31
33
  borderRadius: _consts.DRAG_HANDLE_BORDER_RADIUS,
32
34
  color: "var(--ds-icon, #44546F)",
33
35
  cursor: 'grab',
34
- ':hover': {
36
+ zIndex: 2,
37
+ '&:hover': {
35
38
  backgroundColor: "var(--ds-background-neutral-subtle-hovered, #091E420F)"
36
39
  },
37
- ':active': {
40
+ '&:active': {
38
41
  backgroundColor: "var(--ds-background-neutral-subtle-pressed, #091E4224)"
39
42
  }
40
43
  });
@@ -43,11 +46,13 @@ var selectedStyles = (0, _react2.css)({
43
46
  color: "var(--ds-icon-selected, #0C66E4)"
44
47
  });
45
48
  var DragHandle = exports.DragHandle = function DragHandle(_ref) {
46
- var dom = _ref.dom,
49
+ var view = _ref.view,
47
50
  api = _ref.api,
48
- start = _ref.start;
51
+ getPos = _ref.getPos,
52
+ anchorName = _ref.anchorName,
53
+ nodeType = _ref.nodeType;
54
+ var start = getPos();
49
55
  var buttonRef = (0, _react.useRef)(null);
50
- var domRef = (0, _react.useRef)(dom);
51
56
  var _useState = (0, _react.useState)(false),
52
57
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
53
58
  dragHandleSelected = _useState2[0],
@@ -55,19 +60,19 @@ var DragHandle = exports.DragHandle = function DragHandle(_ref) {
55
60
  var handleClick = (0, _react.useCallback)(function () {
56
61
  var _api$core, _api$core2;
57
62
  setDragHandleSelected(!dragHandleSelected);
58
- // TODO - add drag menu
59
- // api?.core?.actions.execute(({ tr }) =>
60
- // tr.setMeta(key, {
61
- // toggleMenu: true,
62
- // }),
63
- // );
64
63
  api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(function (_ref2) {
65
64
  var tr = _ref2.tr;
66
- tr.setSelection((0, _getSelection.getSelection)(tr, start));
65
+ if (start === undefined) {
66
+ return tr;
67
+ }
68
+ tr = (0, _utils.selectNode)(tr, start, nodeType);
69
+ tr.setMeta(_main.key, {
70
+ pos: start
71
+ });
67
72
  return tr;
68
73
  });
69
74
  api === null || api === void 0 || (_api$core2 = api.core) === null || _api$core2 === void 0 || _api$core2.actions.focus();
70
- }, [start, api, dragHandleSelected, setDragHandleSelected]);
75
+ }, [start, api, dragHandleSelected, setDragHandleSelected, nodeType]);
71
76
  (0, _react.useEffect)(function () {
72
77
  var element = buttonRef.current;
73
78
  if (!element) {
@@ -80,40 +85,57 @@ var DragHandle = exports.DragHandle = function DragHandle(_ref) {
80
85
  (0, _setCustomNativeDragPreview.setCustomNativeDragPreview)({
81
86
  render: function render(_ref4) {
82
87
  var container = _ref4.container;
83
- return (0, _dragPreview.dragPreview)(container, domRef);
88
+ var dom = view.dom.querySelector("[data-drag-handler-anchor-name=\"".concat(anchorName, "\"]"));
89
+ if (!dom) {
90
+ return;
91
+ }
92
+ return (0, _dragPreview.dragPreview)(container, dom);
84
93
  },
85
94
  nativeSetDragImage: nativeSetDragImage
86
95
  });
87
96
  },
88
97
  onDragStart: function onDragStart() {
89
- var _api$core3, _api$core4;
90
- api === null || api === void 0 || (_api$core3 = api.core) === null || _api$core3 === void 0 || _api$core3.actions.execute(function (_ref5) {
91
- var tr = _ref5.tr;
92
- var newTr = tr;
93
- newTr.setSelection((0, _getSelection.getSelection)(newTr, start));
94
- newTr.setMeta(_main.key, {
95
- isDragging: true,
96
- start: start
97
- });
98
- return newTr;
99
- });
98
+ var _api$core3, _api$blockControls, _api$core4;
99
+ if (start === undefined) {
100
+ return;
101
+ }
102
+ api === null || api === void 0 || (_api$core3 = api.core) === null || _api$core3 === void 0 || _api$core3.actions.execute(api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.commands.setNodeDragged(start, anchorName, nodeType));
100
103
  api === null || api === void 0 || (_api$core4 = api.core) === null || _api$core4 === void 0 || _api$core4.actions.focus();
101
104
  },
102
105
  onDrop: function onDrop() {
103
106
  var _api$core5;
104
- api === null || api === void 0 || (_api$core5 = api.core) === null || _api$core5 === void 0 || _api$core5.actions.execute(function (_ref6) {
105
- var tr = _ref6.tr;
107
+ api === null || api === void 0 || (_api$core5 = api.core) === null || _api$core5 === void 0 || _api$core5.actions.execute(function (_ref5) {
108
+ var tr = _ref5.tr;
106
109
  return tr.setMeta(_main.key, {
107
110
  isDragging: false
108
111
  });
109
112
  });
110
113
  }
111
114
  });
112
- }, [api, start]);
115
+ }, [api, start, view, anchorName, nodeType]);
116
+ var positionStyles = (0, _react.useMemo)(function () {
117
+ var supportsAnchor = CSS.supports('top', "anchor(".concat(anchorName, " start)")) && CSS.supports('left', "anchor(".concat(anchorName, " start)"));
118
+ var dom = view.dom.querySelector("[data-drag-handler-anchor-name=\"".concat(anchorName, "\"]"));
119
+ if (supportsAnchor) {
120
+ var hasResizer = (anchorName.includes('table') || anchorName.includes('mediaSingle')) && dom;
121
+ return {
122
+ left: hasResizer ? (0, _dragHandlePositions.getLeftPosition)(dom, nodeType) : "calc(anchor(".concat(anchorName, " start) - ").concat(_consts.DRAG_HANDLE_WIDTH, "px - ").concat((0, _consts.dragHandleGap)(nodeType), "px)"),
123
+ top: anchorName.includes('table') ? "calc(anchor(".concat(anchorName, " start) + ").concat(_consts.DRAG_HANDLE_HEIGHT, "px)") : "anchor(".concat(anchorName, " start)")
124
+ };
125
+ }
126
+ if (!dom) {
127
+ return;
128
+ }
129
+ return {
130
+ left: (0, _dragHandlePositions.getLeftPosition)(dom, nodeType),
131
+ top: (0, _dragHandlePositions.getTopPosition)(dom)
132
+ };
133
+ }, [anchorName, view, nodeType]);
113
134
  return (0, _react2.jsx)("button", {
114
135
  type: "button",
115
136
  css: [dragHandleButtonStyles, dragHandleSelected && selectedStyles],
116
137
  ref: buttonRef,
138
+ style: positionStyles,
117
139
  onClick: handleClick,
118
140
  "data-testid": "block-ctrl-drag-handle"
119
141
  }, (0, _react2.jsx)(_dragHandler.default, {
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.dragPreview = void 0;
7
- var dragPreview = exports.dragPreview = function dragPreview(container, domRef) {
8
- var rect = domRef.current.getBoundingClientRect();
7
+ var dragPreview = exports.dragPreview = function dragPreview(container, dom) {
8
+ var rect = dom.getBoundingClientRect();
9
9
  container.style.width = "".concat(rect.width, "px");
10
10
  container.style.height = "".concat(rect.height, "px");
11
11
  container.style.pointerEvents = 'none';
@@ -13,8 +13,8 @@ var dragPreview = exports.dragPreview = function dragPreview(container, domRef)
13
13
  // ProseMirror class is required to make sure the cloned dom is styled correctly
14
14
  parent.classList.add('ProseMirror');
15
15
  parent.style.opacity = '0.3';
16
- var resizer = domRef.current.querySelector('.resizer-item');
17
- var clonedDom = resizer ? resizer.cloneNode(true) : domRef.current.cloneNode(true);
16
+ var resizer = dom.querySelector('.resizer-item');
17
+ var clonedDom = resizer ? resizer.cloneNode(true) : dom.cloneNode(true);
18
18
 
19
19
  // Remove any margin from the cloned element to ensure is doesn't position incorrectly
20
20
  clonedDom.style.marginLeft = '0';
@@ -100,7 +100,7 @@ var DropTarget = exports.DropTarget = function DropTarget(_ref) {
100
100
  isDraggedOver && (0, _react2.jsx)("div", {
101
101
  css: styleDropIndicator,
102
102
  style: {
103
- 'width': "".concat(lineLength, "px")
103
+ width: "".concat(lineLength, "px")
104
104
  },
105
105
  "data-testid": "block-ctrl-drop-indicator"
106
106
  }, (0, _react2.jsx)(_box.DropIndicator, {