@progress/kendo-editor-common 1.8.2-dev.202204011319 → 1.9.0-dev.202204060830
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.
- package/dist/cdn/js/kendo-editor-common.js +2 -2
- package/dist/cdn/main.js +1 -1
- package/dist/es/config/constants.js +4 -0
- package/dist/es/config/schema.js +9 -5
- package/dist/es/main.js +1 -0
- package/dist/es/plugins/image-resize.js +4 -3
- package/dist/es/plugins/table-resize/column-resize.js +294 -0
- package/dist/es/plugins/table-resize/index.js +8 -0
- package/dist/es/plugins/table-resize/row-resize.js +244 -0
- package/dist/es/plugins/table-resize/table-resize.js +281 -0
- package/dist/es/plugins/table-resize/table-view.js +87 -0
- package/dist/es/plugins/table-resize/utils.js +53 -0
- package/dist/es2015/config/constants.js +4 -0
- package/dist/es2015/config/schema.js +11 -8
- package/dist/es2015/main.js +1 -0
- package/dist/es2015/plugins/image-resize.js +4 -3
- package/dist/es2015/plugins/table-resize/column-resize.js +290 -0
- package/dist/es2015/plugins/table-resize/index.js +8 -0
- package/dist/es2015/plugins/table-resize/row-resize.js +240 -0
- package/dist/es2015/plugins/table-resize/table-resize.js +277 -0
- package/dist/es2015/plugins/table-resize/table-view.js +84 -0
- package/dist/es2015/plugins/table-resize/utils.js +51 -0
- package/dist/npm/config/constants.d.ts +4 -0
- package/dist/npm/config/constants.js +4 -0
- package/dist/npm/config/schema.d.ts +1 -0
- package/dist/npm/config/schema.js +8 -4
- package/dist/npm/main.d.ts +1 -0
- package/dist/npm/main.js +2 -0
- package/dist/npm/plugins/image-resize.js +4 -3
- package/dist/npm/plugins/table-resize/column-resize.d.ts +2 -0
- package/dist/npm/plugins/table-resize/column-resize.js +297 -0
- package/dist/npm/plugins/table-resize/index.d.ts +1 -0
- package/dist/npm/plugins/table-resize/index.js +10 -0
- package/dist/npm/plugins/table-resize/row-resize.d.ts +2 -0
- package/dist/npm/plugins/table-resize/row-resize.js +247 -0
- package/dist/npm/plugins/table-resize/table-resize.d.ts +6 -0
- package/dist/npm/plugins/table-resize/table-resize.js +283 -0
- package/dist/npm/plugins/table-resize/table-view.d.ts +17 -0
- package/dist/npm/plugins/table-resize/table-view.js +89 -0
- package/dist/npm/plugins/table-resize/utils.d.ts +12 -0
- package/dist/npm/plugins/table-resize/utils.js +59 -0
- package/dist/systemjs/kendo-editor-common.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { tableNodeTypes, TableMap } from 'prosemirror-tables';
|
|
3
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
4
|
+
import { colgroupAttr } from '../../config/constants';
|
|
5
|
+
import { TableView } from './table-view';
|
|
6
|
+
import { otherResizeHandle, otherResizing, parseStyle, setNodeStyle, tableColumnResizing as key } from './utils';
|
|
7
|
+
export function columnResizing() {
|
|
8
|
+
// tslint:disable-next-line:variable-name
|
|
9
|
+
const View = TableView, lastColumnResizable = true, handleWidth = 5, cellMinWidth = 25;
|
|
10
|
+
let plugin = new Plugin({
|
|
11
|
+
key,
|
|
12
|
+
state: {
|
|
13
|
+
init(_, state) {
|
|
14
|
+
this.spec.props.nodeViews[tableNodeTypes(state.schema).table.name] = (node, view) => new View(node, view);
|
|
15
|
+
return new ResizeState(-1, null);
|
|
16
|
+
},
|
|
17
|
+
apply(tr, prev) {
|
|
18
|
+
return prev.apply(tr);
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
props: {
|
|
22
|
+
attributes(state) {
|
|
23
|
+
if (otherResizeHandle(key, state)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
let pluginState = key.getState(state);
|
|
27
|
+
return pluginState.activeHandle > -1 ? { class: 'resize-cursor' } : null;
|
|
28
|
+
},
|
|
29
|
+
handleDOMEvents: {
|
|
30
|
+
mousemove(view, event) {
|
|
31
|
+
if (!otherResizing(key, view.state)) {
|
|
32
|
+
handleMouseMove(view, event, handleWidth, cellMinWidth, lastColumnResizable);
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
},
|
|
36
|
+
mouseleave(view) {
|
|
37
|
+
handleMouseLeave(view);
|
|
38
|
+
return false;
|
|
39
|
+
},
|
|
40
|
+
mousedown(view, event) {
|
|
41
|
+
return handleMouseDown(view, event, cellMinWidth);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
decorations(state) {
|
|
45
|
+
if (otherResizing(key, state)) {
|
|
46
|
+
return DecorationSet.empty;
|
|
47
|
+
}
|
|
48
|
+
let pluginState = key.getState(state);
|
|
49
|
+
if (pluginState.activeHandle > -1) {
|
|
50
|
+
return handleDecorations(state, pluginState.activeHandle);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
nodeViews: {}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return plugin;
|
|
57
|
+
}
|
|
58
|
+
function cellAround($pos) {
|
|
59
|
+
for (let d = $pos.depth - 1; d > 0; d--) {
|
|
60
|
+
if ($pos.node(d).type.spec.tableRole === 'row') {
|
|
61
|
+
return $pos.node(0).resolve($pos.before(d + 1));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
function pointsAtCell($pos) {
|
|
67
|
+
return $pos.parent.type.spec.tableRole === 'row' && $pos.nodeAfter;
|
|
68
|
+
}
|
|
69
|
+
class ResizeState {
|
|
70
|
+
constructor(activeHandle, dragging) {
|
|
71
|
+
this.activeHandle = activeHandle;
|
|
72
|
+
this.dragging = dragging;
|
|
73
|
+
}
|
|
74
|
+
apply(tr) {
|
|
75
|
+
let state = this, action = tr.getMeta(key);
|
|
76
|
+
if (action && action.setHandle != null) {
|
|
77
|
+
return new ResizeState(action.setHandle, null);
|
|
78
|
+
}
|
|
79
|
+
if (action && action.setDragging !== undefined) {
|
|
80
|
+
return new ResizeState(state.activeHandle, action.setDragging);
|
|
81
|
+
}
|
|
82
|
+
if (state.activeHandle > -1 && tr.docChanged) {
|
|
83
|
+
let handle = tr.mapping.map(state.activeHandle, -1);
|
|
84
|
+
if (!pointsAtCell(tr.doc.resolve(handle))) {
|
|
85
|
+
handle = null;
|
|
86
|
+
}
|
|
87
|
+
state = new ResizeState(handle, state.dragging);
|
|
88
|
+
}
|
|
89
|
+
return state;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function handleMouseMove(view, event, handleWidth, _cellMinWidth, lastColumnResizable) {
|
|
93
|
+
let pluginState = key.getState(view.state);
|
|
94
|
+
if (!pluginState.dragging) {
|
|
95
|
+
let target = domCellAround(event.target), cell = -1;
|
|
96
|
+
if (target) {
|
|
97
|
+
let { left, right } = target.getBoundingClientRect();
|
|
98
|
+
if (event.clientX - left <= handleWidth) {
|
|
99
|
+
cell = edgeCell(view, event, 'left');
|
|
100
|
+
}
|
|
101
|
+
else if (right - event.clientX <= handleWidth) {
|
|
102
|
+
cell = edgeCell(view, event, 'right');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (cell !== pluginState.activeHandle) {
|
|
106
|
+
if (!lastColumnResizable && cell !== -1) {
|
|
107
|
+
let $cell = view.state.doc.resolve(cell);
|
|
108
|
+
let table = $cell.node(-1), map = TableMap.get(table), start = $cell.start(-1);
|
|
109
|
+
let col = map.colCount($cell.pos - start) + $cell.nodeAfter.attrs.colspan - 1;
|
|
110
|
+
if (col === map.width - 1) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
updateHandle(view, cell);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function handleMouseLeave(view) {
|
|
119
|
+
let pluginState = key.getState(view.state);
|
|
120
|
+
if (pluginState.activeHandle > -1 && !pluginState.dragging) {
|
|
121
|
+
updateHandle(view, -1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function handleMouseDown(view, event, cellMinWidth) {
|
|
125
|
+
let pluginState = key.getState(view.state);
|
|
126
|
+
if (pluginState.activeHandle === -1 || pluginState.dragging) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
let $cell = view.state.doc.resolve(pluginState.activeHandle);
|
|
130
|
+
const row = $cell.parent;
|
|
131
|
+
const cellIndex = $cell.index();
|
|
132
|
+
let colSpan = 0;
|
|
133
|
+
for (let i = 0; i <= cellIndex; i++) {
|
|
134
|
+
colSpan += row.child(i).attrs.colspan;
|
|
135
|
+
}
|
|
136
|
+
const tableNode = $cell.node($cell.depth - 1);
|
|
137
|
+
let dom = view.domAtPos(pluginState.activeHandle);
|
|
138
|
+
let domCell = dom.node.childNodes[dom.offset];
|
|
139
|
+
let tableDom = domCell.closest('table');
|
|
140
|
+
let col, tableAttrs;
|
|
141
|
+
if (tableNode.attrs[colgroupAttr]) {
|
|
142
|
+
const colgroup = tableDom.firstChild;
|
|
143
|
+
col = colgroup.children[colSpan - 1];
|
|
144
|
+
if (!col.style.width) {
|
|
145
|
+
col.style.width = col.offsetWidth + 'px';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
let total = 0;
|
|
150
|
+
for (let i = 0; i < row.childCount; i++) {
|
|
151
|
+
total += row.child(i).attrs.colspan;
|
|
152
|
+
}
|
|
153
|
+
const colgroup = document.createElement('colgroup');
|
|
154
|
+
const cols = new Array(total);
|
|
155
|
+
for (let i = 0; i < total; i++) {
|
|
156
|
+
cols[i] = document.createElement('col');
|
|
157
|
+
colgroup.appendChild(cols[i]);
|
|
158
|
+
}
|
|
159
|
+
tableDom.insertBefore(colgroup, tableDom.firstChild);
|
|
160
|
+
col = cols[cellIndex];
|
|
161
|
+
col.style.width = col.offsetWidth + 'px';
|
|
162
|
+
tableAttrs = Object.assign({}, tableNode.attrs, { [colgroupAttr]: '<colgroup>' + cols.reduce((acc, cur) => acc + cur.outerHTML, '') + '</colgroup>' });
|
|
163
|
+
}
|
|
164
|
+
let width = parseFloat(col.style.width);
|
|
165
|
+
const tr = view.state.tr.setMeta(key, { setDragging: { startX: event.clientX, startWidth: width } });
|
|
166
|
+
if (!tableDom.style.width) {
|
|
167
|
+
const widths = Array.from(col.parentNode.children).map((c) => c.style.width);
|
|
168
|
+
if (widths.every(Boolean)) {
|
|
169
|
+
const sum = widths.reduce((acc, cur) => acc + parseFloat(cur), 0);
|
|
170
|
+
tableAttrs = setNodeStyle(tableNode.attrs, 'width', sum + 'px');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (tableAttrs) {
|
|
174
|
+
const tablePos = $cell.posAtIndex(0, $cell.depth - 1) - 1;
|
|
175
|
+
tr.setNodeMarkup(tablePos, null, tableAttrs);
|
|
176
|
+
}
|
|
177
|
+
view.dispatch(tr);
|
|
178
|
+
function finish(ev) {
|
|
179
|
+
ev.view.removeEventListener('mouseup', finish);
|
|
180
|
+
ev.view.removeEventListener('mousemove', move);
|
|
181
|
+
let curPluginState = key.getState(view.state);
|
|
182
|
+
if (curPluginState.dragging) {
|
|
183
|
+
updateColumnWidth(view, curPluginState.activeHandle, draggedWidth(curPluginState.dragging, ev, cellMinWidth));
|
|
184
|
+
view.dispatch(view.state.tr.setMeta(key, { setDragging: null }));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function move(ev) {
|
|
188
|
+
if (!ev.which) {
|
|
189
|
+
return finish(ev);
|
|
190
|
+
}
|
|
191
|
+
let curPluginState = key.getState(view.state);
|
|
192
|
+
let dragged = draggedWidth(curPluginState.dragging, ev, cellMinWidth);
|
|
193
|
+
displayColumnWidth(view, curPluginState.activeHandle, dragged, cellMinWidth);
|
|
194
|
+
}
|
|
195
|
+
event.view.addEventListener('mouseup', finish);
|
|
196
|
+
event.view.addEventListener('mousemove', move);
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
function domCellAround(target) {
|
|
201
|
+
while (target && target.nodeName !== 'TD' && target.nodeName !== 'TH') {
|
|
202
|
+
target = target.classList.contains('ProseMirror') ? null : target.parentNode;
|
|
203
|
+
}
|
|
204
|
+
return target;
|
|
205
|
+
}
|
|
206
|
+
function edgeCell(view, event, side) {
|
|
207
|
+
let found = view.posAtCoords({ left: event.clientX, top: event.clientY });
|
|
208
|
+
if (!found) {
|
|
209
|
+
return -1;
|
|
210
|
+
}
|
|
211
|
+
let { pos } = found;
|
|
212
|
+
let $cell = cellAround(view.state.doc.resolve(pos));
|
|
213
|
+
if (!$cell) {
|
|
214
|
+
return -1;
|
|
215
|
+
}
|
|
216
|
+
if (side === 'right') {
|
|
217
|
+
return $cell.pos;
|
|
218
|
+
}
|
|
219
|
+
let map = TableMap.get($cell.node(-1)), start = $cell.start(-1);
|
|
220
|
+
let index = map.map.indexOf($cell.pos - start);
|
|
221
|
+
return index % map.width === 0 ? -1 : start + map.map[index - 1];
|
|
222
|
+
}
|
|
223
|
+
function draggedWidth(dragging, event, cellMinWidth) {
|
|
224
|
+
let offset = event.clientX - dragging.startX;
|
|
225
|
+
return Math.max(cellMinWidth, dragging.startWidth + offset);
|
|
226
|
+
}
|
|
227
|
+
function updateHandle(view, value) {
|
|
228
|
+
view.dispatch(view.state.tr.setMeta(key, { setHandle: value }));
|
|
229
|
+
}
|
|
230
|
+
function updateColumnWidth(view, cell, _width) {
|
|
231
|
+
let $cell = view.state.doc.resolve(cell);
|
|
232
|
+
let tableNode = $cell.node(-1), start = $cell.start(-1);
|
|
233
|
+
let tr = view.state.tr;
|
|
234
|
+
const tablePos = $cell.posAtIndex(0, $cell.depth - 1) - 1;
|
|
235
|
+
const tableDom = view.nodeDOM(start).closest('table');
|
|
236
|
+
let attrs = tableNode.attrs;
|
|
237
|
+
if (tableNode && attrs[colgroupAttr]) {
|
|
238
|
+
const colgroup = tableDom.firstChild;
|
|
239
|
+
attrs = Object.assign({}, attrs, { [colgroupAttr]: colgroup.outerHTML });
|
|
240
|
+
}
|
|
241
|
+
const tableDomWidth = tableDom.style.width;
|
|
242
|
+
if (tableDom && tableDomWidth && parseStyle(attrs.style).width !== tableDomWidth) {
|
|
243
|
+
attrs = setNodeStyle(attrs, 'width', tableDomWidth);
|
|
244
|
+
tr.setNodeMarkup(tablePos, null, attrs);
|
|
245
|
+
}
|
|
246
|
+
if (tr.docChanged) {
|
|
247
|
+
view.dispatch(tr);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function displayColumnWidth(view, cell, width, _cellMinWidth) {
|
|
251
|
+
let $cell = view.state.doc.resolve(cell);
|
|
252
|
+
let table = $cell.node(-1), start = $cell.start(-1);
|
|
253
|
+
let col = TableMap.get(table).colCount($cell.pos - start) + $cell.nodeAfter.attrs.colspan - 1;
|
|
254
|
+
let dom = view.domAtPos($cell.start(-1)).node;
|
|
255
|
+
if (dom.nodeName !== 'TABLE') {
|
|
256
|
+
dom = dom.closest('table');
|
|
257
|
+
}
|
|
258
|
+
const tableDom = dom;
|
|
259
|
+
const colgroup = tableDom.firstChild;
|
|
260
|
+
const cols = Array.from(colgroup.children);
|
|
261
|
+
cols[col].style.width = width + 'px';
|
|
262
|
+
if (tableDom.style.width) {
|
|
263
|
+
const widths = cols.map(c => c.style.width);
|
|
264
|
+
if (widths.every(Boolean)) {
|
|
265
|
+
const sum = widths.reduce((acc, cur) => acc + parseFloat(cur), 0);
|
|
266
|
+
tableDom.style.width = sum + 'px';
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function handleDecorations(state, cell) {
|
|
271
|
+
let decorations = [];
|
|
272
|
+
let $cell = state.doc.resolve(cell);
|
|
273
|
+
let table = $cell.node(-1), map = TableMap.get(table), start = $cell.start(-1);
|
|
274
|
+
let col = map.colCount($cell.pos - start) + $cell.nodeAfter.attrs.colspan;
|
|
275
|
+
for (let row = 0; row < map.height; row++) {
|
|
276
|
+
let index = col + row * map.width - 1;
|
|
277
|
+
// For positions that are have either a different cell or the end
|
|
278
|
+
// of the table to their right, and either the top of the table or
|
|
279
|
+
// a different cell above them, add a decoration
|
|
280
|
+
if ((col === map.width || map.map[index] !== map.map[index + 1]) &&
|
|
281
|
+
(row === 0 || map.map[index - 1] !== map.map[index - 1 - map.width])) {
|
|
282
|
+
let cellPos = map.map[index];
|
|
283
|
+
let pos = start + cellPos + table.nodeAt(cellPos).nodeSize - 1;
|
|
284
|
+
let dom = document.createElement('div');
|
|
285
|
+
dom.className = 'column-resize-handle';
|
|
286
|
+
decorations.push(Decoration.widget(pos, dom));
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return DecorationSet.create(state.doc, decorations);
|
|
290
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
import { TableMap, tableNodeTypes } from 'prosemirror-tables';
|
|
3
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
4
|
+
import { otherResizeHandle, otherResizing, parseStyle, setNodeStyle, tableRowResizing as key } from './utils';
|
|
5
|
+
class TableRowView {
|
|
6
|
+
ignoreMutation(record) {
|
|
7
|
+
return record.type === 'attributes' && record.attributeName === 'style' && record.target.nodeName === 'TR';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function rowResizing() {
|
|
11
|
+
const handleWidth = 5, rowMinHeight = 25, lastRowResizable = true;
|
|
12
|
+
let plugin = new Plugin({
|
|
13
|
+
key,
|
|
14
|
+
state: {
|
|
15
|
+
init(_, state) {
|
|
16
|
+
this.spec.props.nodeViews[tableNodeTypes(state.schema).row.name] = (_node, _view) => new TableRowView();
|
|
17
|
+
return new ResizeState(-1, null);
|
|
18
|
+
},
|
|
19
|
+
apply(tr, prev) {
|
|
20
|
+
return prev.apply(tr);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
props: {
|
|
24
|
+
attributes(state) {
|
|
25
|
+
if (otherResizeHandle(key, state)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
let pluginState = key.getState(state);
|
|
29
|
+
return pluginState.activeHandle > -1 ? { class: 'resize-cursor-vertical' } : null;
|
|
30
|
+
},
|
|
31
|
+
handleDOMEvents: {
|
|
32
|
+
mousemove(view, event) {
|
|
33
|
+
if (!otherResizing(key, view.state)) {
|
|
34
|
+
handleMouseMove(view, event, handleWidth, rowMinHeight, lastRowResizable);
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
},
|
|
38
|
+
mouseleave(view) {
|
|
39
|
+
handleMouseLeave(view);
|
|
40
|
+
return false;
|
|
41
|
+
},
|
|
42
|
+
mousedown(view, event) {
|
|
43
|
+
return handleMouseDown(view, event, rowMinHeight);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
decorations(state) {
|
|
47
|
+
if (otherResizing(key, state)) {
|
|
48
|
+
return DecorationSet.empty;
|
|
49
|
+
}
|
|
50
|
+
let pluginState = key.getState(state);
|
|
51
|
+
if (pluginState.activeHandle > -1) {
|
|
52
|
+
return handleDecorations(state, pluginState.activeHandle);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
nodeViews: {}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return plugin;
|
|
59
|
+
}
|
|
60
|
+
function pointsAtCell($pos) {
|
|
61
|
+
return $pos.parent.type.spec.tableRole === 'row' && $pos.nodeAfter;
|
|
62
|
+
}
|
|
63
|
+
class ResizeState {
|
|
64
|
+
constructor(activeHandle, dragging) {
|
|
65
|
+
this.activeHandle = activeHandle;
|
|
66
|
+
this.dragging = dragging;
|
|
67
|
+
}
|
|
68
|
+
apply(tr) {
|
|
69
|
+
let state = this, action = tr.getMeta(key);
|
|
70
|
+
if (action && action.setHandle != null) {
|
|
71
|
+
return new ResizeState(action.setHandle, null);
|
|
72
|
+
}
|
|
73
|
+
if (action && action.setDragging !== undefined) {
|
|
74
|
+
return new ResizeState(state.activeHandle, action.setDragging);
|
|
75
|
+
}
|
|
76
|
+
if (state.activeHandle > -1) { // && tr.docChanged
|
|
77
|
+
let handle = tr.mapping.map(state.activeHandle, -1);
|
|
78
|
+
if (!pointsAtCell(tr.doc.resolve(handle))) {
|
|
79
|
+
handle = null;
|
|
80
|
+
}
|
|
81
|
+
state = new ResizeState(handle, state.dragging);
|
|
82
|
+
}
|
|
83
|
+
return state;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function handleMouseMove(view, event, handleWidth, _rowMinHeight, lastRowResizable) {
|
|
87
|
+
let pluginState = key.getState(view.state);
|
|
88
|
+
if (!pluginState.dragging) {
|
|
89
|
+
let target = domCellAround(event.target), cell = -1;
|
|
90
|
+
if (target) {
|
|
91
|
+
const rowDom = target.parentNode;
|
|
92
|
+
let domRect = rowDom.getBoundingClientRect();
|
|
93
|
+
if (event.clientY - domRect.top <= handleWidth) {
|
|
94
|
+
cell = edgeCell(view, event, 'up');
|
|
95
|
+
}
|
|
96
|
+
else if (domRect.bottom - event.clientY <= handleWidth) {
|
|
97
|
+
cell = edgeCell(view, event, 'down');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (cell !== pluginState.activeHandle) {
|
|
101
|
+
if (!lastRowResizable && cell !== -1) {
|
|
102
|
+
// let $cell = view.state.doc.resolve(cell);
|
|
103
|
+
// let table = $cell.node(-1), map = TableMap.get(table), start = $cell.start(-1);
|
|
104
|
+
// let col = map.colCount($cell.pos - start) + $cell.nodeAfter.attrs.colspan - 1;
|
|
105
|
+
// if (col === map.width - 1) {
|
|
106
|
+
// return;
|
|
107
|
+
// }
|
|
108
|
+
}
|
|
109
|
+
updateHandle(view, cell);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function handleMouseLeave(view) {
|
|
114
|
+
let pluginState = key.getState(view.state);
|
|
115
|
+
if (pluginState.activeHandle > -1 && !pluginState.dragging) {
|
|
116
|
+
updateHandle(view, -1);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function handleMouseDown(view, event, rowMinHeight) {
|
|
120
|
+
let pluginState = key.getState(view.state);
|
|
121
|
+
if (pluginState.activeHandle === -1 || pluginState.dragging) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const doc = view.state.doc;
|
|
125
|
+
let $pos = doc.resolve(pluginState.activeHandle);
|
|
126
|
+
let row = doc.nodeAt(pluginState.activeHandle);
|
|
127
|
+
let table = $pos.parent;
|
|
128
|
+
let rowHeightStr = parseStyle(row.attrs.style).height;
|
|
129
|
+
let tableHeight = parseStyle(table.attrs.style).height;
|
|
130
|
+
let rowHeight = rowHeightStr ? parseFloat(rowHeightStr) : 0;
|
|
131
|
+
if (!rowHeightStr) {
|
|
132
|
+
const tr = view.nodeDOM(pluginState.activeHandle);
|
|
133
|
+
rowHeight = tr.offsetHeight;
|
|
134
|
+
}
|
|
135
|
+
view.dispatch(view.state.tr.setMeta(key, {
|
|
136
|
+
setDragging: {
|
|
137
|
+
startY: event.clientY,
|
|
138
|
+
startHeight: { rowHeight, tableHeight }
|
|
139
|
+
}
|
|
140
|
+
}));
|
|
141
|
+
function finish(ev) {
|
|
142
|
+
ev.view.removeEventListener('mouseup', finish);
|
|
143
|
+
ev.view.removeEventListener('mousemove', move);
|
|
144
|
+
let curPluginState = key.getState(view.state);
|
|
145
|
+
if (curPluginState.dragging) {
|
|
146
|
+
const tr = view.state.tr.setMeta(key, { setDragging: null });
|
|
147
|
+
updateRowHeight(view, tr, curPluginState.activeHandle, draggedHeight(curPluginState.dragging, ev, rowMinHeight));
|
|
148
|
+
view.dispatch(tr);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function move(ev) {
|
|
152
|
+
if (!ev.which) {
|
|
153
|
+
return finish(ev);
|
|
154
|
+
}
|
|
155
|
+
let curPluginState = key.getState(view.state);
|
|
156
|
+
let dragged = draggedHeight(curPluginState.dragging, ev, rowMinHeight);
|
|
157
|
+
let offset = ev.clientY - curPluginState.dragging.startY;
|
|
158
|
+
displayRowHeight(view, curPluginState.activeHandle, dragged, rowMinHeight, offset, tableHeight);
|
|
159
|
+
}
|
|
160
|
+
event.view.addEventListener('mouseup', finish);
|
|
161
|
+
event.view.addEventListener('mousemove', move);
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
function domCellAround(target) {
|
|
166
|
+
while (target && target.nodeName !== 'TD' && target.nodeName !== 'TH') {
|
|
167
|
+
target = target.classList.contains('ProseMirror') ? null : target.parentNode;
|
|
168
|
+
}
|
|
169
|
+
return target;
|
|
170
|
+
}
|
|
171
|
+
function edgeCell(view, event, side) {
|
|
172
|
+
let found = view.posAtCoords({ left: event.clientX, top: event.clientY });
|
|
173
|
+
if (!found) {
|
|
174
|
+
return -1;
|
|
175
|
+
}
|
|
176
|
+
let { pos } = found;
|
|
177
|
+
let doc = view.state.doc;
|
|
178
|
+
let $pos = doc.resolve(pos);
|
|
179
|
+
let rowDepth = new Array($pos.depth + 1).fill(0).findIndex((_, i) => $pos.node(i).type.spec.tableRole === 'row');
|
|
180
|
+
if (rowDepth === -1) {
|
|
181
|
+
return -1;
|
|
182
|
+
}
|
|
183
|
+
let tableDepth = rowDepth - 1;
|
|
184
|
+
let rowIndex = $pos.index(tableDepth);
|
|
185
|
+
rowIndex += side === 'up' ? -1 : 0;
|
|
186
|
+
if (rowIndex < 0) {
|
|
187
|
+
return -1;
|
|
188
|
+
}
|
|
189
|
+
let rowPos = $pos.posAtIndex(rowIndex, tableDepth);
|
|
190
|
+
return rowPos;
|
|
191
|
+
}
|
|
192
|
+
function draggedHeight(dragging, event, rowMinHeight) {
|
|
193
|
+
let offset = event.clientY - dragging.startY;
|
|
194
|
+
return Math.max(rowMinHeight, dragging.startHeight.rowHeight + offset);
|
|
195
|
+
}
|
|
196
|
+
function updateHandle(view, value) {
|
|
197
|
+
view.dispatch(view.state.tr.setMeta(key, { setHandle: value }));
|
|
198
|
+
}
|
|
199
|
+
function updateRowHeight(view, tr, rowPos, height) {
|
|
200
|
+
const doc = view.state.doc;
|
|
201
|
+
let row = doc.nodeAt(rowPos);
|
|
202
|
+
tr.setNodeMarkup(rowPos, null, setNodeStyle(row.attrs, 'height', height + 'px'));
|
|
203
|
+
const dom = view.nodeDOM(rowPos);
|
|
204
|
+
const table = dom && dom.closest('table');
|
|
205
|
+
const tableHeight = table && table.style.height;
|
|
206
|
+
if (tableHeight) {
|
|
207
|
+
let $pos = doc.resolve(rowPos);
|
|
208
|
+
let tablePos = $pos.start($pos.depth) - 1;
|
|
209
|
+
tr.setNodeMarkup(tablePos, null, setNodeStyle($pos.parent.attrs, 'height', tableHeight));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// tslint:disable-next-line:max-line-length
|
|
213
|
+
function displayRowHeight(view, rowPos, height, rowMinHeight, offset, tableHeight) {
|
|
214
|
+
const dom = view.nodeDOM(rowPos);
|
|
215
|
+
if (dom) {
|
|
216
|
+
dom.style.height = Math.max(height, rowMinHeight) + 'px';
|
|
217
|
+
const table = dom.closest('table');
|
|
218
|
+
const newHeight = (parseFloat(tableHeight) + offset) + 'px';
|
|
219
|
+
const current = table && table.style.height;
|
|
220
|
+
if (current && current !== newHeight) {
|
|
221
|
+
table.style.height = (parseFloat(tableHeight) + offset) + 'px';
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function handleDecorations(state, pos) {
|
|
226
|
+
let decorations = [];
|
|
227
|
+
if (typeof pos !== 'number') {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
let $row = state.doc.resolve(pos), table = $row.parent, map = TableMap.get(table), rowIndex = $row.index($row.depth), start = $row.start($row.depth);
|
|
231
|
+
for (let col = 0; col < map.width; col++) {
|
|
232
|
+
let index = col + rowIndex * map.width;
|
|
233
|
+
let cellPos = map.map[index];
|
|
234
|
+
let widgetPos = start + cellPos + table.nodeAt(cellPos).nodeSize - 1;
|
|
235
|
+
let dom = document.createElement('div');
|
|
236
|
+
dom.className = 'row-resize-handle';
|
|
237
|
+
decorations.push(Decoration.widget(widgetPos, dom));
|
|
238
|
+
}
|
|
239
|
+
return DecorationSet.create(state.doc, decorations);
|
|
240
|
+
}
|