@kit-data-manager/pid-component
Version:
The PID-Component is a web component that can be used to evaluate and display FAIR Digital Objects, PIDs, ORCiDs, and possibly other identifiers in a user-friendly way. It is easily extensible to support other identifier types.
19 lines • 8.3 kB
JavaScript
/*!
*
* Copyright 2024-2026 Karlsruhe Institute of Technology.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import{r as t}from"./p-4OKsKdsp.js";export{I as ISBNType,P as PID,a as PIDDataType,b as PIDRecord}from"./p-4OKsKdsp.js";import"./p-BeCqCMz1.js";import"./json-viewer.entry.esm.js";const e=new Set(["SCRIPT","STYLE","TEXTAREA","INPUT","SELECT","NOSCRIPT","PID-COMPONENT","CODE","PRE","SVG"]);const n="data-pid-auto-detected";function o(t,o){if(e.has(t.tagName)){return true}if(t.hasAttribute("contenteditable")){return true}if(t.hasAttribute(n)){return true}if(t.closest(".pid-auto-detect-wrapper")){return true}if(o){try{if(t.matches(o)){return true}}catch(t){}}return false}function r(t,e,n=50){return new Promise((r=>{const i=[];let u=0;const c=document.createTreeWalker(t,NodeFilter.SHOW_TEXT|NodeFilter.SHOW_ELEMENT,{acceptNode(t){var n;if(t.nodeType===Node.ELEMENT_NODE){const n=t;if(o(n,e)){return NodeFilter.FILTER_REJECT}return NodeFilter.FILTER_SKIP}const r=(n=t.textContent)===null||n===void 0?void 0:n.trim();if(!r||r.length<2){return NodeFilter.FILTER_SKIP}return NodeFilter.FILTER_ACCEPT}});const s=typeof requestIdleCallback!=="undefined"?requestIdleCallback:t=>setTimeout((()=>t({timeRemaining:()=>16})),0);function l(t){let e=0;while(e<n&&t.timeRemaining()>0){const t=c.nextNode();if(t===null){r(i);return}if(t.nodeType===Node.TEXT_NODE){const n=t;const o=n.textContent||"";if(o.trim().length>=2){i.push({id:u++,textNode:n,text:o})}e++}}s(l)}s(l)}))}const i="pid-auto-detect-wrapper";const u="data-pid-auto-detected";function c(t,e,n){var o;const r=[];const c=t.parentNode;if(!c)return r;const f=t.textContent||"";const a=[...e].sort(((t,e)=>t.start-e.start)).filter(((t,e,n)=>e===0||t.start>=n[e-1].end));const d=document.createDocumentFragment();let p=0;for(const t of a){if(t.start>p){d.appendChild(document.createTextNode(f.substring(p,t.start)))}const e=document.createElement("span");e.className=i;e.setAttribute(u,"true");e.style.display="inline";const c=document.createElement("span");c.textContent=t.value;e.appendChild(c);const a=document.createElement("pid-component");a.setAttribute("value",t.value);if(n.renderers&&n.renderers.length>0){a.setAttribute("renderers",JSON.stringify(n.renderers))}a.setAttribute("fallback-to-all",String((o=n.fallbackToAll)!==null&&o!==void 0?o:true));a.style.cssText="visibility:hidden;position:absolute;width:0;height:0;overflow:hidden;pointer-events:none;";a.setAttribute("aria-hidden","true");s(a,n);e.appendChild(a);const v=l(c,a,e);d.appendChild(e);r.push({wrapper:e,originalText:t.value,precedingTextNode:null,followingTextNode:null,pidComponent:a,observer:v,originalSpan:c});p=t.end}if(p<f.length){d.appendChild(document.createTextNode(f.substring(p)))}c.replaceChild(d,t);return r}function s(t,e){var n;if(e.settings){const n=typeof e.settings==="string"?e.settings:JSON.stringify(e.settings);t.setAttribute("settings",n)}if(e.darkMode){t.setAttribute("dark-mode",e.darkMode)}if(e.levelOfSubcomponents!==undefined){t.setAttribute("level-of-subcomponents",String(e.levelOfSubcomponents))}if(e.itemsPerPage!==undefined){t.setAttribute("items-per-page",String(e.itemsPerPage))}t.setAttribute("emphasize-component",String((n=e.emphasizeComponent)!==null&&n!==void 0?n:false));if(e.showTopLevelCopy!==undefined){t.setAttribute("show-top-level-copy",String(e.showTopLevelCopy))}if(e.defaultTTL!==undefined){t.setAttribute("default-t-t-l",String(e.defaultTTL))}}function l(t,e,n){let o=false;let r=null;let i=null;function u(){if(r){clearInterval(r);r=null}if(i){clearTimeout(i);i=null}a.disconnect()}function c(){o=true;u();e.remove();f(n,t.textContent||"")}function s(){o=true;u();t.style.display="none";t.setAttribute("aria-hidden","true");e.style.cssText="";e.removeAttribute("aria-hidden")}function l(){if(o)return;const t=e.shadowRoot;if(!t)return;const n=t.querySelectorAll("*");if(n.length===0)return;const r=t.querySelector('[class*="relative"]');if(r){const t=r.style;if(t&&t.display==="none"){c();return}}const i=t.querySelector('[role="alert"]');if(i){c();return}const u=t.querySelector(".animate-spin");if(u)return;const l=t.querySelector('[role="button"], pid-collapsible, a[href], copy-button, color-highlight, locale-visualization');if(l){s();return}if(n.length>2&&!u&&!i){s();return}}const a=new MutationObserver(l);r=setInterval((()=>{if(o){u();return}if(e.shadowRoot){try{a.observe(e.shadowRoot,{childList:true,subtree:true,attributes:true})}catch(t){}l()}}),100);i=setTimeout((()=>{if(!o){l();if(!o){c()}}}),15e3);a.observe(e,{attributes:true,childList:true});return a}function f(t,e){const n=t.parentNode;if(!n)return;const o=document.createTextNode(e);n.replaceChild(o,t);n.normalize()}function d(t){for(const e of t){e.observer.disconnect();f(e.wrapper,e.originalText)}}function p(){const e=new Map;return t.filter((t=>t.key!=="FallbackType")).map((t=>{var n;e.set(t.key,(n=t.priority)!==null&&n!==void 0?n:99);return{key:t.key,autoDiscoverableByDefault:t.autoDiscoverableByDefault,check:e=>{const n=new t.constructor(e);return n.quickCheck()}}})).sort(((t,n)=>{var o,r;return((o=e.get(t.key))!==null&&o!==void 0?o:99)-((r=e.get(n.key))!==null&&r!==void 0?r:99)}))}let v;function m(){if(!v){v=p()}return v}const h=[];m().forEach((t=>h.push(t)));const N=/^[\s.,;:!?\-–—"'`´«»()[\]{}<>*/\\#@^~|]+/;const g=/[\s.,;:!?\-–—"'`´«»()[\]{}<>*/\\#@^~|]+$/;function w(t){const e=N.exec(t);const n=e?e[0].length:0;let o=t.substring(n);const r=g.exec(o);if(r){o=o.substring(0,o.length-r[0].length)}return{sanitized:o,leadingStripped:n}}function y(t,e,n=true){const o=m();if(e&&e.length>0){for(const n of e){const e=o.find((t=>t.key===n));if(e&&e.check(t)){return e.key}}if(!n){return null}}for(const e of o){if(e.autoDiscoverableByDefault&&e.check(t)){return e.key}}return null}const T=10;function S(t={}){const e=t.root||document.body;const n=t.renderers;let o=null;let i=[];let u=false;function s(e){var o;const r=/[\s,;()[\]{}<>"']+/;const i=[];let u=e;let c=0;while(u.length>0){const e=r.exec(u);let s;let l;if(e===null){s=u;l=c;u=""}else{if(e.index>0){s=u.substring(0,e.index);l=c}else{c+=e[0].length;u=u.substring(e[0].length);continue}c+=e.index+e[0].length;u=u.substring(e.index+e[0].length)}if(s.length<2)continue;const{sanitized:f,leadingStripped:a}=w(s);if(f.length<2)continue;const d=y(f,n,(o=t.fallbackToAll)!==null&&o!==void 0?o:true);if(d!==null){i.push({start:l+a,end:l+a+f.length,value:f,rendererKey:d})}}return i}async function l(){if(u)return;const n=await r(e,t.exclude);const o=C(n,T);for(const e of o){if(u)return;await new Promise((n=>{const o=typeof requestIdleCallback!=="undefined"?requestIdleCallback:t=>setTimeout(t,0);o((async()=>{for(const n of e){if(u)break;if(!n.textNode.parentNode)continue;const e=s(n.text);if(e.length>0){const o=c(n.textNode,e,t);i.push(...o)}}n()}))}))}}if(t.observe){o=new MutationObserver((e=>{var n;if(u)return;for(const o of e){for(const e of Array.from(o.addedNodes)){if(e.nodeType===Node.ELEMENT_NODE){const o=e;if((n=o.classList)===null||n===void 0?void 0:n.contains("pid-auto-detect-wrapper"))continue;if(o.tagName==="PID-COMPONENT")continue;r(o,t.exclude).then((async e=>{for(const n of e){if(u)break;if(!n.textNode.parentNode)continue;const e=s(n.text);if(e.length>0){const o=c(n.textNode,e,t);i.push(...o)}}}))}}}}));o.observe(e,{childList:true,subtree:true})}l().then((t=>void 0)).catch((t=>console.error("Error during initial PID detection scan:",t)));return{stop(){if(o){o.disconnect()}},rescan(){if(!u){l().then((t=>void 0)).catch((t=>console.error("Error during PID detection rescan:",t)))}},destroy(){u=true;if(o){o.disconnect();o=null}d(i);i=[]}}}function C(t,e){const n=[];for(let o=0;o<t.length;o+=e){n.push(t.slice(o,o+e))}return n}export{S as initPidDetection};
//# sourceMappingURL=index.esm.js.map