{"version":3,"file":"LocalStorageField.CVEcs5R5.mjs","names":["useState","useMemo"],"sources":["https:/framerusercontent.com/modules/sdQeg8vkXajaH9JzPMnD/Mlh6iF4ooEWRjcuU6aMP/LocalStorageField.js"],"sourcesContent":["import{jsx as _jsx}from\"react/jsx-runtime\";import{addPropertyControls,ControlType,RenderTarget}from\"framer\";import{useEffect,useState,useMemo}from\"react\";/**\n * @framerSupportedLayoutWidth any-prefer-fixed\n * @framerSupportedLayoutHeight any\n * @framerIntrinsicWidth 200\n * @framerIntrinsicHeight 40\n */function LocalStorageField(props){const{templateText,fallbackText,nameTransform,emailTransform,font,fontColor,padding,textAlign,wrapText}=props;const[storageValues,setStorageValues]=useState({});const isCanvas=RenderTarget.current()===RenderTarget.canvas;// Find localStorage key that contains the search term\nconst findMatchingKey=searchTerm=>{if(!searchTerm||typeof window===\"undefined\"||!window.localStorage)return null;const keys=Object.keys(window.localStorage);const lowerSearchTerm=searchTerm.toLowerCase();// Find the first key that contains the search term (case-insensitive)\nreturn keys.find(key=>key.toLowerCase().includes(lowerSearchTerm));};// Clean value by removing quotes and parsing JSON if needed\nconst cleanValue=value=>{if(!value)return\"\";// Try to parse as JSON first (for objects/arrays)\ntry{const parsed=JSON.parse(value);// If it's an object or array, return the parsed value\nif(typeof parsed===\"object\"&&parsed!==null){return parsed;}}catch(e){// Not valid JSON, continue with string cleaning\n}// Remove leading and trailing quotes (both single and double)\nreturn value.replace(/^[\"']|[\"']$/g,\"\");};// Helper function to get nested property value\nconst getNestedProperty=(obj,path)=>{if(!obj||typeof obj!==\"object\")return null;const keys=path.split(\".\");let current=obj;for(const key of keys){if(current[key]===undefined)return null;current=current[key];}return current;};// Load all localStorage values\nconst loadStorageValues=()=>{if(typeof window===\"undefined\"||!window.localStorage)return{};const values={};// Common field patterns to look for\nconst fieldPatterns=[\"firstName\",\"lastname\",\"email\",\"phone\",\"company\",\"city\",\"country\"];fieldPatterns.forEach(pattern=>{const matchingKey=findMatchingKey(pattern);if(matchingKey){const value=window.localStorage.getItem(matchingKey);values[pattern.toLowerCase()]=cleanValue(value)||\"\";}});// Also check for any other variables in the template\nconst variableMatches=templateText?.match(/\\{([^}]+)\\}/g)||[];variableMatches.forEach(match=>{const varName=match.slice(1,-1);const lowerVarName=varName.toLowerCase();// Check if this is a nested property (contains dot notation)\nif(varName.includes(\".\")){const[rootKey,...restPath]=varName.split(\".\");const matchingKey=findMatchingKey(rootKey);if(matchingKey){const value=window.localStorage.getItem(matchingKey);const cleanedValue=cleanValue(value);if(typeof cleanedValue===\"object\"&&cleanedValue!==null){// Store the full path as key for easy access later\nconst nestedValue=getNestedProperty(cleanedValue,restPath.join(\".\"));if(nestedValue!==null&&nestedValue!==undefined){values[varName]=nestedValue;}}}}else if(!values[lowerVarName]){const matchingKey=findMatchingKey(varName);if(matchingKey){const value=window.localStorage.getItem(matchingKey);values[lowerVarName]=cleanValue(value)||\"\";}}});return values;};// Read from localStorage with SSR safety\nuseEffect(()=>{if(isCanvas)return;try{const values=loadStorageValues();setStorageValues(values);}catch(error){console.error(\"Error reading localStorage:\",error);setStorageValues({});}},[templateText,isCanvas]);// Listen for storage events (when localStorage changes in another tab/window)\nuseEffect(()=>{if(isCanvas)return;const handleStorageChange=()=>{// Reload all values when any storage changes\nconst values=loadStorageValues();setStorageValues(values);};window.addEventListener(\"storage\",handleStorageChange);return()=>window.removeEventListener(\"storage\",handleStorageChange);},[templateText,isCanvas]);// Text transformation functions\nconst transformText=(text,transform)=>{if(!text||!transform||transform===\"none\")return text;switch(transform){case\"uppercase\":return text.toUpperCase();case\"lowercase\":return text.toLowerCase();case\"capitalize\":return text.charAt(0).toUpperCase()+text.slice(1).toLowerCase();case\"startCase\":return text.toLowerCase().split(\" \").map(word=>word.charAt(0).toUpperCase()+word.slice(1)).join(\" \");default:return text;}};// Apply transformations to specific field types\nconst applyFieldTransform=(value,fieldName)=>{const lowerFieldName=fieldName.toLowerCase();// Apply name transformations to name fields\nif((lowerFieldName.includes(\"firstname\")||lowerFieldName.includes(\"lastname\")||lowerFieldName.includes(\"name\"))&&nameTransform){return transformText(value,nameTransform);}// Apply email transformations to email fields\nif(lowerFieldName.includes(\"email\")&&emailTransform){return transformText(value,emailTransform);}return value;};// Process template text and replace variables\nconst processTemplate=()=>{if(!templateText)return\"\";let processed=templateText;let hasReplacements=false;// Replace all {variable} patterns\nconst variableMatches=templateText.match(/\\{([^}]+)\\}/g)||[];variableMatches.forEach(match=>{const varName=match.slice(1,-1);const lowerVarName=varName.toLowerCase();// Check both exact match and lowercase for backwards compatibility\nconst value=storageValues[varName]||storageValues[lowerVarName];if(value!==undefined&&value!==null){// Convert non-string values to string\nconst stringValue=typeof value===\"string\"?value:String(value);const transformedValue=applyFieldTransform(stringValue,varName.split(\".\").pop()// Use the last part of the path for field detection\n);processed=processed.replace(match,transformedValue);hasReplacements=true;}});// If no replacements were made and we have a fallback, use it\nif(!hasReplacements&&fallbackText){return fallbackText;}return processed;};// Memoize the display text\nconst displayText=useMemo(()=>{if(isCanvas){return templateText||\"{Your text with {variables}}\";}return processTemplate();},[isCanvas,templateText,fallbackText,storageValues,nameTransform,emailTransform]);// Compute text styles\nconst textStyles=useMemo(()=>{const styles={padding:typeof padding===\"number\"?`${padding}px`:padding,display:\"block\",width:\"100%\",wordBreak:wrapText?\"break-word\":\"normal\",whiteSpace:wrapText?\"normal\":\"nowrap\",overflow:wrapText?\"visible\":\"hidden\",textOverflow:wrapText?\"clip\":\"ellipsis\",color:fontColor,textAlign:textAlign||\"left\"};// Apply all font properties\nif(font){if(font.fontFamily)styles.fontFamily=font.fontFamily;if(font.fontSize!==undefined)styles.fontSize=typeof font.fontSize===\"number\"?`${font.fontSize}px`:font.fontSize;if(font.fontWeight!==undefined)styles.fontWeight=font.fontWeight;if(font.lineHeight!==undefined)styles.lineHeight=font.lineHeight;if(font.letterSpacing!==undefined)styles.letterSpacing=typeof font.letterSpacing===\"number\"?`${font.letterSpacing}px`:font.letterSpacing;// Only use font.textAlign if textAlign prop is not explicitly set\nif(font.textAlign&&!textAlign)styles.textAlign=font.textAlign;}return styles;},[padding,font,fontColor,textAlign,wrapText]);return /*#__PURE__*/_jsx(\"div\",{style:textStyles,children:displayText});}// Export the component\nexport default LocalStorageField;// Property Controls\naddPropertyControls(LocalStorageField,{templateText:{type:ControlType.String,title:\"Template Text\",defaultValue:\"Hello {firstName}!\",placeholder:\"Text with {variables}\",description:\"Use {variableName} or {object.property} for nested values\",displayTextArea:true},fallbackText:{type:ControlType.String,title:\"Fallback Text\",defaultValue:\"\",placeholder:\"Text when no variables found\",description:\"Text to display when no localStorage variables are found\"},nameTransform:{type:ControlType.Enum,title:\"Name Transform\",defaultValue:\"startCase\",options:[\"none\",\"uppercase\",\"lowercase\",\"capitalize\",\"startCase\"],optionTitles:[\"None\",\"UPPERCASE\",\"lowercase\",\"Capitalize\",\"Start Case\"],description:\"Transform for name fields (firstName, lastName, etc.)\"},emailTransform:{type:ControlType.Enum,title:\"Email Transform\",defaultValue:\"lowercase\",options:[\"none\",\"uppercase\",\"lowercase\"],optionTitles:[\"None\",\"UPPERCASE\",\"lowercase\"],description:\"Transform for email fields\"},// Font with Framer's native font picker and extended controls\nfont:{type:\"font\",title:\"Font\",controls:\"extended\",defaultFontType:\"sans-serif\",defaultValue:{fontFamily:\"Inter\",fontSize:16,fontWeight:400,lineHeight:1.5,letterSpacing:0,textAlign:\"left\"}},fontColor:{type:ControlType.Color,title:\"Font Color\",defaultValue:\"#000000\"},textAlign:{type:ControlType.Enum,title:\"Text Align\",defaultValue:\"left\",options:[\"left\",\"center\",\"right\",\"justify\"],optionTitles:[\"Left\",\"Center\",\"Right\",\"Justify\"],description:\"How to align the text horizontally\"},wrapText:{type:ControlType.Boolean,title:\"Wrap Text\",defaultValue:true,description:\"Whether to wrap text to multiple lines\"},// Layout\npadding:{type:ControlType.FusedNumber,title:\"Padding\",defaultValue:0,toggleKey:\"paddingMixed\",toggleTitles:[\"All\",\"Individual\"],valueKeys:[\"paddingTop\",\"paddingRight\",\"paddingBottom\",\"paddingLeft\"],valueLabels:[\"T\",\"R\",\"B\",\"L\"],min:0}});\nexport const __FramerMetadata__ = {\"exports\":{\"default\":{\"type\":\"reactComponent\",\"name\":\"LocalStorageField\",\"slots\":[],\"annotations\":{\"framerIntrinsicWidth\":\"200\",\"framerSupportedLayoutWidth\":\"any-prefer-fixed\",\"framerSupportedLayoutHeight\":\"any\",\"framerContractVersion\":\"1\",\"framerIntrinsicHeight\":\"40\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./LocalStorageField.map"],"mappings":"6MAKG,SAAS,EAAkB,EAAM,CAAC,GAAK,CAAC,eAAa,eAAa,gBAAc,iBAAe,OAAK,YAAU,UAAQ,YAAU,YAAU,EAAW,CAAC,EAAc,GAAkBA,EAAS,EAAE,CAAC,CAAO,EAAS,EAAa,SAAS,GAAG,EAAa,OACrP,EAAgB,GAAY,CAAC,GAAG,CAAC,GAAmB,IAAS,QAAa,CAAC,EAAO,aAAa,OAAO,KAAK,IAAM,EAAK,OAAO,KAAK,EAAO,aAAa,CAAO,EAAgB,EAAW,aAAa,CAC3M,OAAO,EAAK,KAAK,GAAK,EAAI,aAAa,CAAC,SAAS,EAAgB,CAAC,EAC5D,EAAW,GAAO,CAAC,GAAG,CAAC,EAAM,MAAM,GACzC,GAAG,CAAC,IAAM,EAAO,KAAK,MAAM,EAAM,CAClC,GAAG,OAAO,GAAS,UAAU,EAAe,OAAO,OAAiB,EAEpE,OAAO,EAAM,QAAQ,eAAe,GAAG,EACjC,GAAmB,EAAI,IAAO,CAAC,GAAG,CAAC,GAAK,OAAO,GAAM,SAAS,OAAO,KAAK,IAAM,EAAK,EAAK,MAAM,IAAI,CAAK,EAAQ,EAAI,IAAI,IAAM,KAAO,EAAK,CAAC,GAAG,EAAQ,KAAO,IAAA,GAAU,OAAO,KAAK,EAAQ,EAAQ,GAAM,OAAO,GACjN,MAAsB,CAAC,GAAU,IAAS,QAAa,CAAC,EAAO,aAAa,MAAM,EA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