@atlaskit/editor-plugin-table 5.1.0 → 5.2.1

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 (81) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/plugins/table/index.js +3 -2
  3. package/dist/cjs/plugins/table/nodeviews/TableCell.js +111 -0
  4. package/dist/cjs/plugins/table/nodeviews/TableNodeViewBase.js +30 -0
  5. package/dist/cjs/plugins/table/{pm-plugins/sticky-headers/nodeviews/tableRow.js → nodeviews/TableRow.js} +363 -303
  6. package/dist/cjs/plugins/table/pm-plugins/main.js +16 -13
  7. package/dist/cjs/plugins/table/pm-plugins/sticky-headers/index.js +1 -8
  8. package/dist/cjs/plugins/table/pm-plugins/sticky-headers/plugin.js +1 -9
  9. package/dist/cjs/plugins/table/types.js +4 -1
  10. package/dist/cjs/plugins/table/utils/dom.js +31 -1
  11. package/dist/cjs/plugins/table/utils/index.js +12 -0
  12. package/dist/cjs/plugins/table/utils/nodes.js +31 -7
  13. package/dist/es2019/plugins/table/index.js +3 -2
  14. package/dist/es2019/plugins/table/nodeviews/{tableCell.js → TableCell.js} +28 -24
  15. package/dist/es2019/plugins/table/nodeviews/TableNodeViewBase.js +22 -0
  16. package/dist/es2019/plugins/table/{pm-plugins/sticky-headers/nodeviews/tableRow.js → nodeviews/TableRow.js} +324 -280
  17. package/dist/es2019/plugins/table/pm-plugins/main.js +8 -8
  18. package/dist/es2019/plugins/table/pm-plugins/sticky-headers/index.js +1 -2
  19. package/dist/es2019/plugins/table/pm-plugins/sticky-headers/plugin.js +1 -9
  20. package/dist/es2019/plugins/table/types.js +5 -1
  21. package/dist/es2019/plugins/table/utils/dom.js +30 -0
  22. package/dist/es2019/plugins/table/utils/index.js +1 -1
  23. package/dist/es2019/plugins/table/utils/nodes.js +16 -0
  24. package/dist/esm/plugins/table/index.js +3 -2
  25. package/dist/esm/plugins/table/nodeviews/TableCell.js +105 -0
  26. package/dist/esm/plugins/table/nodeviews/TableNodeViewBase.js +24 -0
  27. package/dist/esm/plugins/table/{pm-plugins/sticky-headers/nodeviews/tableRow.js → nodeviews/TableRow.js} +364 -303
  28. package/dist/esm/plugins/table/pm-plugins/main.js +16 -13
  29. package/dist/esm/plugins/table/pm-plugins/sticky-headers/index.js +1 -2
  30. package/dist/esm/plugins/table/pm-plugins/sticky-headers/plugin.js +1 -9
  31. package/dist/esm/plugins/table/types.js +5 -1
  32. package/dist/esm/plugins/table/utils/dom.js +30 -0
  33. package/dist/esm/plugins/table/utils/index.js +1 -1
  34. package/dist/esm/plugins/table/utils/nodes.js +24 -0
  35. package/dist/types/plugins/table/nodeviews/TableCell.d.ts +13 -0
  36. package/dist/types/plugins/table/nodeviews/TableNodeViewBase.d.ts +18 -0
  37. package/dist/types/plugins/table/nodeviews/TableRow.d.ts +62 -0
  38. package/dist/types/plugins/table/pm-plugins/main.d.ts +1 -1
  39. package/dist/types/plugins/table/pm-plugins/sticky-headers/index.d.ts +0 -1
  40. package/dist/types/plugins/table/pm-plugins/sticky-headers/plugin.d.ts +1 -1
  41. package/dist/types/plugins/table/types.d.ts +15 -0
  42. package/dist/types/plugins/table/utils/dom.d.ts +6 -0
  43. package/dist/types/plugins/table/utils/index.d.ts +1 -1
  44. package/dist/types/plugins/table/utils/nodes.d.ts +12 -2
  45. package/dist/types-ts4.5/plugins/table/nodeviews/TableCell.d.ts +13 -0
  46. package/dist/types-ts4.5/plugins/table/nodeviews/TableNodeViewBase.d.ts +18 -0
  47. package/dist/types-ts4.5/plugins/table/nodeviews/TableRow.d.ts +62 -0
  48. package/dist/types-ts4.5/plugins/table/pm-plugins/main.d.ts +1 -1
  49. package/dist/types-ts4.5/plugins/table/pm-plugins/sticky-headers/index.d.ts +0 -1
  50. package/dist/types-ts4.5/plugins/table/pm-plugins/sticky-headers/plugin.d.ts +1 -1
  51. package/dist/types-ts4.5/plugins/table/types.d.ts +15 -0
  52. package/dist/types-ts4.5/plugins/table/utils/dom.d.ts +6 -0
  53. package/dist/types-ts4.5/plugins/table/utils/index.d.ts +1 -1
  54. package/dist/types-ts4.5/plugins/table/utils/nodes.d.ts +12 -2
  55. package/package.json +4 -2
  56. package/src/__tests__/unit/nodeviews/cell.ts +2 -2
  57. package/src/__tests__/unit/pm-plugins/sticky-headers/tableRow.tsx +25 -148
  58. package/src/plugins/table/index.tsx +2 -0
  59. package/src/plugins/table/nodeviews/{tableCell.tsx → TableCell.ts} +41 -46
  60. package/src/plugins/table/nodeviews/TableNodeViewBase.ts +32 -0
  61. package/src/plugins/table/{pm-plugins/sticky-headers/nodeviews/tableRow.ts → nodeviews/TableRow.ts} +246 -246
  62. package/src/plugins/table/pm-plugins/main.ts +10 -19
  63. package/src/plugins/table/pm-plugins/sticky-headers/index.ts +0 -1
  64. package/src/plugins/table/pm-plugins/sticky-headers/plugin.ts +1 -9
  65. package/src/plugins/table/types.ts +18 -0
  66. package/src/plugins/table/utils/dom.ts +38 -0
  67. package/src/plugins/table/utils/index.ts +2 -0
  68. package/src/plugins/table/utils/nodes.ts +30 -2
  69. package/tsconfig.app.json +6 -0
  70. package/dist/cjs/plugins/table/nodeviews/tableCell.js +0 -99
  71. package/dist/cjs/plugins/table/pm-plugins/sticky-headers/nodeviews/dom.js +0 -35
  72. package/dist/es2019/plugins/table/pm-plugins/sticky-headers/nodeviews/dom.js +0 -29
  73. package/dist/esm/plugins/table/nodeviews/tableCell.js +0 -93
  74. package/dist/esm/plugins/table/pm-plugins/sticky-headers/nodeviews/dom.js +0 -29
  75. package/dist/types/plugins/table/nodeviews/tableCell.d.ts +0 -17
  76. package/dist/types/plugins/table/pm-plugins/sticky-headers/nodeviews/dom.d.ts +0 -6
  77. package/dist/types/plugins/table/pm-plugins/sticky-headers/nodeviews/tableRow.d.ts +0 -73
  78. package/dist/types-ts4.5/plugins/table/nodeviews/tableCell.d.ts +0 -17
  79. package/dist/types-ts4.5/plugins/table/pm-plugins/sticky-headers/nodeviews/dom.d.ts +0 -6
  80. package/dist/types-ts4.5/plugins/table/pm-plugins/sticky-headers/nodeviews/tableRow.d.ts +0 -73
  81. package/src/plugins/table/pm-plugins/sticky-headers/nodeviews/dom.ts +0 -37
@@ -4,288 +4,210 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.supportedHeaderRow = exports.TableRowNodeView = void 0;
7
+ exports.default = void 0;
8
8
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
9
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
11
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
12
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
13
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
10
14
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
15
  var _debounce = _interopRequireDefault(require("lodash/debounce"));
12
16
  var _throttle = _interopRequireDefault(require("lodash/throttle"));
13
17
  var _ui = require("@atlaskit/editor-common/ui");
14
18
  var _utils = require("@atlaskit/editor-common/utils");
15
- var _types = require("../../../types");
16
- var _consts = require("../../../ui/consts");
17
- var _pluginKey = require("../../plugin-key");
18
- var _dom = require("../../table-resizing/utils/dom");
19
- var _commands = require("../commands");
20
- var _dom2 = require("./dom");
19
+ var _closestEdge = require("@atlaskit/pragmatic-drag-and-drop-hitbox/addon/closest-edge");
20
+ var _element = require("@atlaskit/pragmatic-drag-and-drop/adapter/element");
21
+ var _pluginFactory = require("../pm-plugins/plugin-factory");
22
+ var _pluginKey = require("../pm-plugins/plugin-key");
23
+ var _commands = require("../pm-plugins/sticky-headers/commands");
24
+ var _dom = require("../pm-plugins/table-resizing/utils/dom");
25
+ var _types = require("../types");
26
+ var _consts = require("../ui/consts");
27
+ var _dom2 = require("../utils/dom");
28
+ var _nodes = require("../utils/nodes");
29
+ var _TableNodeViewBase = _interopRequireDefault(require("./TableNodeViewBase"));
30
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
31
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
21
32
  // limit scroll event calls
22
33
  var HEADER_ROW_SCROLL_THROTTLE_TIMEOUT = 200;
23
34
 
24
35
  // timeout for resetting the scroll class - if it’s too long then users won’t be able to click on the header cells,
25
36
  // if too short it would trigger too many dom updates.
26
37
  var HEADER_ROW_SCROLL_RESET_DEBOUNCE_TIMEOUT = 400;
27
- var anyChildCellMergedAcrossRow = function anyChildCellMergedAcrossRow(node) {
28
- return (0, _utils.mapChildren)(node, function (child) {
29
- return child.attrs.rowspan || 0;
30
- }).some(function (rowspan) {
31
- return rowspan > 1;
32
- });
33
- };
34
-
35
- /**
36
- * Check if a given node is a header row with this definition:
37
- * - all children are tableHeader cells
38
- * - no table cells have been have merged with other table row cells
39
- *
40
- * @param node ProseMirror node
41
- * @return boolean if it meets definition
42
- */
43
- var supportedHeaderRow = exports.supportedHeaderRow = function supportedHeaderRow(node) {
44
- var allHeaders = (0, _utils.mapChildren)(node, function (child) {
45
- return child.type.name === 'tableHeader';
46
- }).every(Boolean);
47
- var someMerged = anyChildCellMergedAcrossRow(node);
48
- return allHeaders && !someMerged;
49
- };
50
- var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
51
- function TableRowNodeView(node, view, getPos, eventDispatcher) {
52
- var _this = this;
53
- (0, _classCallCheck2.default)(this, TableRowNodeView);
54
- // this is the sticky header table row
55
- (0, _defineProperty2.default)(this, "colControlsOffset", 0);
56
- (0, _defineProperty2.default)(this, "focused", false);
57
- (0, _defineProperty2.default)(this, "topPosEditorElement", 0);
58
- (0, _defineProperty2.default)(this, "sentinels", {});
59
- /* external events */
60
- (0, _defineProperty2.default)(this, "listening", false);
61
- (0, _defineProperty2.default)(this, "headerRowMouseScrollEnd", (0, _debounce.default)(function () {
38
+ var TableRow = exports.default = /*#__PURE__*/function (_ref) {
39
+ (0, _inherits2.default)(TableRow, _ref);
40
+ var _super = _createSuper(TableRow);
41
+ function TableRow(node, view, getPos, eventDispatcher) {
42
+ var _this;
43
+ (0, _classCallCheck2.default)(this, TableRow);
44
+ _this = _super.call(this, node, view, getPos, eventDispatcher);
45
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "colControlsOffset", 0);
46
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "focused", false);
47
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "topPosEditorElement", 0);
48
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "sentinels", {});
49
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "listening", false);
50
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "padding", 0);
51
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "top", 0);
52
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "headerRowMouseScrollEnd", (0, _debounce.default)(function () {
62
53
  _this.dom.classList.remove('no-pointer-events');
63
54
  }, HEADER_ROW_SCROLL_RESET_DEBOUNCE_TIMEOUT));
64
55
  // When the header is sticky, the header row is set to position: fixed
65
56
  // This prevents mouse wheel scrolling on the scroll-parent div when user's mouse is hovering the header row.
66
57
  // This fix sets pointer-events: none on the header row briefly to avoid this behaviour
67
- (0, _defineProperty2.default)(this, "headerRowMouseScroll", (0, _throttle.default)(function () {
58
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "headerRowMouseScroll", (0, _throttle.default)(function () {
68
59
  if (_this.isSticky) {
69
60
  _this.dom.classList.add('no-pointer-events');
70
61
  _this.headerRowMouseScrollEnd();
71
62
  }
72
63
  }, HEADER_ROW_SCROLL_THROTTLE_TIMEOUT));
73
- /* receive external events */
74
- (0, _defineProperty2.default)(this, "onTablePluginState", function (state) {
75
- var tableRef = state.tableRef;
76
- var tree = _this.tree;
77
- if (!tree) {
78
- return;
64
+ _this.isHeaderRow = (0, _nodes.supportedHeaderRow)(node);
65
+ _this.isSticky = false;
66
+ var _getPluginState = (0, _pluginFactory.getPluginState)(view.state),
67
+ pluginConfig = _getPluginState.pluginConfig,
68
+ isDragAndDropEnabled = _getPluginState.isDragAndDropEnabled;
69
+ _this.isStickyHeaderEnabled = !!pluginConfig.stickyHeaders;
70
+ _this.isDragAndDropEnabled = !!isDragAndDropEnabled;
71
+ if (_this.isHeaderRow) {
72
+ _this.dom.setAttribute('data-header-row', 'true');
73
+ if (_this.isStickyHeaderEnabled) {
74
+ _this.subscribe();
79
75
  }
76
+ }
77
+ if (_this.isDragAndDropEnabled) {
78
+ _this.addDropTarget(_this.contentDOM);
79
+ }
80
+ return _this;
81
+ }
80
82
 
81
- // when header rows are toggled off - mark sentinels as unobserved
82
- if (!state.isHeaderRowEnabled) {
83
- [_this.sentinels.top, _this.sentinels.bottom].forEach(function (el) {
84
- if (el) {
85
- delete el.dataset.isObserved;
86
- }
87
- });
88
- }
89
- var isCurrentTableSelected = tableRef === tree.table;
83
+ /**
84
+ * Variables
85
+ */
90
86
 
91
- // If current table selected and header row is toggled off, turn off sticky header
92
- if (isCurrentTableSelected && !state.isHeaderRowEnabled && _this.tree) {
93
- _this.makeRowHeaderNotSticky(_this.tree.table);
94
- }
95
- _this.focused = isCurrentTableSelected;
96
- var wrapper = tree.wrapper;
97
- var tableContainer = wrapper.parentElement;
98
- var tableContentWrapper = tableContainer.parentElement;
99
- var layoutContainer = tableContentWrapper && tableContentWrapper.parentElement;
100
- if (isCurrentTableSelected) {
101
- _this.colControlsOffset = _consts.tableControlsSpacing;
102
- if (layoutContainer && layoutContainer.getAttribute('data-layout-content')) {
103
- // move table a little out of the way
104
- // to provide spacing for table controls
105
- tableContentWrapper.style.paddingLeft = '11px';
106
- }
107
- } else {
108
- _this.colControlsOffset = 0;
109
- if (layoutContainer && layoutContainer.getAttribute('data-layout-content')) {
110
- tableContentWrapper.style.removeProperty('padding-left');
111
- }
87
+ // @ts-ignore
88
+ (0, _createClass2.default)(TableRow, [{
89
+ key: "update",
90
+ value:
91
+ /**
92
+ * Methods: Nodeview Lifecycle
93
+ */
94
+ function update(node) {
95
+ // do nothing if nodes were identical
96
+ if (node === this.node) {
97
+ return true;
112
98
  }
113
99
 
114
- // run after table style changes have been committed
115
- setTimeout(function () {
116
- (0, _dom.syncStickyRowToTable)(tree.table);
117
- }, 0);
118
- });
119
- (0, _defineProperty2.default)(this, "updateStickyHeaderWidth", function () {
120
- // table width might have changed, sync that back to sticky row
121
- var tree = _this.tree;
122
- if (!tree) {
123
- return;
100
+ // see if we're changing into a header row or
101
+ // changing away from one
102
+ var newNodeIsHeaderRow = (0, _nodes.supportedHeaderRow)(node);
103
+ if (this.isHeaderRow !== newNodeIsHeaderRow) {
104
+ return false; // re-create nodeview
124
105
  }
125
- (0, _dom.syncStickyRowToTable)(tree.table);
126
- });
127
- (0, _defineProperty2.default)(this, "shouldHeaderStick", function (tree) {
128
- var wrapper = tree.wrapper;
129
- var tableWrapperRect = wrapper.getBoundingClientRect();
130
- var editorAreaRect = _this.editorScrollableElement.getBoundingClientRect();
131
- var stickyHeaderRect = _this.contentDOM.getBoundingClientRect();
132
- var firstHeaderRow = !_this.dom.previousElementSibling;
133
- var subsequentRows = !!_this.dom.nextElementSibling;
134
- var isHeaderValid = firstHeaderRow && subsequentRows;
135
106
 
136
- // if the table wrapper is less than the editor top pos then make it sticky
137
- // Make header sticky if table wrapper top is outside viewport
138
- // but bottom is still in the viewport.
139
- if (tableWrapperRect.top < editorAreaRect.top && tableWrapperRect.bottom > editorAreaRect.top && isHeaderValid) {
140
- return true;
141
- }
107
+ // node is different but no need to re-create nodeview
108
+ this.node = node;
142
109
 
143
- // if the sticky header is below the editor area make it non-sticky
144
- if (stickyHeaderRect.top > editorAreaRect.top) {
145
- return false;
110
+ // don't do anything if we're just a regular tr
111
+ if (!this.isHeaderRow) {
112
+ return true;
146
113
  }
147
114
 
148
- // otherwise make it non-sticky
149
- return false;
150
- });
151
- /**
152
- * Manually refire the intersection observers.
153
- * Useful when the header may have detached from the table.
154
- */
155
- (0, _defineProperty2.default)(this, "refireIntersectionObservers", function () {
156
- if (_this.isSticky) {
157
- [_this.sentinels.top, _this.sentinels.bottom].forEach(function (el) {
158
- if (el && _this.intersectionObserver) {
159
- _this.intersectionObserver.unobserve(el);
160
- _this.intersectionObserver.observe(el);
161
- }
162
- });
115
+ // something changed, sync widths
116
+ if (this.isStickyHeaderEnabled) {
117
+ var tbody = this.dom.parentElement;
118
+ var table = tbody && tbody.parentElement;
119
+ (0, _dom.syncStickyRowToTable)(table);
163
120
  }
164
- });
165
- (0, _defineProperty2.default)(this, "makeHeaderRowSticky", function (tree, scrollTop) {
166
- var _tbody$firstChild;
167
- // If header row height is more than 50% of viewport height don't do this
168
- if (_this.isSticky || _this.stickyRowHeight && _this.stickyRowHeight > window.innerHeight / 2) {
169
- return;
121
+ return true;
122
+ }
123
+ }, {
124
+ key: "destroy",
125
+ value: function destroy() {
126
+ var _this$dropTargetClean;
127
+ if (this.isStickyHeaderEnabled) {
128
+ this.unsubscribe();
129
+ var tree = (0, _dom2.getTree)(this.dom);
130
+ if (tree) {
131
+ this.makeRowHeaderNotSticky(tree.table, true);
132
+ }
133
+ this.emitOff(true);
170
134
  }
171
- var table = tree.table,
172
- wrapper = tree.wrapper;
173
135
 
174
- // ED-16035 Make sure sticky header is only applied to first row
175
- var tbody = _this.dom.parentElement;
176
- var isFirstHeader = tbody === null || tbody === void 0 || (_tbody$firstChild = tbody.firstChild) === null || _tbody$firstChild === void 0 ? void 0 : _tbody$firstChild.isEqualNode(_this.dom);
177
- if (!isFirstHeader) {
178
- return;
179
- }
180
- var currentTableTop = _this.getCurrentTableTop(tree);
181
- if (!scrollTop) {
182
- scrollTop = (0, _dom2.getTop)(_this.editorScrollableElement);
136
+ // If a drop target cleanup method has been set then we should call it.
137
+ (_this$dropTargetClean = this.dropTargetCleanup) === null || _this$dropTargetClean === void 0 || _this$dropTargetClean.call(this);
138
+ }
139
+ }, {
140
+ key: "ignoreMutation",
141
+ value: function ignoreMutation(mutationRecord) {
142
+ /* tableRows are not directly editable by the user
143
+ * so it should be safe to ignore mutations that we cause
144
+ * by updating styles and classnames on this DOM element
145
+ *
146
+ * Update: should not ignore mutations for row selection to avoid known issue with table selection highlight in firefox
147
+ * Related bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=1289673
148
+ * */
149
+ var isTableSelection = mutationRecord.type === 'selection' && mutationRecord.target.nodeName === 'TR';
150
+ /**
151
+ * Update: should not ignore mutations when an node is added, as this interferes with
152
+ * prosemirrors handling of some language inputs in Safari (ie. Pinyin, Hiragana).
153
+ *
154
+ * In paticular, when a composition occurs at the start of the first node inside a table cell, if the resulting mutation
155
+ * from the composition end is ignored than prosemirror will end up with; invalid table markup nesting and a misplaced
156
+ * selection and insertion.
157
+ */
158
+ var isNodeInsertion = mutationRecord.type === 'childList' && mutationRecord.target.nodeName === 'TR' && mutationRecord.addedNodes.length;
159
+ if (isTableSelection || isNodeInsertion) {
160
+ return false;
183
161
  }
184
- var domTop = currentTableTop > 0 ? scrollTop : scrollTop + currentTableTop;
185
- if (!_this.isSticky) {
186
- var _this$editorScrollabl;
187
- (0, _dom.syncStickyRowToTable)(table);
188
- _this.dom.classList.add('sticky');
189
- table.classList.add(_types.TableCssClassName.TABLE_STICKY);
190
- _this.isSticky = true;
162
+ return true;
163
+ }
191
164
 
192
- /**
193
- * The logic below is not desirable, but acts as a fail safe for scenarios where the sticky header
194
- * detaches from the table. This typically happens during a fast scroll by the user which causes
195
- * the intersection observer logic to not fire as expected.
196
- */
197
- (_this$editorScrollabl = _this.editorScrollableElement) === null || _this$editorScrollabl === void 0 || _this$editorScrollabl.addEventListener('scrollend', _this.refireIntersectionObservers, {
198
- passive: true,
199
- once: true
200
- });
201
- var fastScrollThresholdMs = 500;
202
- setTimeout(function () {
203
- _this.refireIntersectionObservers();
204
- }, fastScrollThresholdMs);
205
- }
206
- _this.dom.style.top = "".concat(domTop, "px");
207
- (0, _dom.updateStickyMargins)(table);
208
- _this.dom.scrollLeft = wrapper.scrollLeft;
209
- _this.emitOn(domTop, _this.colControlsOffset);
210
- });
211
- (0, _defineProperty2.default)(this, "makeRowHeaderNotSticky", function (table) {
212
- var isEditorDestroyed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
213
- if (!_this.isSticky || !table || !_this.dom) {
214
- return;
215
- }
216
- _this.dom.style.removeProperty('width');
217
- _this.dom.classList.remove('sticky');
218
- table.classList.remove(_types.TableCssClassName.TABLE_STICKY);
219
- _this.isSticky = false;
220
- _this.dom.style.top = '';
221
- table.style.removeProperty('margin-top');
222
- _this.emitOff(isEditorDestroyed);
223
- });
224
- (0, _defineProperty2.default)(this, "getWrapperoffset", function () {
225
- var inverse = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
226
- var focusValue = inverse ? !_this.focused : _this.focused;
227
- return focusValue ? 0 : _consts.tableControlsSpacing;
228
- });
229
- (0, _defineProperty2.default)(this, "getWrapperRefTop", function (wrapper) {
230
- return Math.round((0, _dom2.getTop)(wrapper)) + _this.getWrapperoffset();
231
- });
232
- // TODO: rename!
233
- (0, _defineProperty2.default)(this, "getScrolledTableTop", function (wrapper) {
234
- return _this.getWrapperRefTop(wrapper) - _this.topPosEditorElement;
235
- });
236
- (0, _defineProperty2.default)(this, "getCurrentTableTop", function (tree) {
237
- return _this.getScrolledTableTop(tree.wrapper) + tree.table.clientHeight;
238
- });
239
- /* emit external events */
240
- (0, _defineProperty2.default)(this, "padding", 0);
241
- (0, _defineProperty2.default)(this, "top", 0);
242
- (0, _defineProperty2.default)(this, "emitOn", function (top, padding) {
243
- if (top === _this.top && padding === _this.padding) {
165
+ /**
166
+ * Methods
167
+ */
168
+ }, {
169
+ key: "addDropTarget",
170
+ value: function addDropTarget(element) {
171
+ var pos = this.getPos();
172
+ if (!Number.isFinite(pos)) {
244
173
  return;
245
174
  }
246
- _this.top = top;
247
- _this.padding = padding;
248
- (0, _commands.updateStickyState)({
249
- pos: _this.getPos(),
250
- top: top,
251
- sticky: true,
252
- padding: padding
253
- })(_this.view.state, _this.view.dispatch, _this.view);
254
- });
255
- (0, _defineProperty2.default)(this, "emitOff", function (isEditorDestroyed) {
256
- if (_this.top === 0 && _this.padding === 0) {
257
- return;
175
+ if (this.dropTargetCleanup) {
176
+ this.dropTargetCleanup();
258
177
  }
259
- _this.top = 0;
260
- _this.padding = 0;
261
- if (!isEditorDestroyed) {
262
- (0, _commands.updateStickyState)({
263
- pos: _this.getPos(),
264
- sticky: false,
265
- top: _this.top,
266
- padding: _this.padding
267
- })(_this.view.state, _this.view.dispatch, _this.view);
268
- }
269
- });
270
- this.view = view;
271
- this.node = node;
272
- this.getPos = getPos;
273
- this.eventDispatcher = eventDispatcher;
274
- this.dom = document.createElement('tr');
275
- this.contentDOM = this.dom;
276
- this.lastTimePainted = 0;
277
- this.isHeaderRow = supportedHeaderRow(node);
278
- this.isSticky = false;
279
- this.lastStickyTimestamp = undefined;
280
- if (this.isHeaderRow) {
281
- this.dom.setAttribute('data-header-row', 'true');
282
- this.subscribe();
283
- }
284
- }
285
- (0, _createClass2.default)(TableRowNodeView, [{
286
- key: "tree",
287
- get: function get() {
288
- return (0, _dom2.getTree)(this.dom);
178
+ var resolvedPos = this.view.state.doc.resolve(pos);
179
+ var targetIndex = resolvedPos.index();
180
+ var localId = resolvedPos.parent.attrs.localId;
181
+ this.dropTargetCleanup = (0, _element.dropTargetForElements)({
182
+ element: element,
183
+ canDrop: function canDrop(_ref2) {
184
+ var _data$indexes, _data$indexes2;
185
+ var source = _ref2.source;
186
+ var data = source.data;
187
+ return (
188
+ // Only draggables of row type can be dropped on this target
189
+ data.type === 'table-row' &&
190
+ // Only draggables which came from the same table can be dropped on this target
191
+ data.localId === localId &&
192
+ // Only draggables which DO NOT include this drop targets index can be dropped
193
+ !!((_data$indexes = data.indexes) !== null && _data$indexes !== void 0 && _data$indexes.length) && ((_data$indexes2 = data.indexes) === null || _data$indexes2 === void 0 ? void 0 : _data$indexes2.indexOf(targetIndex)) === -1
194
+ );
195
+ },
196
+ getData: function getData(_ref3) {
197
+ var input = _ref3.input,
198
+ element = _ref3.element;
199
+ var data = {
200
+ localId: localId,
201
+ type: 'table-row',
202
+ targetIndex: targetIndex
203
+ };
204
+ return (0, _closestEdge.attachClosestEdge)(data, {
205
+ input: input,
206
+ element: element,
207
+ allowedEdges: ['top', 'bottom']
208
+ });
209
+ }
210
+ });
289
211
  }
290
212
  }, {
291
213
  key: "subscribe",
@@ -295,8 +217,8 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
295
217
  this.initObservers();
296
218
  this.topPosEditorElement = (0, _dom2.getTop)(this.editorScrollableElement);
297
219
  }
298
- this.eventDispatcher.on('widthPlugin', this.updateStickyHeaderWidth);
299
- this.eventDispatcher.on(_pluginKey.pluginKey.key, this.onTablePluginState);
220
+ this.eventDispatcher.on('widthPlugin', this.updateStickyHeaderWidth.bind(this));
221
+ this.eventDispatcher.on(_pluginKey.pluginKey.key, this.onTablePluginState.bind(this));
300
222
  this.listening = true;
301
223
  this.dom.addEventListener('wheel', this.headerRowMouseScroll.bind(this), {
302
224
  passive: true
@@ -350,9 +272,9 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
350
272
  this.resizeObserver.observe(this.editorScrollableElement);
351
273
  }
352
274
  window.requestAnimationFrame(function () {
353
- var _this2$tree;
275
+ var _getTree;
354
276
  // we expect tree to be defined after animation frame
355
- var tableContainer = (_this2$tree = _this2.tree) === null || _this2$tree === void 0 ? void 0 : _this2$tree.wrapper.closest(".".concat(_types.TableCssClassName.NODEVIEW_WRAPPER));
277
+ var tableContainer = (_getTree = (0, _dom2.getTree)(_this2.dom)) === null || _getTree === void 0 ? void 0 : _getTree.wrapper.closest(".".concat(_types.TableCssClassName.NODEVIEW_WRAPPER));
356
278
  if (tableContainer) {
357
279
  _this2.sentinels.top = tableContainer.getElementsByClassName(_types.TableCssClassName.TABLE_STICKY_SENTINEL_TOP).item(0);
358
280
  _this2.sentinels.bottom = tableContainer.getElementsByClassName(_types.TableCssClassName.TABLE_STICKY_SENTINEL_BOTTOM).item(0);
@@ -374,10 +296,11 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
374
296
  value: function createResizeObserver() {
375
297
  var _this3 = this;
376
298
  this.resizeObserver = new ResizeObserver(function (entries) {
377
- if (!_this3.tree) {
299
+ var tree = (0, _dom2.getTree)(_this3.dom);
300
+ if (!tree) {
378
301
  return;
379
302
  }
380
- var table = _this3.tree.table;
303
+ var table = tree.table;
381
304
  entries.forEach(function (entry) {
382
305
  var _this3$editorScrollab;
383
306
  // On resize of the parent scroll element we need to adjust the width
@@ -403,10 +326,11 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
403
326
  value: function createIntersectionObserver() {
404
327
  var _this4 = this;
405
328
  this.intersectionObserver = new IntersectionObserver(function (entries, _) {
406
- if (!_this4.tree) {
329
+ var tree = (0, _dom2.getTree)(_this4.dom);
330
+ if (!tree) {
407
331
  return;
408
332
  }
409
- var table = _this4.tree.table;
333
+ var table = tree.table;
410
334
  if (table.rows.length < 2) {
411
335
  // ED-19307 - When there's only one row in a table the top & bottom sentinels become inverted. This creates some nasty visiblity
412
336
  // toggling side-effects because the intersection observers gets confused.
@@ -425,7 +349,7 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
425
349
  var sentinelIsBelowScrollArea = (((_entry$rootBounds2 = entry.rootBounds) === null || _entry$rootBounds2 === void 0 ? void 0 : _entry$rootBounds2.bottom) || 0) < entry.boundingClientRect.bottom;
426
350
  if (!entry.isIntersecting && !sentinelIsBelowScrollArea) {
427
351
  var _entry$rootBounds3;
428
- _this4.tree && _this4.makeHeaderRowSticky(_this4.tree, (_entry$rootBounds3 = entry.rootBounds) === null || _entry$rootBounds3 === void 0 ? void 0 : _entry$rootBounds3.top);
352
+ tree && _this4.makeHeaderRowSticky(tree, (_entry$rootBounds3 = entry.rootBounds) === null || _entry$rootBounds3 === void 0 ? void 0 : _entry$rootBounds3.top);
429
353
  _this4.lastStickyTimestamp = Date.now();
430
354
  } else {
431
355
  table && _this4.makeRowHeaderNotSticky(table);
@@ -445,7 +369,7 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
445
369
  }
446
370
  } else if (entry.isIntersecting && sentinelIsAboveScrollArea) {
447
371
  var _entry$rootBounds5;
448
- _this4.tree && _this4.makeHeaderRowSticky(_this4.tree, entry === null || entry === void 0 || (_entry$rootBounds5 = entry.rootBounds) === null || _entry$rootBounds5 === void 0 ? void 0 : _entry$rootBounds5.top);
372
+ tree && _this4.makeHeaderRowSticky(tree, entry === null || entry === void 0 || (_entry$rootBounds5 = entry.rootBounds) === null || _entry$rootBounds5 === void 0 ? void 0 : _entry$rootBounds5.top);
449
373
  _this4.lastStickyTimestamp = Date.now();
450
374
  }
451
375
  }
@@ -455,72 +379,208 @@ var TableRowNodeView = exports.TableRowNodeView = /*#__PURE__*/function () {
455
379
  root: this.editorScrollableElement
456
380
  });
457
381
  }
458
-
459
- /* paint/update loop */
382
+ /* receive external events */
460
383
  }, {
461
- key: "update",
462
- value: /* nodeview lifecycle */
463
- function update(node) {
464
- // do nothing if nodes were identical
465
- if (node === this.node) {
466
- return true;
384
+ key: "onTablePluginState",
385
+ value: function onTablePluginState(state) {
386
+ var tableRef = state.tableRef;
387
+ var tree = (0, _dom2.getTree)(this.dom);
388
+ if (!tree) {
389
+ return;
467
390
  }
468
391
 
469
- // see if we're changing into a header row or
470
- // changing away from one
471
- var newNodeIsHeaderRow = supportedHeaderRow(node);
472
- if (this.isHeaderRow !== newNodeIsHeaderRow) {
473
- return false; // re-create nodeview
392
+ // when header rows are toggled off - mark sentinels as unobserved
393
+ if (!state.isHeaderRowEnabled) {
394
+ [this.sentinels.top, this.sentinels.bottom].forEach(function (el) {
395
+ if (el) {
396
+ delete el.dataset.isObserved;
397
+ }
398
+ });
474
399
  }
400
+ var isCurrentTableSelected = tableRef === tree.table;
475
401
 
476
- // node is different but no need to re-create nodeview
477
- this.node = node;
402
+ // If current table selected and header row is toggled off, turn off sticky header
403
+ if (isCurrentTableSelected && !state.isHeaderRowEnabled && tree) {
404
+ this.makeRowHeaderNotSticky(tree.table);
405
+ }
406
+ this.focused = isCurrentTableSelected;
407
+ var wrapper = tree.wrapper;
408
+ var tableContainer = wrapper.parentElement;
409
+ var tableContentWrapper = tableContainer.parentElement;
410
+ var layoutContainer = tableContentWrapper && tableContentWrapper.parentElement;
411
+ if (isCurrentTableSelected) {
412
+ this.colControlsOffset = _consts.tableControlsSpacing;
413
+ if (layoutContainer && layoutContainer.getAttribute('data-layout-content')) {
414
+ // move table a little out of the way
415
+ // to provide spacing for table controls
416
+ tableContentWrapper.style.paddingLeft = '11px';
417
+ }
418
+ } else {
419
+ this.colControlsOffset = 0;
420
+ if (layoutContainer && layoutContainer.getAttribute('data-layout-content')) {
421
+ tableContentWrapper.style.removeProperty('padding-left');
422
+ }
423
+ }
478
424
 
479
- // don't do anything if we're just a regular tr
480
- if (!this.isHeaderRow) {
481
- return true;
425
+ // run after table style changes have been committed
426
+ setTimeout(function () {
427
+ (0, _dom.syncStickyRowToTable)(tree.table);
428
+ }, 0);
429
+ }
430
+ }, {
431
+ key: "updateStickyHeaderWidth",
432
+ value: function updateStickyHeaderWidth() {
433
+ // table width might have changed, sync that back to sticky row
434
+ var tree = (0, _dom2.getTree)(this.dom);
435
+ if (!tree) {
436
+ return;
482
437
  }
438
+ (0, _dom.syncStickyRowToTable)(tree.table);
439
+ }
483
440
 
484
- // something changed, sync widths
441
+ /**
442
+ * Manually refire the intersection observers.
443
+ * Useful when the header may have detached from the table.
444
+ */
445
+ }, {
446
+ key: "refireIntersectionObservers",
447
+ value: function refireIntersectionObservers() {
448
+ var _this5 = this;
449
+ if (this.isSticky) {
450
+ [this.sentinels.top, this.sentinels.bottom].forEach(function (el) {
451
+ if (el && _this5.intersectionObserver) {
452
+ _this5.intersectionObserver.unobserve(el);
453
+ _this5.intersectionObserver.observe(el);
454
+ }
455
+ });
456
+ }
457
+ }
458
+ }, {
459
+ key: "makeHeaderRowSticky",
460
+ value: function makeHeaderRowSticky(tree, scrollTop) {
461
+ var _tbody$firstChild,
462
+ _this6 = this;
463
+ // If header row height is more than 50% of viewport height don't do this
464
+ if (this.isSticky || this.stickyRowHeight && this.stickyRowHeight > window.innerHeight / 2) {
465
+ return;
466
+ }
467
+ var table = tree.table,
468
+ wrapper = tree.wrapper;
469
+
470
+ // ED-16035 Make sure sticky header is only applied to first row
485
471
  var tbody = this.dom.parentElement;
486
- var table = tbody && tbody.parentElement;
487
- (0, _dom.syncStickyRowToTable)(table);
488
- return true;
472
+ var isFirstHeader = tbody === null || tbody === void 0 || (_tbody$firstChild = tbody.firstChild) === null || _tbody$firstChild === void 0 ? void 0 : _tbody$firstChild.isEqualNode(this.dom);
473
+ if (!isFirstHeader) {
474
+ return;
475
+ }
476
+ var currentTableTop = this.getCurrentTableTop(tree);
477
+ if (!scrollTop) {
478
+ scrollTop = (0, _dom2.getTop)(this.editorScrollableElement);
479
+ }
480
+ var domTop = currentTableTop > 0 ? scrollTop : scrollTop + currentTableTop;
481
+ if (!this.isSticky) {
482
+ var _this$editorScrollabl;
483
+ (0, _dom.syncStickyRowToTable)(table);
484
+ this.dom.classList.add('sticky');
485
+ table.classList.add(_types.TableCssClassName.TABLE_STICKY);
486
+ this.isSticky = true;
487
+
488
+ /**
489
+ * The logic below is not desirable, but acts as a fail safe for scenarios where the sticky header
490
+ * detaches from the table. This typically happens during a fast scroll by the user which causes
491
+ * the intersection observer logic to not fire as expected.
492
+ */
493
+ (_this$editorScrollabl = this.editorScrollableElement) === null || _this$editorScrollabl === void 0 || _this$editorScrollabl.addEventListener('scrollend', this.refireIntersectionObservers, {
494
+ passive: true,
495
+ once: true
496
+ });
497
+ var fastScrollThresholdMs = 500;
498
+ setTimeout(function () {
499
+ _this6.refireIntersectionObservers();
500
+ }, fastScrollThresholdMs);
501
+ }
502
+ this.dom.style.top = "".concat(domTop, "px");
503
+ (0, _dom.updateStickyMargins)(table);
504
+ this.dom.scrollLeft = wrapper.scrollLeft;
505
+ this.emitOn(domTop, this.colControlsOffset);
489
506
  }
490
507
  }, {
491
- key: "destroy",
492
- value: function destroy() {
493
- this.unsubscribe();
494
- if (this.tree) {
495
- this.makeRowHeaderNotSticky(this.tree.table, true);
508
+ key: "makeRowHeaderNotSticky",
509
+ value: function makeRowHeaderNotSticky(table) {
510
+ var isEditorDestroyed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
511
+ if (!this.isSticky || !table || !this.dom) {
512
+ return;
496
513
  }
497
- this.emitOff(true);
514
+ this.dom.style.removeProperty('width');
515
+ this.dom.classList.remove('sticky');
516
+ table.classList.remove(_types.TableCssClassName.TABLE_STICKY);
517
+ this.isSticky = false;
518
+ this.dom.style.top = '';
519
+ table.style.removeProperty('margin-top');
520
+ this.emitOff(isEditorDestroyed);
498
521
  }
499
522
  }, {
500
- key: "ignoreMutation",
501
- value: function ignoreMutation(mutationRecord) {
502
- /* tableRows are not directly editable by the user
503
- * so it should be safe to ignore mutations that we cause
504
- * by updating styles and classnames on this DOM element
505
- *
506
- * Update: should not ignore mutations for row selection to avoid known issue with table selection highlight in firefox
507
- * Related bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=1289673
508
- * */
509
- var isTableSelection = mutationRecord.type === 'selection' && mutationRecord.target.nodeName === 'TR';
510
- /**
511
- * Update: should not ignore mutations when an node is added, as this interferes with
512
- * prosemirrors handling of some language inputs in Safari (ie. Pinyin, Hiragana).
513
- *
514
- * In paticular, when a composition occurs at the start of the first node inside a table cell, if the resulting mutation
515
- * from the composition end is ignored than prosemirror will end up with; invalid table markup nesting and a misplaced
516
- * selection and insertion.
517
- */
518
- var isNodeInsertion = mutationRecord.type === 'childList' && mutationRecord.target.nodeName === 'TR' && mutationRecord.addedNodes.length;
519
- if (isTableSelection || isNodeInsertion) {
520
- return false;
523
+ key: "getWrapperoffset",
524
+ value: function getWrapperoffset() {
525
+ var inverse = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
526
+ var focusValue = inverse ? !this.focused : this.focused;
527
+ return focusValue ? 0 : _consts.tableControlsSpacing;
528
+ }
529
+ }, {
530
+ key: "getWrapperRefTop",
531
+ value: function getWrapperRefTop(wrapper) {
532
+ return Math.round((0, _dom2.getTop)(wrapper)) + this.getWrapperoffset();
533
+ }
534
+
535
+ // TODO: rename!
536
+ }, {
537
+ key: "getScrolledTableTop",
538
+ value: function getScrolledTableTop(wrapper) {
539
+ return this.getWrapperRefTop(wrapper) - this.topPosEditorElement;
540
+ }
541
+ }, {
542
+ key: "getCurrentTableTop",
543
+ value: function getCurrentTableTop(tree) {
544
+ return this.getScrolledTableTop(tree.wrapper) + tree.table.clientHeight;
545
+ }
546
+
547
+ /* emit external events */
548
+ }, {
549
+ key: "emitOn",
550
+ value: function emitOn(top, padding) {
551
+ if (top === this.top && padding === this.padding) {
552
+ return;
553
+ }
554
+ this.top = top;
555
+ this.padding = padding;
556
+ var pos = this.getPos();
557
+ if (Number.isFinite(pos)) {
558
+ (0, _commands.updateStickyState)({
559
+ pos: pos,
560
+ top: top,
561
+ sticky: true,
562
+ padding: padding
563
+ })(this.view.state, this.view.dispatch, this.view);
564
+ }
565
+ }
566
+ }, {
567
+ key: "emitOff",
568
+ value: function emitOff(isEditorDestroyed) {
569
+ if (this.top === 0 && this.padding === 0) {
570
+ return;
571
+ }
572
+ this.top = 0;
573
+ this.padding = 0;
574
+ var pos = this.getPos();
575
+ if (!isEditorDestroyed && Number.isFinite(pos)) {
576
+ (0, _commands.updateStickyState)({
577
+ pos: pos,
578
+ sticky: false,
579
+ top: this.top,
580
+ padding: this.padding
581
+ })(this.view.state, this.view.dispatch, this.view);
521
582
  }
522
- return true;
523
583
  }
524
584
  }]);
525
- return TableRowNodeView;
526
- }();
585
+ return TableRow;
586
+ }(_TableNodeViewBase.default);