@theia/notebook 1.51.0 → 1.53.0-next.4

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.
Files changed (75) hide show
  1. package/lib/browser/contributions/cell-operations.d.ts +1 -1
  2. package/lib/browser/contributions/cell-operations.d.ts.map +1 -1
  3. package/lib/browser/contributions/cell-operations.js +3 -2
  4. package/lib/browser/contributions/cell-operations.js.map +1 -1
  5. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  6. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  7. package/lib/browser/contributions/notebook-actions-contribution.js +17 -20
  8. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  10. package/lib/browser/contributions/notebook-cell-actions-contribution.js +23 -7
  11. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  12. package/lib/browser/contributions/notebook-preferences.d.ts +3 -0
  13. package/lib/browser/contributions/notebook-preferences.d.ts.map +1 -1
  14. package/lib/browser/contributions/notebook-preferences.js +9 -1
  15. package/lib/browser/contributions/notebook-preferences.js.map +1 -1
  16. package/lib/browser/contributions/notebook-undo-redo-handler.d.ts +10 -0
  17. package/lib/browser/contributions/notebook-undo-redo-handler.d.ts.map +1 -0
  18. package/lib/browser/contributions/notebook-undo-redo-handler.js +49 -0
  19. package/lib/browser/contributions/notebook-undo-redo-handler.js.map +1 -0
  20. package/lib/browser/notebook-editor-widget.d.ts +6 -0
  21. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  22. package/lib/browser/notebook-editor-widget.js +39 -1
  23. package/lib/browser/notebook-editor-widget.js.map +1 -1
  24. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  25. package/lib/browser/notebook-frontend-module.js +4 -1
  26. package/lib/browser/notebook-frontend-module.js.map +1 -1
  27. package/lib/browser/service/notebook-context-manager.d.ts +2 -1
  28. package/lib/browser/service/notebook-context-manager.d.ts.map +1 -1
  29. package/lib/browser/service/notebook-context-manager.js +6 -3
  30. package/lib/browser/service/notebook-context-manager.js.map +1 -1
  31. package/lib/browser/view/notebook-cell-editor.d.ts +7 -1
  32. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  33. package/lib/browser/view/notebook-cell-editor.js +75 -5
  34. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  35. package/lib/browser/view/notebook-cell-list-view.d.ts +2 -0
  36. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  37. package/lib/browser/view/notebook-cell-list-view.js +22 -1
  38. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  39. package/lib/browser/view/notebook-code-cell-view.d.ts +1 -1
  40. package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -1
  41. package/lib/browser/view/notebook-code-cell-view.js +19 -17
  42. package/lib/browser/view/notebook-code-cell-view.js.map +1 -1
  43. package/lib/browser/view/notebook-find-widget.d.ts +63 -0
  44. package/lib/browser/view/notebook-find-widget.d.ts.map +1 -0
  45. package/lib/browser/view/notebook-find-widget.js +225 -0
  46. package/lib/browser/view/notebook-find-widget.js.map +1 -0
  47. package/lib/browser/view/notebook-markdown-cell-view.d.ts +4 -0
  48. package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -1
  49. package/lib/browser/view/notebook-markdown-cell-view.js +105 -8
  50. package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -1
  51. package/lib/browser/view-model/notebook-cell-model.d.ts +24 -1
  52. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  53. package/lib/browser/view-model/notebook-cell-model.js +71 -1
  54. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  55. package/lib/browser/view-model/notebook-model.d.ts +8 -1
  56. package/lib/browser/view-model/notebook-model.d.ts.map +1 -1
  57. package/lib/browser/view-model/notebook-model.js +66 -14
  58. package/lib/browser/view-model/notebook-model.js.map +1 -1
  59. package/package.json +9 -8
  60. package/src/browser/contributions/cell-operations.ts +3 -2
  61. package/src/browser/contributions/notebook-actions-contribution.ts +20 -20
  62. package/src/browser/contributions/notebook-cell-actions-contribution.ts +23 -8
  63. package/src/browser/contributions/notebook-preferences.ts +10 -1
  64. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -0
  65. package/src/browser/notebook-editor-widget.tsx +49 -2
  66. package/src/browser/notebook-frontend-module.ts +7 -3
  67. package/src/browser/service/notebook-context-manager.ts +8 -4
  68. package/src/browser/style/index.css +172 -5
  69. package/src/browser/view/notebook-cell-editor.tsx +79 -6
  70. package/src/browser/view/notebook-cell-list-view.tsx +29 -3
  71. package/src/browser/view/notebook-code-cell-view.tsx +19 -17
  72. package/src/browser/view/notebook-find-widget.tsx +335 -0
  73. package/src/browser/view/notebook-markdown-cell-view.tsx +134 -17
  74. package/src/browser/view-model/notebook-cell-model.ts +94 -8
  75. package/src/browser/view-model/notebook-model.ts +77 -20
@@ -14,14 +14,15 @@
14
14
  // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
15
  // *****************************************************************************
16
16
  import * as React from '@theia/core/shared/react';
17
- import { CellEditType, CellKind } from '../../common';
17
+ import { CellEditType, CellKind, NotebookCellsChangeType } from '../../common';
18
18
  import { NotebookCellModel } from '../view-model/notebook-cell-model';
19
19
  import { NotebookModel } from '../view-model/notebook-model';
20
20
  import { NotebookCellToolbarFactory } from './notebook-cell-toolbar-factory';
21
- import { codicon } from '@theia/core/lib/browser';
21
+ import { animationFrame, codicon, onDomEvent } from '@theia/core/lib/browser';
22
22
  import { CommandRegistry, DisposableCollection, nls } from '@theia/core';
23
23
  import { NotebookCommands } from '../contributions/notebook-actions-contribution';
24
24
  import { NotebookCellActionContribution } from '../contributions/notebook-cell-actions-contribution';
25
+ import { NotebookContextManager } from '../service/notebook-context-manager';
25
26
 
26
27
  export interface CellRenderer {
27
28
  render(notebookData: NotebookModel, cell: NotebookCellModel, index: number): React.ReactNode
@@ -31,6 +32,7 @@ export interface CellRenderer {
31
32
  interface CellListProps {
32
33
  renderers: Map<CellKind, CellRenderer>;
33
34
  notebookModel: NotebookModel;
35
+ notebookContext: NotebookContextManager;
34
36
  toolbarRenderer: NotebookCellToolbarFactory;
35
37
  commandRegistry: CommandRegistry
36
38
  }
@@ -66,6 +68,13 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
66
68
  }
67
69
  }));
68
70
 
71
+ this.toDispose.push(props.notebookModel.onDidChangeContent(events => {
72
+ if (events.some(e => e.kind === NotebookCellsChangeType.Move)) {
73
+ // When a cell has been moved, we need to rerender the whole component
74
+ this.forceUpdate();
75
+ }
76
+ }));
77
+
69
78
  this.toDispose.push(props.notebookModel.onDidChangeSelectedCell(e => {
70
79
  this.setState({
71
80
  ...this.state,
@@ -80,7 +89,24 @@ export class NotebookCellListView extends React.Component<CellListProps, Noteboo
80
89
  }
81
90
 
82
91
  override render(): React.ReactNode {
83
- return <ul className='theia-notebook-cell-list'>
92
+ return <ul className='theia-notebook-cell-list' ref={
93
+ ref => {
94
+ this.toDispose.push(onDomEvent(document, 'focusin', () => {
95
+ animationFrame().then(() => {
96
+ let hasCellFocus = false;
97
+ let hasFocus = false;
98
+ if (ref?.contains(document.activeElement)) {
99
+ if (this.props.notebookModel.selectedCell) {
100
+ hasCellFocus = true;
101
+ }
102
+ hasFocus = true;
103
+ }
104
+ this.props.notebookContext.changeCellFocus(hasCellFocus);
105
+ this.props.notebookContext.changeCellListFocus(hasFocus);
106
+ });
107
+ }));
108
+ }
109
+ }>
84
110
  {this.props.notebookModel.cells
85
111
  .map((cell, index) =>
86
112
  <React.Fragment key={'cell-' + cell.handle}>
@@ -150,7 +150,7 @@ export interface NotebookCodeCellStatusProps {
150
150
  notebook: NotebookModel;
151
151
  cell: NotebookCellModel;
152
152
  commandRegistry: CommandRegistry;
153
- executionStateService: NotebookExecutionStateService;
153
+ executionStateService?: NotebookExecutionStateService;
154
154
  onClick: () => void;
155
155
  }
156
156
 
@@ -171,22 +171,24 @@ export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStat
171
171
  };
172
172
 
173
173
  let currentInterval: NodeJS.Timeout | undefined;
174
- this.toDispose.push(props.executionStateService.onDidChangeExecution(event => {
175
- if (event.affectsCell(this.props.cell.uri)) {
176
- this.setState({ currentExecution: event.changed, executionTime: 0 });
177
- clearInterval(currentInterval);
178
- if (event.changed?.state === NotebookCellExecutionState.Executing) {
179
- const startTime = Date.now();
180
- // The resolution of the time display is only a single digit after the decimal point.
181
- // Therefore, we only need to update the display every 100ms.
182
- currentInterval = setInterval(() => {
183
- this.setState({
184
- executionTime: Date.now() - startTime
185
- });
186
- }, 100);
174
+ if (props.executionStateService) {
175
+ this.toDispose.push(props.executionStateService.onDidChangeExecution(event => {
176
+ if (event.affectsCell(this.props.cell.uri)) {
177
+ this.setState({ currentExecution: event.changed, executionTime: 0 });
178
+ clearInterval(currentInterval);
179
+ if (event.changed?.state === NotebookCellExecutionState.Executing) {
180
+ const startTime = Date.now();
181
+ // The resolution of the time display is only a single digit after the decimal point.
182
+ // Therefore, we only need to update the display every 100ms.
183
+ currentInterval = setInterval(() => {
184
+ this.setState({
185
+ executionTime: Date.now() - startTime
186
+ });
187
+ }, 100);
188
+ }
187
189
  }
188
- }
189
- }));
190
+ }));
191
+ }
190
192
 
191
193
  this.toDispose.push(props.cell.onDidChangeLanguage(() => {
192
194
  this.forceUpdate();
@@ -200,7 +202,7 @@ export class NotebookCodeCellStatus extends React.Component<NotebookCodeCellStat
200
202
  override render(): React.ReactNode {
201
203
  return <div className='notebook-cell-status' onClick={() => this.props.onClick()}>
202
204
  <div className='notebook-cell-status-left'>
203
- {this.renderExecutionState()}
205
+ {this.props.executionStateService && this.renderExecutionState()}
204
206
  </div>
205
207
  <div className='notebook-cell-status-right'>
206
208
  <span className='notebook-cell-language-label' onClick={() => {
@@ -0,0 +1,335 @@
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { nls } from '@theia/core';
18
+ import * as React from '@theia/core/shared/react';
19
+ import { codicon } from '@theia/core/lib/browser';
20
+ import debounce = require('lodash/debounce');
21
+
22
+ export interface NotebookEditorFindMatch {
23
+ selected: boolean;
24
+ show(): void;
25
+ replace?(value: string): void;
26
+ }
27
+
28
+ export interface NotebookEditorFindMatchOptions {
29
+ search: string;
30
+ matchCase: boolean;
31
+ wholeWord: boolean;
32
+ regex: boolean;
33
+ activeFilters: string[];
34
+ }
35
+
36
+ export interface NotebookEditorFindFilter {
37
+ id: string;
38
+ label: string;
39
+ active: boolean;
40
+ }
41
+
42
+ export interface NotebookEditorFindOptions {
43
+ search?: string;
44
+ jumpToMatch?: boolean;
45
+ matchCase?: boolean;
46
+ wholeWord?: boolean;
47
+ regex?: boolean;
48
+ modifyIndex?: (matches: NotebookEditorFindMatch[], index: number) => number;
49
+ }
50
+
51
+ export interface NotebookFindWidgetProps {
52
+ hidden?: boolean;
53
+ filters?: NotebookEditorFindFilter[];
54
+ onClose(): void;
55
+ onSearch(options: NotebookEditorFindMatchOptions): NotebookEditorFindMatch[];
56
+ onReplace(matches: NotebookEditorFindMatch[], value: string): void;
57
+ }
58
+
59
+ export interface NotebookFindWidgetState {
60
+ search: string;
61
+ replace: string;
62
+ expanded: boolean;
63
+ matchCase: boolean;
64
+ wholeWord: boolean;
65
+ regex: boolean;
66
+ activeFilters: string[];
67
+ currentMatch: number;
68
+ matches: NotebookEditorFindMatch[];
69
+ }
70
+
71
+ export class NotebookFindWidget extends React.Component<NotebookFindWidgetProps, NotebookFindWidgetState> {
72
+
73
+ private searchRef = React.createRef<HTMLInputElement>();
74
+ private debounceSearch = debounce(this.search.bind(this), 50);
75
+
76
+ constructor(props: NotebookFindWidgetProps) {
77
+ super(props);
78
+ this.state = {
79
+ search: '',
80
+ replace: '',
81
+ currentMatch: 0,
82
+ matches: [],
83
+ expanded: false,
84
+ matchCase: false,
85
+ regex: false,
86
+ wholeWord: false,
87
+ activeFilters: props.filters?.filter(filter => filter.active).map(filter => filter.id) || []
88
+ };
89
+ }
90
+
91
+ override render(): React.ReactNode {
92
+ const hasMatches = this.hasMatches();
93
+ const canReplace = this.canReplace();
94
+ const canReplaceAll = this.canReplaceAll();
95
+ return (
96
+ <div onKeyUp={event => {
97
+ if (event.key === 'Escape') {
98
+ this.props.onClose();
99
+ }
100
+ }} className={`theia-notebook-find-widget ${!this.state.expanded ? 'search-mode' : ''} ${this.props.hidden ? 'hidden' : ''}`}>
101
+ <div className='theia-notebook-find-widget-expand' title={nls.localizeByDefault('Toggle Replace')} onClick={() => {
102
+ this.setState({
103
+ expanded: !this.state.expanded
104
+ });
105
+ }}>
106
+ <div className={codicon(`chevron-${this.state.expanded ? 'down' : 'right'}`)}></div>
107
+ </div>
108
+ <div className='theia-notebook-find-widget-inputs'>
109
+ <div className='theia-notebook-find-widget-input-wrapper'>
110
+ <input
111
+ ref={this.searchRef}
112
+ type='text'
113
+ className='theia-input theia-notebook-find-widget-input'
114
+ placeholder={nls.localizeByDefault('Find')}
115
+ value={this.state.search}
116
+ onChange={event => {
117
+ this.setState({
118
+ search: event.target.value
119
+ });
120
+ this.debounceSearch({});
121
+ }}
122
+ onKeyDown={event => {
123
+ if (event.key === 'Enter') {
124
+ if (event.shiftKey) {
125
+ this.gotoPreviousMatch();
126
+ } else {
127
+ this.gotoNextMatch();
128
+ }
129
+ event.preventDefault();
130
+ }
131
+ }}
132
+ />
133
+ <div
134
+ className={`${codicon('case-sensitive', true)} option ${this.state.matchCase ? 'enabled' : ''}`}
135
+ title={nls.localizeByDefault('Match Case')}
136
+ onClick={() => {
137
+ this.search({
138
+ matchCase: !this.state.matchCase
139
+ });
140
+ }}></div>
141
+ <div
142
+ className={`${codicon('whole-word', true)} option ${this.state.wholeWord ? 'enabled' : ''}`}
143
+ title={nls.localizeByDefault('Match Whole Word')}
144
+ onClick={() => {
145
+ this.search({
146
+ wholeWord: !this.state.wholeWord
147
+ });
148
+ }}></div>
149
+ <div
150
+ className={`${codicon('regex', true)} option ${this.state.regex ? 'enabled' : ''}`}
151
+ title={nls.localizeByDefault('Use Regular Expression')}
152
+ onClick={() => {
153
+ this.search({
154
+ regex: !this.state.regex
155
+ });
156
+ }}></div>
157
+ {/* <div
158
+ className={`${codicon('filter', true)} option ${this.state.wholeWord ? 'enabled' : ''}`}
159
+ title={nls.localizeByDefault('Find Filters')}></div> */}
160
+ </div>
161
+ <input
162
+ type='text'
163
+ className='theia-input theia-notebook-find-widget-replace'
164
+ placeholder={nls.localizeByDefault('Replace')}
165
+ value={this.state.replace}
166
+ onChange={event => {
167
+ this.setState({
168
+ replace: event.target.value
169
+ });
170
+ }}
171
+ onKeyDown={event => {
172
+ if (event.key === 'Enter') {
173
+ this.replaceOne();
174
+ event.preventDefault();
175
+ }
176
+ }}
177
+ />
178
+ </div>
179
+ <div className='theia-notebook-find-widget-buttons'>
180
+ <div className='theia-notebook-find-widget-buttons-first'>
181
+ <div className='theia-notebook-find-widget-matches-count'>
182
+ {this.getMatchesCount()}
183
+ </div>
184
+ <div
185
+ className={`${codicon('arrow-up', hasMatches)} ${hasMatches ? '' : 'disabled'}`}
186
+ title={nls.localizeByDefault('Previous Match')}
187
+ onClick={() => {
188
+ this.gotoPreviousMatch();
189
+ }}
190
+ ></div>
191
+ <div
192
+ className={`${codicon('arrow-down', hasMatches)} ${hasMatches ? '' : 'disabled'}`}
193
+ title={nls.localizeByDefault('Next Match')}
194
+ onClick={() => {
195
+ this.gotoNextMatch();
196
+ }}
197
+ ></div>
198
+ <div
199
+ className={codicon('close', true)}
200
+ title={nls.localizeByDefault('Close')}
201
+ onClick={() => {
202
+ this.props.onClose();
203
+ }}
204
+ ></div>
205
+ </div>
206
+ <div className='theia-notebook-find-widget-buttons-second'>
207
+ <div
208
+ className={`${codicon('replace', canReplace)} ${canReplace ? '' : 'disabled'}`}
209
+ title={nls.localizeByDefault('Replace')}
210
+ onClick={() => {
211
+ this.replaceOne();
212
+ }}
213
+ ></div>
214
+ <div
215
+ className={`${codicon('replace-all', canReplaceAll)} ${canReplaceAll ? '' : 'disabled'}`}
216
+ title={nls.localizeByDefault('Replace All')}
217
+ onClick={() => {
218
+ this.replaceAll();
219
+ }}
220
+ ></div>
221
+ </div>
222
+ </div>
223
+ </div>
224
+ );
225
+ }
226
+
227
+ private hasMatches(): boolean {
228
+ return this.state.matches.length > 0;
229
+ }
230
+
231
+ private canReplace(): boolean {
232
+ return Boolean(this.state.matches[this.state.currentMatch]?.replace);
233
+ }
234
+
235
+ private canReplaceAll(): boolean {
236
+ return this.state.matches.some(match => Boolean(match.replace));
237
+ }
238
+
239
+ private getMatchesCount(): string {
240
+ if (this.hasMatches()) {
241
+ return nls.localizeByDefault('{0} of {1}', this.state.currentMatch + 1, this.state.matches.length);
242
+ } else {
243
+ return nls.localizeByDefault('No results');
244
+ }
245
+ }
246
+
247
+ private gotoNextMatch(): void {
248
+ this.search({
249
+ modifyIndex: (matches, index) => (index + 1) % matches.length,
250
+ jumpToMatch: true
251
+ });
252
+ }
253
+
254
+ private gotoPreviousMatch(): void {
255
+ this.search({
256
+ modifyIndex: (matches, index) => (index === 0 ? matches.length : index) - 1,
257
+ jumpToMatch: true
258
+ });
259
+ }
260
+
261
+ private replaceOne(): void {
262
+ const existingMatches = this.state.matches;
263
+ const match = existingMatches[this.state.currentMatch];
264
+ if (match) {
265
+ match.replace?.(this.state.replace);
266
+ this.search({
267
+ jumpToMatch: true,
268
+ modifyIndex: (matches, index) => {
269
+ if (matches.length < existingMatches.length) {
270
+ return index % matches.length;
271
+ } else {
272
+ const diff = matches.length - existingMatches.length;
273
+ return (index + diff + 1) % matches.length;
274
+ }
275
+ }
276
+ });
277
+ }
278
+ }
279
+
280
+ private replaceAll(): void {
281
+ this.props.onReplace(this.state.matches, this.state.replace);
282
+ this.search({});
283
+ }
284
+
285
+ override componentDidUpdate(prevProps: Readonly<NotebookFindWidgetProps>, prevState: Readonly<NotebookFindWidgetState>): void {
286
+ if (!this.props.hidden && prevProps.hidden) {
287
+ // Focus the search input when the widget switches from hidden to visible.
288
+ this.searchRef.current?.focus();
289
+ }
290
+ }
291
+
292
+ focusSearch(content?: string): void {
293
+ this.searchRef.current?.focus();
294
+ if (content) {
295
+ this.search({
296
+ search: content,
297
+ jumpToMatch: false
298
+ });
299
+ }
300
+ }
301
+
302
+ search(options: NotebookEditorFindOptions): void {
303
+ const matchCase = options.matchCase ?? this.state.matchCase;
304
+ const wholeWord = options.wholeWord ?? this.state.wholeWord;
305
+ const regex = options.regex ?? this.state.regex;
306
+ const search = options.search ?? this.state.search;
307
+ const matches = this.props.onSearch({
308
+ search,
309
+ matchCase,
310
+ wholeWord,
311
+ regex,
312
+ activeFilters: this.state.activeFilters
313
+ });
314
+ let currentMatch = Math.max(0, Math.min(this.state.currentMatch, matches.length - 1));
315
+ if (options.modifyIndex && matches.length > 0) {
316
+ currentMatch = options.modifyIndex(matches, currentMatch);
317
+ }
318
+ const selectedMatch = matches[currentMatch];
319
+ if (selectedMatch) {
320
+ selectedMatch.selected = true;
321
+ if (options.jumpToMatch) {
322
+ selectedMatch.show();
323
+ }
324
+ }
325
+ this.setState({
326
+ search,
327
+ matches,
328
+ currentMatch,
329
+ matchCase,
330
+ wholeWord,
331
+ regex
332
+ });
333
+ }
334
+
335
+ }
@@ -23,8 +23,12 @@ import { NotebookCellModel } from '../view-model/notebook-cell-model';
23
23
  import { CellEditor } from './notebook-cell-editor';
24
24
  import { inject, injectable } from '@theia/core/shared/inversify';
25
25
  import { MonacoEditorServices } from '@theia/monaco/lib/browser/monaco-editor';
26
- import { nls } from '@theia/core';
26
+ import { CommandRegistry, nls } from '@theia/core';
27
27
  import { NotebookContextManager } from '../service/notebook-context-manager';
28
+ import { NotebookOptionsService } from '../service/notebook-options';
29
+ import { NotebookCodeCellStatus } from './notebook-code-cell-view';
30
+ import { NotebookEditorFindMatch, NotebookEditorFindMatchOptions } from './notebook-find-widget';
31
+ import * as mark from 'advanced-mark.js';
28
32
 
29
33
  @injectable()
30
34
  export class NotebookMarkdownCellRenderer implements CellRenderer {
@@ -37,9 +41,21 @@ export class NotebookMarkdownCellRenderer implements CellRenderer {
37
41
  @inject(NotebookContextManager)
38
42
  protected readonly notebookContextManager: NotebookContextManager;
39
43
 
44
+ @inject(CommandRegistry)
45
+ protected readonly commandRegistry: CommandRegistry;
46
+
47
+ @inject(NotebookOptionsService)
48
+ protected readonly notebookOptionsService: NotebookOptionsService;
49
+
40
50
  render(notebookModel: NotebookModel, cell: NotebookCellModel): React.ReactNode {
41
- return <MarkdownCell markdownRenderer={this.markdownRenderer} monacoServices={this.monacoServices}
42
- cell={cell} notebookModel={notebookModel} notebookContextManager={this.notebookContextManager} />;
51
+ return <MarkdownCell
52
+ markdownRenderer={this.markdownRenderer}
53
+ commandRegistry={this.commandRegistry}
54
+ monacoServices={this.monacoServices}
55
+ notebookOptionsService={this.notebookOptionsService}
56
+ cell={cell}
57
+ notebookModel={notebookModel}
58
+ notebookContextManager={this.notebookContextManager} />;
43
59
  }
44
60
 
45
61
  renderDragImage(cell: NotebookCellModel): HTMLElement {
@@ -53,39 +69,140 @@ export class NotebookMarkdownCellRenderer implements CellRenderer {
53
69
  }
54
70
 
55
71
  interface MarkdownCellProps {
56
- markdownRenderer: MarkdownRenderer,
57
- monacoServices: MonacoEditorServices
72
+ markdownRenderer: MarkdownRenderer;
73
+ monacoServices: MonacoEditorServices;
58
74
 
59
- cell: NotebookCellModel,
60
- notebookModel: NotebookModel
75
+ commandRegistry: CommandRegistry;
76
+ cell: NotebookCellModel;
77
+ notebookModel: NotebookModel;
61
78
  notebookContextManager: NotebookContextManager;
79
+ notebookOptionsService: NotebookOptionsService;
62
80
  }
63
81
 
64
- function MarkdownCell({ markdownRenderer, monacoServices, cell, notebookModel, notebookContextManager }: MarkdownCellProps): React.JSX.Element {
82
+ function MarkdownCell({
83
+ markdownRenderer, monacoServices, cell, notebookModel, notebookContextManager, notebookOptionsService, commandRegistry
84
+ }: MarkdownCellProps): React.JSX.Element {
65
85
  const [editMode, setEditMode] = React.useState(cell.editing);
86
+ let empty = false;
66
87
 
67
88
  React.useEffect(() => {
68
89
  const listener = cell.onDidRequestCellEditChange(cellEdit => setEditMode(cellEdit));
69
90
  return () => listener.dispose();
70
91
  }, [editMode]);
71
92
 
72
- let markdownContent: HTMLElement = React.useMemo(() => {
93
+ React.useEffect(() => {
94
+ if (!editMode) {
95
+ const instance = new mark(markdownContent);
96
+ cell.onMarkdownFind = options => {
97
+ instance.unmark();
98
+ if (empty) {
99
+ return [];
100
+ }
101
+ return searchInMarkdown(instance, options);
102
+ };
103
+ return () => {
104
+ cell.onMarkdownFind = undefined;
105
+ instance.unmark();
106
+ };
107
+ }
108
+ }, [editMode, cell.source]);
109
+
110
+ let markdownContent: HTMLElement[] = React.useMemo(() => {
73
111
  const markdownString = new MarkdownStringImpl(cell.source, { supportHtml: true, isTrusted: true });
74
- return markdownRenderer.render(markdownString).element;
75
- }, [cell, editMode]);
112
+ const rendered = markdownRenderer.render(markdownString).element;
113
+ const children: HTMLElement[] = [];
114
+ rendered.childNodes.forEach(child => {
115
+ if (child instanceof HTMLElement) {
116
+ children.push(child);
117
+ }
118
+ });
119
+ return children;
120
+ }, [cell.source]);
76
121
 
77
- if (!markdownContent.hasChildNodes()) {
122
+ if (markdownContent.length === 0) {
78
123
  const italic = document.createElement('i');
79
124
  italic.className = 'theia-notebook-empty-markdown';
80
125
  italic.innerText = nls.localizeByDefault('Empty markdown cell, double-click or press enter to edit.');
81
126
  italic.style.pointerEvents = 'none';
82
- markdownContent = italic;
127
+ markdownContent = [italic];
128
+ empty = true;
83
129
  }
84
130
 
85
131
  return editMode ?
86
- <CellEditor cell={cell} notebookModel={notebookModel} monacoServices={monacoServices} notebookContextManager={notebookContextManager} /> :
87
- <div className='theia-notebook-markdown-content'
132
+ (<div className='theia-notebook-markdown-editor-container' key="code">
133
+ <CellEditor notebookModel={notebookModel} cell={cell}
134
+ monacoServices={monacoServices}
135
+ notebookContextManager={notebookContextManager}
136
+ fontInfo={notebookOptionsService.editorFontInfo} />
137
+ <NotebookCodeCellStatus cell={cell} notebook={notebookModel}
138
+ commandRegistry={commandRegistry}
139
+ onClick={() => cell.requestFocusEditor()} />
140
+ </div >) :
141
+ (<div className='theia-notebook-markdown-content' key="markdown"
88
142
  onDoubleClick={() => cell.requestEdit()}
89
- ref={node => node?.replaceChildren(markdownContent)}
90
- />;
143
+ ref={node => node?.replaceChildren(...markdownContent)}
144
+ />);
145
+ }
146
+
147
+ function searchInMarkdown(instance: mark, options: NotebookEditorFindMatchOptions): NotebookEditorFindMatch[] {
148
+ const matches: NotebookEditorFindMatch[] = [];
149
+ const markOptions: mark.MarkOptions & mark.RegExpOptions = {
150
+ className: 'theia-find-match',
151
+ diacritics: false,
152
+ caseSensitive: options.matchCase,
153
+ acrossElements: true,
154
+ separateWordSearch: false,
155
+ each: node => {
156
+ matches.push(new MarkdownEditorFindMatch(node));
157
+ }
158
+ };
159
+ if (options.regex || options.wholeWord) {
160
+ let search = options.search;
161
+ if (options.wholeWord) {
162
+ if (!options.regex) {
163
+ search = escapeRegExp(search);
164
+ }
165
+ search = '\\b' + search + '\\b';
166
+ }
167
+ instance.markRegExp(new RegExp(search, options.matchCase ? '' : 'i'), markOptions);
168
+ } else {
169
+ instance.mark(options.search, markOptions);
170
+ }
171
+ return matches;
172
+ }
173
+
174
+ function escapeRegExp(value: string): string {
175
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
176
+ }
177
+
178
+ class MarkdownEditorFindMatch implements NotebookEditorFindMatch {
179
+
180
+ constructor(readonly node: Node) { }
181
+
182
+ private _selected = false;
183
+
184
+ get selected(): boolean {
185
+ return this._selected;
186
+ }
187
+
188
+ set selected(selected: boolean) {
189
+ this._selected = selected;
190
+ const className = 'theia-find-match-selected';
191
+ if (this.node instanceof HTMLElement) {
192
+ if (selected) {
193
+ this.node.classList.add(className);
194
+ } else {
195
+ this.node.classList.remove(className);
196
+ }
197
+ }
198
+ }
199
+
200
+ show(): void {
201
+ if (this.node instanceof HTMLElement) {
202
+ this.node.scrollIntoView({
203
+ behavior: 'instant',
204
+ block: 'center'
205
+ });
206
+ }
207
+ }
91
208
  }