index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. var x = Object.defineProperty;
  2. var T = (i, e, n) => e in i ? x(i, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[e] = n;
  3. var h = (i, e, n) => T(i, typeof e != "symbol" ? e + "" : e, n);
  4. import { Inject as l, Disposable as R, ObjectMatrix as $, UniverInstanceType as y, IUniverInstanceService as E, sequenceExecuteAsync as P, toDisposable as D, ICommandService as b, DependentOn as O, Injector as A, Plugin as j } from "@univerjs/core";
  5. import { singleReferenceToGrid as u, serializeRange as M } from "@univerjs/engine-formula";
  6. import { RefRangeService as W, SheetsSelectionsService as L, handleCommonRangeChangeWithEffectRefCommandsSkipNoInterests as B } from "@univerjs/sheets";
  7. import { ThreadCommentModel as S, DeleteCommentMutation as N, AddCommentMutation as V, UpdateCommentRefMutation as C, UniverThreadCommentPlugin as H } from "@univerjs/thread-comment";
  8. import { Subject as F } from "rxjs";
  9. var z = Object.defineProperty, G = Object.getOwnPropertyDescriptor, q = (i, e, n, t) => {
  10. for (var r = t > 1 ? void 0 : t ? G(e, n) : e, s = i.length - 1, o; s >= 0; s--)
  11. (o = i[s]) && (r = (t ? o(e, n, r) : o(r)) || r);
  12. return t && r && z(e, n, r), r;
  13. }, w = (i, e) => (n, t) => e(n, t, i);
  14. let p = class extends R {
  15. constructor(e, n) {
  16. super();
  17. h(this, "_matrixMap", /* @__PURE__ */ new Map());
  18. h(this, "_locationMap", /* @__PURE__ */ new Map());
  19. h(this, "_commentUpdate$", new F());
  20. h(this, "commentUpdate$", this._commentUpdate$.asObservable());
  21. this._threadCommentModel = e, this._univerInstanceService = n, this._init(), this.disposeWithMe(() => {
  22. this._commentUpdate$.complete();
  23. });
  24. }
  25. _init() {
  26. this._initData(), this._initUpdateTransform();
  27. }
  28. _ensureCommentMatrix(e, n) {
  29. let t = this._matrixMap.get(e);
  30. t || (t = /* @__PURE__ */ new Map(), this._matrixMap.set(e, t));
  31. let r = t.get(n);
  32. return r || (r = new $(), t.set(n, r)), r;
  33. }
  34. _ensureCommentLocationMap(e, n) {
  35. let t = this._locationMap.get(e);
  36. t || (t = /* @__PURE__ */ new Map(), this._locationMap.set(e, t));
  37. let r = t.get(n);
  38. return r || (r = /* @__PURE__ */ new Map(), t.set(n, r)), r;
  39. }
  40. _addCommentToMatrix(e, n, t, r) {
  41. var o;
  42. const s = (o = e.getValue(n, t)) != null ? o : /* @__PURE__ */ new Set();
  43. s.add(r), e.setValue(n, t, s);
  44. }
  45. _deleteCommentFromMatrix(e, n, t, r) {
  46. if (n >= 0 && t >= 0) {
  47. const s = e.getValue(n, t);
  48. s && s.has(r) && (s.delete(r), s.size === 0 && e.realDeleteValue(n, t));
  49. }
  50. }
  51. _ensure(e, n) {
  52. const t = this._ensureCommentMatrix(e, n), r = this._ensureCommentLocationMap(e, n);
  53. return { matrix: t, locationMap: r };
  54. }
  55. _initData() {
  56. const e = this._threadCommentModel.getAll();
  57. for (const n of e)
  58. for (const t of n.threads) {
  59. const { unitId: r, subUnitId: s, root: o } = t;
  60. this._addComment(r, s, o);
  61. }
  62. }
  63. _addComment(e, n, t) {
  64. const r = u(t.ref), s = t.parentId, { row: o, column: a } = r, c = t.id, { matrix: m, locationMap: d } = this._ensure(e, n);
  65. !s && o >= 0 && a >= 0 && (this._addCommentToMatrix(m, o, a, c), d.set(c, { row: o, column: a })), s || this._commentUpdate$.next({
  66. unitId: e,
  67. subUnitId: n,
  68. payload: t,
  69. type: "add",
  70. isRoot: !s,
  71. ...r
  72. });
  73. }
  74. // eslint-disable-next-line max-lines-per-function
  75. _initUpdateTransform() {
  76. this.disposeWithMe(this._threadCommentModel.commentUpdate$.subscribe((e) => {
  77. const { unitId: n, subUnitId: t } = e;
  78. try {
  79. if (this._univerInstanceService.getUnitType(n) !== y.UNIVER_SHEET)
  80. return;
  81. } catch {
  82. }
  83. const { matrix: r, locationMap: s } = this._ensure(n, t);
  84. switch (e.type) {
  85. case "add": {
  86. this._addComment(e.unitId, e.subUnitId, e.payload);
  87. break;
  88. }
  89. case "delete": {
  90. const { isRoot: o, comment: a } = e.payload;
  91. if (o) {
  92. const c = u(a.ref), { row: m, column: d } = c;
  93. this._deleteCommentFromMatrix(r, m, d, a.id), this._commentUpdate$.next({
  94. ...e,
  95. ...c
  96. });
  97. }
  98. break;
  99. }
  100. case "update": {
  101. const { commentId: o } = e.payload, a = this._threadCommentModel.getComment(n, t, o);
  102. if (!a)
  103. return;
  104. const c = u(a.ref);
  105. this._commentUpdate$.next({
  106. ...e,
  107. ...c
  108. });
  109. break;
  110. }
  111. case "updateRef": {
  112. const o = u(e.payload.ref), { commentId: a } = e.payload, c = s.get(a);
  113. if (!c)
  114. return;
  115. const { row: m, column: d } = c;
  116. this._deleteCommentFromMatrix(r, m, d, a), s.delete(a), o.row >= 0 && o.column >= 0 && (this._addCommentToMatrix(r, o.row, o.column, a), s.set(a, { row: o.row, column: o.column })), this._commentUpdate$.next({
  117. ...e,
  118. ...o
  119. });
  120. break;
  121. }
  122. case "resolve": {
  123. const { unitId: o, subUnitId: a, payload: c } = e, { locationMap: m } = this._ensure(o, a), d = m.get(c.commentId);
  124. d && this._commentUpdate$.next({
  125. ...e,
  126. ...d
  127. });
  128. break;
  129. }
  130. }
  131. }));
  132. }
  133. getByLocation(e, n, t, r) {
  134. var a;
  135. return (a = this.getAllByLocation(e, n, t, r).filter((c) => !c.resolved)[0]) == null ? void 0 : a.id;
  136. }
  137. getAllByLocation(e, n, t, r) {
  138. const o = this._ensureCommentMatrix(e, n).getValue(t, r);
  139. return o ? Array.from(o).map((a) => this.getComment(e, n, a)).filter(Boolean) : [];
  140. }
  141. getComment(e, n, t) {
  142. return this._threadCommentModel.getComment(e, n, t);
  143. }
  144. getCommentWithChildren(e, n, t, r) {
  145. const s = this.getByLocation(e, n, t, r);
  146. if (!s)
  147. return;
  148. const o = this.getComment(e, n, s);
  149. if (o)
  150. return this._threadCommentModel.getThread(e, n, o.threadId);
  151. }
  152. showCommentMarker(e, n, t, r) {
  153. const s = this.getByLocation(e, n, t, r);
  154. if (!s)
  155. return !1;
  156. const o = this.getComment(e, n, s);
  157. return !!(o && !o.resolved);
  158. }
  159. getSubUnitAll(e, n) {
  160. return this._threadCommentModel.getUnit(e).filter((t) => t.subUnitId === n).map((t) => t.root);
  161. }
  162. };
  163. p = q([
  164. w(0, l(S)),
  165. w(1, E)
  166. ], p);
  167. var J = Object.defineProperty, K = Object.getOwnPropertyDescriptor, Q = (i, e, n, t) => {
  168. for (var r = t > 1 ? void 0 : t ? K(e, n) : e, s = i.length - 1, o; s >= 0; s--)
  169. (o = i[s]) && (r = (t ? o(e, n, r) : o(r)) || r);
  170. return t && r && J(e, n, r), r;
  171. }, _ = (i, e) => (n, t) => e(n, t, i);
  172. let f = class extends R {
  173. constructor(e, n, t, r, s) {
  174. super();
  175. h(this, "_disposableMap", /* @__PURE__ */ new Map());
  176. h(this, "_watcherMap", /* @__PURE__ */ new Map());
  177. h(this, "_handleRangeChange", (e, n, t, r, s) => {
  178. const o = t.id, a = {
  179. startColumn: t.column,
  180. endColumn: t.column,
  181. startRow: t.row,
  182. endRow: t.row
  183. };
  184. return r ? {
  185. redos: [{
  186. id: C.id,
  187. params: {
  188. unitId: e,
  189. subUnitId: n,
  190. payload: {
  191. ref: M(r),
  192. commentId: o
  193. },
  194. silent: s
  195. }
  196. }],
  197. undos: [{
  198. id: C.id,
  199. params: {
  200. unitId: e,
  201. subUnitId: n,
  202. payload: {
  203. ref: M(a),
  204. commentId: o
  205. },
  206. silent: s
  207. }
  208. }]
  209. } : {
  210. redos: [{
  211. id: N.id,
  212. params: {
  213. unitId: e,
  214. subUnitId: n,
  215. commentId: o
  216. }
  217. }],
  218. undos: [{
  219. id: V.id,
  220. params: {
  221. unitId: e,
  222. subUnitId: n,
  223. comment: t,
  224. sync: !0
  225. }
  226. }]
  227. };
  228. });
  229. this._refRangeService = e, this._sheetsThreadCommentModel = n, this._threadCommentModel = t, this._selectionManagerService = r, this._commandService = s, this._initData(), this._initRefRange();
  230. }
  231. _getIdWithUnitId(e, n, t) {
  232. return `${e}-${n}-${t}`;
  233. }
  234. _register(e, n, t) {
  235. const r = t.id, s = {
  236. startColumn: t.column,
  237. endColumn: t.column,
  238. startRow: t.row,
  239. endRow: t.row
  240. };
  241. this._disposableMap.set(
  242. this._getIdWithUnitId(e, n, r),
  243. this._refRangeService.registerRefRange(s, (o) => {
  244. const a = B(s, o, { selectionManagerService: this._selectionManagerService }), c = Array.isArray(a) ? a[0] : a;
  245. return c && c.startColumn === s.startColumn && c.startRow === s.startRow ? {
  246. undos: [],
  247. redos: []
  248. } : this._handleRangeChange(e, n, t, c, !1);
  249. }, e, n)
  250. );
  251. }
  252. _watch(e, n, t) {
  253. const r = t.id, s = {
  254. startColumn: t.column,
  255. endColumn: t.column,
  256. startRow: t.row,
  257. endRow: t.row
  258. };
  259. this._watcherMap.set(
  260. this._getIdWithUnitId(e, n, r),
  261. this._refRangeService.watchRange(e, n, s, (o, a) => {
  262. const { redos: c } = this._handleRangeChange(e, n, t, a, !0);
  263. P(c, this._commandService, { onlyLocal: !0 });
  264. }, !0)
  265. );
  266. }
  267. _unwatch(e, n, t) {
  268. var s;
  269. const r = this._getIdWithUnitId(e, n, t);
  270. (s = this._watcherMap.get(r)) == null || s.dispose(), this._watcherMap.delete(r);
  271. }
  272. _unregister(e, n, t) {
  273. var s;
  274. const r = this._getIdWithUnitId(e, n, t);
  275. (s = this._disposableMap.get(r)) == null || s.dispose(), this._disposableMap.delete(r);
  276. }
  277. _initData() {
  278. const e = this._threadCommentModel.getAll();
  279. for (const n of e)
  280. for (const t of n.threads) {
  281. const { unitId: r, subUnitId: s, root: o } = t, a = u(o.ref), c = {
  282. ...o,
  283. ...a
  284. };
  285. this._register(r, s, c), this._watch(r, s, c);
  286. }
  287. }
  288. _initRefRange() {
  289. this.disposeWithMe(
  290. this._sheetsThreadCommentModel.commentUpdate$.subscribe((e) => {
  291. const { unitId: n, subUnitId: t } = e;
  292. switch (e.type) {
  293. case "add": {
  294. if (e.payload.parentId)
  295. return;
  296. const r = {
  297. ...e.payload,
  298. row: e.row,
  299. column: e.column
  300. };
  301. this._register(e.unitId, e.subUnitId, r), this._watch(e.unitId, e.subUnitId, r);
  302. break;
  303. }
  304. case "delete": {
  305. this._unregister(n, t, e.payload.commentId), this._unwatch(n, t, e.payload.commentId);
  306. break;
  307. }
  308. case "updateRef": {
  309. const r = this._sheetsThreadCommentModel.getComment(n, t, e.payload.commentId);
  310. if (!r)
  311. return;
  312. this._unregister(n, t, e.payload.commentId);
  313. const s = {
  314. ...r,
  315. row: e.row,
  316. column: e.column
  317. };
  318. e.silent || (this._unwatch(n, t, e.payload.commentId), this._watch(n, t, s)), this._register(e.unitId, e.subUnitId, s);
  319. break;
  320. }
  321. }
  322. })
  323. ), this.disposeWithMe(D(() => {
  324. this._disposableMap.forEach((e) => {
  325. e.dispose();
  326. }), this._disposableMap.clear();
  327. }));
  328. }
  329. };
  330. f = Q([
  331. _(0, l(W)),
  332. _(1, l(p)),
  333. _(2, l(S)),
  334. _(3, l(L)),
  335. _(4, b)
  336. ], f);
  337. const X = "SHEET_THREAD_COMMENT_BASE_PLUGIN";
  338. var I = Object.defineProperty, Y = Object.getOwnPropertyDescriptor, Z = (i, e, n) => e in i ? I(i, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[e] = n, k = (i, e, n, t) => {
  339. for (var r = t > 1 ? void 0 : t ? Y(e, n) : e, s = i.length - 1, o; s >= 0; s--)
  340. (o = i[s]) && (r = (t ? o(e, n, r) : o(r)) || r);
  341. return t && r && I(e, n, r), r;
  342. }, v = (i, e) => (n, t) => e(n, t, i), U = (i, e, n) => Z(i, typeof e != "symbol" ? e + "" : e, n);
  343. let g = class extends j {
  344. constructor(i, e, n) {
  345. super(), this._injector = e, this._commandService = n;
  346. }
  347. onStarting() {
  348. [
  349. [p],
  350. [f]
  351. ].forEach((i) => {
  352. this._injector.add(i);
  353. }), this._injector.get(f);
  354. }
  355. };
  356. U(g, "pluginName", X);
  357. U(g, "type", y.UNIVER_SHEET);
  358. g = k([
  359. O(H),
  360. v(1, l(A)),
  361. v(2, l(b))
  362. ], g);
  363. export {
  364. p as SheetsThreadCommentModel,
  365. f as SheetsThreadCommentRefRangeController,
  366. g as UniverSheetsThreadCommentPlugin
  367. };