-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVsCodeEditor.vb
More file actions
82 lines (64 loc) · 2.76 KB
/
VsCodeEditor.vb
File metadata and controls
82 lines (64 loc) · 2.76 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
Imports System.Threading
Imports Microsoft.VisualBasic.ComponentModel
Imports Microsoft.VisualBasic.Net.Http
Imports Microsoft.VisualBasic.Net.Protocols.ContentTypes
Imports Microsoft.Web.WebView2.Core
Public Class VsCodeEditor : Implements IFileReference
Public Shared ReadOnly Property vscode_url As String
Get
Return $"http://localhost:{lspclient.lsp_server}/index.html"
End Get
End Property
Public Property FilePath As String Implements IFileReference.FilePath
Public ReadOnly Property MimeType As ContentType() Implements IFileReference.MimeType
Get
Return {}
End Get
End Property
Public ReadOnly Property ScriptText As String
Get
Dim getter_js As String = "rstudio.getCodeText();"
Dim fetch = WebView21.ExecuteScriptAsync(getter_js)
Call fetch.Wait()
Return fetch.Result
End Get
End Property
Public Event OnFocus()
Public Event EditCode()
Private Async Sub VsCodeEditor_Load(sender As Object, e As EventArgs) Handles Me.Load
Await WebKit.Init(WebView21)
End Sub
Private Sub WebView21_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles WebView21.CoreWebView2InitializationCompleted
WebView21.CoreWebView2.Navigate(vscode_url)
End Sub
Dim ready As Boolean = False
Private Sub WebView21_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView21.NavigationCompleted
If FilePath Is Nothing Then
WebView21.ExecuteScriptAsync($"run_vscode('','r');")
Else
WebView21.ExecuteScriptAsync($"run_vscode('{FilePath.Replace("\", "/")}','r');")
End If
ready = True
End Sub
Public Sub LoadScript(str As String)
Call Task.Run(
Sub()
Do While App.Running AndAlso Not ready
Call Thread.Sleep(1)
Loop
Call WebView21.ExecuteScriptAsync($"run_vscode('base64://{str.Base64String}','r');")
End Sub)
End Sub
Private Sub WebView21_NavigationStarting(sender As Object, e As CoreWebView2NavigationStartingEventArgs) Handles WebView21.NavigationStarting
ready = False
End Sub
Private Sub WebView21_GotFocus(sender As Object, e As EventArgs) Handles WebView21.GotFocus
RaiseEvent OnFocus()
End Sub
Private Sub webView2_WebMessageReceived(sender As Object, args As CoreWebView2WebMessageReceivedEventArgs) Handles WebView21.WebMessageReceived
Dim message = args.TryGetWebMessageAsString()
If message = "input" OrElse message = "change" Then
RaiseEvent EditCode()
End If
End Sub
End Class