Skip to content

Commit 2b18c87

Browse files
committed
lazy load editor
1 parent 9370b86 commit 2b18c87

3 files changed

Lines changed: 61 additions & 37 deletions

File tree

src/components/sourceEditor/SourceEditor.ts

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
2-
import { Compartment } from '@codemirror/state'
3-
import { EditorView } from '@codemirror/view'
41
import type { ThemeMode } from 'pane-registry'
2+
import { Compartment, EditorState } from '@codemirror/state'
3+
import { EditorView, drawSelection, keymap, lineNumbers } from '@codemirror/view'
4+
import { defaultHighlightStyle, syntaxHighlighting, StreamLanguage } from '@codemirror/language'
5+
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
6+
import { css } from '@codemirror/lang-css'
7+
import { html } from '@codemirror/lang-html'
8+
import { javascript } from '@codemirror/lang-javascript'
9+
import { json } from '@codemirror/lang-json'
10+
import { xml } from '@codemirror/lang-xml'
11+
// import { oneDark } from '@codemirror/theme-one-dark'
12+
import { turtle } from '@codemirror/legacy-modes/mode/turtle'
13+
import { sparql } from '@codemirror/legacy-modes/mode/sparql'
14+
import { ntriples } from '@codemirror/legacy-modes/mode/ntriples'
515
import { darkThemeExtension } from './themes/dark'
616

717
export class SourceEditor {
8-
private _view: EditorView | null = null
9-
private _languageCompartment = new Compartment()
10-
private _editableCompartment = new Compartment()
18+
private _view: any = null
19+
private _languageCompartment: any = null
20+
private _editableCompartment: any = null
1121

1222
async initialize(container: HTMLElement, initialDoc = '', contentType: string = 'text/turtle', theme: ThemeMode = 'dark') {
1323
if (this._view) {
1424
this._view.destroy()
1525
}
16-
const [
17-
{ EditorState },
18-
{ drawSelection, keymap, lineNumbers },
19-
{ defaultHighlightStyle, syntaxHighlighting },
20-
{ defaultKeymap, history, historyKeymap },
21-
{ oneDark }
22-
] = await Promise.all([
23-
import(/* webpackChunkName: "codemirror-core" */ '@codemirror/state'),
24-
import(/* webpackChunkName: "codemirror-core" */ '@codemirror/view'),
25-
import(/* webpackChunkName: "codemirror-core" */ '@codemirror/language'),
26-
import(/* webpackChunkName: "codemirror-core" */ '@codemirror/commands'),
27-
import(/* webpackChunkName: "codemirror-core" */ '@codemirror/theme-one-dark')
28-
])
26+
27+
this._languageCompartment = new Compartment()
28+
this._editableCompartment = new Compartment()
2929
const languageExtension = await this._getLanguageExtension(contentType)
3030
/* we could add this if we want to update automatically
3131
perhaps or tell the user they have unsaved changes ,
@@ -103,52 +103,39 @@ export class SourceEditor {
103103
}
104104

105105
private async _getLanguageExtension(contentType: string) {
106-
const getStreamLanguage = async () =>
107-
(await import(/* webpackChunkName: "codemirror-core" */ '@codemirror/language')).StreamLanguage
108-
106+
109107
switch (contentType) {
110108
case 'text/turtle':
111109
case 'text/n3':
112-
const turtleStreamLanguage = await getStreamLanguage()
113-
const { turtle } = await import(/* webpackChunkName: "lang-rdf" */ '@codemirror/legacy-modes/mode/turtle')
114-
return turtleStreamLanguage.define(turtle)
110+
return StreamLanguage.define(turtle)
115111

116112
case 'application/sparql-update':
117113
case 'application/sparql-query':
118-
const sparqlStreamLanguage = await getStreamLanguage()
119-
const { sparql } = await import(/* webpackChunkName: "lang-rdf" */ '@codemirror/legacy-modes/mode/sparql')
120-
return sparqlStreamLanguage.define(sparql)
114+
return StreamLanguage.define(sparql)
121115

122116
case 'application/nquads':
123117
case 'application/n-quads':
124118
case 'application/n-triples':
125-
const nTriplesStreamLanguage = await getStreamLanguage()
126-
const { ntriples } = await import(/* webpackChunkName: "lang-rdf" */ '@codemirror/legacy-modes/mode/ntriples')
127-
return nTriplesStreamLanguage.define(ntriples)
119+
return StreamLanguage.define(ntriples)
128120

129121
case 'application/json':
130122
case 'application/ld+json':
131-
const { json } = await import(/* webpackChunkName: "lang-json" */ '@codemirror/lang-json')
132123
return json()
133124

134125
case 'text/html':
135126
case 'application/xhtml+xml':
136-
const { html } = await import(/* webpackChunkName: "lang-html" */ '@codemirror/lang-html')
137127
return html()
138128

139129
case 'application/rdf+xml':
140130
case 'application/xml':
141-
const { xml } = await import(/* webpackChunkName: "lang-xml" */ '@codemirror/lang-xml')
142131
return xml()
143132

144133
case 'text/css':
145-
const { css } = await import(/* webpackChunkName: "lang-css" */ '@codemirror/lang-css')
146134
return css()
147135

148136
case 'text/javascript':
149137
case 'application/javascript':
150138
case 'application/ecmascript':
151-
const { javascript } = await import(/* webpackChunkName: "lang-javascript" */ '@codemirror/lang-javascript')
152139
return javascript()
153140

154141
default:

src/components/sourceEditor/SourceEditorCard.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { html } from 'lit'
22
import { createRef, ref } from 'lit/directives/ref.js'
33
import { customElement, property } from 'lit/decorators.js'
44
import { LiveStore, NamedNode } from 'rdflib'
5+
import type { SourceEditor } from './SourceEditor'
56
import { fetchContentAndMetadata, setUnedited } from '../../helpers'
67
import styles from './SourceEditorCard.styles.css'
78
import WebComponent from '../../primitives/WebComponent'
89
import { SourcePaneState } from '../../types'
9-
import { SourceEditor } from './SourceEditor'
1010
import { getStatusSection } from '../../StatusSection'
1111

1212
@customElement('source-editor-card')
@@ -50,6 +50,7 @@ export default class SourceEditorCard extends WebComponent {
5050
const sourcePaneEditor = this._editorMount.value
5151
if (!sourcePaneEditor) return
5252
try {
53+
const { SourceEditor } = await import('./SourceEditor')
5354
const { content, metadata } = await fetchContentAndMetadata(this.store, this.subject, this.sourcePaneState)
5455
this._editor = new SourceEditor()
5556
await this._editor.initialize(sourcePaneEditor, content, metadata.contentType)

test/sourceEditor.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ jest.mock('@codemirror/theme-one-dark', () => ({
8181
oneDark: { name: 'oneDark' }
8282
}))
8383

84+
jest.mock('@codemirror/lang-css', () => ({
85+
css: jest.fn(() => ({ type: 'css' }))
86+
}))
87+
88+
jest.mock('@codemirror/lang-html', () => ({
89+
html: jest.fn(() => ({ type: 'html' }))
90+
}))
91+
92+
jest.mock('@codemirror/lang-javascript', () => ({
93+
javascript: jest.fn(() => ({ type: 'javascript' }))
94+
}))
95+
96+
jest.mock('@codemirror/lang-json', () => ({
97+
json: jest.fn(() => ({ type: 'json' }))
98+
}))
99+
100+
jest.mock('@codemirror/lang-xml', () => ({
101+
xml: jest.fn(() => ({ type: 'xml' }))
102+
}))
103+
104+
jest.mock('@codemirror/legacy-modes/mode/turtle', () => ({
105+
turtle: { name: 'turtle' }
106+
}))
107+
108+
jest.mock('@codemirror/legacy-modes/mode/sparql', () => ({
109+
sparql: { name: 'sparql' }
110+
}))
111+
112+
jest.mock('@codemirror/legacy-modes/mode/ntriples', () => ({
113+
ntriples: { name: 'ntriples' }
114+
}))
115+
116+
jest.mock('../src/components/sourceEditor/themes/dark', () => ({
117+
darkThemeExtension: [{ type: 'darkTheme' }]
118+
}))
119+
84120
const { SourceEditor } = require('../src/components/sourceEditor/SourceEditor')
85121

86122
describe('SourceEditor', () => {

0 commit comments

Comments
 (0)