aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dumbymap.mjs
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-09-29 14:12:07 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-09-29 15:06:28 +0800
commit88201b62764931868f0bc3cae978edb3fd307485 (patch)
tree84300275f6e9a56ac47f5adcc287cfcbf5050504 /src/dumbymap.mjs
parentc3f47cff4a7fb897f802558f85ac79e10297b95b (diff)
feat(mapclay): add callback to indicate rendering progress
Diffstat (limited to 'src/dumbymap.mjs')
-rw-r--r--src/dumbymap.mjs42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs
index cb029de..241c6b9 100644
--- a/src/dumbymap.mjs
+++ b/src/dumbymap.mjs
@@ -426,19 +426,47 @@ export const generateMaps = (container, { delay, mapCallback }) => {
426 426
427 // Render each code block with "language-map" class 427 // Render each code block with "language-map" class
428 const elementsWithMapConfig = Array.from( 428 const elementsWithMapConfig = Array.from(
429 container.querySelectorAll("pre:has(.language-map)") ?? [], 429 container.querySelectorAll('pre:has(.language-map)') ?? []
430 ); 430 )
431 // Add default aliases into each config 431 /**
432 * updateAttributeByStep.
433 *
434 * @param {Object} -- renderer which is running steps
435 */
436 const updateAttributeByStep = ({ results, target, steps }) => {
437 let passNum = results
438 .filter(r => r.type === 'step' && r.state.match(/success|skip/))
439 .length
440 const total = steps.length;
441 passNum += `/${total}`;
442 if (results.filter(r=>r.type === 'step').length === total) {
443 passNum += '\u0020'
444 }
445
446 // FIXME HACK use MutationObserver for animation
447 if (!target.animations) target.animations = Promise.resolve();
448 target.animations = target.animations.then(async () => {
449 await new Promise(resolve => setTimeout(resolve, 150));
450 target.setAttribute('data-report', passNum);
451 });
452 }
453 /**
454 * config converter for mapclay.renderWith()
455 *
456 * @param {Object} config
457 * @return {Object} -- converted config
458 */
432 const configConverter = config => ({ 459 const configConverter = config => ({
433 use: config.use ?? "Leaflet", 460 use: config.use ?? 'Leaflet',
434 width: "100%", 461 width: '100%',
435 ...config, 462 ...config,
436 aliases: { 463 aliases: {
437 ...defaultAliases, 464 ...defaultAliases,
438 ...(config.aliases ?? {}), 465 ...(config.aliases ?? {}),
439 }, 466 },
440 }); 467 stepCallback: updateAttributeByStep,
441 const render = renderWith(configConverter); 468 })
469 const render = renderWith(configConverter)
442 elementsWithMapConfig.forEach(target => { 470 elementsWithMapConfig.forEach(target => {
443 // Get text in code block starts with markdown text '```map' 471 // Get text in code block starts with markdown text '```map'
444 const configText = target 472 const configText = target