-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12eb52d
commit 75b12e5
Showing
8 changed files
with
206 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { h } from "hastscript"; | ||
import svgToMiniDataURI from "mini-svg-data-uri"; | ||
|
||
function image({ | ||
svg, | ||
...rest | ||
}: { | ||
width?: string | number; | ||
height?: string | number; | ||
alt?: string; | ||
svg: string; | ||
class?: string; | ||
}) { | ||
return h("img", { | ||
src: svgToMiniDataURI(svg), | ||
...rest, | ||
}); | ||
} | ||
|
||
function figure(cssClass: string, children: any[]) { | ||
return h( | ||
"figure", | ||
{ | ||
class: `beoe ${cssClass}`, | ||
}, | ||
children | ||
); | ||
} | ||
|
||
/** | ||
* Maybe rename img to data-uri | ||
*/ | ||
export type Strategy = | ||
// SVG directly in the HTML | ||
| "inline" | ||
// this doesn't make sense | ||
// | "inline-class-dark-mode" | ||
// SVG as data-uri in img | ||
| "img" | ||
// SVG as data-uri in img and source inside of a picture | ||
| "picture-dark-mode" | ||
// SVG as data-uri in two imgs with light and dark classes | ||
| "img-class-dark-mode"; | ||
|
||
type SvgStrategyBase = { | ||
strategy?: Strategy; | ||
cssClass: string; | ||
}; | ||
|
||
type SvgStrategyCbResult = { | ||
width?: string | number; | ||
height?: string | number; | ||
lightSvg: string; | ||
darkSvg?: string; | ||
}; | ||
|
||
type SvgStrategyOptions = SvgStrategyCbResult & SvgStrategyBase; | ||
|
||
export function svgStrategy({ | ||
strategy, | ||
cssClass, | ||
lightSvg, | ||
darkSvg, | ||
width, | ||
height, | ||
}: SvgStrategyOptions) { | ||
switch (strategy) { | ||
case "img": { | ||
return figure(cssClass, [image({ svg: lightSvg, width, height })]); | ||
} | ||
case "img-class-dark-mode": { | ||
if (!darkSvg) | ||
return figure(cssClass, [image({ svg: lightSvg, width, height })]); | ||
|
||
return figure( | ||
cssClass, | ||
// wrap in additional div for svg-pan-zoom | ||
[ | ||
h("div", [ | ||
image({ svg: lightSvg, width, height, class: "beoe-light" }), | ||
image({ svg: darkSvg, width, height, class: "beoe-dark" }), | ||
]), | ||
] | ||
); | ||
} | ||
case "img-class-dark-mode": { | ||
if (!darkSvg) | ||
return figure(cssClass, [image({ svg: lightSvg, width, height })]); | ||
|
||
const imgLight = image({ svg: lightSvg, width, height }); | ||
const imgDark = h("source", { | ||
width, | ||
height, | ||
src: svgToMiniDataURI(darkSvg), | ||
media: `(prefers-color-scheme: dark)`, | ||
}); | ||
|
||
return figure(cssClass, [h("picture", [imgLight, imgDark])]); | ||
} | ||
// case "inline-class-dark-mode": { | ||
// return `<figure class="beoe ${cssClass}"> | ||
// <div> | ||
// <div class="beoe-light">${lightSvg}</div> | ||
// <div class="beoe-dark">${darkSvg}</div> | ||
// </div> | ||
// </figure>`; | ||
// } | ||
default: { | ||
return `<figure class="beoe ${cssClass}">${lightSvg}</figure>`; | ||
} | ||
} | ||
} | ||
|
||
type SvgStrategyCb = (darkMode: boolean) => SvgStrategyCbResult; | ||
export function svgStrategyCb( | ||
strategy: Strategy, | ||
cssClass: string, | ||
cb: SvgStrategyCb | ||
) { | ||
return svgStrategy({ | ||
strategy, | ||
cssClass, | ||
...cb( | ||
strategy === "img-class-dark-mode" || strategy === "picture-dark-mode" | ||
), | ||
}); | ||
} | ||
|
||
type SvgStrategyCbAsync = (darkMode: boolean) => Promise<SvgStrategyCbResult>; | ||
export async function svgStrategyCbAsync( | ||
strategy: Strategy, | ||
cssClass: string, | ||
cb: SvgStrategyCbAsync | ||
) { | ||
return svgStrategy({ | ||
strategy, | ||
cssClass, | ||
...(await cb( | ||
strategy === "img-class-dark-mode" || strategy === "picture-dark-mode" | ||
)), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<figure class="beoe d2"><svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 256 434"><svg class="d2-1843626214" viewBox="-101 -101 256 434"><rect width="256" height="434" x="-101" y="-101" fill="#FFF" class="fill-N7" rx="0"></rect><g class="shape"><rect width="53" height="66" x="1" fill="#F7F8FE" stroke="#0D32B2" class="stroke-B1 fill-B6" style="stroke-width:2"></rect></g><text x="27.5" y="38.5" fill="#0A0F25" class="text-bold fill-N1" style="text-anchor:middle;font-size:16px">x</text><g class="shape"><rect width="54" height="66" y="166" fill="#F7F8FE" stroke="#0D32B2" class="stroke-B1 fill-B6" style="stroke-width:2"></rect></g><text x="27" y="204.5" fill="#0A0F25" class="text-bold fill-N1" style="text-anchor:middle;font-size:16px">y</text><marker id="a" markerHeight="12" markerUnits="userSpaceOnUse" markerWidth="10" orient="auto" refX="7" refY="6" viewBox="0 0 10 12"><polygon fill="#0D32B2" stroke-width="2" points="0.000000,0.000000 10.000000,6.000000 0.000000,12.000000" class="connection fill-B1"></polygon></marker><path fill="none" stroke="#0D32B2" marker-end="url(#a)" d="M27 68v94" class="connection stroke-B1" mask="url(#b)" style="stroke-width:2"></path><mask id="b" width="256" height="434" x="-101" y="-101" maskUnits="userSpaceOnUse">,<rect width="256" height="434" x="-101" y="-101" fill="#fff"></rect>,<rect width="8" height="21" x="23.5" y="22.5" fill="rgba(0,0,0,0.75)"></rect>,<rect width="9" height="21" x="22.5" y="188.5" fill="rgba(0,0,0,0.75)"></rect>,</mask></svg></svg></figure> | ||
<figure class="beoe d2"><svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 256 434"><svg id="d2-svg" class="d2-1843626214" viewBox="-101 -101 256 434"><rect width="256" height="434" x="-101" y="-101" stroke-width="0" rx="0" style="fill:#fff"></rect><style>.d2-1843626214 .text-bold{font-family:"d2-1843626214-font-bold"}@font-face{font-family:d2-1843626214-font-bold;src:url(data:application/font-woff;base64,d09GRgABAAAAAAZwAAoAAAAACywAAguFAAAAAAAAAAAAAAAAAAAAAAAAAABPUy8yAAAA9AAAAGAAAABgXxHXrmNtYXAAAAFUAAAAMgAAADIADQC0Z2x5ZgAAAYgAAAEQAAABEBXyvOFoZWFkAAACmAAAADYAAAA2G38e1GhoZWEAAALQAAAAJAAAACQKfwXCaG10eAAAAvQAAAAMAAAADAa9AGpsb2NhAAADAAAAAAgAAAAIAFgAtG1heHAAAAMIAAAAIAAAACAAGwD3bmFtZQAAAygAAAMoAAAIKgjwVkFwb3N0AAAGUAAAAB0AAAAg/9EAMgADAioCvAAFAAACigJYAAAASwKKAlgAAAFeADIBKQAAAgsHAwMEAwICBGAAAvcAAAADAAAAAAAAAABBREJPACAAIP//Au7/BgAAA9gBESAAAZ8AAAAAAfAClAAAACAAAwAAAAEAAwABAAAADAAEACYAAAAEAAQAAQAAAHn//wAAAHj///+JAAEAAAAAAAEAAgAAAAAABQBQAAACYgKUAAMACQAPABIAFQAAMxEhESUzJycjBzczNzcjFwM3JwERB1ACEv6lpCcpBCkpBCogmB96X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/l26eH4mi5nHMujI7y4a0sxZ/yA4xs6siljR9afxcQifiYzdefiOFMdUzL1vGTuqdZIFd59wuUOpRvqyOUz0B6Vlk7zS7RnASNTRSaGU/VyqY3c+heaIqaqpZzt7X25DXPbveUW35Bqh0u1LjiVk1swet9UvXc0c60fj4CQlAtZDEiZ0qDgRrzPCbgixnGs7p1oSwpaK58yz41UEjEVgw6J4szI9Dcw3fjGfbChe2dvSSj/kunlqqr7ZHHq1e2M3qh7yzvfuhytTaBhU03X1DQQ18S0H2mn1vn78s31uqU85YiUmPBfL8AzPJrsc8AhY2UY6GZur0NTL0STlxyq+ksiWQ2l58giHODxnAMOeMnzd/q4ZOKMi1txWc/d4pgjuhx+UBUL+y5HvF59+/+sv4tpU7U4nq5OL+49xSd3UOsX2rPb97KniZWTmFu02604I2BacnG76zW5x3j/AAAA//8BAAD///S3T1F4nGJgZgCD/+cYjBiwAAAAAAD//wEAAP//LwECAwAAAA==)}.connection,.shape{stroke-linejoin:round}.shape{shape-rendering:geometricPrecision}.connection{stroke-linecap:round}.d2-1843626214 .fill-N1{fill:#0a0f25}.d2-1843626214 .fill-B6{fill:#f7f8fe}.d2-1843626214 .stroke-B1{stroke:#0d32b2}</style><g id="x"><g class="shape"><path fill="#F7F8FE" stroke="#0D32B2" d="M1 0h53v66H1z" class="stroke-B1 fill-B6" style="stroke-width:2"></path></g><text x="27.5" y="38.5" fill="#0A0F25" class="text-bold fill-N1" style="text-anchor:middle;font-size:16px">x</text></g><g id="y"><g class="shape"><path fill="#F7F8FE" stroke="#0D32B2" d="M0 166h54v66H0z" class="stroke-B1 fill-B6" style="stroke-width:2"></path></g><text x="27" y="204.5" fill="#0A0F25" class="text-bold fill-N1" style="text-anchor:middle;font-size:16px">y</text></g><g id="(x -> y)[0]"><marker id="mk-3488378134" markerHeight="12" markerUnits="userSpaceOnUse" markerWidth="10" orient="auto" refX="7" refY="6" viewBox="0 0 10 12"><path stroke-width="2" d="m0 0 10 6-10 6z" class="connection" style="fill:#0d32b2"></path></marker><path fill="none" stroke="#0D32B2" marker-end="url(#mk-3488378134)" d="M27 68v94" class="connection stroke-B1" mask="url(#d2-1843626214)" style="stroke-width:2"></path></g><mask id="d2-1843626214" width="256" height="434" x="-101" y="-101" maskUnits="userSpaceOnUse"><path fill="#fff" d="M-101-101h256v434h-256z"></path><path fill="rgba(0,0,0,0.75)" d="M23.5 22.5h8v21h-8zM22.5 188.5h9v21h-9z"></path></mask></svg></svg></figure> |
Oops, something went wrong.