Skip to content

Commit 523663d

Browse files
committed
Fix drag and drop bug #120
1 parent cbf2468 commit 523663d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const CODE_SNIPPET_CREATE_NEW_BTN = 'jp-createSnippetBtn';
106106
/**
107107
* The threshold in pixels to start a drag event.
108108
*/
109-
const DRAG_THRESHOLD = 5;
109+
const DRAG_THRESHOLD = 3;
110110

111111
/**
112112
* A class used to indicate a snippet item.
@@ -354,6 +354,9 @@ export class CodeSnippetDisplay extends React.Component<
354354
target.addEventListener('mouseup', this._evtMouseUp, true);
355355
target.addEventListener('mousemove', this.handleDragMove, true);
356356

357+
// since a browser has its own drag'n'drop support for images and some other elements.
358+
target.ondragstart = () => false;
359+
357360
event.preventDefault();
358361
}
359362

@@ -369,6 +372,9 @@ export class CodeSnippetDisplay extends React.Component<
369372
}
370373

371374
private handleDragMove(event: MouseEvent): void {
375+
event.preventDefault();
376+
event.stopPropagation();
377+
372378
const data = this._dragData;
373379

374380
if (
@@ -383,7 +389,12 @@ export class CodeSnippetDisplay extends React.Component<
383389
const idx = (event.target as HTMLElement).id;
384390
const codeSnippet = this.state.codeSnippets[parseInt(idx)];
385391

386-
this.startDrag(data.dragImage, codeSnippet, event.clientX, event.clientY);
392+
void this.startDrag(
393+
data.dragImage,
394+
codeSnippet,
395+
event.clientX,
396+
event.clientY
397+
);
387398
}
388399
}
389400

@@ -404,7 +415,7 @@ export class CodeSnippetDisplay extends React.Component<
404415
): boolean {
405416
const dx = Math.abs(nextX - prevX);
406417
const dy = Math.abs(nextY - prevY);
407-
return dx >= DRAG_THRESHOLD || dy >= DRAG_THRESHOLD;
418+
return dx >= 0 || dy >= DRAG_THRESHOLD;
408419
}
409420

410421
private async startDrag(

src/CodeSnippetWidget.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class CodeSnippetWidget extends ReactWidget {
6767
this.openCodeSnippetEditor.bind(this);
6868
this.updateCodeSnippets.bind(this);
6969
this.codeSnippetManager = CodeSnippetContentsService.getInstance();
70+
this.node.setAttribute('data-lm-dragscroll', 'true');
7071
}
7172

7273
get codeSnippetWidgetModel(): CodeSnippetWidgetModel {

style/index.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
text-overflow: ellipsis;
4343
color: var(--jp-ui-font-color0);
4444
display: flex;
45+
align-items: center;
4546
}
4647

4748
.jp-codeSnippetsContainer-button {
@@ -69,6 +70,15 @@
6970
background-color: var(--jp-layout-color2);
7071
}
7172

73+
#jp-codeSnippet-rename {
74+
background-color: var(--jp-layout-color2);
75+
border: 1px solid var(--jp-layout-color1);
76+
border-radius: 4px;
77+
font-size: var(--jp-ui-font-size1);
78+
box-sizing: border-box;
79+
margin: 0px;
80+
}
81+
7282
.jp-codeSnippet-metadata {
7383
flex-basis: 95%;
7484
width: 100%;

0 commit comments

Comments
 (0)