| 68 | }; |
| 69 | |
| 70 | export default function MarkdownCell(props: MarkdownCellProps) { |
| 71 | const { codeTheme, theme } = useTheme(); |
| 72 | const { readOnly, cell } = props; |
| 73 | const defaultState = cell.text ? 'view' : 'edit'; |
| 74 | const [status, setStatus] = useState<'edit' | 'view'>(defaultState); |
| 75 | const [text, setText] = useState(cell.text); |
| 76 | const [error, setError] = useState<string | null>(null); |
| 77 | |
| 78 | useEffect(() => { |
| 79 | if (status === 'edit') { |
| 80 | setText(cell.text); |
| 81 | } |
| 82 | }, [status, cell.text]); |
| 83 | |
| 84 | // Initializes mermaid and updates it on theme change |
| 85 | useEffect(() => { |
| 86 | mermaid.initialize({ |
| 87 | startOnLoad: false, |
| 88 | theme: 'base', |
| 89 | fontFamily: 'Instrument Sans', |
| 90 | darkMode: theme === 'dark', |
| 91 | themeVariables: theme === 'dark' ? MERMAID_DARK_OVERRIDES : MERMAID_LIGHT_OVERRIDES, |
| 92 | }); |
| 93 | }, [theme]); |
| 94 | |
| 95 | // Rerenders mermaid diagrams when the cell is in view mode |
| 96 | useEffect(() => { |
| 97 | if (status === 'view') { |
| 98 | mermaid.run(); |
| 99 | } |
| 100 | }, [status]); |
| 101 | |
| 102 | const keyMap = Prec.highest( |
| 103 | keymap.of( |
| 104 | !readOnly |
| 105 | ? [ |
| 106 | { |
| 107 | key: 'Mod-Enter', |
| 108 | run: () => { |
| 109 | onSave(); |
| 110 | return true; |
| 111 | }, |
| 112 | }, |
| 113 | { |
| 114 | key: 'Escape', |
| 115 | run: () => { |
| 116 | setStatus('view'); |
| 117 | return true; |
| 118 | }, |
| 119 | }, |
| 120 | ] |
| 121 | : [], |
| 122 | ), |
| 123 | ); |
| 124 | |
| 125 | function onSave() { |
| 126 | if (readOnly) { |
| 127 | return; |