@sanity/hierarchical-document-list 2.0.1 → 2.1.0

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 CHANGED
@@ -148,4 +148,4 @@ import*as e from"sanity";import{useClient as t,TextWithTone as n,useTimeAgo as r
148
148
  pointer-events: none;
149
149
  margin-top: -calc(var(--stroke-width) * 3);
150
150
  }
151
- `,Hr=e=>{const{lowerSiblingCounts:t,scaffoldBlockPxWidth:n,listIndex:r,swapDepth:o,swapFrom:i,swapLength:a,treeIndex:s}=e,c=t.map(((e,t)=>{if(e<0||s===r||t!==o)return null;let c="";c=r===(i||0)+(a||0)-1?"rst__highlightBottomLeftCorner":s===i?"rst__highlightTopLeftCorner":"rst__highlightLineVertical";return m("div",{style:{width:n,left:n*t},className:`rst__unclickable rst__absoluteLineBlock ${c||""}`,tabIndex:-1},t)}));return y(g,{children:[c,m(Vr,{})]})},Fr=e=>{const{children:t,lowerSiblingCounts:n,connectDropTarget:r,isOver:o,draggedNode:i,canDrop:a}=e,s=n.length;return r(y("div",{style:e.style,children:[m("div",{style:{paddingLeft:10+14*s+"px"},children:U.Children.map(t,(e=>U.cloneElement(e,{isOver:o,canDrop:a,draggedNode:i})))}),m(Hr,{...e})]}))};const Gr=e=>e.children,Ur=e=>{const{status:n,allItems:r}=function(e){const n=t({apiVersion:"2021-09-01"}),[r,o]=U.useState("loading"),[i,a]=U.useReducer(fe,{});function s(e){"mutation"===e.type&&(e.result?a({type:"addOrEditItem",item:e.result}):a({type:"removeItem",itemId:e.documentId}))}function c(e){a({type:"setInitialData",items:e}),o("success")}return U.useEffect((()=>{const{filter:t,params:r}=function(e){let{referenceTo:t,referenceOptions:n}=e;const r=["_type in $docTypes"];return n?.filter&&r.push(n.filter),{filter:r.join(" && "),params:{...n?.filterParams||{},docTypes:t.map((e=>e))}}}(e),i=`*[${t}] {\n _id,\n _type,\n _updatedAt,\n }`;n.fetch(i,r).then(c).catch((()=>{o("error")}));const a=n.listen(i,r).subscribe(s);return()=>{a.unsubscribe()}}),[]),{status:r,allItems:i}}(e.options),o=(i={tree:e.tree,allItems:r}).tree?Object.entries(i.allItems).filter((e=>{let[t]=e;return t&&!i.tree.some((e=>e?.value?.reference?._ref===t))})).map((e=>{let[,t]=e;return De(t)})).filter(Boolean):Object.entries(i.allItems).map((e=>De(e[1]))).filter(Boolean);var i;const{localTree:a,handleVisibilityToggle:s}=function(e){let{tree:t,allItems:n}=e;const[r,o]=U.useState({});return{localTree:Ne({tree:t,allItems:n,visibilityMap:r}),handleVisibilityToggle:function(e){o({...r,[e.node._key]:e.expanded})}}}({tree:e.tree,allItems:r}),c=Le({patchPrefix:e.patchPrefix,onChange:e.onChange,localTree:a}),[l,d]=U.useState(null);U.useEffect((()=>{e.options.documentId&&d(document.getElementById(e.options.documentId))}),[e.options.documentId]);const u=J((e=>c.handleMovedNode(e)),[c]),p=Z((()=>function(e){let{placeholder:t}=e;return{theme:{nodeContentRenderer:Mr,placeholderRenderer:e=>m(Br,{...t,...e}),treeNodeRenderer:Fr,style:{height:"100%"},innerStyle:void 0,scaffoldBlockPxWidth:44,slideRegionSize:100}}}({placeholder:{title:"Add items from the list below"}})),[]),h=Z((()=>({...c,allItemsStatus:n})),[c,n]);return m(Gr,{children:m($,{id:e.options.documentId,children:l?m(oe,{backend:ie,options:{rootElement:l},children:m(ye.Provider,{value:h,children:y(D,{space:4,paddingTop:4,children:[m(N,{style:{minHeight:(f=a,50+51*re({treeData:f})+"px")},borderBottom:"success"!==n||o?.length>0,children:m(Q,{maxDepth:e.options.maxDepth,onChange:Yr,onVisibilityToggle:s,canDrop:Wr,onMoveNode:u,treeData:a,...p})}),"success"===n&&o?.length>0&&y(D,{space:1,paddingX:2,paddingTop:3,children:[y(D,{space:2,paddingX:2,paddingBottom:3,children:[m(R,{size:2,as:"h2",weight:"semibold",children:"Add more items"}),m(R,{size:1,muted:!0,children:"Only published documents are shown."})]}),o.map((e=>m(_e,{item:e,action:m(j,{portal:!0,placement:"left",content:m($,{padding:2,children:m(R,{size:1,children:"Add to list"})}),children:m(F,{onClick:()=>{c.addItem(e)},mode:"bleed",icon:A,style:{cursor:"pointer"}})})},e.publishedId||e.draftId)))]}),"loading"===n&&m(L,{padding:4,align:"center",justify:"center",children:m(G,{size:3,muted:!0})}),"error"===n&&m(L,{padding:4,align:"center",justify:"center",children:m(R,{size:2,weight:"semibold",children:"Something went wrong when loading documents"})})]})})}):null})});var f};function Wr(e){let{nextPath:t,prevPath:n}=e;return!(t.length>=n.length&&n.every(((e,n)=>t[n]===e)))}const Yr=()=>{};function qr(e){const t=ce(e.path||[]);if("insert"===e.type){const{position:n,items:r}=e;return{insert:{[n]:t,items:r}}}if("unset"===e.type)return{unset:[t]};if(!e.type)throw new Error(`Missing patch type in patch ${JSON.stringify(e)}`);return t?{[e.type]:{[t]:e.value}}:{[e.type]:e.value}}const Xr="tree",Kr=e=>{const t=e.options.documentType||Te,n=e.options.fieldKeyInDocument||Xr,{published:r,draft:o,liveEdit:i}=d(e.options.documentId,t),{patch:a}=u(e.options.documentId,t),s=r?.[n]||[],c=U.useCallback((e=>{a?.execute&&a.execute(Pe(e.patches,t).map(qr))}),[a]);return U.useEffect((()=>{r?._id||!a?.execute||a?.disabled||a.execute([{setIfMissing:{[n]:[]}}])}),[r?._id,a]),i?o?._id?m(de,{title:"This hierarchy tree contains a draft",subtitle:"Click on the button below to publish your draft in order to continue editing the live\n published document."}):r?._id?m($,{paddingBottom:5,paddingRight:2,children:m(Ur,{options:e.options,tree:s,onChange:c,patchPrefix:n})}):m(L,{padding:5,align:"center",justify:"center",height:"fill",children:m(G,{width:4,muted:!0})}):m(de,{title:"Invalid configuration",subtitle:"The `documentType` passed to `createDeskHiearchy` isn't live editable. \\nTo continue using this plugin, add `liveEdit: true` to your custom schema type or unset `documentType` in your hierarchy configuration."})},Jr={invalidDocumentId:"Please add a documentId to your tree",invalidDocumentType:"Please add a valid documentType to createHierarchicalSchemas",invalidReferenceTo:"Missing valid 'referenceTo' value"};function Zr(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";throw new Error(`[hierarchical input] ${Jr[e]} ${t}`)}const Qr=e=>{const{documentId:t,referenceTo:n}=e;return"string"==typeof t||t||Zr("invalidDocumentId"),Array.isArray(n)||Zr("invalidReferenceTo",`(documentId "${t}")`),t=>m(Kr,{...t,options:e})};function eo(e){const{documentId:t,referenceTo:n,referenceOptions:r,context:o,S:i,creatableTypes:a}=e;if(!i||!o)throw new Error("Invalid configuration. S or context props are undefined. These props are available as function parameters when configuring structure, and must be passed along to createDeskHierarchy. Confer the plugin README for example usage.");const{schema:s}=o,c=a&&!a.some((e=>n.indexOf(e)))?a:n;let l=(1===n?.length?i.documentTypeList(n[0]).schemaType(n[0]):i.documentList().filter("_type in $types").params({types:n})).id(t).menuItems((c||[]).map((e=>i.menuItem().intent({type:"create",params:{type:e}}).title(`Create ${s.get(e)?.title}`).icon(s.get(e)?.icon||E)))).canHandleIntent(((t,n)=>"edit"===t&&n.id===e.documentId||!!c.includes(n.type)));return r?.filter&&(l=l.filter(r.filter)),r?.filterParams&&(l=l.params(r.filterParams)),i.listItem().id(t).title(e.title||t).icon(e.icon).child(Object.assign(l.serialize(),{type:"component",component:Qr(e),options:e,__preserveInstance:!0},e.title?{title:e.title}:{}))}const to=e=>{const t=e.type.options.documentType||Te,n=U.useCallback((n=>{const r=Pe(n?.patches,t);e.onChange(new c(r))}),[e.onChange]);return m(p,{description:e.type.description,title:e.type.title,__unstable_presence:e.presence,children:m(Ur,{options:e.type.options,tree:e.value||[],onChange:n})})};function no(e){let{referenceTo:t,referenceOptions:n,documentType:r}=e;return{name:r?Ie(r,"nodeValue"):Ae,type:"object",title:`Hierarchical node value (${r})`,fields:[{name:"docType",type:"string"},{name:"reference",type:"reference",weak:!0,to:t.map((e=>({type:e}))),options:n}]}}function ro(e){return{name:e.documentType?Ie(e.documentType,"node"):Ce,title:`Hierarchical node (${e.documentType})`,type:"object",fields:[{name:"parent",type:"string"},e.documentType?{name:"value",type:Ie(e.documentType,"nodeValue")}:{...no(e),name:"value",type:"object"}]}}function oo(e){return{name:e.documentType?Ie(e.documentType,"array"):Ee,title:`Hierarchical array of nodes (${e.documentType})`,type:"array",of:[e.documentType?{type:Ie(e.documentType,"node")}:ro(e)]}}function io(e){let{name:t,title:n,options:r,...o}=e;return Array.isArray(r?.referenceTo)||Zr("invalidReferenceTo",`(field of name "${t}")`),{...o,name:t,title:n,inputComponent:to,options:r,...r.documentType?{type:Ie(r.documentType,"array")}:{...oo(r),name:t}}}function ao(e){return{name:e.documentType,title:"Hierarchical tree",type:"document",liveEdit:!0,fields:[io({name:e.fieldKeyInDocument||Xr,title:"Hierarchical Tree",options:e})],preview:{select:{id:"_id",tree:"tree"},prepare(e){let{id:t,tree:n}=e;return{title:`Hierarchical documents (ID: ${t})`,subtitle:`${n?.length||0} document(s) in its list.`}}}}}function so(e){return(!Array.isArray(e.referenceTo)||e.referenceTo.length<=0)&&Zr("invalidReferenceTo"),e.documentType||Zr("invalidDocumentType"),[ao(e),oo(e),ro(e),no(e)]}var co=h({name:"hierarchy.tree",title:"Hierarchical tree",type:"document",liveEdit:!0,fields:[{name:"unusedField",title:"Unused field",type:"string",hidden:!0}]});const lo=f({name:"sanity-plugin-hierarchical-document-list"});export{eo as createDeskHierarchy,so as createHierarchicalSchemas,xe as flatDataToTree,lo as hierarchicalDocumentList,co as hierarchyTree};//# sourceMappingURL=index.esm.js.map
151
+ `,Hr=e=>{const{lowerSiblingCounts:t,scaffoldBlockPxWidth:n,listIndex:r,swapDepth:o,swapFrom:i,swapLength:a,treeIndex:s}=e,c=t.map(((e,t)=>{if(e<0||s===r||t!==o)return null;let c="";c=r===(i||0)+(a||0)-1?"rst__highlightBottomLeftCorner":s===i?"rst__highlightTopLeftCorner":"rst__highlightLineVertical";return m("div",{style:{width:n,left:n*t},className:`rst__unclickable rst__absoluteLineBlock ${c||""}`,tabIndex:-1},t)}));return y(g,{children:[c,m(Vr,{})]})},Fr=e=>{const{children:t,lowerSiblingCounts:n,connectDropTarget:r,isOver:o,draggedNode:i,canDrop:a}=e,s=n.length;return r(y("div",{style:e.style,children:[m("div",{style:{paddingLeft:10+14*s+"px"},children:U.Children.map(t,(e=>U.cloneElement(e,{isOver:o,canDrop:a,draggedNode:i})))}),m(Hr,{...e})]}))};const Gr=e=>e.children,Ur=e=>{const{status:n,allItems:r}=function(e){const n=t({apiVersion:"2021-09-01"}),[r,o]=U.useState("loading"),[i,a]=U.useReducer(fe,{});function s(e){"mutation"===e.type&&(e.result?a({type:"addOrEditItem",item:e.result}):a({type:"removeItem",itemId:e.documentId}))}function c(e){a({type:"setInitialData",items:e}),o("success")}return U.useEffect((()=>{const{filter:t,params:r}=function(e){let{referenceTo:t,referenceOptions:n}=e;const r=["_type in $docTypes"];return n?.filter&&r.push(n.filter),{filter:r.join(" && "),params:{...n?.filterParams||{},docTypes:t.map((e=>e))}}}(e),i=`*[${t}] {\n _id,\n _type,\n _updatedAt,\n }`;n.fetch(i,r).then(c).catch((()=>{o("error")}));const a=n.listen(i,r).subscribe(s);return()=>{a.unsubscribe()}}),[]),{status:r,allItems:i}}(e.options),o=(i={tree:e.tree,allItems:r}).tree?Object.entries(i.allItems).filter((e=>{let[t]=e;return t&&!i.tree.some((e=>e?.value?.reference?._ref===t))})).map((e=>{let[,t]=e;return De(t)})).filter(Boolean):Object.entries(i.allItems).map((e=>De(e[1]))).filter(Boolean);var i;const{localTree:a,handleVisibilityToggle:s}=function(e){let{tree:t,allItems:n}=e;const[r,o]=U.useState({});return{localTree:Ne({tree:t,allItems:n,visibilityMap:r}),handleVisibilityToggle:function(e){o({...r,[e.node._key]:e.expanded})}}}({tree:e.tree,allItems:r}),c=Le({patchPrefix:e.patchPrefix,onChange:e.onChange,localTree:a}),[l,d]=U.useState(null),[u,p]=U.useState(""),h=()=>{const t=document.querySelector(`#${e.options.documentId} [data-known-size]`),n=Number(t?.dataset.knownSize||51);p(function(e,t){return 50+t*re({treeData:e})+"px"}(a,n))};U.useEffect((()=>{e.options.documentId&&d(document.getElementById(e.options.documentId))}),[e.options.documentId]),U.useEffect((()=>{setTimeout(h)}),[]),U.useEffect((()=>{h()}),[e.options.documentId,a]);const f=J((e=>c.handleMovedNode(e)),[c]),g=Z((()=>function(e){let{placeholder:t}=e;return{theme:{nodeContentRenderer:Mr,placeholderRenderer:e=>m(Br,{...t,...e}),treeNodeRenderer:Fr,style:{height:"100%"},innerStyle:void 0,scaffoldBlockPxWidth:44,slideRegionSize:100}}}({placeholder:{title:"Add items from the list below"}})),[]),v=Z((()=>({...c,allItemsStatus:n})),[c,n]);return m(Gr,{children:m($,{id:e.options.documentId,children:l?m(oe,{backend:ie,options:{rootElement:l},children:m(ye.Provider,{value:v,children:y(D,{space:4,paddingTop:4,children:[m(N,{style:{minHeight:u},borderBottom:"success"!==n||o?.length>0,children:m(Q,{maxDepth:e.options.maxDepth,onChange:Yr,onVisibilityToggle:s,canDrop:Wr,onMoveNode:f,treeData:a,...g})}),"success"===n&&o?.length>0&&y(D,{space:1,paddingX:2,paddingTop:3,children:[y(D,{space:2,paddingX:2,paddingBottom:3,children:[m(R,{size:2,as:"h2",weight:"semibold",children:"Add more items"}),m(R,{size:1,muted:!0,children:"Only published documents are shown."})]}),o.map((e=>m(_e,{item:e,action:m(j,{portal:!0,placement:"left",content:m($,{padding:2,children:m(R,{size:1,children:"Add to list"})}),children:m(F,{onClick:()=>{c.addItem(e)},mode:"bleed",icon:A,style:{cursor:"pointer"}})})},e.publishedId||e.draftId)))]}),"loading"===n&&m(L,{padding:4,align:"center",justify:"center",children:m(G,{size:3,muted:!0})}),"error"===n&&m(L,{padding:4,align:"center",justify:"center",children:m(R,{size:2,weight:"semibold",children:"Something went wrong when loading documents"})})]})})}):null})})};function Wr(e){let{nextPath:t,prevPath:n}=e;return!(t.length>=n.length&&n.every(((e,n)=>t[n]===e)))}const Yr=()=>{};function qr(e){const t=ce(e.path||[]);if("insert"===e.type){const{position:n,items:r}=e;return{insert:{[n]:t,items:r}}}if("unset"===e.type)return{unset:[t]};if(!e.type)throw new Error(`Missing patch type in patch ${JSON.stringify(e)}`);return t?{[e.type]:{[t]:e.value}}:{[e.type]:e.value}}const Xr="tree",Kr=e=>{const t=e.options.documentType||Te,n=e.options.fieldKeyInDocument||Xr,{published:r,draft:o,liveEdit:i}=d(e.options.documentId,t),{patch:a}=u(e.options.documentId,t),s=r?.[n]||[],c=U.useCallback((e=>{a?.execute&&a.execute(Pe(e.patches,t).map(qr))}),[a]);return U.useEffect((()=>{r?._id||!a?.execute||a?.disabled||a.execute([{setIfMissing:{[n]:[]}}])}),[r?._id,a]),i?o?._id?m(de,{title:"This hierarchy tree contains a draft",subtitle:"Click on the button below to publish your draft in order to continue editing the live\n published document."}):r?._id?m($,{paddingBottom:5,paddingRight:2,children:m(Ur,{options:e.options,tree:s,onChange:c,patchPrefix:n})}):m(L,{padding:5,align:"center",justify:"center",height:"fill",children:m(G,{width:4,muted:!0})}):m(de,{title:"Invalid configuration",subtitle:"The `documentType` passed to `createDeskHiearchy` isn't live editable. \\nTo continue using this plugin, add `liveEdit: true` to your custom schema type or unset `documentType` in your hierarchy configuration."})},Jr={invalidDocumentId:"Please add a documentId to your tree",invalidDocumentType:"Please add a valid documentType to createHierarchicalSchemas",invalidReferenceTo:"Missing valid 'referenceTo' value"};function Zr(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";throw new Error(`[hierarchical input] ${Jr[e]} ${t}`)}const Qr=e=>{const{documentId:t,referenceTo:n}=e;return"string"==typeof t||t||Zr("invalidDocumentId"),Array.isArray(n)||Zr("invalidReferenceTo",`(documentId "${t}")`),t=>m(Kr,{...t,options:e})};function eo(e){const{documentId:t,referenceTo:n,referenceOptions:r,context:o,S:i,creatableTypes:a}=e;if(!i||!o)throw new Error("Invalid configuration. S or context props are undefined. These props are available as function parameters when configuring structure, and must be passed along to createDeskHierarchy. Confer the plugin README for example usage.");const{schema:s}=o,c=a&&!a.some((e=>n.indexOf(e)))?a:n;let l=(1===n?.length?i.documentTypeList(n[0]).schemaType(n[0]):i.documentList().filter("_type in $types").params({types:n})).id(t).menuItems((c||[]).map((e=>i.menuItem().intent({type:"create",params:{type:e}}).title(`Create ${s.get(e)?.title}`).icon(s.get(e)?.icon||E)))).canHandleIntent(((t,n)=>"edit"===t&&n.id===e.documentId||!!c.includes(n.type)));return r?.filter&&(l=l.filter(r.filter)),r?.filterParams&&(l=l.params(r.filterParams)),i.listItem().id(t).title(e.title||t).icon(e.icon).child(Object.assign(l.serialize(),{type:"component",component:Qr(e),options:e,__preserveInstance:!0},e.title?{title:e.title}:{}))}const to=e=>{const t=e.type.options.documentType||Te,n=U.useCallback((n=>{const r=Pe(n?.patches,t);e.onChange(new c(r))}),[e.onChange]);return m(p,{description:e.type.description,title:e.type.title,__unstable_presence:e.presence,children:m(Ur,{options:e.type.options,tree:e.value||[],onChange:n})})};function no(e){let{referenceTo:t,referenceOptions:n,documentType:r}=e;return{name:r?Ie(r,"nodeValue"):Ae,type:"object",title:`Hierarchical node value (${r})`,fields:[{name:"docType",type:"string"},{name:"reference",type:"reference",weak:!0,to:t.map((e=>({type:e}))),options:n}]}}function ro(e){return{name:e.documentType?Ie(e.documentType,"node"):Ce,title:`Hierarchical node (${e.documentType})`,type:"object",fields:[{name:"parent",type:"string"},e.documentType?{name:"value",type:Ie(e.documentType,"nodeValue")}:{...no(e),name:"value",type:"object"}]}}function oo(e){return{name:e.documentType?Ie(e.documentType,"array"):Ee,title:`Hierarchical array of nodes (${e.documentType})`,type:"array",of:[e.documentType?{type:Ie(e.documentType,"node")}:ro(e)]}}function io(e){let{name:t,title:n,options:r,...o}=e;return Array.isArray(r?.referenceTo)||Zr("invalidReferenceTo",`(field of name "${t}")`),{...o,name:t,title:n,inputComponent:to,options:r,...r.documentType?{type:Ie(r.documentType,"array")}:{...oo(r),name:t}}}function ao(e){return{name:e.documentType,title:"Hierarchical tree",type:"document",liveEdit:!0,fields:[io({name:e.fieldKeyInDocument||Xr,title:"Hierarchical Tree",options:e})],preview:{select:{id:"_id",tree:"tree"},prepare(e){let{id:t,tree:n}=e;return{title:`Hierarchical documents (ID: ${t})`,subtitle:`${n?.length||0} document(s) in its list.`}}}}}function so(e){return(!Array.isArray(e.referenceTo)||e.referenceTo.length<=0)&&Zr("invalidReferenceTo"),e.documentType||Zr("invalidDocumentType"),[ao(e),oo(e),ro(e),no(e)]}var co=h({name:"hierarchy.tree",title:"Hierarchical tree",type:"document",liveEdit:!0,fields:[{name:"unusedField",title:"Unused field",type:"string",hidden:!0}]});const lo=f({name:"sanity-plugin-hierarchical-document-list"});export{eo as createDeskHierarchy,so as createHierarchicalSchemas,xe as flatDataToTree,lo as hierarchicalDocumentList,co as hierarchyTree};//# sourceMappingURL=index.esm.js.map