Skip to content

Commit 1028ca7

Browse files
authored
Added basics for notebook cell drag image renderers (#13698)
* added basics for drag image renderers and improved basic drag image slightly Signed-off-by: Jonah Iden <[email protected]> * fix lint Signed-off-by: Jonah Iden <[email protected]> * reiew chages Signed-off-by: Jonah Iden <[email protected]> --------- Signed-off-by: Jonah Iden <[email protected]>
1 parent 767311b commit 1028ca7

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/notebook/src/browser/style/index.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,11 @@
281281
line-height: 22px;
282282
opacity: 0.7;
283283
}
284+
285+
.theia-notebook-drag-ghost-image {
286+
position: absolute;
287+
top: -99999px;
288+
left: -99999px;
289+
height: 500px;
290+
width: 500px;
291+
}

packages/notebook/src/browser/view/notebook-cell-list-view.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { NotebookCellActionContribution } from '../contributions/notebook-cell-a
2525

2626
export interface CellRenderer {
2727
render(notebookData: NotebookModel, cell: NotebookCellModel, index: number): React.ReactNode
28+
renderDragImage(cell: NotebookCellModel): HTMLElement
2829
}
2930

3031
interface CellListProps {
@@ -43,6 +44,8 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
4344

4445
protected toDispose = new DisposableCollection();
4546

47+
protected dragGhost: HTMLElement | undefined;
48+
4649
constructor(props: CellListProps) {
4750
super(props);
4851
this.state = { selectedCell: props.notebookModel.selectedCell, dragOverIndicator: undefined };
@@ -79,7 +82,7 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
7982
this.setState({ ...this.state, selectedCell: cell });
8083
this.props.notebookModel.setSelectedCell(cell);
8184
}}
82-
onDragStart={e => this.onDragStart(e, index)}
85+
onDragStart={e => this.onDragStart(e, index, cell)}
8386
onDragOver={e => this.onDragOver(e, cell)}
8487
onDrop={e => this.onDrop(e, index)}
8588
draggable={true}
@@ -114,12 +117,22 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
114117
return renderer.render(this.props.notebookModel, cell, index);
115118
}
116119

117-
protected onDragStart(event: React.DragEvent<HTMLLIElement>, index: number): void {
120+
protected onDragStart(event: React.DragEvent<HTMLLIElement>, index: number, cell: NotebookCellModel): void {
118121
event.stopPropagation();
119122
if (!this.isEnabled()) {
120123
event.preventDefault();
121124
return;
122125
}
126+
127+
if (this.dragGhost) {
128+
this.dragGhost.remove();
129+
}
130+
this.dragGhost = document.createElement('div');
131+
this.dragGhost.classList.add('theia-notebook-drag-ghost-image');
132+
this.dragGhost.appendChild(this.props.renderers.get(cell.cellKind)?.renderDragImage(cell) ?? document.createElement('div'));
133+
document.body.appendChild(this.dragGhost);
134+
event.dataTransfer.setDragImage(this.dragGhost, -10, 0);
135+
123136
event.dataTransfer.setData('text/theia-notebook-cell-index', index.toString());
124137
event.dataTransfer.setData('text/plain', this.props.notebookModel.cells[index].source);
125138
}

packages/notebook/src/browser/view/notebook-code-cell-view.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ export class NotebookCodeCellRenderer implements CellRenderer {
9999
</div >;
100100
}
101101

102+
renderDragImage(cell: NotebookCellModel): HTMLElement {
103+
const dragImage = document.createElement('div');
104+
dragImage.className = 'theia-notebook-drag-image';
105+
dragImage.textContent = nls.localize('theia/notebook/dragGhostImage/codeText', 'Code cell selected');
106+
return dragImage;
107+
}
108+
102109
protected getOrCreateMonacoFontInfo(): BareFontInfo {
103110
if (!this.fontInfo) {
104111
this.fontInfo = this.createFontInfo();

packages/notebook/src/browser/view/notebook-markdown-cell-view.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ export class NotebookMarkdownCellRenderer implements CellRenderer {
4242
cell={cell} notebookModel={notebookModel} notebookContextManager={this.notebookContextManager} />;
4343
}
4444

45+
renderDragImage(cell: NotebookCellModel): HTMLElement {
46+
const dragImage = document.createElement('div');
47+
dragImage.className = 'theia-notebook-drag-image';
48+
dragImage.textContent = nls.localize('theia/notebook/dragGhostImage/markdownText', 'Mardown cell selected');
49+
return dragImage;
50+
}
4551
}
4652

4753
interface MarkdownCellProps {

0 commit comments

Comments
 (0)