index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. var H = Object.defineProperty;
  2. var q = (e, t, r) => t in e ? H(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
  3. var h = (e, t, r) => q(e, typeof t != "symbol" ? t + "" : t, r);
  4. import { Inject as w, Disposable as y, Rectangle as R, IUniverInstanceService as U, ICommandService as I, CommandType as K, sequenceExecute as W, CellValueType as C, Plugin as Y, merge as z, UniverInstanceType as J, Injector as Q, IConfigService as X } from "@univerjs/core";
  5. import { getSheetCommandTarget as T, ReorderRangeCommand as Z } from "@univerjs/sheets";
  6. import { FormulaDataModel as k } from "@univerjs/engine-formula";
  7. const ee = "sheets-sort.config", N = {};
  8. var f = /* @__PURE__ */ ((e) => (e.DESC = "desc", e.ASC = "asc", e))(f || {});
  9. const D = (e) => e.replace(/-/gi, "").replace(/'/gi, ""), te = (e, t) => {
  10. const r = e === null || e === "", o = t === null || t === "";
  11. return r && o ? 0 : r ? 1 : o ? -1 : null;
  12. }, re = (e, t, r) => {
  13. const o = typeof e == "number", n = typeof t == "number";
  14. return o && n ? e < t ? r === f.ASC ? -1 : 1 : e > t ? r === f.ASC ? 1 : -1 : 0 : o ? r === f.ASC ? 1 : -1 : n ? r === f.ASC ? -1 : 1 : null;
  15. }, ne = (e, t, r) => {
  16. const o = typeof e == "string", n = typeof t == "string";
  17. if (o && (e = D(e.toLocaleLowerCase())), n && (t = D(t.toLocaleLowerCase())), !o && !n)
  18. return null;
  19. if (o && n) {
  20. const s = e, i = t;
  21. return s < i ? r === f.ASC ? -1 : 1 : s > i ? r === f.ASC ? 1 : -1 : 0;
  22. }
  23. return o ? r === f.ASC ? 1 : -1 : n ? r === f.ASC ? -1 : 1 : null;
  24. }, M = (e) => !e || Object.keys(e).length === 0 || (e == null ? void 0 : e.v) == null && (e == null ? void 0 : e.p) == null;
  25. var oe = Object.defineProperty, se = Object.getOwnPropertyDescriptor, ie = (e, t, r, o) => {
  26. for (var n = o > 1 ? void 0 : o ? se(t, r) : t, s = e.length - 1, i; s >= 0; s--)
  27. (i = e[s]) && (n = (o ? i(t, r, n) : i(n)) || n);
  28. return o && n && oe(t, r, n), n;
  29. }, p = (e, t) => (r, o) => t(r, o, e);
  30. let g = class extends y {
  31. constructor(t, r, o) {
  32. super();
  33. h(this, "_compareFns", []);
  34. this._univerInstanceService = t, this._commandService = r, this._formulaDataModel = o;
  35. }
  36. mergeCheck(t) {
  37. var c;
  38. const { unitId: r, subUnitId: o, range: n } = t, s = (c = this._univerInstanceService.getUnit(r)) == null ? void 0 : c.getSheetBySheetId(o);
  39. if (!s)
  40. return !1;
  41. const i = s.getMergeData().filter((u) => R.contains(n, u));
  42. return i.length === 0 ? !0 : ue(n, i);
  43. }
  44. emptyCheck(t) {
  45. var i;
  46. const { unitId: r, subUnitId: o, range: n } = t, s = (i = this._univerInstanceService.getUnit(r)) == null ? void 0 : i.getSheetBySheetId(o);
  47. if (!s)
  48. return !1;
  49. for (let c = n.startRow; c <= n.endRow; c++)
  50. for (let u = n.startColumn; u <= n.endColumn; u++)
  51. if (!M(s.getCellRaw(c, u)))
  52. return !0;
  53. return !1;
  54. }
  55. singleCheck(t) {
  56. return t.range.startRow !== t.range.endRow;
  57. }
  58. formulaCheck(t) {
  59. var i, c;
  60. const { unitId: r, subUnitId: o, range: n } = t, s = (c = (i = this._formulaDataModel.getArrayFormulaRange()) == null ? void 0 : i[r]) == null ? void 0 : c[o];
  61. for (const u in s) {
  62. const a = s[Number(u)];
  63. for (const m in a) {
  64. const d = a[Number(m)];
  65. if (d && R.intersects(n, d))
  66. return !1;
  67. }
  68. }
  69. return !0;
  70. }
  71. registerCompareFn(t) {
  72. this._compareFns.unshift(t);
  73. }
  74. getAllCompareFns() {
  75. return this._compareFns;
  76. }
  77. applySort(t, r, o) {
  78. var i;
  79. const { unitId: n, subUnitId: s } = T(this._univerInstanceService) || {};
  80. this._commandService.executeCommand($.id, {
  81. orderRules: t.orderRules,
  82. range: t.range,
  83. hasTitle: (i = t.hasTitle) != null ? i : !1,
  84. unitId: r || n,
  85. subUnitId: o || s
  86. });
  87. }
  88. };
  89. g = ie([
  90. p(0, U),
  91. p(1, I),
  92. p(2, w(k))
  93. ], g);
  94. function ue(e, t) {
  95. const r = e.endRow - e.startRow + 1, o = e.endColumn - e.startColumn + 1;
  96. let n = null, s = null;
  97. const i = r * o;
  98. let c = 0;
  99. for (const u of t)
  100. if (u.startRow >= e.startRow && u.endRow <= e.endRow && u.startColumn >= e.startColumn && u.endColumn <= e.endColumn) {
  101. const a = u.endRow - u.startRow + 1, m = u.endColumn - u.startColumn + 1;
  102. if (n === null && s === null)
  103. n = a, s = m;
  104. else if (a !== n || m !== s)
  105. return !1;
  106. c += a * m;
  107. }
  108. return c === i;
  109. }
  110. const $ = {
  111. id: "sheet.command.sort-range",
  112. type: K.COMMAND,
  113. handler: (e, t) => {
  114. const { range: r, orderRules: o, hasTitle: n, unitId: s, subUnitId: i } = t, c = e.get(g), u = e.get(U), { worksheet: a } = T(u, t) || {};
  115. if (!a)
  116. return !1;
  117. const m = a.getMergeData().filter((l) => R.contains(r, l)), d = m.map((l) => l.startRow), { startRow: b, endRow: j } = r, x = n ? b + 1 : b, S = [], A = [];
  118. for (let l = x; l <= j; l++)
  119. a.getRowFiltered(l) || a.getRowRawVisible(l) !== !1 && (m.length && !d.includes(l) || (S.push({
  120. index: l,
  121. value: ce(a, l, o)
  122. }), A.push(l)));
  123. const V = c.getAllCompareFns();
  124. S.sort(ae(o, le(V)));
  125. const F = {};
  126. S.forEach(({ index: l, value: Se }, B) => {
  127. F[A[B]] = l;
  128. });
  129. const L = {
  130. id: Z.id,
  131. params: {
  132. unitId: s,
  133. subUnitId: i,
  134. range: r,
  135. order: F
  136. }
  137. }, G = e.get(I);
  138. return W([L], G).result;
  139. }
  140. };
  141. function ce(e, t, r) {
  142. const o = [];
  143. return r.forEach(({ colIndex: n }) => {
  144. o.push(e.getCellRaw(t, n));
  145. }), o;
  146. }
  147. function le(e) {
  148. return (t, r, o) => {
  149. for (let n = 0; n < e.length; n++) {
  150. const s = e[n](t, r, o);
  151. if (s != null)
  152. return s;
  153. }
  154. return 0;
  155. };
  156. }
  157. function ae(e, t) {
  158. return function(r, o) {
  159. let n = null;
  160. for (let s = 0; s < e.length; s++) {
  161. const i = r.value[s], c = o.value[s];
  162. if (n = t(e[s].type, i, c), n !== 0 && n !== null && n !== void 0)
  163. return n;
  164. }
  165. return 0;
  166. };
  167. }
  168. var fe = Object.defineProperty, me = Object.getOwnPropertyDescriptor, ge = (e, t, r, o) => {
  169. for (var n = o > 1 ? void 0 : o ? me(t, r) : t, s = e.length - 1, i; s >= 0; s--)
  170. (i = e[s]) && (n = (o ? i(t, r, n) : i(n)) || n);
  171. return o && n && fe(t, r, n), n;
  172. }, O = (e, t) => (r, o) => t(r, o, e);
  173. let v = class extends y {
  174. constructor(e, t) {
  175. super(), this._commandService = e, this._sortService = t, this._initCommands(), this._registerCompareFns();
  176. }
  177. _initCommands() {
  178. [
  179. $
  180. ].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
  181. }
  182. _registerCompareFns() {
  183. const e = (t, r, o) => {
  184. const n = this._getCommonValue(r), s = this._getCommonValue(o), i = [
  185. te,
  186. ne,
  187. re
  188. ];
  189. for (let c = 0; c < i.length; c++) {
  190. const u = i[c](n, s, t);
  191. if (u !== null)
  192. return u;
  193. }
  194. return null;
  195. };
  196. this._sortService.registerCompareFn(e);
  197. }
  198. _getCommonValue(e) {
  199. var r, o;
  200. if (M(e))
  201. return null;
  202. const t = (o = (r = e == null ? void 0 : e.p) == null ? void 0 : r.body) == null ? void 0 : o.dataStream;
  203. return t || ((e == null ? void 0 : e.t) === C.NUMBER ? Number.parseFloat(`${e.v}`) : (e == null ? void 0 : e.t) === C.STRING ? typeof e.v == "number" ? e.v : `${e.v}` : (e == null ? void 0 : e.t) === C.BOOLEAN ? `${e.v}` : (e == null ? void 0 : e.t) === C.FORCE_STRING ? Number.parseFloat(`${e.v}`) : `${e == null ? void 0 : e.v}`);
  204. }
  205. };
  206. v = ge([
  207. O(0, I),
  208. O(1, w(g))
  209. ], v);
  210. var de = Object.defineProperty, he = Object.getOwnPropertyDescriptor, Ce = (e, t, r, o) => {
  211. for (var n = o > 1 ? void 0 : o ? he(t, r) : t, s = e.length - 1, i; s >= 0; s--)
  212. (i = e[s]) && (n = (o ? i(t, r, n) : i(n)) || n);
  213. return o && n && de(t, r, n), n;
  214. }, P = (e, t) => (r, o) => t(r, o, e);
  215. const _e = "SHEET_SORT_PLUGIN";
  216. var _;
  217. let E = (_ = class extends Y {
  218. constructor(e = N, t, r) {
  219. super(), this._config = e, this._injector = t, this._configService = r;
  220. const { ...o } = z(
  221. {},
  222. N,
  223. this._config
  224. );
  225. this._configService.setConfig(ee, o);
  226. }
  227. onStarting() {
  228. [
  229. [v],
  230. [g]
  231. ].forEach((e) => this._injector.add(e));
  232. }
  233. onReady() {
  234. this._injector.get(v);
  235. }
  236. }, h(_, "type", J.UNIVER_SHEET), h(_, "pluginName", _e), _);
  237. E = Ce([
  238. P(1, w(Q)),
  239. P(2, X)
  240. ], E);
  241. export {
  242. g as SheetsSortService,
  243. $ as SortRangeCommand,
  244. f as SortType,
  245. E as UniverSheetsSortPlugin
  246. };