Files
Jacek Pyziak cd264483f8 Add PSR HTTP Message Interfaces and Dependencies
- Implemented StreamInterface, UploadedFileInterface, and UriInterface as per PSR standards.
- Added getallheaders function to retrieve HTTP headers in a compatible manner.
- Included LICENSE files for ralouphie/getallheaders and symfony/deprecation-contracts.
- Introduced function for triggering deprecation notices in Symfony.
2025-12-28 12:44:00 +01:00

1 line
8.0 KiB
JavaScript

"use strict";(()=>{const{createBlock:createBlock}=wp.blocks,{dispatch:dispatch,select:select}=wp.data;class atfpCreateNewBlock{constructor(){this.updateBlockStore={},this.loaderRemove=null,this.loader=null,this.replaceAttributes=null,this.updateBlockId=new Array}copyTranslateText=()=>{const selection=window.getSelection(),range=document.createRange();range.selectNodeContents(document.getElementById("atfp-copy-text")),selection.removeAllRanges(),selection.addRange(range),document.execCommand("copy"),selection.removeAllRanges()};noticeInitialize=()=>{dispatch("core/notices").createNotice("info",'To enable translation, please include the Make This Content Available for Translation text in your block content. For help, watch the video and click <b>"Copy Text"</b> to use. Then, paste it into the section of your block you want automatically translated',{isDismissible:!1,id:"atfp-notice-id",actions:[{label:"Watch Video.",url:`${atfpAddBlockVars.atfp_demo_page_url}#custom-block-translate`}],__unstableHTML:!0}).then(()=>{const targetAnchor=document.querySelector(`a[href^="${atfpAddBlockVars.atfp_demo_page_url}#custom-block-translate"]`);targetAnchor&&targetAnchor.addEventListener("click",e=>{e.preventDefault(),window.open(targetAnchor.href,"_blank")})})};copyBtnInitialize=()=>{const copyBtn=document.createElement("div");copyBtn.id="atfp-copy-btn",copyBtn.innerHTML="Copy Text",copyBtn.addEventListener("click",this.copyTranslateText),copyBtn.ariaLabel="Copy Text",copyBtn.title='Click to copy the text "Make This Content Available for Translation"';const copyText=document.createElement("div");copyText.id="atfp-copy-text",copyText.innerHTML="Make This Content Available for Translation",document.body.appendChild(copyBtn),document.body.appendChild(copyText)};addBlockInitialize=newBlock=>{this.newBlock=newBlock,this.creteNewBlock(),this.skeletonLoader()};removeLoader=()=>{clearTimeout(this.loaderRemove),this.loaderRemove=setTimeout(()=>{this.loader&&this.loader.remove()},2e3)};creteNewBlock=()=>{const newBlock=createBlock(this.newBlock);this.updateBlockData(newBlock)};updateBlockData=async Block=>{await dispatch("core/block-editor").insertBlocks([Block]),setTimeout(()=>{const blockWrp=document.getElementById(`block-${Block.clientId}`);blockWrp&&blockWrp.appendChild(this.loader)},100),setTimeout(()=>{this.updateBlockContent(Block)},400)};updateBlockContent=Block=>{const newBlock=document.getElementById(`block-${Block.clientId}`);newBlock&&this.updateContent(newBlock)};updateContent=async ele=>{const element=ele;let i=1;if(this.removeLoader(),element)if("true"==element.contentEditable&&element.children.length<2)element.innerHTML="Make This Content Available for Translation";else{const innerElements=element.getElementsByTagName("*");for(let innerElement of innerElements)i===innerElements.length&&(this.removeLoader(),setTimeout(()=>{this.updateBlockFromStore()},500)),i++,["SCRIPT","STYLE","META","LINK","TITLE","NOSCRIPT","STYLE","SCRIPT","NOSCRIPT","STYLE","SCRIPT","NOSCRIPT","STYLE","SCRIPT","NOSCRIPT"].includes(innerElement.tagName)||innerElement.childNodes.length>0&&innerElement.childNodes.forEach(child=>{child.nodeType===Node.TEXT_NODE&&this.updateBlockAttr(innerElement,child)})}};updateBlockAttr=(innerElement,child)=>{let blockId=!1;if(innerElement.classList.contains("wp-block"))blockId=innerElement.dataset.block;else{const parentBlock=innerElement.closest(".wp-block");parentBlock&&(blockId=parentBlock.dataset.block)}const blockAttributes=select("core/block-editor").getBlockAttributes(blockId);let index=0;if(!this.updateBlockStore[blockId]){let attributes=JSON.parse(JSON.stringify(blockAttributes));this.updateBlockStore[blockId]={attributes:attributes},this.updateBlockStore[blockId].updateBlockData={}}const updateNestedAttributes=async(attributes,child)=>{const updateAttributes=async key=>{if(index++,"string"==typeof attributes[key]&&""!==attributes[key].trim()&&(attributes[key].trim()===child.textContent.trim()||attributes[key]===child.textContent.trim())){const originalValue=attributes[key],newValue="Make This Content Available for Translation "+index;this.updateBlockStore[blockId].updateBlockData[newValue.replace(/\s+/g,"-")]=originalValue,attributes[key]=newValue}};if("object"==typeof attributes&&null!==attributes)for(const key of Object.keys(attributes))await updateAttributes(key),"object"==typeof attributes[key]&&null!==attributes[key]&&await updateNestedAttributes(attributes[key],child)},blockStoreAttributes=this.updateBlockStore[blockId].attributes;updateNestedAttributes(blockStoreAttributes,child)};updateBlockFromStore=()=>{const blockStoreAttributes=this.updateBlockStore;Object.keys(blockStoreAttributes).forEach(blockId=>{const blockAttributes=blockStoreAttributes[blockId].attributes;this.removeLoader(),dispatch("core/block-editor").updateBlockAttributes(blockId,blockAttributes).then(()=>{clearTimeout(this.replaceAttributes),this.replaceAttributes=setTimeout(()=>{this.removeLoader();const blockIds=Object.keys(this.updateBlockStore);this.replaceBlockContent(blockIds[0])},500)})})};replaceBlockContent=blockId=>{const blockStoreAttributes=this.updateBlockStore,checkValidAttributes=(value=!1,blockId)=>{const blockElement=document.querySelector(`#block-${blockId}`),regex=new RegExp(value,"g"),matchFound=regex.test(blockElement.innerText);return matchFound},blockAttributes=blockStoreAttributes[blockId].attributes,upateNestedAttributes=async attributes=>{const updateAttributes=async key=>{if("string"==typeof attributes[key]&&attributes[key].includes("Make This Content Available for Translation"))try{const keyWithDashes=attributes[key].replace(/\s+/g,"-"),originalValue=this.updateBlockStore[blockId].updateBlockData[keyWithDashes],status=checkValidAttributes(attributes[key],blockId);attributes[key]=status?"Make This Content Available for Translation":originalValue}catch(e){console.log(`${attributes[key]} is not valid JSON.`)}};if("object"==typeof attributes&&null!==attributes)for(const key of Object.keys(attributes))await updateAttributes(key),"object"==typeof attributes[key]&&null!==attributes[key]&&await upateNestedAttributes(attributes[key])};upateNestedAttributes(blockAttributes),setTimeout(()=>{dispatch("core/block-editor").updateBlockAttributes(blockId,blockAttributes).then(()=>{const blockIds=Object.keys(this.updateBlockStore);if(blockIds.length>0){this.updateBlockId.push(blockId),dispatch("core/block-editor").selectBlock(null),this.removeLoader();const firstBlockId=blockIds.find(id=>!this.updateBlockId.includes(id));firstBlockId&&this.replaceBlockContent(firstBlockId)}})},500)};skeletonLoader=()=>{const loader=document.createElement("div"),loaderContainer=()=>{const container='<style>.atfp-loader-wrapper{position:absolute;width:100%;height:100%;top:0;left:0;z-index:99999;}.atfp-loader-container{width:100%;height:100%;}.atfp-loader-skeleton{--skbg:hsl(227deg, 13%, 50%, 0.2);display:grid;gap:20px;width:100%;height:100%;background:#ffffff;padding:15px;border-radius:8px;box-shadow:0 4px 12px rgba(0, 0, 0, 0.1);transition:transform 0.3s ease;transform:scale(1.02);}.atfp-loader-shimmer{display:flex;aspect-ratio:2/1;width:100%;height:100%;background:var(--skbg);border-radius:4px;overflow:hidden;position:relative;}.atfp-loader-shimmer::before{content:"";position:absolute;width:100%;height:100%;background-image:linear-gradient(-90deg,transparent 8%,rgba(255,255,255,0.28) 18%,transparent 33%);background-size:200%;animation:shimerAnimate 1.5s ease-in-out infinite;}@keyframes shimerAnimate{0%{background-position:100% 0;}100%{background-position:-100% 0;}}</style>';return'<div class="atfp-loader-container">'+container+'<div class="atfp-loader-skeleton"><span class="atfp-loader-shimmer"></span></div></div>'};loader.className="atfp-loader-wrapper",loader.innerHTML=loaderContainer(),this.loader=loader}}window.addEventListener("load",()=>{const atfpCreateBlockObj=new atfpCreateNewBlock;atfpCreateBlockObj.copyBtnInitialize(),atfpCreateBlockObj.noticeInitialize();const urlParams=new URLSearchParams(window.location.search);let newBlock="";urlParams.has("atfp_new_block")&&""!==urlParams.get("atfp_new_block").trim()&&(newBlock=urlParams.get("atfp_new_block"),atfpCreateBlockObj.addBlockInitialize(newBlock))})})();