storybook-addon-sdc
Version:
Drupal Single Directory Components as stories
142 lines (136 loc) • 4.71 kB
JavaScript
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/twig.ts
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";
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/twig.ts
function createTwigCore(iconPacks) {
let twig = null;
patchDrupalAttribute(DrupalAttribute3);
function renderIcon(packId, iconId, settings) {
const pack = iconPacks[packId];
if (!twig || !pack || !iconId) return "";
const ctx = buildIconContext(pack, iconId, settings || {});
try {
return twig.twig({ data: pack.template }).render(ctx);
} catch {
return "";
}
}
function registerCore(Twig) {
twig = Twig;
if (Twig.__sdcIconRegistered) return;
Twig.__sdcIconRegistered = true;
Twig.extendFunction("icon", function(packId, iconId, settings) {
const pack = iconPacks[packId];
if (!pack || !iconId) return "";
const ctx = buildIconContext(pack, iconId, settings || {});
try {
return Twig.twig({ data: pack.template }).render(ctx);
} catch (e) {
console.error(
'[SDC Icons] Error rendering icon "' + packId + ":" + iconId + '":',
e
);
return "";
}
});
}
return { renderIcon, registerCore, getTwig: () => twig };
}
function createTwigRuntime(iconPacks) {
const core = createTwigCore(iconPacks);
return { renderIcon: core.renderIcon, registerSdcRuntime: core.registerCore };
}
export {
PrintableArray,
createTwigCore,
createTwigRuntime
};