@jupyterlab/cells 4.0.0-alpha.2 → 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.
- package/lib/celldragutils.d.ts +1 -7
- package/lib/celldragutils.js +12 -13
- package/lib/celldragutils.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/inputarea.d.ts +9 -19
- package/lib/inputarea.js +16 -29
- package/lib/inputarea.js.map +1 -1
- package/lib/model.d.ts +121 -79
- package/lib/model.js +179 -392
- package/lib/model.js.map +1 -1
- package/lib/placeholder.d.ts +23 -19
- package/lib/placeholder.js +30 -35
- package/lib/placeholder.js.map +1 -1
- package/lib/resizeHandle.d.ts +0 -1
- package/lib/resizeHandle.js +9 -11
- package/lib/resizeHandle.js.map +1 -1
- package/lib/searchprovider.d.ts +31 -0
- package/lib/searchprovider.js +359 -0
- package/lib/searchprovider.js.map +1 -0
- package/lib/testutils.d.ts +22 -0
- package/lib/testutils.js +48 -0
- package/lib/testutils.js.map +1 -0
- package/lib/widget.d.ts +226 -42
- package/lib/widget.js +672 -270
- package/lib/widget.js.map +1 -1
- package/package.json +36 -33
- package/src/celldragutils.ts +214 -0
- package/src/collapser.tsx +150 -0
- package/src/headerfooter.ts +52 -0
- package/src/index.ts +17 -0
- package/src/inputarea.ts +292 -0
- package/src/model.ts +845 -0
- package/src/placeholder.ts +125 -0
- package/src/resizeHandle.ts +96 -0
- package/src/searchprovider.ts +433 -0
- package/src/testutils.ts +57 -0
- package/src/widget.ts +2290 -0
- package/style/collapser.css +5 -4
- package/style/headerfooter.css +3 -3
- package/style/index.css +4 -2
- package/style/index.js +4 -2
- package/style/inputarea.css +19 -17
- package/style/placeholder.css +1 -1
- package/style/widget.css +85 -7
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/* -----------------------------------------------------------------------------
|
|
2
|
+
| Copyright (c) Jupyter Development Team.
|
|
3
|
+
| Distributed under the terms of the Modified BSD License.
|
|
4
|
+
|----------------------------------------------------------------------------*/
|
|
5
|
+
|
|
6
|
+
import { ellipsesIcon } from '@jupyterlab/ui-components';
|
|
7
|
+
import { Widget } from '@lumino/widgets';
|
|
8
|
+
import { Message } from '@lumino/messaging';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The CSS class added to placeholders.
|
|
12
|
+
*/
|
|
13
|
+
const PLACEHOLDER_CLASS = 'jp-Placeholder';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The CSS classes added to input placeholder prompts.
|
|
17
|
+
*/
|
|
18
|
+
const INPUT_PROMPT_CLASS = 'jp-Placeholder-prompt jp-InputPrompt';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The CSS classes added to output placeholder prompts.
|
|
22
|
+
*/
|
|
23
|
+
const OUTPUT_PROMPT_CLASS = 'jp-Placeholder-prompt jp-OutputPrompt';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The CSS class added to placeholder content.
|
|
27
|
+
*/
|
|
28
|
+
const CONTENT_CLASS = 'jp-Placeholder-content';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The CSS class added to input placeholders.
|
|
32
|
+
*/
|
|
33
|
+
const INPUT_PLACEHOLDER_CLASS = 'jp-InputPlaceholder';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The CSS class added to output placeholders.
|
|
37
|
+
*/
|
|
38
|
+
const OUTPUT_PLACEHOLDER_CLASS = 'jp-OutputPlaceholder';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Placeholder widget options
|
|
42
|
+
*/
|
|
43
|
+
export interface IPlaceholderOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Prompt element CSS class
|
|
46
|
+
*/
|
|
47
|
+
promptClass: string;
|
|
48
|
+
/**
|
|
49
|
+
* Ellipsis button callback
|
|
50
|
+
*/
|
|
51
|
+
callback: (e: MouseEvent) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* An base class for placeholders
|
|
56
|
+
*
|
|
57
|
+
* ### Notes
|
|
58
|
+
* A placeholder is the element that is shown when input/output
|
|
59
|
+
* is hidden.
|
|
60
|
+
*/
|
|
61
|
+
export class Placeholder extends Widget {
|
|
62
|
+
/**
|
|
63
|
+
* Construct a new placeholder.
|
|
64
|
+
*/
|
|
65
|
+
constructor(options: IPlaceholderOptions) {
|
|
66
|
+
const node = document.createElement('div');
|
|
67
|
+
|
|
68
|
+
super({ node });
|
|
69
|
+
const innerNode = document.createElement('div');
|
|
70
|
+
innerNode.className = options.promptClass;
|
|
71
|
+
node.insertAdjacentHTML('afterbegin', innerNode.outerHTML);
|
|
72
|
+
this._button = document.createElement('div');
|
|
73
|
+
this._button.classList.add(CONTENT_CLASS);
|
|
74
|
+
node.appendChild(this._button);
|
|
75
|
+
ellipsesIcon.element({
|
|
76
|
+
container: this._button.appendChild(document.createElement('div')),
|
|
77
|
+
className: 'jp-MoreHorizIcon',
|
|
78
|
+
elementPosition: 'center',
|
|
79
|
+
height: 'auto',
|
|
80
|
+
width: '32px'
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
this.addClass(PLACEHOLDER_CLASS);
|
|
84
|
+
this._callback = options.callback;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
protected onAfterAttach(msg: Message): void {
|
|
88
|
+
super.onAfterAttach(msg);
|
|
89
|
+
this._button.addEventListener('click', this._callback);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
protected onBeforeDetach(msg: Message): void {
|
|
93
|
+
this._button.removeEventListener('click', this._callback);
|
|
94
|
+
super.onBeforeDetach(msg);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private _callback: (e: MouseEvent) => void;
|
|
98
|
+
private _button: HTMLElement;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The input placeholder class.
|
|
103
|
+
*/
|
|
104
|
+
export class InputPlaceholder extends Placeholder {
|
|
105
|
+
/**
|
|
106
|
+
* Construct a new input placeholder.
|
|
107
|
+
*/
|
|
108
|
+
constructor(callback: (e: MouseEvent) => void) {
|
|
109
|
+
super({ callback, promptClass: INPUT_PROMPT_CLASS });
|
|
110
|
+
this.addClass(INPUT_PLACEHOLDER_CLASS);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The output placeholder class.
|
|
116
|
+
*/
|
|
117
|
+
export class OutputPlaceholder extends Placeholder {
|
|
118
|
+
/**
|
|
119
|
+
* Construct a new output placeholder.
|
|
120
|
+
*/
|
|
121
|
+
constructor(callback: (e: MouseEvent) => void) {
|
|
122
|
+
super({ callback, promptClass: OUTPUT_PROMPT_CLASS });
|
|
123
|
+
this.addClass(OUTPUT_PLACEHOLDER_CLASS);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
import { Message } from '@lumino/messaging';
|
|
8
|
+
|
|
9
|
+
const RESIZE_HANDLE_CLASS = 'jp-CellResizeHandle';
|
|
10
|
+
|
|
11
|
+
const CELL_RESIZED_CLASS = 'jp-mod-resizedCell';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A handle that allows to change input/output proportions in side-by-side mode.
|
|
15
|
+
*/
|
|
16
|
+
export class ResizeHandle extends Widget {
|
|
17
|
+
private _isActive: boolean = false;
|
|
18
|
+
private _isDragging: boolean = false;
|
|
19
|
+
private _protectedWidth = 10;
|
|
20
|
+
|
|
21
|
+
constructor(protected targetNode: HTMLElement) {
|
|
22
|
+
super();
|
|
23
|
+
this.addClass(RESIZE_HANDLE_CLASS);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
protected onAfterAttach(msg: Message) {
|
|
27
|
+
super.onAfterAttach(msg);
|
|
28
|
+
this.node.addEventListener('dblclick', this);
|
|
29
|
+
this.node.addEventListener('mousedown', this);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected onAfterDetach(msg: Message) {
|
|
33
|
+
super.onAfterAttach(msg);
|
|
34
|
+
this.node.removeEventListener('dblclick', this);
|
|
35
|
+
this.node.removeEventListener('mousedown', this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Handle the DOM events for the widget.
|
|
40
|
+
*
|
|
41
|
+
* @param event - The DOM event sent to the widget.
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
handleEvent(event: Event): void {
|
|
45
|
+
switch (event.type) {
|
|
46
|
+
case 'dblclick':
|
|
47
|
+
this.targetNode.parentNode?.childNodes.forEach(node => {
|
|
48
|
+
(node as HTMLElement).classList.remove(CELL_RESIZED_CLASS);
|
|
49
|
+
});
|
|
50
|
+
document.documentElement.style.setProperty(
|
|
51
|
+
'--jp-side-by-side-output-size',
|
|
52
|
+
`1fr`
|
|
53
|
+
);
|
|
54
|
+
this._isActive = false;
|
|
55
|
+
break;
|
|
56
|
+
case 'mousedown':
|
|
57
|
+
this._isDragging = true;
|
|
58
|
+
if (!this._isActive) {
|
|
59
|
+
this.targetNode.parentNode?.childNodes.forEach(node => {
|
|
60
|
+
(node as HTMLElement).classList.add(CELL_RESIZED_CLASS);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
this._isActive = true;
|
|
64
|
+
}
|
|
65
|
+
window.addEventListener('mousemove', this);
|
|
66
|
+
window.addEventListener('mouseup', this);
|
|
67
|
+
break;
|
|
68
|
+
case 'mousemove': {
|
|
69
|
+
if (!this._isActive || !this._isDragging) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const targetRect = this.targetNode.getBoundingClientRect();
|
|
73
|
+
|
|
74
|
+
const width = targetRect.width - this._protectedWidth * 2;
|
|
75
|
+
const position =
|
|
76
|
+
(event as MouseEvent).clientX - targetRect.x - this._protectedWidth;
|
|
77
|
+
|
|
78
|
+
const outputRatio = width / position - 1;
|
|
79
|
+
|
|
80
|
+
document.documentElement.style.setProperty(
|
|
81
|
+
'--jp-side-by-side-output-size',
|
|
82
|
+
`${outputRatio}fr`
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case 'mouseup':
|
|
88
|
+
this._isDragging = false;
|
|
89
|
+
window.removeEventListener('mousemove', this);
|
|
90
|
+
window.removeEventListener('mouseup', this);
|
|
91
|
+
break;
|
|
92
|
+
default:
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { CodeEditor } from '@jupyterlab/codeeditor';
|
|
5
|
+
import { CodeMirrorEditor, EditorSearchProvider } from '@jupyterlab/codemirror';
|
|
6
|
+
import { signalToPromise } from '@jupyterlab/coreutils';
|
|
7
|
+
import {
|
|
8
|
+
GenericSearchProvider,
|
|
9
|
+
IBaseSearchProvider,
|
|
10
|
+
IFilters,
|
|
11
|
+
ISearchMatch
|
|
12
|
+
} from '@jupyterlab/documentsearch';
|
|
13
|
+
import { OutputArea } from '@jupyterlab/outputarea';
|
|
14
|
+
import { ICellModel } from './model';
|
|
15
|
+
import { Cell, CodeCell, MarkdownCell } from './widget';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Class applied on highlighted search matches
|
|
19
|
+
*/
|
|
20
|
+
export const SELECTED_HIGHLIGHT_CLASS = 'jp-mod-selected';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Search provider for cells.
|
|
24
|
+
*/
|
|
25
|
+
export class CellSearchProvider
|
|
26
|
+
extends EditorSearchProvider<ICellModel>
|
|
27
|
+
implements IBaseSearchProvider
|
|
28
|
+
{
|
|
29
|
+
constructor(protected cell: Cell<ICellModel>) {
|
|
30
|
+
super();
|
|
31
|
+
if (!this.cell.inViewport && !this.cell.editor) {
|
|
32
|
+
void signalToPromise(cell.inViewportChanged).then(([, inViewport]) => {
|
|
33
|
+
if (inViewport) {
|
|
34
|
+
this.cmHandler.setEditor(this.editor as CodeMirrorEditor);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Text editor
|
|
42
|
+
*/
|
|
43
|
+
protected get editor(): CodeEditor.IEditor | null {
|
|
44
|
+
return this.cell.editor;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Editor content model
|
|
49
|
+
*/
|
|
50
|
+
protected get model() {
|
|
51
|
+
return this.cell.model;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Code cell search provider
|
|
57
|
+
*/
|
|
58
|
+
class CodeCellSearchProvider extends CellSearchProvider {
|
|
59
|
+
/**
|
|
60
|
+
* Constructor
|
|
61
|
+
*
|
|
62
|
+
* @param cell Cell widget
|
|
63
|
+
*/
|
|
64
|
+
constructor(cell: Cell<ICellModel>) {
|
|
65
|
+
super(cell);
|
|
66
|
+
|
|
67
|
+
this.currentProviderIndex = -1;
|
|
68
|
+
this.outputsProvider = [];
|
|
69
|
+
|
|
70
|
+
const outputs = (this.cell as CodeCell).outputArea;
|
|
71
|
+
this._onOutputsChanged(outputs, outputs.widgets.length).catch(reason => {
|
|
72
|
+
console.error(`Failed to initialize search on cell outputs.`, reason);
|
|
73
|
+
});
|
|
74
|
+
outputs.outputLengthChanged.connect(this._onOutputsChanged, this);
|
|
75
|
+
outputs.disposed.connect(() => {
|
|
76
|
+
outputs.outputLengthChanged.disconnect(this._onOutputsChanged);
|
|
77
|
+
}, this);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Number of matches in the cell.
|
|
82
|
+
*/
|
|
83
|
+
get matchesCount(): number {
|
|
84
|
+
if (!this.isActive) {
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
super.matchesCount +
|
|
90
|
+
this.outputsProvider.reduce(
|
|
91
|
+
(sum, provider) => sum + (provider.matchesCount ?? 0),
|
|
92
|
+
0
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Clear currently highlighted match.
|
|
99
|
+
*/
|
|
100
|
+
async clearHighlight(): Promise<void> {
|
|
101
|
+
await super.clearHighlight();
|
|
102
|
+
await Promise.all(
|
|
103
|
+
this.outputsProvider.map(provider => provider.clearHighlight())
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Dispose the search provider
|
|
109
|
+
*/
|
|
110
|
+
dispose(): void {
|
|
111
|
+
if (this.isDisposed) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
super.dispose();
|
|
115
|
+
this.outputsProvider.map(provider => {
|
|
116
|
+
provider.dispose();
|
|
117
|
+
});
|
|
118
|
+
this.outputsProvider.length = 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Highlight the next match.
|
|
123
|
+
*
|
|
124
|
+
* @returns The next match if there is one.
|
|
125
|
+
*/
|
|
126
|
+
async highlightNext(): Promise<ISearchMatch | undefined> {
|
|
127
|
+
if (this.matchesCount === 0 || !this.isActive) {
|
|
128
|
+
this.currentIndex = null;
|
|
129
|
+
} else {
|
|
130
|
+
if (this.currentProviderIndex === -1) {
|
|
131
|
+
const match = await super.highlightNext();
|
|
132
|
+
if (match) {
|
|
133
|
+
this.currentIndex = this.cmHandler.currentIndex;
|
|
134
|
+
return match;
|
|
135
|
+
} else {
|
|
136
|
+
this.currentProviderIndex = 0;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
while (this.currentProviderIndex < this.outputsProvider.length) {
|
|
141
|
+
const provider = this.outputsProvider[this.currentProviderIndex];
|
|
142
|
+
const match = await provider.highlightNext(false);
|
|
143
|
+
if (match) {
|
|
144
|
+
this.currentIndex =
|
|
145
|
+
super.matchesCount +
|
|
146
|
+
this.outputsProvider
|
|
147
|
+
.slice(0, this.currentProviderIndex)
|
|
148
|
+
.reduce(
|
|
149
|
+
(sum, provider) => (sum += provider.matchesCount ?? 0),
|
|
150
|
+
0
|
|
151
|
+
) +
|
|
152
|
+
provider.currentMatchIndex!;
|
|
153
|
+
return match;
|
|
154
|
+
} else {
|
|
155
|
+
this.currentProviderIndex += 1;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
this.currentProviderIndex = -1;
|
|
160
|
+
this.currentIndex = null;
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Highlight the previous match.
|
|
167
|
+
*
|
|
168
|
+
* @returns The previous match if there is one.
|
|
169
|
+
*/
|
|
170
|
+
async highlightPrevious(): Promise<ISearchMatch | undefined> {
|
|
171
|
+
if (this.matchesCount === 0 || !this.isActive) {
|
|
172
|
+
this.currentIndex = null;
|
|
173
|
+
} else {
|
|
174
|
+
if (this.currentIndex === null) {
|
|
175
|
+
this.currentProviderIndex = this.outputsProvider.length - 1;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
while (this.currentProviderIndex >= 0) {
|
|
179
|
+
const provider = this.outputsProvider[this.currentProviderIndex];
|
|
180
|
+
|
|
181
|
+
const match = await provider.highlightPrevious(false);
|
|
182
|
+
if (match) {
|
|
183
|
+
this.currentIndex =
|
|
184
|
+
super.matchesCount +
|
|
185
|
+
this.outputsProvider
|
|
186
|
+
.slice(0, this.currentProviderIndex)
|
|
187
|
+
.reduce(
|
|
188
|
+
(sum, provider) => (sum += provider.matchesCount ?? 0),
|
|
189
|
+
0
|
|
190
|
+
) +
|
|
191
|
+
provider.currentMatchIndex!;
|
|
192
|
+
return match;
|
|
193
|
+
} else {
|
|
194
|
+
this.currentProviderIndex -= 1;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const match = await super.highlightPrevious();
|
|
199
|
+
if (match) {
|
|
200
|
+
this.currentIndex = this.cmHandler.currentIndex;
|
|
201
|
+
return match;
|
|
202
|
+
} else {
|
|
203
|
+
this.currentIndex = null;
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Initialize the search using the provided options. Should update the UI to highlight
|
|
211
|
+
* all matches and "select" the first match.
|
|
212
|
+
*
|
|
213
|
+
* @param query A RegExp to be use to perform the search
|
|
214
|
+
* @param filters Filter parameters to pass to provider
|
|
215
|
+
*/
|
|
216
|
+
async startQuery(query: RegExp | null, filters?: IFilters): Promise<void> {
|
|
217
|
+
await super.startQuery(query, filters);
|
|
218
|
+
|
|
219
|
+
// Search outputs
|
|
220
|
+
if (filters?.output !== false && this.isActive) {
|
|
221
|
+
await Promise.all(
|
|
222
|
+
this.outputsProvider.map(provider => provider.startQuery(query))
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async endQuery(): Promise<void> {
|
|
228
|
+
await super.endQuery();
|
|
229
|
+
if (this.filters?.output !== false && this.isActive) {
|
|
230
|
+
await Promise.all(
|
|
231
|
+
this.outputsProvider.map(provider => provider.endQuery())
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private async _onOutputsChanged(
|
|
237
|
+
outputArea: OutputArea,
|
|
238
|
+
changes: number
|
|
239
|
+
): Promise<void> {
|
|
240
|
+
this.outputsProvider.forEach(provider => {
|
|
241
|
+
provider.dispose();
|
|
242
|
+
});
|
|
243
|
+
this.outputsProvider.length = 0;
|
|
244
|
+
|
|
245
|
+
this.currentProviderIndex = -1;
|
|
246
|
+
this.outputsProvider = (this.cell as CodeCell).outputArea.widgets.map(
|
|
247
|
+
output => new GenericSearchProvider(output)
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
if (this.isActive && this.query && this.filters?.output !== false) {
|
|
251
|
+
await Promise.all([
|
|
252
|
+
this.outputsProvider.map(provider => {
|
|
253
|
+
void provider.startQuery(this.query);
|
|
254
|
+
})
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this._stateChanged.emit();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
protected outputsProvider: GenericSearchProvider[];
|
|
262
|
+
protected currentProviderIndex: number;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Markdown cell search provider
|
|
267
|
+
*/
|
|
268
|
+
class MarkdownCellSearchProvider extends CellSearchProvider {
|
|
269
|
+
/**
|
|
270
|
+
* Constructor
|
|
271
|
+
*
|
|
272
|
+
* @param cell Cell widget
|
|
273
|
+
*/
|
|
274
|
+
constructor(cell: Cell<ICellModel>) {
|
|
275
|
+
super(cell);
|
|
276
|
+
this.renderedProvider = new GenericSearchProvider(
|
|
277
|
+
(cell as MarkdownCell).renderer
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Clear currently highlighted match
|
|
283
|
+
*/
|
|
284
|
+
async clearHighlight(): Promise<void> {
|
|
285
|
+
await super.clearHighlight();
|
|
286
|
+
await this.renderedProvider.clearHighlight();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Dispose the search provider
|
|
291
|
+
*/
|
|
292
|
+
dispose(): void {
|
|
293
|
+
if (this.isDisposed) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
super.dispose();
|
|
297
|
+
this.renderedProvider.dispose();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Stop the search and clean any UI elements.
|
|
302
|
+
*/
|
|
303
|
+
async endQuery(): Promise<void> {
|
|
304
|
+
await super.endQuery();
|
|
305
|
+
await this.renderedProvider.endQuery();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Highlight the next match.
|
|
310
|
+
*
|
|
311
|
+
* @returns The next match if there is one.
|
|
312
|
+
*/
|
|
313
|
+
async highlightNext(): Promise<ISearchMatch | undefined> {
|
|
314
|
+
let match: ISearchMatch | undefined = undefined;
|
|
315
|
+
if (!this.isActive) {
|
|
316
|
+
return match;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const cell = this.cell as MarkdownCell;
|
|
320
|
+
if (cell.rendered && this.matchesCount > 0) {
|
|
321
|
+
// Unrender the cell
|
|
322
|
+
this._unrenderedByHighligh = true;
|
|
323
|
+
const waitForRendered = signalToPromise(cell.renderedChanged);
|
|
324
|
+
cell.rendered = false;
|
|
325
|
+
await waitForRendered;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
match = await super.highlightNext();
|
|
329
|
+
|
|
330
|
+
return match;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Highlight the previous match.
|
|
335
|
+
*
|
|
336
|
+
* @returns The previous match if there is one.
|
|
337
|
+
*/
|
|
338
|
+
async highlightPrevious(): Promise<ISearchMatch | undefined> {
|
|
339
|
+
let match: ISearchMatch | undefined = undefined;
|
|
340
|
+
const cell = this.cell as MarkdownCell;
|
|
341
|
+
if (cell.rendered && this.matchesCount > 0) {
|
|
342
|
+
// Unrender the cell if there are matches within the cell
|
|
343
|
+
this._unrenderedByHighligh = true;
|
|
344
|
+
const waitForRendered = signalToPromise(cell.renderedChanged);
|
|
345
|
+
cell.rendered = false;
|
|
346
|
+
await waitForRendered;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
match = await super.highlightPrevious();
|
|
350
|
+
|
|
351
|
+
return match;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Initialize the search using the provided options. Should update the UI
|
|
356
|
+
* to highlight all matches and "select" the first match.
|
|
357
|
+
*
|
|
358
|
+
* @param query A RegExp to be use to perform the search
|
|
359
|
+
* @param filters Filter parameters to pass to provider
|
|
360
|
+
*/
|
|
361
|
+
async startQuery(query: RegExp | null, filters?: IFilters): Promise<void> {
|
|
362
|
+
await super.startQuery(query, filters);
|
|
363
|
+
const cell = this.cell as MarkdownCell;
|
|
364
|
+
if (cell.rendered) {
|
|
365
|
+
this.onRenderedChanged(cell, cell.rendered);
|
|
366
|
+
}
|
|
367
|
+
cell.renderedChanged.connect(this.onRenderedChanged, this);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Replace all matches in the cell source with the provided text
|
|
372
|
+
*
|
|
373
|
+
* @param newText The replacement text.
|
|
374
|
+
* @returns Whether a replace occurred.
|
|
375
|
+
*/
|
|
376
|
+
async replaceAllMatches(newText: string): Promise<boolean> {
|
|
377
|
+
const result = await super.replaceAllMatches(newText);
|
|
378
|
+
// if the cell is rendered force update
|
|
379
|
+
if ((this.cell as MarkdownCell).rendered) {
|
|
380
|
+
this.cell.update();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Callback on rendered state change
|
|
388
|
+
*
|
|
389
|
+
* @param cell Cell that emitted the change
|
|
390
|
+
* @param rendered New rendered value
|
|
391
|
+
*/
|
|
392
|
+
protected onRenderedChanged(cell: MarkdownCell, rendered: boolean): void {
|
|
393
|
+
if (!this._unrenderedByHighligh) {
|
|
394
|
+
this.currentIndex = null;
|
|
395
|
+
}
|
|
396
|
+
this._unrenderedByHighligh = false;
|
|
397
|
+
if (this.isActive) {
|
|
398
|
+
if (rendered) {
|
|
399
|
+
void this.renderedProvider.startQuery(this.query);
|
|
400
|
+
} else {
|
|
401
|
+
// Force cursor position to ensure reverse search is working as expected
|
|
402
|
+
cell.editor?.setCursorPosition({ column: 0, line: 0 });
|
|
403
|
+
void this.renderedProvider.endQuery();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
protected renderedProvider: GenericSearchProvider;
|
|
409
|
+
private _unrenderedByHighligh = false;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Factory to create a cell search provider
|
|
414
|
+
*
|
|
415
|
+
* @param cell Cell widget
|
|
416
|
+
* @returns Cell search provider
|
|
417
|
+
*/
|
|
418
|
+
export function createCellSearchProvider(
|
|
419
|
+
cell: Cell<ICellModel>
|
|
420
|
+
): CellSearchProvider {
|
|
421
|
+
if (cell.isPlaceholder()) {
|
|
422
|
+
return new CellSearchProvider(cell);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
switch (cell.model.type) {
|
|
426
|
+
case 'code':
|
|
427
|
+
return new CodeCellSearchProvider(cell);
|
|
428
|
+
case 'markdown':
|
|
429
|
+
return new MarkdownCellSearchProvider(cell);
|
|
430
|
+
default:
|
|
431
|
+
return new CellSearchProvider(cell);
|
|
432
|
+
}
|
|
433
|
+
}
|
package/src/testutils.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { CodeEditorWrapper, IEditorServices } from '@jupyterlab/codeeditor';
|
|
5
|
+
import {
|
|
6
|
+
CodeMirrorEditorFactory,
|
|
7
|
+
CodeMirrorMimeTypeService,
|
|
8
|
+
EditorLanguageRegistry
|
|
9
|
+
} from '@jupyterlab/codemirror';
|
|
10
|
+
import { CodeCellModel } from './model';
|
|
11
|
+
import { Cell } from './widget';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The default notebook content.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export namespace NBTestUtils {
|
|
18
|
+
const editorServices: IEditorServices = (function () {
|
|
19
|
+
const languages = new EditorLanguageRegistry();
|
|
20
|
+
const factoryService = new CodeMirrorEditorFactory({ languages });
|
|
21
|
+
const mimeTypeService = new CodeMirrorMimeTypeService(languages);
|
|
22
|
+
return {
|
|
23
|
+
factoryService,
|
|
24
|
+
mimeTypeService
|
|
25
|
+
};
|
|
26
|
+
})();
|
|
27
|
+
|
|
28
|
+
export const editorFactory =
|
|
29
|
+
editorServices.factoryService.newInlineEditor.bind(
|
|
30
|
+
editorServices.factoryService
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export const mimeTypeService = editorServices.mimeTypeService;
|
|
34
|
+
/**
|
|
35
|
+
* Create a base cell content factory.
|
|
36
|
+
*/
|
|
37
|
+
export function createBaseCellFactory(): Cell.IContentFactory {
|
|
38
|
+
return new Cell.ContentFactory({ editorFactory });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create a new code cell content factory.
|
|
43
|
+
*/
|
|
44
|
+
export function createCodeCellFactory(): Cell.IContentFactory {
|
|
45
|
+
return new Cell.ContentFactory({ editorFactory });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a cell editor widget.
|
|
50
|
+
*/
|
|
51
|
+
export function createCellEditor(model?: CodeCellModel): CodeEditorWrapper {
|
|
52
|
+
return new CodeEditorWrapper({
|
|
53
|
+
model: model ?? new CodeCellModel(),
|
|
54
|
+
factory: editorFactory
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|