|
261 | 261 | // Load a single script, module, or css, awaiting until done |
262 | 262 | const loadModuleOrCss = async (s) => { |
263 | 263 | return new Promise((resolve, reject) => { |
264 | | - // import maps are handled in the import-map-loader script |
| 264 | + |
265 | 265 | if (s.startsWith("{")) { |
266 | 266 | resolve(); |
267 | 267 | return; |
268 | 268 | } |
269 | | - // if (!foundImportMap && s.startsWith("{")) { |
270 | | - // try { |
271 | | - // if (foundImportMap) { |
272 | | - // window.logStderr( |
273 | | - // "Found multiple import maps, only the first will be used" |
274 | | - // ); |
275 | | - // return; |
276 | | - // } |
277 | | - // const maybeImports = JSON.parse(decodeURIComponent(s)); |
278 | | - // if (maybeImports.imports) { |
279 | | - // foundImportMap = true; |
280 | | - // var importMapElement = document.createElement("script"); |
281 | | - // importMapElement.innerHTML = decodeURIComponent(s); |
282 | | - // importMapElement.type = "importmap"; |
283 | | - // document |
284 | | - // .getElementsByTagName("head")[0] |
285 | | - // .appendChild(importMapElement); |
286 | | - // import maps do not fire onload events, so resolve here |
287 | | - // the timing of this may not be perfect, so note this here |
288 | | - // in case there are issues |
289 | | - // resolve(); |
290 | | - // return; |
291 | | - // } |
292 | | - // } catch (e) { |
293 | | - // console.log("Error loading module", s); |
294 | | - // window.logStderr(`error loading module: ${s}`); |
295 | | - // return; |
296 | | - // } |
297 | | - // } else { |
298 | 269 | let elementTrimmed = s.trim(); |
299 | 270 | if (elementTrimmed.startsWith("<link")) { |
300 | 271 | const href = /<link\s+.*href="([^"]*)"/.exec(elementTrimmed) |
|
375 | 346 | }; |
376 | 347 |
|
377 | 348 | const runmdFromUrl = async () => { |
| 349 | + let mdContent = getMdFromUrl(); |
| 350 | + await runOnMarkdown(mdContent); |
| 351 | + } |
| 352 | + |
| 353 | + const runmdFromInputs = async () => { |
| 354 | + await runOnMarkdown(metaframe.getInput("md")); |
| 355 | + } |
| 356 | + |
| 357 | + let _lastMdContent = null; |
| 358 | + const runOnMarkdown = async (mdContent) => { |
| 359 | + |
378 | 360 | // ensure all modules are loaded |
379 | 361 | await Promise.all(window.__metaframeMdAwaiting); |
380 | | - // clear all children of root |
381 | | - document |
382 | | - .getElementById("root") |
383 | | - .textContent = ""; |
384 | 362 |
|
385 | 363 | const [___, hashParamsCurrent] = getUrlHashParamsFromHashString(window.location.hash); |
386 | | - const editorWidth = hashParamsCurrent["editorWidth"] || 80; |
| 364 | + const editorWidth = hashParamsCurrent["editorWidth"] || "80ch"; |
387 | 365 | const inEditMode = hashParamsCurrent["edit"] === "true"; |
388 | 366 | const defaultBgColor = hashParamsCurrent["bgColor"]; |
389 | 367 | if (!inEditMode) { |
390 | 368 | removeEditor(); |
391 | 369 | } |
392 | | - |
| 370 | + const widthRootCss = `calc(100% - ${editorWidth})`; |
393 | 371 | // set up (maybe re-override globals) |
394 | 372 | const root = document.getElementById("root"); |
395 | 373 | root.style.width = inEditMode |
396 | | - ? `calc(100% - ${editorWidth}ch)` |
| 374 | + ? widthRootCss |
397 | 375 | : "100%"; |
398 | 376 | if (defaultBgColor) { |
399 | 377 | root.style.background = defaultBgColor; |
400 | 378 | } |
401 | 379 |
|
402 | 380 | const editor = document.getElementById("editor-root"); |
403 | 381 | editor.style.width = inEditMode |
404 | | - ? `${editorWidth}ch` |
| 382 | + ? `${editorWidth}` |
405 | 383 | : "0%"; |
406 | 384 |
|
407 | | - let mdContent = getMdFromUrl(); |
| 385 | + if (_lastMdContent === mdContent) { |
| 386 | + return; |
| 387 | + } |
| 388 | + _lastMdContent = mdContent; |
408 | 389 |
|
409 | | - if (mdContent) { |
| 390 | + // clear all children of root |
| 391 | + document |
| 392 | + .getElementById("root") |
| 393 | + .textContent = ""; |
410 | 394 |
|
411 | | - const metaframe = globalThis.metaframe; |
412 | | - // prettier-ignore |
413 | | - metaframe |
414 | | - ?.setOutput("md", mdContent); |
| 395 | + // let mdContent = getMdFromUrl(); |
415 | 396 |
|
416 | | - const MD = markdownit({html: true, linkify: true, typographer: true}); |
417 | | - MD.use(MermaidPlugIn); |
418 | | - MD.use(SingleScreenPlugin, options || {}); |
419 | | - MD.use(markdownAnchor, { |
420 | | - level: [1, 2, 3] |
421 | | - }); |
| 397 | + const metaframe = globalThis.metaframe; |
| 398 | + // prettier-ignore |
| 399 | + metaframe |
| 400 | + ?.setOutput("md", mdContent); |
422 | 401 |
|
423 | | - const rules = options |
424 | | - ?.dm === 'slide' |
425 | | - ? { |
| 402 | + const MD = markdownit({html: true, linkify: true, typographer: true}); |
| 403 | + MD.use(MermaidPlugIn); |
| 404 | + MD.use(SingleScreenPlugin, options || {}); |
| 405 | + MD.use(markdownAnchor, { |
| 406 | + level: [1, 2, 3] |
| 407 | + }); |
426 | 408 |
|
427 | | - table_close: () => '</table>\n</div>\n</div>', |
428 | | - table_open: () => '<div class="row"><div class="rowCellTable">\n<table>\n' |
429 | | - } |
430 | | - : {}; |
431 | | - |
432 | | - MD.renderer.rules = { |
433 | | - ...MD.renderer.rules, |
434 | | - ...rules |
435 | | - }; |
436 | | - |
437 | | - try { |
438 | | - var result = MD.render(mdContent); |
439 | | - root.innerHTML = result; |
440 | | - Mermaid.run(); |
441 | | - } catch (err) { |
442 | | - console.error(err); |
443 | | - var result = MD.render(`# Error rendering markdown \n\n - markdown: \`${mdContent}\` \n - err: ${err}`); |
444 | | - root.innerHTML = result; |
445 | | - } |
| 409 | + const rules = options |
| 410 | + ?.dm === 'slide' |
| 411 | + ? { |
| 412 | + |
| 413 | + table_close: () => '</table>\n</div>\n</div>', |
| 414 | + table_open: () => '<div class="row"><div class="rowCellTable">\n<table>\n' |
| 415 | + } |
| 416 | + : {}; |
| 417 | + |
| 418 | + MD.renderer.rules = { |
| 419 | + ...MD.renderer.rules, |
| 420 | + ...rules |
| 421 | + }; |
| 422 | + |
| 423 | + try { |
| 424 | + var result = MD.render(mdContent); |
| 425 | + root.innerHTML = result; |
| 426 | + Mermaid.run(); |
| 427 | + } catch (err) { |
| 428 | + console.error(err); |
| 429 | + var result = MD.render(`# Error rendering markdown \n\n - markdown: \`${mdContent}\` \n - err: ${err}`); |
| 430 | + root.innerHTML = result; |
446 | 431 | } |
447 | 432 | } |
448 | 433 |
|
|
457 | 442 | } |
458 | 443 | var metaframe = globalThis.metaframe; |
459 | 444 |
|
| 445 | + metaframe.onInput("md", (md) => { |
| 446 | + if (md) { |
| 447 | + setHashParamValueBase64EncodedInWindow("md", md); |
| 448 | + } |
| 449 | + }); |
| 450 | + |
| 451 | + metaframe.getInputs()["md"] && setHashParamValueBase64EncodedInWindow("md", metaframe.getInputs()["md"]); |
| 452 | + |
460 | 453 | metaframe |
461 | 454 | .connected() |
462 | 455 | .then(() => metaframe.log("connected")); |
|
0 commit comments