-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathhds-code-editor.ts
181 lines (154 loc) · 4.82 KB
/
hds-code-editor.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { assert } from '@ember/debug';
import Modifier from 'ember-modifier';
import { dropTask } from 'ember-concurrency';
import hdsDarkTheme from './hds-code-editor/themes/hds-dark-theme.ts';
import hdsDarkHighlightStyle from './hds-code-editor/highlight-styles/hds-dark-highlight-style.ts';
import type { PositionalArgs, NamedArgs } from 'ember-modifier';
import type { EditorView, ViewUpdate } from '@codemirror/view';
import type {
HdsCodeEditorLanguages,
CodemirrorGoModule,
CodemirrorJsonModule,
CodemirrorSqlModule,
CodemirrorHclModule,
CodemirrorLanguageModule,
} from 'src/types/hds-code-editor.types';
export interface HdsCodeEditorSignature {
Args: {
Named: {
language?: HdsCodeEditorLanguages;
value?: string;
onInput?: (newVal: string) => void;
onBlur?: (editor: EditorView, event: FocusEvent) => void;
onSetup?: (editor: EditorView) => unknown;
};
};
}
const LOADER_HEIGHT = '164px';
export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignature> {
editor!: EditorView;
element!: HTMLElement;
onInput: HdsCodeEditorSignature['Args']['Named']['onInput'];
observer!: IntersectionObserver;
modify(
element: HTMLElement,
positional: PositionalArgs<HdsCodeEditorSignature>,
named: NamedArgs<HdsCodeEditorSignature>
): void {
assert('HdsCodeEditor must have an element', element);
this.observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// @ts-ignore
if (entry.isIntersecting && this.setupTask.performCount === 0) {
// @ts-ignore
this.setupTask.perform(element, positional, named);
}
});
},
{
rootMargin: LOADER_HEIGHT,
}
);
this.observer.observe(element);
}
willRemove() {
this.observer.disconnect();
}
@dropTask *loadLanguageTask(language?: HdsCodeEditorLanguages) {
if (language === undefined) {
return;
}
let module: CodemirrorLanguageModule | null = null;
let languageFunction = null;
switch (language) {
case 'go':
module = yield import('@codemirror/lang-go');
languageFunction = (module as CodemirrorGoModule).go;
break;
case 'json':
module = yield import('@codemirror/lang-json');
languageFunction = (module as CodemirrorJsonModule).json;
break;
case 'sql':
module = yield import('@codemirror/lang-sql');
languageFunction = (module as CodemirrorSqlModule).sql;
break;
case 'hcl':
module = yield import('codemirror-lang-hcl');
languageFunction = (module as CodemirrorHclModule).hcl;
break;
default:
throw new Error(`Language ${language} is not supported`);
}
return languageFunction();
}
@dropTask *setupTask(
element: HTMLElement,
_positional: PositionalArgs<HdsCodeEditorSignature>,
named: NamedArgs<HdsCodeEditorSignature>
) {
const { onInput, onSetup, language, value } = named;
const [
{
EditorView,
keymap,
lineNumbers,
highlightActiveLineGutter,
highlightSpecialChars,
highlightActiveLine,
},
{ EditorState },
{ defaultKeymap, history, historyKeymap },
{ bracketMatching, syntaxHighlighting },
] = yield Promise.all([
import('@codemirror/view'),
import('@codemirror/state'),
import('@codemirror/commands'),
import('@codemirror/language'),
]);
this.onInput = onInput;
// @ts-ignore
const languageExtension = yield this.loadLanguageTask.perform(language);
let extensions = [
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
highlightActiveLine(),
EditorView.updateListener.of((update: ViewUpdate) => {
// toggle a class if the update has/does not have a selection
if (update.selectionSet) {
update.view.dom.classList.toggle(
'cm-hasSelection',
!update.state.selection.main.empty
);
}
// call the onInput callback if the document has changed
if (!update.docChanged || this.onInput === undefined) {
return;
}
this.onInput(update.state.doc.toString());
}),
hdsDarkTheme,
keymap.of([...defaultKeymap, ...historyKeymap]),
bracketMatching(),
syntaxHighlighting(hdsDarkHighlightStyle),
history(),
];
if (languageExtension !== undefined) {
extensions = [languageExtension, ...extensions];
}
const state = EditorState.create({
doc: value,
extensions,
});
const editor = new EditorView({ state, parent: element });
this.editor = editor;
this.element = element;
onSetup?.(editor);
}
}