Skip to content

Commit

Permalink
Change cell type on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunanand committed Dec 18, 2024
1 parent 11c6ac7 commit 62b2012
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 1 addition & 5 deletions ui/src/ide/editor/notebook/NbButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ function NbButtons(props){
{ label: 'Markdown', value: 'markdown' },
{ label: 'Raw', value: 'raw' },
]

const changeCellType = (value) =>{

}

return (
<div className='text-editor-tool'>
Expand All @@ -23,7 +19,7 @@ function NbButtons(props){
<button type='button' className='editor-button' onClick={() => props.stopKernel()}><i className='fas fa-square' /></button>
<button type='button' className='editor-button' onClick={() => props.restartKernel()}><i className='fas fa-redo' /></button>
<button type='button' className='editor-button' onClick={() => props.reExecuteNotebook()}><i className='fas fa-forward' /></button>
<select onChange={e => changeCellType(e.target.value)} className="form-select editor-select" value={props.notebook.cells.length > 0 && props.notebook.cells[props.focusedIndex].cell_type}>
<select onChange={e => props.changeCellType(e.target.value)} className="form-select editor-select" value={props.notebook.cells.length > 0 && props.notebook.cells[props.focusedIndex].cell_type}>
{options.map((option, index) => (
<option key={index} value={option.value}>{option.label}</option>
))}
Expand Down
18 changes: 17 additions & 1 deletion ui/src/ide/editor/notebook/NotebookEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ export default function NotebookEditor(props) {
});
}

const changeCellType = (value: string) =>{
setNotebook((prevNotebook) => {
const updatedCells = prevNotebook.cells.map((cell, idx) => {

// If index is provided and valid, update the cell at that index
if (idx === focusedIndex) {
return { ...cell, cell_type: value };
}
return cell;
});

return { ...prevNotebook, cells: updatedCells };
});
}

const submitCell = (source: string, cellId: string) => {
setNotebook((prevNotebook) => {
const updatedCells = prevNotebook.cells.map((cell) => {
Expand Down Expand Up @@ -359,7 +374,7 @@ export default function NotebookEditor(props) {
};

const [copiedCell, setCopiedCell] = useState<ICell|null>(null); // To store the copied or cut cell
const [cutCellIndex, setCutCellIndex] = useState<number|null>(null); // To store the index of the cut cell
const [cutCellIndex, setCutCellIndex] = useState<number|null>(null); // To store the index of the cut cell

// Function to copy the cell at the current focused index
const copyCell = () => {
Expand Down Expand Up @@ -459,6 +474,7 @@ const [cutCellIndex, setCutCellIndex] = useState<number|null>(null); // To store
notebook={notebook}
kernelName={kernelName}
kernelStatus={kernelStatus}
changeCellType={changeCellType}
/>
{debugMode && (
<div>
Expand Down

0 comments on commit 62b2012

Please sign in to comment.