@tomjs/vite-plugin-vscode 2.5.3 → 2.5.4

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