UNPKG

storybook-addon-sdc

Version:

Drupal Single Directory Components as stories

198 lines (190 loc) 6.35 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/renderer/twing.ts import { createSynchronousFunction } from "twing"; import DrupalAttribute3 from "drupal-attribute"; // src/renderer/icons.ts import DrupalAttribute from "drupal-attribute"; function buildIconContext(pack, iconId, settings) { const settingsObj = settings instanceof Map ? Object.fromEntries(settings) : settings ?? {}; const resolved = {}; const packSettings = pack.settings || {}; for (const key of Object.keys(packSettings)) { const def = packSettings[key]; resolved[key] = Object.prototype.hasOwnProperty.call(settingsObj, key) ? settingsObj[key] : def && def.default !== void 0 ? def.default : ""; } let source = ""; let content = ""; let svgAttrs = {}; let group = ""; if (pack.extractor === "svg_sprite") { source = pack.sourceUrls && pack.sourceUrls[0] ? pack.sourceUrls[0] : ""; } else if (pack.extractor === "svg") { const svgData = pack.svgIcons && pack.svgIcons[iconId]; if (svgData) { source = svgData.sourceUrl || ""; content = svgData.content || ""; svgAttrs = svgData.attrs || {}; group = svgData.group || ""; } } else { const pathData = pack.pathIcons && pack.pathIcons[iconId]; if (pathData) { source = pathData.sourceUrl || ""; group = pathData.group || ""; } else if (pack.sourceUrls && pack.sourceUrls[0]) { const base = pack.sourceUrls[0]; source = base.indexOf("{icon_id}") !== -1 ? base.replace(/\{icon_id\}/g, iconId) : base + "/" + iconId; } } const attributes = new DrupalAttribute(Object.entries(svgAttrs)); return Object.assign( { icon_id: iconId, source, content, attributes, group }, resolved ); } // src/renderer/attributes.ts import DrupalAttribute2 from "drupal-attribute"; // src/renderer/collections.ts function entries(value) { if (value == null) return []; if (value instanceof Map) return [...value.entries()]; if (Array.isArray(value)) return value.map((x, i) => [i, x]); if (typeof value === "object") return Object.entries(value); return []; } // src/renderer/attributes.ts function toAttribute(value) { const v = value; if (v && typeof v.addClass === "function") return value; return new DrupalAttribute2( entries(value) ); } function castVarsAttributes(vars) { if (vars == null) return vars; if (vars instanceof Map) { if (vars.has("attributes")) { vars.set("attributes", toAttribute(vars.get("attributes"))); } } else if (typeof vars === "object") { const o = vars; if (Object.prototype.hasOwnProperty.call(o, "attributes")) { o["attributes"] = toAttribute(o["attributes"]); } } return vars; } function patchDrupalAttribute(AttrClass) { const proto = AttrClass.prototype; if (!proto || proto.__sdcClassPatched) return; const normalize = (self) => { if (typeof self.get !== "function") return; const existing = self.get("class"); if (existing == null || Array.isArray(existing)) return; if (existing instanceof Map) { self.set("class", Array.from(existing.values())); } else if (typeof existing === "string") { self.set("class", existing === "" ? [] : [existing]); } else { self.set("class", [existing]); } }; ["addClass", "removeClass", "hasClass"].forEach((name) => { const orig = proto[name]; if (typeof orig !== "function") return; proto[name] = function(...args) { normalize(this); return orig.apply(this, args); }; }); Object.defineProperty(proto, "__sdcClassPatched", { value: true, enumerable: false }); } // src/renderer/nodes.ts var PrintableArray = class { constructor(...items) { __publicField(this, "length"); for (let i = 0; i < items.length; i++) this[i] = items[i]; this.length = items.length; } [Symbol.iterator]() { let i = 0; return { next: () => i < this.length ? { value: this[i++], done: false } : { value: void 0, done: true } }; } toString() { return Array.prototype.join.call(this, ""); } }; // src/renderer/twing.ts function createTwingCore(iconPacks) { let env = null; patchDrupalAttribute(DrupalAttribute3); function renderIcon(packId, iconId, settings) { const pack = iconPacks[packId]; if (!env || !pack || !iconId) return ""; const ctx = buildIconContext(pack, iconId, settings || {}); try { return env.render("_sdc_icon_" + packId, ctx); } catch { return ""; } } function registerCore(e) { env = e; if (!e.__sdcIncludeOverridden && typeof e.functions?.get === "function") { const origInclude = e.functions.get("include"); if (origInclude && typeof origInclude.callable === "function") { e.__sdcIncludeOverridden = true; e.addFunction( createSynchronousFunction( "include", (execCtx, template, variables, withContext, ignoreMissing, sandboxed) => origInclude.callable( execCtx, template, castVarsAttributes(variables), withContext, ignoreMissing, sandboxed ), [...origInclude.acceptedArguments] ) ); } } const loader = e.loader; if (loader && typeof loader.setTemplate === "function") { for (const [packId, pack] of Object.entries(iconPacks)) { loader.setTemplate("_sdc_icon_" + packId, pack.template); } } e.addFunction( createSynchronousFunction( "icon", (_twingCtx, packId, iconId, settings) => renderIcon(packId, iconId, settings), [ { name: "pack_id" }, { name: "icon_id" }, { name: "settings", defaultValue: {} } ] ) ); } return { renderIcon, registerCore, getEnv: () => env }; } function createTwingRuntime(iconPacks) { const core = createTwingCore(iconPacks); return { renderIcon: core.renderIcon, registerSdcRuntime: core.registerCore }; } export { PrintableArray, createTwingCore, createTwingRuntime };