UNPKG

@10up/block-components

Version:

10up Components built for the WordPress Block Editor.

2 lines 5.06 kB
!function(){"use strict";var e={5539:function(e,t,r){r.d(t,{DragHandle:function(){return i}});var n=r(7966);function o(){return o=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},o.apply(null,arguments)}const i=e=>(0,n.createElement)("span",o({},e,{style:{cursor:e.isDragging?"grabbing":"grab"}}),(0,n.createElement)("svg",{style:{marginRight:"10px",cursor:"grab",flexShrink:0},width:"18",height:"18",xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 18 18",role:"img","aria-hidden":"true",focusable:"false"},(0,n.createElement)("path",{d:"M5 4h2V2H5v2zm6-2v2h2V2h-2zm-6 8h2V8H5v2zm6 0h2V8h-2v2zm-6 6h2v-2H5v2zm6 0h2v-2h-2v2z"})))},845:function(e){e.exports=require("@dnd-kit/core")},1502:function(e){e.exports=require("@dnd-kit/sortable")},5414:function(e){e.exports=require("@dnd-kit/utilities")},2627:function(e){e.exports=require("@wordpress/block-editor")},9416:function(e){e.exports=require("@wordpress/blocks")},2050:function(e){e.exports=require("@wordpress/components")},5214:function(e){e.exports=require("@wordpress/data")},7966:function(e){e.exports=require("@wordpress/element")},8450:function(e){e.exports=require("@wordpress/i18n")},4333:function(e){e.exports=require("uuid")}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,r),i.exports}r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,{a:t}),t},r.d=function(e,t){for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};r.r(n),r.d(n,{AbstractRepeater:function(){return h},AttributeRepeater:function(){return y},Repeater:function(){return x}});var o=r(7966),i=r(2627),a=r(9416),s=r(5214),l=r(2050),u=r(8450),d=r(4333),c=r(845),f=r(1502),p=require("@dnd-kit/modifiers"),g=r(5414),m=r(5539);function v(){return v=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},v.apply(null,arguments)}const b=({children:e,item:t={},setItem:r=null,removeItem:n=null,id:i=""})=>{const{attributes:a,listeners:s,setNodeRef:l,transform:u,transition:d,isDragging:c}=(0,f.useSortable)({id:i}),p={transform:g.CSS.Transform.toString(u),transition:d,display:"flex",zIndex:c?999:1,position:"relative"},b=e(t,i,r,n);return(0,o.cloneElement)(b,{ref:l,style:p,className:c?`${b.props.className} repeater-item--is-dragging`:b.props.className},[(0,o.createElement)(m.DragHandle,v({className:"repeater-item__drag-handle"},a,s,{isDragging:c})),b.props.children])},h=({children:e,addButton:t=null,allowReordering:r=!1,onChange:n,value:i,defaultValue:a=[]})=>{const s=(0,c.useSensors)((0,c.useSensor)(c.PointerSensor),(0,c.useSensor)(c.KeyboardSensor,{coordinateGetter:f.sortableKeyboardCoordinates}));function g(){const e=JSON.parse(JSON.stringify(a));a.length||e.push({}),e[0].id=(0,d.v4)(),n([...i,...e])}function m(e,t){const r=JSON.parse(JSON.stringify(i));r[t]="object"===typeof e&&null!==e?{...r[t],...e}:e,n(r)}function v(e){const t=JSON.parse(JSON.stringify(i)).filter((t,r)=>e!==r);n(t)}const h=i.map(e=>e.id);return(0,o.createElement)(o.Fragment,null,r?(0,o.createElement)(c.DndContext,{sensors:s,collisionDetection:c.closestCenter,onDragEnd:e=>function(e){const{active:t,over:r}=e;t.id!==r?.id&&n((e=>{const n=e.findIndex(e=>e.id===t.id),o=e.findIndex(e=>e.id===r?.id);return(0,f.arrayMove)(e,n,o)})(i))}(e),modifiers:[p.restrictToVerticalAxis]},(0,o.createElement)(f.SortableContext,{items:h,strategy:f.verticalListSortingStrategy},i.map((t,r)=>(0,o.createElement)(b,{item:t,setItem:e=>m(e,r),removeItem:()=>v(r),key:t.id,id:t.id},(t,n,o,i)=>e(t,n,e=>o(e,r),()=>i(r)))))):i.map((t,r)=>e(t,t.id,e=>m(e,r),()=>v(r))),"function"===typeof t?t(g):(0,o.createElement)(l.Button,{variant:"primary",onClick:()=>g()},(0,u.__)("Add item")))},y=({children:e,attribute:t=null,addButton:r=null,allowReordering:n=!1})=>{const{clientId:l,name:u}=(0,i.useBlockEditContext)(),{updateBlockAttributes:c}=(0,s.dispatch)(i.store),f=(0,s.useSelect)(e=>e(i.store).getBlockAttributes(l)[t]||[],[t,l]),{defaultRepeaterData:p}=(0,s.useSelect)(e=>({defaultRepeaterData:e(a.store).getBlockType(u).attributes[t].default}),[t]);p.length&&(p[0].id=(0,d.v4)());return(0,o.createElement)(h,{addButton:r,allowReordering:n,onChange:e=>{null!==t&&c(l,{[t]:e})},value:f,defaultValue:p},e)},x=({children:e,addButton:t=null,allowReordering:r=!1,onChange:n,value:i,defaultValue:a=[],attribute:s=null})=>s?(0,o.createElement)(y,{attribute:s,addButton:t,allowReordering:r},e):(0,o.createElement)(h,{addButton:t,allowReordering:r,onChange:n,value:i,defaultValue:a},e);module.exports=n}(); //# sourceMappingURL=index.js.map