@jupyterlab/cells 4.0.0-alpha.19 → 4.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,214 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ /**
7
+ * This module contains some utility functions to operate on cells. This
8
+ * could be shared by widgets that contain cells, like the CodeConsole or
9
+ * Notebook widgets.
10
+ */
11
+
12
+ import { ICodeCellModel } from './model';
13
+ import { Cell } from './widget';
14
+ import { h, VirtualDOM } from '@lumino/virtualdom';
15
+ import * as nbformat from '@jupyterlab/nbformat';
16
+
17
+ /**
18
+ * Constants for drag
19
+ */
20
+
21
+ /**
22
+ * The threshold in pixels to start a drag event.
23
+ */
24
+ const DRAG_THRESHOLD = 5;
25
+ /**
26
+ * The class name added to drag images.
27
+ */
28
+ const DRAG_IMAGE_CLASS = 'jp-dragImage';
29
+
30
+ /**
31
+ * The class name added to singular drag images
32
+ */
33
+ const SINGLE_DRAG_IMAGE_CLASS = 'jp-dragImage-singlePrompt';
34
+
35
+ /**
36
+ * The class name added to the drag image cell content.
37
+ */
38
+ const CELL_DRAG_CONTENT_CLASS = 'jp-dragImage-content';
39
+
40
+ /**
41
+ * The class name added to the drag image cell content.
42
+ */
43
+ const CELL_DRAG_PROMPT_CLASS = 'jp-dragImage-prompt';
44
+
45
+ /**
46
+ * The class name added to the drag image cell content.
47
+ */
48
+ const CELL_DRAG_MULTIPLE_BACK = 'jp-dragImage-multipleBack';
49
+
50
+ export namespace CellDragUtils {
51
+ export type ICellTargetArea = 'input' | 'prompt' | 'cell' | 'unknown';
52
+
53
+ /**
54
+ * Find the cell index containing the target html element.
55
+ * This function traces up the DOM hierarchy to find the root cell
56
+ * node. Then find the corresponding child and select it.
57
+ *
58
+ * @param node - the cell node or a child of the cell node.
59
+ * @param cells - an iterable of Cells
60
+ * @param isCellNode - a function that takes in a node and checks if
61
+ * it is a cell node.
62
+ *
63
+ * @returns index of the cell we're looking for. Returns -1 if
64
+ * the cell is not founds
65
+ */
66
+ export function findCell(
67
+ node: HTMLElement,
68
+ cells: Iterable<Cell>,
69
+ isCellNode: (node: HTMLElement) => boolean
70
+ ): number {
71
+ let cellIndex = -1;
72
+ while (node && node.parentElement) {
73
+ if (isCellNode(node)) {
74
+ let index = -1;
75
+ for (const cell of cells) {
76
+ if (cell.node === node) {
77
+ cellIndex = ++index;
78
+ break;
79
+ }
80
+ }
81
+ break;
82
+ }
83
+ node = node.parentElement;
84
+ }
85
+ return cellIndex;
86
+ }
87
+
88
+ /**
89
+ * Detect which part of the cell triggered the MouseEvent
90
+ *
91
+ * @param cell - The cell which contains the MouseEvent's target
92
+ * @param target - The DOM node which triggered the MouseEvent
93
+ */
94
+ export function detectTargetArea(
95
+ cell: Cell,
96
+ target: HTMLElement
97
+ ): ICellTargetArea {
98
+ let targetArea: ICellTargetArea;
99
+ if (cell) {
100
+ if (cell.editorWidget?.node.contains(target)) {
101
+ targetArea = 'input';
102
+ } else if (cell.promptNode?.contains(target)) {
103
+ targetArea = 'prompt';
104
+ } else {
105
+ targetArea = 'cell';
106
+ }
107
+ } else {
108
+ targetArea = 'unknown';
109
+ }
110
+ return targetArea;
111
+ }
112
+
113
+ /**
114
+ * Detect if a drag event should be started. This is down if the
115
+ * mouse is moved beyond a certain distance (DRAG_THRESHOLD).
116
+ *
117
+ * @param prevX - X Coordinate of the mouse pointer during the mousedown event
118
+ * @param prevY - Y Coordinate of the mouse pointer during the mousedown event
119
+ * @param nextX - Current X Coordinate of the mouse pointer
120
+ * @param nextY - Current Y Coordinate of the mouse pointer
121
+ */
122
+ export function shouldStartDrag(
123
+ prevX: number,
124
+ prevY: number,
125
+ nextX: number,
126
+ nextY: number
127
+ ): boolean {
128
+ const dx = Math.abs(nextX - prevX);
129
+ const dy = Math.abs(nextY - prevY);
130
+ return dx >= DRAG_THRESHOLD || dy >= DRAG_THRESHOLD;
131
+ }
132
+
133
+ /**
134
+ * Create an image for the cell(s) to be dragged
135
+ *
136
+ * @param activeCell - The cell from where the drag event is triggered
137
+ * @param selectedCells - The cells to be dragged
138
+ */
139
+ export function createCellDragImage(
140
+ activeCell: Cell,
141
+ selectedCells: nbformat.ICell[]
142
+ ): HTMLElement {
143
+ const count = selectedCells.length;
144
+ let promptNumber: string;
145
+ if (activeCell.model.type === 'code') {
146
+ const executionCount = (activeCell.model as ICodeCellModel)
147
+ .executionCount;
148
+ promptNumber = ' ';
149
+ if (executionCount) {
150
+ promptNumber = executionCount.toString();
151
+ }
152
+ } else {
153
+ promptNumber = '';
154
+ }
155
+
156
+ const cellContent = activeCell.model.sharedModel
157
+ .getSource()
158
+ .split('\n')[0]
159
+ .slice(0, 26);
160
+ if (count > 1) {
161
+ if (promptNumber !== '') {
162
+ return VirtualDOM.realize(
163
+ h.div(
164
+ h.div(
165
+ { className: DRAG_IMAGE_CLASS },
166
+ h.span(
167
+ { className: CELL_DRAG_PROMPT_CLASS },
168
+ '[' + promptNumber + ']:'
169
+ ),
170
+ h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent)
171
+ ),
172
+ h.div({ className: CELL_DRAG_MULTIPLE_BACK }, '')
173
+ )
174
+ );
175
+ } else {
176
+ return VirtualDOM.realize(
177
+ h.div(
178
+ h.div(
179
+ { className: DRAG_IMAGE_CLASS },
180
+ h.span({ className: CELL_DRAG_PROMPT_CLASS }),
181
+ h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent)
182
+ ),
183
+ h.div({ className: CELL_DRAG_MULTIPLE_BACK }, '')
184
+ )
185
+ );
186
+ }
187
+ } else {
188
+ if (promptNumber !== '') {
189
+ return VirtualDOM.realize(
190
+ h.div(
191
+ h.div(
192
+ { className: `${DRAG_IMAGE_CLASS} ${SINGLE_DRAG_IMAGE_CLASS}` },
193
+ h.span(
194
+ { className: CELL_DRAG_PROMPT_CLASS },
195
+ '[' + promptNumber + ']:'
196
+ ),
197
+ h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent)
198
+ )
199
+ )
200
+ );
201
+ } else {
202
+ return VirtualDOM.realize(
203
+ h.div(
204
+ h.div(
205
+ { className: `${DRAG_IMAGE_CLASS} ${SINGLE_DRAG_IMAGE_CLASS}` },
206
+ h.span({ className: CELL_DRAG_PROMPT_CLASS }),
207
+ h.span({ className: CELL_DRAG_CONTENT_CLASS }, cellContent)
208
+ )
209
+ )
210
+ );
211
+ }
212
+ }
213
+ }
214
+ }
@@ -0,0 +1,150 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ import { ReactWidget } from '@jupyterlab/ui-components';
7
+ import { ElementExt } from '@lumino/domutils';
8
+
9
+ import * as React from 'react';
10
+
11
+ import { Cell, CodeCell } from './widget';
12
+
13
+ /**
14
+ * The CSS class added to all collapsers.
15
+ */
16
+ const COLLAPSER_CLASS = 'jp-Collapser';
17
+
18
+ /**
19
+ * The CSS class added to the collapser child.
20
+ */
21
+ const COLLAPSER_CHILD_CLASS = 'jp-Collapser-child';
22
+
23
+ /**
24
+ * The CSS class added to input collapsers.
25
+ */
26
+ const INPUT_COLLAPSER = 'jp-InputCollapser';
27
+
28
+ /**
29
+ * The CSS class added to output collapsers.
30
+ */
31
+ const OUTPUT_COLLAPSER = 'jp-OutputCollapser';
32
+
33
+ /**
34
+ * Abstract collapser base class.
35
+ *
36
+ * ### Notes
37
+ * A collapser is a visible div to the left of a cell's
38
+ * input/output that a user can click on to collapse the
39
+ * input/output.
40
+ */
41
+ export abstract class Collapser extends ReactWidget {
42
+ /**
43
+ * Construct a new collapser.
44
+ */
45
+ constructor() {
46
+ super();
47
+ this.addClass(COLLAPSER_CLASS);
48
+ }
49
+
50
+ /**
51
+ * Is the input/output of the parent collapsed.
52
+ */
53
+ get collapsed(): boolean {
54
+ return false;
55
+ }
56
+
57
+ /**
58
+ * Render the collapser with the virtual DOM.
59
+ */
60
+ protected render(): React.ReactElement<any> {
61
+ const childClass = COLLAPSER_CHILD_CLASS;
62
+ return <div className={childClass} onClick={e => this.handleClick(e)} />;
63
+ }
64
+
65
+ /**
66
+ * Handle the click event.
67
+ */
68
+ protected abstract handleClick(e: React.MouseEvent<HTMLDivElement>): void;
69
+ }
70
+
71
+ /**
72
+ * A collapser subclass to collapse a cell's input area.
73
+ */
74
+ export class InputCollapser extends Collapser {
75
+ /**
76
+ * Construct a new input collapser.
77
+ */
78
+ constructor() {
79
+ super();
80
+ this.addClass(INPUT_COLLAPSER);
81
+ }
82
+
83
+ /**
84
+ * Is the cell's input collapsed?
85
+ */
86
+ get collapsed(): boolean {
87
+ const cell = this.parent?.parent as Cell | undefined | null;
88
+ if (cell) {
89
+ return cell.inputHidden;
90
+ } else {
91
+ return false;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Handle a click event for the user to collapse the cell's input.
97
+ */
98
+ protected handleClick(e: React.MouseEvent<HTMLDivElement>): void {
99
+ const cell = this.parent?.parent as Cell | undefined | null;
100
+ if (cell) {
101
+ cell.inputHidden = !cell.inputHidden;
102
+ }
103
+ /* We need this until we watch the cell state */
104
+ this.update();
105
+ }
106
+ }
107
+
108
+ /**
109
+ * A collapser subclass to collapse a cell's output area.
110
+ */
111
+ export class OutputCollapser extends Collapser {
112
+ /**
113
+ * Construct a new output collapser.
114
+ */
115
+ constructor() {
116
+ super();
117
+ this.addClass(OUTPUT_COLLAPSER);
118
+ }
119
+
120
+ /**
121
+ * Is the cell's output collapsed?
122
+ */
123
+ get collapsed(): boolean {
124
+ const cell = this.parent?.parent as CodeCell | undefined | null;
125
+ if (cell) {
126
+ return cell.outputHidden;
127
+ } else {
128
+ return false;
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Handle a click event for the user to collapse the cell's output.
134
+ */
135
+ protected handleClick(e: React.MouseEvent<HTMLDivElement>): void {
136
+ const cell = this.parent?.parent as CodeCell | undefined | null;
137
+ if (cell) {
138
+ cell.outputHidden = !cell.outputHidden;
139
+ /* Scroll cell into view after output collapse */
140
+ if (cell.outputHidden) {
141
+ let area = cell.parent?.node;
142
+ if (area) {
143
+ ElementExt.scrollIntoViewIfNeeded(area, cell.node);
144
+ }
145
+ }
146
+ }
147
+ /* We need this until we watch the cell state */
148
+ this.update();
149
+ }
150
+ }
@@ -0,0 +1,52 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ import { Widget } from '@lumino/widgets';
7
+
8
+ /**
9
+ * The CSS class added to the cell header.
10
+ */
11
+ const CELL_HEADER_CLASS = 'jp-CellHeader';
12
+
13
+ /**
14
+ * The CSS class added to the cell footer.
15
+ */
16
+ const CELL_FOOTER_CLASS = 'jp-CellFooter';
17
+
18
+ /**
19
+ * The interface for a cell header.
20
+ */
21
+ export interface ICellHeader extends Widget {}
22
+
23
+ /**
24
+ * Default implementation of a cell header.
25
+ */
26
+ export class CellHeader extends Widget implements ICellHeader {
27
+ /**
28
+ * Construct a new cell header.
29
+ */
30
+ constructor() {
31
+ super();
32
+ this.addClass(CELL_HEADER_CLASS);
33
+ }
34
+ }
35
+
36
+ /**
37
+ * The interface for a cell footer.
38
+ */
39
+ export interface ICellFooter extends Widget {}
40
+
41
+ /**
42
+ * Default implementation of a cell footer.
43
+ */
44
+ export class CellFooter extends Widget implements ICellFooter {
45
+ /**
46
+ * Construct a new cell footer.
47
+ */
48
+ constructor() {
49
+ super();
50
+ this.addClass(CELL_FOOTER_CLASS);
51
+ }
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+ /**
6
+ * @packageDocumentation
7
+ * @module cells
8
+ */
9
+
10
+ export * from './celldragutils';
11
+ export * from './collapser';
12
+ export * from './headerfooter';
13
+ export * from './inputarea';
14
+ export * from './model';
15
+ export * from './placeholder';
16
+ export * from './searchprovider';
17
+ export * from './widget';