forked from microbit-foundation/python-editor-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnd.ts
249 lines (228 loc) · 6.62 KB
/
dnd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* (c) 2022, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import { ChangeSet, Extension, Transaction } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { deployment } from "../../deployment";
import { flags } from "../../flags";
import { SessionSettings } from "../../settings/session-settings";
import { dndDecorations } from "./dnd-decorations";
import "./dnd.css";
import { calculateChanges } from "./edits";
export const debug = (message: string, ...args: any) => {
if (flags.dndDebug) {
console.log(message, ...args);
}
};
/**
* Information stashed last time we handled dragover.
* Cleared on drop or dragleave.
*/
interface LastDragPos {
/**
* The last drag position.
*/
logicalPosition: LogicalPosition;
/**
* The inverse set of changes to the changes made for preview.
*/
previewUndo: ChangeSet;
}
interface LogicalPosition {
line: number;
indent: number | undefined;
}
export type CodeInsertType =
/**
* A potentially multi-line example snippet.
*/
| "example"
/**
* A function call.
*/
| "call";
export interface DragContext {
code: string;
type: CodeInsertType;
id?: string;
}
let dragContext: DragContext | undefined;
/**
* Set the dragged code.
*
* There's no access to the content via the event in dragover (as it may be cross-document),
* we use that event to draw a preview, so we need shared state with the drag.
*
* Set it in dragstart and clear it in dragend.
*/
export const setDragContext = (context: DragContext | undefined) => {
dragContext = context;
};
// We add the class to the parent element that we own as otherwise CM
// will remove it when it re-renders. Might be worth replacing this
// with a CM compartment with the style.
const findWrappingSection = (view: EditorView) => {
let e: HTMLElement | null = view.contentDOM;
while (e && e.localName !== "section") {
e = e.parentElement;
}
if (!e) {
throw new Error("Unexpected DOM structure");
}
return e;
};
const suppressChildDragEnterLeave = (view: EditorView) => {
findWrappingSection(view).classList.add("cm-drag-in-progress");
};
const clearSuppressChildDragEnterLeave = (view: EditorView) => {
findWrappingSection(view).classList.remove("cm-drag-in-progress");
};
const dndHandlers = ({ sessionSettings, setSessionSettings }: DragTracker) => {
let lastDragPos: LastDragPos | undefined;
const revertPreview = (view: EditorView) => {
if (lastDragPos) {
view.dispatch({
userEvent: "dnd.cleanup",
changes: lastDragPos.previewUndo,
annotations: [Transaction.addToHistory.of(false)],
});
lastDragPos = undefined;
}
};
return [
EditorView.domEventHandlers({
dragover(event, view) {
if (!view.state.facet(EditorView.editable)) {
return;
}
if (dragContext) {
event.preventDefault();
const logicalPosition = findLogicalPosition(view, event);
if (
logicalPosition.line !== lastDragPos?.logicalPosition.line ||
logicalPosition.indent !== lastDragPos?.logicalPosition.indent
) {
debug(" dragover", logicalPosition);
revertPreview(view);
const transaction = calculateChanges(
view.state,
dragContext.code,
dragContext.type,
logicalPosition.line,
logicalPosition.indent
);
lastDragPos = {
logicalPosition,
previewUndo: transaction.changes.invert(view.state.doc),
};
// Take just the changes, skip the selection updates we perform on drop.
view.dispatch({
userEvent: "dnd.preview",
changes: transaction.changes,
annotations: [Transaction.addToHistory.of(false)],
});
}
}
},
dragenter(event, view) {
if (!view.state.facet(EditorView.editable) || !dragContext) {
return;
}
debug("dragenter");
event.preventDefault();
suppressChildDragEnterLeave(view);
},
dragleave(event, view) {
if (!view.state.facet(EditorView.editable) || !dragContext) {
return;
}
if (event.target === view.contentDOM) {
event.preventDefault();
clearSuppressChildDragEnterLeave(view);
revertPreview(view);
debug(
" dragleave",
{
x: event.clientX,
y: event.clientY,
},
event.target
);
} else {
debug(
" dragleave (ignored)",
{
x: event.clientX,
y: event.clientY,
},
event.target
);
}
},
drop(event, view) {
if (!view.state.facet(EditorView.editable) || !dragContext) {
return;
}
deployment.logging.event({
type: "code-drop",
message: dragContext.id,
});
if (!sessionSettings.dragDropSuccess) {
setSessionSettings({
...sessionSettings,
dragDropSuccess: true,
});
}
debug(" drop");
clearSuppressChildDragEnterLeave(view);
event.preventDefault();
const logicalPosition = findLogicalPosition(view, event);
revertPreview(view);
view.dispatch(
calculateChanges(
view.state,
dragContext.code,
dragContext.type,
logicalPosition.line,
logicalPosition.indent,
false
)
);
view.focus();
},
}),
];
};
const findLogicalPosition = (
view: EditorView,
event: DragEvent
): { line: number; indent: number | undefined } => {
const height = (event.y || event.clientY) - view.documentTop;
const visualLine = view.lineBlockAtHeight(height);
const line = view.state.doc.lineAt(visualLine.from);
const pos = view.posAtCoords({
x: event.x || event.clientX,
y: event.y || event.clientY,
});
const column = pos !== null ? pos - visualLine.from : undefined;
const indent = column !== undefined ? Math.floor(column / 4) : undefined;
return {
line: line.number,
indent,
};
};
interface DragTracker {
sessionSettings: SessionSettings;
setSessionSettings: (sessionSettings: SessionSettings) => void;
}
/**
* Support for dropping code snippets.
*
* Note this requires coordination from the drag end via {@link setDraggedCode}.
*/
export const dndSupport = (dragTracker: DragTracker): Extension => [
dndHandlers(dragTracker),
dndDecorations(),
];