@alanizcreative/formation-static 1.3.0-beta.48 → 1.3.0-beta.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/components/Navigation/Navigation.js +1 -1
- package/cjs/objects/Form/FormField.js +1 -1
- package/cjs/objects/Form/FormTypes.d.ts +1 -0
- package/cjs/utils/image/image.js +1 -1
- package/lib/components/Navigation/Navigation.js +1 -1
- package/lib/objects/Form/FormField.js +1 -1
- package/lib/objects/Form/FormTypes.d.ts +1 -0
- package/lib/utils/image/image.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Navigation=void 0;const link_js_1=require("../../utils/link/link.js"),array_js_1=require("../../utils/array/array.js"),object_js_1=require("../../utils/object/object.js"),string_js_1=require("../../utils/string/string.js"),function_js_1=require("../../utils/function/function.js"),number_js_1=require("../../utils/number/number.js"),contentType_js_1=require("../../utils/contentType/contentType.js"),archive_js_1=require("../../utils/archive/archive.js");class Navigation{navigations=[];items=[];init=!1;#itemsById=new Map;#navigationsByLocation=new Map;constructor(props){this.init=this.#initialize(props)}#initialize(props){if(!(0,object_js_1.isObjectStrict)(props))return!1;const{navigations:navigations,items:items}=props;return!(!(0,array_js_1.isArrayStrict)(navigations)||!(0,array_js_1.isArrayStrict)(items))&&(this.navigations=navigations,this.items=items,this.items.forEach(item=>{(0,object_js_1.isObjectStrict)(item)&&this.#itemsById.set(item.id,item)}),this.navigations.forEach(nav=>{if(!(0,object_js_1.isObjectStrict)(nav))return;const{title:title,location:location,items:items=[]}=nav,locations=(0,array_js_1.isArrayStrict)(location)?location:[location];(0,string_js_1.isStringStrict)(title)&&(0,array_js_1.isArrayStrict)(items)&&locations.forEach(loc=>{this.#navigationsByLocation.set(loc,{title:title,items:items})})}),!0)}#recurseItemChildren(children,output,currentLink="",currentType=[]){let childCurrent=!1;return children.forEach(child=>{const newItem=this.#getItem(this.#itemsById.get(child?.id||""),currentLink,currentType);if(!newItem)return;const{current:current,archiveCurrent:archiveCurrent}=newItem;(current||archiveCurrent)&&(childCurrent=!0),output.push(newItem)}),childCurrent}#getItem(item,currentLink,currentType=[]){if(!(0,object_js_1.isObjectStrict)(item))return;const{id:id,title:title,link:link,internalLink:internalLink,externalLink:externalLink,children:children}=item;let external=!1;(0,string_js_1.isStringStrict)(externalLink)&&(external=!0);const newLink=(0,string_js_1.isStringStrict)(link)?link:(0,link_js_1.getLink)(internalLink,externalLink),newItem={...item,id:id,title:title,link:newLink,external:external};newLink&&!external&&(newItem.current=newLink===currentLink);const internalId=internalLink?.id;if((0,string_js_1.isStringStrict)(internalId)){let archiveCurrent=!1;for(const type of currentType){if(internalId===(0,archive_js_1.getArchiveMeta)(type,internalLink?.locale).id){archiveCurrent=!0;break}}newItem.archiveCurrent=archiveCurrent}let descendentCurrent=!1;if((0,array_js_1.isArrayStrict)(children)){const newChildren=[];descendentCurrent=this.#recurseItemChildren(children,newChildren,currentLink,currentType),newItem.children=newChildren}return descendentCurrent&&(newItem.descendentCurrent=descendentCurrent),newItem}#getItems(items=[],currentLink,currentType){const resItems=[];return items.forEach(item=>{const newItem=this.#getItem(this.#itemsById.get(item?.id||""),currentLink,currentType);newItem&&resItems.push(newItem)}),resItems}#recurseOutput=(items,output,args,depth=0,maxDepth)=>{if((0,number_js_1.isNumber)(maxDepth)&&depth>maxDepth)return;const dataAttr=(0,string_js_1.isStringStrict)(args.dataAttr)?args.dataAttr:"data-nav",depthAttr=args.depthAttr?` ${dataAttr}-depth="${depth}"`:"",listFilterArgs={args:args,output:output,items:items,depth:depth};(0,function_js_1.isFunction)(args.filterBeforeList)&&args.filterBeforeList(listFilterArgs);const listClasses=(0,string_js_1.isStringStrict)(args.listClass)?` class="${args.listClass}"`:"",listAttrs=(0,string_js_1.isStringStrict)(args.listAttr)?` ${args.listAttr}`:"",listTag=(0,string_js_1.isStringStrict)(args.listTag)?args.listTag:"ul";output.ref+=`<${listTag}${depthAttr}${listClasses}${listAttrs}>`,items.forEach((item,index)=>{const
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Navigation=void 0;const link_js_1=require("../../utils/link/link.js"),array_js_1=require("../../utils/array/array.js"),object_js_1=require("../../utils/object/object.js"),string_js_1=require("../../utils/string/string.js"),function_js_1=require("../../utils/function/function.js"),number_js_1=require("../../utils/number/number.js"),contentType_js_1=require("../../utils/contentType/contentType.js"),archive_js_1=require("../../utils/archive/archive.js");class Navigation{navigations=[];items=[];init=!1;#itemsById=new Map;#navigationsByLocation=new Map;constructor(props){this.init=this.#initialize(props)}#initialize(props){if(!(0,object_js_1.isObjectStrict)(props))return!1;const{navigations:navigations,items:items}=props;return!(!(0,array_js_1.isArrayStrict)(navigations)||!(0,array_js_1.isArrayStrict)(items))&&(this.navigations=navigations,this.items=items,this.items.forEach(item=>{(0,object_js_1.isObjectStrict)(item)&&this.#itemsById.set(item.id,item)}),this.navigations.forEach(nav=>{if(!(0,object_js_1.isObjectStrict)(nav))return;const{title:title,location:location,items:items=[]}=nav,locations=(0,array_js_1.isArrayStrict)(location)?location:[location];(0,string_js_1.isStringStrict)(title)&&(0,array_js_1.isArrayStrict)(items)&&locations.forEach(loc=>{this.#navigationsByLocation.set(loc,{title:title,items:items})})}),!0)}#recurseItemChildren(children,output,currentLink="",currentType=[]){let childCurrent=!1;return children.forEach(child=>{const newItem=this.#getItem(this.#itemsById.get(child?.id||""),currentLink,currentType);if(!newItem)return;const{current:current,archiveCurrent:archiveCurrent}=newItem;(current||archiveCurrent)&&(childCurrent=!0),output.push(newItem)}),childCurrent}#getItem(item,currentLink,currentType=[]){if(!(0,object_js_1.isObjectStrict)(item))return;const{id:id,title:title,link:link,internalLink:internalLink,externalLink:externalLink,children:children}=item;let external=!1;(0,string_js_1.isStringStrict)(externalLink)&&(external=!0);const newLink=(0,string_js_1.isStringStrict)(link)?link:(0,link_js_1.getLink)(internalLink,externalLink),newItem={...item,id:id,title:title,link:newLink,external:external};newLink&&!external&&(newItem.current=newLink===currentLink);const internalId=internalLink?.id;if((0,string_js_1.isStringStrict)(internalId)){let archiveCurrent=!1;for(const type of currentType){if(internalId===(0,archive_js_1.getArchiveMeta)(type,internalLink?.locale).id){archiveCurrent=!0;break}}newItem.archiveCurrent=archiveCurrent}let descendentCurrent=!1;if((0,array_js_1.isArrayStrict)(children)){const newChildren=[];descendentCurrent=this.#recurseItemChildren(children,newChildren,currentLink,currentType),newItem.children=newChildren}return descendentCurrent&&(newItem.descendentCurrent=descendentCurrent),newItem}#getItems(items=[],currentLink,currentType){const resItems=[];return items.forEach(item=>{const newItem=this.#getItem(this.#itemsById.get(item?.id||""),currentLink,currentType);newItem&&resItems.push(newItem)}),resItems}#recurseOutput=(items,output,args,depth=0,maxDepth)=>{if((0,number_js_1.isNumber)(maxDepth)&&depth>maxDepth)return;const dataAttr=(0,string_js_1.isStringStrict)(args.dataAttr)?args.dataAttr:"data-nav",depthAttr=args.depthAttr?` ${dataAttr}-depth="${depth}"`:"",listFilterArgs={args:args,output:output,items:items,depth:depth};(0,function_js_1.isFunction)(args.filterBeforeList)&&args.filterBeforeList(listFilterArgs);const listClasses=(0,string_js_1.isStringStrict)(args.listClass)?` class="${args.listClass}"`:"",listAttrs=(0,string_js_1.isStringStrict)(args.listAttr)?` ${args.listAttr}`:"",listTag=(0,string_js_1.isStringStrict)(args.listTag)?args.listTag:"ul";output.ref+=`<${listTag}${depthAttr}${listClasses}${listAttrs}>`,items.forEach((item,index)=>{const filterArgs={args:args,item:item,output:output,index:index,items:items,depth:depth};(0,function_js_1.isFunction)(args.filterBeforeItem)&&args.filterBeforeItem(filterArgs);const{title:title,link:link,external:external=!1,children:children,current:current=!1,descendentCurrent:descendentCurrent=!1,archiveCurrent:archiveCurrent=!1}=item,itemClasses=(0,string_js_1.isStringStrict)(args.itemClass)?` class="${args.itemClass}"`:"",itemTag=(0,string_js_1.isStringStrict)(args.itemTag)?args.itemTag:"li";let itemAttrs=(0,string_js_1.isStringStrict)(args.itemAttr)?` ${args.itemAttr}`:"";current&&(itemAttrs+=` ${dataAttr}-current`),descendentCurrent&&(itemAttrs+=` ${dataAttr}-descendent-current`),archiveCurrent&&(itemAttrs+=` ${dataAttr}-archive-current`),output.ref+=`<${itemTag}${depthAttr}${itemClasses}${itemAttrs}>`,(0,function_js_1.isFunction)(args.filterBeforeLink)&&args.filterBeforeLink(filterArgs);const linkClassesArr=[];(0,string_js_1.isStringStrict)(args.linkClass)&&linkClassesArr.push(args.linkClass),!external&&(0,string_js_1.isStringStrict)(args.internalLinkClass)&&linkClassesArr.push(args.internalLinkClass);const hasLink=(0,string_js_1.isStringStrict)(link),linkClasses=linkClassesArr.length?` class="${linkClassesArr.join(" ")}"`:"",linkAttrsArr=[hasLink?`href="${link}"`:'type="button"'];(0,string_js_1.isStringStrict)(args.linkAttr)&&linkAttrsArr.push(args.linkAttr),current&&(linkAttrsArr.push(`${dataAttr}-current`),hasLink&&linkAttrsArr.push('aria-current="page"')),descendentCurrent&&linkAttrsArr.push(`${dataAttr}-descendent-current`),archiveCurrent&&linkAttrsArr.push(`${dataAttr}-archive-current`);const linkAttrs=` ${linkAttrsArr.join(" ")}`,linkTag=hasLink?"a":"button";output.ref+=`<${linkTag}${depthAttr}${linkClasses}${linkAttrs}>`,(0,function_js_1.isFunction)(args.filterBeforeLinkText)&&args.filterBeforeLinkText(filterArgs),output.ref+=title,(0,function_js_1.isFunction)(args.filterAfterLinkText)&&args.filterAfterLinkText(filterArgs),output.ref+=`</${linkTag}>`,(0,function_js_1.isFunction)(args.filterAfterLink)&&args.filterAfterLink(filterArgs),(0,array_js_1.isArrayStrict)(children)&&this.#recurseOutput(children,output,args,depth+1,maxDepth),output.ref+=`</${itemTag}>`,(0,function_js_1.isFunction)(args.filterAfterItem)&&args.filterAfterItem(filterArgs)}),output.ref+=`</${listTag}>`,(0,function_js_1.isFunction)(args.filterAfterList)&&args.filterAfterList(listFilterArgs)};getOutput(location,args,maxDepth){const nav=this.#navigationsByLocation.get(location);if(!nav)return"";args=Object.assign({depthAttr:!1},args||{});const items=nav.items,{currentLink:currentLink,currentType:currentType}=args,currLink=(0,string_js_1.isStringStrict)(currentLink)?currentLink:"",currType=((0,array_js_1.isArrayStrict)(currentType)?currentType:[currentType]).map(type=>(0,contentType_js_1.normalizeContentType)(type)),normalizedItems=this.#getItems(items,currLink,currType);if(!normalizedItems.length)return"";const output={ref:""};return this.#recurseOutput(normalizedItems,output,args,0,maxDepth),output.ref}getBreadcrumbs(items,args){if(!(0,array_js_1.isArrayStrict)(items))return"";const current=(args=Object.assign({a11yClass:"a-hide-vis"},args||{})).current,dataAttr=(0,string_js_1.isStringStrict)(args.dataAttr)?args.dataAttr:"data-nav",listClasses=(0,string_js_1.isStringStrict)(args.listClass)?` class="${args.listClass}"`:"",listAttrs=(0,string_js_1.isStringStrict)(args.listAttr)?` ${args.listAttr}`:"",filteredItems=items.filter(item=>!!(0,string_js_1.isStringStrict)(item.title)&&(!!(0,string_js_1.isString)(item.slug)&&(item.slug=(0,link_js_1.getSlug)({id:item.id,slug:item.slug,contentType:item.contentType,itemData:item.internalLink}),!0))),itemClasses=(0,string_js_1.isStringStrict)(args.itemClass)?` class="${args.itemClass}"`:"",itemAttrs=(0,string_js_1.isStringStrict)(args.itemAttr)?` ${args.itemAttr}`:"",lastItemIndex=filteredItems.length-1,itemsArr=filteredItems.map((item,index)=>{const{title:title,slug:slug}=item,link=(0,link_js_1.getPermalink)(slug),output={ref:""},lastLevel=lastItemIndex===index,filterArgs={output:output,lastLevel:lastLevel};output.ref+=`<li${itemClasses}${itemAttrs}${lastLevel?` ${dataAttr}-last`:""}>`,(0,function_js_1.isFunction)(args.filterBeforeLink)&&args.filterBeforeLink(filterArgs);const linkClassesArr=[];(0,string_js_1.isStringStrict)(args.linkClass)&&linkClassesArr.push(args.linkClass),(0,string_js_1.isStringStrict)(args.internalLinkClass)&&linkClassesArr.push(args.internalLinkClass);const linkClasses=linkClassesArr.length?` class="${linkClassesArr.join(" ")}"`:"",linkAttrs=(0,string_js_1.isStringStrict)(args.linkAttr)?` ${args.linkAttr}`:"";return output.ref+=`<a${linkClasses} href="${link}"${linkAttrs}>${title}</a>`,(0,function_js_1.isFunction)(args.filterAfterLink)&&args.filterAfterLink(filterArgs),output.ref+="</li>",output.ref}),currentLabel=(0,string_js_1.isStringStrict)(args.currentLabel)?args.currentLabel:"(current page)",currentClasses=(0,string_js_1.isStringStrict)(args.currentClass)?` class="${args.currentClass}"`:"",a11yClasses=(0,string_js_1.isStringStrict)(args.a11yClass)?` class="${args.a11yClass}"`:"",currentItem=(0,string_js_1.isStringStrict)(current)?`\n <li${itemClasses}${itemAttrs} ${dataAttr}-current>\n <span${currentClasses}>${current}<span${a11yClasses}> ${currentLabel}</span></span>\n </li>\n `:"";return`\n <ol${listClasses}${listAttrs}>\n ${itemsArr.join("")}\n ${currentItem}\n </ol>\n `}getItemsById(){return this.#itemsById}getNavigationsByLocation(){return this.#navigationsByLocation}getNavigationByLocation(location){return this.#navigationsByLocation.get(location)}}exports.Navigation=Navigation;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FormField=void 0;const uuid_1=require("uuid"),filter_js_1=require("../../utils/filter/filter.js"),string_js_1=require("../../utils/string/string.js"),object_js_1=require("../../utils/object/object.js"),FormField=props=>{if(!(0,object_js_1.isObjectStrict)(props))return[];if(props=(0,filter_js_1.applyFilters)("formFieldProps",props),!(0,object_js_1.isObjectStrict)(props))return[];const{args:args}=props,{type:type="text",name:name,label:label,hint:hint,value:value,required:required=!1,attr:attr,emptyError:emptyError,invalidError:invalidError,fieldClasses:fieldClasses,fieldAttr:fieldAttr,fieldsetClasses:fieldsetClasses,fieldsetAttr:fieldsetAttr,labelClasses:labelClasses,classes:classes,radioIcon:radioIcon,checkboxIcon:checkboxIcon}=(0,object_js_1.isObjectStrict)(args)?args:{},isHidden="hidden"===type;if(!(0,string_js_1.isStringStrict)(label)&&!isHidden)return[];let isFieldset="fieldset"===type;if(!isFieldset&&!(0,string_js_1.isStringStrict)(name))return[];const isOption="checkbox"===type||"radio"===type,isSelect="select"===type;("checkbox-group"===type||"radio-group"===type)&&(isFieldset=!0);const isTextarea="textarea"===type,hasValue=(0,string_js_1.isStringStrict)(value),id=(0,uuid_1.v4)();let optionIcon="",selectIcon="";"radio"===type&&(0,string_js_1.isStringStrict)(radioIcon)&&(optionIcon=radioIcon),"checkbox"===type&&(0,string_js_1.isStringStrict)(checkboxIcon)&&(optionIcon=checkboxIcon),"select"===type&&(0,string_js_1.isStringStrict)(args.selectIcon)&&(selectIcon=args.selectIcon);const requiredIcon=required&&(0,string_js_1.isStringStrict)(args.requiredIcon)?args.requiredIcon:"",fieldAttrs=["data-form-field"],fieldsetAttrs=[],attrs=["data-form-input"];if((0,string_js_1.isStringStrict)(attr)){const attrsArr=attr.split("\n");for(const attrItem of attrsArr){const[attrName,attrValue]=attrItem.split(" : ");(0,string_js_1.isStringStrict)(attrName)&&(0,string_js_1.isStringStrict)(attrValue)&&attrs.push(`${attrName}="${attrValue}"`)}}isTextarea||isSelect||!hasValue||attrs.push(`value="${value}"`),(0,string_js_1.isStringStrict)(classes)&&attrs.push(`class="${classes}"`),(0,string_js_1.isStringStrict)(fieldClasses)&&fieldAttrs.push(`class="${fieldClasses}"`),(0,string_js_1.isStringStrict)(fieldsetClasses)&&fieldsetAttrs.push(`class="${fieldsetClasses}"`),(0,string_js_1.isStringStrict)(fieldAttr)&&fieldAttrs.push(fieldAttr),(0,string_js_1.isStringStrict)(fieldsetAttr)&&fieldsetAttrs.push(fieldsetAttr),(0,string_js_1.isStringStrict)(emptyError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-empty="${emptyError}"`),(0,string_js_1.isStringStrict)(invalidError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-invalid="${invalidError}"`),required&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(isFieldset?"data-form-required":"required");const fieldAtts=` ${fieldAttrs.join(" ")}`,fieldsetAtts=fieldsetAttrs.length?` ${fieldsetAttrs.join(" ")}`:"",atts=` ${attrs.join(" ")}`;let hintOutput="";(0,string_js_1.isStringStrict)(hint)&&(hintOutput=`<small data-form-hint>${hint}</small>`);let labelBefore="",labelAfter="";const labelClass=(0,string_js_1.isStringStrict)(labelClasses)?` class="${labelClasses}"`:"";isFieldset?labelBefore=`\n <legend id="${(0,uuid_1.v4)()}"${labelClass}>\n <span data-form-legend>\n <span data-form-legend-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </legend>\n `:isOption?labelAfter=`\n <label for="${id}"${labelClass}>\n <span data-form-option>\n ${optionIcon}\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n </span>\n ${hintOutput}\n </label>\n `:labelBefore=`\n <label for="${id}"${labelClass}>\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </label>\n `;let beforeOutput="",afterOutput="";switch(type){case"text":case"email":case"checkbox":case"radio":case"number":case"password":case"tel":case"url":case"hidden":beforeOutput=`<input type="${type}" name="${name}" id="${id}"${atts}>`;break;case"textarea":beforeOutput=`<textarea name="${name}" id="${id}"${atts}>${hasValue?value:""}</textarea>`;break;case"select":beforeOutput=`\n <div data-form-select>\n <select name="${name}" id="${id}"${atts}>`,afterOutput=`\n </select>\n ${selectIcon}\n </div>`}return isHidden?[beforeOutput]:[`\n
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FormField=void 0;const uuid_1=require("uuid"),filter_js_1=require("../../utils/filter/filter.js"),string_js_1=require("../../utils/string/string.js"),object_js_1=require("../../utils/object/object.js"),FormField=props=>{if(!(0,object_js_1.isObjectStrict)(props))return[];if(props=(0,filter_js_1.applyFilters)("formFieldProps",props),!(0,object_js_1.isObjectStrict)(props))return[];const{args:args}=props,{type:type="text",name:name,label:label,hint:hint,value:value,required:required=!1,attr:attr,emptyError:emptyError,invalidError:invalidError,fieldTag:fieldTag="div",fieldClasses:fieldClasses,fieldAttr:fieldAttr,fieldsetClasses:fieldsetClasses,fieldsetAttr:fieldsetAttr,labelClasses:labelClasses,classes:classes,radioIcon:radioIcon,checkboxIcon:checkboxIcon}=(0,object_js_1.isObjectStrict)(args)?args:{},isHidden="hidden"===type;if(!(0,string_js_1.isStringStrict)(label)&&!isHidden)return[];let isFieldset="fieldset"===type;if(!isFieldset&&!(0,string_js_1.isStringStrict)(name))return[];const isOption="checkbox"===type||"radio"===type,isSelect="select"===type;("checkbox-group"===type||"radio-group"===type)&&(isFieldset=!0);const isTextarea="textarea"===type,hasValue=(0,string_js_1.isStringStrict)(value),id=(0,uuid_1.v4)();let optionIcon="",selectIcon="";"radio"===type&&(0,string_js_1.isStringStrict)(radioIcon)&&(optionIcon=radioIcon),"checkbox"===type&&(0,string_js_1.isStringStrict)(checkboxIcon)&&(optionIcon=checkboxIcon),"select"===type&&(0,string_js_1.isStringStrict)(args.selectIcon)&&(selectIcon=args.selectIcon);const requiredIcon=required&&(0,string_js_1.isStringStrict)(args.requiredIcon)?args.requiredIcon:"",fieldAttrs=["data-form-field"],fieldsetAttrs=[],attrs=["data-form-input"];if((0,string_js_1.isStringStrict)(attr)){const attrsArr=attr.split("\n");for(const attrItem of attrsArr){const[attrName,attrValue]=attrItem.split(" : ");(0,string_js_1.isStringStrict)(attrName)&&(0,string_js_1.isStringStrict)(attrValue)&&attrs.push(`${attrName}="${attrValue}"`)}}isTextarea||isSelect||!hasValue||attrs.push(`value="${value}"`),(0,string_js_1.isStringStrict)(classes)&&attrs.push(`class="${classes}"`),(0,string_js_1.isStringStrict)(fieldClasses)&&fieldAttrs.push(`class="${fieldClasses}"`),(0,string_js_1.isStringStrict)(fieldsetClasses)&&fieldsetAttrs.push(`class="${fieldsetClasses}"`),(0,string_js_1.isStringStrict)(fieldAttr)&&fieldAttrs.push(fieldAttr),(0,string_js_1.isStringStrict)(fieldsetAttr)&&fieldsetAttrs.push(fieldsetAttr),(0,string_js_1.isStringStrict)(emptyError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-empty="${emptyError}"`),(0,string_js_1.isStringStrict)(invalidError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-invalid="${invalidError}"`),required&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(isFieldset?"data-form-required":"required");const fieldAtts=` ${fieldAttrs.join(" ")}`,fieldsetAtts=fieldsetAttrs.length?` ${fieldsetAttrs.join(" ")}`:"",atts=` ${attrs.join(" ")}`;let hintOutput="";(0,string_js_1.isStringStrict)(hint)&&(hintOutput=`<small data-form-hint>${hint}</small>`);let labelBefore="",labelAfter="";const labelClass=(0,string_js_1.isStringStrict)(labelClasses)?` class="${labelClasses}"`:"";isFieldset?labelBefore=`\n <legend id="${(0,uuid_1.v4)()}"${labelClass}>\n <span data-form-legend>\n <span data-form-legend-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </legend>\n `:isOption?labelAfter=`\n <label for="${id}"${labelClass}>\n <span data-form-option>\n ${optionIcon}\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n </span>\n ${hintOutput}\n </label>\n `:labelBefore=`\n <label for="${id}"${labelClass}>\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </label>\n `;let beforeOutput="",afterOutput="";switch(type){case"text":case"email":case"checkbox":case"radio":case"number":case"password":case"tel":case"url":case"hidden":beforeOutput=`<input type="${type}" name="${name}" id="${id}"${atts}>`;break;case"textarea":beforeOutput=`<textarea name="${name}" id="${id}"${atts}>${hasValue?value:""}</textarea>`;break;case"select":beforeOutput=`\n <div data-form-select>\n <select name="${name}" id="${id}"${atts}>`,afterOutput=`\n </select>\n ${selectIcon}\n </div>`}return isHidden?[beforeOutput]:[`\n <${fieldTag}${fieldAtts}>\n ${isFieldset?`<fieldset${fieldsetAtts}>`:""}\n ${labelBefore}\n ${isFieldset?"<div data-form-group>":""}\n ${beforeOutput}`,`${afterOutput}\n ${labelAfter}\n ${isFieldset?"</div></fieldset>":""}\n </${fieldTag}>\n `]};exports.FormField=FormField;
|
package/cjs/utils/image/image.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getImageMaxWidth=exports.getImageClosestSize=exports.getImage=void 0;const config_js_1=require("../../config/config.js"),string_js_1=require("../string/string.js"),array_js_1=require("../array/array.js"),number_js_1=require("../number/number.js"),object_js_1=require("../object/object.js"),dataSource_js_1=require("../dataSource/dataSource.js"),getImageUrl=(url,format,quality,width,height,params)=>{const urlObj=new URL(url);return Object.entries(params).forEach(([key,value])=>{let val=value;"%format"===val&&(val=format),"%quality"===val&&(val=`${quality}`),"%width"===val&&(val=`${width}`),"%height"===val&&(val=`${height}`),urlObj.searchParams.set(key,val)}),urlObj.toString()},getImage=(args,returnDetails=!1)=>{const{data:data,classes:classes,attr:attr,alt:imageAlt="inherit",width:width="auto",height:height="auto",lazy:lazy=!0,picture:picture=!1,quality:quality=75,source:source=config_js_1.config.source,maxWidth:maxWidth=1200,viewportWidth:viewportWidth=100,format:format="webp",params:params={fm:"%format",q:"%quality",w:"%width",h:"%height"}}=(0,object_js_1.isObjectStrict)(args)?args:{},fallback=returnDetails?{output:"",aspectRatio:0,naturalWidth:0,naturalHeight:0,src:"",srcFallback:"",srcset:[],sizes:""}:"";if(!(0,object_js_1.isObjectStrict)(data))return fallback;const{path:path="",alt:dataAlt="",width:naturalWidth=1,height:naturalHeight=1,format:naturalFormat="jpg",sizes:dataSizes}=data;let{url:url=config_js_1.config.image.remoteUrl}=data,alt="inherit"===imageAlt?dataAlt:imageAlt;(0,string_js_1.isStringStrict)(alt)||(alt="");const isLocal="local"===source,isRemote=dataSource_js_1.dataSource.isContentful(source)||"remote"===source,isWordpress=dataSource_js_1.dataSource.isWordPress(source);if(isLocal){if(!(0,string_js_1.isStringStrict)(path))return fallback;url=`${config_js_1.config.image.localUrl}/${path}`}const aspectRatio=naturalHeight/naturalWidth,aspectRatioReverse=naturalWidth/naturalHeight;let w=naturalWidth,h=naturalHeight;if((0,number_js_1.isNumber)(width)&&(w=width,h=(0,string_js_1.isString)(height)?w*aspectRatio:height),(0,number_js_1.isNumber)(height)&&(h=height,w=(0,string_js_1.isString)(width)?h*aspectRatioReverse:width),w>maxWidth&&(w=Math.round(maxWidth),h=Math.round(w*aspectRatio)),isWordpress&&dataSizes&&!dataSizes[w]){const isNatural=w===naturalWidth;isNatural||(w=getImageClosestSize(w,Object.keys(dataSizes).map(s=>parseInt(s,10))),h=Math.round(w*aspectRatio)),isNatural&&(dataSizes[w]=url)}let src=url,srcFallback=url;isLocal&&(src=`${url}.${format}`,srcFallback=`${url}.${naturalFormat}`),isRemote&&(src=getImageUrl(url,format,quality,w,h,params),srcFallback=getImageUrl(url,naturalFormat,quality,w,h,params));const sizes=`(min-width: ${w/16}rem) ${w/16}rem, ${viewportWidth}vw`,srcsetFallback=[];let srcset=[...config_js_1.config.image.sizes];srcset.includes(w)||srcset.push(w),srcset=srcset.filter(s=>s<=w),srcset.sort((a,b)=>a-b);const srcsetSource=[];srcset.forEach(s=>{if(isLocal){const base=`${url}${s!==naturalWidth?`@${s}`:""}`;srcsetFallback.push(`${base}.${naturalFormat} ${s}w`),srcsetSource.push(`${base}.${format} ${s}w`)}if(isRemote){const sizeHeight=Math.round(s*aspectRatio);srcsetFallback.push(getImageUrl(url,naturalFormat,quality,s,sizeHeight,params)+` ${s}w`),srcsetSource.push(getImageUrl(url,format,quality,s,sizeHeight,params)+` ${s}w`)}if(isWordpress){const sizeUrl=dataSizes?.[s];(0,string_js_1.isStringStrict)(sizeUrl)&&srcsetSource.push(`${sizeUrl} ${s}w`)}});let sourceOutput="";picture&&(sourceOutput=`<source srcset="${srcsetSource.join(", ")}" sizes="${sizes}" type="image/${format}">`);let eagerHackOutput="";lazy||(eagerHackOutput=`\n <img\n alt=""\n role="presentation"\n aria-hidden="true"\n src="data:image/svg+xml;charset=utf-8,%3Csvg height='${h}' width='${w}' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E" style="pointerEvents: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%"\n >\n `);const output=`\n ${eagerHackOutput}\n ${sourceOutput}\n <img\n ${(0,string_js_1.isStringStrict)(classes)?` class="${classes}"`:""}\n alt="${alt}"\n src="${picture?srcFallback:src}"\n srcset="${picture?srcsetFallback.join(", "):srcsetSource.join(", ")}"\n sizes="${sizes}"\n width="${w}"\n height="${h}"\n ${(0,string_js_1.isStringStrict)(attr)?` ${attr}`:""}\n ${lazy?' loading="lazy" decoding="async"':' loading="eager" fetchpriority="high"'}\n >\n `;return returnDetails?{output:output,aspectRatio:aspectRatio,naturalWidth:naturalWidth,naturalHeight:naturalHeight,src:src,srcFallback:srcFallback,srcset:srcsetSource,sizes:sizes}:output};exports.getImage=getImage;const getImageClosestSize=(size,sizes=[])=>(sizes.length||(sizes=config_js_1.config.image.sizes),[...sizes].reduce((prev,curr)=>Math.abs(curr-size)<=Math.abs(prev-size)?curr:prev));exports.getImageClosestSize=getImageClosestSize;const getImageMaxWidth=args=>{if(!(0,object_js_1.isObjectStrict)(args))return 0;const{parents:parents,widths:widths,maxWidths:maxWidths,breakpoints:breakpoints,source:source=config_js_1.config.source}=args;if(!((0,array_js_1.isArrayStrict)(parents)&&(0,object_js_1.isObjectStrict)(widths)&&(0,object_js_1.isObjectStrict)(maxWidths)&&(0,array_js_1.isArrayStrict)(breakpoints)))return 0;const w=[];let m=0;if(parents.forEach(parent=>{if(!(0,object_js_1.isObjectStrict)(parent))return;const{renderType:renderType,args:args}=parent;if((0,object_js_1.isObjectStrict)(args)){if("column"===renderType){const{width:width="Default",widthSmall:widthSmall="Default",widthMedium:widthMedium="Default",widthLarge:widthLarge="Default"}=args;w[0]=(0,number_js_1.isNumber)(widths[width])&&widths[width]>0?widths[width]:1,w[1]=(0,number_js_1.isNumber)(widths[widthSmall])&&widths[widthSmall]>0?widths[widthSmall]:w[0],w[2]=(0,number_js_1.isNumber)(widths[widthMedium])&&widths[widthMedium]>0?widths[widthMedium]:w[1],w[3]=(0,number_js_1.isNumber)(widths[widthLarge])&&widths[widthLarge]>0?widths[widthLarge]:w[2]}if("container"===renderType){const{maxWidth:maxWidth="Default"}=args;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getImageMaxWidth=exports.getImageClosestSize=exports.getImage=void 0;const config_js_1=require("../../config/config.js"),string_js_1=require("../string/string.js"),array_js_1=require("../array/array.js"),number_js_1=require("../number/number.js"),object_js_1=require("../object/object.js"),dataSource_js_1=require("../dataSource/dataSource.js"),getImageUrl=(url,format,quality,width,height,params)=>{const urlObj=new URL(url);return Object.entries(params).forEach(([key,value])=>{let val=value;"%format"===val&&(val=format),"%quality"===val&&(val=`${quality}`),"%width"===val&&(val=`${width}`),"%height"===val&&(val=`${height}`),urlObj.searchParams.set(key,val)}),urlObj.toString()},getImage=(args,returnDetails=!1)=>{const{data:data,classes:classes,attr:attr,alt:imageAlt="inherit",width:width="auto",height:height="auto",lazy:lazy=!0,picture:picture=!1,quality:quality=75,source:source=config_js_1.config.source,maxWidth:maxWidth=1200,viewportWidth:viewportWidth=100,format:format="webp",params:params={fm:"%format",q:"%quality",w:"%width",h:"%height"}}=(0,object_js_1.isObjectStrict)(args)?args:{},fallback=returnDetails?{output:"",aspectRatio:0,naturalWidth:0,naturalHeight:0,src:"",srcFallback:"",srcset:[],sizes:""}:"";if(!(0,object_js_1.isObjectStrict)(data))return fallback;const{path:path="",alt:dataAlt="",width:naturalWidth=1,height:naturalHeight=1,format:naturalFormat="jpg",sizes:dataSizes}=data;let{url:url=config_js_1.config.image.remoteUrl}=data,alt="inherit"===imageAlt?dataAlt:imageAlt;(0,string_js_1.isStringStrict)(alt)||(alt="");const isLocal="local"===source,isRemote=dataSource_js_1.dataSource.isContentful(source)||"remote"===source,isWordpress=dataSource_js_1.dataSource.isWordPress(source);if(isLocal){if(!(0,string_js_1.isStringStrict)(path))return fallback;url=`${config_js_1.config.image.localUrl}/${path}`}const aspectRatio=naturalHeight/naturalWidth,aspectRatioReverse=naturalWidth/naturalHeight;let w=naturalWidth,h=naturalHeight;if((0,number_js_1.isNumber)(width)&&(w=width,h=(0,string_js_1.isString)(height)?w*aspectRatio:height),(0,number_js_1.isNumber)(height)&&(h=height,w=(0,string_js_1.isString)(width)?h*aspectRatioReverse:width),w>maxWidth&&(w=Math.round(maxWidth),h=Math.round(w*aspectRatio)),isWordpress&&dataSizes&&!dataSizes[w]){const isNatural=w===naturalWidth;isNatural||(w=getImageClosestSize(w,Object.keys(dataSizes).map(s=>parseInt(s,10))),h=Math.round(w*aspectRatio)),isNatural&&(dataSizes[w]=url)}let src=url,srcFallback=url;isLocal&&(src=`${url}.${format}`,srcFallback=`${url}.${naturalFormat}`),isRemote&&(src=getImageUrl(url,format,quality,w,h,params),srcFallback=getImageUrl(url,naturalFormat,quality,w,h,params));const sizes=`(min-width: ${w/16}rem) ${w/16}rem, ${viewportWidth}vw`,srcsetFallback=[];let srcset=[...config_js_1.config.image.sizes];srcset.includes(w)||srcset.push(w),srcset=srcset.filter(s=>s<=w),srcset.sort((a,b)=>a-b);const srcsetSource=[];srcset.forEach(s=>{if(isLocal){const base=`${url}${s!==naturalWidth?`@${s}`:""}`;srcsetFallback.push(`${base}.${naturalFormat} ${s}w`),srcsetSource.push(`${base}.${format} ${s}w`)}if(isRemote){const sizeHeight=Math.round(s*aspectRatio);srcsetFallback.push(getImageUrl(url,naturalFormat,quality,s,sizeHeight,params)+` ${s}w`),srcsetSource.push(getImageUrl(url,format,quality,s,sizeHeight,params)+` ${s}w`)}if(isWordpress){const sizeUrl=dataSizes?.[s];(0,string_js_1.isStringStrict)(sizeUrl)&&srcsetSource.push(`${sizeUrl} ${s}w`)}});let sourceOutput="";picture&&(sourceOutput=`<source srcset="${srcsetSource.join(", ")}" sizes="${sizes}" type="image/${format}">`);let eagerHackOutput="";lazy||(eagerHackOutput=`\n <img\n alt=""\n role="presentation"\n aria-hidden="true"\n src="data:image/svg+xml;charset=utf-8,%3Csvg height='${h}' width='${w}' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E" style="pointerEvents: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%"\n >\n `);const output=`\n ${eagerHackOutput}\n ${sourceOutput}\n <img\n ${(0,string_js_1.isStringStrict)(classes)?` class="${classes}"`:""}\n alt="${alt}"\n src="${picture?srcFallback:src}"\n srcset="${picture?srcsetFallback.join(", "):srcsetSource.join(", ")}"\n sizes="${sizes}"\n width="${w}"\n height="${h}"\n ${(0,string_js_1.isStringStrict)(attr)?` ${attr}`:""}\n ${lazy?' loading="lazy" decoding="async"':' loading="eager" fetchpriority="high"'}\n >\n `;return returnDetails?{output:output,aspectRatio:aspectRatio,naturalWidth:naturalWidth,naturalHeight:naturalHeight,src:src,srcFallback:srcFallback,srcset:srcsetSource,sizes:sizes}:output};exports.getImage=getImage;const getImageClosestSize=(size,sizes=[])=>(sizes.length||(sizes=config_js_1.config.image.sizes),[...sizes].reduce((prev,curr)=>Math.abs(curr-size)<=Math.abs(prev-size)?curr:prev));exports.getImageClosestSize=getImageClosestSize;const getImageMaxWidth=args=>{if(!(0,object_js_1.isObjectStrict)(args))return 0;const{parents:parents,widths:widths,maxWidths:maxWidths,breakpoints:breakpoints,source:source=config_js_1.config.source}=args;if(!((0,array_js_1.isArrayStrict)(parents)&&(0,object_js_1.isObjectStrict)(widths)&&(0,object_js_1.isObjectStrict)(maxWidths)&&(0,array_js_1.isArrayStrict)(breakpoints)))return 0;const w=[];let m=0;if(parents.forEach(parent=>{if(!(0,object_js_1.isObjectStrict)(parent))return;const{renderType:renderType,args:args}=parent;if((0,object_js_1.isObjectStrict)(args)){if("column"===renderType){const{width:width="Default",widthSmall:widthSmall="Default",widthMedium:widthMedium="Default",widthLarge:widthLarge="Default"}=args;w[0]=(0,number_js_1.isNumber)(widths[width])&&widths[width]>0?widths[width]:1,w[1]=(0,number_js_1.isNumber)(widths[widthSmall])&&widths[widthSmall]>0?widths[widthSmall]:w[0],w[2]=(0,number_js_1.isNumber)(widths[widthMedium])&&widths[widthMedium]>0?widths[widthMedium]:w[1],w[3]=(0,number_js_1.isNumber)(widths[widthLarge])&&widths[widthLarge]>0?widths[widthLarge]:w[2]}if("container"===renderType){const{maxWidth:maxWidth="Default"}=args;(0,number_js_1.isNumber)(maxWidths[maxWidth])&&(m=maxWidths[maxWidth])}}}),!w.length&&0===m)return 0;const bk=[...breakpoints],calc=[];let lastW=1;bk.forEach((b,i)=>{const wd=w[i];(0,number_js_1.isNumber)(wd)&&(lastW=wd,m>0&&b>m?calc.push(wd*m):calc.push(wd*b))}),m>0&&calc.push(lastW*m);const res=2*Math.max(...calc);return"local"===source?getImageClosestSize(res):res};exports.getImageMaxWidth=getImageMaxWidth;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getSlug,getPermalink,getLink}from"../../utils/link/link.js";import{isArrayStrict}from"../../utils/array/array.js";import{isObjectStrict}from"../../utils/object/object.js";import{isStringStrict,isString}from"../../utils/string/string.js";import{isFunction}from"../../utils/function/function.js";import{isNumber}from"../../utils/number/number.js";import{normalizeContentType}from"../../utils/contentType/contentType.js";import{getArchiveMeta}from"../../utils/archive/archive.js";class Navigation{navigations=[];items=[];init=!1;#itemsById=new Map;#navigationsByLocation=new Map;constructor(props){this.init=this.#initialize(props)}#initialize(props){if(!isObjectStrict(props))return!1;const{navigations:navigations,items:items}=props;return!(!isArrayStrict(navigations)||!isArrayStrict(items))&&(this.navigations=navigations,this.items=items,this.items.forEach(item=>{isObjectStrict(item)&&this.#itemsById.set(item.id,item)}),this.navigations.forEach(nav=>{if(!isObjectStrict(nav))return;const{title:title,location:location,items:items=[]}=nav,locations=isArrayStrict(location)?location:[location];isStringStrict(title)&&isArrayStrict(items)&&locations.forEach(loc=>{this.#navigationsByLocation.set(loc,{title:title,items:items})})}),!0)}#recurseItemChildren(children,output,currentLink="",currentType=[]){let childCurrent=!1;return children.forEach(child=>{const newItem=this.#getItem(this.#itemsById.get(child?.id||""),currentLink,currentType);if(!newItem)return;const{current:current,archiveCurrent:archiveCurrent}=newItem;(current||archiveCurrent)&&(childCurrent=!0),output.push(newItem)}),childCurrent}#getItem(item,currentLink,currentType=[]){if(!isObjectStrict(item))return;const{id:id,title:title,link:link,internalLink:internalLink,externalLink:externalLink,children:children}=item;let external=!1;isStringStrict(externalLink)&&(external=!0);const newLink=isStringStrict(link)?link:getLink(internalLink,externalLink),newItem={...item,id:id,title:title,link:newLink,external:external};newLink&&!external&&(newItem.current=newLink===currentLink);const internalId=internalLink?.id;if(isStringStrict(internalId)){let archiveCurrent=!1;for(const type of currentType){if(internalId===getArchiveMeta(type,internalLink?.locale).id){archiveCurrent=!0;break}}newItem.archiveCurrent=archiveCurrent}let descendentCurrent=!1;if(isArrayStrict(children)){const newChildren=[];descendentCurrent=this.#recurseItemChildren(children,newChildren,currentLink,currentType),newItem.children=newChildren}return descendentCurrent&&(newItem.descendentCurrent=descendentCurrent),newItem}#getItems(items=[],currentLink,currentType){const resItems=[];return items.forEach(item=>{const newItem=this.#getItem(this.#itemsById.get(item?.id||""),currentLink,currentType);newItem&&resItems.push(newItem)}),resItems}#recurseOutput=(items,output,args,depth=0,maxDepth)=>{if(isNumber(maxDepth)&&depth>maxDepth)return;const dataAttr=isStringStrict(args.dataAttr)?args.dataAttr:"data-nav",depthAttr=args.depthAttr?` ${dataAttr}-depth="${depth}"`:"",listFilterArgs={args:args,output:output,items:items,depth:depth};isFunction(args.filterBeforeList)&&args.filterBeforeList(listFilterArgs);const listClasses=isStringStrict(args.listClass)?` class="${args.listClass}"`:"",listAttrs=isStringStrict(args.listAttr)?` ${args.listAttr}`:"",listTag=isStringStrict(args.listTag)?args.listTag:"ul";output.ref+=`<${listTag}${depthAttr}${listClasses}${listAttrs}>`,items.forEach((item,index)=>{const
|
|
1
|
+
import{getSlug,getPermalink,getLink}from"../../utils/link/link.js";import{isArrayStrict}from"../../utils/array/array.js";import{isObjectStrict}from"../../utils/object/object.js";import{isStringStrict,isString}from"../../utils/string/string.js";import{isFunction}from"../../utils/function/function.js";import{isNumber}from"../../utils/number/number.js";import{normalizeContentType}from"../../utils/contentType/contentType.js";import{getArchiveMeta}from"../../utils/archive/archive.js";class Navigation{navigations=[];items=[];init=!1;#itemsById=new Map;#navigationsByLocation=new Map;constructor(props){this.init=this.#initialize(props)}#initialize(props){if(!isObjectStrict(props))return!1;const{navigations:navigations,items:items}=props;return!(!isArrayStrict(navigations)||!isArrayStrict(items))&&(this.navigations=navigations,this.items=items,this.items.forEach(item=>{isObjectStrict(item)&&this.#itemsById.set(item.id,item)}),this.navigations.forEach(nav=>{if(!isObjectStrict(nav))return;const{title:title,location:location,items:items=[]}=nav,locations=isArrayStrict(location)?location:[location];isStringStrict(title)&&isArrayStrict(items)&&locations.forEach(loc=>{this.#navigationsByLocation.set(loc,{title:title,items:items})})}),!0)}#recurseItemChildren(children,output,currentLink="",currentType=[]){let childCurrent=!1;return children.forEach(child=>{const newItem=this.#getItem(this.#itemsById.get(child?.id||""),currentLink,currentType);if(!newItem)return;const{current:current,archiveCurrent:archiveCurrent}=newItem;(current||archiveCurrent)&&(childCurrent=!0),output.push(newItem)}),childCurrent}#getItem(item,currentLink,currentType=[]){if(!isObjectStrict(item))return;const{id:id,title:title,link:link,internalLink:internalLink,externalLink:externalLink,children:children}=item;let external=!1;isStringStrict(externalLink)&&(external=!0);const newLink=isStringStrict(link)?link:getLink(internalLink,externalLink),newItem={...item,id:id,title:title,link:newLink,external:external};newLink&&!external&&(newItem.current=newLink===currentLink);const internalId=internalLink?.id;if(isStringStrict(internalId)){let archiveCurrent=!1;for(const type of currentType){if(internalId===getArchiveMeta(type,internalLink?.locale).id){archiveCurrent=!0;break}}newItem.archiveCurrent=archiveCurrent}let descendentCurrent=!1;if(isArrayStrict(children)){const newChildren=[];descendentCurrent=this.#recurseItemChildren(children,newChildren,currentLink,currentType),newItem.children=newChildren}return descendentCurrent&&(newItem.descendentCurrent=descendentCurrent),newItem}#getItems(items=[],currentLink,currentType){const resItems=[];return items.forEach(item=>{const newItem=this.#getItem(this.#itemsById.get(item?.id||""),currentLink,currentType);newItem&&resItems.push(newItem)}),resItems}#recurseOutput=(items,output,args,depth=0,maxDepth)=>{if(isNumber(maxDepth)&&depth>maxDepth)return;const dataAttr=isStringStrict(args.dataAttr)?args.dataAttr:"data-nav",depthAttr=args.depthAttr?` ${dataAttr}-depth="${depth}"`:"",listFilterArgs={args:args,output:output,items:items,depth:depth};isFunction(args.filterBeforeList)&&args.filterBeforeList(listFilterArgs);const listClasses=isStringStrict(args.listClass)?` class="${args.listClass}"`:"",listAttrs=isStringStrict(args.listAttr)?` ${args.listAttr}`:"",listTag=isStringStrict(args.listTag)?args.listTag:"ul";output.ref+=`<${listTag}${depthAttr}${listClasses}${listAttrs}>`,items.forEach((item,index)=>{const filterArgs={args:args,item:item,output:output,index:index,items:items,depth:depth};isFunction(args.filterBeforeItem)&&args.filterBeforeItem(filterArgs);const{title:title,link:link,external:external=!1,children:children,current:current=!1,descendentCurrent:descendentCurrent=!1,archiveCurrent:archiveCurrent=!1}=item,itemClasses=isStringStrict(args.itemClass)?` class="${args.itemClass}"`:"",itemTag=isStringStrict(args.itemTag)?args.itemTag:"li";let itemAttrs=isStringStrict(args.itemAttr)?` ${args.itemAttr}`:"";current&&(itemAttrs+=` ${dataAttr}-current`),descendentCurrent&&(itemAttrs+=` ${dataAttr}-descendent-current`),archiveCurrent&&(itemAttrs+=` ${dataAttr}-archive-current`),output.ref+=`<${itemTag}${depthAttr}${itemClasses}${itemAttrs}>`,isFunction(args.filterBeforeLink)&&args.filterBeforeLink(filterArgs);const linkClassesArr=[];isStringStrict(args.linkClass)&&linkClassesArr.push(args.linkClass),!external&&isStringStrict(args.internalLinkClass)&&linkClassesArr.push(args.internalLinkClass);const hasLink=isStringStrict(link),linkClasses=linkClassesArr.length?` class="${linkClassesArr.join(" ")}"`:"",linkAttrsArr=[hasLink?`href="${link}"`:'type="button"'];isStringStrict(args.linkAttr)&&linkAttrsArr.push(args.linkAttr),current&&(linkAttrsArr.push(`${dataAttr}-current`),hasLink&&linkAttrsArr.push('aria-current="page"')),descendentCurrent&&linkAttrsArr.push(`${dataAttr}-descendent-current`),archiveCurrent&&linkAttrsArr.push(`${dataAttr}-archive-current`);const linkAttrs=` ${linkAttrsArr.join(" ")}`,linkTag=hasLink?"a":"button";output.ref+=`<${linkTag}${depthAttr}${linkClasses}${linkAttrs}>`,isFunction(args.filterBeforeLinkText)&&args.filterBeforeLinkText(filterArgs),output.ref+=title,isFunction(args.filterAfterLinkText)&&args.filterAfterLinkText(filterArgs),output.ref+=`</${linkTag}>`,isFunction(args.filterAfterLink)&&args.filterAfterLink(filterArgs),isArrayStrict(children)&&this.#recurseOutput(children,output,args,depth+1,maxDepth),output.ref+=`</${itemTag}>`,isFunction(args.filterAfterItem)&&args.filterAfterItem(filterArgs)}),output.ref+=`</${listTag}>`,isFunction(args.filterAfterList)&&args.filterAfterList(listFilterArgs)};getOutput(location,args,maxDepth){const nav=this.#navigationsByLocation.get(location);if(!nav)return"";args=Object.assign({depthAttr:!1},args||{});const items=nav.items,{currentLink:currentLink,currentType:currentType}=args,currLink=isStringStrict(currentLink)?currentLink:"",currType=(isArrayStrict(currentType)?currentType:[currentType]).map(type=>normalizeContentType(type)),normalizedItems=this.#getItems(items,currLink,currType);if(!normalizedItems.length)return"";const output={ref:""};return this.#recurseOutput(normalizedItems,output,args,0,maxDepth),output.ref}getBreadcrumbs(items,args){if(!isArrayStrict(items))return"";const current=(args=Object.assign({a11yClass:"a-hide-vis"},args||{})).current,dataAttr=isStringStrict(args.dataAttr)?args.dataAttr:"data-nav",listClasses=isStringStrict(args.listClass)?` class="${args.listClass}"`:"",listAttrs=isStringStrict(args.listAttr)?` ${args.listAttr}`:"",filteredItems=items.filter(item=>!!isStringStrict(item.title)&&(!!isString(item.slug)&&(item.slug=getSlug({id:item.id,slug:item.slug,contentType:item.contentType,itemData:item.internalLink}),!0))),itemClasses=isStringStrict(args.itemClass)?` class="${args.itemClass}"`:"",itemAttrs=isStringStrict(args.itemAttr)?` ${args.itemAttr}`:"",lastItemIndex=filteredItems.length-1,itemsArr=filteredItems.map((item,index)=>{const{title:title,slug:slug}=item,link=getPermalink(slug),output={ref:""},lastLevel=lastItemIndex===index,filterArgs={output:output,lastLevel:lastLevel};output.ref+=`<li${itemClasses}${itemAttrs}${lastLevel?` ${dataAttr}-last`:""}>`,isFunction(args.filterBeforeLink)&&args.filterBeforeLink(filterArgs);const linkClassesArr=[];isStringStrict(args.linkClass)&&linkClassesArr.push(args.linkClass),isStringStrict(args.internalLinkClass)&&linkClassesArr.push(args.internalLinkClass);const linkClasses=linkClassesArr.length?` class="${linkClassesArr.join(" ")}"`:"",linkAttrs=isStringStrict(args.linkAttr)?` ${args.linkAttr}`:"";return output.ref+=`<a${linkClasses} href="${link}"${linkAttrs}>${title}</a>`,isFunction(args.filterAfterLink)&&args.filterAfterLink(filterArgs),output.ref+="</li>",output.ref}),currentLabel=isStringStrict(args.currentLabel)?args.currentLabel:"(current page)",currentClasses=isStringStrict(args.currentClass)?` class="${args.currentClass}"`:"",a11yClasses=isStringStrict(args.a11yClass)?` class="${args.a11yClass}"`:"",currentItem=isStringStrict(current)?`\n <li${itemClasses}${itemAttrs} ${dataAttr}-current>\n <span${currentClasses}>${current}<span${a11yClasses}> ${currentLabel}</span></span>\n </li>\n `:"";return`\n <ol${listClasses}${listAttrs}>\n ${itemsArr.join("")}\n ${currentItem}\n </ol>\n `}getItemsById(){return this.#itemsById}getNavigationsByLocation(){return this.#navigationsByLocation}getNavigationByLocation(location){return this.#navigationsByLocation.get(location)}}export{Navigation};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{v4 as uuid}from"uuid";import{applyFilters}from"../../utils/filter/filter.js";import{isStringStrict}from"../../utils/string/string.js";import{isObjectStrict}from"../../utils/object/object.js";const FormField=props=>{if(!isObjectStrict(props))return[];if(props=applyFilters("formFieldProps",props),!isObjectStrict(props))return[];const{args:args}=props,{type:type="text",name:name,label:label,hint:hint,value:value,required:required=!1,attr:attr,emptyError:emptyError,invalidError:invalidError,fieldClasses:fieldClasses,fieldAttr:fieldAttr,fieldsetClasses:fieldsetClasses,fieldsetAttr:fieldsetAttr,labelClasses:labelClasses,classes:classes,radioIcon:radioIcon,checkboxIcon:checkboxIcon}=isObjectStrict(args)?args:{},isHidden="hidden"===type;if(!isStringStrict(label)&&!isHidden)return[];let isFieldset="fieldset"===type;if(!isFieldset&&!isStringStrict(name))return[];const isOption="checkbox"===type||"radio"===type,isSelect="select"===type;("checkbox-group"===type||"radio-group"===type)&&(isFieldset=!0);const isTextarea="textarea"===type,hasValue=isStringStrict(value),id=uuid();let optionIcon="",selectIcon="";"radio"===type&&isStringStrict(radioIcon)&&(optionIcon=radioIcon),"checkbox"===type&&isStringStrict(checkboxIcon)&&(optionIcon=checkboxIcon),"select"===type&&isStringStrict(args.selectIcon)&&(selectIcon=args.selectIcon);const requiredIcon=required&&isStringStrict(args.requiredIcon)?args.requiredIcon:"",fieldAttrs=["data-form-field"],fieldsetAttrs=[],attrs=["data-form-input"];if(isStringStrict(attr)){const attrsArr=attr.split("\n");for(const attrItem of attrsArr){const[attrName,attrValue]=attrItem.split(" : ");isStringStrict(attrName)&&isStringStrict(attrValue)&&attrs.push(`${attrName}="${attrValue}"`)}}isTextarea||isSelect||!hasValue||attrs.push(`value="${value}"`),isStringStrict(classes)&&attrs.push(`class="${classes}"`),isStringStrict(fieldClasses)&&fieldAttrs.push(`class="${fieldClasses}"`),isStringStrict(fieldsetClasses)&&fieldsetAttrs.push(`class="${fieldsetClasses}"`),isStringStrict(fieldAttr)&&fieldAttrs.push(fieldAttr),isStringStrict(fieldsetAttr)&&fieldsetAttrs.push(fieldsetAttr),isStringStrict(emptyError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-empty="${emptyError}"`),isStringStrict(invalidError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-invalid="${invalidError}"`),required&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(isFieldset?"data-form-required":"required");const fieldAtts=` ${fieldAttrs.join(" ")}`,fieldsetAtts=fieldsetAttrs.length?` ${fieldsetAttrs.join(" ")}`:"",atts=` ${attrs.join(" ")}`;let hintOutput="";isStringStrict(hint)&&(hintOutput=`<small data-form-hint>${hint}</small>`);let labelBefore="",labelAfter="";const labelClass=isStringStrict(labelClasses)?` class="${labelClasses}"`:"";isFieldset?labelBefore=`\n <legend id="${uuid()}"${labelClass}>\n <span data-form-legend>\n <span data-form-legend-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </legend>\n `:isOption?labelAfter=`\n <label for="${id}"${labelClass}>\n <span data-form-option>\n ${optionIcon}\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n </span>\n ${hintOutput}\n </label>\n `:labelBefore=`\n <label for="${id}"${labelClass}>\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </label>\n `;let beforeOutput="",afterOutput="";switch(type){case"text":case"email":case"checkbox":case"radio":case"number":case"password":case"tel":case"url":case"hidden":beforeOutput=`<input type="${type}" name="${name}" id="${id}"${atts}>`;break;case"textarea":beforeOutput=`<textarea name="${name}" id="${id}"${atts}>${hasValue?value:""}</textarea>`;break;case"select":beforeOutput=`\n <div data-form-select>\n <select name="${name}" id="${id}"${atts}>`,afterOutput=`\n </select>\n ${selectIcon}\n </div>`}return isHidden?[beforeOutput]:[`\n
|
|
1
|
+
import{v4 as uuid}from"uuid";import{applyFilters}from"../../utils/filter/filter.js";import{isStringStrict}from"../../utils/string/string.js";import{isObjectStrict}from"../../utils/object/object.js";const FormField=props=>{if(!isObjectStrict(props))return[];if(props=applyFilters("formFieldProps",props),!isObjectStrict(props))return[];const{args:args}=props,{type:type="text",name:name,label:label,hint:hint,value:value,required:required=!1,attr:attr,emptyError:emptyError,invalidError:invalidError,fieldTag:fieldTag="div",fieldClasses:fieldClasses,fieldAttr:fieldAttr,fieldsetClasses:fieldsetClasses,fieldsetAttr:fieldsetAttr,labelClasses:labelClasses,classes:classes,radioIcon:radioIcon,checkboxIcon:checkboxIcon}=isObjectStrict(args)?args:{},isHidden="hidden"===type;if(!isStringStrict(label)&&!isHidden)return[];let isFieldset="fieldset"===type;if(!isFieldset&&!isStringStrict(name))return[];const isOption="checkbox"===type||"radio"===type,isSelect="select"===type;("checkbox-group"===type||"radio-group"===type)&&(isFieldset=!0);const isTextarea="textarea"===type,hasValue=isStringStrict(value),id=uuid();let optionIcon="",selectIcon="";"radio"===type&&isStringStrict(radioIcon)&&(optionIcon=radioIcon),"checkbox"===type&&isStringStrict(checkboxIcon)&&(optionIcon=checkboxIcon),"select"===type&&isStringStrict(args.selectIcon)&&(selectIcon=args.selectIcon);const requiredIcon=required&&isStringStrict(args.requiredIcon)?args.requiredIcon:"",fieldAttrs=["data-form-field"],fieldsetAttrs=[],attrs=["data-form-input"];if(isStringStrict(attr)){const attrsArr=attr.split("\n");for(const attrItem of attrsArr){const[attrName,attrValue]=attrItem.split(" : ");isStringStrict(attrName)&&isStringStrict(attrValue)&&attrs.push(`${attrName}="${attrValue}"`)}}isTextarea||isSelect||!hasValue||attrs.push(`value="${value}"`),isStringStrict(classes)&&attrs.push(`class="${classes}"`),isStringStrict(fieldClasses)&&fieldAttrs.push(`class="${fieldClasses}"`),isStringStrict(fieldsetClasses)&&fieldsetAttrs.push(`class="${fieldsetClasses}"`),isStringStrict(fieldAttr)&&fieldAttrs.push(fieldAttr),isStringStrict(fieldsetAttr)&&fieldsetAttrs.push(fieldsetAttr),isStringStrict(emptyError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-empty="${emptyError}"`),isStringStrict(invalidError)&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(`data-form-invalid="${invalidError}"`),required&&!isHidden&&(isFieldset?fieldsetAttrs:attrs).push(isFieldset?"data-form-required":"required");const fieldAtts=` ${fieldAttrs.join(" ")}`,fieldsetAtts=fieldsetAttrs.length?` ${fieldsetAttrs.join(" ")}`:"",atts=` ${attrs.join(" ")}`;let hintOutput="";isStringStrict(hint)&&(hintOutput=`<small data-form-hint>${hint}</small>`);let labelBefore="",labelAfter="";const labelClass=isStringStrict(labelClasses)?` class="${labelClasses}"`:"";isFieldset?labelBefore=`\n <legend id="${uuid()}"${labelClass}>\n <span data-form-legend>\n <span data-form-legend-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </legend>\n `:isOption?labelAfter=`\n <label for="${id}"${labelClass}>\n <span data-form-option>\n ${optionIcon}\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n </span>\n ${hintOutput}\n </label>\n `:labelBefore=`\n <label for="${id}"${labelClass}>\n <span data-form-label>\n <span data-form-label-text>${label}</span>\n ${requiredIcon}\n </span>\n ${hintOutput}\n </label>\n `;let beforeOutput="",afterOutput="";switch(type){case"text":case"email":case"checkbox":case"radio":case"number":case"password":case"tel":case"url":case"hidden":beforeOutput=`<input type="${type}" name="${name}" id="${id}"${atts}>`;break;case"textarea":beforeOutput=`<textarea name="${name}" id="${id}"${atts}>${hasValue?value:""}</textarea>`;break;case"select":beforeOutput=`\n <div data-form-select>\n <select name="${name}" id="${id}"${atts}>`,afterOutput=`\n </select>\n ${selectIcon}\n </div>`}return isHidden?[beforeOutput]:[`\n <${fieldTag}${fieldAtts}>\n ${isFieldset?`<fieldset${fieldsetAtts}>`:""}\n ${labelBefore}\n ${isFieldset?"<div data-form-group>":""}\n ${beforeOutput}`,`${afterOutput}\n ${labelAfter}\n ${isFieldset?"</div></fieldset>":""}\n </${fieldTag}>\n `]};export{FormField};
|
package/lib/utils/image/image.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{config}from"../../config/config.js";import{isString,isStringStrict}from"../string/string.js";import{isArrayStrict}from"../array/array.js";import{isNumber}from"../number/number.js";import{isObjectStrict}from"../object/object.js";import{dataSource}from"../dataSource/dataSource.js";const getImageUrl=(url,format,quality,width,height,params)=>{const urlObj=new URL(url);return Object.entries(params).forEach(([key,value])=>{let val=value;"%format"===val&&(val=format),"%quality"===val&&(val=`${quality}`),"%width"===val&&(val=`${width}`),"%height"===val&&(val=`${height}`),urlObj.searchParams.set(key,val)}),urlObj.toString()},getImage=(args,returnDetails=!1)=>{const{data:data,classes:classes,attr:attr,alt:imageAlt="inherit",width:width="auto",height:height="auto",lazy:lazy=!0,picture:picture=!1,quality:quality=75,source:source=config.source,maxWidth:maxWidth=1200,viewportWidth:viewportWidth=100,format:format="webp",params:params={fm:"%format",q:"%quality",w:"%width",h:"%height"}}=isObjectStrict(args)?args:{},fallback=returnDetails?{output:"",aspectRatio:0,naturalWidth:0,naturalHeight:0,src:"",srcFallback:"",srcset:[],sizes:""}:"";if(!isObjectStrict(data))return fallback;const{path:path="",alt:dataAlt="",width:naturalWidth=1,height:naturalHeight=1,format:naturalFormat="jpg",sizes:dataSizes}=data;let{url:url=config.image.remoteUrl}=data,alt="inherit"===imageAlt?dataAlt:imageAlt;isStringStrict(alt)||(alt="");const isLocal="local"===source,isRemote=dataSource.isContentful(source)||"remote"===source,isWordpress=dataSource.isWordPress(source);if(isLocal){if(!isStringStrict(path))return fallback;url=`${config.image.localUrl}/${path}`}const aspectRatio=naturalHeight/naturalWidth,aspectRatioReverse=naturalWidth/naturalHeight;let w=naturalWidth,h=naturalHeight;if(isNumber(width)&&(w=width,h=isString(height)?w*aspectRatio:height),isNumber(height)&&(h=height,w=isString(width)?h*aspectRatioReverse:width),w>maxWidth&&(w=Math.round(maxWidth),h=Math.round(w*aspectRatio)),isWordpress&&dataSizes&&!dataSizes[w]){const isNatural=w===naturalWidth;isNatural||(w=getImageClosestSize(w,Object.keys(dataSizes).map(s=>parseInt(s,10))),h=Math.round(w*aspectRatio)),isNatural&&(dataSizes[w]=url)}let src=url,srcFallback=url;isLocal&&(src=`${url}.${format}`,srcFallback=`${url}.${naturalFormat}`),isRemote&&(src=getImageUrl(url,format,quality,w,h,params),srcFallback=getImageUrl(url,naturalFormat,quality,w,h,params));const sizes=`(min-width: ${w/16}rem) ${w/16}rem, ${viewportWidth}vw`,srcsetFallback=[];let srcset=[...config.image.sizes];srcset.includes(w)||srcset.push(w),srcset=srcset.filter(s=>s<=w),srcset.sort((a,b)=>a-b);const srcsetSource=[];srcset.forEach(s=>{if(isLocal){const base=`${url}${s!==naturalWidth?`@${s}`:""}`;srcsetFallback.push(`${base}.${naturalFormat} ${s}w`),srcsetSource.push(`${base}.${format} ${s}w`)}if(isRemote){const sizeHeight=Math.round(s*aspectRatio);srcsetFallback.push(getImageUrl(url,naturalFormat,quality,s,sizeHeight,params)+` ${s}w`),srcsetSource.push(getImageUrl(url,format,quality,s,sizeHeight,params)+` ${s}w`)}if(isWordpress){const sizeUrl=dataSizes?.[s];isStringStrict(sizeUrl)&&srcsetSource.push(`${sizeUrl} ${s}w`)}});let sourceOutput="";picture&&(sourceOutput=`<source srcset="${srcsetSource.join(", ")}" sizes="${sizes}" type="image/${format}">`);let eagerHackOutput="";lazy||(eagerHackOutput=`\n <img\n alt=""\n role="presentation"\n aria-hidden="true"\n src="data:image/svg+xml;charset=utf-8,%3Csvg height='${h}' width='${w}' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E" style="pointerEvents: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%"\n >\n `);const output=`\n ${eagerHackOutput}\n ${sourceOutput}\n <img\n ${isStringStrict(classes)?` class="${classes}"`:""}\n alt="${alt}"\n src="${picture?srcFallback:src}"\n srcset="${picture?srcsetFallback.join(", "):srcsetSource.join(", ")}"\n sizes="${sizes}"\n width="${w}"\n height="${h}"\n ${isStringStrict(attr)?` ${attr}`:""}\n ${lazy?' loading="lazy" decoding="async"':' loading="eager" fetchpriority="high"'}\n >\n `;return returnDetails?{output:output,aspectRatio:aspectRatio,naturalWidth:naturalWidth,naturalHeight:naturalHeight,src:src,srcFallback:srcFallback,srcset:srcsetSource,sizes:sizes}:output},getImageClosestSize=(size,sizes=[])=>(sizes.length||(sizes=config.image.sizes),[...sizes].reduce((prev,curr)=>Math.abs(curr-size)<=Math.abs(prev-size)?curr:prev)),getImageMaxWidth=args=>{if(!isObjectStrict(args))return 0;const{parents:parents,widths:widths,maxWidths:maxWidths,breakpoints:breakpoints,source:source=config.source}=args;if(!(isArrayStrict(parents)&&isObjectStrict(widths)&&isObjectStrict(maxWidths)&&isArrayStrict(breakpoints)))return 0;const w=[];let m=0;if(parents.forEach(parent=>{if(!isObjectStrict(parent))return;const{renderType:renderType,args:args}=parent;if(isObjectStrict(args)){if("column"===renderType){const{width:width="Default",widthSmall:widthSmall="Default",widthMedium:widthMedium="Default",widthLarge:widthLarge="Default"}=args;w[0]=isNumber(widths[width])&&widths[width]>0?widths[width]:1,w[1]=isNumber(widths[widthSmall])&&widths[widthSmall]>0?widths[widthSmall]:w[0],w[2]=isNumber(widths[widthMedium])&&widths[widthMedium]>0?widths[widthMedium]:w[1],w[3]=isNumber(widths[widthLarge])&&widths[widthLarge]>0?widths[widthLarge]:w[2]}if("container"===renderType){const{maxWidth:maxWidth="Default"}=args;
|
|
1
|
+
import{config}from"../../config/config.js";import{isString,isStringStrict}from"../string/string.js";import{isArrayStrict}from"../array/array.js";import{isNumber}from"../number/number.js";import{isObjectStrict}from"../object/object.js";import{dataSource}from"../dataSource/dataSource.js";const getImageUrl=(url,format,quality,width,height,params)=>{const urlObj=new URL(url);return Object.entries(params).forEach(([key,value])=>{let val=value;"%format"===val&&(val=format),"%quality"===val&&(val=`${quality}`),"%width"===val&&(val=`${width}`),"%height"===val&&(val=`${height}`),urlObj.searchParams.set(key,val)}),urlObj.toString()},getImage=(args,returnDetails=!1)=>{const{data:data,classes:classes,attr:attr,alt:imageAlt="inherit",width:width="auto",height:height="auto",lazy:lazy=!0,picture:picture=!1,quality:quality=75,source:source=config.source,maxWidth:maxWidth=1200,viewportWidth:viewportWidth=100,format:format="webp",params:params={fm:"%format",q:"%quality",w:"%width",h:"%height"}}=isObjectStrict(args)?args:{},fallback=returnDetails?{output:"",aspectRatio:0,naturalWidth:0,naturalHeight:0,src:"",srcFallback:"",srcset:[],sizes:""}:"";if(!isObjectStrict(data))return fallback;const{path:path="",alt:dataAlt="",width:naturalWidth=1,height:naturalHeight=1,format:naturalFormat="jpg",sizes:dataSizes}=data;let{url:url=config.image.remoteUrl}=data,alt="inherit"===imageAlt?dataAlt:imageAlt;isStringStrict(alt)||(alt="");const isLocal="local"===source,isRemote=dataSource.isContentful(source)||"remote"===source,isWordpress=dataSource.isWordPress(source);if(isLocal){if(!isStringStrict(path))return fallback;url=`${config.image.localUrl}/${path}`}const aspectRatio=naturalHeight/naturalWidth,aspectRatioReverse=naturalWidth/naturalHeight;let w=naturalWidth,h=naturalHeight;if(isNumber(width)&&(w=width,h=isString(height)?w*aspectRatio:height),isNumber(height)&&(h=height,w=isString(width)?h*aspectRatioReverse:width),w>maxWidth&&(w=Math.round(maxWidth),h=Math.round(w*aspectRatio)),isWordpress&&dataSizes&&!dataSizes[w]){const isNatural=w===naturalWidth;isNatural||(w=getImageClosestSize(w,Object.keys(dataSizes).map(s=>parseInt(s,10))),h=Math.round(w*aspectRatio)),isNatural&&(dataSizes[w]=url)}let src=url,srcFallback=url;isLocal&&(src=`${url}.${format}`,srcFallback=`${url}.${naturalFormat}`),isRemote&&(src=getImageUrl(url,format,quality,w,h,params),srcFallback=getImageUrl(url,naturalFormat,quality,w,h,params));const sizes=`(min-width: ${w/16}rem) ${w/16}rem, ${viewportWidth}vw`,srcsetFallback=[];let srcset=[...config.image.sizes];srcset.includes(w)||srcset.push(w),srcset=srcset.filter(s=>s<=w),srcset.sort((a,b)=>a-b);const srcsetSource=[];srcset.forEach(s=>{if(isLocal){const base=`${url}${s!==naturalWidth?`@${s}`:""}`;srcsetFallback.push(`${base}.${naturalFormat} ${s}w`),srcsetSource.push(`${base}.${format} ${s}w`)}if(isRemote){const sizeHeight=Math.round(s*aspectRatio);srcsetFallback.push(getImageUrl(url,naturalFormat,quality,s,sizeHeight,params)+` ${s}w`),srcsetSource.push(getImageUrl(url,format,quality,s,sizeHeight,params)+` ${s}w`)}if(isWordpress){const sizeUrl=dataSizes?.[s];isStringStrict(sizeUrl)&&srcsetSource.push(`${sizeUrl} ${s}w`)}});let sourceOutput="";picture&&(sourceOutput=`<source srcset="${srcsetSource.join(", ")}" sizes="${sizes}" type="image/${format}">`);let eagerHackOutput="";lazy||(eagerHackOutput=`\n <img\n alt=""\n role="presentation"\n aria-hidden="true"\n src="data:image/svg+xml;charset=utf-8,%3Csvg height='${h}' width='${w}' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E" style="pointerEvents: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%"\n >\n `);const output=`\n ${eagerHackOutput}\n ${sourceOutput}\n <img\n ${isStringStrict(classes)?` class="${classes}"`:""}\n alt="${alt}"\n src="${picture?srcFallback:src}"\n srcset="${picture?srcsetFallback.join(", "):srcsetSource.join(", ")}"\n sizes="${sizes}"\n width="${w}"\n height="${h}"\n ${isStringStrict(attr)?` ${attr}`:""}\n ${lazy?' loading="lazy" decoding="async"':' loading="eager" fetchpriority="high"'}\n >\n `;return returnDetails?{output:output,aspectRatio:aspectRatio,naturalWidth:naturalWidth,naturalHeight:naturalHeight,src:src,srcFallback:srcFallback,srcset:srcsetSource,sizes:sizes}:output},getImageClosestSize=(size,sizes=[])=>(sizes.length||(sizes=config.image.sizes),[...sizes].reduce((prev,curr)=>Math.abs(curr-size)<=Math.abs(prev-size)?curr:prev)),getImageMaxWidth=args=>{if(!isObjectStrict(args))return 0;const{parents:parents,widths:widths,maxWidths:maxWidths,breakpoints:breakpoints,source:source=config.source}=args;if(!(isArrayStrict(parents)&&isObjectStrict(widths)&&isObjectStrict(maxWidths)&&isArrayStrict(breakpoints)))return 0;const w=[];let m=0;if(parents.forEach(parent=>{if(!isObjectStrict(parent))return;const{renderType:renderType,args:args}=parent;if(isObjectStrict(args)){if("column"===renderType){const{width:width="Default",widthSmall:widthSmall="Default",widthMedium:widthMedium="Default",widthLarge:widthLarge="Default"}=args;w[0]=isNumber(widths[width])&&widths[width]>0?widths[width]:1,w[1]=isNumber(widths[widthSmall])&&widths[widthSmall]>0?widths[widthSmall]:w[0],w[2]=isNumber(widths[widthMedium])&&widths[widthMedium]>0?widths[widthMedium]:w[1],w[3]=isNumber(widths[widthLarge])&&widths[widthLarge]>0?widths[widthLarge]:w[2]}if("container"===renderType){const{maxWidth:maxWidth="Default"}=args;isNumber(maxWidths[maxWidth])&&(m=maxWidths[maxWidth])}}}),!w.length&&0===m)return 0;const bk=[...breakpoints],calc=[];let lastW=1;bk.forEach((b,i)=>{const wd=w[i];isNumber(wd)&&(lastW=wd,m>0&&b>m?calc.push(wd*m):calc.push(wd*b))}),m>0&&calc.push(lastW*m);const res=2*Math.max(...calc);return"local"===source?getImageClosestSize(res):res};export{getImage,getImageClosestSize,getImageMaxWidth};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanizcreative/formation-static",
|
|
3
|
-
"version": "1.3.0-beta.
|
|
3
|
+
"version": "1.3.0-beta.50",
|
|
4
4
|
"description": "Foundation and utilities to build static sites using local JSON files, Contentful, or WordPress",
|
|
5
5
|
"main": "./lib/config/config.js",
|
|
6
6
|
"packageManager": "pnpm@10.15.1",
|