Skip to content

Commit

Permalink
Do not initialize the text in the editor using an undoable transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
grymmy committed Mar 14, 2023
1 parent ba6ed6c commit d44ec86
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 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
3 changes: 2 additions & 1 deletion 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

0 comments on commit d44ec86

Please sign in to comment.