Skip to content

Commit

Permalink
Merge pull request hackclub#968 from grymmy/fix-initial-undo
Browse files Browse the repository at this point in the history
Do not initialize the text in the editor using an undoable transaction
  • Loading branch information
sampoder committed Mar 15, 2023
2 parents 047fdf5 + 07baf14 commit 4617955
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/codemirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export default function CodeMirror(props: CodeMirrorProps) {
if (!parent.current) throw new Error('Oh golly! The editor parent ref is null')
let lastCode: string = props.initialCode ?? ''
const editor = new EditorView({
state: createEditorState(() => {
state: createEditorState(props.initialCode ? props.initialCode : "", () => {
if (editor.state.doc.toString() === lastCode) return
lastCode = editor.state.doc.toString()
onCodeChangeRef.current?.()
}, () => onRunShortcutRef.current?.()),
parent: parent.current
})
if (props.initialCode) editor.dispatch({ changes: { from: 0, insert: props.initialCode } })

props.onEditorView?.(editor)
}, [])

Expand Down
5 changes: 3 additions & 2 deletions src/lib/codemirror/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { effect, signal } from '@preact/signals'
import { h, render } from 'preact'
import SearchBox from '../../components/search-box'

export function createEditorState(onUpdate = () => {}, onRunShortcut = () => {}): EditorState {
export function createEditorState(initialCode: string, onUpdate = () => {}, onRunShortcut = () => {}): EditorState {
return EditorState.create({
doc: initialCode,
extensions: [
lineNumbers(),
highlightActiveLineGutter(),
Expand Down Expand Up @@ -93,4 +94,4 @@ export function createEditorState(onUpdate = () => {}, onRunShortcut = () => {})
widgets
]
})
}
}

0 comments on commit 4617955

Please sign in to comment.