@@ -60,6 +60,7 @@ import { useApolloClient } from "@apollo/client";
6060import { CanvasContextMenu } from "./CanvasContextMenu" ;
6161import styles from "./canvas.style.js" ;
6262import { ShareProjDialog } from "./ShareProjDialog" ;
63+ import { RichNode } from "./RichNode" ;
6364
6465const nanoid = customAlphabet ( lowercase + numbers , 20 ) ;
6566
@@ -741,7 +742,7 @@ const CodeNode = memo<Props>(function ({
741742 ) ;
742743} ) ;
743744
744- const nodeTypes = { scope : ScopeNode , code : CodeNode } ;
745+ const nodeTypes = { scope : ScopeNode , code : CodeNode , rich : RichNode } ;
745746
746747const level2color = {
747748 0 : "rgba(187, 222, 251, 0.5)" ,
@@ -768,6 +769,39 @@ function getAbsPos({ node, nodesMap }) {
768769 }
769770}
770771
772+ /**
773+ * For historical reason, the state.pod.type and DB schema pod.type are "CODE",
774+ * "DECK", "WYSIWYG", while the node types in react-flow are "code", "scope",
775+ * "rich". These two functions document this and handle the conversion.
776+ * @param dbtype
777+ * @returns
778+ */
779+ function dbtype2nodetype ( dbtype : string ) {
780+ switch ( dbtype ) {
781+ case "CODE" :
782+ return "code" ;
783+ case "DECK" :
784+ return "scope" ;
785+ case "WYSIWYG" :
786+ return "rich" ;
787+ default :
788+ throw new Error ( `unknown dbtype ${ dbtype } ` ) ;
789+ }
790+ }
791+
792+ function nodetype2dbtype ( nodetype : string ) {
793+ switch ( nodetype ) {
794+ case "code" :
795+ return "CODE" ;
796+ case "scope" :
797+ return "DECK" ;
798+ case "rich" :
799+ return "WYSIWYG" ;
800+ default :
801+ throw new Error ( `unknown nodetype ${ nodetype } ` ) ;
802+ }
803+ }
804+
771805export function Canvas ( ) {
772806 const [ nodes , setNodes , onNodesChange ] = useNodesStateSynced ( [ ] ) ;
773807 const [ edges , setEdges ] = useState < any [ ] > ( [ ] ) ;
@@ -795,7 +829,7 @@ export function Canvas() {
795829 if ( id !== "ROOT" ) {
796830 res . push ( {
797831 id : id ,
798- type : pod . type === "CODE" ? "code" : "scope" ,
832+ type : dbtype2nodetype ( pod . type ) ,
799833 data : {
800834 // label: `ID: ${id}, parent: ${pods[id].parent}, pos: ${pods[id].x}, ${pods[id].y}`,
801835 label : id ,
@@ -809,7 +843,7 @@ export function Canvas() {
809843 level,
810844 style : {
811845 backgroundColor :
812- pod . type === "CODE "
846+ pod . type !== "DECK "
813847 ? undefined
814848 : level2color [ level ] || level2color [ "default" ] ,
815849 width : pod . width || undefined ,
@@ -904,23 +938,28 @@ export function Canvas() {
904938 ) ;
905939
906940 const addNode = useCallback (
907- ( x : number , y : number , type : string ) => {
941+ ( x : number , y : number , type : "code" | "scope" | "rich" ) => {
908942 const reactFlowBounds = reactFlowWrapper . current . getBoundingClientRect ( ) ;
909943 let style ;
910944
911- // if (type === "code") type = "default";
912- if ( type === "scope" ) {
913- style = {
914- backgroundColor : level2color [ 0 ] ,
915- width : 600 ,
916- height : 600 ,
917- } ;
918- } else {
919- style = {
920- width : 300 ,
921- // we must not set the height here, otherwise the auto layout will not work
922- height : undefined ,
923- } ;
945+ switch ( type ) {
946+ case "scope" :
947+ style = {
948+ backgroundColor : level2color [ 0 ] ,
949+ width : 600 ,
950+ height : 600 ,
951+ } ;
952+ break ;
953+ case "code" :
954+ case "rich" :
955+ style = {
956+ width : 300 ,
957+ // we must not set the height here, otherwise the auto layout will not work
958+ height : undefined ,
959+ } ;
960+ break ;
961+ default :
962+ throw new Error ( `unknown type ${ type } ` ) ;
924963 }
925964
926965 const position = reactFlowInstance . project ( {
@@ -951,7 +990,7 @@ export function Canvas() {
951990 addPod ( apolloClient , {
952991 id,
953992 parent : "ROOT" ,
954- type : type === "code" ? "CODE" : "DECK" ,
993+ type : nodetype2dbtype ( type ) ,
955994 lang : "python" ,
956995 x : position . x ,
957996 y : position . y ,
@@ -1405,6 +1444,7 @@ export function Canvas() {
14051444 y = { points . y }
14061445 addCode = { ( ) => addNode ( client . x , client . y , "code" ) }
14071446 addScope = { ( ) => addNode ( client . x , client . y , "scope" ) }
1447+ addRich = { ( ) => addNode ( client . x , client . y , "rich" ) }
14081448 onShareClick = { ( ) => {
14091449 setShareOpen ( true ) ;
14101450 } }
0 commit comments