@jsenv/core 25.4.0 → 25.4.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.
Files changed (32) hide show
  1. package/dist/build_manifest.js +4 -4
  2. package/dist/compile_proxy/asset-manifest.json +2 -2
  3. package/dist/compile_proxy/{compile_proxy.html__inline__20_2334a374.js.map → compile_proxy.html__inline__20_f4285042.js.map} +3 -2
  4. package/dist/compile_proxy/{compile_proxy_9f737eaf.html → compile_proxy_ab528227.html} +6 -4
  5. package/dist/redirector/asset-manifest.json +2 -2
  6. package/dist/redirector/{redirector.html__inline__12_009c47c7.js.map → redirector.html__inline__12_404b8295.js.map} +6 -5
  7. package/dist/redirector/{redirector_96f74871.html → redirector_6df2620a.html} +57 -52
  8. package/dist/toolbar/asset-manifest.json +2 -2
  9. package/dist/toolbar/{toolbar.main_86335f90.js.map → toolbar.main_279b3a68.js.map} +175 -180
  10. package/dist/toolbar/{toolbar_d3a918a4.html → toolbar_0a91ca3b.html} +1232 -1285
  11. package/dist/toolbar_injector/asset-manifest.json +2 -2
  12. package/dist/toolbar_injector/toolbar_injector_34f6ad8e.js +976 -0
  13. package/dist/toolbar_injector/toolbar_injector_34f6ad8e.js.map +294 -0
  14. package/package.json +4 -5
  15. package/src/dev_server.js +36 -6
  16. package/src/importUsingChildProcess.js +1 -0
  17. package/src/internal/compiling/babel_plugin_transform_import_meta.js +23 -55
  18. package/src/internal/compiling/js-compilation-service/transformJs.js +1 -3
  19. package/src/internal/compiling/jsenvCompilerForHtml.js +9 -11
  20. package/src/internal/compiling/jsenv_directory/comparison_utils.js +4 -1
  21. package/src/internal/compiling/startCompileServer.js +1 -0
  22. package/src/internal/dev_server/toolbar/compilation/toolbar.compilation.js +9 -2
  23. package/src/internal/dev_server/toolbar/toolbar.html +6 -0
  24. package/src/internal/dev_server/toolbar/toolbar.injector.js +11 -3
  25. package/src/internal/dev_server/toolbar/toolbar.main.js +10 -14
  26. package/src/internal/features/browser_feature_detection/browser_feature_detection.js +7 -3
  27. package/src/internal/import-resolution/importmap_default.js +2 -17
  28. package/src/internal/node_launcher/createControllableNodeProcess.js +0 -3
  29. package/src/internal/node_runtime/nodeControllableFile.mjs +1 -5
  30. package/src/internal/redirector/redirector.html +6 -4
  31. package/dist/toolbar_injector/toolbar_injector_978bbd14.js +0 -267
  32. package/dist/toolbar_injector/toolbar_injector_978bbd14.js.map +0 -119
@@ -139,7 +139,7 @@ export const compileHtml = async ({
139
139
  addHtmlMutation,
140
140
  })
141
141
  }
142
- await visitImportmapScript({
142
+ await visitImportmapScripts({
143
143
  logger,
144
144
  url,
145
145
  compiledUrl,
@@ -245,7 +245,7 @@ const visitRessourceHints = async ({ ressourceHints, addHtmlMutation }) => {
245
245
  )
246
246
  }
247
247
 
248
- const visitImportmapScript = async ({
248
+ const visitImportmapScripts = async ({
249
249
  logger,
250
250
  url,
251
251
  compiledUrl,
@@ -265,14 +265,15 @@ const visitImportmapScript = async ({
265
265
  const type = typeAttribute ? typeAttribute.value : "application/javascript"
266
266
  return type === "importmap"
267
267
  })
268
+ const jsenvImportmap = getDefaultImportmap(compiledUrl, {
269
+ projectDirectoryUrl,
270
+ compileDirectoryUrl,
271
+ })
272
+
268
273
  // in case there is no importmap, force the presence
269
274
  // so that '@jsenv/core/' are still remapped
270
275
  if (importmapScripts.length === 0) {
271
- const defaultImportMap = getDefaultImportmap(compiledUrl, {
272
- projectDirectoryUrl,
273
- compileDirectoryUrl,
274
- })
275
- const defaultImportMapAsText = JSON.stringify(defaultImportMap, null, " ")
276
+ const defaultImportMapAsText = JSON.stringify(jsenvImportmap, null, " ")
276
277
  onHtmlImportmapInfo({
277
278
  url: compiledUrl,
278
279
  text: defaultImportMapAsText,
@@ -324,6 +325,7 @@ const visitImportmapScript = async ({
324
325
  )
325
326
  importmap = {}
326
327
  }
328
+ importmap = composeTwoImportMaps(jsenvImportmap, importmap)
327
329
  const importmapAsText = JSON.stringify(importmap, null, " ")
328
330
  onHtmlImportmapInfo({
329
331
  url: importmapUrl,
@@ -343,10 +345,6 @@ const visitImportmapScript = async ({
343
345
  return
344
346
  }
345
347
 
346
- const jsenvImportmap = getDefaultImportmap(compiledUrl, {
347
- projectDirectoryUrl,
348
- compileDirectoryUrl,
349
- })
350
348
  const htmlImportmap = JSON.parse(
351
349
  getHtmlNodeTextNode(firstImportmapScript).value,
352
350
  )
@@ -20,5 +20,8 @@ export const sameValueInTwoObjects = (object, secondObject) => {
20
20
  }
21
21
 
22
22
  export const sameValuesInTwoArrays = (array, secondArray) => {
23
- return array.every((value) => secondArray.includes(value))
23
+ return (
24
+ array.length === secondArray.length &&
25
+ array.every((value) => secondArray.includes(value))
26
+ )
24
27
  }
@@ -289,6 +289,7 @@ export const startCompileServer = async ({
289
289
  sourcemapMappingFileInfo.url,
290
290
  projectDirectoryUrl,
291
291
  ),
292
+ availableCompileIds: Object.keys(jsenvDirectory.compileDirectories),
292
293
  },
293
294
  null,
294
295
  " ",
@@ -92,7 +92,9 @@ export const renderCompilationInToolbar = ({ compileGroup }) => {
92
92
  ? "mismatch"
93
93
  : actualCompileId
94
94
  ? "source"
95
- : "compiled",
95
+ : expectedCompiledId
96
+ ? "compiled"
97
+ : "force",
96
98
  })
97
99
  if (filesCompilation === "yes") {
98
100
  document.querySelector(
@@ -106,7 +108,12 @@ export const renderCompilationInToolbar = ({ compileGroup }) => {
106
108
  filesCompilationRootNode.querySelector(
107
109
  "a.link_to_compiled_files",
108
110
  ).onclick = () => {
109
- window.parent.location.href = `/${jsenvDirectoryRelativeUrl}${compileId}/${compileGroup.fileRelativeUrl}`
111
+ window.parent.location.href = `/${jsenvDirectoryRelativeUrl}${expectedCompiledId}/${compileGroup.fileRelativeUrl}`
112
+ }
113
+ filesCompilationRootNode.querySelector(
114
+ "a.link_to_compilation_forced_files",
115
+ ).onclick = () => {
116
+ window.parent.location.href = `/${jsenvDirectoryRelativeUrl}force/${compileGroup.fileRelativeUrl}`
110
117
  }
111
118
  filesCompilationRootNode.querySelector(
112
119
  "a.link_to_appropriate_files",
@@ -460,6 +460,12 @@
460
460
  href="javascript:void(0);"
461
461
  >Switch to compiled files</a
462
462
  >
463
+ <a
464
+ data-when="compilation_link:force"
465
+ class="link_to_compilation_forced_files"
466
+ href="javascript:void(0);"
467
+ >Force compilation</a
468
+ >
463
469
  </div>
464
470
  </div>
465
471
  </div>
@@ -1,3 +1,4 @@
1
+ import { fetchExploringJson } from "@jsenv/core/src/internal/dev_server/exploring/fetchExploringJson.js"
1
2
  import { setAttributes, setStyles } from "./util/dom.js"
2
3
 
3
4
  // eslint-disable-next-line no-undef
@@ -12,7 +13,7 @@ const injectToolbar = async () => {
12
13
  window.requestAnimationFrame(resolve)
13
14
  }
14
15
  })
15
-
16
+ const exploringJSON = await fetchExploringJson()
16
17
  const placeholder = getToolbarPlaceholder()
17
18
 
18
19
  const iframe = document.createElement("iframe")
@@ -36,13 +37,20 @@ const injectToolbar = async () => {
36
37
  "border": "none",
37
38
  })
38
39
  const iframeLoadedPromise = iframeToLoadedPromise(iframe)
39
- const jsenvToolbarHtmlServerUrl = `/${TOOLBAR_BUILD_RELATIVE_URL}`
40
+ const jsenvCoreDirectoryServerUrl = new URL(
41
+ exploringJSON.jsenvCoreDirectoryRelativeUrl,
42
+ document.location.origin,
43
+ ).href
44
+ const jsenvToolbarHtmlServerUrl = new URL(
45
+ TOOLBAR_BUILD_RELATIVE_URL,
46
+ jsenvCoreDirectoryServerUrl,
47
+ )
40
48
  // set iframe src BEFORE putting it into the DOM (prevent firefox adding an history entry)
41
49
  iframe.setAttribute("src", jsenvToolbarHtmlServerUrl)
42
50
  placeholder.parentNode.replaceChild(iframe, placeholder)
43
51
 
44
52
  addToolbarEventCallback(iframe, "toolbar_ready", () => {
45
- sendCommandToToolbar(iframe, "renderToolbar")
53
+ sendCommandToToolbar(iframe, "renderToolbar", { exploringJSON })
46
54
  })
47
55
 
48
56
  await iframeLoadedPromise
@@ -1,7 +1,6 @@
1
1
  import { urlIsInsideOf } from "@jsenv/filesystem/src/urlIsInsideOf.js"
2
2
  import { urlToRelativeUrl } from "@jsenv/filesystem/src/urlToRelativeUrl.js"
3
3
 
4
- import { fetchExploringJson } from "../exploring/fetchExploringJson.js"
5
4
  import { startJavaScriptAnimation } from "../toolbar/util/animation.js"
6
5
  import "./focus/toolbar.focus.js"
7
6
  import {
@@ -25,12 +24,11 @@ import { makeToolbarResponsive } from "./responsive/toolbar.responsive.js"
25
24
 
26
25
  const toolbarVisibilityPreference = createPreference("toolbar")
27
26
 
28
- const renderToolbar = async () => {
27
+ const renderToolbar = async ({ exploringJSON }) => {
29
28
  const executedFileCompiledUrl = window.parent.location.href
30
29
  const compileServerOrigin = window.parent.location.origin
31
30
  // this should not block the whole toolbar rendering + interactivity
32
- const exploringConfig = await fetchExploringJson()
33
- const { jsenvDirectoryRelativeUrl, livereloading } = exploringConfig
31
+ const { jsenvDirectoryRelativeUrl, livereloading } = exploringJSON
34
32
  const compileGroup = getCompileGroup({
35
33
  executedFileCompiledUrl,
36
34
  jsenvDirectoryRelativeUrl,
@@ -168,25 +166,24 @@ const getCompileGroup = ({
168
166
  jsenvDirectoryRelativeUrl,
169
167
  compileServerOrigin,
170
168
  }) => {
171
- const outDirectoryServerUrl = String(
172
- new URL(jsenvDirectoryRelativeUrl, compileServerOrigin),
173
- )
174
- if (urlIsInsideOf(executedFileCompiledUrl, outDirectoryServerUrl)) {
169
+ const jsenvDirectoryServerUrl = new URL(
170
+ jsenvDirectoryRelativeUrl,
171
+ compileServerOrigin,
172
+ ).href
173
+ if (urlIsInsideOf(executedFileCompiledUrl, jsenvDirectoryServerUrl)) {
175
174
  const afterCompileDirectory = urlToRelativeUrl(
176
175
  executedFileCompiledUrl,
177
- outDirectoryServerUrl,
176
+ jsenvDirectoryServerUrl,
178
177
  )
179
178
  const slashIndex = afterCompileDirectory.indexOf("/")
180
179
  const fileRelativeUrl = afterCompileDirectory.slice(slashIndex + 1)
181
180
  return {
182
181
  fileRelativeUrl,
183
- jsenvDirectoryRelativeUrl,
184
182
  compileId: afterCompileDirectory.slice(0, slashIndex),
185
183
  }
186
184
  }
187
185
  return {
188
186
  fileRelativeUrl: new URL(executedFileCompiledUrl).pathname.slice(1),
189
- jsenvDirectoryRelativeUrl,
190
187
  compileId: null,
191
188
  }
192
189
  }
@@ -228,13 +225,12 @@ const sendEventToParent = (name, data) => {
228
225
  }
229
226
 
230
227
  window.toolbar = {
231
- render: renderToolbar,
232
228
  show: showToolbar,
233
229
  hide: () => hideToolbar(),
234
230
  }
235
231
 
236
- addExternalCommandCallback("renderToolbar", () => {
237
- renderToolbar()
232
+ addExternalCommandCallback("renderToolbar", (data) => {
233
+ renderToolbar(data)
238
234
  })
239
235
  addExternalCommandCallback("showToolbar", () => {
240
236
  showToolbar()
@@ -14,8 +14,11 @@ export const scanBrowserRuntimeFeatures = async ({
14
14
  forceSource = false,
15
15
  } = {}) => {
16
16
  const jsenvCompileProfileUrl = "/__jsenv_compile_profile__"
17
- const { jsenvDirectoryRelativeUrl, inlineImportMapIntoHTML } =
18
- await fetchJson(jsenvCompileProfileUrl)
17
+ const {
18
+ jsenvDirectoryRelativeUrl,
19
+ inlineImportMapIntoHTML,
20
+ availableCompileIds,
21
+ } = await fetchJson(jsenvCompileProfileUrl)
19
22
  const { name, version } = detectBrowser()
20
23
  const featuresReport = await detectSupportedFeatures({
21
24
  coverageHandledFromOutside,
@@ -42,9 +45,10 @@ export const scanBrowserRuntimeFeatures = async ({
42
45
  return {
43
46
  jsenvDirectoryRelativeUrl,
44
47
  inlineImportMapIntoHTML,
48
+ availableCompileIds,
49
+ runtimeReport,
45
50
  compileProfile,
46
51
  compileId,
47
- runtimeReport,
48
52
  }
49
53
  }
50
54
 
@@ -18,7 +18,6 @@ export const getDefaultImportmap = (
18
18
  jsenvCoreDirectoryUrl,
19
19
  projectDirectoryUrl,
20
20
  )
21
-
22
21
  let jsenvCoreUrl
23
22
  if (compileDirectoryUrl && urlIsInsideOf(url, compileDirectoryUrl)) {
24
23
  jsenvCoreUrl = resolveUrl(
@@ -28,25 +27,11 @@ export const getDefaultImportmap = (
28
27
  } else {
29
28
  jsenvCoreUrl = jsenvCoreDirectoryUrl
30
29
  }
31
-
30
+ const jsenvCoreRelativeUrl = urlToRelativeUrl(jsenvCoreUrl, url)
32
31
  const importmap = {
33
32
  imports: {
34
- "@jsenv/core/": makeRelativeMapping(jsenvCoreUrl, url),
33
+ "@jsenv/core/": `./${jsenvCoreRelativeUrl}`,
35
34
  },
36
35
  }
37
36
  return importmap
38
37
  }
39
-
40
- // this function just here to ensure relative urls starts with './'
41
- // so that importmap do not consider them as bare specifiers
42
- const makeRelativeMapping = (url, baseUrl) => {
43
- const relativeUrl = urlToRelativeUrl(url, baseUrl)
44
-
45
- if (urlIsInsideOf(url, baseUrl)) {
46
- if (relativeUrl.startsWith("../")) return relativeUrl
47
- if (relativeUrl.startsWith("./")) return relativeUrl
48
- return `./${relativeUrl}`
49
- }
50
-
51
- return relativeUrl
52
- }
@@ -277,7 +277,6 @@ export const createControllableNodeProcess = async ({
277
277
 
278
278
  const sendToProcess = async (childProcess, type, data) => {
279
279
  const source = uneval(data, { functionAllowed: true })
280
-
281
280
  return new Promise((resolve, reject) => {
282
281
  childProcess.send({ type, data: source }, (error) => {
283
282
  if (error) {
@@ -295,12 +294,10 @@ const installProcessOutputListener = (childProcess, callback) => {
295
294
  callback({ type: "log", text: String(chunk) })
296
295
  }
297
296
  childProcess.stdout.on("data", stdoutDataCallback)
298
-
299
297
  const stdErrorDataCallback = (chunk) => {
300
298
  callback({ type: "error", text: String(chunk) })
301
299
  }
302
300
  childProcess.stderr.on("data", stdErrorDataCallback)
303
-
304
301
  return () => {
305
302
  childProcess.stdout.removeListener("data", stdoutDataCallback)
306
303
  childProcess.stderr.removeListener("data", stdoutDataCallback)
@@ -59,8 +59,7 @@ const sendActionFailed = (error) => {
59
59
  sendToParent(
60
60
  ACTION_RESPONSE_EVENT_NAME,
61
61
  // process.send algorithm does not send non enumerable values
62
- // because it works with JSON.stringify I guess so use uneval
63
-
62
+ // so use @jsenv/uneval
64
63
  uneval(
65
64
  {
66
65
  status: ACTION_RESPONSE_STATUS_FAILED,
@@ -91,7 +90,6 @@ const sendToParent = (type, data) => {
91
90
  if (!process.connected) {
92
91
  return
93
92
  }
94
-
95
93
  // this can keep process alive longer than expected
96
94
  // when source is a long string.
97
95
  // It means node process may stay alive longer than expected
@@ -111,11 +109,9 @@ const onceProcessMessage = (type, callback) => {
111
109
  callback(eval(`(${event.data})`))
112
110
  }
113
111
  }
114
-
115
112
  const removeListener = () => {
116
113
  process.removeListener("message", listener)
117
114
  }
118
-
119
115
  process.on("message", listener)
120
116
  return removeListener
121
117
  }
@@ -13,10 +13,12 @@
13
13
  import { scanBrowserRuntimeFeatures } from "../features/browser_feature_detection/browser_feature_detection.js"
14
14
 
15
15
  const redirect = async () => {
16
- const redirectTarget = new URLSearchParams(window.location.search).get(
17
- "redirect",
18
- )
19
- const browserRuntimeProfile = await scanBrowserRuntimeFeatures()
16
+ const searchParams = new URLSearchParams(window.location.search)
17
+ const redirectTarget = searchParams.get("redirect")
18
+ const forceCompilation = searchParams.get("force_compilation")
19
+ const browserRuntimeProfile = await scanBrowserRuntimeFeatures({
20
+ forceCompilation: forceCompilation === "1",
21
+ })
20
22
  const href = `${getDirectoryUrl(
21
23
  browserRuntimeProfile,
22
24
  )}${redirectTarget}`
@@ -1,267 +0,0 @@
1
- (function () {
2
- 'use strict';
3
-
4
- var nativeTypeOf = function nativeTypeOf(obj) {
5
- return typeof obj;
6
- };
7
-
8
- var customTypeOf = function customTypeOf(obj) {
9
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
10
- };
11
-
12
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? nativeTypeOf : customTypeOf;
13
-
14
- var setStyles = function setStyles(element, styles) {
15
- var elementStyle = element.style;
16
- var restoreStyles = Object.keys(styles).map(function (styleName) {
17
- var restore;
18
-
19
- if (styleName in elementStyle) {
20
- var currentStyle = elementStyle[styleName];
21
-
22
- restore = function restore() {
23
- elementStyle[styleName] = currentStyle;
24
- };
25
- } else {
26
- restore = function restore() {
27
- delete elementStyle[styleName];
28
- };
29
- }
30
-
31
- elementStyle[styleName] = styles[styleName];
32
- return restore;
33
- });
34
- return function () {
35
- restoreStyles.forEach(function (restore) {
36
- return restore();
37
- });
38
- };
39
- };
40
- var setAttributes = function setAttributes(element, attributes) {
41
- Object.keys(attributes).forEach(function (name) {
42
- element.setAttribute(name, attributes[name]);
43
- });
44
- };
45
-
46
- function _await(value, then, direct) {
47
- if (direct) {
48
- return then ? then(value) : value;
49
- }
50
-
51
- if (!value || !value.then) {
52
- value = Promise.resolve(value);
53
- }
54
-
55
- return then ? value.then(then) : value;
56
- }
57
-
58
- var TOOLBAR_BUILD_RELATIVE_URL = "dist/toolbar/toolbar_d3a918a4.html";
59
-
60
- function _async(f) {
61
- return function () {
62
- for (var args = [], i = 0; i < arguments.length; i++) {
63
- args[i] = arguments[i];
64
- }
65
-
66
- try {
67
- return Promise.resolve(f.apply(this, args));
68
- } catch (e) {
69
- return Promise.reject(e);
70
- }
71
- };
72
- }
73
-
74
- var jsenvLogoSvgUrl = new URL("assets/jsenv-logo_188b9ca6.svg", document.currentScript && document.currentScript.src || document.baseURI);
75
-
76
- var injectToolbar = _async(function () {
77
- return _await(new Promise(function (resolve) {
78
- if (window.requestIdleCallback) {
79
- window.requestIdleCallback(resolve, {
80
- timeout: 400
81
- });
82
- } else {
83
- window.requestAnimationFrame(resolve);
84
- }
85
- }), function () {
86
- var placeholder = getToolbarPlaceholder();
87
- var iframe = document.createElement("iframe");
88
- setAttributes(iframe, {
89
- tabindex: -1,
90
- // sandbox: "allow-forms allow-modals allow-pointer-lock allow-popups allow-presentation allow-same-origin allow-scripts allow-top-navigation-by-user-activation",
91
- // allow: "accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; microphone; midi; payment; vr",
92
- allowtransparency: true
93
- });
94
- setStyles(iframe, {
95
- "position": "fixed",
96
- "zIndex": 1000,
97
- "bottom": 0,
98
- "left": 0,
99
- "width": "100%",
100
- "height": 0,
101
-
102
- /* ensure toolbar children are not focusable when hidden */
103
- "visibility": "hidden",
104
- "transition-duration": "300ms",
105
- "transition-property": "height, visibility",
106
- "border": "none"
107
- });
108
- var iframeLoadedPromise = iframeToLoadedPromise(iframe);
109
- var jsenvToolbarHtmlServerUrl = "/".concat(TOOLBAR_BUILD_RELATIVE_URL); // set iframe src BEFORE putting it into the DOM (prevent firefox adding an history entry)
110
-
111
- iframe.setAttribute("src", jsenvToolbarHtmlServerUrl);
112
- placeholder.parentNode.replaceChild(iframe, placeholder);
113
- addToolbarEventCallback(iframe, "toolbar_ready", function () {
114
- sendCommandToToolbar(iframe, "renderToolbar");
115
- });
116
- return _await(iframeLoadedPromise, function () {
117
- iframe.removeAttribute("tabindex");
118
- var div = document.createElement("div");
119
- div.innerHTML = "\n<div id=\"jsenv-toolbar-trigger\">\n <svg id=\"jsenv-toolbar-trigger-icon\">\n <use xlink:href=\"".concat(jsenvLogoSvgUrl, "#jsenv-logo\"></use>\n </svg>\n <style>\n #jsenv-toolbar-trigger {\n display: block;\n overflow: hidden;\n position: fixed;\n z-index: 1000;\n bottom: -32px;\n right: 20px;\n height: 40px;\n width: 40px;\n padding: 0;\n margin: 0;\n border-radius: 5px 5px 0 0;\n border: 1px solid rgba(0, 0, 0, 0.33);\n border-bottom: none;\n box-shadow: 0px 0px 6px 2px rgba(0, 0, 0, 0.46);\n background: transparent;\n text-align: center;\n transition: 600ms;\n }\n\n #jsenv-toolbar-trigger:hover {\n cursor: pointer;\n }\n\n #jsenv-toolbar-trigger[data-expanded] {\n bottom: 0;\n }\n\n #jsenv-toolbar-trigger-icon {\n width: 35px;\n height: 35px;\n opacity: 0;\n transition: 600ms;\n }\n\n #jsenv-toolbar-trigger[data-expanded] #jsenv-toolbar-trigger-icon {\n opacity: 1;\n }\n </style>\n</div>");
120
- var toolbarTrigger = div.firstElementChild;
121
- iframe.parentNode.appendChild(toolbarTrigger);
122
- var timer;
123
-
124
- toolbarTrigger.onmouseenter = function () {
125
- toolbarTrigger.setAttribute("data-animate", "");
126
- timer = setTimeout(expandToolbarTrigger, 500);
127
- };
128
-
129
- toolbarTrigger.onmouseleave = function () {
130
- clearTimeout(timer);
131
- collapseToolbarTrigger();
132
- };
133
-
134
- toolbarTrigger.onfocus = function () {
135
- toolbarTrigger.removeAttribute("data-animate");
136
- expandToolbarTrigger();
137
- };
138
-
139
- toolbarTrigger.onblur = function () {
140
- toolbarTrigger.removeAttribute("data-animate");
141
- clearTimeout(timer);
142
- collapseToolbarTrigger();
143
- };
144
-
145
- toolbarTrigger.onclick = function () {
146
- sendCommandToToolbar(iframe, "showToolbar");
147
- };
148
-
149
- var showToolbarTrigger = function showToolbarTrigger() {
150
- toolbarTrigger.style.display = "block";
151
- };
152
-
153
- var hideToolbarTrigger = function hideToolbarTrigger() {
154
- toolbarTrigger.style.display = "none";
155
- };
156
-
157
- var expandToolbarTrigger = function expandToolbarTrigger() {
158
- toolbarTrigger.setAttribute("data-expanded", "");
159
- };
160
-
161
- var collapseToolbarTrigger = function collapseToolbarTrigger() {
162
- toolbarTrigger.removeAttribute("data-expanded", "");
163
- };
164
-
165
- hideToolbarTrigger();
166
- addToolbarEventCallback(iframe, "toolbar-visibility-change", function (visible) {
167
- if (visible) {
168
- hideToolbarTrigger();
169
- } else {
170
- showToolbarTrigger();
171
- }
172
- });
173
- return iframe;
174
- });
175
- });
176
- });
177
-
178
- var addToolbarEventCallback = function addToolbarEventCallback(iframe, eventName, callback) {
179
- var messageEventCallback = function messageEventCallback(messageEvent) {
180
- var data = messageEvent.data;
181
-
182
- if (_typeof(data) !== "object") {
183
- return;
184
- }
185
-
186
- var __jsenv__ = data.__jsenv__;
187
-
188
- if (!__jsenv__) {
189
- return;
190
- }
191
-
192
- if (__jsenv__.event !== eventName) {
193
- return;
194
- }
195
-
196
- callback(__jsenv__.data);
197
- };
198
-
199
- window.addEventListener("message", messageEventCallback, false);
200
- return function () {
201
- window.removeEventListener("message", messageEventCallback, false);
202
- };
203
- };
204
-
205
- var sendCommandToToolbar = function sendCommandToToolbar(iframe, command) {
206
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
207
- args[_key - 2] = arguments[_key];
208
- }
209
-
210
- iframe.contentWindow.postMessage({
211
- __jsenv__: {
212
- command: command,
213
- args: args
214
- }
215
- }, window.origin);
216
- };
217
-
218
- var getToolbarPlaceholder = function getToolbarPlaceholder() {
219
- var placeholder = queryPlaceholder();
220
-
221
- if (placeholder) {
222
- if (document.body.contains(placeholder)) {
223
- return placeholder;
224
- } // otherwise iframe would not be visible because in <head>
225
-
226
-
227
- console.warn("element with [data-jsenv-toolbar-placeholder] must be inside document.body");
228
- return createTooolbarPlaceholder();
229
- }
230
-
231
- return createTooolbarPlaceholder();
232
- };
233
-
234
- var queryPlaceholder = function queryPlaceholder() {
235
- return document.querySelector("[data-jsenv-toolbar-placeholder]");
236
- };
237
-
238
- var createTooolbarPlaceholder = function createTooolbarPlaceholder() {
239
- var placeholder = document.createElement("span");
240
- document.body.appendChild(placeholder);
241
- return placeholder;
242
- };
243
-
244
- var iframeToLoadedPromise = function iframeToLoadedPromise(iframe) {
245
- return new Promise(function (resolve) {
246
- var onload = function onload() {
247
- iframe.removeEventListener("load", onload, true);
248
- resolve();
249
- };
250
-
251
- iframe.addEventListener("load", onload, true);
252
- });
253
- };
254
-
255
- if (document.readyState === "complete") {
256
- injectToolbar();
257
- } else {
258
- window.addEventListener("load", injectToolbar); // document.addEventListener("readystatechange", () => {
259
- // if (document.readyState === "complete") {
260
- // injectToolbar()
261
- // }
262
- // })
263
- }
264
-
265
- })();
266
-
267
- //# sourceMappingURL=toolbar_injector_978bbd14.js.map