@sanity/table 1.1.2 → 1.1.4

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/lib/index.js CHANGED
@@ -1,2 +1,437 @@
1
- "use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function t(t){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?e(Object(o),!0).forEach((function(e){n(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function n(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}Object.defineProperty(exports,"__esModule",{value:!0});var r=require("sanity"),o=require("react/jsx-runtime"),l=require("react"),i=require("@sanity/ui"),s=require("@sanity/icons");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a,u=c(l),d=new Uint8Array(16);function p(){if(!a&&!(a="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return a(d)}var f=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function h(e){return"string"==typeof e&&f.test(e)}for(var y=[],x=0;x<256;++x)y.push((x+256).toString(16).substr(1));function g(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=(y[e[t+0]]+y[e[t+1]]+y[e[t+2]]+y[e[t+3]]+"-"+y[e[t+4]]+y[e[t+5]]+"-"+y[e[t+6]]+y[e[t+7]]+"-"+y[e[t+8]]+y[e[t+9]]+"-"+y[e[t+10]]+y[e[t+11]]+y[e[t+12]]+y[e[t+13]]+y[e[t+14]]+y[e[t+15]]).toLowerCase();if(!h(n))throw TypeError("Stringified UUID is invalid");return n}function m(e,t,n){function r(e,r,o,l){if("string"==typeof e&&(e=function(e){e=unescape(encodeURIComponent(e));for(var t=[],n=0;n<e.length;++n)t.push(e.charCodeAt(n));return t}(e)),"string"==typeof r&&(r=function(e){if(!h(e))throw TypeError("Invalid UUID");var t,n=new Uint8Array(16);return n[0]=(t=parseInt(e.slice(0,8),16))>>>24,n[1]=t>>>16&255,n[2]=t>>>8&255,n[3]=255&t,n[4]=(t=parseInt(e.slice(9,13),16))>>>8,n[5]=255&t,n[6]=(t=parseInt(e.slice(14,18),16))>>>8,n[7]=255&t,n[8]=(t=parseInt(e.slice(19,23),16))>>>8,n[9]=255&t,n[10]=(t=parseInt(e.slice(24,36),16))/1099511627776&255,n[11]=t/4294967296&255,n[12]=t>>>24&255,n[13]=t>>>16&255,n[14]=t>>>8&255,n[15]=255&t,n}(r)),16!==r.length)throw TypeError("Namespace must be array-like (16 iterable integer values, 0-255)");var i=new Uint8Array(16+e.length);if(i.set(r),i.set(e,r.length),(i=n(i))[6]=15&i[6]|t,i[8]=63&i[8]|128,o){l=l||0;for(var s=0;s<16;++s)o[l+s]=i[s];return o}return g(i)}try{r.name=e}catch(e){}return r.DNS="6ba7b810-9dad-11d1-80b4-00c04fd430c8",r.URL="6ba7b811-9dad-11d1-80b4-00c04fd430c8",r}function v(e){return 14+(e+64>>>9<<4)+1}function j(e,t){var n=(65535&e)+(65535&t);return(e>>16)+(t>>16)+(n>>16)<<16|65535&n}function w(e,t,n,r,o,l){return j((i=j(j(t,e),j(r,l)))<<(s=o)|i>>>32-s,n);var i,s}function b(e,t,n,r,o,l,i){return w(t&n|~t&r,e,t,o,l,i)}function C(e,t,n,r,o,l,i){return w(t&r|n&~r,e,t,o,l,i)}function A(e,t,n,r,o,l,i){return w(t^n^r,e,t,o,l,i)}function I(e,t,n,r,o,l,i){return w(n^(t|~r),e,t,o,l,i)}function k(e,t,n){var r=(e=e||{}).random||(e.rng||p)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(var o=0;o<16;++o)t[n+o]=r[o];return t}return g(r)}function T(e,t,n,r){switch(e){case 0:return t&n^~t&r;case 1:case 3:return t^n^r;case 2:return t&n^t&r^n&r}}function O(e,t){return e<<t|e>>>32-t}m("v3",48,(function(e){if("string"==typeof e){var t=unescape(encodeURIComponent(e));e=new Uint8Array(t.length);for(var n=0;n<t.length;++n)e[n]=t.charCodeAt(n)}return function(e){for(var t=[],n=32*e.length,r="0123456789abcdef",o=0;o<n;o+=8){var l=e[o>>5]>>>o%32&255,i=parseInt(r.charAt(l>>>4&15)+r.charAt(15&l),16);t.push(i)}return t}(function(e,t){e[t>>5]|=128<<t%32,e[v(t)-1]=t;for(var n=1732584193,r=-271733879,o=-1732584194,l=271733878,i=0;i<e.length;i+=16){var s=n,c=r,a=o,u=l;n=b(n,r,o,l,e[i],7,-680876936),l=b(l,n,r,o,e[i+1],12,-389564586),o=b(o,l,n,r,e[i+2],17,606105819),r=b(r,o,l,n,e[i+3],22,-1044525330),n=b(n,r,o,l,e[i+4],7,-176418897),l=b(l,n,r,o,e[i+5],12,1200080426),o=b(o,l,n,r,e[i+6],17,-1473231341),r=b(r,o,l,n,e[i+7],22,-45705983),n=b(n,r,o,l,e[i+8],7,1770035416),l=b(l,n,r,o,e[i+9],12,-1958414417),o=b(o,l,n,r,e[i+10],17,-42063),r=b(r,o,l,n,e[i+11],22,-1990404162),n=b(n,r,o,l,e[i+12],7,1804603682),l=b(l,n,r,o,e[i+13],12,-40341101),o=b(o,l,n,r,e[i+14],17,-1502002290),n=C(n,r=b(r,o,l,n,e[i+15],22,1236535329),o,l,e[i+1],5,-165796510),l=C(l,n,r,o,e[i+6],9,-1069501632),o=C(o,l,n,r,e[i+11],14,643717713),r=C(r,o,l,n,e[i],20,-373897302),n=C(n,r,o,l,e[i+5],5,-701558691),l=C(l,n,r,o,e[i+10],9,38016083),o=C(o,l,n,r,e[i+15],14,-660478335),r=C(r,o,l,n,e[i+4],20,-405537848),n=C(n,r,o,l,e[i+9],5,568446438),l=C(l,n,r,o,e[i+14],9,-1019803690),o=C(o,l,n,r,e[i+3],14,-187363961),r=C(r,o,l,n,e[i+8],20,1163531501),n=C(n,r,o,l,e[i+13],5,-1444681467),l=C(l,n,r,o,e[i+2],9,-51403784),o=C(o,l,n,r,e[i+7],14,1735328473),n=A(n,r=C(r,o,l,n,e[i+12],20,-1926607734),o,l,e[i+5],4,-378558),l=A(l,n,r,o,e[i+8],11,-2022574463),o=A(o,l,n,r,e[i+11],16,1839030562),r=A(r,o,l,n,e[i+14],23,-35309556),n=A(n,r,o,l,e[i+1],4,-1530992060),l=A(l,n,r,o,e[i+4],11,1272893353),o=A(o,l,n,r,e[i+7],16,-155497632),r=A(r,o,l,n,e[i+10],23,-1094730640),n=A(n,r,o,l,e[i+13],4,681279174),l=A(l,n,r,o,e[i],11,-358537222),o=A(o,l,n,r,e[i+3],16,-722521979),r=A(r,o,l,n,e[i+6],23,76029189),n=A(n,r,o,l,e[i+9],4,-640364487),l=A(l,n,r,o,e[i+12],11,-421815835),o=A(o,l,n,r,e[i+15],16,530742520),n=I(n,r=A(r,o,l,n,e[i+2],23,-995338651),o,l,e[i],6,-198630844),l=I(l,n,r,o,e[i+7],10,1126891415),o=I(o,l,n,r,e[i+14],15,-1416354905),r=I(r,o,l,n,e[i+5],21,-57434055),n=I(n,r,o,l,e[i+12],6,1700485571),l=I(l,n,r,o,e[i+3],10,-1894986606),o=I(o,l,n,r,e[i+10],15,-1051523),r=I(r,o,l,n,e[i+1],21,-2054922799),n=I(n,r,o,l,e[i+8],6,1873313359),l=I(l,n,r,o,e[i+15],10,-30611744),o=I(o,l,n,r,e[i+6],15,-1560198380),r=I(r,o,l,n,e[i+13],21,1309151649),n=I(n,r,o,l,e[i+4],6,-145523070),l=I(l,n,r,o,e[i+11],10,-1120210379),o=I(o,l,n,r,e[i+2],15,718787259),r=I(r,o,l,n,e[i+9],21,-343485551),n=j(n,s),r=j(r,c),o=j(o,a),l=j(l,u)}return[n,r,o,l]}(function(e){if(0===e.length)return[];for(var t=8*e.length,n=new Uint32Array(v(t)),r=0;r<t;r+=8)n[r>>5]|=(255&e[r/8])<<r%32;return n}(e),8*e.length))})),m("v5",80,(function(e){var t=[1518500249,1859775393,2400959708,3395469782],n=[1732584193,4023233417,2562383102,271733878,3285377520];if("string"==typeof e){var r=unescape(encodeURIComponent(e));e=[];for(var o=0;o<r.length;++o)e.push(r.charCodeAt(o))}else Array.isArray(e)||(e=Array.prototype.slice.call(e));e.push(128);for(var l=e.length/4+2,i=Math.ceil(l/16),s=new Array(i),c=0;c<i;++c){for(var a=new Uint32Array(16),u=0;u<16;++u)a[u]=e[64*c+4*u]<<24|e[64*c+4*u+1]<<16|e[64*c+4*u+2]<<8|e[64*c+4*u+3];s[c]=a}s[i-1][14]=8*(e.length-1)/Math.pow(2,32),s[i-1][14]=Math.floor(s[i-1][14]),s[i-1][15]=8*(e.length-1)&4294967295;for(var d=0;d<i;++d){for(var p=new Uint32Array(80),f=0;f<16;++f)p[f]=s[d][f];for(var h=16;h<80;++h)p[h]=O(p[h-3]^p[h-8]^p[h-14]^p[h-16],1);for(var y=n[0],x=n[1],g=n[2],m=n[3],v=n[4],j=0;j<80;++j){var w=Math.floor(j/20),b=O(y,5)+T(w,x,g,m)+v+t[w]+p[j]>>>0;v=m,m=g,g=O(x,30)>>>0,x=y,y=b}n[0]=n[0]+y>>>0,n[1]=n[1]+x>>>0,n[2]=n[2]+g>>>0,n[3]=n[3]+m>>>0,n[4]=n[4]+v>>>0}return[n[0]>>24&255,n[0]>>16&255,n[0]>>8&255,255&n[0],n[1]>>24&255,n[1]>>16&255,n[1]>>8&255,255&n[1],n[2]>>24&255,n[2]>>16&255,n[2]>>8&255,255&n[2],n[3]>>24&255,n[3]>>16&255,n[3]>>8&255,255&n[3],n[4]>>24&255,n[4]>>16&255,n[4]>>8&255,255&n[4]]}));const R=e=>{var t;return o.jsx("table",{style:{width:"100%"},children:o.jsxs("tbody",{children:[e.rows.map(((t,n)=>{const r=(t=>(n,r)=>o.jsx("td",{children:o.jsx(i.TextInput,{fontSize:1,padding:3,value:n,onChange:n=>e.updateCell(n,t,r)})},"cell-".concat(t,"-").concat(r)))(n);return o.jsxs("tr",{children:[t.cells.map(r),o.jsx("td",{children:o.jsx(i.Box,{marginLeft:1,style:{textAlign:"center"},children:o.jsx(i.Button,{icon:s.RemoveIcon,padding:2,onClick:()=>e.removeRow(n),mode:"bleed"})})},n)]},"row-".concat(n))})),o.jsx("tr",{children:((null==(t=e.rows[0])?void 0:t.cells)||[]).map(((t,n)=>o.jsx("td",{children:o.jsx(i.Box,{marginTop:1,style:{textAlign:"center"},children:o.jsx(i.Button,{icon:s.RemoveIcon,padding:2,onClick:()=>e.removeColumn(n),mode:"bleed"})})},n)))})]})})},S=e=>{const[t,n]=l.useState(null),[r,c]=l.useState("");return o.jsxs(u.default.Fragment,{children:[t&&o.jsx(i.Dialog,{header:"Add ".concat(t.type),id:"dialog-add",onClose:()=>n(null),zOffset:1e3,children:o.jsxs(i.Card,{padding:4,children:[o.jsx(i.TextInput,{style:{textAlign:"left"},fontSize:2,padding:3,type:"number",value:r,onChange:e=>{c(e.currentTarget.value)}}),o.jsx(i.Box,{marginTop:4,children:o.jsxs(i.Inline,{space:1,style:{textAlign:"right"},children:[o.jsx(i.Button,{text:"Cancel",mode:"ghost",onClick:()=>n(null)}),o.jsx(i.Button,{text:"Confirm",tone:"critical",onClick:()=>{const e=parseInt(null!=r?r:"0",10);e<100&&(n(null),null==t||t.callback(e),c(void 0))}})]})})]})}),o.jsx(i.MenuButton,{button:o.jsx(i.Button,{icon:s.ControlsIcon,fontSize:1,padding:2,mode:"ghost"}),id:"menu-button-example",menu:o.jsxs(i.Menu,{children:[o.jsx(i.MenuItem,{icon:s.AddIcon,fontSize:1,text:"Add Row(s)",onClick:()=>{n({type:"rows",callback:t=>e.addRows(t)})}}),o.jsx(i.MenuItem,{icon:s.AddIcon,fontSize:1,text:"Add Row At Index",onClick:()=>{n({type:"rows",callback:t=>e.addRowAt(t)})}}),o.jsx(i.MenuItem,{icon:s.AddIcon,fontSize:1,text:"Add Column(s)",onClick:()=>{n({type:"columns",callback:t=>e.addColumns(t)})}}),o.jsx(i.MenuItem,{icon:s.AddIcon,fontSize:1,text:"Add Column At Index",onClick:()=>{n({type:"columns",callback:t=>e.addColumnAt(t)})}}),o.jsx(i.MenuDivider,{}),o.jsx(i.MenuItem,{icon:s.WarningOutlineIcon,fontSize:1,text:"Remove",tone:"critical",onClick:e.remove})]}),placement:e.placement})]})};var B;const M=null!=(B=globalThis.structuredClone)?B:e=>JSON.parse(JSON.stringify(e)),U=e=>{var n,c,a;const{rowType:u="tableRow",value:d,onChange:p}=e,[f,h]=l.useState(null),y=e=>p(r.set(e)),x=()=>{h({type:"table",callback:g})},g=()=>{p(r.unset()),h(null)},m=e=>{if(!d)return;const t=M(d);t.rows.forEach((t=>{t.cells.splice(e,1)})),y(t),h(null)};return o.jsxs("div",{children:[f&&o.jsx(i.Dialog,{header:"Remove ".concat(f.type),id:"dialog-remove",onClose:()=>h(null),zOffset:1e3,children:o.jsxs(i.Card,{padding:4,children:[o.jsxs(i.Text,{children:["Are you sure you want to remove this ",f.type,"?"]}),o.jsx(i.Box,{marginTop:4,children:o.jsxs(i.Inline,{space:1,style:{textAlign:"right"},children:[o.jsx(i.Button,{text:"Cancel",mode:"ghost",onClick:()=>h(null)}),o.jsx(i.Button,{text:"Confirm",tone:"critical",onClick:()=>f.callback()})]})})]})}),o.jsx(i.Box,{children:o.jsx(i.Flex,{justify:"flex-end",children:(null==(n=null==d?void 0:d.rows)?void 0:n.length)&&o.jsx(S,{addColumns:e=>{if(!d)return;const t=M(d);return t.rows.forEach(((n,r)=>{for(let n=0;n<e;n++)t.rows[r].cells.push("")})),y(t)},addColumnAt:e=>{if(!d)return;const t=M(d);return t.rows.forEach(((n,r)=>{t.rows[r].cells.splice(e,0,"")})),y(t)},addRows:function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;var t;if(!d)return;const n=M(d),r=null!=(t=null==d?void 0:d.rows[0].cells.length)?t:0;for(let t=0;t<e;t++)n.rows.push({_type:u,_key:k(),cells:Array(r).fill("")});return y(n)},addRowAt:function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;if(!d)return;const t=M(d),n=d.rows[0].cells.length;return t.rows.splice(e,0,{_type:u,_key:k(),cells:Array(n).fill("")}),y(t)},remove:x,placement:"left"})})}),(null==(c=null==d?void 0:d.rows)?void 0:c.length)&&o.jsx(R,{rows:d.rows,removeRow:e=>{if(d)return d.rows.length<=1?x():h({type:"row",callback:()=>(e=>{if(!d)return;const t=M(d);t.rows.splice(e,1),y(t),h(null)})(e)})},removeColumn:e=>{if(d)return d.rows[0].cells.length<=1?x():h({type:"column",callback:()=>m(e)})},updateCell:(e,t,n)=>{if(!d)return;const r=M(d);return r.rows[t].cells[n]=e.target.value,y(r)}}),(!d||!(null==(a=null==d?void 0:d.rows)?void 0:a.length))&&o.jsx(i.Inline,{space:1,children:o.jsx(i.Button,{fontSize:1,padding:3,icon:s.AddIcon,text:"Create Table",tone:"primary",mode:"ghost",onClick:()=>{const e={rows:[{_type:u,_key:k(),cells:["",""]},{_type:u,_key:k(),cells:["",""]}]};return y(t(t({},d),e))}})})]})};function z(){return o.jsx("svg",{width:"1em",height:"1em",viewBox:"0 0 25 25",fill:"none",stroke:"currentColor",strokeWidth:"1.2",children:o.jsx("path",{d:"M3 3h18v18H3zM21 9H3M21 15H3M12 3v18"})})}const E=e=>{let{rows:t}=e;const n=0===t.length?0:t[0].cells.length;return o.jsx(i.Grid,{columns:n,padding:2,children:t.map((e=>e.cells.map(((t,n)=>o.jsx(i.Card,{padding:2,style:{outline:"1px solid #DFE2E9"},children:o.jsx(i.Text,{style:{textOverflow:"elipsis"},children:t})},e._key+n)))))})},P=e=>{var t;const{schemaType:n}=e,{rows:r=[],title:l}=null!=e?e:{title:"Title missing"};return o.jsxs(o.Fragment,{children:[o.jsx(i.Box,{padding:3,children:o.jsxs(i.Inline,{space:3,children:[o.jsx(i.Card,{children:o.jsx(i.Label,{size:4,children:o.jsx(z,{})})}),o.jsx(i.Card,{children:o.jsx(i.Text,{children:null!=(t=null==n?void 0:n.title)?t:l})})]})}),o.jsx(i.Box,{padding:2,children:0===r.length?o.jsx(i.Label,{muted:!0,children:"Empty Table"}):o.jsx(E,{rows:r})})]})},D=r.definePlugin((e=>{const n=r.defineType({title:"Table Row",name:(null==e?void 0:e.rowType)||"tableRow",type:"object",fields:[{name:"cells",type:"array",of:[{type:"string"}]}]});var l;return{name:"table",schema:{types:[n,r.defineType({title:"Table",name:"table",type:"object",fields:[{name:"rows",type:"array",of:[{type:n.name}]}],components:{input:(l=n.name,e=>o.jsx(U,t(t({},e),{},{rowType:l}))),preview:P},preview:{select:{rows:"rows",title:"title"},prepare:e=>{let{title:t,rows:n=[]}=e;return{title:t,rows:n}}}})]}}}));exports.TableComponent=U,exports.TablePreview=P,exports.table=D;
1
+ import { set, unset, definePlugin, defineType } from "sanity";
2
+ import { jsx, jsxs, Fragment } from "react/jsx-runtime";
3
+ import { RemoveIcon, ControlsIcon, AddIcon, WarningOutlineIcon } from "@sanity/icons";
4
+ import { Box, Button, TextInput, Dialog, Card, Inline, MenuButton, Menu, MenuItem, MenuDivider, Text, Flex, Label, Grid } from "@sanity/ui";
5
+ import { useState } from "react";
6
+ var getRandomValues, rnds8 = new Uint8Array(16);
7
+ function rng() {
8
+ if (!getRandomValues && (getRandomValues = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto < "u" && typeof msCrypto.getRandomValues == "function" && msCrypto.getRandomValues.bind(msCrypto), !getRandomValues))
9
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
10
+ return getRandomValues(rnds8);
11
+ }
12
+ var REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
13
+ function validate(uuid) {
14
+ return typeof uuid == "string" && REGEX.test(uuid);
15
+ }
16
+ var byteToHex = [];
17
+ for (var i = 0; i < 256; ++i)
18
+ byteToHex.push((i + 256).toString(16).substr(1));
19
+ function stringify(arr) {
20
+ var offset = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, uuid = (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
21
+ if (!validate(uuid))
22
+ throw TypeError("Stringified UUID is invalid");
23
+ return uuid;
24
+ }
25
+ function v4(options, buf, offset) {
26
+ options = options || {};
27
+ var rnds = options.random || (options.rng || rng)();
28
+ return rnds[6] = rnds[6] & 15 | 64, rnds[8] = rnds[8] & 63 | 128, stringify(rnds);
29
+ }
30
+ const TableInput = (props) => {
31
+ var _a2;
32
+ const updateCell = props.updateCell, renderRowCell = (rowIndex) => function(cell, cellIndex) {
33
+ return /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(
34
+ TextInput,
35
+ {
36
+ fontSize: 1,
37
+ padding: 3,
38
+ value: cell,
39
+ onChange: (e) => updateCell(e, rowIndex, cellIndex)
40
+ }
41
+ ) }, `cell-${rowIndex}-${cellIndex}`);
42
+ }, renderRow = (row, rowIndex) => {
43
+ const renderCell = renderRowCell(rowIndex);
44
+ return /* @__PURE__ */ jsxs("tr", { children: [
45
+ row.cells.map(renderCell),
46
+ /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(Box, { marginLeft: 1, style: { textAlign: "center" }, children: /* @__PURE__ */ jsx(
47
+ Button,
48
+ {
49
+ icon: RemoveIcon,
50
+ padding: 2,
51
+ onClick: () => props.removeRow(rowIndex),
52
+ mode: "bleed"
53
+ }
54
+ ) }) }, rowIndex)
55
+ ] }, `row-${rowIndex}`);
56
+ };
57
+ return /* @__PURE__ */ jsx("table", { style: { width: "100%" }, children: /* @__PURE__ */ jsxs("tbody", { children: [
58
+ props.rows.map(renderRow),
59
+ /* @__PURE__ */ jsx("tr", { children: (((_a2 = props.rows[0]) == null ? void 0 : _a2.cells) || []).map((_, i) => /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(Box, { marginTop: 1, style: { textAlign: "center" }, children: /* @__PURE__ */ jsx(
60
+ Button,
61
+ {
62
+ icon: RemoveIcon,
63
+ padding: 2,
64
+ onClick: () => props.removeColumn(i),
65
+ mode: "bleed"
66
+ }
67
+ ) }) }, i)) })
68
+ ] }) });
69
+ }, TableMenu = (props) => {
70
+ const { remove: handleRemove } = props, [dialog, setDialog] = useState(null), [count, setCount] = useState(""), updateCount = (e) => {
71
+ setCount(e.currentTarget.value);
72
+ }, addRows = () => {
73
+ setDialog({ type: "rows", callback: (c) => props.addRows(c) });
74
+ }, addRowAt = () => {
75
+ setDialog({ type: "rows", callback: (index) => props.addRowAt(index) });
76
+ }, addColumns = () => {
77
+ setDialog({
78
+ type: "columns",
79
+ callback: (c) => props.addColumns(c)
80
+ });
81
+ }, addColumnsAt = () => {
82
+ setDialog({ type: "columns", callback: (index) => props.addColumnAt(index) });
83
+ }, onConfirm = () => {
84
+ const parsedCount = parseInt(count != null ? count : "0", 10);
85
+ parsedCount < 100 && (setDialog(null), dialog == null || dialog.callback(parsedCount), setCount(void 0));
86
+ };
87
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
88
+ dialog && /* @__PURE__ */ jsx(
89
+ Dialog,
90
+ {
91
+ header: `Add ${dialog.type}`,
92
+ id: "dialog-add",
93
+ onClose: () => setDialog(null),
94
+ zOffset: 1e3,
95
+ children: /* @__PURE__ */ jsxs(Card, { padding: 4, children: [
96
+ /* @__PURE__ */ jsx(
97
+ TextInput,
98
+ {
99
+ style: { textAlign: "left" },
100
+ fontSize: 2,
101
+ padding: 3,
102
+ type: "number",
103
+ value: count,
104
+ onChange: updateCount
105
+ }
106
+ ),
107
+ /* @__PURE__ */ jsx(Box, { marginTop: 4, children: /* @__PURE__ */ jsxs(Inline, { space: 1, style: { textAlign: "right" }, children: [
108
+ /* @__PURE__ */ jsx(
109
+ Button,
110
+ {
111
+ text: "Cancel",
112
+ mode: "ghost",
113
+ onClick: () => setDialog(null)
114
+ }
115
+ ),
116
+ /* @__PURE__ */ jsx(Button, { text: "Confirm", tone: "critical", onClick: onConfirm })
117
+ ] }) })
118
+ ] })
119
+ }
120
+ ),
121
+ /* @__PURE__ */ jsx(
122
+ MenuButton,
123
+ {
124
+ button: /* @__PURE__ */ jsx(Button, { icon: ControlsIcon, fontSize: 1, padding: 2, mode: "ghost" }),
125
+ id: "menu-button-example",
126
+ menu: /* @__PURE__ */ jsxs(Menu, { children: [
127
+ /* @__PURE__ */ jsx(
128
+ MenuItem,
129
+ {
130
+ icon: AddIcon,
131
+ fontSize: 1,
132
+ text: "Add Row(s)",
133
+ onClick: addRows
134
+ }
135
+ ),
136
+ /* @__PURE__ */ jsx(
137
+ MenuItem,
138
+ {
139
+ icon: AddIcon,
140
+ fontSize: 1,
141
+ text: "Add Row At Index",
142
+ onClick: addRowAt
143
+ }
144
+ ),
145
+ /* @__PURE__ */ jsx(
146
+ MenuItem,
147
+ {
148
+ icon: AddIcon,
149
+ fontSize: 1,
150
+ text: "Add Column(s)",
151
+ onClick: addColumns
152
+ }
153
+ ),
154
+ /* @__PURE__ */ jsx(
155
+ MenuItem,
156
+ {
157
+ icon: AddIcon,
158
+ fontSize: 1,
159
+ text: "Add Column At Index",
160
+ onClick: addColumnsAt
161
+ }
162
+ ),
163
+ /* @__PURE__ */ jsx(MenuDivider, {}),
164
+ /* @__PURE__ */ jsx(
165
+ MenuItem,
166
+ {
167
+ icon: WarningOutlineIcon,
168
+ fontSize: 1,
169
+ text: "Remove",
170
+ tone: "critical",
171
+ onClick: handleRemove
172
+ }
173
+ )
174
+ ] }),
175
+ popover: { placement: props.placement }
176
+ }
177
+ )
178
+ ] });
179
+ };
180
+ var __defProp = Object.defineProperty, __defProps = Object.defineProperties, __getOwnPropDescs = Object.getOwnPropertyDescriptors, __getOwnPropSymbols = Object.getOwnPropertySymbols, __hasOwnProp = Object.prototype.hasOwnProperty, __propIsEnum = Object.prototype.propertyIsEnumerable, __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues = (a, b) => {
181
+ for (var prop in b || (b = {}))
182
+ __hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
183
+ if (__getOwnPropSymbols)
184
+ for (var prop of __getOwnPropSymbols(b))
185
+ __propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
186
+ return a;
187
+ }, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)), _a;
188
+ const deepClone = (_a = globalThis.structuredClone) != null ? _a : (data) => JSON.parse(JSON.stringify(data)), TableComponent = (props) => {
189
+ var _a2, _b, _c;
190
+ const { rowType = "tableRow", value, onChange } = props, [dialog, setDialog] = useState(null), updateValue = (v) => onChange(set(v)), resetValue = () => onChange(unset()), createTable = () => {
191
+ const newValue = {
192
+ rows: [
193
+ {
194
+ _type: rowType,
195
+ _key: v4(),
196
+ cells: ["", ""]
197
+ },
198
+ {
199
+ _type: rowType,
200
+ _key: v4(),
201
+ cells: ["", ""]
202
+ }
203
+ ]
204
+ };
205
+ return updateValue(__spreadValues(__spreadValues({}, value), newValue));
206
+ }, confirmRemoveTable = () => {
207
+ setDialog({ type: "table", callback: removeTable });
208
+ }, removeTable = () => {
209
+ resetValue(), setDialog(null);
210
+ }, addRows = (count = 1) => {
211
+ var _a3;
212
+ if (!value)
213
+ return;
214
+ const newValue = deepClone(value), columnCount = (_a3 = value == null ? void 0 : value.rows[0].cells.length) != null ? _a3 : 0;
215
+ for (let i = 0; i < count; i++)
216
+ newValue.rows.push({
217
+ _type: rowType,
218
+ _key: v4(),
219
+ cells: Array(columnCount).fill("")
220
+ });
221
+ return updateValue(newValue);
222
+ }, addRowAt = (index = 0) => {
223
+ if (!value)
224
+ return;
225
+ const newValue = deepClone(value), columnCount = value.rows[0].cells.length;
226
+ return newValue.rows.splice(index, 0, {
227
+ _type: rowType,
228
+ _key: v4(),
229
+ cells: Array(columnCount).fill("")
230
+ }), updateValue(newValue);
231
+ }, removeRow = (index) => {
232
+ if (!value)
233
+ return;
234
+ const newValue = deepClone(value);
235
+ newValue.rows.splice(index, 1), updateValue(newValue), setDialog(null);
236
+ }, confirmRemoveRow = (index) => {
237
+ if (value)
238
+ return value.rows.length <= 1 ? confirmRemoveTable() : setDialog({ type: "row", callback: () => removeRow(index) });
239
+ }, confirmRemoveColumn = (index) => {
240
+ if (value)
241
+ return value.rows[0].cells.length <= 1 ? confirmRemoveTable() : setDialog({ type: "column", callback: () => removeColumn(index) });
242
+ }, addColumns = (count) => {
243
+ if (!value)
244
+ return;
245
+ const newValue = deepClone(value);
246
+ return newValue.rows.forEach((_, i) => {
247
+ for (let j = 0; j < count; j++)
248
+ newValue.rows[i].cells.push("");
249
+ }), updateValue(newValue);
250
+ }, addColumnAt = (index) => {
251
+ if (!value)
252
+ return;
253
+ const newValue = deepClone(value);
254
+ return newValue.rows.forEach((_, i) => {
255
+ newValue.rows[i].cells.splice(index, 0, "");
256
+ }), updateValue(newValue);
257
+ }, removeColumn = (index) => {
258
+ if (!value)
259
+ return;
260
+ const newValue = deepClone(value);
261
+ newValue.rows.forEach((row) => {
262
+ row.cells.splice(index, 1);
263
+ }), updateValue(newValue), setDialog(null);
264
+ }, updateCell = (e, rowIndex, cellIndex) => {
265
+ if (!value)
266
+ return;
267
+ const newValue = deepClone(value);
268
+ return newValue.rows[rowIndex].cells[cellIndex] = e.target.value, updateValue(newValue);
269
+ };
270
+ return /* @__PURE__ */ jsxs("div", { children: [
271
+ dialog && /* @__PURE__ */ jsx(
272
+ Dialog,
273
+ {
274
+ header: `Remove ${dialog.type}`,
275
+ id: "dialog-remove",
276
+ onClose: () => setDialog(null),
277
+ zOffset: 1e3,
278
+ children: /* @__PURE__ */ jsxs(Card, { padding: 4, children: [
279
+ /* @__PURE__ */ jsxs(Text, { children: [
280
+ "Are you sure you want to remove this ",
281
+ dialog.type,
282
+ "?"
283
+ ] }),
284
+ /* @__PURE__ */ jsx(Box, { marginTop: 4, children: /* @__PURE__ */ jsxs(Inline, { space: 1, style: { textAlign: "right" }, children: [
285
+ /* @__PURE__ */ jsx(
286
+ Button,
287
+ {
288
+ text: "Cancel",
289
+ mode: "ghost",
290
+ onClick: () => setDialog(null)
291
+ }
292
+ ),
293
+ /* @__PURE__ */ jsx(
294
+ Button,
295
+ {
296
+ text: "Confirm",
297
+ tone: "critical",
298
+ onClick: () => dialog.callback()
299
+ }
300
+ )
301
+ ] }) })
302
+ ] })
303
+ }
304
+ ),
305
+ /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Flex, { justify: "flex-end", children: ((_a2 = value == null ? void 0 : value.rows) == null ? void 0 : _a2.length) && /* @__PURE__ */ jsx(
306
+ TableMenu,
307
+ {
308
+ addColumns,
309
+ addColumnAt,
310
+ addRows,
311
+ addRowAt,
312
+ remove: confirmRemoveTable,
313
+ placement: "left"
314
+ }
315
+ ) }) }),
316
+ ((_b = value == null ? void 0 : value.rows) == null ? void 0 : _b.length) && /* @__PURE__ */ jsx(
317
+ TableInput,
318
+ {
319
+ rows: value.rows,
320
+ removeRow: confirmRemoveRow,
321
+ removeColumn: confirmRemoveColumn,
322
+ updateCell
323
+ }
324
+ ),
325
+ (!value || !((_c = value == null ? void 0 : value.rows) != null && _c.length)) && /* @__PURE__ */ jsx(Inline, { space: 1, children: /* @__PURE__ */ jsx(
326
+ Button,
327
+ {
328
+ fontSize: 1,
329
+ padding: 3,
330
+ icon: AddIcon,
331
+ text: "Create Table",
332
+ tone: "primary",
333
+ mode: "ghost",
334
+ onClick: createTable
335
+ }
336
+ ) })
337
+ ] });
338
+ };
339
+ function createTableComponent(rowType) {
340
+ return function(props) {
341
+ return /* @__PURE__ */ jsx(TableComponent, __spreadProps(__spreadValues({}, props), { rowType }));
342
+ };
343
+ }
344
+ function TableIcon() {
345
+ return /* @__PURE__ */ jsx(
346
+ "svg",
347
+ {
348
+ width: "1em",
349
+ height: "1em",
350
+ viewBox: "0 0 25 25",
351
+ fill: "none",
352
+ stroke: "currentColor",
353
+ strokeWidth: "1.2",
354
+ children: /* @__PURE__ */ jsx("path", { d: "M3 3h18v18H3zM21 9H3M21 15H3M12 3v18" })
355
+ }
356
+ );
357
+ }
358
+ const Table = ({ rows }) => {
359
+ const numCols = rows.length === 0 ? 0 : rows[0].cells.length;
360
+ return /* @__PURE__ */ jsx(Grid, { columns: numCols, padding: 2, children: rows.map(
361
+ (row) => row.cells.map((cell, i) => /* @__PURE__ */ jsx(
362
+ Card,
363
+ {
364
+ padding: 2,
365
+ style: { outline: "1px solid #DFE2E9" },
366
+ children: /* @__PURE__ */ jsx(Text, { style: { textOverflow: "elipsis" }, children: cell })
367
+ },
368
+ row._key + i
369
+ ))
370
+ ) });
371
+ }, TablePreview = (props) => {
372
+ var _a2;
373
+ const { schemaType, rows = [], title = "Title missing" } = props;
374
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
375
+ /* @__PURE__ */ jsx(Box, { padding: 3, children: /* @__PURE__ */ jsxs(Inline, { space: 3, children: [
376
+ /* @__PURE__ */ jsx(Card, { children: /* @__PURE__ */ jsx(Label, { size: 4, children: /* @__PURE__ */ jsx(TableIcon, {}) }) }),
377
+ /* @__PURE__ */ jsx(Card, { children: /* @__PURE__ */ jsx(Text, { children: (_a2 = schemaType == null ? void 0 : schemaType.title) != null ? _a2 : title }) })
378
+ ] }) }),
379
+ /* @__PURE__ */ jsx(Box, { padding: 2, children: rows.length === 0 ? /* @__PURE__ */ jsx(Label, { muted: !0, children: "Empty Table" }) : /* @__PURE__ */ jsx(Table, { rows }) })
380
+ ] });
381
+ }, table = definePlugin((config) => {
382
+ const tableRowSchema = defineType({
383
+ title: "Table Row",
384
+ name: (config == null ? void 0 : config.rowType) || "tableRow",
385
+ type: "object",
386
+ fields: [
387
+ {
388
+ name: "cells",
389
+ type: "array",
390
+ of: [{ type: "string" }]
391
+ }
392
+ ]
393
+ }), tableSchema = defineType({
394
+ title: "Table",
395
+ name: "table",
396
+ type: "object",
397
+ fields: [
398
+ {
399
+ name: "rows",
400
+ type: "array",
401
+ of: [
402
+ {
403
+ type: tableRowSchema.name
404
+ }
405
+ ]
406
+ }
407
+ ],
408
+ components: {
409
+ /* eslint-disable @typescript-eslint/no-explicit-any */
410
+ input: createTableComponent(tableRowSchema.name),
411
+ preview: TablePreview
412
+ /* eslint-enable @typescript-eslint/no-explicit-any */
413
+ },
414
+ preview: {
415
+ select: {
416
+ rows: "rows",
417
+ title: "title"
418
+ },
419
+ prepare: ({ title, rows = [] }) => ({
420
+ title,
421
+ rows
422
+ })
423
+ }
424
+ });
425
+ return {
426
+ name: "table",
427
+ schema: {
428
+ types: [tableRowSchema, tableSchema]
429
+ }
430
+ };
431
+ });
432
+ export {
433
+ TableComponent,
434
+ TablePreview,
435
+ table
436
+ };
2
437
  //# sourceMappingURL=index.js.map