facade.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { SortType as r, SortRangeCommand as c } from "@univerjs/sheets-sort";
  2. import { FRange as S, FWorksheet as l, FSheetEventName as R } from "@univerjs/sheets/facade";
  3. import { RANGE_TYPE as k, FEventName as w, FUniver as C, ICommandService as p } from "@univerjs/core";
  4. class f extends S {
  5. sort(o) {
  6. const e = this._range.startColumn, s = (Array.isArray(o) ? o : [o]).map((n) => typeof n == "number" ? { colIndex: n + e, type: r.ASC } : {
  7. colIndex: n.column + e,
  8. type: n.ascending ? r.ASC : r.DESC
  9. });
  10. return this._commandService.syncExecuteCommand(c.id, {
  11. orderRules: s,
  12. range: this._range,
  13. hasTitle: !1,
  14. unitId: this._workbook.getUnitId(),
  15. subUnitId: this._worksheet.getSheetId()
  16. }), this;
  17. }
  18. }
  19. S.extend(f);
  20. class v extends l {
  21. sort(o, e = !0) {
  22. const t = [{
  23. // real column index should be colIndex - 1.
  24. colIndex: o,
  25. type: e ? r.ASC : r.DESC
  26. }], s = {
  27. startRow: 0,
  28. startColumn: 0,
  29. endRow: this._worksheet.getRowCount() - 1,
  30. endColumn: this._worksheet.getColumnCount() - 1,
  31. rangeType: k.ALL
  32. };
  33. return this._commandService.syncExecuteCommand(c.id, {
  34. orderRules: t,
  35. range: s,
  36. hasTitle: !1,
  37. unitId: this._workbook.getUnitId(),
  38. subUnitId: this._worksheet.getSheetId()
  39. }), this;
  40. }
  41. }
  42. l.extend(v);
  43. w.extend(R);
  44. class E extends C {
  45. _initialize(o) {
  46. const e = o.get(p);
  47. this.disposeWithMe(e.beforeCommandExecuted((t) => {
  48. switch (t.id) {
  49. case c.id:
  50. this._beforeRangeSort(t);
  51. break;
  52. }
  53. })), this.disposeWithMe(e.onCommandExecuted((t) => {
  54. switch (t.id) {
  55. case c.id:
  56. this._onRangeSorted(t);
  57. break;
  58. }
  59. }));
  60. }
  61. _beforeRangeSort(o) {
  62. const e = o.params, t = this.getUniverSheet(e.unitId), s = t.getSheetBySheetId(e.subUnitId), { startColumn: n, endColumn: h, startRow: a, endRow: m } = e.range, g = s.getRange(a, n, m - a + 1, h - n + 1), i = {
  63. workbook: t,
  64. worksheet: s,
  65. range: g,
  66. sortColumn: e.orderRules.map((d) => ({
  67. column: d.colIndex - n,
  68. ascending: d.type === r.ASC
  69. }))
  70. };
  71. if (this.fireEvent(this.Event.SheetBeforeRangeSort, i), i.cancel)
  72. throw new Error("SortRangeCommand canceled.");
  73. }
  74. _onRangeSorted(o) {
  75. const e = o.params, t = this.getUniverSheet(e.unitId), s = t.getSheetBySheetId(e.subUnitId), { startColumn: n, endColumn: h, startRow: a, endRow: m } = e.range, g = s.getRange(a, n, m - a + 1, h - n + 1), i = {
  76. workbook: t,
  77. worksheet: s,
  78. range: g,
  79. sortColumn: e.orderRules.map((d) => ({
  80. column: d.colIndex - n,
  81. ascending: d.type === r.ASC
  82. }))
  83. };
  84. if (this.fireEvent(this.Event.SheetRangeSorted, i), i.cancel)
  85. throw new Error("SortRangeCommand canceled.");
  86. }
  87. }
  88. C.extend(E);