UNPKG

markmap-lib

Version:

Visualize your Markdown as mindmaps with Markmap

165 lines (140 loc) 3.92 kB
"use strict"; exports.__esModule = true; exports.buildCode = buildCode; exports.memoize = memoize; exports.loadJS = loadJS; exports.loadCSS = loadCSS; exports.initializePlugins = initializePlugins; exports.persistJS = persistJS; exports.persistCSS = persistCSS; exports.persistPlugins = persistPlugins; var _html = require("./html"); var _base = require("./base"); function buildCode(fn, ...args) { const params = args.map(arg => { if (typeof arg === 'function') return arg.toString(); return JSON.stringify(arg != null ? arg : null); }).join(','); return `(${fn.toString()})(${params})`; } function memoize(fn) { const cache = {}; return function memoized(...args) { const key = `${args[0]}`; let data = cache[key]; if (!data) { data = { value: fn(...args) }; cache[key] = data; } return data.value; }; } function createElement(tagName, props, attrs) { const el = document.createElement(tagName); if (props) { Object.entries(props).forEach(([key, value]) => { el[key] = value; }); } if (attrs) { Object.entries(attrs).forEach(([key, value]) => { el.setAttribute(key, value); }); } return el; } const memoizedPreloadJS = memoize(url => { document.head.append(createElement('link', { rel: 'preload', as: 'script', href: url })); }); function loadJSItem(item, context) { if (item.type === 'script') { return new Promise((resolve, reject) => { document.head.append(createElement('script', Object.assign(Object.assign({}, item.data), {}, { onload: resolve, onerror: reject }))); }); } else if (item.type === 'iife') { const { fn, getParams } = item.data; fn(...((getParams == null ? void 0 : getParams(context)) || [])); } } function loadCSSItem(item) { if (item.type === 'style') { document.head.append(createElement('style', { textContent: item.data })); } else if (item.type === 'stylesheet') { document.head.append(createElement('link', Object.assign({ rel: 'stylesheet' }, item.data))); } } async function loadJS(items, options) { const needPreload = items.filter(item => item.type === 'script'); if (needPreload.length > 1) needPreload.forEach(item => memoizedPreloadJS(item.data.src)); for (const item of items) { await loadJSItem(item, options); } } function loadCSS(items) { for (const item of items) { loadCSSItem(item); } } async function initializePlugins(Markmap, plugins, options) { options = Object.assign({}, options); await Promise.all(plugins.map(plugin => { loadCSS(plugin.styles); return loadJS(plugin.scripts, options); })); for (const { initialize } of plugins) { if (initialize) initialize(Markmap, options); } } function persistJS(items, context) { return items.map(item => { if (item.type === 'script') return (0, _html.wrapHtml)('script', '', item.data); if (item.type === 'iife') { const { fn, getParams } = item.data; return (0, _html.wrapHtml)('script', (0, _html.escapeScript)(buildCode(fn, ...((getParams == null ? void 0 : getParams(context)) || [])))); } }); } function persistCSS(items) { return items.map(item => { if (item.type === 'stylesheet') { return (0, _html.wrapHtml)('link', null, Object.assign({ rel: 'stylesheet' }, item.data)); } /* else if (item.type === 'style') */ return (0, _html.wrapHtml)('style', item.data); }); } function persistPlugins(plugins, context) { const js = (0, _base.flatMap)(plugins, plugin => persistJS(plugin.scripts, context)).join(''); const css = (0, _base.flatMap)(plugins, plugin => persistCSS(plugin.styles)).join(''); const initializers = plugins.map(({ initialize }) => initialize).filter(Boolean); return { js, css, initializers }; }