-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSourceEditor.ts
More file actions
151 lines (129 loc) · 4.27 KB
/
Copy pathSourceEditor.ts
File metadata and controls
151 lines (129 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type { ThemeMode } from 'pane-registry'
import { Compartment, EditorState } from '@codemirror/state'
import { EditorView, drawSelection, keymap, lineNumbers } from '@codemirror/view'
import { defaultHighlightStyle, syntaxHighlighting, StreamLanguage } from '@codemirror/language'
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
import { css } from '@codemirror/lang-css'
import { html } from '@codemirror/lang-html'
import { javascript } from '@codemirror/lang-javascript'
import { json } from '@codemirror/lang-json'
import { xml } from '@codemirror/lang-xml'
import { vscodeDark, vscodeLight } from '@uiw/codemirror-theme-vscode'
import { turtle } from '@codemirror/legacy-modes/mode/turtle'
import { sparql } from '@codemirror/legacy-modes/mode/sparql'
import { ntriples } from '@codemirror/legacy-modes/mode/ntriples'
export class SourceEditor {
private _view: any = null
private _languageCompartment: any = null
private _editableCompartment: any = null
private _onDirtyChange?: (dirty: boolean) => void
private _isDirty = false
async initialize(container: HTMLElement, initialDoc = '', contentType: string = 'text/turtle', theme: ThemeMode = 'dark', onDirtyChange?: (dirty: boolean) => void) {
if (this._view) {
this._view.destroy()
}
this._languageCompartment = new Compartment()
this._editableCompartment = new Compartment()
this._onDirtyChange = onDirtyChange
this._isDirty = false
const languageExtension = await this._getLanguageExtension(contentType)
const state = EditorState.create({
doc: initialDoc,
extensions: [
theme === 'dark' ? vscodeDark : vscodeLight,
EditorView.theme({
'&': {
minHeight: '6lh'
}
}),
this._languageCompartment.of(languageExtension),
this._editableCompartment.of(EditorView.editable.of(true)),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
lineNumbers(),
history(),
drawSelection(),
EditorView.lineWrapping,
EditorView.updateListener.of((update) => {
if (update.docChanged && !this._isDirty) {
this._isDirty = true
this._onDirtyChange?.(true)
}
}),
keymap.of([
...defaultKeymap,
...historyKeymap
])
]
})
this._view = new EditorView({
state,
parent: container
})
}
destroy() {
this._view?.destroy()
this._view = null
this._isDirty = false
}
getValue(): string {
return this._view ? this._view.state.doc.toString() : ''
}
replaceContent(text: string) {
if (!this._view) return
const current = this._view.state.doc.toString()
if (current === text) return
this._view.dispatch({
changes: { from: 0, to: this._view.state.doc.length, insert: text }
})
}
resetDirtyState() {
this._isDirty = false
}
setReadOnly(readOnly: boolean) {
if (!this._view) return
this._view.dispatch({
effects: this._editableCompartment.reconfigure(EditorView.editable.of(!readOnly))
})
}
focusEditor() {
this._view?.focus()
}
async setLanguage(contentType: string) {
if (!this._view) return
const extension = await this._getLanguageExtension(contentType)
this._view.dispatch({
effects: this._languageCompartment.reconfigure(extension)
})
}
private async _getLanguageExtension(contentType: string) {
switch (contentType) {
case 'text/turtle':
case 'text/n3':
return StreamLanguage.define(turtle)
case 'application/sparql-update':
case 'application/sparql-query':
return StreamLanguage.define(sparql)
case 'application/nquads':
case 'application/n-quads':
case 'application/n-triples':
return StreamLanguage.define(ntriples)
case 'application/json':
case 'application/ld+json':
return json()
case 'text/html':
case 'application/xhtml+xml':
return html()
case 'application/rdf+xml':
case 'application/xml':
return xml()
case 'text/css':
return css()
case 'text/javascript':
case 'application/javascript':
case 'application/ecmascript':
return javascript()
default:
return []
}
}
}