Skip to content

Commit

Permalink
Refactor createEditor, createState to take all options
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Mar 20, 2024
1 parent 278e4e5 commit 541f5cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
18 changes: 12 additions & 6 deletions demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ This is the [G]chorus
# ✅ Snippets - type "title", "start_of…", "tab" or any other ChordPro directive
`

createEditor(document.querySelector('#editor')!, { doc }, {
onChange: (doc, viewUpdate) => console.log("onChange", doc, viewUpdate),
onChangeInterval: 500, // onChange events are debounced by default (300ms)
onFocus: viewUpdate => console.log("onFocus", viewUpdate),
onBlur: viewUpdate => console.log("onBlur", viewUpdate),
onPaste: (event, view) => console.log("onPaste", event, view),
createEditor({
parent: document.querySelector('#editor')!,
state: {
doc,
events: {
onChange: (doc, viewUpdate) => console.log("onChange", doc, viewUpdate),
onChangeInterval: 500, // onChange events are debounced by default (300ms)
onFocus: viewUpdate => console.log("onFocus", viewUpdate),
onBlur: viewUpdate => console.log("onBlur", viewUpdate),
onPaste: (event, view) => console.log("onPaste", event, view)
}
}
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chordbook/editor",
"version": "0.0.2",
"version": "0.0.4",
"type": "module",
"description": "The ChordPro editor used by ChordBook.app",
"module": "dist/index.js",
Expand Down
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { EditorState, EditorStateConfig } from '@codemirror/state'
import { EditorView } from '@codemirror/view'
import { EditorView, EditorViewConfig } from '@codemirror/view'
import extensions from "./extensions"
import { EventCallbacks, eventsToExtensions } from "./events"

export type { EventCallbacks }
export { extensions, eventsToExtensions }

export function createState(state: EditorStateConfig = {}, events: EventCallbacks = {}) {
export interface StateConfig extends EditorStateConfig {
events?: EventCallbacks
}

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

export function createEditor (element: HTMLElement, state: EditorStateConfig = {}, events: EventCallbacks = {}) {
export function createEditor({ state, ...config }: EditorViewConfig = {}) {
return new EditorView({
parent: element,
state: createState(state, events),
...config,
state: createState(state)
})
}

0 comments on commit 541f5cb

Please sign in to comment.