Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/cellDnd.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { performCellDropEdits } from '../../browser/view/cellParts/cellDnd.js';
7
import { CellKind } from '../../common/notebookCommon.js';
8
import { withTestNotebook } from './testNotebookEditor.js';
9
import assert from 'assert';
10
import { ICellRange } from '../../common/notebookRange.js';
11
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
12
13
interface IBeginningState {
14
startOrder: string[];
15
selections: ICellRange[];
16
focus: number;
17
}
18
19
interface IDragAction {
20
dragIdx: number;
21
dragOverIdx: number;
22
direction: 'above' | 'below';
23
}
24
25
interface IEndState {
26
endOrder: string[];
27
selection: ICellRange;
28
focus: number;
29
}
30
31
async function testCellDnd(beginning: IBeginningState, dragAction: IDragAction, end: IEndState) {
32
await withTestNotebook(
33
beginning.startOrder.map(text => [text, 'plaintext', CellKind.Code, []]),
34
(editor, viewModel) => {
35
editor.setSelections(beginning.selections);
36
editor.setFocus({ start: beginning.focus, end: beginning.focus + 1 });
37
performCellDropEdits(editor, viewModel.cellAt(dragAction.dragIdx)!, dragAction.direction, viewModel.cellAt(dragAction.dragOverIdx)!);
38
39
for (const i in end.endOrder) {
40
assert.equal(viewModel.viewCells[i].getText(), end.endOrder[i]);
41
}
42
43
assert.equal(editor.getSelections().length, 1);
44
assert.deepStrictEqual(editor.getSelections()[0], end.selection);
45
assert.deepStrictEqual(editor.getFocus(), { start: end.focus, end: end.focus + 1 });
46
});
47
}
48
49
suite('cellDND', () => {
50
ensureNoDisposablesAreLeakedInTestSuite();
51
52
test('drag 1 cell', async () => {
53
await testCellDnd(
54
{
55
startOrder: ['0', '1', '2', '3'],
56
selections: [{ start: 0, end: 1 }],
57
focus: 0
58
},
59
{
60
dragIdx: 0,
61
dragOverIdx: 1,
62
direction: 'below'
63
},
64
{
65
endOrder: ['1', '0', '2', '3'],
66
selection: { start: 1, end: 2 },
67
focus: 1
68
}
69
);
70
});
71
72
test('drag multiple contiguous cells down', async () => {
73
await testCellDnd(
74
{
75
startOrder: ['0', '1', '2', '3'],
76
selections: [{ start: 1, end: 3 }],
77
focus: 1
78
},
79
{
80
dragIdx: 1,
81
dragOverIdx: 3,
82
direction: 'below'
83
},
84
{
85
endOrder: ['0', '3', '1', '2'],
86
selection: { start: 2, end: 4 },
87
focus: 2
88
}
89
);
90
});
91
92
test('drag multiple contiguous cells up', async () => {
93
await testCellDnd(
94
{
95
startOrder: ['0', '1', '2', '3'],
96
selections: [{ start: 2, end: 4 }],
97
focus: 2
98
},
99
{
100
dragIdx: 3,
101
dragOverIdx: 0,
102
direction: 'above'
103
},
104
{
105
endOrder: ['2', '3', '0', '1'],
106
selection: { start: 0, end: 2 },
107
focus: 0
108
}
109
);
110
});
111
112
test('drag ranges down', async () => {
113
await testCellDnd(
114
{
115
startOrder: ['0', '1', '2', '3'],
116
selections: [{ start: 0, end: 1 }, { start: 2, end: 3 }],
117
focus: 0
118
},
119
{
120
dragIdx: 0,
121
dragOverIdx: 3,
122
direction: 'below'
123
},
124
{
125
endOrder: ['1', '3', '0', '2'],
126
selection: { start: 2, end: 4 },
127
focus: 2
128
}
129
);
130
});
131
132
test('drag ranges up', async () => {
133
await testCellDnd(
134
{
135
startOrder: ['0', '1', '2', '3'],
136
selections: [{ start: 1, end: 2 }, { start: 3, end: 4 }],
137
focus: 1
138
},
139
{
140
dragIdx: 1,
141
dragOverIdx: 0,
142
direction: 'above'
143
},
144
{
145
endOrder: ['1', '3', '0', '2'],
146
selection: { start: 0, end: 2 },
147
focus: 0
148
}
149
);
150
});
151
152
test('drag ranges between ranges', async () => {
153
await testCellDnd(
154
{
155
startOrder: ['0', '1', '2', '3'],
156
selections: [{ start: 0, end: 1 }, { start: 3, end: 4 }],
157
focus: 0
158
},
159
{
160
dragIdx: 0,
161
dragOverIdx: 1,
162
direction: 'below'
163
},
164
{
165
endOrder: ['1', '0', '3', '2'],
166
selection: { start: 1, end: 3 },
167
focus: 1
168
}
169
);
170
});
171
172
test('drag ranges just above a range', async () => {
173
await testCellDnd(
174
{
175
startOrder: ['0', '1', '2', '3'],
176
selections: [{ start: 1, end: 2 }, { start: 3, end: 4 }],
177
focus: 1
178
},
179
{
180
dragIdx: 1,
181
dragOverIdx: 1,
182
direction: 'above'
183
},
184
{
185
endOrder: ['0', '1', '3', '2'],
186
selection: { start: 1, end: 3 },
187
focus: 1
188
}
189
);
190
});
191
192
test('drag ranges inside a range', async () => {
193
await testCellDnd(
194
{
195
startOrder: ['0', '1', '2', '3'],
196
selections: [{ start: 0, end: 2 }, { start: 3, end: 4 }],
197
focus: 0
198
},
199
{
200
dragIdx: 0,
201
dragOverIdx: 0,
202
direction: 'below'
203
},
204
{
205
endOrder: ['0', '1', '3', '2'],
206
selection: { start: 0, end: 3 },
207
focus: 0
208
}
209
);
210
});
211
212
test('dragged cell is not focused or selected', async () => {
213
await testCellDnd(
214
{
215
startOrder: ['0', '1', '2', '3'],
216
selections: [{ start: 1, end: 2 }],
217
focus: 1
218
},
219
{
220
dragIdx: 2,
221
dragOverIdx: 3,
222
direction: 'below'
223
},
224
{
225
endOrder: ['0', '1', '3', '2'],
226
selection: { start: 3, end: 4 },
227
focus: 3
228
}
229
);
230
});
231
});
232
233