Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basics for notebook cell drag image renderers #13698

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/notebook/src/browser/style/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,11 @@
line-height: 22px;
opacity: 0.7;
}

.theia-notebook-drag-ghost-image {
position: absolute;
top: -99999px;
left: -99999px;
height: 500px;
width: 500px;
}
17 changes: 15 additions & 2 deletions packages/notebook/src/browser/view/notebook-cell-list-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { NotebookCellActionContribution } from '../contributions/notebook-cell-a

export interface CellRenderer {
render(notebookData: NotebookModel, cell: NotebookCellModel, index: number): React.ReactNode
renderDragImage(cell: NotebookCellModel): HTMLElement
}

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

protected toDispose = new DisposableCollection();

protected dragGhost: HTMLElement | undefined;

constructor(props: CellListProps) {
super(props);
this.state = { selectedCell: props.notebookModel.selectedCell, dragOverIndicator: undefined };
Expand Down Expand Up @@ -79,7 +82,7 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
this.setState({ ...this.state, selectedCell: cell });
this.props.notebookModel.setSelectedCell(cell);
}}
onDragStart={e => this.onDragStart(e, index)}
onDragStart={e => this.onDragStart(e, index, cell)}
onDragOver={e => this.onDragOver(e, cell)}
onDrop={e => this.onDrop(e, index)}
draggable={true}
Expand Down Expand Up @@ -114,12 +117,22 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
return renderer.render(this.props.notebookModel, cell, index);
}

protected onDragStart(event: React.DragEvent<HTMLLIElement>, index: number): void {
protected onDragStart(event: React.DragEvent<HTMLLIElement>, index: number, cell: NotebookCellModel): void {
event.stopPropagation();
if (!this.isEnabled()) {
event.preventDefault();
return;
}

if (this.dragGhost) {
this.dragGhost.remove();
}
this.dragGhost = document.createElement('div');
this.dragGhost.classList.add('theia-notebook-drag-ghost-image');
this.dragGhost.appendChild(this.props.renderers.get(cell.cellKind)?.renderDragImage(cell) ?? document.createElement('div'));
document.body.appendChild(this.dragGhost);
event.dataTransfer.setDragImage(this.dragGhost, -10, 0);

event.dataTransfer.setData('text/theia-notebook-cell-index', index.toString());
event.dataTransfer.setData('text/plain', this.props.notebookModel.cells[index].source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export class NotebookCodeCellRenderer implements CellRenderer {
</div >;
}

renderDragImage(cell: NotebookCellModel): HTMLElement {
const dragImage = document.createElement('div');
dragImage.className = 'theia-notebook-drag-image';
dragImage.textContent = nls.localize('theia/notebook/dragGhostImage/codeText', 'Code cell selected');
return dragImage;
}

protected getOrCreateMonacoFontInfo(): BareFontInfo {
if (!this.fontInfo) {
this.fontInfo = this.createFontInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export class NotebookMarkdownCellRenderer implements CellRenderer {
cell={cell} notebookModel={notebookModel} notebookContextManager={this.notebookContextManager} />;
}

renderDragImage(cell: NotebookCellModel): HTMLElement {
const dragImage = document.createElement('div');
dragImage.className = 'theia-notebook-drag-image';
dragImage.textContent = nls.localize('theia/notebook/dragGhostImage/markdownText', 'Mardown cell selected');
return dragImage;
}
}

interface MarkdownCellProps {
Expand Down
Loading