@@ -22,7 +22,6 @@ import {
2222} from "@codemirror/language" ;
2323import { lintGutter , lintKeymap } from "@codemirror/lint" ;
2424import { highlightSelectionMatches , searchKeymap } from "@codemirror/search" ;
25- import type { Extension } from "@codemirror/state" ;
2625import { EditorState } from "@codemirror/state" ;
2726import type { KeyBinding } from "@codemirror/view" ;
2827import {
@@ -81,22 +80,14 @@ interface EditorAndAttachment {
8180 renderEditor : Attachment < HTMLDivElement | HTMLPreElement > ;
8281}
8382
84- function setup (
85- value : string | undefined ,
86- extensions : Extension [ ] ,
87- ) : EditorAndAttachment {
88- const view = new EditorView ( {
89- state : EditorState . create (
90- value !== undefined ? { doc : value , extensions } : { extensions } ,
91- ) ,
92- } ) ;
83+ function editor_attachment ( editor : EditorView ) : EditorAndAttachment {
9384 return {
94- editor : view ,
85+ editor,
9586 renderEditor : ( el ) => {
96- el . appendChild ( view . dom ) ;
87+ el . appendChild ( editor . dom ) ;
9788
9889 return ( ) => {
99- el . removeChild ( view . dom ) ;
90+ el . removeChild ( editor . dom ) ;
10091 } ;
10192 } ,
10293 } ;
@@ -105,12 +96,16 @@ function setup(
10596/**
10697 * A basic readonly editor for an asynchronously loaded document.
10798 */
108- export function initDocumentPreviewEditor ( value : string ) : EditorAndAttachment {
109- return setup ( value , [
110- baseExtensions ,
111- EditorState . readOnly . of ( true ) ,
112- placeholder ( "Loading..." ) ,
113- ] ) ;
99+ export function initDocumentPreviewEditor ( ) : EditorAndAttachment {
100+ return editor_attachment (
101+ new EditorView ( {
102+ extensions : [
103+ baseExtensions ,
104+ EditorState . readOnly . of ( true ) ,
105+ placeholder ( "Loading..." ) ,
106+ ] ,
107+ } ) ,
108+ ) ;
114109}
115110
116111/**
@@ -121,11 +116,14 @@ export class BeancountTextarea extends HTMLTextAreaElement {
121116 super ( ) ;
122117 getBeancountLanguageSupport ( )
123118 . then ( ( beancount ) => {
124- const { editor } = setup ( this . value , [
125- beancount ,
126- syntaxHighlighting ( defaultHighlightStyle ) ,
127- EditorView . editable . of ( false ) ,
128- ] ) ;
119+ const editor = new EditorView ( {
120+ doc : this . value ,
121+ extensions : [
122+ beancount ,
123+ syntaxHighlighting ( defaultHighlightStyle ) ,
124+ EditorView . editable . of ( false ) ,
125+ ] ,
126+ } ) ;
129127 this . parentNode ?. insertBefore ( editor . dom , this ) ;
130128 this . style . display = "none" ;
131129 } )
@@ -140,64 +138,80 @@ export function initBeancountEditor(
140138 value : string ,
141139 onDocChanges : ( s : EditorState ) => void ,
142140 commands : KeyBinding [ ] ,
143- beancount : LanguageSupport ,
141+ beancount : ( ) => LanguageSupport ,
144142) : EditorAndAttachment {
145143 const $indent = store_get ( indent ) ;
146144 const $currency_column = store_get ( currency_column ) ;
147- return setup ( value , [
148- beancount ,
149- indentUnit . of ( " " . repeat ( $indent ) ) ,
150- ...( $currency_column ? [ rulerPlugin ( $currency_column - 1 ) ] : [ ] ) ,
151- keymap . of ( commands ) ,
152- EditorView . updateListener . of ( ( update ) => {
153- if ( update . docChanged ) {
154- onDocChanges ( update . state ) ;
155- }
145+ return editor_attachment (
146+ new EditorView ( {
147+ doc : value ,
148+ extensions : [
149+ beancount ( ) ,
150+ indentUnit . of ( " " . repeat ( $indent ) ) ,
151+ ...( $currency_column ? [ rulerPlugin ( $currency_column - 1 ) ] : [ ] ) ,
152+ keymap . of ( commands ) ,
153+ EditorView . updateListener . of ( ( update ) => {
154+ if ( update . docChanged ) {
155+ onDocChanges ( update . state ) ;
156+ }
157+ } ) ,
158+ baseExtensions ,
159+ syntaxHighlighting ( beancountEditorHighlight ) ,
160+ ] ,
156161 } ) ,
157- baseExtensions ,
158- syntaxHighlighting ( beancountEditorHighlight ) ,
159- ] ) ;
162+ ) ;
160163}
161164
162165/**
163166 * A basic readonly BQL editor that only does syntax highlighting.
164167 */
165168export function initReadonlyQueryEditor ( value : string ) : EditorAndAttachment {
166- return setup ( value , [
167- bql ,
168- syntaxHighlighting ( beancountQueryHighlight ) ,
169- EditorView . editable . of ( false ) ,
170- ] ) ;
169+ return editor_attachment (
170+ new EditorView ( {
171+ doc : value ,
172+ extensions : [
173+ bql ,
174+ syntaxHighlighting ( beancountQueryHighlight ) ,
175+ EditorView . editable . of ( false ) ,
176+ ] ,
177+ } ) ,
178+ ) ;
171179}
172180
173181/**
174182 * The main BQL editor.
175183 */
176184export function initQueryEditor (
177- value : string | undefined ,
185+ value : string ,
178186 onDocChanges : ( s : EditorState ) => void ,
179- _placeholder : string ,
180- submit : ( ) => void ,
187+ placeholder_value : string ,
188+ get_submit : ( ) => ( ) => void ,
181189) : EditorAndAttachment {
182- return setup ( value , [
183- bql ,
184- EditorView . updateListener . of ( ( update ) => {
185- if ( update . docChanged ) {
186- onDocChanges ( update . state ) ;
187- }
190+ return editor_attachment (
191+ new EditorView ( {
192+ doc : value ,
193+ extensions : [
194+ bql ,
195+ EditorView . updateListener . of ( ( update ) => {
196+ if ( update . docChanged ) {
197+ onDocChanges ( update . state ) ;
198+ }
199+ } ) ,
200+ keymap . of ( [
201+ {
202+ key : "Control-Enter" ,
203+ mac : "Meta-Enter" ,
204+ run : ( ) => {
205+ const submit = get_submit ( ) ;
206+ submit ( ) ;
207+ return true ;
208+ } ,
209+ } ,
210+ ] ) ,
211+ placeholder ( placeholder_value ) ,
212+ baseExtensions ,
213+ syntaxHighlighting ( beancountQueryHighlight ) ,
214+ ] ,
188215 } ) ,
189- keymap . of ( [
190- {
191- key : "Control-Enter" ,
192- mac : "Meta-Enter" ,
193- run : ( ) => {
194- submit ( ) ;
195- return true ;
196- } ,
197- } ,
198- ] ) ,
199- placeholder ( _placeholder ) ,
200- baseExtensions ,
201- syntaxHighlighting ( beancountQueryHighlight ) ,
202- ] ) ;
216+ ) ;
203217}
0 commit comments