-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.ts
More file actions
96 lines (81 loc) · 2.64 KB
/
source.ts
File metadata and controls
96 lines (81 loc) · 2.64 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
import { Element } from '@core/element'
import { html, css } from 'lit'
import { customElement } from 'lit/decorators.js'
import { consume } from '@lit/context'
import { EditorView, basicSetup } from 'codemirror'
import type { EditorViewConfig } from '@codemirror/view'
import { javascript } from '@codemirror/lang-javascript'
import { oneDark } from '@codemirror/theme-one-dark'
import { sourceContext } from '../../controller/DataManager.js'
import '../placeholder.js'
const SOURCE_COMPONENT = 'wdio-devtools-source'
@customElement(SOURCE_COMPONENT)
export class DevtoolsSource extends Element {
static styles = [...Element.styles, css`
:host {
display: flex;
width: 100%;
height: 100%;
}
.cm-editor {
width: 100%;
height: 100%;
padding: 10px 0px;
}
.cm-content {
padding: 0!important;
}
`]
@consume({ context: sourceContext, subscribe: true })
sources: Record<string, string> = {}
connectedCallback(): void {
super.connectedCallback()
window.addEventListener('app-source-highlight', this.#highlightCallSource.bind(this))
}
#renderEditor (filePath: string, highlightLine?: number) {
if (!this.sources) return
const source = this.sources[filePath]
if (!source) return
const container = this.shadowRoot?.querySelector('section') || this.shadowRoot?.querySelector('.cm-editor')
if (!container) return
const opts: EditorViewConfig = {
root: this.shadowRoot!,
extensions: [basicSetup, javascript(), oneDark],
doc: source,
selection: { anchor: 4 }
}
const editorView = new EditorView(opts)
container.replaceWith(editorView.dom)
if (highlightLine && highlightLine > 0) {
try {
const lineInfo = editorView.state.doc.line(highlightLine)
requestAnimationFrame(() => {
editorView.dispatch({
selection: { anchor: lineInfo.from },
effects: EditorView.scrollIntoView(lineInfo.from, { y: 'center' })
})
})
} catch { /* ignore */ }
}
}
#highlightCallSource (ev: CustomEvent<string>) {
const [filePath, line] = ev.detail.split(':')
this.#renderEditor(filePath, parseInt(line, 10))
this.closest('wdio-devtools-tabs')?.activateTab('Source')
}
render() {
const sourceFileNames = Object.keys(this.sources || {})
if (sourceFileNames.length === 0) {
return html`<wdio-devtools-placeholder></wdio-devtools-placeholder>`
}
this.#renderEditor(sourceFileNames[0])
return html`
<section class="p-2">loading...</section>
`
}
}
declare global {
interface HTMLElementTagNameMap {
[SOURCE_COMPONENT]: DevtoolsSource
}
}