@progress/kendo-editor-common 1.9.0-dev.202204081457 → 1.9.0-dev.202204180753

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 (33) hide show
  1. package/dist/cdn/js/kendo-editor-common.js +2 -2
  2. package/dist/cdn/main.js +1 -1
  3. package/dist/es/config/constants.js +2 -1
  4. package/dist/es/plugins/image-resize.js +4 -14
  5. package/dist/es/plugins/resize-utils.js +11 -0
  6. package/dist/es/plugins/table-resize/column-resize.js +28 -49
  7. package/dist/es/plugins/table-resize/row-resize.js +40 -54
  8. package/dist/es/plugins/table-resize/table-resize.js +38 -64
  9. package/dist/es/plugins/table-resize/table-view.js +9 -9
  10. package/dist/es/plugins/table-resize/utils.js +26 -2
  11. package/dist/es2015/config/constants.js +2 -1
  12. package/dist/es2015/plugins/image-resize.js +4 -14
  13. package/dist/es2015/plugins/resize-utils.js +11 -0
  14. package/dist/es2015/plugins/table-resize/column-resize.js +28 -49
  15. package/dist/es2015/plugins/table-resize/row-resize.js +40 -54
  16. package/dist/es2015/plugins/table-resize/table-resize.js +40 -66
  17. package/dist/es2015/plugins/table-resize/table-view.js +9 -9
  18. package/dist/es2015/plugins/table-resize/utils.js +26 -2
  19. package/dist/npm/config/constants.d.ts +2 -1
  20. package/dist/npm/config/constants.js +2 -1
  21. package/dist/npm/plugins/image-resize.js +6 -16
  22. package/dist/npm/plugins/resize-utils.d.ts +35 -0
  23. package/dist/npm/plugins/resize-utils.js +13 -0
  24. package/dist/npm/plugins/table-resize/column-resize.js +28 -49
  25. package/dist/npm/plugins/table-resize/row-resize.js +39 -53
  26. package/dist/npm/plugins/table-resize/table-resize.d.ts +2 -4
  27. package/dist/npm/plugins/table-resize/table-resize.js +37 -63
  28. package/dist/npm/plugins/table-resize/table-view.d.ts +7 -7
  29. package/dist/npm/plugins/table-resize/table-view.js +8 -8
  30. package/dist/npm/plugins/table-resize/utils.d.ts +10 -0
  31. package/dist/npm/plugins/table-resize/utils.js +29 -2
  32. package/dist/systemjs/kendo-editor-common.js +1 -1
  33. package/package.json +1 -1
@@ -3,4 +3,5 @@ export var colgroupAttr = 'k-colgroup-data';
3
3
  export var resizableAttr = 'resizable-node';
4
4
  export var resizableWrap = 'k-editor-resize-wrap-element';
5
5
  export var resizeHandle = 'k-editor-resize-handle';
6
- export var dataDirection = 'data-direction';
6
+ export var dataResizeDirTable = 'data-direction';
7
+ export var dataResizeDirImage = 'data-direction-image';
@@ -1,20 +1,10 @@
1
1
  import * as tslib_1 from "tslib";
2
2
  import { NodeSelection, Plugin, PluginKey } from "prosemirror-state";
3
3
  import { Decoration, DecorationSet } from "prosemirror-view";
4
- import { dataDirection, resizeHandle } from "../config/constants";
4
+ import { dataResizeDirImage, resizeHandle } from "../config/constants";
5
5
  import { changeStylesString } from "../utils";
6
+ import { directions, handles } from './resize-utils';
6
7
  export var imageResizeKey = new PluginKey('image-resize');
7
- var directions = {
8
- 'southeast': { x: 1, y: 1 },
9
- 'east': { x: 1, y: 0 },
10
- 'south': { x: 0, y: 1 },
11
- 'north': { x: 0, y: -1 },
12
- 'west': { x: -1, y: 0 },
13
- 'southwest': { x: -1, y: 1 },
14
- 'northwest': { x: -1, y: -1 },
15
- 'northeast': { x: 1, y: -1 } // top right
16
- };
17
- var handles = Object.keys(directions);
18
8
  var setSize = function (domNode, sizeType, value) {
19
9
  domNode.style[sizeType] = value + 'px';
20
10
  };
@@ -110,7 +100,7 @@ var handleMouseUp = function (view) {
110
100
  };
111
101
  var handleMouseDown = function (view, event, options) {
112
102
  var target = event.target;
113
- var activeHandle = target.getAttribute(dataDirection);
103
+ var activeHandle = target.getAttribute(dataResizeDirImage);
114
104
  if (!activeHandle) {
115
105
  return false;
116
106
  }
@@ -223,7 +213,7 @@ export var imageResizing = function (options) {
223
213
  for (var i = 0; i < handles.length; i++) {
224
214
  var dom = document.createElement('div');
225
215
  dom.className = resizeHandle + ' ' + handles[i];
226
- dom.setAttribute(dataDirection, handles[i]);
216
+ dom.setAttribute(dataResizeDirImage, handles[i]);
227
217
  wrapper.appendChild(dom);
228
218
  }
229
219
  return DecorationSet.create(state.doc, [Decoration.widget(state.selection.from + 1, wrapper)]);
@@ -0,0 +1,11 @@
1
+ export var directions = {
2
+ 'southeast': { x: 1, y: 1 },
3
+ 'east': { x: 1, y: 0 },
4
+ 'south': { x: 0, y: 1 },
5
+ 'north': { x: 0, y: -1 },
6
+ 'west': { x: -1, y: 0 },
7
+ 'southwest': { x: -1, y: 1 },
8
+ 'northwest': { x: -1, y: -1 },
9
+ 'northeast': { x: 1, y: -1 } // top right
10
+ };
11
+ export var handles = Object.keys(directions);
@@ -4,10 +4,10 @@ import { tableNodeTypes, TableMap } from 'prosemirror-tables';
4
4
  import { Decoration, DecorationSet } from 'prosemirror-view';
5
5
  import { colgroupAttr } from '../../config/constants';
6
6
  import { TableView } from './table-view';
7
- import { otherResizeHandle, otherResizing, parseStyle, setNodeStyle, tableColumnResizing as key } from './utils';
7
+ import { cellIndexes, domCellAround, otherResizeHandle, otherResizing, parentNode, parseStyle, setNodeStyle, tableColumnResizing as key } from './utils';
8
8
  export function columnResizing() {
9
9
  // tslint:disable-next-line:variable-name
10
- var View = TableView, lastColumnResizable = true, handleWidth = 5, cellMinWidth = 25;
10
+ var View = TableView, handleWidth = 5, cellMinWidth = 25;
11
11
  var plugin = new Plugin({
12
12
  key: key,
13
13
  state: {
@@ -30,7 +30,7 @@ export function columnResizing() {
30
30
  handleDOMEvents: {
31
31
  mousemove: function (view, event) {
32
32
  if (!otherResizing(key, view.state)) {
33
- handleMouseMove(view, event, handleWidth, cellMinWidth, lastColumnResizable);
33
+ handleMouseMove(view, event, handleWidth);
34
34
  }
35
35
  return false;
36
36
  },
@@ -43,29 +43,21 @@ export function columnResizing() {
43
43
  }
44
44
  },
45
45
  decorations: function (state) {
46
- if (otherResizing(key, state)) {
47
- return DecorationSet.empty;
48
- }
49
- var pluginState = key.getState(state);
50
- if (pluginState.activeHandle > -1) {
51
- return handleDecorations(state, pluginState.activeHandle);
46
+ if (!otherResizing(key, state)) {
47
+ var pluginState = key.getState(state);
48
+ if (pluginState.activeHandle > -1) {
49
+ return handleDecorations(state, pluginState.activeHandle);
50
+ }
52
51
  }
52
+ return DecorationSet.empty;
53
53
  },
54
54
  nodeViews: {}
55
55
  }
56
56
  });
57
57
  return plugin;
58
58
  }
59
- function cellAround($pos) {
60
- for (var d = $pos.depth - 1; d > 0; d--) {
61
- if ($pos.node(d).type.spec.tableRole === 'row') {
62
- return $pos.node(0).resolve($pos.before(d + 1));
63
- }
64
- }
65
- return null;
66
- }
67
59
  function pointsAtCell($pos) {
68
- return $pos.parent.type.spec.tableRole === 'row' && $pos.nodeAfter;
60
+ return Boolean($pos.parent.type.spec.tableRole === 'row' && $pos.nodeAfter);
69
61
  }
70
62
  var ResizeState = /** @class */ (function () {
71
63
  function ResizeState(activeHandle, dragging) {
@@ -83,7 +75,7 @@ var ResizeState = /** @class */ (function () {
83
75
  if (state.activeHandle > -1 && tr.docChanged) {
84
76
  var handle = tr.mapping.map(state.activeHandle, -1);
85
77
  if (!pointsAtCell(tr.doc.resolve(handle))) {
86
- handle = null;
78
+ handle = -1;
87
79
  }
88
80
  state = new ResizeState(handle, state.dragging);
89
81
  }
@@ -91,28 +83,22 @@ var ResizeState = /** @class */ (function () {
91
83
  };
92
84
  return ResizeState;
93
85
  }());
94
- function handleMouseMove(view, event, handleWidth, _cellMinWidth, lastColumnResizable) {
86
+ function handleMouseMove(view, event, handleWidth) {
95
87
  var pluginState = key.getState(view.state);
96
88
  if (!pluginState.dragging) {
97
89
  var target = domCellAround(event.target), cell = -1;
98
90
  if (target) {
91
+ var indexes = cellIndexes(target);
99
92
  var _a = target.getBoundingClientRect(), left = _a.left, right = _a.right;
100
- if (event.clientX - left <= handleWidth) {
101
- cell = edgeCell(view, event, 'left');
93
+ if (Math.abs(event.clientX - left) <= handleWidth && indexes.cellIndex > 0) {
94
+ indexes.cellIndex--;
95
+ cell = edgeCell(view, event, indexes);
102
96
  }
103
- else if (right - event.clientX <= handleWidth) {
104
- cell = edgeCell(view, event, 'right');
97
+ else if (right - event.clientX > 0 && right - event.clientX <= handleWidth) {
98
+ cell = edgeCell(view, event, indexes);
105
99
  }
106
100
  }
107
101
  if (cell !== pluginState.activeHandle) {
108
- if (!lastColumnResizable && cell !== -1) {
109
- var $cell = view.state.doc.resolve(cell);
110
- var table = $cell.node(-1), map = TableMap.get(table), start = $cell.start(-1);
111
- var col = map.colCount($cell.pos - start) + $cell.nodeAfter.attrs.colspan - 1;
112
- if (col === map.width - 1) {
113
- return;
114
- }
115
- }
116
102
  updateHandle(view, cell);
117
103
  }
118
104
  }
@@ -170,7 +156,7 @@ function handleMouseDown(view, event, cellMinWidth) {
170
156
  var widths = Array.from(col.parentNode.children).map(function (c) { return c.style.width; });
171
157
  if (widths.every(Boolean)) {
172
158
  var sum = widths.reduce(function (acc, cur) { return acc + parseFloat(cur); }, 0);
173
- tableAttrs = setNodeStyle(tableNode.attrs, 'width', sum + 'px');
159
+ tableAttrs = setNodeStyle(tableAttrs || tableNode.attrs, 'width', sum + 'px');
174
160
  }
175
161
  }
176
162
  if (tableAttrs) {
@@ -200,28 +186,21 @@ function handleMouseDown(view, event, cellMinWidth) {
200
186
  event.preventDefault();
201
187
  return true;
202
188
  }
203
- function domCellAround(target) {
204
- while (target && target.nodeName !== 'TD' && target.nodeName !== 'TH') {
205
- target = target.classList.contains('ProseMirror') ? null : target.parentNode;
206
- }
207
- return target;
208
- }
209
- function edgeCell(view, event, side) {
189
+ function edgeCell(view, event, indexes) {
210
190
  var found = view.posAtCoords({ left: event.clientX, top: event.clientY });
211
191
  if (!found) {
212
192
  return -1;
213
193
  }
214
- var pos = found.pos;
215
- var $cell = cellAround(view.state.doc.resolve(pos));
216
- if (!$cell) {
194
+ var $pos = view.state.doc.resolve(found.pos);
195
+ var parentTable = parentNode($pos, function (n) { return n.type.spec.tableRole === 'table'; });
196
+ if (parentTable === null) {
217
197
  return -1;
218
198
  }
219
- if (side === 'right') {
220
- return $cell.pos;
221
- }
222
- var map = TableMap.get($cell.node(-1)), start = $cell.start(-1);
223
- var index = map.map.indexOf($cell.pos - start);
224
- return index % map.width === 0 ? -1 : start + map.map[index - 1];
199
+ var tablePos = $pos.start(parentTable.depth);
200
+ var tableNode = parentTable.node;
201
+ var map = TableMap.get(tableNode);
202
+ var cell = tablePos + map.map[(map.width * indexes.rowIndex) + indexes.cellIndex];
203
+ return cell;
225
204
  }
226
205
  function draggedWidth(dragging, event, cellMinWidth) {
227
206
  var offset = event.clientX - dragging.startX;
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from 'prosemirror-state';
2
2
  import { TableMap, tableNodeTypes } from 'prosemirror-tables';
3
3
  import { Decoration, DecorationSet } from 'prosemirror-view';
4
- import { otherResizeHandle, otherResizing, parseStyle, setNodeStyle, tableRowResizing as key } from './utils';
4
+ import { domCellAround, otherResizeHandle, otherResizing, parentNode, parseStyle, setNodeStyle, tableRowResizing as key } from './utils';
5
5
  var TableRowView = /** @class */ (function () {
6
6
  function TableRowView() {
7
7
  }
@@ -11,7 +11,7 @@ var TableRowView = /** @class */ (function () {
11
11
  return TableRowView;
12
12
  }());
13
13
  export function rowResizing() {
14
- var handleWidth = 5, rowMinHeight = 25, lastRowResizable = true;
14
+ var handleWidth = 5;
15
15
  var plugin = new Plugin({
16
16
  key: key,
17
17
  state: {
@@ -34,7 +34,7 @@ export function rowResizing() {
34
34
  handleDOMEvents: {
35
35
  mousemove: function (view, event) {
36
36
  if (!otherResizing(key, view.state)) {
37
- handleMouseMove(view, event, handleWidth, rowMinHeight, lastRowResizable);
37
+ handleMouseMove(view, event, handleWidth);
38
38
  }
39
39
  return false;
40
40
  },
@@ -43,7 +43,7 @@ export function rowResizing() {
43
43
  return false;
44
44
  },
45
45
  mousedown: function (view, event) {
46
- return handleMouseDown(view, event, rowMinHeight);
46
+ return handleMouseDown(view, event);
47
47
  }
48
48
  },
49
49
  decorations: function (state) {
@@ -76,7 +76,7 @@ var ResizeState = /** @class */ (function () {
76
76
  if (action && action.setDragging !== undefined) {
77
77
  return new ResizeState(state.activeHandle, action.setDragging);
78
78
  }
79
- if (state.activeHandle > -1) { // && tr.docChanged
79
+ if (state.activeHandle > -1) {
80
80
  var handle = tr.mapping.map(state.activeHandle, -1);
81
81
  if (!pointsAtCell(tr.doc.resolve(handle))) {
82
82
  handle = null;
@@ -87,30 +87,22 @@ var ResizeState = /** @class */ (function () {
87
87
  };
88
88
  return ResizeState;
89
89
  }());
90
- function handleMouseMove(view, event, handleWidth, _rowMinHeight, lastRowResizable) {
90
+ function handleMouseMove(view, event, handleWidth) {
91
91
  var pluginState = key.getState(view.state);
92
92
  if (!pluginState.dragging) {
93
- var target = domCellAround(event.target), cell = -1;
93
+ var target = domCellAround(event.target), row = -1;
94
94
  if (target) {
95
95
  var rowDom = target.parentNode;
96
96
  var domRect = rowDom.getBoundingClientRect();
97
- if (event.clientY - domRect.top <= handleWidth) {
98
- cell = edgeCell(view, event, 'up');
97
+ if (Math.abs(event.clientY - domRect.top) <= handleWidth && rowDom.rowIndex > 0) {
98
+ row = edgeRow(view, event, rowDom.rowIndex - 1);
99
99
  }
100
- else if (domRect.bottom - event.clientY <= handleWidth) {
101
- cell = edgeCell(view, event, 'down');
100
+ else if (domRect.bottom - event.clientY > 0 && domRect.bottom - event.clientY <= handleWidth) {
101
+ row = edgeRow(view, event, rowDom.rowIndex);
102
102
  }
103
103
  }
104
- if (cell !== pluginState.activeHandle) {
105
- if (!lastRowResizable && cell !== -1) {
106
- // let $cell = view.state.doc.resolve(cell);
107
- // let table = $cell.node(-1), map = TableMap.get(table), start = $cell.start(-1);
108
- // let col = map.colCount($cell.pos - start) + $cell.nodeAfter.attrs.colspan - 1;
109
- // if (col === map.width - 1) {
110
- // return;
111
- // }
112
- }
113
- updateHandle(view, cell);
104
+ if (row !== pluginState.activeHandle) {
105
+ updateHandle(view, row);
114
106
  }
115
107
  }
116
108
  }
@@ -120,7 +112,7 @@ function handleMouseLeave(view) {
120
112
  updateHandle(view, -1);
121
113
  }
122
114
  }
123
- function handleMouseDown(view, event, rowMinHeight) {
115
+ function handleMouseDown(view, event) {
124
116
  var pluginState = key.getState(view.state);
125
117
  if (pluginState.activeHandle === -1 || pluginState.dragging) {
126
118
  return false;
@@ -148,7 +140,7 @@ function handleMouseDown(view, event, rowMinHeight) {
148
140
  var curPluginState = key.getState(view.state);
149
141
  if (curPluginState.dragging) {
150
142
  var tr = view.state.tr.setMeta(key, { setDragging: null });
151
- updateRowHeight(view, tr, curPluginState.activeHandle, draggedHeight(curPluginState.dragging, ev, rowMinHeight));
143
+ updateRowHeight(view, tr, curPluginState.activeHandle);
152
144
  view.dispatch(tr);
153
145
  }
154
146
  }
@@ -157,54 +149,45 @@ function handleMouseDown(view, event, rowMinHeight) {
157
149
  return finish(ev);
158
150
  }
159
151
  var curPluginState = key.getState(view.state);
160
- var dragged = draggedHeight(curPluginState.dragging, ev, rowMinHeight);
152
+ var dragged = draggedHeight(curPluginState.dragging, ev);
161
153
  var offset = ev.clientY - curPluginState.dragging.startY;
162
- displayRowHeight(view, curPluginState.activeHandle, dragged, rowMinHeight, offset, tableHeight);
154
+ displayRowHeight(view, curPluginState.activeHandle, dragged, offset, tableHeight);
163
155
  }
164
156
  event.view.addEventListener('mouseup', finish);
165
157
  event.view.addEventListener('mousemove', move);
166
158
  event.preventDefault();
167
159
  return true;
168
160
  }
169
- function domCellAround(target) {
170
- while (target && target.nodeName !== 'TD' && target.nodeName !== 'TH') {
171
- target = target.classList.contains('ProseMirror') ? null : target.parentNode;
172
- }
173
- return target;
174
- }
175
- function edgeCell(view, event, side) {
161
+ function edgeRow(view, event, rowIndex) {
176
162
  var found = view.posAtCoords({ left: event.clientX, top: event.clientY });
177
163
  if (!found) {
178
164
  return -1;
179
165
  }
180
- var pos = found.pos;
181
- var doc = view.state.doc;
182
- var $pos = doc.resolve(pos);
183
- var rowDepth = new Array($pos.depth + 1).fill(0).findIndex(function (_, i) { return $pos.node(i).type.spec.tableRole === 'row'; });
184
- if (rowDepth === -1) {
185
- return -1;
186
- }
187
- var tableDepth = rowDepth - 1;
188
- var rowIndex = $pos.index(tableDepth);
189
- rowIndex += side === 'up' ? -1 : 0;
190
- if (rowIndex < 0) {
166
+ var $pos = view.state.doc.resolve(found.pos);
167
+ var parentTable = parentNode($pos, function (n) { return n.type.spec.tableRole === 'table'; });
168
+ if (parentTable === null) {
191
169
  return -1;
192
170
  }
193
- var rowPos = $pos.posAtIndex(rowIndex, tableDepth);
194
- return rowPos;
171
+ var tablePos = $pos.start(parentTable.depth);
172
+ var tableNode = parentTable.node;
173
+ var map = TableMap.get(tableNode);
174
+ var row = tablePos + map.map[(map.width * rowIndex)] - 1;
175
+ return row;
195
176
  }
196
- function draggedHeight(dragging, event, rowMinHeight) {
177
+ function draggedHeight(dragging, event) {
197
178
  var offset = event.clientY - dragging.startY;
198
- return Math.max(rowMinHeight, dragging.startHeight.rowHeight + offset);
179
+ return dragging.startHeight.rowHeight + offset;
199
180
  }
200
181
  function updateHandle(view, value) {
201
182
  view.dispatch(view.state.tr.setMeta(key, { setHandle: value }));
202
183
  }
203
- function updateRowHeight(view, tr, rowPos, height) {
184
+ function updateRowHeight(view, tr, rowPos) {
204
185
  var doc = view.state.doc;
205
186
  var row = doc.nodeAt(rowPos);
206
- tr.setNodeMarkup(rowPos, null, setNodeStyle(row.attrs, 'height', height + 'px'));
207
187
  var dom = view.nodeDOM(rowPos);
188
+ var win = (dom.ownerDocument && dom.ownerDocument.defaultView) || window;
189
+ dom.style.height = win.getComputedStyle(dom).height;
190
+ tr.setNodeMarkup(rowPos, null, setNodeStyle(row.attrs, 'height', dom.style.height));
208
191
  var table = dom && dom.closest('table');
209
192
  var tableHeight = table && table.style.height;
210
193
  if (tableHeight) {
@@ -213,29 +196,32 @@ function updateRowHeight(view, tr, rowPos, height) {
213
196
  tr.setNodeMarkup(tablePos, null, setNodeStyle($pos.parent.attrs, 'height', tableHeight));
214
197
  }
215
198
  }
216
- // tslint:disable-next-line:max-line-length
217
- function displayRowHeight(view, rowPos, height, rowMinHeight, offset, tableHeight) {
199
+ function displayRowHeight(view, rowPos, height, offset, tableHeight) {
218
200
  var dom = view.nodeDOM(rowPos);
219
201
  if (dom) {
220
- dom.style.height = Math.max(height, rowMinHeight) + 'px';
202
+ dom.style.height = height + 'px';
203
+ var win = (dom.ownerDocument && dom.ownerDocument.defaultView) || window;
204
+ dom.style.height = win.getComputedStyle(dom).height;
221
205
  var table = dom.closest('table');
222
206
  var newHeight = (parseFloat(tableHeight) + offset) + 'px';
223
207
  var current = table && table.style.height;
224
208
  if (current && current !== newHeight) {
225
209
  table.style.height = (parseFloat(tableHeight) + offset) + 'px';
210
+ table.style.height = win.getComputedStyle(table).height;
226
211
  }
227
212
  }
228
213
  }
229
214
  function handleDecorations(state, pos) {
230
215
  var decorations = [];
231
216
  if (typeof pos !== 'number') {
232
- return;
217
+ return DecorationSet.empty;
233
218
  }
234
219
  var $row = state.doc.resolve(pos), table = $row.parent, map = TableMap.get(table), rowIndex = $row.index($row.depth), start = $row.start($row.depth);
235
220
  for (var col = 0; col < map.width; col++) {
236
221
  var index = col + rowIndex * map.width;
237
222
  var cellPos = map.map[index];
238
- var widgetPos = start + cellPos + table.nodeAt(cellPos).nodeSize - 1;
223
+ var cell = table.nodeAt(cellPos);
224
+ var widgetPos = start + cellPos + (cell ? cell.nodeSize : 0) - 1;
239
225
  var dom = document.createElement('div');
240
226
  dom.className = 'row-resize-handle';
241
227
  decorations.push(Decoration.widget(widgetPos, dom));
@@ -1,26 +1,8 @@
1
1
  import * as tslib_1 from "tslib";
2
2
  import { NodeSelection, Plugin } from 'prosemirror-state';
3
- import { colgroupAttr, dataDirection, resizableAttr } from '../../config/constants';
4
- import { getTable, parseStyle, setNodeStyle, tableResizeKey as key } from './utils';
5
- function parentNode(pos, predicate) {
6
- for (var d = pos.depth; d > 0; d--) {
7
- var node = pos.node(d);
8
- if (predicate(node)) {
9
- return node;
10
- }
11
- }
12
- return null;
13
- }
14
- var directions = {
15
- 'southeast': { x: 1, y: 1 },
16
- 'east': { x: 1, y: 0 },
17
- 'south': { x: 0, y: 1 },
18
- 'north': { x: 0, y: -1 },
19
- 'west': { x: -1, y: 0 },
20
- 'southwest': { x: -1, y: 1 },
21
- 'northwest': { x: -1, y: -1 },
22
- 'northeast': { x: 1, y: -1 } // top right
23
- };
3
+ import { colgroupAttr, dataResizeDirTable, resizableAttr } from '../../config/constants';
4
+ import { getTable, parentNode, parseStyle, setNodeStyle, tableResizeKey as key } from './utils';
5
+ import { directions } from './../resize-utils';
24
6
  var commonDir = {
25
7
  'southeast': true,
26
8
  'southwest': true,
@@ -29,9 +11,6 @@ var commonDir = {
29
11
  };
30
12
  var horizontalDir = tslib_1.__assign({ 'east': true, 'west': true }, commonDir);
31
13
  var verticalDir = tslib_1.__assign({ 'south': true, 'north': true }, commonDir);
32
- var setSize = function (domNode, sizeType, value) {
33
- domNode.style[sizeType] = value + 'px';
34
- };
35
14
  var ResizeState = /** @class */ (function () {
36
15
  function ResizeState(activeHandle, dragging, nodePosition) {
37
16
  this.activeHandle = activeHandle;
@@ -48,73 +27,62 @@ var ResizeState = /** @class */ (function () {
48
27
  };
49
28
  return ResizeState;
50
29
  }());
51
- var handleMouseMove = function (view, event, options) {
30
+ var handleMouseMove = function (view, event) {
52
31
  var state = key.getState(view.state);
53
32
  var dragging = state.dragging, nodePosition = state.nodePosition, activeHandle = state.activeHandle;
54
33
  if (nodePosition < 0 || !dragging) {
55
34
  return;
56
35
  }
57
- var resizedNode = getTable(view.nodeDOM(nodePosition));
58
- var rect = resizedNode.getBoundingClientRect();
36
+ var tableDom = getTable(view.nodeDOM(nodePosition));
37
+ var rect = tableDom.getBoundingClientRect();
59
38
  var dir = directions[activeHandle];
60
39
  var diffX = (event.clientX - dragging.startX) * dir.x;
61
40
  var diffY = (event.clientY - dragging.startY) * dir.y;
62
- var nodeWidth = resizedNode.offsetWidth;
63
- var nodeHeight = resizedNode.offsetHeight;
41
+ var win = (tableDom.ownerDocument && tableDom.ownerDocument.defaultView) || window;
42
+ var compStyles = win.getComputedStyle(tableDom);
43
+ var nodeWidth = /px/.test(compStyles.width) ? parseFloat(compStyles.width) : tableDom.offsetWidth;
44
+ var nodeHeight = /px/.test(compStyles.height) ? parseFloat(compStyles.height) : tableDom.offsetHeight;
64
45
  var width = dir.x ? diffX + nodeWidth : rect.width;
65
46
  var height = dir.y ? diffY + nodeHeight : rect.height;
66
- if (options.lockRatio && dir.x && dir.y) {
67
- var ratio = Math.min(width / nodeWidth, height / nodeHeight);
68
- var lockWidth = nodeWidth * ratio;
69
- var lockHeight = nodeHeight * ratio;
70
- dragging.startX = event.clientX - (width - lockWidth) * dir.x;
71
- dragging.startY = event.clientY - (height - lockHeight) * dir.y;
72
- width = lockWidth;
73
- height = lockHeight;
74
- }
75
- else {
76
- dragging.startX = dir.x ? event.clientX : dragging.startX;
77
- dragging.startY = dir.y ? event.clientY : dragging.startY;
78
- }
47
+ dragging.startX = dir.x ? event.clientX : dragging.startX;
48
+ dragging.startY = dir.y ? event.clientY : dragging.startY;
79
49
  if (horizontalDir[activeHandle]) {
80
- setSize(resizedNode, 'width', width);
50
+ tableDom.style.width = width + 'px';
81
51
  }
82
52
  if (verticalDir[activeHandle]) {
83
- setSize(resizedNode, 'height', height);
53
+ tableDom.style.height = height + 'px';
84
54
  }
85
55
  };
86
56
  var toPercents = function (view, tr, tablePos) {
87
57
  var tableNode = view.state.doc.nodeAt(tablePos);
88
58
  var tableDom = getTable(view.nodeDOM(tablePos));
89
- var _a = tableSize(tableDom), tableWidth = _a.width, tableHeight = _a.height, colsWidth = _a.colsWidth;
59
+ var _a = tableSize(tableDom), width = _a.width, height = _a.height, colsWidth = _a.colsWidth, rowsHeight = _a.rowsHeight, offsetWidth = _a.offsetWidth, offsetHeight = _a.offsetHeight;
90
60
  var colgroup = tableDom.firstChild;
91
61
  var cols = Array.from((colgroup && colgroup.children) || []);
92
- // const borders = new Array(cols.length).fill(tableWidth / tableOffsetWidth / cols.length);
93
62
  var widthChanged = false;
94
63
  cols.forEach(function (col, i) {
95
64
  if (col.style.width && !/%$/.test(col.style.width)) {
96
- col.style.width = ((colsWidth[i]) * 100 / tableWidth) + '%';
65
+ col.style.width = ((colsWidth[i]) * 100 / width) + '%';
97
66
  widthChanged = true;
98
67
  }
99
68
  });
100
- var rows = Array.from(tableDom.rows);
101
69
  var heightChange = false;
102
70
  tableNode.forEach(function (row, offset, index) {
103
71
  var rowHeight = parseStyle(row.attrs.style).height;
104
72
  if (rowHeight && !/%$/.test(rowHeight)) {
105
- tr.setNodeMarkup(tablePos + offset + 1, null, setNodeStyle(row.attrs, 'height', (rows[index].offsetHeight * 100 / tableHeight) + '%'));
73
+ tr.setNodeMarkup(tablePos + offset + 1, null, setNodeStyle(row.attrs, 'height', (rowsHeight[index] * 100 / height) + '%'));
106
74
  heightChange = true;
107
75
  }
108
76
  });
109
77
  var tableAttrs = tableNode.attrs;
110
- if (parseStyle(tableAttrs.style).width !== tableWidth + 'px') {
111
- tableAttrs = setNodeStyle(tableAttrs, 'width', tableWidth + 'px');
78
+ if (parseStyle(tableAttrs.style).width !== offsetWidth + 'px') {
79
+ tableAttrs = setNodeStyle(tableAttrs, 'width', offsetWidth + 'px');
112
80
  }
113
81
  if (widthChanged) {
114
82
  tableAttrs[colgroupAttr] = colgroup.outerHTML;
115
83
  }
116
84
  if (heightChange) {
117
- tableAttrs = setNodeStyle(tableAttrs, 'height', tableHeight + 'px');
85
+ tableAttrs = setNodeStyle(tableAttrs, 'height', offsetHeight + 'px');
118
86
  }
119
87
  if (widthChanged || heightChange) {
120
88
  tr.setNodeMarkup(tablePos, null, tableAttrs);
@@ -123,11 +91,13 @@ var toPercents = function (view, tr, tablePos) {
123
91
  var toPixels = function (view, tr, tablePos, attrs) {
124
92
  var tableNode = view.state.doc.nodeAt(tablePos);
125
93
  var tableDom = getTable(view.nodeDOM(tablePos));
94
+ var win = (tableDom.ownerDocument && tableDom.ownerDocument.defaultView) || window;
95
+ var calcStyle = win.getComputedStyle;
126
96
  var rows = Array.from(tableDom.rows);
127
97
  tableNode.forEach(function (row, offset, index) {
128
98
  var rowHeight = parseStyle(row.attrs.style).height;
129
99
  if (rowHeight && !/px$/.test(rowHeight)) {
130
- tr.setNodeMarkup(tablePos + offset + 1, null, setNodeStyle(row.attrs, 'height', rows[index].offsetHeight + 'px'));
100
+ tr.setNodeMarkup(tablePos + offset + 1, null, setNodeStyle(row.attrs, 'height', calcStyle(rows[index]).height));
131
101
  }
132
102
  });
133
103
  var colgroup = tableDom.firstChild;
@@ -135,7 +105,7 @@ var toPixels = function (view, tr, tablePos, attrs) {
135
105
  var widthChanged = false;
136
106
  cols.forEach(function (col, i) {
137
107
  if (col.style.width && !/px$/.test(col.style.width)) {
138
- col.style.width = cols[i].offsetWidth + 'px';
108
+ col.style.width = calcStyle(cols[i]).width;
139
109
  widthChanged = true;
140
110
  }
141
111
  });
@@ -148,9 +118,12 @@ var toPixels = function (view, tr, tablePos, attrs) {
148
118
  var tableSize = function (table) {
149
119
  var cols = Array.from(table.firstChild.children);
150
120
  var colsWidth = cols.map(function (c) { return c.offsetWidth; });
121
+ var rowsHeight = Array.from(table.rows).map(function (row) { return row.offsetHeight; });
151
122
  var width = colsWidth.reduce(function (acc, cur) { return acc + cur; }, 0);
152
- var height = table.offsetHeight; // Array.from(table.rows).reduce((acc, cur) => acc + cur.offsetHeight, 0);
153
- return { width: width, height: height, colsWidth: colsWidth, offsetWidth: table.offsetWidth };
123
+ var height = rowsHeight.reduce(function (acc, cur) { return acc + cur; }, 0);
124
+ var offsetHeight = table.offsetHeight;
125
+ var offsetWidth = table.offsetWidth;
126
+ return { width: width, height: height, colsWidth: colsWidth, rowsHeight: rowsHeight, offsetWidth: offsetWidth, offsetHeight: offsetHeight };
154
127
  };
155
128
  var handleMouseUp = function (view) {
156
129
  var _a = key.getState(view.state), dragging = _a.dragging, nodePosition = _a.nodePosition, activeHandle = _a.activeHandle;
@@ -160,7 +133,7 @@ var handleMouseUp = function (view) {
160
133
  var rect = tableSize(dom);
161
134
  if (node) {
162
135
  var width = rect.offsetWidth + 'px';
163
- var height = rect.height + 'px';
136
+ var height = rect.offsetHeight + 'px';
164
137
  var tr = view.state.tr;
165
138
  var attrs = node.attrs;
166
139
  var parsedStyles = parseStyle(attrs.style);
@@ -170,7 +143,7 @@ var handleMouseUp = function (view) {
170
143
  if (verticalDir[activeHandle] && dom.style.height && parsedStyles.height !== height) {
171
144
  attrs = setNodeStyle(attrs, 'height', height);
172
145
  }
173
- toPixels(view, tr, nodePosition, attrs);
146
+ attrs = toPixels(view, tr, nodePosition, attrs);
174
147
  tr.setNodeMarkup(nodePosition, null, attrs);
175
148
  tr.setMeta('commandName', 'node-resize');
176
149
  tr.setMeta('args', attrs);
@@ -183,9 +156,9 @@ var handleMouseUp = function (view) {
183
156
  }
184
157
  }
185
158
  };
186
- var handleMouseDown = function (view, event, options) {
159
+ var handleMouseDown = function (view, event) {
187
160
  var target = event.target;
188
- var activeHandle = target.getAttribute(dataDirection);
161
+ var activeHandle = target.getAttribute(dataResizeDirTable);
189
162
  if (!activeHandle) {
190
163
  return false;
191
164
  }
@@ -202,7 +175,7 @@ var handleMouseDown = function (view, event, options) {
202
175
  view.dispatch(transaction);
203
176
  var curWindow = event.view || window;
204
177
  function move(e) {
205
- handleMouseMove(view, e, options);
178
+ handleMouseMove(view, e);
206
179
  }
207
180
  function finish(_e) {
208
181
  curWindow.removeEventListener('mouseup', finish);
@@ -214,7 +187,7 @@ var handleMouseDown = function (view, event, options) {
214
187
  return true;
215
188
  };
216
189
  export var tableResizing = function (options) {
217
- if (options === void 0) { options = { node: 'table', lockRatio: false }; }
190
+ if (options === void 0) { options = { node: 'table' }; }
218
191
  return new Plugin({
219
192
  key: key,
220
193
  view: function (_viewObj) { return ({
@@ -224,7 +197,8 @@ export var tableResizing = function (options) {
224
197
  if (isNodeSelected && selection instanceof NodeSelection) {
225
198
  return { node: selection.node, pos: selection.from };
226
199
  }
227
- var node = parentNode(selection.$from, function (n) { return n.type === nodeType; });
200
+ var parent = parentNode(selection.$from, function (n) { return n.type === nodeType; });
201
+ var node = parent && parent.node;
228
202
  if (node) {
229
203
  var tableDepth = new Array(selection.$from.depth + 1).fill(0)
230
204
  .findIndex(function (_, i) { return selection.$from.node(i).type.spec.tableRole === 'table'; });
@@ -273,7 +247,7 @@ export var tableResizing = function (options) {
273
247
  props: {
274
248
  handleDOMEvents: {
275
249
  mousedown: function (view, event) {
276
- return handleMouseDown(view, event, options);
250
+ return handleMouseDown(view, event);
277
251
  }
278
252
  }
279
253
  }