1 line
7.5 KiB
Plaintext
1 line
7.5 KiB
Plaintext
{"version":3,"file":"css-calc.min.js","sources":["../../src/simpleline/css-calc.js"],"sourcesContent":["/*eslint no-trailing-spaces: \"off\"*/\n/*eslint no-eval: \"off\"*/\n\n/***********************************\n * Licence: MIT\n * (c) 2023 Morglod/jchnkl\n * converted from the typescript @ https://github.com/Morglod/csscalc/\n */\n\n// units -> pixels\nexport const Absolute = {\n /** browser version of pixel */\n px: 1,\n /** One centimeter. 1cm = 96px/2.54 */\n cm: 96 / 2.54,\n /** One millimeter. 1mm = 1/10th of 1cm */\n mm: 96 / 25.4,\n /** One quarter of a millimeter. 1Q = 1/40th of 1cm */\n Q: 96 / 101.6,\n /** One inch. 1in = 2.54cm = 96px */\n in: 96,\n /** One pica. 1pc = 12pt = 1/6th of 1in */\n pc: 96 / 6,\n /** One point. 1pt = 1/72nd of 1in */\n pt: 96 / 72\n };\n\n // units ->(calc context)-> pixels\n export const Relative = {\n /**\n * Equal to 1% of the height of the viewport\n * @param {number} count\n * @param {object} ctx\n */\n vh: (count = 1, ctx) => {\n return ((ctx ? ctx.viewportHeight : window.innerHeight) / 100) * count;\n },\n /**\n * Equal to 1% of the width of the viewport\n * @param {number} count\n * @param {object} ctx\n */\n vw: (count = 1, ctx) => {\n return ((ctx ? ctx.viewportWidth : window.innerWidth) / 100) * count;\n },\n /**\n * 1/100th of the smallest viewport side\n * @param {number} count\n * @param {object} ctx\n */\n vmin: (count = 1, ctx) => {\n return (\n ((ctx\n ? Math.min(ctx.viewportWidth, ctx.viewportHeight)\n : Math.min(window.innerWidth, window.innerHeight)) /\n 100) *\n count\n );\n },\n /**\n * 1/100th of the largest viewport side\n * @param {number} count\n * @param {object} ctx\n */\n vmax: (count = 1, ctx) => {\n return (\n ((ctx\n ? Math.max(ctx.viewportWidth, ctx.viewportHeight)\n : Math.max(window.innerWidth, window.innerHeight)) /\n 100) *\n count\n );\n },\n /**\n * Represents the font-size of <html> element\n * @param {number} count\n * @param {object} ctx\n */\n rem: (count = 1, ctx) => {\n return (\n (ctx\n ? ctx.htmlFontSize\n : parseFloat(\n window.getComputedStyle(document.querySelector(\"html\")).fontSize\n )) * count\n );\n },\n /**\n * percent of width\n * @param {number} count\n * @param {object} ctx\n */\n \"%w\": (count = 1, ctx) => {\n return ((ctx ? ctx.width : document.body.clientWidth) / 100) * count;\n },\n /**\n * percent of height\n * @param {number} count\n * @param {object} ctx\n */\n \"%h\": (count = 1, ctx) => {\n return ((ctx ? ctx.height : document.body.clientHeight) / 100) * count;\n }\n };\n\n export const Units = {\n ...Relative,\n ...Absolute\n };\n\n export const UnitRegexpStr = `(?:\\\\s|^)(\\\\d*(?:\\\\.\\\\d+)?)(${Object.keys(\n Units\n ).join(\"|\")})(?:\\\\s|$|\\\\n)`;\n export const UnitRegexp = new RegExp(UnitRegexpStr);\n export const UnitRegexpGM = new RegExp(UnitRegexpStr, \"gm\");\n\n /**\n *\n * @param {*} count\n * @param {*} fromUnits\n * @param {*} toUnits\n * @param {*} ctx\n * @returns\n */\n export function convert(count, fromUnits, toUnits, ctx = calcCtx()) {\n const baseUnit = Units[fromUnits];\n const basePx =\n typeof baseUnit === \"function\" ? baseUnit(count, ctx) : baseUnit * count;\n\n const dstUnit = Units[toUnits];\n const dstBasePx = typeof dstUnit === \"function\" ? dstUnit(1, ctx) : dstUnit;\n\n return basePx / dstBasePx;\n }\n\n /**\n *\n * @param {*} expr\n * @param {*} toUnits\n * @param {*} ctx\n * @returns\n */\n export function convertAllInStr(expr, toUnits, ctx = calcCtx()) {\n return expr.replace(UnitRegexpGM, (substr, count, unit) => {\n return convert(parseFloat(count), unit, toUnits, ctx).toString();\n });\n }\n\n /**\n *\n * @param {*} el\n * @returns\n */\n export function calcCtx(el) {\n if (el) {\n const rect = el.getBoundingClientRect();\n\n return {\n width: rect.width,\n height: rect.height,\n viewportWidth: window.innerWidth,\n viewportHeight: window.innerHeight,\n htmlFontSize: parseFloat(\n window.getComputedStyle(document.querySelector(\"html\")).fontSize\n ),\n };\n } else {\n return {\n width: document.body.clientWidth,\n height: document.body.clientHeight,\n viewportWidth: window.innerWidth,\n viewportHeight: window.innerHeight,\n htmlFontSize: parseFloat(\n window.getComputedStyle(document.querySelector(\"html\")).fontSize\n )\n };\n }\n }\n\n /**\n *\n * @param {*} expression\n * @param {*} el_ctx\n * @param {*} ctx\n * @returns\n */\n export function calc(expression, el_ctx, ctx) {\n if (el_ctx === undefined) {ctx = calcCtx(); }\n else {\n if (el_ctx instanceof HTMLElement) {\n if (!ctx) {ctx = calcCtx(el_ctx); }\n } else {\n ctx = el_ctx;\n }\n }\n\n return eval(convertAllInStr(expression, \"px\", ctx));\n }\n"],"names":["Absolute","px","cm","mm","Q","in","pc","pt","Relative","vh","count","ctx","viewportHeight","window","innerHeight","vw","viewportWidth","innerWidth","vmin","Math","min","vmax","max","rem","htmlFontSize","parseFloat","getComputedStyle","document","querySelector","fontSize","width","body","clientWidth","height","clientHeight","Units","UnitRegexpStr","Object","keys","join","UnitRegexp","RegExp","UnitRegexpGM","convert","fromUnits","toUnits","calcCtx","baseUnit","basePx","dstUnit","convertAllInStr","expr","replace","substr","unit","toString","el","rect","getBoundingClientRect","calc","expression","el_ctx","undefined","HTMLElement","eval"],"mappings":"8xCAUaA,SAAW,CAEpBC,GAAI,EAEJC,GAAI,GAAK,KAETC,GAAI,GAAK,KAETC,EAAG,GAAK,MAERC,GAAI,GAEJC,GAAI,GAEJC,GAAI,GAAK,mCAIEC,SAAW,CAMtBC,GAAI,eAACC,6DAAQ,EAAGC,kDACLA,IAAMA,IAAIC,eAAiBC,OAAOC,aAAe,IAAOJ,OAOnEK,GAAI,eAACL,6DAAQ,EAAGC,kDACLA,IAAMA,IAAIK,cAAgBH,OAAOI,YAAc,IAAOP,OAOjEQ,KAAM,eAACR,6DAAQ,EAAGC,kDAEZA,IACEQ,KAAKC,IAAIT,IAAIK,cAAeL,IAAIC,gBAChCO,KAAKC,IAAIP,OAAOI,WAAYJ,OAAOC,cACrC,IACFJ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