Skip to content

Commit 3202b3a

Browse files
committed
refactor: remove dead input value parsing logic
1 parent 760fdd0 commit 3202b3a

File tree

2 files changed

+43
-144
lines changed

2 files changed

+43
-144
lines changed

src/notebooks/deepnote/converters/inputConverters.ts

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -170,32 +170,11 @@ export class InputSelectBlockConverter extends BaseInputBlockConverter<typeof De
170170
return cell;
171171
}
172172

173-
override applyChangesToBlock(block: DeepnoteBlock, cell: NotebookCellData): void {
173+
override applyChangesToBlock(block: DeepnoteBlock, _cell: NotebookCellData): void {
174174
block.content = '';
175175

176-
// Parse the cell value to extract the selection
177-
const cellValue = cell.value.trim();
178-
let value: string | string[] | null;
179-
180-
if (cellValue.startsWith('[') && cellValue.endsWith(']')) {
181-
// Multi-select: parse array, map 'None' to null
182-
const arrayContent = cellValue.slice(1, -1);
183-
value = arrayContent
184-
.split(',')
185-
.map((v) => v.trim())
186-
.filter((v) => v)
187-
.map((v) => v.replace(/^["']|["']$/g, ''));
188-
} else {
189-
// Single select: 'None' => null, else strip quotes
190-
const stripped = cellValue.replace(/^["']|["']$/g, '');
191-
if (stripped === 'None') {
192-
// Represent empty selection
193-
value = null;
194-
} else {
195-
value = stripped;
196-
}
197-
}
198-
176+
// Select blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
177+
// Just preserve existing metadata
199178
const existingMetadata = this.schema().safeParse(block.metadata);
200179
const baseMetadata = existingMetadata.success ? existingMetadata.data : this.defaultConfig();
201180

@@ -205,8 +184,7 @@ export class InputSelectBlockConverter extends BaseInputBlockConverter<typeof De
205184

206185
block.metadata = {
207186
...(block.metadata ?? {}),
208-
...baseMetadata,
209-
deepnote_variable_value: value
187+
...baseMetadata
210188
};
211189
}
212190
}
@@ -285,13 +263,11 @@ export class InputCheckboxBlockConverter extends BaseInputBlockConverter<typeof
285263
return cell;
286264
}
287265

288-
override applyChangesToBlock(block: DeepnoteBlock, cell: NotebookCellData): void {
266+
override applyChangesToBlock(block: DeepnoteBlock, _cell: NotebookCellData): void {
289267
block.content = '';
290268

291-
// Parse the cell value to get boolean
292-
const cellValue = cell.value.trim();
293-
const value = cellValue === 'True' || cellValue === 'true';
294-
269+
// Checkbox blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
270+
// Just preserve existing metadata
295271
const existingMetadata = this.schema().safeParse(block.metadata);
296272
const baseMetadata = existingMetadata.success ? existingMetadata.data : this.defaultConfig();
297273

@@ -301,8 +277,7 @@ export class InputCheckboxBlockConverter extends BaseInputBlockConverter<typeof
301277

302278
block.metadata = {
303279
...(block.metadata ?? {}),
304-
...baseMetadata,
305-
deepnote_variable_value: value
280+
...baseMetadata
306281
};
307282
}
308283
}
@@ -326,12 +301,11 @@ export class InputDateBlockConverter extends BaseInputBlockConverter<typeof Deep
326301
return cell;
327302
}
328303

329-
override applyChangesToBlock(block: DeepnoteBlock, cell: NotebookCellData): void {
304+
override applyChangesToBlock(block: DeepnoteBlock, _cell: NotebookCellData): void {
330305
block.content = '';
331306

332-
// Remove quotes from the cell value
333-
const value = cell.value.trim().replace(/^["']|["']$/g, '');
334-
307+
// Date blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
308+
// Just preserve existing metadata
335309
const existingMetadata = this.schema().safeParse(block.metadata);
336310
const baseMetadata = existingMetadata.success ? existingMetadata.data : this.defaultConfig();
337311

@@ -341,8 +315,7 @@ export class InputDateBlockConverter extends BaseInputBlockConverter<typeof Deep
341315

342316
block.metadata = {
343317
...(block.metadata ?? {}),
344-
...baseMetadata,
345-
deepnote_variable_value: value
318+
...baseMetadata
346319
};
347320
}
348321
}
@@ -366,19 +339,11 @@ export class InputDateRangeBlockConverter extends BaseInputBlockConverter<typeof
366339
return cell;
367340
}
368341

369-
override applyChangesToBlock(block: DeepnoteBlock, cell: NotebookCellData): void {
342+
override applyChangesToBlock(block: DeepnoteBlock, _cell: NotebookCellData): void {
370343
block.content = '';
371344

372-
// Parse the cell value to extract the date range
373-
const cellValue = cell.value.trim();
374-
let value: [string, string] | null = null;
375-
376-
// Try to parse as tuple
377-
const tupleMatch = cellValue.match(/\(\s*["']([^"']*)["']\s*,\s*["']([^"']*)["']\s*\)/);
378-
if (tupleMatch) {
379-
value = [tupleMatch[1], tupleMatch[2]];
380-
}
381-
345+
// Date range blocks are readonly - edits are reverted by DeepnoteInputBlockEditProtection
346+
// Just preserve existing metadata
382347
const existingMetadata = this.schema().safeParse(block.metadata);
383348
const baseMetadata = existingMetadata.success ? existingMetadata.data : this.defaultConfig();
384349

@@ -388,9 +353,7 @@ export class InputDateRangeBlockConverter extends BaseInputBlockConverter<typeof
388353

389354
block.metadata = {
390355
...(block.metadata ?? {}),
391-
...baseMetadata,
392-
deepnote_variable_value:
393-
value !== null ? value : existingMetadata.success ? existingMetadata.data.deepnote_variable_value : null
356+
...baseMetadata
394357
};
395358
}
396359
}

src/notebooks/deepnote/converters/inputConverters.unit.test.ts

Lines changed: 27 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,27 @@ suite('InputSelectBlockConverter', () => {
308308
});
309309

310310
suite('applyChangesToBlock', () => {
311-
test('applies selected value from cell', () => {
311+
test('preserves existing metadata (select blocks are readonly)', () => {
312312
const block: DeepnoteBlock = {
313313
blockGroup: 'test-group',
314314
content: '',
315315
id: 'block-123',
316316
metadata: {
317317
deepnote_variable_name: 'input_3',
318-
deepnote_variable_options: ['Option A', 'Option B']
318+
deepnote_variable_options: ['Option A', 'Option B'],
319+
deepnote_variable_value: 'Option A'
319320
},
320321
sortingKey: 'a0',
321322
type: 'input-select'
322323
};
324+
// Cell content is ignored since select blocks are readonly
323325
const cell = new NotebookCellData(NotebookCellKind.Code, '"Option B"', 'python');
324326

325327
converter.applyChangesToBlock(block, cell);
326328

327329
assert.strictEqual(block.content, '');
328-
assert.strictEqual(block.metadata?.deepnote_variable_value, 'Option B');
329-
// Variable name should be preserved
330+
// Value should be preserved from metadata, not parsed from cell
331+
assert.strictEqual(block.metadata?.deepnote_variable_value, 'Option A');
330332
assert.strictEqual(block.metadata?.deepnote_variable_name, 'input_3');
331333
assert.deepStrictEqual(block.metadata?.deepnote_variable_options, ['Option A', 'Option B']);
332334
});
@@ -534,34 +536,37 @@ suite('InputCheckboxBlockConverter', () => {
534536
});
535537

536538
suite('applyChangesToBlock', () => {
537-
test('applies True value from cell', () => {
539+
test('preserves existing metadata (checkbox blocks are readonly)', () => {
538540
const block: DeepnoteBlock = {
539541
blockGroup: 'test-group',
540542
content: '',
541543
id: 'block-123',
542544
metadata: {
543-
deepnote_variable_name: 'checkbox1'
545+
deepnote_variable_name: 'checkbox1',
546+
deepnote_variable_value: false
544547
},
545548
sortingKey: 'a0',
546549
type: 'input-checkbox'
547550
};
551+
// Cell content is ignored since checkbox blocks are readonly
548552
const cell = new NotebookCellData(NotebookCellKind.Code, 'True', 'python');
549553

550554
converter.applyChangesToBlock(block, cell);
551555

552556
assert.strictEqual(block.content, '');
553-
assert.strictEqual(block.metadata?.deepnote_variable_value, true);
554-
// Variable name should be preserved
557+
// Value should be preserved from metadata, not parsed from cell
558+
assert.strictEqual(block.metadata?.deepnote_variable_value, false);
555559
assert.strictEqual(block.metadata?.deepnote_variable_name, 'checkbox1');
556560
});
557561

558-
test('applies False value', () => {
562+
test('preserves default value', () => {
559563
const block: DeepnoteBlock = {
560564
blockGroup: 'test-group',
561565
content: '',
562566
id: 'block-123',
563567
metadata: {
564568
deepnote_variable_name: 'checkbox1',
569+
deepnote_variable_value: true,
565570
deepnote_variable_default_value: true
566571
},
567572
sortingKey: 'a0',
@@ -571,23 +576,8 @@ suite('InputCheckboxBlockConverter', () => {
571576

572577
converter.applyChangesToBlock(block, cell);
573578

574-
assert.strictEqual(block.metadata?.deepnote_variable_value, false);
575-
assert.strictEqual(block.metadata?.deepnote_variable_default_value, true);
576-
});
577-
578-
test('handles lowercase true', () => {
579-
const block: DeepnoteBlock = {
580-
blockGroup: 'test-group',
581-
content: '',
582-
id: 'block-123',
583-
sortingKey: 'a0',
584-
type: 'input-checkbox'
585-
};
586-
const cell = new NotebookCellData(NotebookCellKind.Code, 'true', 'python');
587-
588-
converter.applyChangesToBlock(block, cell);
589-
590579
assert.strictEqual(block.metadata?.deepnote_variable_value, true);
580+
assert.strictEqual(block.metadata?.deepnote_variable_default_value, true);
591581
});
592582
});
593583
});
@@ -638,46 +628,30 @@ suite('InputDateBlockConverter', () => {
638628
});
639629

640630
suite('applyChangesToBlock', () => {
641-
test('applies date value from cell', () => {
631+
test('preserves existing metadata (date blocks are readonly)', () => {
642632
const block: DeepnoteBlock = {
643633
blockGroup: 'test-group',
644634
content: '',
645635
id: 'block-123',
646636
metadata: {
647637
deepnote_variable_name: 'date1',
648-
deepnote_input_date_version: 2
638+
deepnote_input_date_version: 2,
639+
deepnote_variable_value: '2025-01-01T00:00:00.000Z'
649640
},
650641
sortingKey: 'a0',
651642
type: 'input-date'
652643
};
644+
// Cell content is ignored since date blocks are readonly
653645
const cell = new NotebookCellData(NotebookCellKind.Code, '"2025-12-31T00:00:00.000Z"', 'python');
654646

655647
converter.applyChangesToBlock(block, cell);
656648

657649
assert.strictEqual(block.content, '');
658-
assert.strictEqual(block.metadata?.deepnote_variable_value, '2025-12-31T00:00:00.000Z');
659-
// Other metadata should be preserved
650+
// Value should be preserved from metadata, not parsed from cell
651+
assert.strictEqual(block.metadata?.deepnote_variable_value, '2025-01-01T00:00:00.000Z');
660652
assert.strictEqual(block.metadata?.deepnote_variable_name, 'date1');
661653
assert.strictEqual(block.metadata?.deepnote_input_date_version, 2);
662654
});
663-
664-
test('handles unquoted date value', () => {
665-
const block: DeepnoteBlock = {
666-
blockGroup: 'test-group',
667-
content: '',
668-
id: 'block-123',
669-
metadata: {
670-
deepnote_variable_name: 'date1'
671-
},
672-
sortingKey: 'a0',
673-
type: 'input-date'
674-
};
675-
const cell = new NotebookCellData(NotebookCellKind.Code, '2025-12-31', 'python');
676-
677-
converter.applyChangesToBlock(block, cell);
678-
679-
assert.strictEqual(block.metadata?.deepnote_variable_value, '2025-12-31');
680-
});
681655
});
682656
});
683657

@@ -745,66 +719,28 @@ suite('InputDateRangeBlockConverter', () => {
745719
});
746720

747721
suite('applyChangesToBlock', () => {
748-
test('applies date range tuple from cell', () => {
722+
test('preserves existing metadata (date range blocks are readonly)', () => {
749723
const block: DeepnoteBlock = {
750724
blockGroup: 'test-group',
751725
content: '',
752726
id: 'block-123',
753727
metadata: {
754-
deepnote_variable_name: 'range1'
728+
deepnote_variable_name: 'range1',
729+
deepnote_variable_value: ['2025-06-01', '2025-06-30']
755730
},
756731
sortingKey: 'a0',
757732
type: 'input-date-range'
758733
};
734+
// Cell content is ignored since date range blocks are readonly
759735
const cell = new NotebookCellData(NotebookCellKind.Code, '("2025-01-01", "2025-12-31")', 'python');
760736

761737
converter.applyChangesToBlock(block, cell);
762738

763739
assert.strictEqual(block.content, '');
764-
assert.deepStrictEqual(block.metadata?.deepnote_variable_value, ['2025-01-01', '2025-12-31']);
765-
// Variable name should be preserved
740+
// Value should be preserved from metadata, not parsed from cell
741+
assert.deepStrictEqual(block.metadata?.deepnote_variable_value, ['2025-06-01', '2025-06-30']);
766742
assert.strictEqual(block.metadata?.deepnote_variable_name, 'range1');
767743
});
768-
769-
test('handles invalid tuple format', () => {
770-
const block: DeepnoteBlock = {
771-
blockGroup: 'test-group',
772-
content: '',
773-
id: 'block-123',
774-
metadata: {
775-
deepnote_variable_name: 'range1',
776-
deepnote_variable_value: ['2025-01-01', '2025-12-31']
777-
},
778-
sortingKey: 'a0',
779-
type: 'input-date-range'
780-
};
781-
const cell = new NotebookCellData(NotebookCellKind.Code, 'invalid', 'python');
782-
783-
converter.applyChangesToBlock(block, cell);
784-
785-
// Should preserve existing value when parse fails
786-
assert.deepStrictEqual(block.metadata?.deepnote_variable_value, ['2025-01-01', '2025-12-31']);
787-
});
788-
789-
test('handles empty value', () => {
790-
const block: DeepnoteBlock = {
791-
blockGroup: 'test-group',
792-
content: '',
793-
id: 'block-123',
794-
metadata: {
795-
deepnote_variable_name: 'range1',
796-
deepnote_variable_value: ['2025-01-01', '2025-12-31']
797-
},
798-
sortingKey: 'a0',
799-
type: 'input-date-range'
800-
};
801-
const cell = new NotebookCellData(NotebookCellKind.Code, '', 'python');
802-
803-
converter.applyChangesToBlock(block, cell);
804-
805-
// Should preserve existing value when cell is empty
806-
assert.deepStrictEqual(block.metadata?.deepnote_variable_value, ['2025-01-01', '2025-12-31']);
807-
});
808744
});
809745
});
810746

0 commit comments

Comments
 (0)