@@ -22,7 +22,7 @@ import {
2222 keyEventHasModifier ,
2323 type ModifierKey ,
2424} from "catcolab-ui-components" ;
25- import type { Cell , Notebook , MorType , ObType } from "catlog-wasm" ;
25+ import type { Cell , MorType , Notebook , ObType } from "catlog-wasm" ;
2626import {
2727 type CellActions ,
2828 type FormalCellEditorProps ,
@@ -72,7 +72,7 @@ export function NotebookEditor<T>(props: {
7272 changeNotebook : ( f : ( nb : Notebook < T > ) => void ) => void ;
7373
7474 formalCellEditor : Component < FormalCellEditorProps < T > > ;
75- cellConstructors ?: CellConstructor < T > [ ] ;
75+ cellConstructors : ( cellType ?: string , cellName ?: string ) => CellConstructor < T > [ ] ;
7676 cellLabel ?: ( content : T ) => string | undefined ;
7777
7878 /** Called to duplicate an existing cell.
@@ -143,16 +143,28 @@ export function NotebookEditor<T>(props: {
143143 return cell . tag === "formal" ;
144144 } ;
145145
146- const isObType = ( content : any ) : content is { tag : "object" ; obType : ObType } => {
147- return content && content . tag === "object" ;
146+ const isObType = ( content : unknown ) : content is { tag : "object" ; obType : ObType } => {
147+ return (
148+ typeof content === "object" &&
149+ content !== null &&
150+ "tag" in content &&
151+ content . tag === "object"
152+ ) ;
148153 } ;
149154
150- const isMorType = ( content : any ) : content is { tag : "morphism" ; morType : MorType } => {
151- return content && content . tag === "morphism" ;
155+ const isMorType = ( content : unknown ) : content is { tag : "morphism" ; morType : MorType } => {
156+ return (
157+ typeof content === "object" &&
158+ content !== null &&
159+ "tag" in content &&
160+ content . tag === "morphism"
161+ ) ;
152162 } ;
153163
154164 const retypeCellAs = ( i : number , newCell : Cell < T > ) => {
155- if ( ! newCell || ! isFormalCell ( newCell ) ) return ;
165+ if ( ! newCell || ! isFormalCell ( newCell ) ) {
166+ return ;
167+ }
156168 const mutator = ( cellContent : T ) => {
157169 if ( isObType ( cellContent ) && isObType ( newCell . content ) ) {
158170 cellContent . obType = newCell . content . obType ;
@@ -161,19 +173,22 @@ export function NotebookEditor<T>(props: {
161173 }
162174 } ;
163175 props . changeNotebook ( ( nb ) => {
164- NotebookUtils . retypeCell ( nb , i , mutator ) ;
176+ NotebookUtils . retypeCell ( nb , i , mutator ) ;
165177 } ) ;
166- setActiveCell ( i ) ;
167178 } ;
168179
169- const cellConstructors = ( ) : CellConstructor < T > [ ] => [
170- {
171- name : "Text" ,
172- description : "Start writing text" ,
173- shortcut : [ "T" ] ,
174- construct : ( ) => newRichTextCell ( ) ,
175- } ,
176- ...( props . cellConstructors ?? [ ] ) ,
180+ const cellConstructors = ( cellType ?: string , cellName ?: string ) : CellConstructor < T > [ ] => [
181+ ...( cellType && cellName
182+ ? [ ]
183+ : [
184+ {
185+ name : "Text" ,
186+ description : "Start writing text" ,
187+ shortcut : [ "T" ] ,
188+ construct : ( ) => newRichTextCell ( ) ,
189+ } ,
190+ ] ) ,
191+ ...( props . cellConstructors ( cellType , cellName ) ?? [ ] ) ,
177192 ] ;
178193
179194 const replaceCommands = ( i : number ) : Completion [ ] =>
@@ -187,8 +202,8 @@ export function NotebookEditor<T>(props: {
187202 } ;
188203 } ) ;
189204
190- const retypeCommands = ( i : number ) : Completion [ ] =>
191- cellConstructors ( ) . map ( ( cc ) => {
205+ const retypeCommands = ( i : number , cellType ?: string , cellName ?: string ) : Completion [ ] =>
206+ cellConstructors ( cellType , cellName ) . map ( ( cc ) => {
192207 const { name, description, shortcut } = cc ;
193208 return {
194209 name,
@@ -327,18 +342,25 @@ export function NotebookEditor<T>(props: {
327342 const cell = props . notebook . cellContents [ cellId ] ;
328343 invariant ( cell , `Failed to find contents for cell '${ cellId } '` ) ;
329344
330- // const tag = (cell.tag === "formal" ? props.cellLabel?.(cell.content) : undefined) as string;
331- const cellName = ( cell : Cell < T > ) =>
345+ const cellName = ( cell : Cell < T > ) =>
332346 ( cell . tag === "formal"
333347 ? props . cellLabel ?.( cell . content )
334348 : undefined ) as string ;
335- // const cellType = isFormalCell(cell)
336- // ? isObType(cell.content)
337- // ? "ObType"
338- // : isMorType(cell.content)
339- // ? "MorType"
340- // : ""
341- // : "";
349+
350+ const cellType = ( cell : Cell < T > ) =>
351+ cell . tag === "formal"
352+ ? isObType ( cell . content )
353+ ? "ObType"
354+ : "MorType"
355+ : undefined ;
356+
357+ // if (isObType(cellContent) && isObType(newCell.content)) {
358+ // cellContent.obType = newCell.content.obType;
359+ // } else if (isMorType(cellContent) && isMorType(newCell.content)) {
360+ // cellContent.morType = newCell.content.morType;
361+ // }
362+
363+ // const cellType = "ObType";
342364
343365 if ( cell . tag !== "rich-text" ) {
344366 cellActions . duplicate = ( ) => {
@@ -360,13 +382,15 @@ export function NotebookEditor<T>(props: {
360382 cellId = { cell . id }
361383 index = { i ( ) }
362384 actions = { cellActions }
363- tag = {
364- cellName ( cell )
365- }
385+ tag = { cellName ( cell ) }
366386 currentDropTarget = { currentDropTarget ( ) }
367387 setCurrentDropTarget = { setCurrentDropTarget }
368- isActive = { isActive ( ) }
369- replaceCommands = { retypeCommands ( i ( ) ) }
388+ isActive = { isActive ( ) }
389+ replaceCommands = { retypeCommands (
390+ i ( ) ,
391+ cellType ( cell ) ,
392+ cellName ( cell ) ,
393+ ) }
370394 >
371395 < Switch >
372396 < Match when = { cell . tag === "rich-text" } >
0 commit comments