( parent: HTMLElement, initialCode: string, extra: Extension[], onChange?: (code: string) => void, )
| 509 | const baseKeymap = [indentWithTab, ...closeBracketsKeymap, ...defaultKeymap, ...historyKeymap]; |
| 510 | |
| 511 | function buildEditor( |
| 512 | parent: HTMLElement, |
| 513 | initialCode: string, |
| 514 | extra: Extension[], |
| 515 | onChange?: (code: string) => void, |
| 516 | ): WatEditor { |
| 517 | const extensions: Extension[] = [...baseExtensions, ...extra]; |
| 518 | if (onChange) { |
| 519 | extensions.push( |
| 520 | EditorView.updateListener.of((update) => { |
| 521 | if (update.docChanged) onChange(update.state.doc.toString()); |
| 522 | }), |
| 523 | ); |
| 524 | } |
| 525 | const view = new EditorView({ |
| 526 | state: EditorState.create({ doc: initialCode, extensions }), |
| 527 | parent, |
| 528 | }); |
| 529 | return { |
| 530 | view, |
| 531 | getValue: () => view.state.doc.toString(), |
| 532 | setValue(code: string) { |
| 533 | view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: code } }); |
| 534 | }, |
| 535 | destroy: () => view.destroy(), |
| 536 | }; |
| 537 | } |
| 538 | |
| 539 | export function createJsEditor( |
| 540 | parent: HTMLElement, |
no test coverage detected