index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. var A = Object.defineProperty;
  2. var j = (s, t, e) => t in s ? A(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
  3. var u = (s, t, e) => j(s, typeof t != "symbol" ? t + "" : t, e);
  4. import { createIdentifier as L, Disposable as U, Inject as g, LifecycleService as V, LifecycleStages as O, IResourceManagerService as W, CommandType as l, ICommandService as C, Plugin as H, merge as J, UniverInstanceType as G, mergeOverrideWithDependencies as b, Injector as Y, IConfigService as K, dayjs as B } from "@univerjs/core";
  5. import { Subject as Z } from "rxjs";
  6. class F extends U {
  7. constructor() {
  8. super();
  9. u(this, "_dataSource", null);
  10. u(this, "syncUpdateMutationToColla", !0);
  11. }
  12. set dataSource(e) {
  13. this._dataSource = e;
  14. }
  15. get dataSource() {
  16. return this._dataSource;
  17. }
  18. async getThreadComment(e, n, o) {
  19. return this._dataSource ? (await this._dataSource.listComments(e, n, [o]))[0] : null;
  20. }
  21. async addComment(e) {
  22. var n;
  23. return this._dataSource ? this._dataSource.addComment(e) : { ...e, threadId: (n = e.threadId) != null ? n : e.id };
  24. }
  25. async updateComment(e) {
  26. return this._dataSource ? this._dataSource.updateComment(e) : !0;
  27. }
  28. async resolveComment(e) {
  29. return this._dataSource ? this._dataSource.resolveComment(e) : !0;
  30. }
  31. async deleteComment(e, n, o, r) {
  32. return this._dataSource ? this._dataSource.deleteComment(e, n, o, r) : !0;
  33. }
  34. async listThreadComments(e, n, o) {
  35. return this.dataSource ? this.dataSource.listComments(e, n, o) : !1;
  36. }
  37. saveToSnapshot(e, n) {
  38. if (this._dataSource) {
  39. const o = {};
  40. return Object.keys(e).forEach((r) => {
  41. const a = e[r];
  42. o[r] = a.map(this.dataSource.saveCommentToSnapshot);
  43. }), o;
  44. }
  45. return e;
  46. }
  47. }
  48. const p = L("univer.thread-comment.data-source-service");
  49. var q = Object.defineProperty, z = Object.getOwnPropertyDescriptor, Q = (s, t, e, n) => {
  50. for (var o = n > 1 ? void 0 : n ? z(t, e) : t, r = s.length - 1, a; r >= 0; r--)
  51. (a = s[r]) && (o = (n ? a(t, e, o) : a(o)) || o);
  52. return n && o && q(t, e, o), o;
  53. }, I = (s, t) => (e, n) => t(e, n, s);
  54. let i = class extends U {
  55. constructor(t, e) {
  56. super();
  57. u(this, "_commentsMap", /* @__PURE__ */ new Map());
  58. u(this, "_threadMap", /* @__PURE__ */ new Map());
  59. u(this, "_commentUpdate$", new Z());
  60. u(this, "commentUpdate$", this._commentUpdate$.asObservable());
  61. u(this, "_tasks", []);
  62. this._dataSourceService = t, this._lifecycleService = e, this.disposeWithMe(() => {
  63. this._commentUpdate$.complete();
  64. }), this.disposeWithMe(this._lifecycleService.lifecycle$.subscribe((n) => {
  65. const o = /* @__PURE__ */ new Map();
  66. n === O.Rendered && (this._tasks.forEach(({ unitId: r, subUnitId: a, threadIds: d }) => {
  67. let m = o.get(r);
  68. m || (m = /* @__PURE__ */ new Map(), o.set(r, m));
  69. let c = m.get(a);
  70. c || (c = /* @__PURE__ */ new Set(), m.set(a, c));
  71. for (const h of d)
  72. c.add(h);
  73. }), this._tasks = [], o.forEach((r, a) => {
  74. r.forEach((d, m) => {
  75. this.syncThreadComments(a, m, Array.from(d));
  76. });
  77. }));
  78. }));
  79. }
  80. _ensureCommentMap(t, e) {
  81. let n = this._commentsMap.get(t);
  82. n || (n = /* @__PURE__ */ new Map(), this._commentsMap.set(t, n));
  83. let o = n.get(e);
  84. return o || (o = /* @__PURE__ */ new Map(), n.set(e, o)), o;
  85. }
  86. ensureMap(t, e) {
  87. return this._ensureCommentMap(t, e);
  88. }
  89. _ensureThreadMap(t, e) {
  90. let n = this._threadMap.get(t);
  91. n || (n = /* @__PURE__ */ new Map(), this._threadMap.set(t, n));
  92. let o = n.get(e);
  93. return o || (o = /* @__PURE__ */ new Map(), n.set(e, o)), o;
  94. }
  95. _replaceComment(t, e, n) {
  96. const o = this._ensureCommentMap(t, e), r = o.get(n.id);
  97. if (r) {
  98. const { children: a, ...d } = n, m = {
  99. ...d,
  100. ref: r.ref
  101. };
  102. o.set(n.id, m), a == null || a.forEach((c) => {
  103. o.set(c.id, {
  104. ...c,
  105. ref: ""
  106. });
  107. }), this._commentUpdate$.next({
  108. unitId: t,
  109. subUnitId: e,
  110. type: "syncUpdate",
  111. payload: m
  112. }), !!n.resolved != !!r.resolved && this._commentUpdate$.next({
  113. unitId: t,
  114. subUnitId: e,
  115. type: "resolve",
  116. payload: {
  117. commentId: n.id,
  118. resolved: !!n.resolved
  119. }
  120. });
  121. }
  122. }
  123. async syncThreadComments(t, e, n) {
  124. if (this._lifecycleService.stage < O.Rendered) {
  125. this._tasks.push({ unitId: t, subUnitId: e, threadIds: n });
  126. return;
  127. }
  128. const o = this._ensureThreadMap(t, e), r = this._ensureCommentMap(t, e), a = await this._dataSourceService.listThreadComments(t, e, n);
  129. if (!a)
  130. return;
  131. const d = new Set(n);
  132. a.forEach((m) => {
  133. this._replaceComment(t, e, m), d.delete(m.threadId);
  134. }), d.forEach((m) => {
  135. o.delete(m), r.forEach((c, h) => {
  136. c.threadId === m && r.delete(h);
  137. });
  138. });
  139. }
  140. addComment(t, e, n, o) {
  141. const r = this._ensureCommentMap(t, e), { parentId: a, children: d = [], ...m } = n, c = {
  142. ...m,
  143. parentId: a === n.id ? void 0 : a
  144. };
  145. c.threadId || (c.threadId = c.parentId || c.id);
  146. const h = (f) => {
  147. r.set(f.id, f), this._commentUpdate$.next({
  148. unitId: t,
  149. subUnitId: e,
  150. type: "add",
  151. payload: f,
  152. isRoot: !f.parentId
  153. });
  154. };
  155. h(c);
  156. const _ = this._ensureThreadMap(t, e);
  157. if (!c.parentId) {
  158. _.set(c.threadId, c);
  159. for (const f of d)
  160. h(f);
  161. }
  162. return o && this.syncThreadComments(t, e, [c.threadId]), !0;
  163. }
  164. updateComment(t, e, n, o) {
  165. const a = this._ensureCommentMap(t, e).get(n.commentId);
  166. return a && (a.updated = !0, a.text = n.text, a.attachments = n.attachments, a.updateT = n.updateT, this._commentUpdate$.next({
  167. unitId: t,
  168. subUnitId: e,
  169. type: "update",
  170. payload: n,
  171. silent: o
  172. })), !0;
  173. }
  174. updateCommentRef(t, e, n, o) {
  175. const a = this._ensureCommentMap(t, e).get(n.commentId);
  176. return a ? (a.ref = n.ref, this._commentUpdate$.next({
  177. unitId: t,
  178. subUnitId: e,
  179. type: "updateRef",
  180. payload: n,
  181. silent: o,
  182. threadId: a.threadId
  183. }), !0) : !1;
  184. }
  185. resolveComment(t, e, n, o) {
  186. const a = this._ensureCommentMap(t, e).get(n);
  187. return a ? (a.resolved = o, this._commentUpdate$.next({
  188. unitId: t,
  189. subUnitId: e,
  190. type: "resolve",
  191. payload: {
  192. commentId: n,
  193. resolved: o
  194. }
  195. }), !0) : !1;
  196. }
  197. getComment(t, e, n) {
  198. return this._ensureCommentMap(t, e).get(n);
  199. }
  200. getRootComment(t, e, n) {
  201. return this._ensureThreadMap(t, e).get(n);
  202. }
  203. getThread(t, e, n) {
  204. const o = this._ensureCommentMap(t, e), r = Array.from(o.values()).filter((c) => c.threadId === n);
  205. let a;
  206. const d = [], m = /* @__PURE__ */ new Set();
  207. for (const c of r)
  208. c.parentId ? d.push(c) : a = c, m.add(c.personId);
  209. if (a)
  210. return {
  211. root: a,
  212. children: d,
  213. relativeUsers: m,
  214. unitId: t,
  215. subUnitId: e,
  216. threadId: n
  217. };
  218. }
  219. getCommentWithChildren(t, e, n) {
  220. const o = this.getComment(t, e, n);
  221. if (o)
  222. return this.getThread(t, e, o.threadId);
  223. }
  224. _deleteComment(t, e, n) {
  225. const o = this._ensureCommentMap(t, e), r = o.get(n);
  226. r && (o.delete(n), this._commentUpdate$.next({
  227. unitId: t,
  228. subUnitId: e,
  229. type: "delete",
  230. payload: {
  231. commentId: n,
  232. isRoot: !r.parentId,
  233. comment: r
  234. }
  235. }));
  236. }
  237. deleteThread(t, e, n) {
  238. this._ensureThreadMap(t, e).delete(n), this._ensureCommentMap(t, e).forEach((a) => {
  239. a.threadId === n && this._deleteComment(t, e, a.id);
  240. });
  241. }
  242. deleteComment(t, e, n) {
  243. const r = this._ensureCommentMap(t, e).get(n);
  244. return r && (r.parentId ? this._deleteComment(t, e, n) : this.deleteThread(t, e, r.threadId)), !0;
  245. }
  246. deleteUnit(t) {
  247. const e = this._commentsMap.get(t);
  248. e && e.forEach((n, o) => {
  249. n.forEach((r) => {
  250. this.deleteComment(t, o, r.id);
  251. });
  252. });
  253. }
  254. getUnit(t) {
  255. const e = this._threadMap.get(t);
  256. if (!e)
  257. return [];
  258. const n = [];
  259. return e.forEach((o, r) => {
  260. o.forEach((a, d) => {
  261. const m = this.getThread(t, r, d);
  262. m && n.push(m);
  263. });
  264. }), n;
  265. }
  266. getAll() {
  267. const t = [];
  268. return this._commentsMap.forEach((e, n) => {
  269. t.push({
  270. unitId: n,
  271. threads: this.getUnit(n)
  272. });
  273. }), t;
  274. }
  275. };
  276. i = Q([
  277. I(0, g(p)),
  278. I(1, g(V))
  279. ], i);
  280. var T = /* @__PURE__ */ ((s) => (s[s.UNIVER_UNKNOWN = 0] = "UNIVER_UNKNOWN", s[s.UNIVER_DOC = 1] = "UNIVER_DOC", s[s.UNIVER_SHEET = 2] = "UNIVER_SHEET", s[s.UNIVER_SLIDE = 3] = "UNIVER_SLIDE", s[s.UNIVER_PROJECT = 4] = "UNIVER_PROJECT", s[s.UNRECOGNIZED = -1] = "UNRECOGNIZED", s))(T || {});
  281. const w = "UNIVER_THREAD_COMMENT_PLUGIN";
  282. var X = Object.defineProperty, k = Object.getOwnPropertyDescriptor, ee = (s, t, e, n) => {
  283. for (var o = n > 1 ? void 0 : n ? k(t, e) : t, r = s.length - 1, a; r >= 0; r--)
  284. (a = s[r]) && (o = (n ? a(t, e, o) : a(o)) || o);
  285. return n && o && X(t, e, o), o;
  286. }, v = (s, t) => (e, n) => t(e, n, s);
  287. let S = class extends U {
  288. constructor(s, t, e) {
  289. super(), this._resourceManagerService = s, this._threadCommentModel = t, this._threadCommentDataSourceService = e, this._initSnapshot();
  290. }
  291. _initSnapshot() {
  292. const s = (e) => {
  293. const n = this._threadCommentModel.getUnit(e), o = {};
  294. return n ? (n.forEach((r) => {
  295. var d;
  296. const a = (d = o[r.subUnitId]) != null ? d : [];
  297. a.push({
  298. ...r.root,
  299. children: r.children
  300. }), o[r.subUnitId] = a;
  301. }), JSON.stringify(this._threadCommentDataSourceService.saveToSnapshot(o, e))) : "";
  302. }, t = (e) => {
  303. if (!e)
  304. return {};
  305. try {
  306. return JSON.parse(e);
  307. } catch {
  308. return {};
  309. }
  310. };
  311. this.disposeWithMe(
  312. this._resourceManagerService.registerPluginResource({
  313. pluginName: `SHEET_${w}`,
  314. businesses: [T.UNIVER_SHEET, T.UNIVER_DOC],
  315. toJson: (e) => s(e),
  316. parseJson: (e) => t(e),
  317. onUnLoad: (e) => {
  318. this._threadCommentModel.deleteUnit(e);
  319. },
  320. onLoad: async (e, n) => {
  321. Object.keys(n).forEach((o) => {
  322. const r = n[o];
  323. r.forEach((a) => {
  324. this._threadCommentModel.addComment(e, o, a);
  325. }), this._threadCommentModel.syncThreadComments(e, o, r.map((a) => a.threadId));
  326. });
  327. }
  328. })
  329. );
  330. }
  331. };
  332. S = ee([
  333. v(0, W),
  334. v(1, g(i)),
  335. v(2, p)
  336. ], S);
  337. const P = {
  338. id: "thread-comment.mutation.add-comment",
  339. type: l.MUTATION,
  340. handler(s, t, e) {
  341. if (!t)
  342. return !1;
  343. const n = s.get(i), { unitId: o, subUnitId: r, comment: a, sync: d } = t, m = d || (e == null ? void 0 : e.fromChangeset) && !a.parentId;
  344. return n.addComment(o, r, a, m);
  345. }
  346. }, $ = {
  347. id: "thread-comment.mutation.update-comment",
  348. type: l.MUTATION,
  349. handler(s, t) {
  350. if (!t)
  351. return !1;
  352. const e = s.get(i), { unitId: n, subUnitId: o, payload: r, silent: a } = t;
  353. return e.updateComment(n, o, r, a);
  354. }
  355. }, te = {
  356. id: "thread-comment.mutation.update-comment-ref",
  357. type: l.MUTATION,
  358. handler(s, t) {
  359. if (!t)
  360. return !1;
  361. const e = s.get(i), { unitId: n, subUnitId: o, payload: r, silent: a } = t;
  362. return e.updateCommentRef(n, o, r, a);
  363. }
  364. }, x = {
  365. id: "thread-comment.mutation.resolve-comment",
  366. type: l.MUTATION,
  367. handler(s, t) {
  368. if (!t)
  369. return !1;
  370. const e = s.get(i), { unitId: n, subUnitId: o, resolved: r, commentId: a } = t;
  371. return e.resolveComment(n, o, a, r);
  372. }
  373. }, N = {
  374. id: "thread-comment.mutation.delete-comment",
  375. type: l.MUTATION,
  376. handler(s, t) {
  377. if (!t)
  378. return !1;
  379. const e = s.get(i), { unitId: n, subUnitId: o, commentId: r } = t;
  380. return e.deleteComment(n, o, r);
  381. }
  382. }, ne = {
  383. id: "thread-comment.command.add-comment",
  384. type: l.COMMAND,
  385. async handler(s, t) {
  386. if (!t)
  387. return !1;
  388. const e = s.get(C), n = s.get(p), { comment: o } = t, r = await n.addComment(o), a = n.syncUpdateMutationToColla, d = !o.parentId, m = {
  389. id: P.id,
  390. params: {
  391. ...t,
  392. comment: r
  393. }
  394. };
  395. return d ? await e.executeCommand(m.id, m.params) : e.executeCommand(m.id, m.params, {
  396. onlyLocal: !a
  397. });
  398. }
  399. }, oe = {
  400. id: "thread-comment.command.update-comment",
  401. type: l.COMMAND,
  402. async handler(s, t) {
  403. if (!t)
  404. return !1;
  405. const { unitId: e, subUnitId: n, payload: o } = t, r = s.get(C), a = s.get(i), d = s.get(p), m = d.syncUpdateMutationToColla, c = a.getComment(
  406. e,
  407. n,
  408. o.commentId
  409. );
  410. if (!c)
  411. return !1;
  412. const { children: h, ..._ } = c;
  413. if (!await d.updateComment({
  414. ..._,
  415. ...o
  416. }))
  417. return !1;
  418. const E = {
  419. id: $.id,
  420. params: t
  421. };
  422. return r.executeCommand(E.id, E.params, { onlyLocal: !m }), !0;
  423. }
  424. }, re = {
  425. id: "thread-comment.command.resolve-comment",
  426. type: l.COMMAND,
  427. async handler(s, t) {
  428. if (!t)
  429. return !1;
  430. const { unitId: e, subUnitId: n, resolved: o, commentId: r } = t, a = s.get(p), m = s.get(i).getComment(e, n, r), c = a.syncUpdateMutationToColla;
  431. return !m || !await a.resolveComment({
  432. ...m,
  433. resolved: o
  434. }) ? !1 : s.get(C).executeCommand(
  435. x.id,
  436. t,
  437. { onlyLocal: !c }
  438. );
  439. }
  440. }, ae = {
  441. id: "thread-comment.command.delete-comment",
  442. type: l.COMMAND,
  443. async handler(s, t) {
  444. if (!t)
  445. return !1;
  446. const e = s.get(i), n = s.get(p), o = s.get(C), { unitId: r, subUnitId: a, commentId: d } = t, m = n.syncUpdateMutationToColla, c = e.getComment(r, a, d);
  447. if (!c || !await n.deleteComment(r, a, c.threadId, d))
  448. return !1;
  449. const h = {
  450. id: N.id,
  451. params: t
  452. };
  453. return o.executeCommand(h.id, h.params, { onlyLocal: !m });
  454. }
  455. }, se = {
  456. id: "thread-comment.command.delete-comment-tree",
  457. type: l.COMMAND,
  458. async handler(s, t) {
  459. if (!t)
  460. return !1;
  461. const e = s.get(i), n = s.get(C), o = s.get(p), { unitId: r, subUnitId: a, commentId: d } = t, m = e.getCommentWithChildren(r, a, d);
  462. return !m || !await o.deleteComment(r, a, m.root.threadId, d) ? !1 : await n.executeCommand(N.id, {
  463. unitId: r,
  464. subUnitId: a,
  465. commentId: m.root.id
  466. });
  467. }
  468. }, me = "thread-comment.config", R = {};
  469. var ce = Object.defineProperty, de = Object.getOwnPropertyDescriptor, ie = (s, t, e, n) => {
  470. for (var o = n > 1 ? void 0 : n ? de(t, e) : t, r = s.length - 1, a; r >= 0; r--)
  471. (a = s[r]) && (o = (n ? a(t, e, o) : a(o)) || o);
  472. return n && o && ce(t, e, o), o;
  473. }, y = (s, t) => (e, n) => t(e, n, s), M;
  474. let D = (M = class extends H {
  475. constructor(s = R, t, e, n) {
  476. super(), this._config = s, this._injector = t, this._commandService = e, this._configService = n;
  477. const { ...o } = J(
  478. {},
  479. R,
  480. this._config
  481. );
  482. this._configService.setConfig(me, o);
  483. }
  484. onStarting() {
  485. var s;
  486. b([
  487. [p, { useClass: F }],
  488. [i],
  489. [S]
  490. ], (s = this._config) == null ? void 0 : s.overrides).forEach(
  491. (t) => {
  492. this._injector.add(t);
  493. }
  494. ), [
  495. ne,
  496. oe,
  497. ae,
  498. re,
  499. se,
  500. P,
  501. $,
  502. te,
  503. N,
  504. x
  505. ].forEach((t) => {
  506. this._commandService.registerCommand(t);
  507. }), this._injector.get(S);
  508. }
  509. }, u(M, "pluginName", w), u(M, "type", G.UNIVER_UNKNOWN), M);
  510. D = ie([
  511. y(1, g(Y)),
  512. y(2, C),
  513. y(3, K)
  514. ], D);
  515. function pe(s) {
  516. return B(s).format("YYYY/MM/DD HH:mm");
  517. }
  518. export {
  519. ne as AddCommentCommand,
  520. P as AddCommentMutation,
  521. ae as DeleteCommentCommand,
  522. N as DeleteCommentMutation,
  523. se as DeleteCommentTreeCommand,
  524. p as IThreadCommentDataSourceService,
  525. re as ResolveCommentCommand,
  526. x as ResolveCommentMutation,
  527. w as TC_PLUGIN_NAME,
  528. F as ThreadCommentDataSourceService,
  529. i as ThreadCommentModel,
  530. S as ThreadCommentResourceController,
  531. D as UniverThreadCommentPlugin,
  532. oe as UpdateCommentCommand,
  533. $ as UpdateCommentMutation,
  534. te as UpdateCommentRefMutation,
  535. pe as getDT
  536. };