editor.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import browser from './browser';
  2. import formula from './formula';
  3. import { datagridgrowth } from './getdata';
  4. import { jfrefreshgrid, jfrefreshgridall, jfrefreshrange } from './refresh';
  5. import { getSheetIndex } from '../methods/get';
  6. import Store from '../store';
  7. const editor = {
  8. //worker+blob实现深拷贝替换extend
  9. deepCopyFlowDataState:false,
  10. deepCopyFlowDataCache:"",
  11. deepCopyFlowDataWorker:null,
  12. deepCopyFlowData:function(flowData){
  13. let _this = this;
  14. if(_this.deepCopyFlowDataState){
  15. if(_this.deepCopyFlowDataWorker != null){
  16. _this.deepCopyFlowDataWorker.terminate();
  17. }
  18. return _this.deepCopyFlowDataCache;
  19. }
  20. else{
  21. if(flowData == null){
  22. flowData = Store.flowdata;
  23. }
  24. return $.extend(true, [], flowData);
  25. }
  26. },
  27. webWorkerFlowDataCache:function(flowData){
  28. let _this = this;
  29. try{
  30. if(_this.deepCopyFlowDataWorker != null){//存新的webwork前先销毁以前的
  31. _this.deepCopyFlowDataWorker.terminate();
  32. }
  33. let funcTxt = 'data:text/javascript;chartset=US-ASCII,onmessage = function (e) { postMessage(e.data); };';
  34. _this.deepCopyFlowDataState = false;
  35. //适配IE
  36. let worker;
  37. if(browser.isIE() == 1){
  38. let response = "self.onmessage=function(e){postMessage(e.data);}";
  39. worker = new Worker('./plugins/Worker-helper.js');
  40. worker.postMessage(response);
  41. }
  42. else{
  43. worker = new Worker(funcTxt);
  44. }
  45. _this.deepCopyFlowDataWorker = worker;
  46. worker.postMessage(flowData);
  47. worker.onmessage = function(e) {
  48. _this.deepCopyFlowDataCache = e.data;
  49. _this.deepCopyFlowDataState = true;
  50. };
  51. }
  52. catch(e){
  53. _this.deepCopyFlowDataCache = $.extend(true, [], flowData);
  54. }
  55. },
  56. /**
  57. * @param {Array} dataChe
  58. * @param {Object} range 是否指定选区,默认为当前选区
  59. * @since Add range parameter. Update by siwei@2020-09-10.
  60. */
  61. controlHandler: function (dataChe, range) {
  62. let _this = this;
  63. let d = _this.deepCopyFlowData(Store.flowdata);//取数据
  64. // let last = Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1];
  65. let last = range || Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1];
  66. let curR = last["row"] == null ? 0 : last["row"][0];
  67. let curC = last["column"] == null ? 0 : last["column"][0];
  68. let rlen = dataChe.length, clen = dataChe[0].length;
  69. let addr = curR + rlen - d.length, addc = curC + clen - d[0].length;
  70. if(addr > 0 || addc > 0){
  71. d = datagridgrowth([].concat(d), addr, addc, true);
  72. }
  73. for (let r = 0; r < rlen; r++) {
  74. let x = [].concat(d[r + curR]);
  75. for (let c = 0; c < clen; c++) {
  76. let value = "";
  77. if (dataChe[r] != null && dataChe[r][c] != null) {
  78. value = dataChe[r][c];
  79. }
  80. x[c + curC] = value;
  81. }
  82. d[r + curR] = x;
  83. }
  84. if (addr > 0 || addc > 0) {
  85. jfrefreshgridall(d[0].length, d.length, d, null, Store.luckysheet_select_save, "datachangeAll");
  86. }
  87. else {
  88. jfrefreshrange(d, Store.luckysheet_select_save);
  89. }
  90. },
  91. clearRangeByindex: function (st_r, ed_r, st_c, ed_c, sheetIndex) {
  92. let index = getSheetIndex(sheetIndex);
  93. let d = $.extend(true, [], Store.luckysheetfile[index]["data"]);
  94. for (let r = st_r; r <= ed_r; r++) {
  95. let x = [].concat(d[r]);
  96. for (let c = st_c; c <= ed_c; c++) {
  97. formula.delFunctionGroup(r, c);
  98. formula.execFunctionGroup(r, c, "");
  99. x[c] = null;
  100. }
  101. d[r] = x;
  102. }
  103. if(sheetIndex == Store.currentSheetIndex){
  104. let rlen = ed_r - st_r + 1,
  105. clen = ed_c - st_c + 1;
  106. if (rlen > 5000) {
  107. jfrefreshgrid(d, [{ "row": [st_r, ed_r], "column": [st_c, ed_c] }]);
  108. }
  109. else {
  110. jfrefreshrange(d, { "row": [st_r, ed_r], "column": [st_c, ed_c] });
  111. }
  112. }
  113. else{
  114. Store.luckysheetfile[index]["data"] = d;
  115. }
  116. },
  117. controlHandlerD: function (dataChe) {
  118. let _this = this;
  119. let d = _this.deepCopyFlowData(Store.flowdata);//取数据
  120. let last = Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1];
  121. let r1 = last["row"][0], r2 = last["row"][1];
  122. let c1 = last["column"][0], c2 = last["column"][1];
  123. let rlen = dataChe.length, clen = dataChe[0].length;
  124. let addr = r1 + rlen - d.length, addc = c1 + clen - d[0].length;
  125. if(addr >0 || addc > 0){
  126. d = datagridgrowth([].concat(d), addr, addc, true);
  127. }
  128. for(let r = r1; r <= r2; r++){
  129. for(let c = c1; c <= c2; c++){
  130. d[r][c] = null;
  131. }
  132. }
  133. for(let i = 0; i < rlen; i++){
  134. for(let j = 0; j < clen; j++){
  135. d[r1 + i][c1 + j] = dataChe[i][j];
  136. }
  137. }
  138. let range = [
  139. { "row": [r1, r2], "column": [c1, c2] },
  140. { "row": [r1, r1 + rlen - 1], "column": [c1, c1 + clen - 1] }
  141. ];
  142. jfrefreshgrid(d, range);
  143. }
  144. };
  145. export default editor;