@@ -9,7 +9,6 @@ import './CellEditorMonacoWidget.css';
9
9
// React.
10
10
import React from 'react' ;
11
11
12
- import * as DOM from '../../../../../base/browser/dom.js' ;
13
12
import { EditorExtensionsRegistry , IEditorContributionDescription } from '../../../../../editor/browser/editorExtensions.js' ;
14
13
import { CodeEditorWidget } from '../../../../../editor/browser/widget/codeEditor/codeEditorWidget.js' ;
15
14
import { Event } from '../../../../../base/common/event.js' ;
@@ -37,10 +36,6 @@ export function CellEditorMonacoWidget({ cell }: { cell: PositronNotebookCellGen
37
36
/> ;
38
37
}
39
38
40
- // Padding for the editor widget. The sizing is not perfect but this helps the editor not overflow
41
- // its container. In the future we should figure out how to make sure this is sized correctly.
42
- const EDITOR_INSET_PADDING_PX = 1 ;
43
-
44
39
/**
45
40
* Create a cell editor widget for a cell.
46
41
* @param cell Cell whose editor is to be created
@@ -51,107 +46,77 @@ export function useCellEditorWidget(cell: PositronNotebookCellGeneral) {
51
46
const environment = useEnvironment ( ) ;
52
47
const instance = useNotebookInstance ( ) ;
53
48
54
- const sizeObservable = environment . sizeObservable ;
55
-
56
- // Grab the wrapping div for the editor. This is used for passing context key service
49
+ // Create an element ref to contain the editor
57
50
const editorPartRef = React . useRef < HTMLDivElement > ( null ) ;
58
- // Grab a ref to the div that will hold the editor. This is needed to pass an element to the
59
- // editor creation function.
60
-
61
51
62
52
// Create the editor
63
53
React . useEffect ( ( ) => {
64
54
if ( ! editorPartRef . current ) { return ; }
65
55
66
- const disposableStore = new DisposableStore ( ) ;
67
-
68
- // We need to use a native dom element here instead of a react ref one because the elements
69
- // created by react's refs are not _true_ dom elements and thus calls like `refEl instanceof
70
- // HTMLElement` will return false. This is a problem when we hand the elements into the
71
- // editor widget as it expects a true dom element.
72
- const nativeContainer = DOM . $ ( '.positron-monaco-editor-container' ) ;
73
- editorPartRef . current . appendChild ( nativeContainer ) ;
56
+ const disposables = new DisposableStore ( ) ;
74
57
75
58
const language = cell . cellModel . language ;
76
- const editorContextKeyService = environment . scopedContextKeyProviderCallback ( editorPartRef . current ) ;
77
- disposableStore . add ( editorContextKeyService ) ;
59
+ const editorContextKeyService = disposables . add ( environment . scopedContextKeyProviderCallback ( editorPartRef . current ) ) ;
78
60
const editorInstaService = services . instantiationService . createChild ( new ServiceCollection ( [ IContextKeyService , editorContextKeyService ] ) ) ;
79
61
const editorOptions = new CellEditorOptions ( instance . getBaseCellEditorOptions ( language ) , instance . notebookOptions , services . configurationService ) ;
80
62
81
-
82
- const editor = editorInstaService . createInstance ( CodeEditorWidget , nativeContainer , {
63
+ const editor = disposables . add ( editorInstaService . createInstance ( CodeEditorWidget , editorPartRef . current , {
83
64
...editorOptions . getDefaultValue ( ) ,
84
- // Turns off the margin of the editor. This should probably be placed in a settable
85
- // option somewhere eventually.
86
- glyphMargin : false ,
87
65
dimension : {
88
- width : 500 ,
89
- height : 200
66
+ width : 0 ,
67
+ height : 0 ,
90
68
} ,
91
69
} , {
92
70
contributions : getNotebookEditorContributions ( )
93
- } ) ;
94
- disposableStore . add ( editor ) ;
71
+ } ) ) ;
95
72
cell . attachEditor ( editor ) ;
96
73
97
- editor . setValue ( cell . getContent ( ) ) ;
98
-
99
- disposableStore . add (
100
- editor . onDidFocusEditorWidget ( ( ) => {
101
- instance . setEditingCell ( cell ) ;
102
- } )
103
- ) ;
74
+ // Request model for cell and pass to editor.
75
+ cell . getTextEditorModel ( ) . then ( model => {
76
+ editor . setModel ( model ) ;
77
+ } ) ;
104
78
105
- disposableStore . add (
106
- editor . onDidBlurEditorWidget ( ( ) => {
107
- } )
108
- ) ;
79
+ disposables . add ( editor . onDidFocusEditorWidget ( ( ) => {
80
+ instance . setEditingCell ( cell ) ;
81
+ } ) ) ;
109
82
83
+ // disposables.add(editor.onDidBlurEditorWidget(() => {
84
+ // }));
110
85
111
86
/**
112
87
* Resize the editor widget to fill the width of its container and the height of its
113
88
* content.
114
89
* @param height Height to set. Defaults to checking content height.
115
90
*/
116
91
function resizeEditor ( height : number = editor . getContentHeight ( ) ) {
92
+ if ( ! editorPartRef . current ) { return ; }
117
93
editor . layout ( {
118
94
height,
119
- width : ( editorPartRef . current ? .offsetWidth ?? 500 ) - EDITOR_INSET_PADDING_PX * 2
95
+ width : editorPartRef . current . offsetWidth ,
120
96
} ) ;
121
97
}
122
98
123
- // Request model for cell and pass to editor.
124
- cell . getTextEditorModel ( ) . then ( model => {
125
- editor . setModel ( model ) ;
126
- resizeEditor ( ) ;
127
-
128
- editor . onDidContentSizeChange ( e => {
129
- if ( ! ( e . contentHeightChanged || e . contentWidthChanged ) ) { return ; }
130
- resizeEditor ( e . contentHeight ) ;
131
- } ) ;
132
- } ) ;
133
-
134
- // Keep the width up-to-date as the window resizes.
99
+ // Resize the editor when its content size changes
100
+ disposables . add ( editor . onDidContentSizeChange ( e => {
101
+ if ( ! ( e . contentHeightChanged || e . contentWidthChanged ) ) { return ; }
102
+ resizeEditor ( e . contentHeight ) ;
103
+ } ) ) ;
135
104
136
- disposableStore . add ( Event . fromObservable ( sizeObservable ) ( ( ) => {
105
+ // Resize the editor as the window resizes.
106
+ disposables . add ( Event . fromObservable ( environment . size ) ( ( ) => {
137
107
resizeEditor ( ) ;
138
108
} ) ) ;
139
109
140
110
services . logService . info ( 'Positron Notebook | useCellEditorWidget() | Setting up editor widget' ) ;
141
111
142
-
143
112
return ( ) => {
144
113
services . logService . info ( 'Positron Notebook | useCellEditorWidget() | Disposing editor widget' ) ;
145
- disposableStore . dispose ( ) ;
146
- nativeContainer . remove ( ) ;
114
+ disposables . dispose ( ) ;
147
115
cell . detachEditor ( ) ;
148
116
} ;
149
- } , [ cell , environment , instance , services . configurationService , services . instantiationService , services . logService , sizeObservable ] ) ;
150
-
151
-
117
+ } , [ cell , environment , instance , services . configurationService , services . instantiationService , services . logService ] ) ;
152
118
153
119
return { editorPartRef } ;
154
-
155
120
}
156
121
157
122
0 commit comments