@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 • 12.7 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,h as e}from"./p-BeCqCMz1.js";const s=()=>`.sc-json-viewer-h{display:block;font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif;--color-text-secondary:#6b7280}[theme=dark].sc-json-viewer-h{--color-text-secondary:#9ca3af;--color-background:#1f2937;--color-text:#f9fafb}[theme=light].sc-json-viewer-h{--color-text-secondary:#6b7280;--color-background:#fff;--color-text:#111827} (prefers-color-scheme:dark){[theme=system].sc-json-viewer-h{--color-text-secondary:#9ca3af;--color-background:#1f2937;--color-text:#f9fafb}}summary.sc-json-viewer{position:relative}summary.sc-json-viewer:before{content:"►";position:absolute;left:0;font-size:.75rem;color:var(--color-text-secondary);transform:rotate(0);transition:transform .2s}details[open].sc-json-viewer>summary.sc-json-viewer:before{transform:rotate(90deg)}`;const i=class{constructor(s){t(this,s);this.viewMode="tree";this.maxHeight=500;this.showLineNumbers=true;this.expandAll=false;this.theme="system";this.expandedNodes=new Set;this.parsedData=null;this.error=null;this.copied=false;this.isDarkMode=false;this.sanitizeData=t=>{if(Array.isArray(t)){return t.map(this.sanitizeData)}if(t!==null&&typeof t==="object"){const e={};Object.entries(t).forEach((([t,s])=>{if(!t.startsWith("$")){e[t]=this.sanitizeData(s)}}));return e}return t};this.toggleView=()=>{this.currentViewMode=this.currentViewMode==="tree"?"code":"tree"};this.copyToClipboard=async()=>{try{const t=JSON.stringify(this.parsedData,null,2);await navigator.clipboard.writeText(t);this.copied=true;setTimeout((()=>{this.copied=false}),2e3)}catch(t){console.error("Failed to copy JSON to clipboard:",t);this.createFallbackCopyMethod(JSON.stringify(this.parsedData,null,2))}};this.handleDarkModeChange=()=>{this.updateDarkMode()};this.renderTreeNode=(t,s,i=0,r="")=>{const o=typeof s==="object"&&s!==null;const a=r?`${r}.${t}`:t;const n=`node-${a}`;const l=Array.isArray(s);const h=t=>{if(t.key==="Enter"||t.key===" "){t.preventDefault();const e=t.target.closest("details");if(e){e.open=!e.open;if(e.open){this.expandedNodes.add(n)}else{this.expandedNodes.delete(n)}this.expandedNodes=new Set(this.expandedNodes)}}};if(o){const r=l?Object.entries(s):Object.entries(s);const o=r.length;const c=`${o} ${o===1?"item":"items"}`;const d=l?"array":"object";const g=this.expandedNodes.has(n);const p=t=>{const e=t.target.closest("details");if(e&&e.open){this.expandedNodes.add(n)}else{this.expandedNodes.delete(n)}this.expandedNodes=new Set(this.expandedNodes)};return e("div",{class:"ml-4"},e("details",{class:"mb-1",open:g,onToggle:p,id:n},e("summary",{class:`group relative flex cursor-pointer list-none items-center rounded py-1 pl-5 font-mono ${this.isDarkMode?"hover:bg-gray-700":"hover:bg-gray-50"} focus:ring-2 focus:ring-blue-500 focus:ring-offset-1 focus:outline-hidden`,onKeyDown:h,tabIndex:0,role:"button","aria-expanded":g?"true":"false","aria-controls":`${n}-content`,"aria-label":`${t}: ${d} with ${c}, ${g?"click to collapse":"click to expand"}`},e("div",{class:"flex w-full items-center"},e("span",{class:`mr-2 font-medium ${this.isDarkMode?"text-blue-400":"text-blue-600"}`},t,": "),e("span",{class:`flex items-center gap-1 ${this.isDarkMode?"text-gray-400":"text-gray-500"}`},e("span",null,l?"[":"{"),e("span",null,c),e("span",null,l?"]":"}"),e("span",{class:`ml-2 text-xs opacity-0 transition-opacity duration-200 group-hover:opacity-100 ${this.isDarkMode?"text-blue-400":"text-blue-500"}`,"aria-hidden":"true"},g?"Click to collapse":"Click to expand")))),e("div",{id:`${n}-content`,class:`ml-4 border-l-2 pl-3 ${this.isDarkMode?"border-gray-600":"border-gray-200"}`,role:"group","aria-label":`Contents of ${t} ${d}`},r.map((([t,s],r)=>e("div",{key:`${n}-${r}`},this.renderTreeNode(l?`${t}`:t,s,i+1,a)))))))}let c="";let d=JSON.stringify(s);let g=typeof s;if(g==="string"){c=this.isDarkMode?"text-green-400":"text-green-600"}else if(g==="number"){c=this.isDarkMode?"text-blue-400":"text-blue-600"}else if(g==="boolean"){c=this.isDarkMode?"text-purple-400":"text-purple-600"}else if(s===null){c=this.isDarkMode?"text-gray-400":"text-gray-500";d="null";g="undefined"}return e("div",{class:`flex items-center py-1 font-mono text-sm ${this.isDarkMode?"hover:bg-gray-700":"hover:bg-gray-50"} rounded-sm`,tabIndex:0,role:"treeitem","aria-label":`${t}: ${d} (${g})`},e("span",{class:`font-medium ${this.isDarkMode?"text-blue-400":"text-blue-600"}`},t,": "),e("span",{class:c},d))};this.formatCodeLine=t=>{const e=this.isDarkMode?"text-green-400":"text-green-600";const s=this.isDarkMode?"text-purple-400":"text-purple-600";const i=this.isDarkMode?"text-gray-400":"text-gray-500";const r=this.isDarkMode?"text-blue-400":"text-blue-600";return t.replace(/(".+?")(: )?/g,`<span class="${e}">$1</span>$2`).replace(/: (true|false)([,}\]\s])/g,`: <span class="${s}">$1</span>$2`).replace(/: (null)([,}\]\s])/g,`: <span class="${i}">$1</span>$2`).replace(/: ([0-9]+(\.[0-9]+)?)([,}\]\s])/g,`: <span class="${r}">$1</span>$3`)}}handleDataChange(){this.parseData()}handleViewModeChange(){this.currentViewMode=this.viewMode}handleExpandAllChange(){if(this.expandAll){this.expandAllNodes()}else{this.collapseAllNodes()}}handleThemeChange(){this.updateDarkMode()}componentWillLoad(){this.currentViewMode=this.viewMode;this.parseData();if(this.expandAll){this.expandAllNodes()}this.initializeDarkMode()}disconnectedCallback(){this.cleanupDarkModeListener()}async expandAllNodes(){this.expandNodeRecursive(this.parsedData,"root")}async collapseAllNodes(){this.expandedNodes=new Set}render(){if(this.error){return e("div",{class:"p-4 text-center text-red-500",role:"alert","aria-live":"assertive"},e("p",null,"Invalid JSON: ",this.error),e("slot",null))}if(!this.parsedData){return e("div",{class:"p-4 text-center text-gray-500",role:"status","aria-live":"polite"},e("p",null,"No data provided"),e("slot",null))}const t=JSON.stringify(this.parsedData,null,2);const s=this.maxHeight>0?{maxHeight:`${this.maxHeight}px`}:{};const i=`json-viewer-${Math.random().toString(36).substring(2,11)}`;const r=`${i}-content`;return e("div",{class:`overflow-hidden rounded-lg border shadow-sm ${this.isDarkMode?"border-gray-600 bg-gray-800 text-gray-50":"border-gray-200 bg-white text-gray-800"}`,role:"region","aria-labelledby":`${i}-title`},e("div",{class:`flex items-center justify-between border-b p-3 ${this.isDarkMode?"border-gray-600":"border-gray-200"}`},e("div",{class:"flex items-center gap-2"},e("span",{id:`${i}-title`,class:`text-sm font-medium ${this.isDarkMode?"text-gray-200":"text-gray-800"}`},"JSON Viewer"),e("span",{class:`text-xs ${this.isDarkMode?"text-gray-400":"text-gray-500"}`,"aria-live":"polite"},"(",Object.keys(this.parsedData).length," ",Object.keys(this.parsedData).length===1?"property":"properties",")")),e("button",{onClick:this.toggleView,class:`flex cursor-pointer items-center gap-1 rounded-md px-3 py-1.5 text-xs font-medium transition-all duration-200 ${this.isDarkMode?"border border-gray-600 bg-gray-900 text-gray-50 hover:border-blue-600 hover:bg-gray-700":"border border-gray-200 bg-gray-100 text-gray-800 hover:border-blue-400 hover:bg-gray-50"} focus:ring-2 focus:ring-blue-500 focus:ring-offset-1 focus:outline-hidden`,"aria-controls":r,"aria-label":`Switch to ${this.currentViewMode==="tree"?"code":"tree"} view`,type:"button"},this.currentViewMode==="tree"?"Code View":"Tree View")),e("div",{id:r,class:`relative overflow-auto ${this.isDarkMode?"bg-gray-800":"bg-white"}`,style:s,role:"group","aria-label":`JSON data in ${this.currentViewMode} view`},e("button",{onClick:this.copyToClipboard,class:`absolute top-2 right-2 z-10 rounded-md p-1 transition-all duration-200 ${this.copied?this.isDarkMode?"bg-green-600 text-white":"bg-green-100 text-green-800":this.isDarkMode?"bg-gray-700 text-gray-300 hover:bg-gray-600 hover:text-white":"bg-gray-100 text-gray-600 hover:bg-gray-200 hover:text-gray-800"} opacity-75 hover:opacity-100 focus:ring-2 focus:ring-blue-500 focus:ring-offset-1 focus:outline-hidden`,title:this.copied?"Copied!":"Copy JSON to clipboard","aria-label":this.copied?"JSON copied to clipboard":"Copy JSON to clipboard",type:"button"},e("span",{class:"sr-only"},this.copied?"Copied!":"Copy JSON"),this.copied?e("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2","stroke-linecap":"round","stroke-linejoin":"round",class:"h-4 w-4","aria-hidden":"true"},e("title",null,"Check mark"),e("polyline",{points:"20 6 9 17 4 12"})):e("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2","stroke-linecap":"round","stroke-linejoin":"round",class:"h-4 w-4","aria-hidden":"true"},e("title",null,"Copy icon"),e("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),e("path",{d:"M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1"}))),this.currentViewMode==="tree"&&e("div",{class:`p-4 pr-12 ${this.isDarkMode?"":"bg-gray-50"}`},Object.entries(this.parsedData).map((([t,s],i)=>e("div",{key:`root-${i}`},this.renderTreeNode(t,s,0,"root"))))),this.currentViewMode==="code"&&e("div",{class:`pr-12 font-mono text-sm ${this.isDarkMode?"":"bg-gray-50"}`},this.showLineNumbers?e("div",{class:"flex"},e("div",{class:`border-r px-2 py-4 text-right select-none ${this.isDarkMode?"border-gray-600 bg-gray-900 text-gray-400":"border-gray-200 bg-gray-100 text-gray-500"}`},t.split("\n").map(((t,s)=>e("div",{class:"min-h-5",key:`line-${s}`},s+1)))),e("pre",{class:`grow overflow-x-auto p-4 whitespace-pre-wrap ${this.isDarkMode?"text-gray-200":"text-gray-800"}`},t.split("\n").map(((t,s)=>e("div",{class:"min-h-5",key:`code-${s}`,innerHTML:this.formatCodeLine(t)}))))):e("pre",{class:"grow overflow-x-auto p-4 whitespace-pre-wrap"},t.split("\n").map(((t,s)=>e("div",{class:"min-h-5",key:`code-${s}`,innerHTML:this.formatCodeLine(t)})))))))}parseData(){try{let t;if(typeof this.data==="string"){t=JSON.parse(this.data)}else if(this.data!==null&&typeof this.data==="object"){t=this.data}else{throw new Error("Invalid data format")}this.parsedData=this.sanitizeData(t);this.error=null}catch(t){this.error=t.message;this.parsedData=null}}createFallbackCopyMethod(t){const e=document.createElement("textarea");e.style.position="fixed";e.style.left="-999999px";e.style.top="-999999px";e.style.opacity="0";e.style.pointerEvents="none";e.value=t;document.body.appendChild(e);e.focus();e.select();try{const t=document.execCommand("copy");if(t){this.copied=true;setTimeout((()=>{this.copied=false}),2e3)}}catch(t){console.error("Fallback copy failed:",t)}finally{document.body.removeChild(e)}}cleanupDarkModeListener(){if(this.darkModeMediaQuery){if(this.darkModeMediaQuery.removeEventListener){this.darkModeMediaQuery.removeEventListener("change",this.handleDarkModeChange)}else if(this.darkModeMediaQuery.removeListener){this.darkModeMediaQuery.removeListener(this.handleDarkModeChange)}}}expandNodeRecursive(t,e){if(t!==null&&typeof t==="object"){this.expandedNodes.add(e);Object.entries(t).forEach((([t,s])=>{const i=e?`${e}.${t}`:t;this.expandNodeRecursive(s,i)}));this.expandedNodes=new Set(this.expandedNodes)}}initializeDarkMode(){if(window.matchMedia){this.darkModeMediaQuery=window.matchMedia("(prefers-color-scheme: dark)");this.updateDarkMode();if(this.darkModeMediaQuery.addEventListener){this.darkModeMediaQuery.addEventListener("change",this.handleDarkModeChange)}else if(this.darkModeMediaQuery.addListener){this.darkModeMediaQuery.addListener(this.handleDarkModeChange)}}else{this.isDarkMode=this.theme==="dark"}}updateDarkMode(){if(this.theme==="dark"){this.isDarkMode=true}else if(this.theme==="light"){this.isDarkMode=false}else if(this.theme==="system"&&this.darkModeMediaQuery){this.isDarkMode=this.darkModeMediaQuery.matches}}static get watchers(){return{data:[{handleDataChange:0}],viewMode:[{handleViewModeChange:0}],expandAll:[{handleExpandAllChange:0}],theme:[{handleThemeChange:0}]}}};i.style=s();export{i as json_viewer};
//# sourceMappingURL=p-b75a0a4a.entry.js.map