facade.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. var E = Object.defineProperty;
  2. var P = (r, e, t) => e in r ? E(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
  3. var w = (r, e, t) => P(r, typeof e != "symbol" ? e + "" : e, t);
  4. import { Inject as v, Injector as F, UserManagerService as S, UniverInstanceType as W, RichTextValue as l, generateRandomId as g, ICommandService as p, IUniverInstanceService as O, RichTextBuilder as $, Tools as B, Range as z, toDisposable as I, FEventName as R } from "@univerjs/core";
  5. import { SheetsThreadCommentModel as h } from "@univerjs/sheets-thread-comment";
  6. import { FRange as x, FWorkbook as j, FWorksheet as A } from "@univerjs/sheets/facade";
  7. import { DeleteCommentTreeCommand as y, DeleteCommentCommand as D, getDT as f, UpdateCommentCommand as M, ResolveCommentCommand as L, AddCommentCommand as b, ThreadCommentModel as N } from "@univerjs/thread-comment";
  8. import { deserializeRangeWithSheet as V } from "@univerjs/engine-formula";
  9. import { filter as H } from "rxjs";
  10. var q = Object.defineProperty, G = Object.getOwnPropertyDescriptor, J = (r, e, t, n) => {
  11. for (var o = n > 1 ? void 0 : n ? G(e, t) : e, m = r.length - 1, s; m >= 0; m--)
  12. (s = r[m]) && (o = (n ? s(e, t, o) : s(o)) || o);
  13. return n && o && q(e, t, o), o;
  14. }, a = (r, e) => (t, n) => e(t, n, r);
  15. class T {
  16. constructor(e) {
  17. w(this, "_comment", {
  18. id: g(),
  19. ref: "",
  20. threadId: "",
  21. dT: "",
  22. personId: "",
  23. text: $.newEmptyData().body,
  24. attachments: [],
  25. unitId: "",
  26. subUnitId: ""
  27. });
  28. e && (this._comment = e);
  29. }
  30. /**
  31. * Create a new FTheadCommentItem
  32. * @param {IThreadComment|undefined} comment The comment
  33. * @returns {FTheadCommentItem} A new instance of FTheadCommentItem
  34. * @example
  35. * ```ts
  36. * const comment = univerAPI.newTheadComment();
  37. * ```
  38. */
  39. static create(e) {
  40. return new T(e);
  41. }
  42. /**
  43. * Get the person id of the comment
  44. * @returns {string} The person id of the comment
  45. * @example
  46. * ```ts
  47. * const comment = univerAPI.getActiveWorkbook()
  48. * .getSheetById(sheetId)
  49. * .getCommentById(commentId);
  50. * const personId = comment.personId;
  51. * ```
  52. */
  53. get personId() {
  54. return this._comment.personId;
  55. }
  56. /**
  57. * Get the date time of the comment
  58. * @returns {string} The date time of the comment
  59. * @example
  60. * ```ts
  61. * const comment = univerAPI.getActiveWorkbook()
  62. * .getSheetById(sheetId)
  63. * .getCommentById(commentId);
  64. * const dateTime = comment.dateTime;
  65. * ```
  66. */
  67. get dateTime() {
  68. return this._comment.dT;
  69. }
  70. /**
  71. * Get the content of the comment
  72. * @returns {RichTextValue} The content of the comment
  73. * @example
  74. * ```ts
  75. * const comment = univerAPI.getActiveWorkbook()
  76. * .getSheetById(sheetId)
  77. * .getCommentById(commentId);
  78. * const content = comment.content;
  79. * ```
  80. */
  81. get content() {
  82. return l.createByBody(this._comment.text);
  83. }
  84. /**
  85. * Get the id of the comment
  86. * @returns {string} The id of the comment
  87. * @example
  88. * ```ts
  89. * const comment = univerAPI.getActiveWorkbook()
  90. * .getSheetById(sheetId)
  91. * .getCommentById(commentId);
  92. * const id = comment.id;
  93. * ```
  94. */
  95. get id() {
  96. return this._comment.id;
  97. }
  98. /**
  99. * Get the thread id of the comment
  100. * @returns {string} The thread id of the comment
  101. * @example
  102. * ```ts
  103. * const comment = univerAPI.getActiveWorkbook()
  104. * .getSheetById(sheetId)
  105. * .getCommentById(commentId);
  106. * const threadId = comment.threadId;
  107. * ```
  108. */
  109. get threadId() {
  110. return this._comment.threadId;
  111. }
  112. /**
  113. * Copy the comment
  114. * @returns {FTheadCommentBuilder} The comment builder
  115. * @example
  116. * ```ts
  117. * const comment = univerAPI.getActiveWorkbook()
  118. * .getSheetById(sheetId)
  119. * .getCommentById(commentId);
  120. * const newComment = comment.copy();
  121. * ```
  122. */
  123. copy() {
  124. return U.create(B.deepClone(this._comment));
  125. }
  126. }
  127. class U extends T {
  128. static create(e) {
  129. return new U(e);
  130. }
  131. /**
  132. * Set the content of the comment
  133. * @param {IDocumentBody | RichTextValue} content The content of the comment
  134. * @returns {FTheadCommentBuilder} The comment builder
  135. * @example
  136. * ```ts
  137. * const comment = univerAPI.newTheadComment()
  138. * .setContent(univerAPI.newRichText().insertText('hello zhangsan'));
  139. * ```
  140. */
  141. setContent(e) {
  142. return e instanceof l ? this._comment.text = e.getData().body : this._comment.text = e, this;
  143. }
  144. /**
  145. * Set the person id of the comment
  146. * @param {string} userId The person id of the comment
  147. * @returns {FTheadCommentBuilder} The comment builder
  148. * @example
  149. * ```ts
  150. * const comment = univerAPI.newTheadComment()
  151. * .setPersonId('123');
  152. * ```
  153. */
  154. setPersonId(e) {
  155. return this._comment.personId = e, this;
  156. }
  157. /**
  158. * Set the date time of the comment
  159. * @param {Date} date The date time of the comment
  160. * @returns {FTheadCommentBuilder} The comment builder
  161. * @example
  162. * ```ts
  163. * const comment = univerAPI.newTheadComment()
  164. * .setDateTime(new Date());
  165. * ```
  166. */
  167. setDateTime(e) {
  168. return this._comment.dT = f(e), this;
  169. }
  170. /**
  171. * Set the id of the comment
  172. * @param {string} id The id of the comment
  173. * @returns {FTheadCommentBuilder} The comment builder
  174. * @example
  175. * ```ts
  176. * const comment = univerAPI.newTheadComment()
  177. * .setId('123');
  178. * ```
  179. */
  180. setId(e) {
  181. return this._comment.id = e, this;
  182. }
  183. /**
  184. * Set the thread id of the comment
  185. * @param {string} threadId The thread id of the comment
  186. * @returns {FTheadCommentBuilder} The comment builder
  187. * @example
  188. * ```ts
  189. * const comment = univerAPI.newTheadComment()
  190. * .setThreadId('123');
  191. * ```
  192. */
  193. setThreadId(e) {
  194. return this._comment.threadId = e, this;
  195. }
  196. /**
  197. * Build the comment
  198. * @returns {IThreadComment} The comment
  199. * @example
  200. * ```ts
  201. * const comment = univerAPI.newTheadComment()
  202. * .setContent(univerAPI.newRichText().insertText('hello zhangsan'))
  203. * .build();
  204. * ```
  205. */
  206. build() {
  207. return this._comment;
  208. }
  209. }
  210. let i = class {
  211. constructor(r, e, t, n, o, m, s) {
  212. this._thread = r, this._parent = e, this._injector = t, this._commandService = n, this._univerInstanceService = o, this._threadCommentModel = m, this._userManagerService = s;
  213. }
  214. _getRef() {
  215. var t;
  216. const r = ((t = this._parent) == null ? void 0 : t.ref) || this._thread.ref;
  217. return V(r).range;
  218. }
  219. /**
  220. * Whether the comment is a root comment
  221. * @returns Whether the comment is a root comment
  222. * @example
  223. * ```ts
  224. * const comment = univerAPI.getActiveWorkbook()
  225. * .getSheetById(sheetId)
  226. * .getCommentById(commentId);
  227. * const isRoot = comment.getIsRoot();
  228. * ```
  229. */
  230. getIsRoot() {
  231. return !this._parent;
  232. }
  233. /**
  234. * Get the comment data
  235. * @returns The comment data
  236. * @example
  237. * ```ts
  238. * const comment = univerAPI.getActiveWorkbook()
  239. * .getSheetById(sheetId)
  240. * .getCommentById(commentId);
  241. * const commentData = comment.getCommentData();
  242. * ```
  243. */
  244. getCommentData() {
  245. const { children: r, ...e } = this._thread;
  246. return e;
  247. }
  248. /**
  249. * Get the replies of the comment
  250. * @returns the replies of the comment
  251. * @example
  252. * ```ts
  253. * const comment = univerAPI.getActiveWorkbook()
  254. * .getSheetById(sheetId)
  255. * .getCommentById(commentId);
  256. * const replies = comment.getReplies();
  257. * ```
  258. */
  259. getReplies() {
  260. var t;
  261. const r = this._getRef(), e = this._threadCommentModel.getCommentWithChildren(this._thread.unitId, this._thread.subUnitId, r.startRow, r.startColumn);
  262. return (t = e == null ? void 0 : e.children) == null ? void 0 : t.map((n) => this._injector.createInstance(i, n));
  263. }
  264. /**
  265. * Get the range of the comment
  266. * @returns The range of the comment
  267. * @example
  268. * ```ts
  269. * const comment = univerAPI.getActiveWorkbook()
  270. * .getSheetById(sheetId)
  271. * .getCommentById(commentId);
  272. * const range = comment.getRange();
  273. * ```
  274. */
  275. getRange() {
  276. const r = this._univerInstanceService.getUnit(this._thread.unitId, W.UNIVER_SHEET);
  277. if (!r)
  278. return null;
  279. const e = r.getSheetBySheetId(this._thread.subUnitId);
  280. if (!e)
  281. return null;
  282. const t = this._getRef();
  283. return this._injector.createInstance(x, r, e, t);
  284. }
  285. /**
  286. * @deprecated use `getRichText` as instead
  287. */
  288. getContent() {
  289. return this._thread.text;
  290. }
  291. /**
  292. * Get the rich text of the comment
  293. * @returns {RichTextValue} The rich text of the comment
  294. * @example
  295. * ```ts
  296. * const comment = univerAPI.getActiveWorkbook()
  297. * .getSheetById(sheetId)
  298. * .getCommentById(commentId);
  299. * const richText = comment.getRichText();
  300. * ```
  301. */
  302. getRichText() {
  303. const r = this._thread.text;
  304. return l.create({ body: r, documentStyle: {}, id: "d" });
  305. }
  306. /**
  307. * Delete the comment and it's replies
  308. * @returns {Promise<boolean>} success or not
  309. * @example
  310. * ```ts
  311. * const comment = univerAPI.getActiveWorkbook()
  312. * .getSheetById(sheetId)
  313. * .getCommentById(commentId);
  314. * const success = await comment.deleteAsync();
  315. * ```
  316. */
  317. deleteAsync() {
  318. return this._commandService.executeCommand(
  319. this.getIsRoot() ? y.id : D.id,
  320. {
  321. commentId: this._thread.id,
  322. unitId: this._thread.unitId,
  323. subUnitId: this._thread.subUnitId
  324. }
  325. );
  326. }
  327. /**
  328. * @deprecated use `deleteAsync` as instead.
  329. */
  330. delete() {
  331. return this.deleteAsync();
  332. }
  333. /**
  334. * @param content
  335. * @deprecated use `updateAsync` as instead
  336. */
  337. async update(r) {
  338. return this.updateAsync(r);
  339. }
  340. /**
  341. * Update the comment content
  342. * @param content The new content of the comment
  343. * @returns success or not
  344. * @example
  345. * ```ts
  346. * const comment = univerAPI.getActiveWorkbook()
  347. * .getSheetById(sheetId)
  348. * .getCommentById(commentId);
  349. * const success = await comment.updateAsync(univerAPI.newRichText().insertText('hello zhangsan'));
  350. * ```
  351. */
  352. async updateAsync(r) {
  353. const e = r instanceof l ? r.getData().body : r, t = f();
  354. return await this._commandService.executeCommand(
  355. M.id,
  356. {
  357. unitId: this._thread.unitId,
  358. subUnitId: this._thread.subUnitId,
  359. payload: {
  360. commentId: this._thread.id,
  361. text: e,
  362. updated: !0,
  363. updateT: t
  364. }
  365. }
  366. );
  367. }
  368. /**
  369. * @param resolved
  370. * @deprecated use `resolveAsync` as instead
  371. */
  372. resolve(r) {
  373. return this.resolveAsync(r);
  374. }
  375. /**
  376. * Resolve the comment
  377. * @param resolved Whether the comment is resolved
  378. * @returns success or not
  379. * @example
  380. * ```ts
  381. * const comment = univerAPI.getActiveWorkbook()
  382. * .getSheetById(sheetId)
  383. * .getCommentById(commentId);
  384. * const success = await comment.resolveAsync(true);
  385. * ```
  386. */
  387. resolveAsync(r) {
  388. return this._commandService.executeCommand(
  389. L.id,
  390. {
  391. unitId: this._thread.unitId,
  392. subUnitId: this._thread.subUnitId,
  393. commentId: this._thread.id,
  394. resolved: r != null ? r : !this._thread.resolved
  395. }
  396. );
  397. }
  398. /**
  399. * Reply to the comment
  400. * @param comment The comment to reply to
  401. * @returns success or not
  402. * @example
  403. * ```ts
  404. * const comment = univerAPI.getActiveWorkbook()
  405. * .getSheetById(sheetId)
  406. * .getCommentById(commentId);
  407. *
  408. * const reply = univerAPI.newTheadComment()
  409. * .setContent(univerAPI.newRichText().insertText('hello zhangsan'));
  410. *
  411. * const success = await comment.replyAsync(reply);
  412. * ```
  413. */
  414. async replyAsync(r) {
  415. var t;
  416. const e = r.build();
  417. return this._commandService.executeCommand(
  418. b.id,
  419. {
  420. unitId: this._thread.unitId,
  421. subUnitId: this._thread.subUnitId,
  422. comment: {
  423. id: g(),
  424. parentId: this._thread.id,
  425. threadId: this._thread.threadId,
  426. ref: (t = this._parent) == null ? void 0 : t.ref,
  427. unitId: this._thread.unitId,
  428. subUnitId: this._thread.subUnitId,
  429. text: e.text,
  430. attachments: e.attachments,
  431. dT: e.dT || f(),
  432. personId: e.personId || this._userManagerService.getCurrentUser().userID
  433. }
  434. }
  435. );
  436. }
  437. };
  438. i = J([
  439. a(2, v(F)),
  440. a(3, p),
  441. a(4, O),
  442. a(5, v(h)),
  443. a(6, v(S))
  444. ], i);
  445. class K extends x {
  446. getComment() {
  447. const t = this._injector.get(h), n = this._workbook.getUnitId(), o = this._worksheet.getSheetId(), m = t.getByLocation(n, o, this._range.startRow, this._range.startColumn);
  448. if (!m)
  449. return null;
  450. const s = t.getComment(n, o, m);
  451. return s ? this._injector.createInstance(i, s) : null;
  452. }
  453. getComments() {
  454. const t = this._injector.get(h), n = this._workbook.getUnitId(), o = this._worksheet.getSheetId(), m = [];
  455. return z.foreach(this._range, (s, c) => {
  456. const u = t.getByLocation(n, o, s, c);
  457. if (u) {
  458. const C = t.getComment(n, o, u);
  459. C && m.push(this._injector.createInstance(i, C));
  460. }
  461. }), m;
  462. }
  463. addComment(e) {
  464. var k;
  465. const t = this._injector, n = (k = this.getComment()) == null ? void 0 : k.getCommentData(), o = t.get(p), m = t.get(S), s = this._workbook.getUnitId(), c = this._worksheet.getSheetId(), u = `${B.chatAtABC(this._range.startColumn)}${this._range.startRow + 1}`, C = m.getCurrentUser(), _ = e instanceof U ? e.build() : { text: e };
  466. return o.executeCommand(b.id, {
  467. unitId: s,
  468. subUnitId: c,
  469. comment: {
  470. text: _.text,
  471. dT: _.dT || f(),
  472. attachments: [],
  473. id: _.id || g(),
  474. ref: u,
  475. personId: _.personId || C.userID,
  476. parentId: n == null ? void 0 : n.id,
  477. unitId: s,
  478. subUnitId: c,
  479. threadId: (n == null ? void 0 : n.threadId) || g()
  480. }
  481. });
  482. }
  483. clearComment() {
  484. var s;
  485. const e = this._injector, t = (s = this.getComment()) == null ? void 0 : s.getCommentData(), n = e.get(p), o = this._workbook.getUnitId(), m = this._worksheet.getSheetId();
  486. return t ? n.executeCommand(y.id, {
  487. unitId: o,
  488. subUnitId: m,
  489. threadId: t.threadId,
  490. commentId: t.id
  491. }) : Promise.resolve(!0);
  492. }
  493. clearComments() {
  494. const t = this.getComments().map((n) => n.deleteAsync());
  495. return Promise.all(t).then(() => !0);
  496. }
  497. addCommentAsync(e) {
  498. return this.addComment(e);
  499. }
  500. clearCommentAsync() {
  501. return this.clearComment();
  502. }
  503. clearCommentsAsync() {
  504. return this.clearComments();
  505. }
  506. }
  507. x.extend(K);
  508. class Q extends j {
  509. _initialize() {
  510. Object.defineProperty(this, "_threadCommentModel", {
  511. get() {
  512. return this._injector.get(N);
  513. }
  514. });
  515. }
  516. getComments() {
  517. return this._threadCommentModel.getUnit(this._workbook.getUnitId()).map((e) => this._injector.createInstance(i, e.root));
  518. }
  519. clearComments() {
  520. const t = this.getComments().map((n) => n.deleteAsync());
  521. return Promise.all(t).then(() => !0);
  522. }
  523. /**
  524. * @param callback
  525. * @deprecated
  526. */
  527. onThreadCommentChange(e) {
  528. return I(this._threadCommentModel.commentUpdate$.pipe(H((t) => t.unitId === this._workbook.getUnitId())).subscribe(e));
  529. }
  530. /**
  531. * @param callback
  532. * @deprecated
  533. */
  534. onBeforeAddThreadComment(e) {
  535. return I(this._commandService.beforeCommandExecuted((t, n) => {
  536. const o = t.params;
  537. if (t.id === b.id) {
  538. if (o.unitId !== this._workbook.getUnitId())
  539. return;
  540. if (e(o, n) === !1)
  541. throw new Error("Command is stopped by the hook onBeforeAddThreadComment");
  542. }
  543. }));
  544. }
  545. /**
  546. * @param callback
  547. * @deprecated
  548. */
  549. onBeforeUpdateThreadComment(e) {
  550. return I(this._commandService.beforeCommandExecuted((t, n) => {
  551. const o = t.params;
  552. if (t.id === M.id) {
  553. if (o.unitId !== this._workbook.getUnitId())
  554. return;
  555. if (e(o, n) === !1)
  556. throw new Error("Command is stopped by the hook onBeforeUpdateThreadComment");
  557. }
  558. }));
  559. }
  560. /**
  561. * @param callback
  562. * @deprecated
  563. */
  564. onBeforeDeleteThreadComment(e) {
  565. return I(this._commandService.beforeCommandExecuted((t, n) => {
  566. const o = t.params;
  567. if (t.id === D.id || t.id === y.id) {
  568. if (o.unitId !== this._workbook.getUnitId())
  569. return;
  570. if (e(o, n) === !1)
  571. throw new Error("Command is stopped by the hook onBeforeDeleteThreadComment");
  572. }
  573. }));
  574. }
  575. }
  576. j.extend(Q);
  577. class X extends A {
  578. getComments() {
  579. return this._injector.get(h).getSubUnitAll(this._workbook.getUnitId(), this._worksheet.getSheetId()).map((n) => this._injector.createInstance(i, n));
  580. }
  581. clearComments() {
  582. const t = this.getComments().map((n) => n.deleteAsync());
  583. return Promise.all(t).then(() => !0);
  584. }
  585. /**
  586. * Subscribe to comment events.
  587. * @param callback Callback function, param contains comment info and target cell.
  588. */
  589. onCommented(e) {
  590. return this._injector.get(p).onCommandExecuted((n) => {
  591. if (n.id === b.id) {
  592. const o = n.params;
  593. e(o);
  594. }
  595. });
  596. }
  597. getCommentById(e) {
  598. const n = this._injector.get(h).getComment(this._workbook.getUnitId(), this._worksheet.getSheetId(), e);
  599. if (n)
  600. return this._injector.createInstance(i, n);
  601. }
  602. }
  603. A.extend(X);
  604. const d = {
  605. CommentAdded: "CommentAdded",
  606. BeforeCommentAdd: "BeforeCommentAdd",
  607. CommentUpdated: "CommentUpdated",
  608. BeforeCommentUpdate: "BeforeCommentUpdate",
  609. CommentDeleted: "CommentDeleted",
  610. BeforeCommentDeleted: "BeforeCommentDeleted",
  611. CommentResolved: "CommentResolved",
  612. BeforeCommentResolve: "BeforeCommentResolve"
  613. };
  614. class Y extends R {
  615. get CommentAdded() {
  616. return d.CommentAdded;
  617. }
  618. get BeforeCommentAdd() {
  619. return d.BeforeCommentAdd;
  620. }
  621. get CommentUpdated() {
  622. return d.CommentUpdated;
  623. }
  624. get BeforeCommentUpdate() {
  625. return d.BeforeCommentUpdate;
  626. }
  627. get CommentDeleted() {
  628. return d.CommentDeleted;
  629. }
  630. get BeforeCommentDeleted() {
  631. return d.BeforeCommentDeleted;
  632. }
  633. get CommentResolved() {
  634. return d.CommentResolved;
  635. }
  636. get BeforeCommentResolve() {
  637. return d.BeforeCommentResolve;
  638. }
  639. }
  640. R.extend(Y);
  641. export {
  642. i as FThreadComment
  643. };