@tomjs/vite-plugin-vscode 2.5.4 → 2.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,207 @@
1
+ // src/webview/template.html
2
+ var template_default = `<!doctype html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
8
+ <style>
9
+ html,
10
+ body {
11
+ width: 100%;
12
+ height: 100%;
13
+ margin: 0;
14
+ padding: 0;
15
+ overflow: hidden;
16
+ }
17
+
18
+ #webview-patch-iframe {
19
+ width: 100%;
20
+ height: 100%;
21
+ border: none;
22
+ }
23
+
24
+ .outer {
25
+ width: 100%;
26
+ height: 100%;
27
+ overflow: hidden;
28
+ }
29
+ </style>
30
+
31
+ <script type="module" id="webview-patch">
32
+ const TAG = '[@tomjs:vscode:extension] ';
33
+
34
+ function onDomReady(callback, doc) {
35
+ const _doc = doc || document;
36
+ if (_doc.readyState === 'interactive' || _doc.readyState === 'complete') {
37
+ callback();
38
+ } else {
39
+ _doc.addEventListener('DOMContentLoaded', callback);
40
+ }
41
+ }
42
+
43
+ let vsCodeApi;
44
+
45
+ function getApi() {
46
+ if (vsCodeApi) return vsCodeApi;
47
+ return (vsCodeApi = acquireVsCodeApi());
48
+ }
49
+
50
+ function sendInitData(iframe) {
51
+ console.log(TAG + 'init data');
52
+ const dataset = {};
53
+ Object.keys(document.body.dataset).forEach(key => {
54
+ dataset[key] = document.body.dataset[key];
55
+ });
56
+
57
+ iframe.contentWindow.postMessage(
58
+ {
59
+ type: '[vscode:extension]:init',
60
+ data: {
61
+ state: getApi().getState(),
62
+ style: document.getElementById('_defaultStyles').innerHTML,
63
+ root: {
64
+ cssText: document.documentElement.style.cssText,
65
+ },
66
+ body: {
67
+ dataset: dataset,
68
+ className: document.body.className,
69
+ role: document.body.getAttribute('role'),
70
+ },
71
+ },
72
+ },
73
+ '*',
74
+ );
75
+ }
76
+
77
+ function observeAttributeChanges(element, attributeName, callback) {
78
+ const observer = new MutationObserver(function (mutationsList) {
79
+ for (let mutation of mutationsList) {
80
+ if (mutation.type === 'attributes' && mutation.attributeName === attributeName) {
81
+ callback(mutation.target.getAttribute(attributeName));
82
+ }
83
+ }
84
+ });
85
+ observer.observe(element, { attributes: true });
86
+ return observer;
87
+ }
88
+
89
+ // message handler
90
+ let iframeLoaded = false;
91
+ const cacheMessages = [];
92
+
93
+ function handleMessage(e) {
94
+ const iframe = document.getElementById('webview-patch-iframe');
95
+ if (!iframeLoaded || !iframe) {
96
+ return;
97
+ }
98
+ if (e.origin.startsWith('vscode-webview://')) {
99
+ iframe.contentWindow.postMessage(e.data, '*');
100
+ } else if ('{{serverUrl}}'.startsWith(e.origin)) {
101
+ const { type, data } = e.data;
102
+ console.log(TAG + ' received:', e.data);
103
+ if (type === '[vscode:client]:postMessage') {
104
+ getApi().postMessage(data);
105
+ } else if (type === '[vscode:client]:getState') {
106
+ iframe.contentWindow.postMessage(
107
+ {
108
+ type: '[vscode:client]:getState',
109
+ data: getApi().getState(),
110
+ },
111
+ '*',
112
+ );
113
+ } else if (type === '[vscode:client]:setState') {
114
+ getApi().setState(data);
115
+ }
116
+ }
117
+ }
118
+
119
+ window.addEventListener('message', function (event) {
120
+ if (event.origin.startsWith('vscode-webview://')) {
121
+ cacheMessages.push(event);
122
+ return;
123
+ }
124
+ handleMessage(event);
125
+ });
126
+
127
+ let isCacheWorking = false;
128
+ setInterval(() => {
129
+ if (isCacheWorking) {
130
+ return;
131
+ }
132
+
133
+ isCacheWorking = true;
134
+ if (iframeLoaded) {
135
+ let event = cacheMessages.shift();
136
+ while (event) {
137
+ handleMessage(event);
138
+ event = cacheMessages.shift();
139
+ }
140
+ }
141
+ isCacheWorking = false;
142
+ }, 50);
143
+
144
+ onDomReady(function () {
145
+ /** @type {HTMLIFrameElement} */
146
+ const iframe = document.getElementById('webview-patch-iframe');
147
+ observeAttributeChanges(document.body, 'class', function (className) {
148
+ sendInitData(iframe);
149
+ });
150
+
151
+ onDomReady(function () {
152
+ iframeLoaded = true;
153
+ sendInitData(iframe);
154
+ }, iframe.contentDocument);
155
+
156
+ iframe.addEventListener('load', function (e) {
157
+ iframeLoaded = true;
158
+
159
+ let interval = setInterval(() => {
160
+ try {
161
+ if (document.getElementById('_defaultStyles')) {
162
+ sendInitData(iframe);
163
+ // addListeners(iframe);
164
+ clearInterval(interval);
165
+ return;
166
+ }
167
+ } catch (e) {
168
+ clearInterval(interval);
169
+ console.error(e);
170
+ }
171
+ }, 10);
172
+ });
173
+ });
174
+ </script>
175
+ </head>
176
+
177
+ <body>
178
+ <div class="outer">
179
+ <iframe
180
+ id="webview-patch-iframe"
181
+ frameborder="0"
182
+ sandbox="allow-scripts allow-same-origin allow-forms allow-pointer-lock allow-downloads"
183
+ allow="cross-origin-isolated; autoplay; clipboard-read; clipboard-write"
184
+ src="{{serverUrl}}"
185
+ ></iframe>
186
+ </div>
187
+ </body>
188
+ </html>
189
+ `;
190
+
191
+ // src/webview/webview.ts
192
+ function getHtml(options) {
193
+ const opts = {
194
+ serverUrl: ""
195
+ };
196
+ if (typeof options === "string") {
197
+ opts.serverUrl = options;
198
+ } else {
199
+ Object.assign(opts, options);
200
+ }
201
+ return template_default.replace(new RegExp("{{serverUrl}}", "g"), opts.serverUrl);
202
+ }
203
+ var webview_default = getHtml;
204
+ export {
205
+ webview_default as default,
206
+ getHtml
207
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-vscode",
3
- "version": "2.5.4",
3
+ "version": "2.5.5",
4
4
  "description": "Use vue/react to develop 'vscode extension webview', supporting esm/cjs",
5
5
  "keywords": [
6
6
  "vite",