({
code,
framework
}: {
code: string
framework: Framework
})
| 102 | } |
| 103 | |
| 104 | export default function CodeEditor({ |
| 105 | code, |
| 106 | framework |
| 107 | }: { |
| 108 | code: string |
| 109 | framework: Framework |
| 110 | }) { |
| 111 | const tabWidth = 2 |
| 112 | const printWidth = 200 |
| 113 | const params = useParams() |
| 114 | const id = params.id ?? 'new' |
| 115 | const uiContext = useContext(CurrentUiContext) |
| 116 | const [readOnly, setReadOnly] = useState(framework !== 'html') |
| 117 | const editor = useRef<editor.IStandaloneCodeEditor>() |
| 118 | |
| 119 | const [checkResumePos, setCheckResumePos] = useState<Position | undefined>() |
| 120 | const [formattedCode, setFormattedCode] = useState<string>('') |
| 121 | const [bufferedCode, setBufferedCode] = useState<string>('') |
| 122 | const bufferTimer = useRef<NodeJS.Timeout>() |
| 123 | const [editing, setEditing] = useState(false) |
| 124 | const throttledCode = useThrottle(code) |
| 125 | |
| 126 | const saveHistory = useSaveHistory() |
| 127 | const [rawItem, setRawItem] = useAtom(historyAtomFamily({ id })) |
| 128 | const uiState = useAtomValue(uiStateAtom) |
| 129 | const item = useMemo( |
| 130 | () => new ItemWrapper(rawItem, setRawItem, saveHistory), |
| 131 | [rawItem, setRawItem, saveHistory] |
| 132 | ) |
| 133 | const [versionIdx, setVersionIdx] = useVersion(item) |
| 134 | |
| 135 | useEffect(() => { |
| 136 | if (checkResumePos) { |
| 137 | const curVersion = item.version(versionIdx) |
| 138 | if (!curVersion.includes('.')) { |
| 139 | const newVersionIdx = item.editChapter(formattedCode, versionIdx) |
| 140 | setVersionIdx(newVersionIdx) |
| 141 | // TODO: the cursor advances funky after one edit :( |
| 142 | setTimeout(() => { |
| 143 | editor.current?.setPosition(checkResumePos) |
| 144 | }, 100) |
| 145 | } |
| 146 | setCheckResumePos(undefined) |
| 147 | } |
| 148 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 149 | }, [checkResumePos, setCheckResumePos, versionIdx, setVersionIdx]) |
| 150 | |
| 151 | const handleEditorDidMount = (e: editor.IStandaloneCodeEditor, m: Monaco) => { |
| 152 | m.editor.setTheme('openui') |
| 153 | editor.current = e |
| 154 | // This ensures we create a new version and maintain cursor position when |
| 155 | // Editing a non-point version |
| 156 | let position: Position | undefined |
| 157 | let fixPosition = false |
| 158 | e.onDidChangeModelContent(() => { |
| 159 | if (position) { |
| 160 | // E.setPosition(position) |
| 161 | position = undefined |
nothing calls this directly
no test coverage detected