sanity-plugin-graph-view 2.0.2 → 2.0.3
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/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/tool/utils.ts +4 -3
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,e,n,o,i,r;function s(t,e){return e||(e=t.slice(0)),Object.freeze(Object.defineProperties(t,{raw:{value:Object.freeze(e)}}))}import{jsx as a,jsxs as l}from"react/jsx-runtime";import{useUserColorManager as c,useClient as d,definePlugin as h}from"sanity";import{useRouter as u,route as f}from"sanity/router";import p from"bezier-easing";import m from"deep-equal";import{rgba as y}from"polished";import{useEffect as g,useState as b,useCallback as v}from"react";import{ForceGraph2D as x}from"react-force-graph";import{v4 as _}from"uuid";import{black as w,gray as k,white as M,hues as I,COLOR_HUES as j}from"@sanity/color";import{useTheme as P}from"@sanity/ui";import O from"styled-components";const q=O.div(t||(t=s(["\n font-family: ",";\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: ",";\n"])),t=>{let{theme:e}=t;return e.fonts.text.family},w.hex),S=O.div(e||(e=s(["\n position: relative;\n width: 100%;\n height: 100%;\n"]))),A=O.div(n||(n=s(["\n font-family: ",";\n display: none;\n position: absolute;\n bottom: ","px;\n left: 50%;\n transform: translate3d(-50%, 0, 0);\n background: var(--component-bg);\n border-radius: ","px;\n padding: ","px;\n z-index: 1000;\n\n &:empty {\n display: none;\n }\n"])),t=>{let{theme:e}=t;return e.fonts.text.family},t=>{let{theme:e}=t;return e.space[0]},t=>{let{theme:e}=t;return e.radius[2]},t=>{let{theme:e}=t;return e.space[2]}),T=O.div(o||(o=s(["\n color: #ccc;\n position: absolute;\n top: ","px;\n left: ","px;\n\n & > div {\n margin: 5px 0;\n }\n"])),t=>{let{theme:e}=t;return e.space[4]},t=>{let{theme:e}=t;return e.space[4]}),W=O.div(i||(i=s(["\n display: flex;\n"]))),C=O.div(r||(r=s(["\n width: 1.25em;\n height: 1.25em;\n background: currentColor;\n border-radius: 50%;\n margin-right: ","px;\n"])),t=>{let{theme:e}=t;return e.space[2]});function D(t){return null===t?0:Array.isArray(t)?Object.entries(t).reduce((t,e)=>t+D(e),0):"object"==typeof t?Object.entries(t).reduce((t,e)=>{let[n,o]=e;return t+D(n)+D(o)},0):"string"==typeof t?t.length:1}function V(t,e){return t.length>e?"".concat(t.substring(0,e),"…"):t}const z=p(0,.9,1,1),B=p(.25,.1,0,1);function N(t){return(t.substring(0,1).toUpperCase()+t.substring(1)).replace(/\./g," ").replace(/[A-Z]/g," $&").trim()}function H(t){const e={};for(const n of t)e[n._type]=(e[n._type]||0)+1;return e}function R(t){return"".concat(t.title||t.name||t._id).trim()}function U(t,e){return 5+D(t)/e*100}function E(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(null!==t)if("object"==typeof t)for(const[n,o]of Object.entries(t))"_ref"===n&&"string"==typeof o&&o.length>0&&e.push(F(o)),E(o,e);else if(Array.isArray(t))for(const n of t)E(n,e);return e}function F(t){return t.replace(/^drafts\./,"")}class G{constructor(){this.id=null,this.user=null,this.doc=null,this.lastActive=0,this.startTime=0,this.angle=0}}class L{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.sessions=[];const e={};for(const n of t)e[n._id]=n;this.data={nodes:t.map(t=>Object.assign({id:t._id,type:"document",doc:t})),links:t.flatMap(t=>E(t).map(e=>({source:t._id,target:e}))).filter(t=>e[t.source]&&e[t.target])}}setSession(t,e){let n=this.sessions.find(n=>{var o;return n.user.id===t.id&&(null==(o=n.doc)?void 0:o._id)===e.doc._id});n||(n=new G,n.id=_(),n.user=t,n.startTime=Date.now(),n.doc=e.doc,n.angle=2*Math.random()*Math.PI,this.sessions.push(n)),n.lastActive=Date.now()}reapSessions(){for(let t=0;t<this.sessions.length;t++){const e=this.sessions[t];Date.now()-e.lastActive>1e4&&(this.sessions=[...this.sessions.slice(0,t),...this.sessions.slice(t+1)],t--)}}clone(){const t=new L;return Object.assign(t,this),t.data={nodes:[...this.data.nodes],links:[...this.data.links]},t}}const Z=new class{constructor(){this._users=[]}async getById(t,e){let n=this._users.find(e=>e._id===t);var o,i,r;return n||(n=await e.users.getById(t),this._users.push(n),n.image=await(o=n.imageUrl||"https://raw.githubusercontent.com/sanity-io/sanity-plugin-graph-view/main/assets/head-silhouette.jpg",i=40,r=40,new Promise(t=>{const e=new Image(i,r);e.onload=()=>{t(e)},e.onerror=e=>{console.log("Image error",e),t(null)},e.src=o}))),n}};function $(t){var e;const n=t.query||'\n *[\n !(_id in path("_.*")) &&\n !(_type match "system.*") &&\n !(_type match "sanity.*")\n ]\n',o=null!=(e=t.apiVersion)?e:"2022-09-01",i=c(),[r,s]=b(0),[h,f]=b(null),[p,_]=b([]),[I,j]=b({}),[O,G]=b(()=>new L),$=u(),J=d({apiVersion:o}),K=v(t=>{const e=function(t){const e={};for(const n of t)n._id.startsWith("drafts.")||(e[n._id]=n);for(const n of t)if(n._id.startsWith("drafts.")){const t=F(n._id);e[t]=Object.assign(n,{_id:t})}return Object.values(e)}(t);s(Math.max(...e.map(D))),_(e),j(H(e)),G(new L(e))},[]),X=v(async t=>{const e=t.result;if(e){e._id=F(e._id);const n={};for(const t of p)n[t._id]=t;let o;const i=[...p],r=p.findIndex(t=>t._id===e._id);r>=0?(o=i[r],i[r]=e):i.push(e),_(i),j(H(i)),s(Math.max(...i.map(D)));const a=O.clone(),l=E(o||{}).filter(t=>t===e._id||null!==n[t]),c=E(e).filter(t=>t===e._id||null!==n[t]);let d,h=!m(l,c);h&&(a.data.links=a.data.links.filter(t=>t.source.id!==e._id).concat(c.map(t=>({source:e._id,target:t}))));const u=O.data.nodes.findIndex(t=>t.doc&&t.doc._id===e._id);u>=0?(d=O.data.nodes[u],d.doc=e):(d={id:e._id,type:"document",doc:e},a.data.nodes.push(d),h=!0),h&&G(a);const f=await Z.getById(t.identity,J);O.setSession(f,d)}else if("disappear"===t.transition){const e=F(t.documentId),n=p.filter(t=>t._id!==e);_(n),j(H(n)),s(Math.max(...n.map(D)));const o=O.clone();o.data.links=o.data.links.filter(t=>t.source.id!==e&&t.target.id!==e),o.data.nodes=o.data.nodes.filter(t=>t.id!==e),G(o)}},[p,O,J]);!function(t,e,n,o){g(()=>{o.fetch(t).then(t=>{e(t)})},[...n,o])}(n,K,[],J),function(t,e,n,o,i,r){g(()=>{const i=r.listen(t,e,n).subscribe(t=>{o(t)});return()=>{i.unsubscribe()}},[...i,r])}(n,{},{},X,[p,O],J),g(()=>{const t=setInterval(()=>O.reapSessions(),1e3);return()=>clearInterval(t)},[O]);const Y=P().sanity;return a(S,{theme:Y,children:l(q,{theme:Y,children:[a(T,{theme:Y,children:(tt=I,(et=Object.keys(tt),nt=t=>tt[t]||0,et.sort((t,e)=>{const n=nt(t),o=nt(e);return n<o?-1:n>o?1:0})).reverse().slice(0,10)).map(t=>l(W,{className:"legend__row",style:{color:Q(t).fill},children:[a(C,{theme:Y}),a("div",{children:N(t)})]},t))}),h&&a(A,{theme:Y,children:R(h.doc)}),a(x,{graphData:O.data,nodeAutoColorBy:"group",enableNodeDrag:!1,onNodeHover:t=>f(t),onNodeClick:t=>{$.navigateIntent("edit",{id:t.doc._id,documentType:t.doc._type})},linkColor:()=>y(k[500].hex,.25),nodeLabel:()=>"",nodeRelSize:1,nodeVal:t=>U(t.doc,r),onRenderFramePost:(t,e)=>{for(const n of O.sessions){const o=O.data.nodes.find(t=>{var e;return t.doc&&t.doc._id===(null==(e=null==n?void 0:n.doc)?void 0:e._id)});if(o){const s=1e4,a=n.angle,l=Math.sqrt(U(o.doc,r)),c=n.user.image,d=i.get(n.user.displayName).tints[400].hex,h=l*e+40,u=c?c.width:0,f=c?c.height:0,p=o.x+Math.sin(a)*h/e,m=o.y+Math.cos(a)*h/e;t.save();try{if(t.globalAlpha=z(1-Math.min(s,Date.now()-n.lastActive)/s),t.font="bold ".concat(Math.round(12/e),"px sans-serif"),t.beginPath(),t.strokeStyle=y(M.hex,1),t.lineWidth=2/e,t.moveTo(o.x+Math.sin(a)*(h-u/2)/e,o.y+Math.cos(a)*(h-f/2)/e),t.lineTo(o.x+Math.sin(a)*l,o.y+Math.cos(a)*l),t.stroke(),t.beginPath(),t.strokeStyle=y(M.hex,1),t.lineWidth=2/e,t.arc(o.x,o.y,l,0,2*Math.PI,!1),t.stroke(),c){t.save();try{const o=700,i=B(Math.max(0,(o-(Date.now()-n.startTime))/o));i>0&&(t.beginPath(),t.fillStyle=y(d,i),t.arc(p,m,(u/2+10)/e,0,2*Math.PI,!1),t.fill()),t.beginPath(),t.fillStyle=y(M.hex,1),t.arc(p,m,u/e/2,0,2*Math.PI,!1),t.clip(),t.drawImage(c,p-u/e/2,m-f/e/2,u/e,f/e),t.strokeStyle=w.hex,t.lineWidth=6/e,t.stroke(),t.strokeStyle=d,t.lineWidth=4/e,t.stroke()}finally{t.restore()}}t.beginPath(),t.strokeStyle=y(w.hex,1),t.lineWidth=.5/e,t.arc(p,m,u/e/2,0,2*Math.PI,!1),t.stroke();const i=a>=Math.PI/2&&a<1.5*Math.PI,r=i?m-(f/2+5)/e:m+(f/2+5)/e;t.fillStyle=y(M.hex,1),t.textAlign="center",t.textBaseline=i?"bottom":"top",t.fillText(n.user.displayName,p,r)}finally{t.restore()}}}},nodeCanvasObject:(t,e,n)=>{if("document"===t.type){const o=Q(t.doc._type),i=Math.sqrt(U(t.doc,r)),s=Math.min(100,10/n);if(e.beginPath(),e.fillStyle=null!==h&&t.doc._id===h.doc._id?y(k[500].hex,.8):o.fill,e.strokeStyle=o.border,e.lineWidth=.5,e.arc(t.x,t.y,i,0,2*Math.PI,!1),e.stroke(),e.fill(),i*n>10){e.font="".concat(s,"px sans-serif");const o=2*i+30/n;for(let r=50;r>=5;r/=1.2){const s=V(R(t.doc),Math.round(r));if(e.measureText(s).width<o){e.textAlign="center",e.textBaseline="top",e.strokeStyle=y(w.hex,.5),e.lineWidth=2/n,e.strokeText(s,t.x,t.y+i+5/n),e.fillText(s,t.x,t.y+i+5/n);break}}}}},linkCanvasObject:(t,e,n)=>{e.beginPath(),e.strokeStyle=y(k[500].hex,.125),e.lineWidth=2/n,e.moveTo(t.source.x,t.source.y),e.lineTo(t.target.x,t.target.y),e.stroke()}})]})});var tt,et,nt}const J={};let K=0;function Q(t){if(J[t])return J[t];const e=j[K%j.length];return K+=1,J[t]={fill:I[e][400].hex,border:y(w.hex,.5)},J[t]}const X=()=>a("svg",{xmlns:"http://www.w3.org/2000/svg",width:"1em",height:"1em",viewBox:"0 0 46.063 46.063",children:a("path",{fill:"currentColor",d:"M18.022 38.676v2.813h-1.21q-4.864 0-6.525-1.447-1.64-1.445-1.64-5.762v-4.666q0-2.95-1.055-4.083-1.055-1.13-3.828-1.13h-1.19v-2.794h1.19q2.793 0 3.828-1.114 1.055-1.132 1.055-4.043V11.76q0-4.316 1.64-5.742 1.66-1.446 6.524-1.446h1.212v2.793h-1.328q-2.754 0-3.594.86-.84.86-.84 3.613v4.844q0 3.066-.898 4.453-.88 1.387-3.028 1.875 2.168.527 3.047 1.914.88 1.387.88 4.434v4.843q0 2.754.84 3.614.84.86 3.594.86h1.328zM28.04 38.676h1.368q2.735 0 3.555-.84.84-.84.84-3.633V29.36q0-3.047.88-4.434.878-1.387 3.046-1.914-2.17-.488-3.048-1.875-.88-1.387-.88-4.453V11.84q0-2.773-.84-3.613-.82-.86-3.554-.86H28.04V4.574h1.232q4.863 0 6.484 1.446 1.64 1.426 1.64 5.742v4.687q0 2.91 1.055 4.042 1.056 1.114 3.83 1.114h1.21V24.4h-1.21q-2.774 0-3.83 1.13-1.053 1.134-1.053 4.084v4.667q0 4.318-1.64 5.763-1.622 1.446-6.485 1.446h-1.23v-2.814z"})});var Y=Object.defineProperty,tt=Object.getOwnPropertySymbols,et=Object.prototype.hasOwnProperty,nt=Object.prototype.propertyIsEnumerable,ot=(t,e,n)=>e in t?Y(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;const it=h(function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return{name:"@sanity/content-graph-view",tools:e=>[...e,{name:"graph-your-content",title:"Graph",icon:X,component:function(){return a($,((t,e)=>{for(var n in e||(e={}))et.call(e,n)&&ot(t,n,e[n]);if(tt)for(var n of tt(e))nt.call(e,n)&&ot(t,n,e[n]);return t})({},t))},router:f.create("/:selectedDocumentId")}]}});export{it as contentGraphView};//# sourceMappingURL=index.esm.js.map
|
|
1
|
+
var t,e,n,o,i,r;function s(t,e){return e||(e=t.slice(0)),Object.freeze(Object.defineProperties(t,{raw:{value:Object.freeze(e)}}))}import{jsx as a,jsxs as l}from"react/jsx-runtime";import{useUserColorManager as c,useClient as d,definePlugin as h}from"sanity";import{useRouter as u,route as f}from"sanity/router";import p from"bezier-easing";import m from"deep-equal";import{rgba as y}from"polished";import{useEffect as g,useState as b,useCallback as v}from"react";import{ForceGraph2D as x}from"react-force-graph";import{v4 as _}from"uuid";import{black as w,gray as k,white as M,hues as I,COLOR_HUES as j}from"@sanity/color";import{useTheme as P}from"@sanity/ui";import O from"styled-components";const q=O.div(t||(t=s(["\n font-family: ",";\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: ",";\n"])),t=>{let{theme:e}=t;return e.fonts.text.family},w.hex),S=O.div(e||(e=s(["\n position: relative;\n width: 100%;\n height: 100%;\n"]))),A=O.div(n||(n=s(["\n font-family: ",";\n display: none;\n position: absolute;\n bottom: ","px;\n left: 50%;\n transform: translate3d(-50%, 0, 0);\n background: var(--component-bg);\n border-radius: ","px;\n padding: ","px;\n z-index: 1000;\n\n &:empty {\n display: none;\n }\n"])),t=>{let{theme:e}=t;return e.fonts.text.family},t=>{let{theme:e}=t;return e.space[0]},t=>{let{theme:e}=t;return e.radius[2]},t=>{let{theme:e}=t;return e.space[2]}),T=O.div(o||(o=s(["\n color: #ccc;\n position: absolute;\n top: ","px;\n left: ","px;\n\n & > div {\n margin: 5px 0;\n }\n"])),t=>{let{theme:e}=t;return e.space[4]},t=>{let{theme:e}=t;return e.space[4]}),W=O.div(i||(i=s(["\n display: flex;\n"]))),C=O.div(r||(r=s(["\n width: 1.25em;\n height: 1.25em;\n background: currentColor;\n border-radius: 50%;\n margin-right: ","px;\n"])),t=>{let{theme:e}=t;return e.space[2]});function D(t){return null===t?0:"object"==typeof t?Object.entries(t).reduce((t,e)=>{let[n,o]=e;return t+D(n)+D(o)},0):Array.isArray(t)?Object.entries(t).reduce((t,e)=>t+D(e),0):"string"==typeof t?t.length:1}function V(t,e){return t.length>e?"".concat(t.substring(0,e),"…"):t}const z=p(0,.9,1,1),B=p(.25,.1,0,1);function N(t){return(t.substring(0,1).toUpperCase()+t.substring(1)).replace(/\./g," ").replace(/[A-Z]/g," $&").trim()}function H(t){const e={};for(const n of t)e[n._type]=(e[n._type]||0)+1;return e}function R(t){return"".concat(t.title||t.name||t._id).trim()}function U(t,e){return 5+D(t)/e*100}function E(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(null!==t)if("object"==typeof t)for(const[n,o]of Object.entries(t))"_ref"===n&&"string"==typeof o&&o.length>0&&e.push(F(o)),E(o,e);else if(Array.isArray(t))for(const n of t)E(n,e);return e}function F(t){return t.replace(/^drafts\./,"")}class G{constructor(){this.id=null,this.user=null,this.doc=null,this.lastActive=0,this.startTime=0,this.angle=0}}class L{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.sessions=[];const e={};for(const n of t)e[n._id]=n;this.data={nodes:t.map(t=>Object.assign({id:t._id,type:"document",doc:t})),links:t.flatMap(t=>E(t).map(e=>({source:t._id,target:e}))).filter(t=>e[t.source]&&e[t.target])}}setSession(t,e){let n=this.sessions.find(n=>{var o;return n.user.id===t.id&&(null==(o=n.doc)?void 0:o._id)===e.doc._id});n||(n=new G,n.id=_(),n.user=t,n.startTime=Date.now(),n.doc=e.doc,n.angle=2*Math.random()*Math.PI,this.sessions.push(n)),n.lastActive=Date.now()}reapSessions(){for(let t=0;t<this.sessions.length;t++){const e=this.sessions[t];Date.now()-e.lastActive>1e4&&(this.sessions=[...this.sessions.slice(0,t),...this.sessions.slice(t+1)],t--)}}clone(){const t=new L;return Object.assign(t,this),t.data={nodes:[...this.data.nodes],links:[...this.data.links]},t}}const Z=new class{constructor(){this._users=[]}async getById(t,e){let n=this._users.find(e=>e._id===t);var o,i,r;return n||(n=await e.users.getById(t),this._users.push(n),n.image=await(o=n.imageUrl||"https://raw.githubusercontent.com/sanity-io/sanity-plugin-graph-view/main/assets/head-silhouette.jpg",i=40,r=40,new Promise(t=>{const e=new Image(i,r);e.onload=()=>{t(e)},e.onerror=e=>{console.log("Image error",e),t(null)},e.src=o}))),n}};function $(t){var e;const n=t.query||'\n *[\n !(_id in path("_.*")) &&\n !(_type match "system.*") &&\n !(_type match "sanity.*")\n ]\n',o=null!=(e=t.apiVersion)?e:"2022-09-01",i=c(),[r,s]=b(0),[h,f]=b(null),[p,_]=b([]),[I,j]=b({}),[O,G]=b(()=>new L),$=u(),J=d({apiVersion:o}),K=v(t=>{const e=function(t){const e={};for(const n of t)n._id.startsWith("drafts.")||(e[n._id]=n);for(const n of t)if(n._id.startsWith("drafts.")){const t=F(n._id);e[t]=Object.assign(n,{_id:t})}return Object.values(e)}(t);s(Math.max(...e.map(D))),_(e),j(H(e)),G(new L(e))},[]),X=v(async t=>{const e=t.result;if(e){e._id=F(e._id);const n={};for(const t of p)n[t._id]=t;let o;const i=[...p],r=p.findIndex(t=>t._id===e._id);r>=0?(o=i[r],i[r]=e):i.push(e),_(i),j(H(i)),s(Math.max(...i.map(D)));const a=O.clone(),l=E(o||{}).filter(t=>t===e._id||null!==n[t]),c=E(e).filter(t=>t===e._id||null!==n[t]);let d,h=!m(l,c);h&&(a.data.links=a.data.links.filter(t=>t.source.id!==e._id).concat(c.map(t=>({source:e._id,target:t}))));const u=O.data.nodes.findIndex(t=>t.doc&&t.doc._id===e._id);u>=0?(d=O.data.nodes[u],d.doc=e):(d={id:e._id,type:"document",doc:e},a.data.nodes.push(d),h=!0),h&&G(a);const f=await Z.getById(t.identity,J);O.setSession(f,d)}else if("disappear"===t.transition){const e=F(t.documentId),n=p.filter(t=>t._id!==e);_(n),j(H(n)),s(Math.max(...n.map(D)));const o=O.clone();o.data.links=o.data.links.filter(t=>t.source.id!==e&&t.target.id!==e),o.data.nodes=o.data.nodes.filter(t=>t.id!==e),G(o)}},[p,O,J]);!function(t,e,n,o){g(()=>{o.fetch(t).then(t=>{e(t)})},[...n,o])}(n,K,[],J),function(t,e,n,o,i,r){g(()=>{const i=r.listen(t,e,n).subscribe(t=>{o(t)});return()=>{i.unsubscribe()}},[...i,r])}(n,{},{},X,[p,O],J),g(()=>{const t=setInterval(()=>O.reapSessions(),1e3);return()=>clearInterval(t)},[O]);const Y=P().sanity;return a(S,{theme:Y,children:l(q,{theme:Y,children:[a(T,{theme:Y,children:(tt=I,(et=Object.keys(tt),nt=t=>tt[t]||0,et.sort((t,e)=>{const n=nt(t),o=nt(e);return n<o?-1:n>o?1:0})).reverse().slice(0,10)).map(t=>l(W,{className:"legend__row",style:{color:Q(t).fill},children:[a(C,{theme:Y}),a("div",{children:N(t)})]},t))}),h&&a(A,{theme:Y,children:R(h.doc)}),a(x,{graphData:O.data,nodeAutoColorBy:"group",enableNodeDrag:!1,onNodeHover:t=>f(t),onNodeClick:t=>{$.navigateIntent("edit",{id:t.doc._id,documentType:t.doc._type})},linkColor:()=>y(k[500].hex,.25),nodeLabel:()=>"",nodeRelSize:1,nodeVal:t=>U(t.doc,r),onRenderFramePost:(t,e)=>{for(const n of O.sessions){const o=O.data.nodes.find(t=>{var e;return t.doc&&t.doc._id===(null==(e=null==n?void 0:n.doc)?void 0:e._id)});if(o){const s=1e4,a=n.angle,l=Math.sqrt(U(o.doc,r)),c=n.user.image,d=i.get(n.user.displayName).tints[400].hex,h=l*e+40,u=c?c.width:0,f=c?c.height:0,p=o.x+Math.sin(a)*h/e,m=o.y+Math.cos(a)*h/e;t.save();try{if(t.globalAlpha=z(1-Math.min(s,Date.now()-n.lastActive)/s),t.font="bold ".concat(Math.round(12/e),"px sans-serif"),t.beginPath(),t.strokeStyle=y(M.hex,1),t.lineWidth=2/e,t.moveTo(o.x+Math.sin(a)*(h-u/2)/e,o.y+Math.cos(a)*(h-f/2)/e),t.lineTo(o.x+Math.sin(a)*l,o.y+Math.cos(a)*l),t.stroke(),t.beginPath(),t.strokeStyle=y(M.hex,1),t.lineWidth=2/e,t.arc(o.x,o.y,l,0,2*Math.PI,!1),t.stroke(),c){t.save();try{const o=700,i=B(Math.max(0,(o-(Date.now()-n.startTime))/o));i>0&&(t.beginPath(),t.fillStyle=y(d,i),t.arc(p,m,(u/2+10)/e,0,2*Math.PI,!1),t.fill()),t.beginPath(),t.fillStyle=y(M.hex,1),t.arc(p,m,u/e/2,0,2*Math.PI,!1),t.clip(),t.drawImage(c,p-u/e/2,m-f/e/2,u/e,f/e),t.strokeStyle=w.hex,t.lineWidth=6/e,t.stroke(),t.strokeStyle=d,t.lineWidth=4/e,t.stroke()}finally{t.restore()}}t.beginPath(),t.strokeStyle=y(w.hex,1),t.lineWidth=.5/e,t.arc(p,m,u/e/2,0,2*Math.PI,!1),t.stroke();const i=a>=Math.PI/2&&a<1.5*Math.PI,r=i?m-(f/2+5)/e:m+(f/2+5)/e;t.fillStyle=y(M.hex,1),t.textAlign="center",t.textBaseline=i?"bottom":"top",t.fillText(n.user.displayName,p,r)}finally{t.restore()}}}},nodeCanvasObject:(t,e,n)=>{if("document"===t.type){const o=Q(t.doc._type),i=Math.sqrt(U(t.doc,r)),s=Math.min(100,10/n);if(e.beginPath(),e.fillStyle=null!==h&&t.doc._id===h.doc._id?y(k[500].hex,.8):o.fill,e.strokeStyle=o.border,e.lineWidth=.5,e.arc(t.x,t.y,i,0,2*Math.PI,!1),e.stroke(),e.fill(),i*n>10){e.font="".concat(s,"px sans-serif");const o=2*i+30/n;for(let r=50;r>=5;r/=1.2){const s=V(R(t.doc),Math.round(r));if(e.measureText(s).width<o){e.textAlign="center",e.textBaseline="top",e.strokeStyle=y(w.hex,.5),e.lineWidth=2/n,e.strokeText(s,t.x,t.y+i+5/n),e.fillText(s,t.x,t.y+i+5/n);break}}}}},linkCanvasObject:(t,e,n)=>{e.beginPath(),e.strokeStyle=y(k[500].hex,.125),e.lineWidth=2/n,e.moveTo(t.source.x,t.source.y),e.lineTo(t.target.x,t.target.y),e.stroke()}})]})});var tt,et,nt}const J={};let K=0;function Q(t){if(J[t])return J[t];const e=j[K%j.length];return K+=1,J[t]={fill:I[e][400].hex,border:y(w.hex,.5)},J[t]}const X=()=>a("svg",{xmlns:"http://www.w3.org/2000/svg",width:"1em",height:"1em",viewBox:"0 0 46.063 46.063",children:a("path",{fill:"currentColor",d:"M18.022 38.676v2.813h-1.21q-4.864 0-6.525-1.447-1.64-1.445-1.64-5.762v-4.666q0-2.95-1.055-4.083-1.055-1.13-3.828-1.13h-1.19v-2.794h1.19q2.793 0 3.828-1.114 1.055-1.132 1.055-4.043V11.76q0-4.316 1.64-5.742 1.66-1.446 6.524-1.446h1.212v2.793h-1.328q-2.754 0-3.594.86-.84.86-.84 3.613v4.844q0 3.066-.898 4.453-.88 1.387-3.028 1.875 2.168.527 3.047 1.914.88 1.387.88 4.434v4.843q0 2.754.84 3.614.84.86 3.594.86h1.328zM28.04 38.676h1.368q2.735 0 3.555-.84.84-.84.84-3.633V29.36q0-3.047.88-4.434.878-1.387 3.046-1.914-2.17-.488-3.048-1.875-.88-1.387-.88-4.453V11.84q0-2.773-.84-3.613-.82-.86-3.554-.86H28.04V4.574h1.232q4.863 0 6.484 1.446 1.64 1.426 1.64 5.742v4.687q0 2.91 1.055 4.042 1.056 1.114 3.83 1.114h1.21V24.4h-1.21q-2.774 0-3.83 1.13-1.053 1.134-1.053 4.084v4.667q0 4.318-1.64 5.763-1.622 1.446-6.485 1.446h-1.23v-2.814z"})});var Y=Object.defineProperty,tt=Object.getOwnPropertySymbols,et=Object.prototype.hasOwnProperty,nt=Object.prototype.propertyIsEnumerable,ot=(t,e,n)=>e in t?Y(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;const it=h(function(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return{name:"@sanity/content-graph-view",tools:e=>[...e,{name:"graph-your-content",title:"Graph",icon:X,component:function(){return a($,((t,e)=>{for(var n in e||(e={}))et.call(e,n)&&ot(t,n,e[n]);if(tt)for(var n of tt(e))nt.call(e,n)&&ot(t,n,e[n]);return t})({},t))},router:f.create("/:selectedDocumentId")}]}});export{it as contentGraphView};//# sourceMappingURL=index.esm.js.map
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t,n,r,s,i;function o(e,t){return t||(t=e.slice(0)),Object.freeze(Object.defineProperties(e,{raw:{value:Object.freeze(t)}}))}Object.defineProperty(exports,"__esModule",{value:!0});var a=require("react/jsx-runtime"),l=require("sanity"),c=require("sanity/router"),d=require("bezier-easing"),u=require("deep-equal"),h=require("polished"),f=require("react"),p=require("react-force-graph"),g=require("uuid"),y=require("@sanity/color"),m=require("@sanity/ui"),b=require("styled-components");function x(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var v=x(d),_=x(u),w=x(b);const k=w.default.div(e||(e=o(["\n font-family: ",";\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: ",";\n"])),e=>{let{theme:t}=e;return t.fonts.text.family},y.black.hex),j=w.default.div(t||(t=o(["\n position: relative;\n width: 100%;\n height: 100%;\n"]))),q=w.default.div(n||(n=o(["\n font-family: ",";\n display: none;\n position: absolute;\n bottom: ","px;\n left: 50%;\n transform: translate3d(-50%, 0, 0);\n background: var(--component-bg);\n border-radius: ","px;\n padding: ","px;\n z-index: 1000;\n\n &:empty {\n display: none;\n }\n"])),e=>{let{theme:t}=e;return t.fonts.text.family},e=>{let{theme:t}=e;return t.space[0]},e=>{let{theme:t}=e;return t.radius[2]},e=>{let{theme:t}=e;return t.space[2]}),M=w.default.div(r||(r=o(["\n color: #ccc;\n position: absolute;\n top: ","px;\n left: ","px;\n\n & > div {\n margin: 5px 0;\n }\n"])),e=>{let{theme:t}=e;return t.space[4]},e=>{let{theme:t}=e;return t.space[4]}),S=w.default.div(s||(s=o(["\n display: flex;\n"]))),O=w.default.div(i||(i=o(["\n width: 1.25em;\n height: 1.25em;\n background: currentColor;\n border-radius: 50%;\n margin-right: ","px;\n"])),e=>{let{theme:t}=e;return t.space[2]});function P(e){return null===e?0:Array.isArray(e)?Object.entries(e).reduce((e,t)=>e+P(t),0):"object"==typeof e?Object.entries(e).reduce((e,t)=>{let[n,r]=t;return e+P(n)+P(r)},0):"string"==typeof e?e.length:1}function I(e,t){return e.length>t?"".concat(e.substring(0,t),"…"):e}const C=v.default(0,.9,1,1),A=v.default(.25,.1,0,1);function T(e){return(e.substring(0,1).toUpperCase()+e.substring(1)).replace(/\./g," ").replace(/[A-Z]/g," $&").trim()}function W(e){const t={};for(const n of e)t[n._type]=(t[n._type]||0)+1;return t}function D(e){return"".concat(e.title||e.name||e._id).trim()}function V(e,t){return 5+P(e)/t*100}function z(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(null!==e)if("object"==typeof e)for(const[n,r]of Object.entries(e))"_ref"===n&&"string"==typeof r&&r.length>0&&t.push(B(r)),z(r,t);else if(Array.isArray(e))for(const n of e)z(n,t);return t}function B(e){return e.replace(/^drafts\./,"")}class E{constructor(){this.id=null,this.user=null,this.doc=null,this.lastActive=0,this.startTime=0,this.angle=0}}class N{constructor(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.sessions=[];const t={};for(const n of e)t[n._id]=n;this.data={nodes:e.map(e=>Object.assign({id:e._id,type:"document",doc:e})),links:e.flatMap(e=>z(e).map(t=>({source:e._id,target:t}))).filter(e=>t[e.source]&&t[e.target])}}setSession(e,t){let n=this.sessions.find(n=>{var r;return n.user.id===e.id&&(null==(r=n.doc)?void 0:r._id)===t.doc._id});n||(n=new E,n.id=g.v4(),n.user=e,n.startTime=Date.now(),n.doc=t.doc,n.angle=2*Math.random()*Math.PI,this.sessions.push(n)),n.lastActive=Date.now()}reapSessions(){for(let e=0;e<this.sessions.length;e++){const t=this.sessions[e];Date.now()-t.lastActive>1e4&&(this.sessions=[...this.sessions.slice(0,e),...this.sessions.slice(e+1)],e--)}}clone(){const e=new N;return Object.assign(e,this),e.data={nodes:[...this.data.nodes],links:[...this.data.links]},e}}const R=new class{constructor(){this._users=[]}async getById(e,t){let n=this._users.find(t=>t._id===e);var r,s,i;return n||(n=await t.users.getById(e),this._users.push(n),n.image=await(r=n.imageUrl||"https://raw.githubusercontent.com/sanity-io/sanity-plugin-graph-view/main/assets/head-silhouette.jpg",s=40,i=40,new Promise(e=>{const t=new Image(s,i);t.onload=()=>{e(t)},t.onerror=t=>{console.log("Image error",t),e(null)},t.src=r}))),n}};function U(e){var t;const n=e.query||'\n *[\n !(_id in path("_.*")) &&\n !(_type match "system.*") &&\n !(_type match "sanity.*")\n ]\n',r=null!=(t=e.apiVersion)?t:"2022-09-01",s=l.useUserColorManager(),[i,o]=f.useState(0),[d,u]=f.useState(null),[g,b]=f.useState([]),[x,v]=f.useState({}),[w,E]=f.useState(()=>new N),U=c.useRouter(),H=l.useClient({apiVersion:r}),G=f.useCallback(e=>{const t=function(e){const t={};for(const n of e)n._id.startsWith("drafts.")||(t[n._id]=n);for(const n of e)if(n._id.startsWith("drafts.")){const e=B(n._id);t[e]=Object.assign(n,{_id:e})}return Object.values(t)}(e);o(Math.max(...t.map(P))),b(t),v(W(t)),E(new N(t))},[]),F=f.useCallback(async e=>{const t=e.result;if(t){t._id=B(t._id);const n={};for(const e of g)n[e._id]=e;let r;const s=[...g],i=g.findIndex(e=>e._id===t._id);i>=0?(r=s[i],s[i]=t):s.push(t),b(s),v(W(s)),o(Math.max(...s.map(P)));const a=w.clone(),l=z(r||{}).filter(e=>e===t._id||null!==n[e]),c=z(t).filter(e=>e===t._id||null!==n[e]);let d,u=!_.default(l,c);u&&(a.data.links=a.data.links.filter(e=>e.source.id!==t._id).concat(c.map(e=>({source:t._id,target:e}))));const h=w.data.nodes.findIndex(e=>e.doc&&e.doc._id===t._id);h>=0?(d=w.data.nodes[h],d.doc=t):(d={id:t._id,type:"document",doc:t},a.data.nodes.push(d),u=!0),u&&E(a);const f=await R.getById(e.identity,H);w.setSession(f,d)}else if("disappear"===e.transition){const t=B(e.documentId),n=g.filter(e=>e._id!==t);b(n),v(W(n)),o(Math.max(...n.map(P)));const r=w.clone();r.data.links=r.data.links.filter(e=>e.source.id!==t&&e.target.id!==t),r.data.nodes=r.data.nodes.filter(e=>e.id!==t),E(r)}},[g,w,H]);!function(e,t,n,r){f.useEffect(()=>{r.fetch(e).then(e=>{t(e)})},[...n,r])}(n,G,[],H),function(e,t,n,r,s,i){f.useEffect(()=>{const s=i.listen(e,t,n).subscribe(e=>{r(e)});return()=>{s.unsubscribe()}},[...s,i])}(n,{},{},F,[g,w],H),f.useEffect(()=>{const e=setInterval(()=>w.reapSessions(),1e3);return()=>clearInterval(e)},[w]);const Z=m.useTheme().sanity;return a.jsx(j,{theme:Z,children:a.jsxs(k,{theme:Z,children:[a.jsx(M,{theme:Z,children:($=x,(J=Object.keys($),K=e=>$[e]||0,J.sort((e,t)=>{const n=K(e),r=K(t);return n<r?-1:n>r?1:0})).reverse().slice(0,10)).map(e=>a.jsxs(S,{className:"legend__row",style:{color:L(e).fill},children:[a.jsx(O,{theme:Z}),a.jsx("div",{children:T(e)})]},e))}),d&&a.jsx(q,{theme:Z,children:D(d.doc)}),a.jsx(p.ForceGraph2D,{graphData:w.data,nodeAutoColorBy:"group",enableNodeDrag:!1,onNodeHover:e=>u(e),onNodeClick:e=>{U.navigateIntent("edit",{id:e.doc._id,documentType:e.doc._type})},linkColor:()=>h.rgba(y.gray[500].hex,.25),nodeLabel:()=>"",nodeRelSize:1,nodeVal:e=>V(e.doc,i),onRenderFramePost:(e,t)=>{for(const n of w.sessions){const r=w.data.nodes.find(e=>{var t;return e.doc&&e.doc._id===(null==(t=null==n?void 0:n.doc)?void 0:t._id)});if(r){const o=1e4,a=n.angle,l=Math.sqrt(V(r.doc,i)),c=n.user.image,d=s.get(n.user.displayName).tints[400].hex,u=l*t+40,f=c?c.width:0,p=c?c.height:0,g=r.x+Math.sin(a)*u/t,m=r.y+Math.cos(a)*u/t;e.save();try{if(e.globalAlpha=C(1-Math.min(o,Date.now()-n.lastActive)/o),e.font="bold ".concat(Math.round(12/t),"px sans-serif"),e.beginPath(),e.strokeStyle=h.rgba(y.white.hex,1),e.lineWidth=2/t,e.moveTo(r.x+Math.sin(a)*(u-f/2)/t,r.y+Math.cos(a)*(u-p/2)/t),e.lineTo(r.x+Math.sin(a)*l,r.y+Math.cos(a)*l),e.stroke(),e.beginPath(),e.strokeStyle=h.rgba(y.white.hex,1),e.lineWidth=2/t,e.arc(r.x,r.y,l,0,2*Math.PI,!1),e.stroke(),c){e.save();try{const r=700,s=A(Math.max(0,(r-(Date.now()-n.startTime))/r));s>0&&(e.beginPath(),e.fillStyle=h.rgba(d,s),e.arc(g,m,(f/2+10)/t,0,2*Math.PI,!1),e.fill()),e.beginPath(),e.fillStyle=h.rgba(y.white.hex,1),e.arc(g,m,f/t/2,0,2*Math.PI,!1),e.clip(),e.drawImage(c,g-f/t/2,m-p/t/2,f/t,p/t),e.strokeStyle=y.black.hex,e.lineWidth=6/t,e.stroke(),e.strokeStyle=d,e.lineWidth=4/t,e.stroke()}finally{e.restore()}}e.beginPath(),e.strokeStyle=h.rgba(y.black.hex,1),e.lineWidth=.5/t,e.arc(g,m,f/t/2,0,2*Math.PI,!1),e.stroke();const s=a>=Math.PI/2&&a<1.5*Math.PI,i=s?m-(p/2+5)/t:m+(p/2+5)/t;e.fillStyle=h.rgba(y.white.hex,1),e.textAlign="center",e.textBaseline=s?"bottom":"top",e.fillText(n.user.displayName,g,i)}finally{e.restore()}}}},nodeCanvasObject:(e,t,n)=>{if("document"===e.type){const r=L(e.doc._type),s=Math.sqrt(V(e.doc,i)),o=Math.min(100,10/n);if(t.beginPath(),t.fillStyle=null!==d&&e.doc._id===d.doc._id?h.rgba(y.gray[500].hex,.8):r.fill,t.strokeStyle=r.border,t.lineWidth=.5,t.arc(e.x,e.y,s,0,2*Math.PI,!1),t.stroke(),t.fill(),s*n>10){t.font="".concat(o,"px sans-serif");const r=2*s+30/n;for(let i=50;i>=5;i/=1.2){const o=I(D(e.doc),Math.round(i));if(t.measureText(o).width<r){t.textAlign="center",t.textBaseline="top",t.strokeStyle=h.rgba(y.black.hex,.5),t.lineWidth=2/n,t.strokeText(o,e.x,e.y+s+5/n),t.fillText(o,e.x,e.y+s+5/n);break}}}}},linkCanvasObject:(e,t,n)=>{t.beginPath(),t.strokeStyle=h.rgba(y.gray[500].hex,.125),t.lineWidth=2/n,t.moveTo(e.source.x,e.source.y),t.lineTo(e.target.x,e.target.y),t.stroke()}})]})});var $,J,K}const H={};let G=0;function L(e){if(H[e])return H[e];const t=y.COLOR_HUES[G%y.COLOR_HUES.length];return G+=1,H[e]={fill:y.hues[t][400].hex,border:h.rgba(y.black.hex,.5)},H[e]}const F=()=>a.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"1em",height:"1em",viewBox:"0 0 46.063 46.063",children:a.jsx("path",{fill:"currentColor",d:"M18.022 38.676v2.813h-1.21q-4.864 0-6.525-1.447-1.64-1.445-1.64-5.762v-4.666q0-2.95-1.055-4.083-1.055-1.13-3.828-1.13h-1.19v-2.794h1.19q2.793 0 3.828-1.114 1.055-1.132 1.055-4.043V11.76q0-4.316 1.64-5.742 1.66-1.446 6.524-1.446h1.212v2.793h-1.328q-2.754 0-3.594.86-.84.86-.84 3.613v4.844q0 3.066-.898 4.453-.88 1.387-3.028 1.875 2.168.527 3.047 1.914.88 1.387.88 4.434v4.843q0 2.754.84 3.614.84.86 3.594.86h1.328zM28.04 38.676h1.368q2.735 0 3.555-.84.84-.84.84-3.633V29.36q0-3.047.88-4.434.878-1.387 3.046-1.914-2.17-.488-3.048-1.875-.88-1.387-.88-4.453V11.84q0-2.773-.84-3.613-.82-.86-3.554-.86H28.04V4.574h1.232q4.863 0 6.484 1.446 1.64 1.426 1.64 5.742v4.687q0 2.91 1.055 4.042 1.056 1.114 3.83 1.114h1.21V24.4h-1.21q-2.774 0-3.83 1.13-1.053 1.134-1.053 4.084v4.667q0 4.318-1.64 5.763-1.622 1.446-6.485 1.446h-1.23v-2.814z"})});var Z=Object.defineProperty,$=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable,Q=(e,t,n)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;const X=l.definePlugin(function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return{name:"@sanity/content-graph-view",tools:t=>[...t,{name:"graph-your-content",title:"Graph",icon:F,component:function(){return a.jsx(U,((e,t)=>{for(var n in t||(t={}))J.call(t,n)&&Q(e,n,t[n]);if($)for(var n of $(t))K.call(t,n)&&Q(e,n,t[n]);return e})({},e))},router:c.route.create("/:selectedDocumentId")}]}});exports.contentGraphView=X;//# sourceMappingURL=index.js.map
|
|
1
|
+
"use strict";var e,t,n,r,s,i;function o(e,t){return t||(t=e.slice(0)),Object.freeze(Object.defineProperties(e,{raw:{value:Object.freeze(t)}}))}Object.defineProperty(exports,"__esModule",{value:!0});var a=require("react/jsx-runtime"),l=require("sanity"),c=require("sanity/router"),d=require("bezier-easing"),u=require("deep-equal"),h=require("polished"),f=require("react"),p=require("react-force-graph"),g=require("uuid"),y=require("@sanity/color"),m=require("@sanity/ui"),b=require("styled-components");function x(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var v=x(d),_=x(u),w=x(b);const k=w.default.div(e||(e=o(["\n font-family: ",";\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: ",";\n"])),e=>{let{theme:t}=e;return t.fonts.text.family},y.black.hex),j=w.default.div(t||(t=o(["\n position: relative;\n width: 100%;\n height: 100%;\n"]))),q=w.default.div(n||(n=o(["\n font-family: ",";\n display: none;\n position: absolute;\n bottom: ","px;\n left: 50%;\n transform: translate3d(-50%, 0, 0);\n background: var(--component-bg);\n border-radius: ","px;\n padding: ","px;\n z-index: 1000;\n\n &:empty {\n display: none;\n }\n"])),e=>{let{theme:t}=e;return t.fonts.text.family},e=>{let{theme:t}=e;return t.space[0]},e=>{let{theme:t}=e;return t.radius[2]},e=>{let{theme:t}=e;return t.space[2]}),M=w.default.div(r||(r=o(["\n color: #ccc;\n position: absolute;\n top: ","px;\n left: ","px;\n\n & > div {\n margin: 5px 0;\n }\n"])),e=>{let{theme:t}=e;return t.space[4]},e=>{let{theme:t}=e;return t.space[4]}),S=w.default.div(s||(s=o(["\n display: flex;\n"]))),O=w.default.div(i||(i=o(["\n width: 1.25em;\n height: 1.25em;\n background: currentColor;\n border-radius: 50%;\n margin-right: ","px;\n"])),e=>{let{theme:t}=e;return t.space[2]});function P(e){return null===e?0:"object"==typeof e?Object.entries(e).reduce((e,t)=>{let[n,r]=t;return e+P(n)+P(r)},0):Array.isArray(e)?Object.entries(e).reduce((e,t)=>e+P(t),0):"string"==typeof e?e.length:1}function I(e,t){return e.length>t?"".concat(e.substring(0,t),"…"):e}const C=v.default(0,.9,1,1),A=v.default(.25,.1,0,1);function T(e){return(e.substring(0,1).toUpperCase()+e.substring(1)).replace(/\./g," ").replace(/[A-Z]/g," $&").trim()}function W(e){const t={};for(const n of e)t[n._type]=(t[n._type]||0)+1;return t}function D(e){return"".concat(e.title||e.name||e._id).trim()}function V(e,t){return 5+P(e)/t*100}function z(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];if(null!==e)if("object"==typeof e)for(const[n,r]of Object.entries(e))"_ref"===n&&"string"==typeof r&&r.length>0&&t.push(B(r)),z(r,t);else if(Array.isArray(e))for(const n of e)z(n,t);return t}function B(e){return e.replace(/^drafts\./,"")}class E{constructor(){this.id=null,this.user=null,this.doc=null,this.lastActive=0,this.startTime=0,this.angle=0}}class N{constructor(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.sessions=[];const t={};for(const n of e)t[n._id]=n;this.data={nodes:e.map(e=>Object.assign({id:e._id,type:"document",doc:e})),links:e.flatMap(e=>z(e).map(t=>({source:e._id,target:t}))).filter(e=>t[e.source]&&t[e.target])}}setSession(e,t){let n=this.sessions.find(n=>{var r;return n.user.id===e.id&&(null==(r=n.doc)?void 0:r._id)===t.doc._id});n||(n=new E,n.id=g.v4(),n.user=e,n.startTime=Date.now(),n.doc=t.doc,n.angle=2*Math.random()*Math.PI,this.sessions.push(n)),n.lastActive=Date.now()}reapSessions(){for(let e=0;e<this.sessions.length;e++){const t=this.sessions[e];Date.now()-t.lastActive>1e4&&(this.sessions=[...this.sessions.slice(0,e),...this.sessions.slice(e+1)],e--)}}clone(){const e=new N;return Object.assign(e,this),e.data={nodes:[...this.data.nodes],links:[...this.data.links]},e}}const R=new class{constructor(){this._users=[]}async getById(e,t){let n=this._users.find(t=>t._id===e);var r,s,i;return n||(n=await t.users.getById(e),this._users.push(n),n.image=await(r=n.imageUrl||"https://raw.githubusercontent.com/sanity-io/sanity-plugin-graph-view/main/assets/head-silhouette.jpg",s=40,i=40,new Promise(e=>{const t=new Image(s,i);t.onload=()=>{e(t)},t.onerror=t=>{console.log("Image error",t),e(null)},t.src=r}))),n}};function U(e){var t;const n=e.query||'\n *[\n !(_id in path("_.*")) &&\n !(_type match "system.*") &&\n !(_type match "sanity.*")\n ]\n',r=null!=(t=e.apiVersion)?t:"2022-09-01",s=l.useUserColorManager(),[i,o]=f.useState(0),[d,u]=f.useState(null),[g,b]=f.useState([]),[x,v]=f.useState({}),[w,E]=f.useState(()=>new N),U=c.useRouter(),H=l.useClient({apiVersion:r}),G=f.useCallback(e=>{const t=function(e){const t={};for(const n of e)n._id.startsWith("drafts.")||(t[n._id]=n);for(const n of e)if(n._id.startsWith("drafts.")){const e=B(n._id);t[e]=Object.assign(n,{_id:e})}return Object.values(t)}(e);o(Math.max(...t.map(P))),b(t),v(W(t)),E(new N(t))},[]),F=f.useCallback(async e=>{const t=e.result;if(t){t._id=B(t._id);const n={};for(const e of g)n[e._id]=e;let r;const s=[...g],i=g.findIndex(e=>e._id===t._id);i>=0?(r=s[i],s[i]=t):s.push(t),b(s),v(W(s)),o(Math.max(...s.map(P)));const a=w.clone(),l=z(r||{}).filter(e=>e===t._id||null!==n[e]),c=z(t).filter(e=>e===t._id||null!==n[e]);let d,u=!_.default(l,c);u&&(a.data.links=a.data.links.filter(e=>e.source.id!==t._id).concat(c.map(e=>({source:t._id,target:e}))));const h=w.data.nodes.findIndex(e=>e.doc&&e.doc._id===t._id);h>=0?(d=w.data.nodes[h],d.doc=t):(d={id:t._id,type:"document",doc:t},a.data.nodes.push(d),u=!0),u&&E(a);const f=await R.getById(e.identity,H);w.setSession(f,d)}else if("disappear"===e.transition){const t=B(e.documentId),n=g.filter(e=>e._id!==t);b(n),v(W(n)),o(Math.max(...n.map(P)));const r=w.clone();r.data.links=r.data.links.filter(e=>e.source.id!==t&&e.target.id!==t),r.data.nodes=r.data.nodes.filter(e=>e.id!==t),E(r)}},[g,w,H]);!function(e,t,n,r){f.useEffect(()=>{r.fetch(e).then(e=>{t(e)})},[...n,r])}(n,G,[],H),function(e,t,n,r,s,i){f.useEffect(()=>{const s=i.listen(e,t,n).subscribe(e=>{r(e)});return()=>{s.unsubscribe()}},[...s,i])}(n,{},{},F,[g,w],H),f.useEffect(()=>{const e=setInterval(()=>w.reapSessions(),1e3);return()=>clearInterval(e)},[w]);const Z=m.useTheme().sanity;return a.jsx(j,{theme:Z,children:a.jsxs(k,{theme:Z,children:[a.jsx(M,{theme:Z,children:($=x,(J=Object.keys($),K=e=>$[e]||0,J.sort((e,t)=>{const n=K(e),r=K(t);return n<r?-1:n>r?1:0})).reverse().slice(0,10)).map(e=>a.jsxs(S,{className:"legend__row",style:{color:L(e).fill},children:[a.jsx(O,{theme:Z}),a.jsx("div",{children:T(e)})]},e))}),d&&a.jsx(q,{theme:Z,children:D(d.doc)}),a.jsx(p.ForceGraph2D,{graphData:w.data,nodeAutoColorBy:"group",enableNodeDrag:!1,onNodeHover:e=>u(e),onNodeClick:e=>{U.navigateIntent("edit",{id:e.doc._id,documentType:e.doc._type})},linkColor:()=>h.rgba(y.gray[500].hex,.25),nodeLabel:()=>"",nodeRelSize:1,nodeVal:e=>V(e.doc,i),onRenderFramePost:(e,t)=>{for(const n of w.sessions){const r=w.data.nodes.find(e=>{var t;return e.doc&&e.doc._id===(null==(t=null==n?void 0:n.doc)?void 0:t._id)});if(r){const o=1e4,a=n.angle,l=Math.sqrt(V(r.doc,i)),c=n.user.image,d=s.get(n.user.displayName).tints[400].hex,u=l*t+40,f=c?c.width:0,p=c?c.height:0,g=r.x+Math.sin(a)*u/t,m=r.y+Math.cos(a)*u/t;e.save();try{if(e.globalAlpha=C(1-Math.min(o,Date.now()-n.lastActive)/o),e.font="bold ".concat(Math.round(12/t),"px sans-serif"),e.beginPath(),e.strokeStyle=h.rgba(y.white.hex,1),e.lineWidth=2/t,e.moveTo(r.x+Math.sin(a)*(u-f/2)/t,r.y+Math.cos(a)*(u-p/2)/t),e.lineTo(r.x+Math.sin(a)*l,r.y+Math.cos(a)*l),e.stroke(),e.beginPath(),e.strokeStyle=h.rgba(y.white.hex,1),e.lineWidth=2/t,e.arc(r.x,r.y,l,0,2*Math.PI,!1),e.stroke(),c){e.save();try{const r=700,s=A(Math.max(0,(r-(Date.now()-n.startTime))/r));s>0&&(e.beginPath(),e.fillStyle=h.rgba(d,s),e.arc(g,m,(f/2+10)/t,0,2*Math.PI,!1),e.fill()),e.beginPath(),e.fillStyle=h.rgba(y.white.hex,1),e.arc(g,m,f/t/2,0,2*Math.PI,!1),e.clip(),e.drawImage(c,g-f/t/2,m-p/t/2,f/t,p/t),e.strokeStyle=y.black.hex,e.lineWidth=6/t,e.stroke(),e.strokeStyle=d,e.lineWidth=4/t,e.stroke()}finally{e.restore()}}e.beginPath(),e.strokeStyle=h.rgba(y.black.hex,1),e.lineWidth=.5/t,e.arc(g,m,f/t/2,0,2*Math.PI,!1),e.stroke();const s=a>=Math.PI/2&&a<1.5*Math.PI,i=s?m-(p/2+5)/t:m+(p/2+5)/t;e.fillStyle=h.rgba(y.white.hex,1),e.textAlign="center",e.textBaseline=s?"bottom":"top",e.fillText(n.user.displayName,g,i)}finally{e.restore()}}}},nodeCanvasObject:(e,t,n)=>{if("document"===e.type){const r=L(e.doc._type),s=Math.sqrt(V(e.doc,i)),o=Math.min(100,10/n);if(t.beginPath(),t.fillStyle=null!==d&&e.doc._id===d.doc._id?h.rgba(y.gray[500].hex,.8):r.fill,t.strokeStyle=r.border,t.lineWidth=.5,t.arc(e.x,e.y,s,0,2*Math.PI,!1),t.stroke(),t.fill(),s*n>10){t.font="".concat(o,"px sans-serif");const r=2*s+30/n;for(let i=50;i>=5;i/=1.2){const o=I(D(e.doc),Math.round(i));if(t.measureText(o).width<r){t.textAlign="center",t.textBaseline="top",t.strokeStyle=h.rgba(y.black.hex,.5),t.lineWidth=2/n,t.strokeText(o,e.x,e.y+s+5/n),t.fillText(o,e.x,e.y+s+5/n);break}}}}},linkCanvasObject:(e,t,n)=>{t.beginPath(),t.strokeStyle=h.rgba(y.gray[500].hex,.125),t.lineWidth=2/n,t.moveTo(e.source.x,e.source.y),t.lineTo(e.target.x,e.target.y),t.stroke()}})]})});var $,J,K}const H={};let G=0;function L(e){if(H[e])return H[e];const t=y.COLOR_HUES[G%y.COLOR_HUES.length];return G+=1,H[e]={fill:y.hues[t][400].hex,border:h.rgba(y.black.hex,.5)},H[e]}const F=()=>a.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"1em",height:"1em",viewBox:"0 0 46.063 46.063",children:a.jsx("path",{fill:"currentColor",d:"M18.022 38.676v2.813h-1.21q-4.864 0-6.525-1.447-1.64-1.445-1.64-5.762v-4.666q0-2.95-1.055-4.083-1.055-1.13-3.828-1.13h-1.19v-2.794h1.19q2.793 0 3.828-1.114 1.055-1.132 1.055-4.043V11.76q0-4.316 1.64-5.742 1.66-1.446 6.524-1.446h1.212v2.793h-1.328q-2.754 0-3.594.86-.84.86-.84 3.613v4.844q0 3.066-.898 4.453-.88 1.387-3.028 1.875 2.168.527 3.047 1.914.88 1.387.88 4.434v4.843q0 2.754.84 3.614.84.86 3.594.86h1.328zM28.04 38.676h1.368q2.735 0 3.555-.84.84-.84.84-3.633V29.36q0-3.047.88-4.434.878-1.387 3.046-1.914-2.17-.488-3.048-1.875-.88-1.387-.88-4.453V11.84q0-2.773-.84-3.613-.82-.86-3.554-.86H28.04V4.574h1.232q4.863 0 6.484 1.446 1.64 1.426 1.64 5.742v4.687q0 2.91 1.055 4.042 1.056 1.114 3.83 1.114h1.21V24.4h-1.21q-2.774 0-3.83 1.13-1.053 1.134-1.053 4.084v4.667q0 4.318-1.64 5.763-1.622 1.446-6.485 1.446h-1.23v-2.814z"})});var Z=Object.defineProperty,$=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable,Q=(e,t,n)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;const X=l.definePlugin(function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return{name:"@sanity/content-graph-view",tools:t=>[...t,{name:"graph-your-content",title:"Graph",icon:F,component:function(){return a.jsx(U,((e,t)=>{for(var n in t||(t={}))J.call(t,n)&&Q(e,n,t[n]);if($)for(var n of $(t))K.call(t,n)&&Q(e,n,t[n]);return e})({},e))},router:c.route.create("/:selectedDocumentId")}]}});exports.contentGraphView=X;//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
package/src/tool/utils.ts
CHANGED
|
@@ -3,13 +3,14 @@ export function sizeOf(value: any): number {
|
|
|
3
3
|
return 0
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
if (typeof value === 'object') {
|
|
7
|
+
return Object.entries(value).reduce((total, [k, v]) => total + sizeOf(k) + sizeOf(v), 0)
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
if (Array.isArray(value)) {
|
|
7
11
|
return Object.entries(value).reduce((total, v) => total + sizeOf(v), 0)
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
if (typeof value === 'object') {
|
|
11
|
-
return Object.entries(value).reduce((total, [k, v]) => total + sizeOf(k) + sizeOf(v), 0)
|
|
12
|
-
}
|
|
13
14
|
if (typeof value === 'string') {
|
|
14
15
|
return value.length
|
|
15
16
|
}
|