UNPKG

5.96 kBJavaScriptView Raw
1/* -----------------------------------------------------------------------------
2| Copyright (c) Jupyter Development Team.
3| Distributed under the terms of the Modified BSD License.
4|----------------------------------------------------------------------------*/
5/**
6 * This module contains some utility functions to operate on cells. This
7 * could be shared by widgets that contain cells, like the CodeConsole or
8 * Notebook widgets.
9 */
10import { each } from '@lumino/algorithm';
11import { h, VirtualDOM } from '@lumino/virtualdom';
12/**
13 * Constants for drag
14 */
15/**
16 * The threshold in pixels to start a drag event.
17 */
18const DRAG_THRESHOLD = 5;
19/**
20 * The class name added to drag images.
21 */
22const DRAG_IMAGE_CLASS = 'jp-dragImage';
23/**
24 * The class name added to singular drag images
25 */
26const SINGLE_DRAG_IMAGE_CLASS = 'jp-dragImage-singlePrompt';
27/**
28 * The class name added to the drag image cell content.
29 */
30const CELL_DRAG_CONTENT_CLASS = 'jp-dragImage-content';
31/**
32 * The class name added to the drag image cell content.
33 */
34const CELL_DRAG_PROMPT_CLASS = 'jp-dragImage-prompt';
35/**
36 * The class name added to the drag image cell content.
37 */
38const CELL_DRAG_MULTIPLE_BACK = 'jp-dragImage-multipleBack';
39export var CellDragUtils;
40(function (CellDragUtils) {
41 /**
42 * Find the cell index containing the target html element.
43 * This function traces up the DOM hierarchy to find the root cell
44 * node. Then find the corresponding child and select it.
45 *
46 * @param node - the cell node or a child of the cell node.
47 * @param cells - an iterable of Cells
48 * @param isCellNode - a function that takes in a node and checks if
49 * it is a cell node.
50 *
51 * @returns index of the cell we're looking for. Returns -1 if
52 * the cell is not founds
53 */
54 function findCell(node, cells, isCellNode) {
55 let cellIndex = -1;
56 while (node && node.parentElement) {
57 if (isCellNode(node)) {
58 each(cells, (cell, index) => {
59 if (cell.node === node) {
60 cellIndex = index;
61 return false;
62 }
63 });
64 break;
65 }
66 node = node.parentElement;
67 }
68 return cellIndex;
69 }
70 CellDragUtils.findCell = findCell;
71 /**
72 * Detect which part of the cell triggered the MouseEvent
73 *
74 * @param cell - The cell which contains the MouseEvent's target
75 * @param target - The DOM node which triggered the MouseEvent
76 */
77 function detectTargetArea(cell, target) {
78 let targetArea;
79 if (cell) {
80 if (cell.editorWidget.node.contains(target)) {
81 targetArea = 'input';
82 }
83 else if (cell.promptNode.contains(target)) {
84 targetArea = 'prompt';
85 }
86 else {
87 targetArea = 'cell';
88 }
89 }
90 else {
91 targetArea = 'unknown';
92 }
93 return targetArea;
94 }
95 CellDragUtils.detectTargetArea = detectTargetArea;
96 /**
97 * Detect if a drag event should be started. This is down if the
98 * mouse is moved beyond a certain distance (DRAG_THRESHOLD).
99 *
100 * @param prevX - X Coordinate of the mouse pointer during the mousedown event
101 * @param prevY - Y Coordinate of the mouse pointer during the mousedown event
102 * @param nextX - Current X Coordinate of the mouse pointer
103 * @param nextY - Current Y Coordinate of the mouse pointer
104 */
105 function shouldStartDrag(prevX, prevY, nextX, nextY) {
106 const dx = Math.abs(nextX - prevX);
107 const dy = Math.abs(nextY - prevY);
108 return dx >= DRAG_THRESHOLD || dy >= DRAG_THRESHOLD;
109 }
110 CellDragUtils.shouldStartDrag = shouldStartDrag;
111 /**
112 * Create an image for the cell(s) to be dragged
113 *
114 * @param activeCell - The cell from where the drag event is triggered
115 * @param selectedCells - The cells to be dragged
116 */
117 function createCellDragImage(activeCell, selectedCells) {
118 const count = selectedCells.length;
119 let promptNumber;
120 if (activeCell.model.type === 'code') {
121 const executionCount = activeCell.model
122 .executionCount;
123 promptNumber = ' ';
124 if (executionCount) {
125 promptNumber = executionCount.toString();
126 }
127 }
128 else {
129 promptNumber = '';
130 }
131 const cellContent = activeCell.model.value.text.split('\n')[0].slice(0, 26);
132 if (count > 1) {
133 if (promptNumber !== '') {
134 return VirtualDOM.realize(h.div(h.div({ className: DRAG_IMAGE_CLASS }, h.span({ className: CELL_DRAG_PROMPT_CLASS }, '[' + promptNumber + ']:'), h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent)), h.div({ className: CELL_DRAG_MULTIPLE_BACK }, '')));
135 }
136 else {
137 return VirtualDOM.realize(h.div(h.div({ className: DRAG_IMAGE_CLASS }, h.span({ className: CELL_DRAG_PROMPT_CLASS }), h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent)), h.div({ className: CELL_DRAG_MULTIPLE_BACK }, '')));
138 }
139 }
140 else {
141 if (promptNumber !== '') {
142 return VirtualDOM.realize(h.div(h.div({ className: `${DRAG_IMAGE_CLASS} ${SINGLE_DRAG_IMAGE_CLASS}` }, h.span({ className: CELL_DRAG_PROMPT_CLASS }, '[' + promptNumber + ']:'), h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent))));
143 }
144 else {
145 return VirtualDOM.realize(h.div(h.div({ className: `${DRAG_IMAGE_CLASS} ${SINGLE_DRAG_IMAGE_CLASS}` }, h.span({ className: CELL_DRAG_PROMPT_CLASS }), h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent))));
146 }
147 }
148 }
149 CellDragUtils.createCellDragImage = createCellDragImage;
150})(CellDragUtils || (CellDragUtils = {}));
151//# sourceMappingURL=celldragutils.js.map
\No newline at end of file