@revolist/revogrid
Version:
Virtual reactive data grid spreadsheet component - RevoGrid.
4 lines • 19.7 kB
JavaScript
/*!
* Built by Revolist OU ❤️
*/
import{h as t,proxyCustomElement as e,HTMLElement as i,createEvent as s,Host as o,transformTag as n}from"@stencil/core/internal/client";import{g as r,M as a,z as l,N as h,b as c}from"./column.service.js";import{c as d}from"./platform.js";import{R as u,i as g,j as p,S as v}from"./consts.js";import{b as f,i as b,c as m,a as y,g as x,d as w,e as j,s as O,f as S,v as D}from"./selection.utils.js";import{l as C,m as M,c as k,d as R,f as E,h as A,g as F,j as T,k as P,o as z}from"./revogr-edit2.js";import{t as K}from"./index2.js";import{d as U}from"./debounce.js";import{d as X}from"./revogr-clipboard2.js";import{d as $}from"./revogr-order-editor2.js";const H=[d.TAB,d.ARROW_UP,d.ARROW_DOWN,d.ARROW_LEFT,d.ARROW_RIGHT];class L{constructor(t){this.sv=t}appendPendingEditValue(t){if(C(t)||1!==t.key.length||t.target instanceof HTMLElement&&M(t.target))return!1;const e=this.sv.selectionStore.get("edit");return"string"==typeof(null==e?void 0:e.val)&&(this.sv.selectionStore.set("edit",Object.assign(Object.assign({},e),{val:`${e.val}${t.key}`})),!0)}async keyDown(t,e,i,{range:s,focus:o}){if(i){if(this.appendPendingEditValue(t))return;switch(t.code){case d.ESCAPE:this.sv.cancel();break;case d.TAB:this.keyChangeSelection(t,e)}}else s&&k(t.code)?this.sv.clearCell():o&&(R(t.code)?this.keyChangeSelection(t,e):E(t.key)?this.sv.change():A(t)||F(t)||(T(t)?this.sv.internalPaste():P(t)?e&&this.selectAll(t):C(t)||1!==t.key.length?await this.keyChangeSelection(t,e):this.sv.change(t.key)))}selectAll(t){const e=this.sv.selectionStore.get("range"),i=this.sv.selectionStore.get("focus");e&&i&&(t.preventDefault(),this.sv.selectAll())}async keyChangeSelection(t,e){const i=this.changeDirectionKey(t,e);if(!i)return!1;await K(u+30);const s=this.sv.selectionStore.get("range"),o=this.sv.selectionStore.get("focus");return this.keyPositionChange(i.changes,s,o,i.isMulti)}keyPositionChange(t,e,i,s=!1){if(!e||!i)return!1;const o=f(e,i,t,s);if(!o)return!1;const n=this.sv.getData();if(s){if([o.start,o.end].some((t=>b(t,n.lastCell)||m(t))))return!1;const t=r(o.start,o.end);return this.sv.range(t)}return this.sv.focus(o.start,t,b(o.start,n.lastCell)?1:m(o.start)?-1:0)}changeDirectionKey(t,e){const i=e&&t.shiftKey;if(H.includes(t.code)&&t.preventDefault(),t.shiftKey&&t.code===d.TAB)return{changes:{x:-1},isMulti:!1};switch(t.code){case d.ARROW_UP:return{changes:{y:-1},isMulti:i};case d.ARROW_DOWN:return{changes:{y:1},isMulti:i};case d.ARROW_LEFT:return{changes:{x:-1},isMulti:i};case d.TAB:case d.ARROW_RIGHT:return{changes:{x:1},isMulti:i}}}}class N{constructor(t){this.sv=t,this.autoFillType=null,this.autoFillInitial=null,this.autoFillStart=null,this.autoFillLast=null}renderAutofill(e,i,s=!1){let o;return o=y(e||Object.assign(Object.assign({},i),{x1:i.x,y1:i.y}),this.sv.dimensionRow.state,this.sv.dimensionCol.state),t("div",{class:{[p]:!0,[g]:s},style:{left:`${o.right}px`,top:`${o.bottom}px`},onMouseDown:t=>this.autoFillHandler(t),onTouchStart:t=>this.autoFillHandler(t)})}autoFillHandler(t,e="AutoFill"){let i=null;t.target instanceof Element&&(i=t.target),i&&(this.selectionStart(i,this.sv.getData(),e),t.preventDefault())}get isAutoFill(){return!!this.autoFillType}selectionMouseMove(t){this.onMouseMoveAutofill||(this.onMouseMoveAutofill=U(((t,e)=>this.doAutofillMouseMove(t,e)),5)),this.isAutoFill&&this.onMouseMoveAutofill(t,this.sv.getData())}getFocus(t,e){return!t&&e&&(t={x:e.x,y:e.y}),t||null}doAutofillMouseMove(t,e){if(!this.autoFillInitial)return;const i=x(t,"clientX",g),s=x(t,"clientY",g);if(null===i||null===s)return;const o=w({x:i,y:s},e);if(this.autoFillLast||this.autoFillLast||(this.autoFillLast=this.autoFillStart),!b(o,e.lastCell))if(this.autoFillLast=o,o.x===this.autoFillInitial.x&&o.y===this.autoFillInitial.y)this.sv.setTempRange(null);else{const t=r(this.autoFillInitial,this.autoFillLast);this.sv.setTempRange({area:t,type:this.autoFillType})}}selectionStart(t,e,i="Selection"){const{top:s,left:o}=t.getBoundingClientRect();this.autoFillInitial=this.getFocus(e.focus,e.range),this.autoFillType=i,this.autoFillStart=w({x:o,y:s},e)}clearAutoFillSelection(t,e){if(this.autoFillInitial)if(this.autoFillInitial=this.getFocus(t,e),"AutoFill"===this.autoFillType){const t=r(this.autoFillInitial,this.autoFillLast);if(t){const{defaultPrevented:i,detail:{range:s}}=this.sv.clearRangeDataApply({range:t});!i&&e?this.applyRangeWithData(s,e):this.sv.setTempRange(null)}}else this.applyRangeOnly(this.autoFillInitial,this.autoFillLast);this.resetAutoFillState()}resetAutoFillState(){this.autoFillType=null,this.autoFillInitial=null,this.autoFillLast=null,this.autoFillStart=null}onRangeApply(t,e,i){this.sv.rangeDataApply({data:t,models:j(t,this.sv.dataStore),type:this.sv.dataStore.get("type"),oldRange:i,newRange:e}),this.sv.setRange(e)}applyRangeWithData(t,e){const i={type:this.sv.dataStore.get("type"),colType:this.sv.columnService.type,newData:{},mapping:{},newRange:t,oldRange:e},{mapping:s,changed:o}=this.sv.columnService.getRangeData(i,this.sv.columnService.columns);i.newData=o,i.mapping=s;let n=this.sv.selectionChanged(i);n.defaultPrevented?this.sv.setTempRange(null):(n=this.sv.rangeCopy(i),n.defaultPrevented?this.sv.setRange(t):this.onRangeApply(i.newData,t,e))}applyRangeOnly(t,e){if(!t||!e)return;const i=r(t,e);this.sv.setRange(i)}}const W=e(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.beforeCopyRegion=s(this,"beforecopyregion",7),this.beforeRegionPaste=s(this,"beforepasteregion",7),this.cellEditApply=s(this,"celleditapply",7),this.beforeFocusCell=s(this,"beforecellfocusinit",7),this.beforeNextViewportFocus=s(this,"beforenextvpfocus",7),this.setEdit=s(this,"setedit",7),this.beforeApplyRange=s(this,"beforeapplyrange",7),this.beforeSetRange=s(this,"beforesetrange",7),this.setRange=s(this,"setrange",7),this.beforeEditRender=s(this,"beforeeditrender",7),this.selectAll=s(this,"selectall",7),this.cancelEdit=s(this,"canceledit",7),this.setTempRange=s(this,"settemprange",7),this.beforeSetTempRange=s(this,"beforesettemprange",7),this.applyFocus=s(this,"applyfocus",7),this.focusCell=s(this,"focuscell",7),this.beforeRangeDataApply=s(this,"beforerangedataapply",7),this.selectionChange=s(this,"selectionchangeinit",7),this.beforeRangeCopyApply=s(this,"beforerangecopyapply",7),this.rangeEditApply=s(this,"rangeeditapply",7),this.rangeClipboardCopy=s(this,"clipboardrangecopy",7),this.rangeClipboardPaste=s(this,"clipboardrangepaste",7),this.beforeKeyDown=s(this,"beforekeydown",7),this.beforeKeyUp=s(this,"beforekeyup",7),this.beforeCellSave=s(this,"beforecellsave",7),this.cellEditDone=s(this,"celledit",7),this.applyChangesOnClose=!1,this.keyboardService=null,this.autoFillService=null,this.unsubscribeSelectionStore=[]}onMouseMove(t){var e;this.selectionStore.get("focus")&&(null===(e=this.autoFillService)||void 0===e||e.selectionMouseMove(t))}onMouseUp(){var t;null===(t=this.autoFillService)||void 0===t||t.clearAutoFillSelection(this.selectionStore.get("focus"),this.selectionStore.get("range"))}onCellDrag(t){var e;null===(e=this.orderEditor)||void 0===e||e.dragStart(t.detail)}onKeyUp(t){this.beforeKeyUp.emit(Object.assign({original:t},this.getData()))}onKeyDown(t){var e;const i=this.beforeKeyDown.emit(Object.assign({original:t},this.getData()));t.defaultPrevented||i.defaultPrevented||null===(e=this.keyboardService)||void 0===e||e.keyDown(t,this.range,!!this.selectionStore.get("edit"),{focus:this.selectionStore.get("focus"),range:this.selectionStore.get("range")})}selectionServiceSet(t){this.unsubscribeSelectionStore.forEach((t=>t())),this.unsubscribeSelectionStore.length=0,this.unsubscribeSelectionStore.push(t.onChange("nextFocus",(t=>t&&this.doFocus(t,t)))),this.keyboardService=new L({selectionStore:t,range:t=>!!t&&this.triggerRangeEvent(t),focus:(t,e,i)=>i?(this.beforeNextViewportFocus.emit(t),!1):this.doFocus(t,t,e),change:t=>{this.readonly||this.doEdit(t)},cancel:async()=>{var t;await(null===(t=this.revogrEdit)||void 0===t?void 0:t.cancelChanges()),this.closeEdit()},clearCell:()=>!this.readonly&&this.clearCell(),internalPaste:()=>!this.readonly&&this.beforeRegionPaste.emit(),getData:()=>this.getData(),selectAll:()=>this.selectAll.emit()}),this.createAutoFillService()}createAutoFillService(){this.autoFillService=new N({dimensionRow:this.dimensionRow,dimensionCol:this.dimensionCol,columnService:this.columnService,dataStore:this.dataStore,clearRangeDataApply:t=>this.beforeRangeDataApply.emit(Object.assign(Object.assign(Object.assign({},t),this.types),{rowDimension:Object.assign({},this.dimensionRow.state),colDimension:Object.assign({},this.dimensionCol.state)})),setTempRange:t=>{const e=this.beforeSetTempRange.emit(Object.assign(Object.assign({tempRange:t},this.getData()),this.types));return e.defaultPrevented?null:this.setTempRange.emit(e.detail.tempRange)},selectionChanged:t=>this.selectionChange.emit(t),rangeCopy:t=>this.beforeRangeCopyApply.emit(t),rangeDataApply:t=>this.rangeEditApply.emit(t),setRange:t=>!!t&&this.triggerRangeEvent(t),getData:()=>this.getData()})}columnServiceSet(){var t;null===(t=this.columnService)||void 0===t||t.destroy(),this.columnService=new a(this.dataStore,this.colData),this.createAutoFillService()}connectedCallback(){this.columnServiceSet(),this.selectionServiceSet(this.selectionStore)}disconnectedCallback(){var t;this.unsubscribeSelectionStore.forEach((t=>t())),this.unsubscribeSelectionStore.length=0,null===(t=this.columnService)||void 0===t||t.destroy()}async componentWillRender(){var t,e;this.selectionStore.get("edit")||await(null===(e=null===(t=this.revogrEdit)||void 0===t?void 0:t.beforeDisconnect)||void 0===e?void 0:e.call(t))}renderRange(e){const i=y(e,this.dimensionRow.state,this.dimensionCol.state),s=O(i);return[t("div",{class:v,style:s},this.isMobileDevice&&t("div",{class:"range-handlers"},t("span",{class:g}),t("span",{class:g})))]}renderEditor(){const e=this.selectionStore.get("edit");if(this.readonly||!e)return null;const i=e.val||l(this.columnService.rowDataModel(e.y,e.x).value),s=Object.assign(Object.assign({},e),this.columnService.getSaveData(e.y,e.x,i)),o=this.beforeEditRender.emit(Object.assign(Object.assign({range:Object.assign(Object.assign({},e),{x1:e.x,y1:e.y})},this.types),{rowDimension:Object.assign({},this.dimensionRow.state),colDimension:Object.assign({},this.dimensionCol.state)}));if(o.defaultPrevented)return null;const n=y(o.detail.range,o.detail.rowDimension,o.detail.colDimension),r=O(n);return t("revogr-edit",{style:r,ref:t=>this.revogrEdit=t,additionalData:this.additionalData,editCell:s,saveOnClose:this.applyChangesOnClose,onCelleditinit:t=>{this.cellEditDone.emit(t.detail)},column:this.columnService.rowDataModel(e.y,e.x),editor:h(this.columnService.columns[e.x],this.editors)})}onEditCell(t){if(t.defaultPrevented)return;const e=this.beforeCellSave.emit(t.detail);e.defaultPrevented||this.cellEdit(e.detail),e.detail.preventFocus||this.focusNext()}render(){var e;const i=[],s=this.renderEditor();if(s)i.push(s);else{const s=this.selectionStore.get("range"),o=this.selectionStore.get("focus");(s||o)&&this.useClipboard&&i.push(t("revogr-clipboard",{readonly:this.readonly,onCopyregion:t=>this.onCopy(t.detail),onClearregion:()=>!this.readonly&&this.clearCell(),ref:t=>this.clipboard=t,onPasteregion:t=>this.onPaste(t.detail)})),s&&i.push(...this.renderRange(s)),o&&!this.readonly&&this.range&&i.push(null===(e=this.autoFillService)||void 0===e?void 0:e.renderAutofill(s,o,this.isMobileDevice)),this.canDrag&&i.push(t("revogr-order-editor",{ref:t=>this.orderEditor=t,dataStore:this.dataStore,dimensionRow:this.dimensionRow,dimensionCol:this.dimensionCol,parent:this.element,rowType:this.types.rowType,onRowdragstartinit:t=>this.rowDragStart(t)}))}return t(o,{key:"d936e8452e84c7a25ecd6502e929f1a5af69467f",class:{mobile:this.isMobileDevice},onDblClick:t=>this.onElementDblClick(t),onMouseDown:t=>this.onElementMouseDown(t),onTouchStart:t=>this.onElementMouseDown(t,!0),onCloseedit:t=>this.closeEdit(t),onCelledit:t=>this.onEditCell(t)},i,t("slot",{key:"cd3525d404aa44fd8d06e7fc459777acb8a9d585",name:"data"}))}doFocus(t,e,i){const{defaultPrevented:s}=this.beforeFocusCell.emit(this.columnService.getSaveData(t.y,t.x));if(s)return!1;const o=Object.assign(Object.assign({range:Object.assign(Object.assign({},t),{x1:e.x,y1:e.y}),next:i},this.types),{rowDimension:Object.assign({},this.dimensionRow.state),colDimension:Object.assign({},this.dimensionCol.state)}),n=this.applyFocus.emit(o);if(n.defaultPrevented)return!1;const{range:r}=n.detail;return!this.focusCell.emit(Object.assign({focus:{x:r.x,y:r.y},end:{x:r.x1,y:r.y1}},n.detail)).defaultPrevented}triggerRangeEvent(t){const e=this.types.rowType,i=this.beforeApplyRange.emit(Object.assign(Object.assign({range:Object.assign({},t)},this.types),{rowDimension:Object.assign({},this.dimensionRow.state),colDimension:Object.assign({},this.dimensionCol.state)}));if(i.defaultPrevented)return!1;const s=this.columnService.getRangeTransformedToProps(i.detail.range,this.dataStore);let o=this.beforeSetRange.emit(s);return!o.defaultPrevented&&(o=this.setRange.emit(Object.assign(Object.assign({},i.detail.range),{type:e})),!o.defaultPrevented&&!o.defaultPrevented)}onElementDblClick(t){if(t.defaultPrevented)return;const e=this.getData();S(t,e)&&this.doEdit()}onElementMouseDown(t,e=!1){var i;const s=t.target;if(M(s)||t.defaultPrevented)return;const o=this.getData(),n=S(t,o);n&&(this.focus(n,this.range&&t.shiftKey),this.range&&(s&&(null===(i=this.autoFillService)||void 0===i||i.selectionStart(s,this.getData())),e?D(t.touches[0],g)&&t.preventDefault():t.preventDefault()))}doEdit(t=""){var e;if(this.canEdit()){const i=this.selectionStore.get("focus");if(!i)return;const s=this.columnService.getSaveData(i.y,i.x);null===(e=this.setEdit)||void 0===e||e.emit(Object.assign(Object.assign({},s),{val:t}))}}async closeEdit(t){this.cancelEdit.emit(),(null==t?void 0:t.detail)&&await this.focusNext()}cellEdit(t){const e=this.columnService.getSaveData(t.rgRow,t.rgCol,t.val);this.cellEditApply.emit(e)}getRegion(){const t=this.selectionStore.get("focus");let e=this.selectionStore.get("range");return e||(e=r(t,t)),e}onCopy(t){var e;const i=this.getRegion();if(this.beforeCopyRegion.emit(i).defaultPrevented)return!1;let s;if(i){const{data:t,mapping:e}=this.columnService.copyRangeArray(i,this.dataStore),o=this.rangeClipboardCopy.emit(Object.assign({range:i,data:t,mapping:e},this.types));o.defaultPrevented||(s=o.detail.data)}return null===(e=this.clipboard)||void 0===e||e.doCopy(t,s),!0}onPaste(t){var e;const i=this.selectionStore.get("focus"),s=null!==this.selectionStore.get("edit");if(!i||s)return;let{changed:o,range:n}=this.columnService.getTransformedDataToApply(i,t);const{defaultPrevented:r}=this.rangeClipboardPaste.emit(Object.assign({data:o,models:j(o,this.dataStore),range:n},this.types));r||null===(e=this.autoFillService)||void 0===e||e.onRangeApply(o,n,n)}async focusNext(){var t;await(null===(t=this.keyboardService)||void 0===t?void 0:t.keyChangeSelection(new KeyboardEvent("keydown",{code:d.ARROW_DOWN}),this.range))||this.closeEdit()}clearCell(){var t;const e=this.selectionStore.get("range");if(e&&!c(e)){const i=this.columnService.getRangeStaticData(e,"");null===(t=this.autoFillService)||void 0===t||t.onRangeApply(i,e,e)}else if(this.canEdit()){const t=this.selectionStore.get("focus");if(!t)return;const e=this.columnService.getSaveData(t.y,t.x);this.cellEdit({rgRow:t.y,rgCol:t.x,val:"",type:e.type,prop:e.prop})}}rowDragStart({detail:t}){t.text=l(this.columnService.rowDataModel(t.cell.y,t.cell.x).value)}canEdit(){var t;if(this.readonly)return!1;const e=this.selectionStore.get("focus");return e&&!(null===(t=this.columnService)||void 0===t?void 0:t.isReadOnly(e.y,e.x))}get edited(){return this.selectionStore.get("edit")}focus(t,e=!1){if(!t)return!1;const i=t,s=this.selectionStore.get("focus");if(e&&s){const t=r(s,i);if(t)return this.triggerRangeEvent(t)}return this.doFocus(t,i)}get types(){return{rowType:this.dataStore.get("type"),colType:this.columnService.type}}getData(){return{el:this.element,rows:this.dimensionRow.state,cols:this.dimensionCol.state,lastCell:this.lastCell,focus:this.selectionStore.get("focus"),range:this.selectionStore.get("range"),edit:this.selectionStore.get("edit")}}get element(){return this}static get watchers(){return{selectionStore:[{selectionServiceSet:0}],dimensionRow:[{createAutoFillService:0}],dimensionCol:[{createAutoFillService:0}],dataStore:[{columnServiceSet:0}],colData:[{columnServiceSet:0}]}}static get style(){return'revogr-overlay-selection{display:block;position:relative;width:100%}revogr-overlay-selection .autofill-handle{position:absolute;width:14px;height:14px;margin-left:-13px;margin-top:-13px;z-index:10;cursor:crosshair}revogr-overlay-selection .autofill-handle::before{content:"";position:absolute;right:0;bottom:0;width:10px;height:10px;background:#0d63e8;border:1px solid white;box-sizing:border-box}revogr-overlay-selection.mobile .autofill-handle{position:absolute;width:30px;height:30px;margin-left:-29px;margin-top:-29px;z-index:10;cursor:crosshair}revogr-overlay-selection.mobile .autofill-handle::before{content:"";position:absolute;right:0;bottom:0;width:12px;height:12px;background:#0d63e8;border:1px solid white;box-sizing:border-box}revogr-overlay-selection .selection-border-range{position:absolute;pointer-events:none;z-index:9;box-shadow:-1px 0 0 #0d63e8 inset, 1px 0 0 #0d63e8 inset, 0 -1px 0 #0d63e8 inset, 0 1px 0 #0d63e8 inset}revogr-overlay-selection .selection-border-range .range-handlers{height:100%;background-color:transparent;width:75%;max-width:50px;min-width:20px;left:50%;transform:translateX(-50%);position:absolute}revogr-overlay-selection .selection-border-range .range-handlers>span{pointer-events:auto;height:20px;width:20px;position:absolute;left:50%;transform:translateX(-50%)}revogr-overlay-selection .selection-border-range .range-handlers>span:before,revogr-overlay-selection .selection-border-range .range-handlers>span:after{position:absolute;border-radius:5px;width:15px;height:5px;left:50%;transform:translateX(-50%);background-color:rgba(0, 0, 0, 0.2)}revogr-overlay-selection .selection-border-range .range-handlers>span:first-child{top:-7px}revogr-overlay-selection .selection-border-range .range-handlers>span:first-child:before{content:"";top:0}revogr-overlay-selection .selection-border-range .range-handlers>span:last-child{bottom:-7px}revogr-overlay-selection .selection-border-range .range-handlers>span:last-child:after{content:"";bottom:0}revogr-overlay-selection revogr-edit{z-index:10}'}},[260,"revogr-overlay-selection",{readonly:[4],range:[4],canDrag:[4,"can-drag"],useClipboard:[4,"use-clipboard"],selectionStore:[16],dimensionRow:[16],dimensionCol:[16],dataStore:[16],colData:[16],lastCell:[16],editors:[16],applyChangesOnClose:[4,"apply-changes-on-close"],additionalData:[8,"additional-data"],isMobileDevice:[4,"is-mobile-device"]},[[5,"touchmove","onMouseMove"],[5,"mousemove","onMouseMove"],[5,"touchend","onMouseUp"],[5,"mouseup","onMouseUp"],[5,"mouseleave","onMouseUp"],[0,"dragstartcell","onCellDrag"],[4,"keyup","onKeyUp"],[4,"keydown","onKeyDown"]],{selectionStore:[{selectionServiceSet:0}],dimensionRow:[{createAutoFillService:0}],dimensionCol:[{createAutoFillService:0}],dataStore:[{columnServiceSet:0}],colData:[{columnServiceSet:0}]}]);function V(){"undefined"!=typeof customElements&&["revogr-overlay-selection","revogr-clipboard","revogr-edit","revogr-order-editor"].forEach((t=>{switch(t){case"revogr-overlay-selection":customElements.get(n(t))||customElements.define(n(t),W);break;case"revogr-clipboard":customElements.get(n(t))||X();break;case"revogr-edit":customElements.get(n(t))||z();break;case"revogr-order-editor":customElements.get(n(t))||$()}}))}export{W as O,V as d}