@atlaskit/editor-plugin-block-controls 1.1.0 → 1.3.0

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 (47) hide show
  1. package/CHANGELOG.md +23 -3
  2. package/dist/cjs/plugin.js +36 -2
  3. package/dist/cjs/pm-plugins/decorations.js +71 -0
  4. package/dist/cjs/pm-plugins/main.js +95 -4
  5. package/dist/cjs/ui/consts.js +8 -0
  6. package/dist/cjs/ui/drag-handle-menu.js +15 -0
  7. package/dist/cjs/ui/drag-handle.js +93 -0
  8. package/dist/cjs/ui/drag-preview.js +35 -0
  9. package/dist/cjs/ui/drop-target.js +72 -0
  10. package/dist/cjs/ui/global-styles.js +19 -0
  11. package/dist/es2019/plugin.js +35 -3
  12. package/dist/es2019/pm-plugins/decorations.js +64 -0
  13. package/dist/es2019/pm-plugins/main.js +97 -5
  14. package/dist/es2019/ui/consts.js +2 -0
  15. package/dist/es2019/ui/drag-handle-menu.js +10 -0
  16. package/dist/es2019/ui/drag-handle.js +86 -0
  17. package/dist/es2019/ui/drag-preview.js +27 -0
  18. package/dist/es2019/ui/drop-target.js +55 -0
  19. package/dist/es2019/ui/global-styles.js +12 -0
  20. package/dist/esm/plugin.js +36 -3
  21. package/dist/esm/pm-plugins/decorations.js +64 -0
  22. package/dist/esm/pm-plugins/main.js +95 -4
  23. package/dist/esm/ui/consts.js +2 -0
  24. package/dist/esm/ui/drag-handle-menu.js +8 -0
  25. package/dist/esm/ui/drag-handle.js +86 -0
  26. package/dist/esm/ui/drag-preview.js +29 -0
  27. package/dist/esm/ui/drop-target.js +62 -0
  28. package/dist/esm/ui/global-styles.js +12 -0
  29. package/dist/types/pm-plugins/decorations.d.ts +12 -0
  30. package/dist/types/pm-plugins/main.d.ts +2 -9
  31. package/dist/types/types.d.ts +25 -2
  32. package/dist/types/ui/consts.d.ts +2 -0
  33. package/dist/types/ui/drag-handle-menu.d.ts +6 -0
  34. package/dist/types/ui/drag-handle.d.ts +8 -0
  35. package/dist/types/ui/drag-preview.d.ts +1 -0
  36. package/dist/types/ui/drop-target.d.ts +7 -0
  37. package/dist/types/ui/global-styles.d.ts +3 -0
  38. package/dist/types-ts4.5/pm-plugins/decorations.d.ts +12 -0
  39. package/dist/types-ts4.5/pm-plugins/main.d.ts +2 -9
  40. package/dist/types-ts4.5/types.d.ts +25 -2
  41. package/dist/types-ts4.5/ui/consts.d.ts +2 -0
  42. package/dist/types-ts4.5/ui/drag-handle-menu.d.ts +6 -0
  43. package/dist/types-ts4.5/ui/drag-handle.d.ts +8 -0
  44. package/dist/types-ts4.5/ui/drag-preview.d.ts +1 -0
  45. package/dist/types-ts4.5/ui/drop-target.d.ts +7 -0
  46. package/dist/types-ts4.5/ui/global-styles.d.ts +3 -0
  47. package/package.json +7 -4
package/CHANGELOG.md CHANGED
@@ -1,17 +1,37 @@
1
1
  # @atlaskit/editor-plugin-block-controls
2
2
 
3
+ ## 1.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#95811](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/95811)
8
+ [`115361312a0e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/115361312a0e) -
9
+ move drag position data to plugin state and map positions for collab support
10
+
11
+ ## 1.2.0
12
+
13
+ ### Minor Changes
14
+
15
+ - [#93512](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/93512)
16
+ [`15155cd6eb61`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/15155cd6eb61) -
17
+ add basic dnd behaviour to block controls plugin
18
+
3
19
  ## 1.1.0
4
20
 
5
21
  ### Minor Changes
6
22
 
7
- - [#91934](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/91934) [`b76a78c6a199`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/b76a78c6a199) - bumped editor-prosemirror version to 4.0.0
23
+ - [#91934](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/91934)
24
+ [`b76a78c6a199`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/b76a78c6a199) -
25
+ bumped editor-prosemirror version to 4.0.0
8
26
 
9
27
  ### Patch Changes
10
28
 
11
- - Updated dependencies
29
+ - Updated dependencies
12
30
 
13
31
  ## 1.0.0
14
32
 
15
33
  ### Major Changes
16
34
 
17
- - [#91350](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/91350) [`f27c4b41ebed`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/f27c4b41ebed) - Adds new Editor Block Controls plugin
35
+ - [#91350](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/91350)
36
+ [`f27c4b41ebed`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/f27c4b41ebed) -
37
+ Adds new Editor Block Controls plugin
@@ -1,10 +1,14 @@
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.blockControlsPlugin = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
7
9
  var _main = require("./pm-plugins/main");
10
+ var _dragHandleMenu = require("./ui/drag-handle-menu");
11
+ var _globalStyles = require("./ui/global-styles");
8
12
  var blockControlsPlugin = exports.blockControlsPlugin = function blockControlsPlugin(_ref) {
9
13
  var api = _ref.api;
10
14
  return {
@@ -17,8 +21,38 @@ var blockControlsPlugin = exports.blockControlsPlugin = function blockControlsPl
17
21
  }
18
22
  }];
19
23
  },
20
- getSharedState: function getSharedState(_editorState) {
21
- return undefined;
24
+ commands: {
25
+ moveNode: function moveNode(start, to) {
26
+ return function (_ref2) {
27
+ var _node$nodeSize, _api$core;
28
+ var tr = _ref2.tr;
29
+ var node = tr.doc.nodeAt(start);
30
+ var size = (_node$nodeSize = node === null || node === void 0 ? void 0 : node.nodeSize) !== null && _node$nodeSize !== void 0 ? _node$nodeSize : 1;
31
+ var end = start + size;
32
+ var nodeCopy = tr.doc.content.cut(start, end); // cut the content
33
+ tr.delete(start, end); // delete the content from the original position
34
+ var mappedTo = tr.mapping.map(to);
35
+ tr.insert(mappedTo, nodeCopy); // insert the content at the new position
36
+ api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.focus();
37
+ return tr;
38
+ };
39
+ }
40
+ },
41
+ getSharedState: function getSharedState(editorState) {
42
+ var _key$getState$isMenuO, _key$getState, _key$getState$activeN, _key$getState2, _key$getState$decorat, _key$getState3;
43
+ if (!editorState) {
44
+ return undefined;
45
+ }
46
+ return {
47
+ isMenuOpen: (_key$getState$isMenuO = (_key$getState = _main.key.getState(editorState)) === null || _key$getState === void 0 ? void 0 : _key$getState.isMenuOpen) !== null && _key$getState$isMenuO !== void 0 ? _key$getState$isMenuO : false,
48
+ activeNode: (_key$getState$activeN = (_key$getState2 = _main.key.getState(editorState)) === null || _key$getState2 === void 0 ? void 0 : _key$getState2.activeNode) !== null && _key$getState$activeN !== void 0 ? _key$getState$activeN : null,
49
+ decorationState: (_key$getState$decorat = (_key$getState3 = _main.key.getState(editorState)) === null || _key$getState3 === void 0 ? void 0 : _key$getState3.decorationState) !== null && _key$getState$decorat !== void 0 ? _key$getState$decorat : []
50
+ };
51
+ },
52
+ contentComponent: function contentComponent() {
53
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_dragHandleMenu.DragHandleMenu, {
54
+ api: api
55
+ }), /*#__PURE__*/_react.default.createElement(_globalStyles.GlobalStylesWrapper, null));
22
56
  }
23
57
  };
24
58
  };
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.dropTargetDecorations = exports.dragHandleDecoration = void 0;
8
+ var _react = require("react");
9
+ var _reactDom = _interopRequireDefault(require("react-dom"));
10
+ var _view = require("@atlaskit/editor-prosemirror/view");
11
+ var _dragHandle = require("../ui/drag-handle");
12
+ var _dropTarget = require("../ui/drop-target");
13
+ var dropTargetDecorations = exports.dropTargetDecorations = function dropTargetDecorations(oldState, newState, api) {
14
+ var decs = [];
15
+ // Decoration state is used to keep track of the position of the drop targets
16
+ // and allows us to easily map the updated position in the plugin apply method.
17
+ var decorationState = [];
18
+ oldState.doc.nodesBetween(0, newState.doc.nodeSize - 2, function (_node, pos, _parent, index) {
19
+ decorationState.push({
20
+ index: index,
21
+ pos: pos
22
+ });
23
+ decs.push(_view.Decoration.widget(pos, function () {
24
+ var element = document.createElement('div');
25
+ _reactDom.default.render( /*#__PURE__*/(0, _react.createElement)(_dropTarget.DropTarget, {
26
+ api: api,
27
+ index: index
28
+ }), element);
29
+ return element;
30
+ }));
31
+ return false;
32
+ });
33
+
34
+ /**
35
+ * We are adding a drop target at the end of the document because by default we
36
+ * draw all drop targets at the top of every node. It's better to draw the drop targets
37
+ * at the top of each node because that way we only need to know the start position of the
38
+ * node and not its size.
39
+ *
40
+ */
41
+ decorationState.push({
42
+ index: decorationState.length + 1,
43
+ pos: newState.doc.nodeSize - 2
44
+ });
45
+ decs.push(_view.Decoration.widget(newState.doc.nodeSize - 2, function () {
46
+ var element = document.createElement('div');
47
+ _reactDom.default.render( /*#__PURE__*/(0, _react.createElement)(_dropTarget.DropTarget, {
48
+ api: api,
49
+ index: decorationState.length
50
+ }), element);
51
+ return element;
52
+ }));
53
+ return {
54
+ decs: decs,
55
+ decorationState: decorationState
56
+ };
57
+ };
58
+ var dragHandleDecoration = exports.dragHandleDecoration = function dragHandleDecoration(oldState,
59
+ // @ts-ignore
60
+ meta, api) {
61
+ return _view.DecorationSet.create(oldState.doc, [_view.Decoration.widget(meta.pos, function (view, getPos) {
62
+ var element = document.createElement('div');
63
+ _reactDom.default.render( /*#__PURE__*/(0, _react.createElement)(_dragHandle.DragHandle, {
64
+ dom: meta.dom,
65
+ api: api,
66
+ start: meta.pos
67
+ }), element);
68
+ element.style.position = 'absolute';
69
+ return element;
70
+ })]);
71
+ };
@@ -7,6 +7,7 @@ exports.key = exports.createPlugin = void 0;
7
7
  var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
8
8
  var _state = require("@atlaskit/editor-prosemirror/state");
9
9
  var _view = require("@atlaskit/editor-prosemirror/view");
10
+ var _decorations = require("./decorations");
10
11
  var key = exports.key = new _state.PluginKey('blockControls');
11
12
  var createPlugin = exports.createPlugin = function createPlugin(api) {
12
13
  return new _safePlugin.SafePlugin({
@@ -14,13 +15,70 @@ var createPlugin = exports.createPlugin = function createPlugin(api) {
14
15
  state: {
15
16
  init: function init() {
16
17
  return {
17
- decorations: _view.DecorationSet.empty
18
+ decorations: _view.DecorationSet.empty,
19
+ decorationState: [],
20
+ activeNode: null,
21
+ isDragging: false,
22
+ isMenuOpen: false,
23
+ start: null,
24
+ end: null
18
25
  };
19
26
  },
20
- apply: function apply(tr, currentState) {
21
- var decorations = currentState.decorations;
27
+ apply: function apply(tr, currentState, oldState, newState) {
28
+ var _decorationState, _meta$pos, _meta$isDragging;
29
+ // return currentState;
30
+ var activeNode = currentState.activeNode,
31
+ decorations = currentState.decorations,
32
+ isMenuOpen = currentState.isMenuOpen,
33
+ decorationState = currentState.decorationState;
34
+ var meta = tr.getMeta(key);
35
+
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) {
42
+ decorations = _view.DecorationSet.create(newState.doc, []);
43
+ var _dropTargetDecoration = (0, _decorations.dropTargetDecorations)(oldState, newState, api),
44
+ decs = _dropTargetDecoration.decs,
45
+ updatedDecorationState = _dropTargetDecoration.decorationState;
46
+ decorationState = updatedDecorationState;
47
+ decorations = decorations.add(newState.doc, decs);
48
+ }
49
+
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);
58
+ }
59
+
60
+ // Map drop target decoration positions when the document changes
61
+ if (tr.docChanged && currentState.isDragging) {
62
+ decorationState = decorationState.map(function (_ref) {
63
+ var index = _ref.index,
64
+ pos = _ref.pos;
65
+ return {
66
+ index: index,
67
+ pos: tr.mapping.map(pos)
68
+ };
69
+ });
70
+ }
71
+
72
+ // 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;
22
74
  return {
23
- decorations: decorations
75
+ decorations: decorations,
76
+ 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
+ },
80
+ 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
24
82
  };
25
83
  }
26
84
  },
@@ -28,6 +86,39 @@ var createPlugin = exports.createPlugin = function createPlugin(api) {
28
86
  decorations: function decorations(state) {
29
87
  var _key$getState;
30
88
  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 && pos.inside && 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
+ }
31
122
  }
32
123
  }
33
124
  });
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DRAG_HANDLE_WIDTH = exports.DRAG_HANDLE_HEIGHT = void 0;
7
+ var DRAG_HANDLE_HEIGHT = exports.DRAG_HANDLE_HEIGHT = 48;
8
+ var DRAG_HANDLE_WIDTH = exports.DRAG_HANDLE_WIDTH = 24;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.DragHandleMenu = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _hooks = require("@atlaskit/editor-common/hooks");
10
+ var DragHandleMenu = exports.DragHandleMenu = function DragHandleMenu(_ref) {
11
+ var api = _ref.api;
12
+ var _useSharedPluginState = (0, _hooks.useSharedPluginState)(api, ['blockControls']),
13
+ blockControlsState = _useSharedPluginState.blockControlsState;
14
+ return blockControlsState !== null && blockControlsState !== void 0 && blockControlsState.isMenuOpen ? /*#__PURE__*/_react.default.createElement("div", null, "menu") : null;
15
+ };
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.DragHandle = void 0;
7
+ var _react = require("react");
8
+ var _react2 = require("@emotion/react");
9
+ var _adapter = require("@atlaskit/pragmatic-drag-and-drop/element/adapter");
10
+ var _setCustomNativeDragPreview = require("@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview");
11
+ var _main = require("../pm-plugins/main");
12
+ var _consts = require("./consts");
13
+ var _dragPreview = require("./drag-preview");
14
+ /** @jsx jsx */
15
+
16
+ var styles = (0, _react2.css)({
17
+ position: 'absolute',
18
+ // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage
19
+ background: 'grey',
20
+ zIndex: 1,
21
+ height: _consts.DRAG_HANDLE_HEIGHT,
22
+ width: _consts.DRAG_HANDLE_WIDTH,
23
+ left: -_consts.DRAG_HANDLE_WIDTH
24
+ });
25
+ var DragHandle = exports.DragHandle = function DragHandle(_ref) {
26
+ var dom = _ref.dom,
27
+ api = _ref.api,
28
+ start = _ref.start;
29
+ var buttonRef = (0, _react.useRef)(null);
30
+ var domRef = (0, _react.useRef)(dom);
31
+ (0, _react.useEffect)(function () {
32
+ var element = buttonRef.current;
33
+ if (!element) {
34
+ return;
35
+ }
36
+ return (0, _adapter.draggable)({
37
+ element: element,
38
+ onGenerateDragPreview: function onGenerateDragPreview(_ref2) {
39
+ var nativeSetDragImage = _ref2.nativeSetDragImage;
40
+ (0, _setCustomNativeDragPreview.setCustomNativeDragPreview)({
41
+ getOffset: function getOffset() {
42
+ var rect = domRef.current.getBoundingClientRect();
43
+ // Offset the drag preview to the center of the element
44
+ return {
45
+ x: 0,
46
+ y: rect.height / 2
47
+ };
48
+ },
49
+ render: function render(_ref3) {
50
+ var container = _ref3.container;
51
+ return (0, _dragPreview.dragPreview)(container, domRef);
52
+ },
53
+ nativeSetDragImage: nativeSetDragImage
54
+ });
55
+ },
56
+ onDragStart: function onDragStart() {
57
+ var _api$core;
58
+ api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(function (_ref4) {
59
+ var tr = _ref4.tr;
60
+ return tr.setMeta(_main.key, {
61
+ isDragging: true,
62
+ start: start
63
+ });
64
+ });
65
+ },
66
+ onDrop: function onDrop() {
67
+ var _api$core2;
68
+ api === null || api === void 0 || (_api$core2 = api.core) === null || _api$core2 === void 0 || _api$core2.actions.execute(function (_ref5) {
69
+ var tr = _ref5.tr;
70
+ return tr.setMeta(_main.key, {
71
+ isDragging: false
72
+ });
73
+ });
74
+ }
75
+ });
76
+ }, [api, start]);
77
+ return (0, _react2.jsx)("div", {
78
+ css: styles,
79
+ style: {
80
+ top: dom.clientHeight / 2 - _consts.DRAG_HANDLE_HEIGHT / 2
81
+ },
82
+ ref: buttonRef,
83
+ onClick: function onClick() {
84
+ var _api$core3;
85
+ api === null || api === void 0 || (_api$core3 = api.core) === null || _api$core3 === void 0 || _api$core3.actions.execute(function (_ref6) {
86
+ var tr = _ref6.tr;
87
+ return tr.setMeta(_main.key, {
88
+ toggleMenu: true
89
+ });
90
+ });
91
+ }
92
+ });
93
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.dragPreview = void 0;
7
+ var dragPreview = exports.dragPreview = function dragPreview(container, domRef) {
8
+ var rect = domRef.current.getBoundingClientRect();
9
+ container.style.width = "".concat(rect.width, "px");
10
+ container.style.height = "".concat(rect.height, "px");
11
+ container.style.pointerEvents = 'none';
12
+ var parent = document.createElement('div');
13
+ // ProseMirror class is required to make sure the cloned dom is styled correctly
14
+ parent.classList.add('ProseMirror');
15
+ var clonedDom = domRef.current.cloneNode(true);
16
+
17
+ // Remove any margin from the cloned element to ensure is doesn't position incorrectly
18
+ clonedDom.style.marginLeft = '0';
19
+ clonedDom.style.marginTop = '0';
20
+ clonedDom.style.marginRight = '0';
21
+ clonedDom.style.marginBottom = '0';
22
+ parent.appendChild(clonedDom);
23
+ container.appendChild(parent);
24
+ var scrollParent = document.querySelector('.fabric-editor-popup-scroll-parent');
25
+ var scrollParentClassNames = scrollParent === null || scrollParent === void 0 ? void 0 : scrollParent.className;
26
+
27
+ // Add the scroll parent class to the container to ensure the cloned element is styled correctly
28
+ container.className = scrollParentClassNames || '';
29
+ container.classList.remove('fabric-editor-popup-scroll-parent');
30
+ // Prevents a scrollbar from showing
31
+ container.style.overflow = 'visible';
32
+ return function () {
33
+ return container;
34
+ };
35
+ };
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.DropTarget = void 0;
9
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
+ var _react = _interopRequireWildcard(require("react"));
11
+ var _adapter = require("@atlaskit/pragmatic-drag-and-drop/element/adapter");
12
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
13
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
14
+ var DropTarget = exports.DropTarget = function DropTarget(_ref) {
15
+ var api = _ref.api,
16
+ index = _ref.index;
17
+ var ref = (0, _react.useRef)(null);
18
+ var _useState = (0, _react.useState)(false),
19
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
20
+ isDraggedOver = _useState2[0],
21
+ setIsDraggedOver = _useState2[1];
22
+ (0, _react.useEffect)(function () {
23
+ var element = ref.current;
24
+ if (!element) {
25
+ return;
26
+ }
27
+ return (0, _adapter.dropTargetForElements)({
28
+ element: element,
29
+ getIsSticky: function getIsSticky() {
30
+ return true;
31
+ },
32
+ onDragEnter: function onDragEnter() {
33
+ return setIsDraggedOver(true);
34
+ },
35
+ onDragLeave: function onDragLeave() {
36
+ return setIsDraggedOver(false);
37
+ },
38
+ onDrop: function onDrop() {
39
+ var _api$blockControls;
40
+ var _ref2 = (api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.sharedState.currentState()) || {},
41
+ activeNode = _ref2.activeNode,
42
+ decorationState = _ref2.decorationState;
43
+ if (!activeNode || !decorationState) {
44
+ return;
45
+ }
46
+ var _ref3 = decorationState.find(function (dec) {
47
+ return dec.index === index;
48
+ }) || {},
49
+ pos = _ref3.pos;
50
+ if (activeNode && pos !== undefined) {
51
+ var _api$core, _api$blockControls2;
52
+ var start = activeNode.pos;
53
+ api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(api === null || api === void 0 || (_api$blockControls2 = api.blockControls) === null || _api$blockControls2 === void 0 || (_api$blockControls2 = _api$blockControls2.commands) === null || _api$blockControls2 === void 0 ? void 0 : _api$blockControls2.moveNode(start, pos));
54
+ }
55
+ }
56
+ });
57
+ }, [index, api]);
58
+ return (
59
+ /*#__PURE__*/
60
+ // Note: Firefox has trouble with using a button element as the handle for drag and drop
61
+ _react.default.createElement("div", {
62
+ style: {
63
+ height: '20px',
64
+ marginTop: '-22px',
65
+ top: '10px',
66
+ position: 'relative',
67
+ borderBottom: "solid ".concat(isDraggedOver ? 'blue' : 'transparent', " 2px")
68
+ },
69
+ ref: ref
70
+ })
71
+ );
72
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.GlobalStylesWrapper = void 0;
7
+ var _react = require("@emotion/react");
8
+ /** @jsx jsx */
9
+
10
+ var globalStyles = (0, _react.css)({
11
+ '.ProseMirror-widget:first-child + *': {
12
+ 'margin-top': '0 !important'
13
+ }
14
+ });
15
+ var GlobalStylesWrapper = exports.GlobalStylesWrapper = function GlobalStylesWrapper() {
16
+ return (0, _react.jsx)(_react.Global, {
17
+ styles: globalStyles
18
+ });
19
+ };
@@ -1,4 +1,7 @@
1
- import { createPlugin } from './pm-plugins/main';
1
+ import React from 'react';
2
+ import { createPlugin, key } from './pm-plugins/main';
3
+ import { DragHandleMenu } from './ui/drag-handle-menu';
4
+ import { GlobalStylesWrapper } from './ui/global-styles';
2
5
  export const blockControlsPlugin = ({
3
6
  api
4
7
  }) => ({
@@ -9,7 +12,36 @@ export const blockControlsPlugin = ({
9
12
  plugin: () => createPlugin(api)
10
13
  }];
11
14
  },
12
- getSharedState(_editorState) {
13
- return undefined;
15
+ commands: {
16
+ moveNode: (start, to) => ({
17
+ tr
18
+ }) => {
19
+ var _node$nodeSize, _api$core;
20
+ const node = tr.doc.nodeAt(start);
21
+ const size = (_node$nodeSize = node === null || node === void 0 ? void 0 : node.nodeSize) !== null && _node$nodeSize !== void 0 ? _node$nodeSize : 1;
22
+ const end = start + size;
23
+ let nodeCopy = tr.doc.content.cut(start, end); // cut the content
24
+ tr.delete(start, end); // delete the content from the original position
25
+ const mappedTo = tr.mapping.map(to);
26
+ tr.insert(mappedTo, nodeCopy); // insert the content at the new position
27
+ api === null || api === void 0 ? void 0 : (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.focus();
28
+ return tr;
29
+ }
30
+ },
31
+ getSharedState(editorState) {
32
+ var _key$getState$isMenuO, _key$getState, _key$getState$activeN, _key$getState2, _key$getState$decorat, _key$getState3;
33
+ if (!editorState) {
34
+ return undefined;
35
+ }
36
+ return {
37
+ isMenuOpen: (_key$getState$isMenuO = (_key$getState = key.getState(editorState)) === null || _key$getState === void 0 ? void 0 : _key$getState.isMenuOpen) !== null && _key$getState$isMenuO !== void 0 ? _key$getState$isMenuO : false,
38
+ activeNode: (_key$getState$activeN = (_key$getState2 = key.getState(editorState)) === null || _key$getState2 === void 0 ? void 0 : _key$getState2.activeNode) !== null && _key$getState$activeN !== void 0 ? _key$getState$activeN : null,
39
+ decorationState: (_key$getState$decorat = (_key$getState3 = key.getState(editorState)) === null || _key$getState3 === void 0 ? void 0 : _key$getState3.decorationState) !== null && _key$getState$decorat !== void 0 ? _key$getState$decorat : []
40
+ };
41
+ },
42
+ contentComponent() {
43
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(DragHandleMenu, {
44
+ api: api
45
+ }), /*#__PURE__*/React.createElement(GlobalStylesWrapper, null));
14
46
  }
15
47
  });
@@ -0,0 +1,64 @@
1
+ import { createElement } from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
4
+ import { DragHandle } from '../ui/drag-handle';
5
+ import { DropTarget } from '../ui/drop-target';
6
+ export const dropTargetDecorations = (oldState, newState, api) => {
7
+ const decs = [];
8
+ // Decoration state is used to keep track of the position of the drop targets
9
+ // and allows us to easily map the updated position in the plugin apply method.
10
+ const decorationState = [];
11
+ oldState.doc.nodesBetween(0, newState.doc.nodeSize - 2, (_node, pos, _parent, index) => {
12
+ decorationState.push({
13
+ index,
14
+ pos
15
+ });
16
+ decs.push(Decoration.widget(pos, () => {
17
+ const element = document.createElement('div');
18
+ ReactDOM.render( /*#__PURE__*/createElement(DropTarget, {
19
+ api,
20
+ index
21
+ }), element);
22
+ return element;
23
+ }));
24
+ return false;
25
+ });
26
+
27
+ /**
28
+ * We are adding a drop target at the end of the document because by default we
29
+ * draw all drop targets at the top of every node. It's better to draw the drop targets
30
+ * at the top of each node because that way we only need to know the start position of the
31
+ * node and not its size.
32
+ *
33
+ */
34
+ decorationState.push({
35
+ index: decorationState.length + 1,
36
+ pos: newState.doc.nodeSize - 2
37
+ });
38
+ decs.push(Decoration.widget(newState.doc.nodeSize - 2, () => {
39
+ const element = document.createElement('div');
40
+ ReactDOM.render( /*#__PURE__*/createElement(DropTarget, {
41
+ api,
42
+ index: decorationState.length
43
+ }), element);
44
+ return element;
45
+ }));
46
+ return {
47
+ decs,
48
+ decorationState
49
+ };
50
+ };
51
+ export const dragHandleDecoration = (oldState,
52
+ // @ts-ignore
53
+ meta, api) => {
54
+ return DecorationSet.create(oldState.doc, [Decoration.widget(meta.pos, (view, getPos) => {
55
+ const element = document.createElement('div');
56
+ ReactDOM.render( /*#__PURE__*/createElement(DragHandle, {
57
+ dom: meta.dom,
58
+ api,
59
+ start: meta.pos
60
+ }), element);
61
+ element.style.position = 'absolute';
62
+ return element;
63
+ })]);
64
+ };