numbl 0.4.4 → 0.4.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.
@@ -7,7 +7,7 @@
7
7
  <title>numbl project</title>
8
8
  <!-- numbl:base -->
9
9
  <script src="./coi-serviceworker.js"></script>
10
- <script type="module" crossorigin src="./assets/index-USrK1-DZ.js"></script>
10
+ <script type="module" crossorigin src="./assets/index-Y_Z9U6zO.js"></script>
11
11
  <link rel="stylesheet" crossorigin href="./assets/index-D5YY8PKx.css">
12
12
  </head>
13
13
  <body>
@@ -5,14 +5,37 @@ Usage:
5
5
  <numbl-embed>
6
6
  <iframe width="100%" height="500" frameborder="0"></iframe>
7
7
 
8
- <script type="text/plain" class="matlab-script">
9
- ... MATLAB script ...
8
+ <script type="text/plain" class="numbl-script">
9
+ ... numbl script ...
10
10
  </script>
11
11
 
12
12
  </numbl-embed>
13
13
 
14
- You can also specify the MATLAB script URL via attribute:
15
- <numbl-embed script="relative-or-absolute-url-to-matlab-script">
14
+ Optional preamble: setup code that runs before the visible script on every Run
15
+ (e.g. installing a package with `mip load --install ...`). It is kept out of the
16
+ editor, and its console output is hidden behind a "Preparing…" message unless it
17
+ errors — in which case the output and error are shown. Override that message
18
+ per-embed with the `preparing-label` attribute (e.g. "Installing…").
19
+
20
+ <numbl-embed preparing-label="Installing…">
21
+ <iframe width="100%" height="500" frameborder="0"></iframe>
22
+
23
+ <script type="text/plain" class="numbl-preamble">
24
+ mip load --install owner/repo/package
25
+ </script>
26
+
27
+ <script type="text/plain" class="numbl-script">
28
+ ... numbl script ...
29
+ </script>
30
+
31
+ </numbl-embed>
32
+
33
+ The classes `matlab-script` / `matlab-preamble` are accepted as synonyms for
34
+ `numbl-script` / `numbl-preamble` (backward compatibility with older embeds).
35
+
36
+ You can also specify the script URL via attribute (a `numbl-preamble` block still
37
+ applies):
38
+ <numbl-embed script="relative-or-absolute-url-to-script">
16
39
  <iframe width="100%" height="500" frameborder="0"></iframe>
17
40
  </numbl-embed>
18
41
 
@@ -123,28 +146,47 @@ class NumblEmbed extends HTMLElement {
123
146
  return utf8ToBase64(text2);
124
147
  };
125
148
 
126
- const scriptElement = this.querySelector("script.matlab-script");
149
+ // Optional preamble: setup code that runs (hidden) before the visible
150
+ // script. `numbl-preamble` is preferred; `matlab-preamble` is a synonym.
151
+ const preambleElement = this.querySelector(
152
+ "script.numbl-preamble, script.matlab-preamble"
153
+ );
154
+ let preambleParam = preambleElement
155
+ ? `&preamble=${encodePlain(preambleElement.textContent.trim())}`
156
+ : "";
157
+
158
+ // Message shown while the preamble runs (defaults to "Preparing..." in the
159
+ // embed page). Override per-embed with the `preparing-label` attribute,
160
+ // e.g. preparing-label="Installing...".
161
+ const preparingLabel = this.getAttribute("preparing-label");
162
+ if (preambleParam && preparingLabel) {
163
+ preambleParam += `&preparing=${utf8ToBase64(preparingLabel)}`;
164
+ }
165
+
166
+ // `numbl-script` is preferred; `matlab-script` is a synonym.
167
+ const scriptElement = this.querySelector(
168
+ "script.numbl-script, script.matlab-script"
169
+ );
127
170
 
128
171
  let scriptBase64;
129
172
  if (scriptElement) {
130
- const matlabScript = scriptElement.textContent.trim();
131
- scriptBase64 = encodePlain(matlabScript);
173
+ scriptBase64 = encodePlain(scriptElement.textContent.trim());
132
174
  } else if (this.attributes.script) {
133
175
  const scriptUrl = this.attributes.script.value;
134
- this.loadScriptFromUrl(scriptUrl);
176
+ this.loadScriptFromUrl(scriptUrl, preambleParam);
135
177
  return;
136
178
  } else {
137
179
  scriptBase64 = null;
138
180
  }
139
181
 
140
182
  if (scriptBase64) {
141
- this.iframe.src = `${numblUrl}/embed?script=${scriptBase64}&${cacheBust}`;
183
+ this.iframe.src = `${numblUrl}/embed?script=${scriptBase64}${preambleParam}&${cacheBust}`;
142
184
  } else {
143
- this.iframe.src = `${numblUrl}/embed?${cacheBust}`;
185
+ this.iframe.src = `${numblUrl}/embed?${cacheBust}${preambleParam}`;
144
186
  }
145
187
  }
146
188
 
147
- async loadScriptFromUrl(url) {
189
+ async loadScriptFromUrl(url, preambleParam = "") {
148
190
  try {
149
191
  let absoluteUrl = url;
150
192
  if (url.startsWith("./") || url.startsWith("../")) {
@@ -164,9 +206,9 @@ class NumblEmbed extends HTMLElement {
164
206
  const numblUrl = this.attributes["numbl-url"]?.value || defaultNumblUrl;
165
207
 
166
208
  const cacheBust = `_cb=${Date.now()}`;
167
- this.iframe.src = `${numblUrl}/embed?script=${scriptBase64}&${cacheBust}`;
209
+ this.iframe.src = `${numblUrl}/embed?script=${scriptBase64}${preambleParam}&${cacheBust}`;
168
210
  } catch (error) {
169
- console.error("Error loading MATLAB script:", error);
211
+ console.error("Error loading script:", error);
170
212
  this.iframe.srcdoc = `
171
213
  <html>
172
214
  <body style="font-family: Arial; padding: 20px;">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "numbl",
3
- "version": "0.4.4",
3
+ "version": "0.4.5",
4
4
  "description": "Run .m source files in the browser and on the command line by compiling to JavaScript",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",