Skip to content

Commit

Permalink
Simplify setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Mar 22, 2024
1 parent a4928cd commit 82b2d23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
17 changes: 6 additions & 11 deletions demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ const el = document.querySelector('#editor')!

createEditor({
parent: el,
state: {
doc,
extensions: [
linter((view) => {
return [{ from: 0, to: 46, message: "This is what warnings look like", severity: "warning" }]
})
],
events: {
changeInterval: 500, // `change` events are debounced by default (300ms)
}
}
doc,
extensions: [
linter((view) => {
return [{ from: 0, to: 46, message: "This is what warnings look like", severity: "warning" }]
})
]
})

el.addEventListener('focus', console.log)
Expand Down
3 changes: 1 addition & 2 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export default [
if (completionStatus(e.state)) return acceptCompletion(e);
return false
},
},

}
])
]
32 changes: 11 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import { EditorState, EditorStateConfig } from '@codemirror/state'
import { EditorState, EditorStateConfig, Extension } from '@codemirror/state'

Check failure on line 1 in src/index.ts

View workflow job for this annotation

GitHub Actions / deploy

'EditorState' is declared but its value is never read.

Check failure on line 1 in src/index.ts

View workflow job for this annotation

GitHub Actions / deploy

'EditorStateConfig' is declared but its value is never read.
import { EditorView, EditorViewConfig } from '@codemirror/view'
import extensions from "./extensions"
import { eventsToExtensions, EventConfig } from "./events"
import { eventsToExtensions } from "./events"

export type { EventConfig }
export { extensions, eventsToExtensions }
export const basicSetup: Extension[] = [
...extensions,
...eventsToExtensions()
]

export interface StateConfig extends EditorStateConfig {
events?: EventConfig
}

export function createState({ events, ...state }: StateConfig = {}) {
return EditorState.create({
...state,
extensions: [
...extensions,
...eventsToExtensions(events),
state.extensions ?? []
],
})
}

export function createEditor({ state, ...config }: EditorViewConfig = {}) {
export function createEditor(config: EditorViewConfig = {}) {
return new EditorView({
...config,
state: createState(state)
extensions: [
...basicSetup,
...(config.extensions ? [config.extensions] : [])
].flat()
})
}

0 comments on commit 82b2d23

Please sign in to comment.