@@ -23,13 +23,14 @@ export abstract class BaseInputBlockConverter<T extends z.ZodObject> implements
2323 abstract getSupportedType ( ) : string ;
2424 abstract defaultConfig ( ) : z . infer < T > ;
2525
26- applyChangesToBlock ( block : DeepnoteBlock , cell : NotebookCellData ) : void {
26+ /**
27+ * Helper method to update block metadata with common logic.
28+ * Clears block.content, parses schema, deletes DEEPNOTE_VSCODE_RAW_CONTENT_KEY,
29+ * and merges metadata with updates.
30+ */
31+ protected updateBlockMetadata ( block : DeepnoteBlock , updates : Record < string , unknown > ) : void {
2732 block . content = '' ;
2833
29- // The cell value now contains just the variable name
30- const variableName = cell . value . trim ( ) ;
31-
32- // Preserve existing metadata and update only the variable name
3334 const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
3435 const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
3536
@@ -40,10 +41,17 @@ export abstract class BaseInputBlockConverter<T extends z.ZodObject> implements
4041 block . metadata = {
4142 ...( block . metadata ?? { } ) ,
4243 ...baseMetadata ,
43- deepnote_variable_name : variableName
44+ ... updates
4445 } ;
4546 }
4647
48+ applyChangesToBlock ( block : DeepnoteBlock , _cell : NotebookCellData ) : void {
49+ // Default implementation: preserve existing metadata
50+ // Readonly blocks (select, checkbox, date, date-range, button) use this default behavior
51+ // Editable blocks override this method to update specific metadata fields
52+ this . updateBlockMetadata ( block , { } ) ;
53+ }
54+
4755 canConvert ( blockType : string ) : boolean {
4856 return blockType . toLowerCase ( ) === this . getSupportedType ( ) ;
4957 }
@@ -86,23 +94,12 @@ export class InputTextBlockConverter extends BaseInputBlockConverter<typeof Deep
8694 }
8795
8896 override applyChangesToBlock ( block : DeepnoteBlock , cell : NotebookCellData ) : void {
89- block . content = '' ;
90-
9197 // The cell value contains the text value
9298 const value = cell . value ;
9399
94- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
95- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
96-
97- if ( block . metadata != null ) {
98- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
99- }
100-
101- block . metadata = {
102- ...( block . metadata ?? { } ) ,
103- ...baseMetadata ,
100+ this . updateBlockMetadata ( block , {
104101 deepnote_variable_value : value
105- } ;
102+ } ) ;
106103 }
107104}
108105
@@ -126,23 +123,12 @@ export class InputTextareaBlockConverter extends BaseInputBlockConverter<typeof
126123 }
127124
128125 override applyChangesToBlock ( block : DeepnoteBlock , cell : NotebookCellData ) : void {
129- block . content = '' ;
130-
131126 // The cell value contains the text value
132127 const value = cell . value ;
133128
134- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
135- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
136-
137- if ( block . metadata != null ) {
138- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
139- }
140-
141- block . metadata = {
142- ...( block . metadata ?? { } ) ,
143- ...baseMetadata ,
129+ this . updateBlockMetadata ( block , {
144130 deepnote_variable_value : value
145- } ;
131+ } ) ;
146132 }
147133}
148134
@@ -165,23 +151,8 @@ export class InputSelectBlockConverter extends BaseInputBlockConverter<typeof De
165151 return cell ;
166152 }
167153
168- override applyChangesToBlock ( block : DeepnoteBlock , _cell : NotebookCellData ) : void {
169- block . content = '' ;
170-
171- // Select blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
172- // Just preserve existing metadata
173- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
174- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
175-
176- if ( block . metadata != null ) {
177- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
178- }
179-
180- block . metadata = {
181- ...( block . metadata ?? { } ) ,
182- ...baseMetadata
183- } ;
184- }
154+ // Select blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
155+ // Uses base class applyChangesToBlock which preserves existing metadata
185156}
186157
187158export class InputSliderBlockConverter extends BaseInputBlockConverter < typeof DeepnoteSliderInputMetadataSchema > {
@@ -204,14 +175,11 @@ export class InputSliderBlockConverter extends BaseInputBlockConverter<typeof De
204175 }
205176
206177 override applyChangesToBlock ( block : DeepnoteBlock , cell : NotebookCellData ) : void {
207- block . content = '' ;
208-
209178 // Parse numeric value; fall back to existing/default
210179 const str = cell . value . trim ( ) ;
211180 const parsed = Number ( str ) ;
212181
213182 const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
214- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
215183
216184 let value : number ;
217185 if ( Number . isFinite ( parsed ) ) {
@@ -227,15 +195,9 @@ export class InputSliderBlockConverter extends BaseInputBlockConverter<typeof De
227195 value = Number . isFinite ( defaultParsed ) ? defaultParsed : 0 ;
228196 }
229197
230- if ( block . metadata != null ) {
231- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
232- }
233-
234- block . metadata = {
235- ...( block . metadata ?? { } ) ,
236- ...baseMetadata ,
198+ this . updateBlockMetadata ( block , {
237199 deepnote_variable_value : value
238- } ;
200+ } ) ;
239201 }
240202}
241203
@@ -258,23 +220,8 @@ export class InputCheckboxBlockConverter extends BaseInputBlockConverter<typeof
258220 return cell ;
259221 }
260222
261- override applyChangesToBlock ( block : DeepnoteBlock , _cell : NotebookCellData ) : void {
262- block . content = '' ;
263-
264- // Checkbox blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
265- // Just preserve existing metadata
266- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
267- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
268-
269- if ( block . metadata != null ) {
270- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
271- }
272-
273- block . metadata = {
274- ...( block . metadata ?? { } ) ,
275- ...baseMetadata
276- } ;
277- }
223+ // Checkbox blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
224+ // Uses base class applyChangesToBlock which preserves existing metadata
278225}
279226
280227export class InputDateBlockConverter extends BaseInputBlockConverter < typeof DeepnoteDateInputMetadataSchema > {
@@ -296,23 +243,8 @@ export class InputDateBlockConverter extends BaseInputBlockConverter<typeof Deep
296243 return cell ;
297244 }
298245
299- override applyChangesToBlock ( block : DeepnoteBlock , _cell : NotebookCellData ) : void {
300- block . content = '' ;
301-
302- // Date blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
303- // Just preserve existing metadata
304- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
305- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
306-
307- if ( block . metadata != null ) {
308- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
309- }
310-
311- block . metadata = {
312- ...( block . metadata ?? { } ) ,
313- ...baseMetadata
314- } ;
315- }
246+ // Date blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
247+ // Uses base class applyChangesToBlock which preserves existing metadata
316248}
317249
318250export class InputDateRangeBlockConverter extends BaseInputBlockConverter < typeof DeepnoteDateRangeInputMetadataSchema > {
@@ -334,23 +266,8 @@ export class InputDateRangeBlockConverter extends BaseInputBlockConverter<typeof
334266 return cell ;
335267 }
336268
337- override applyChangesToBlock ( block : DeepnoteBlock , _cell : NotebookCellData ) : void {
338- block . content = '' ;
339-
340- // Date range blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
341- // Just preserve existing metadata
342- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
343- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
344-
345- if ( block . metadata != null ) {
346- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
347- }
348-
349- block . metadata = {
350- ...( block . metadata ?? { } ) ,
351- ...baseMetadata
352- } ;
353- }
269+ // Date range blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
270+ // Uses base class applyChangesToBlock which preserves existing metadata
354271}
355272
356273export class InputFileBlockConverter extends BaseInputBlockConverter < typeof DeepnoteFileInputMetadataSchema > {
@@ -373,23 +290,12 @@ export class InputFileBlockConverter extends BaseInputBlockConverter<typeof Deep
373290 }
374291
375292 override applyChangesToBlock ( block : DeepnoteBlock , cell : NotebookCellData ) : void {
376- block . content = '' ;
377-
378293 // Remove quotes from the cell value
379294 const value = cell . value . trim ( ) . replace ( / ^ [ " ' ] | [ " ' ] $ / g, '' ) ;
380295
381- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
382- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
383-
384- if ( block . metadata != null ) {
385- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
386- }
387-
388- block . metadata = {
389- ...( block . metadata ?? { } ) ,
390- ...baseMetadata ,
296+ this . updateBlockMetadata ( block , {
391297 deepnote_variable_value : value
392- } ;
298+ } ) ;
393299 }
394300}
395301
@@ -412,20 +318,6 @@ export class ButtonBlockConverter extends BaseInputBlockConverter<typeof Deepnot
412318 return cell ;
413319 }
414320
415- override applyChangesToBlock ( block : DeepnoteBlock , _cell : NotebookCellData ) : void {
416- block . content = '' ;
417-
418- // Button blocks don't store any value from the cell content
419- const existingMetadata = this . schema ( ) . safeParse ( block . metadata ) ;
420- const baseMetadata = existingMetadata . success ? existingMetadata . data : this . defaultConfig ( ) ;
421-
422- if ( block . metadata != null ) {
423- delete block . metadata [ DEEPNOTE_VSCODE_RAW_CONTENT_KEY ] ;
424- }
425-
426- block . metadata = {
427- ...( block . metadata ?? { } ) ,
428- ...baseMetadata
429- } ;
430- }
321+ // Button blocks don't store any value from the cell content
322+ // Uses base class applyChangesToBlock which preserves existing metadata
431323}
0 commit comments