PageContainer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. import { Container } from 'unstated';
  2. import * as entities from 'entities';
  3. import * as toastr from 'toastr';
  4. import { pagePathUtils } from '@growi/core';
  5. import { apiPost } from '../util/apiv1-client';
  6. import loggerFactory from '~/utils/logger';
  7. import { toastError } from '../util/apiNotification';
  8. import {
  9. DetachCodeBlockInterceptor,
  10. RestoreCodeBlockInterceptor,
  11. } from '../util/interceptor/detach-code-blocks';
  12. import {
  13. DrawioInterceptor,
  14. } from '../util/interceptor/drawio-interceptor';
  15. const { isTrashPage } = pagePathUtils;
  16. const logger = loggerFactory('growi:services:PageContainer');
  17. /**
  18. * Service container related to Page
  19. * @extends {Container} unstated Container
  20. */
  21. export default class PageContainer extends Container {
  22. constructor(appContainer) {
  23. super();
  24. this.appContainer = appContainer;
  25. this.appContainer.registerContainer(this);
  26. this.state = {};
  27. const mainContent = document.querySelector('#content-main');
  28. if (mainContent == null) {
  29. logger.debug('#content-main element is not exists');
  30. return;
  31. }
  32. const revisionId = mainContent.getAttribute('data-page-revision-id');
  33. const path = decodeURI(mainContent.getAttribute('data-path'));
  34. this.state = {
  35. // local page data
  36. markdown: null, // will be initialized after initStateMarkdown()
  37. pageId: mainContent.getAttribute('data-page-id'),
  38. revisionId,
  39. revisionCreatedAt: +mainContent.getAttribute('data-page-revision-created'),
  40. path,
  41. tocHtml: '',
  42. isBookmarked: false,
  43. sumOfBookmarks: 0,
  44. seenUsers: [],
  45. seenUserIds: [],
  46. sumOfSeenUsers: [],
  47. isLiked: false,
  48. likers: [],
  49. likerIds: [],
  50. sumOfLikers: 0,
  51. createdAt: mainContent.getAttribute('data-page-created-at'),
  52. updatedAt: mainContent.getAttribute('data-page-updated-at'),
  53. deletedAt: mainContent.getAttribute('data-page-deleted-at') || null,
  54. isUserPage: JSON.parse(mainContent.getAttribute('data-page-user')) != null,
  55. isTrashPage: isTrashPage(path),
  56. isDeleted: JSON.parse(mainContent.getAttribute('data-page-is-deleted')),
  57. isDeletable: JSON.parse(mainContent.getAttribute('data-page-is-deletable')),
  58. isNotCreatable: JSON.parse(mainContent.getAttribute('data-page-is-not-creatable')),
  59. isAbleToDeleteCompletely: JSON.parse(mainContent.getAttribute('data-page-is-able-to-delete-completely')),
  60. isPageExist: mainContent.getAttribute('data-page-id') != null,
  61. pageUser: JSON.parse(mainContent.getAttribute('data-page-user')),
  62. tags: null,
  63. hasChildren: JSON.parse(mainContent.getAttribute('data-page-has-children')),
  64. templateTagData: mainContent.getAttribute('data-template-tags') || null,
  65. shareLinksNumber: mainContent.getAttribute('data-share-links-number'),
  66. shareLinkId: JSON.parse(mainContent.getAttribute('data-share-link-id') || null),
  67. // latest(on remote) information
  68. remoteRevisionId: revisionId,
  69. remoteRevisionBody: null,
  70. remoteRevisionUpdateAt: null,
  71. revisionIdHackmdSynced: mainContent.getAttribute('data-page-revision-id-hackmd-synced') || null,
  72. lastUpdateUsername: mainContent.getAttribute('data-page-last-update-username') || null,
  73. deleteUsername: mainContent.getAttribute('data-page-delete-username') || null,
  74. pageIdOnHackmd: mainContent.getAttribute('data-page-id-on-hackmd') || null,
  75. hasDraftOnHackmd: !!mainContent.getAttribute('data-page-has-draft-on-hackmd'),
  76. isHackmdDraftUpdatingInRealtime: false,
  77. isConflictDiffModalOpen: false,
  78. revisionsOnConflict: {},
  79. };
  80. // parse creator, lastUpdateUser and revisionAuthor
  81. try {
  82. this.state.creator = JSON.parse(mainContent.getAttribute('data-page-creator'));
  83. }
  84. catch (e) {
  85. logger.warn('The data of \'data-page-creator\' is invalid', e);
  86. }
  87. try {
  88. this.state.revisionAuthor = JSON.parse(mainContent.getAttribute('data-page-revision-author'));
  89. this.state.lastUpdateUser = JSON.parse(mainContent.getAttribute('data-page-revision-author'));
  90. }
  91. catch (e) {
  92. logger.warn('The data of \'data-page-revision-author\' is invalid', e);
  93. }
  94. const { interceptorManager } = this.appContainer;
  95. interceptorManager.addInterceptor(new DetachCodeBlockInterceptor(appContainer), 10); // process as soon as possible
  96. interceptorManager.addInterceptor(new DrawioInterceptor(appContainer), 20);
  97. interceptorManager.addInterceptor(new RestoreCodeBlockInterceptor(appContainer), 900); // process as late as possible
  98. this.initStateMarkdown();
  99. this.checkAndUpdateImageUrlCached(this.state.likers);
  100. const { isSharedUser } = this.appContainer;
  101. // see https://dev.growi.org/5fabddf8bbeb1a0048bcb9e9
  102. const isAbleToGetAttachedInformationAboutPages = this.state.isPageExist && !isSharedUser;
  103. if (isAbleToGetAttachedInformationAboutPages) {
  104. // We don't retrieve bookmarks in the initial page load
  105. // as it is stored in a separate collection to like and seen user
  106. // data so it has a separate api endpoint.
  107. this.initialPageLoad();
  108. this.retrieveBookmarkInfo();
  109. }
  110. this.setTocHtml = this.setTocHtml.bind(this);
  111. this.save = this.save.bind(this);
  112. this.checkAndUpdateImageUrlCached = this.checkAndUpdateImageUrlCached.bind(this);
  113. this.emitJoinPageRoomRequest = this.emitJoinPageRoomRequest.bind(this);
  114. this.emitJoinPageRoomRequest();
  115. this.addWebSocketEventHandlers = this.addWebSocketEventHandlers.bind(this);
  116. this.addWebSocketEventHandlers();
  117. const unlinkPageButton = document.getElementById('unlink-page-button');
  118. if (unlinkPageButton != null) {
  119. unlinkPageButton.addEventListener('click', async() => {
  120. try {
  121. const res = await this.appContainer.apiPost('/pages.unlink', { path });
  122. window.location.href = encodeURI(`${res.path}?unlinked=true`);
  123. }
  124. catch (err) {
  125. toastError(err);
  126. }
  127. });
  128. }
  129. }
  130. /**
  131. * Workaround for the mangling in production build to break constructor.name
  132. */
  133. static getClassName() {
  134. return 'PageContainer';
  135. }
  136. get isAbleToOpenPageEditor() {
  137. const { isNotCreatable, isTrashPage } = this.state;
  138. const { isGuestUser } = this.appContainer;
  139. return (!isNotCreatable && !isTrashPage && !isGuestUser);
  140. }
  141. /**
  142. * whether to display reaction buttons
  143. * ex.) like, bookmark
  144. */
  145. get isAbleToShowPageReactionButtons() {
  146. const { isTrashPage, isPageExist } = this.state;
  147. const { isSharedUser } = this.appContainer;
  148. return (!isTrashPage && isPageExist && !isSharedUser);
  149. }
  150. /**
  151. * whether to display tag labels
  152. */
  153. get isAbleToShowTagLabel() {
  154. const { isUserPage } = this.state;
  155. const { isSharedUser } = this.appContainer;
  156. return (!isUserPage && !isSharedUser);
  157. }
  158. /**
  159. * whether to display page management
  160. * ex.) duplicate, rename
  161. */
  162. get isAbleToShowPageManagement() {
  163. const { isPageExist, isTrashPage } = this.state;
  164. const { isSharedUser } = this.appContainer;
  165. return (isPageExist && !isTrashPage && !isSharedUser);
  166. }
  167. /**
  168. * whether to display pageEditorModeManager
  169. * ex.) view, edit, hackmd
  170. */
  171. get isAbleToShowPageEditorModeManager() {
  172. const { isNotCreatable, isTrashPage } = this.state;
  173. const { isSharedUser } = this.appContainer;
  174. return (!isNotCreatable && !isTrashPage && !isSharedUser);
  175. }
  176. /**
  177. * whether to display pageAuthors
  178. * ex.) creator, lastUpdateUser
  179. */
  180. get isAbleToShowPageAuthors() {
  181. const { isPageExist, isUserPage } = this.state;
  182. return (isPageExist && !isUserPage);
  183. }
  184. /**
  185. * whether to like button
  186. * not displayed on user page
  187. */
  188. get isAbleToShowLikeButtons() {
  189. const { isUserPage } = this.state;
  190. const { isSharedUser } = this.appContainer;
  191. return (!isUserPage && !isSharedUser);
  192. }
  193. /**
  194. * whether to Empty Trash Page
  195. * not displayed when guest user and not on trash page
  196. */
  197. get isAbleToShowEmptyTrashButton() {
  198. const { currentUser } = this.appContainer;
  199. const { path, hasChildren } = this.state;
  200. return (currentUser != null && currentUser.admin && path === '/trash' && hasChildren);
  201. }
  202. /**
  203. * whether to display trash management buttons
  204. * ex.) undo, delete completly
  205. * not displayed when guest user
  206. */
  207. get isAbleToShowTrashPageManagementButtons() {
  208. const { currentUser } = this.appContainer;
  209. const { isDeleted } = this.state;
  210. return (isDeleted && currentUser != null);
  211. }
  212. /**
  213. * initialize state for markdown data
  214. */
  215. initStateMarkdown() {
  216. let pageContent = '';
  217. const rawText = document.getElementById('raw-text-original');
  218. if (rawText) {
  219. pageContent = rawText.innerHTML;
  220. }
  221. const markdown = entities.decodeHTML(pageContent);
  222. this.state.markdown = markdown;
  223. }
  224. async initialPageLoad() {
  225. {
  226. const {
  227. data: {
  228. likerIds, sumOfLikers, isLiked, seenUserIds, sumOfSeenUsers, isSeen,
  229. },
  230. } = await this.appContainer.apiv3Get('/page/info', { pageId: this.state.pageId });
  231. await this.setState({
  232. sumOfLikers,
  233. isLiked,
  234. likerIds,
  235. seenUserIds,
  236. sumOfSeenUsers,
  237. isSeen,
  238. });
  239. }
  240. await this.retrieveLikersAndSeenUsers();
  241. }
  242. async toggleLike() {
  243. {
  244. const toggledIsLiked = !this.state.isLiked;
  245. await this.appContainer.apiv3Put('/page/likes', { pageId: this.state.pageId, bool: toggledIsLiked });
  246. await this.setState(state => ({
  247. isLiked: toggledIsLiked,
  248. sumOfLikers: toggledIsLiked ? state.sumOfLikers + 1 : state.sumOfLikers - 1,
  249. likerIds: toggledIsLiked
  250. ? [...this.state.likerIds, this.appContainer.currentUserId]
  251. : state.likerIds.filter(id => id !== this.appContainer.currentUserId),
  252. }));
  253. }
  254. await this.retrieveLikersAndSeenUsers();
  255. }
  256. async retrieveLikersAndSeenUsers() {
  257. const { users } = await this.appContainer.apiGet('/users.list', { user_ids: [...this.state.likerIds, ...this.state.seenUserIds].join(',') });
  258. await this.setState({
  259. likers: users.filter(({ id }) => this.state.likerIds.includes(id)).slice(0, 15),
  260. seenUsers: users.filter(({ id }) => this.state.seenUserIds.includes(id)).slice(0, 15),
  261. });
  262. this.checkAndUpdateImageUrlCached(users);
  263. }
  264. async retrieveBookmarkInfo() {
  265. const response = await this.appContainer.apiv3Get('/bookmarks/info', { pageId: this.state.pageId });
  266. this.setState({
  267. sumOfBookmarks: response.data.sumOfBookmarks,
  268. isBookmarked: response.data.isBookmarked,
  269. });
  270. }
  271. async toggleBookmark() {
  272. const bool = !this.state.isBookmarked;
  273. await this.appContainer.apiv3Put('/bookmarks', { pageId: this.state.pageId, bool });
  274. return this.retrieveBookmarkInfo();
  275. }
  276. async checkAndUpdateImageUrlCached(users) {
  277. const noImageCacheUsers = users.filter((user) => { return user.imageUrlCached == null });
  278. if (noImageCacheUsers.length === 0) {
  279. return;
  280. }
  281. const noImageCacheUserIds = noImageCacheUsers.map((user) => { return user.id });
  282. try {
  283. await this.appContainer.apiv3Put('/users/update.imageUrlCache', { userIds: noImageCacheUserIds });
  284. }
  285. catch (err) {
  286. // Error alert doesn't apear, because user don't need to notice this error.
  287. logger.error(err);
  288. }
  289. }
  290. get navigationContainer() {
  291. return this.appContainer.getContainer('NavigationContainer');
  292. }
  293. setLatestRemotePageData(s2cMessagePageUpdated) {
  294. const newState = {
  295. remoteRevisionId: s2cMessagePageUpdated.revisionId,
  296. remoteRevisionBody: s2cMessagePageUpdated.revisionBody,
  297. remoteRevisionUpdateAt: s2cMessagePageUpdated.revisionUpdateAt,
  298. revisionIdHackmdSynced: s2cMessagePageUpdated.revisionIdHackmdSynced,
  299. // TODO // TODO remove lastUpdateUsername and refactor parts that lastUpdateUsername is used
  300. lastUpdateUsername: s2cMessagePageUpdated.lastUpdateUsername,
  301. lastUpdateUser: s2cMessagePageUpdated.remoteLastUpdateUser,
  302. };
  303. if (s2cMessagePageUpdated.hasDraftOnHackmd != null) {
  304. newState.hasDraftOnHackmd = s2cMessagePageUpdated.hasDraftOnHackmd;
  305. }
  306. this.setState(newState);
  307. }
  308. setTocHtml(tocHtml) {
  309. if (this.state.tocHtml !== tocHtml) {
  310. this.setState({ tocHtml });
  311. }
  312. }
  313. /**
  314. * save success handler
  315. * @param {object} page Page instance
  316. * @param {Array[Tag]} tags Array of Tag
  317. * @param {object} revision Revision instance
  318. */
  319. updateStateAfterSave(page, tags, revision) {
  320. const { editorMode } = this.navigationContainer.state;
  321. // update state of PageContainer
  322. const newState = {
  323. pageId: page._id,
  324. revisionId: revision._id,
  325. revisionCreatedAt: new Date(revision.createdAt).getTime() / 1000,
  326. remoteRevisionId: revision._id,
  327. revisionIdHackmdSynced: page.revisionHackmdSynced,
  328. hasDraftOnHackmd: page.hasDraftOnHackmd,
  329. markdown: revision.body,
  330. createdAt: page.createdAt,
  331. updatedAt: page.updatedAt,
  332. };
  333. if (tags != null) {
  334. newState.tags = tags;
  335. }
  336. this.setState(newState);
  337. // PageEditor component
  338. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  339. if (pageEditor != null) {
  340. if (editorMode !== 'edit') {
  341. pageEditor.updateEditorValue(newState.markdown);
  342. }
  343. }
  344. // PageEditorByHackmd component
  345. const pageEditorByHackmd = this.appContainer.getComponentInstance('PageEditorByHackmd');
  346. if (pageEditorByHackmd != null) {
  347. // reset
  348. if (editorMode !== 'hackmd') {
  349. pageEditorByHackmd.reset();
  350. }
  351. }
  352. // hidden input
  353. $('input[name="revision_id"]').val(newState.revisionId);
  354. }
  355. /**
  356. * Save page
  357. * @param {string} markdown
  358. * @param {object} optionsToSave
  359. * @return {object} { page: Page, tags: Tag[] }
  360. */
  361. async save(markdown, optionsToSave = {}) {
  362. const { editorMode } = this.navigationContainer.state;
  363. const { pageId, path } = this.state;
  364. let { revisionId } = this.state;
  365. const options = Object.assign({}, optionsToSave);
  366. if (editorMode === 'hackmd') {
  367. // set option to sync
  368. options.isSyncRevisionToHackmd = true;
  369. revisionId = this.state.revisionIdHackmdSynced;
  370. }
  371. let res;
  372. if (pageId == null) {
  373. res = await this.createPage(path, markdown, options);
  374. }
  375. else {
  376. res = await this.updatePage(pageId, revisionId, markdown, options);
  377. }
  378. this.updateStateAfterSave(res.page, res.tags, res.revision);
  379. return res;
  380. }
  381. async saveAndReload(optionsToSave) {
  382. if (optionsToSave == null) {
  383. const msg = '\'saveAndReload\' requires the \'optionsToSave\' param';
  384. throw new Error(msg);
  385. }
  386. const { editorMode } = this.navigationContainer.state;
  387. if (editorMode == null) {
  388. logger.warn('\'saveAndReload\' requires the \'errorMode\' param');
  389. return;
  390. }
  391. const { pageId, path } = this.state;
  392. let { revisionId } = this.state;
  393. const options = Object.assign({}, optionsToSave);
  394. let markdown;
  395. if (editorMode === 'hackmd') {
  396. const pageEditorByHackmd = this.appContainer.getComponentInstance('PageEditorByHackmd');
  397. markdown = await pageEditorByHackmd.getMarkdown();
  398. // set option to sync
  399. options.isSyncRevisionToHackmd = true;
  400. revisionId = this.state.revisionIdHackmdSynced;
  401. }
  402. else {
  403. const pageEditor = this.appContainer.getComponentInstance('PageEditor');
  404. markdown = pageEditor.getMarkdown();
  405. }
  406. let res;
  407. if (pageId == null) {
  408. res = await this.createPage(path, markdown, options);
  409. }
  410. else {
  411. res = await this.updatePage(pageId, revisionId, markdown, options);
  412. }
  413. const editorContainer = this.appContainer.getContainer('EditorContainer');
  414. editorContainer.clearDraft(path);
  415. window.location.href = path;
  416. return res;
  417. }
  418. async createPage(pagePath, markdown, tmpParams) {
  419. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  420. // clone
  421. const params = Object.assign(tmpParams, {
  422. path: pagePath,
  423. body: markdown,
  424. });
  425. const res = await this.appContainer.apiv3Post('/pages/', params);
  426. const { page, tags, revision } = res.data;
  427. return { page, tags, revision };
  428. }
  429. async updatePage(pageId, revisionId, markdown, tmpParams) {
  430. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  431. // clone
  432. const params = Object.assign(tmpParams, {
  433. page_id: pageId,
  434. revision_id: revisionId,
  435. body: markdown,
  436. });
  437. const res = await this.appContainer.apiPost('/pages.update', params);
  438. if (!res.ok) {
  439. throw new Error(res.error);
  440. }
  441. return res;
  442. }
  443. deletePage(isRecursively, isCompletely) {
  444. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  445. // control flag
  446. const completely = isCompletely ? true : null;
  447. const recursively = isRecursively ? true : null;
  448. return this.appContainer.apiPost('/pages.remove', {
  449. recursively,
  450. completely,
  451. page_id: this.state.pageId,
  452. revision_id: this.state.revisionId,
  453. });
  454. }
  455. revertRemove(isRecursively) {
  456. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  457. // control flag
  458. const recursively = isRecursively ? true : null;
  459. return this.appContainer.apiPost('/pages.revertRemove', {
  460. recursively,
  461. page_id: this.state.pageId,
  462. });
  463. }
  464. rename(newPagePath, isRecursively, isRenameRedirect, isRemainMetadata) {
  465. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  466. const { pageId, revisionId, path } = this.state;
  467. return this.appContainer.apiv3Put('/pages/rename', {
  468. revisionId,
  469. pageId,
  470. isRecursively,
  471. isRenameRedirect,
  472. isRemainMetadata,
  473. newPagePath,
  474. path,
  475. });
  476. }
  477. showSuccessToastr() {
  478. toastr.success(undefined, 'Saved successfully', {
  479. closeButton: true,
  480. progressBar: true,
  481. newestOnTop: false,
  482. showDuration: '100',
  483. hideDuration: '100',
  484. timeOut: '1200',
  485. extendedTimeOut: '150',
  486. });
  487. }
  488. showErrorToastr(error) {
  489. toastr.error(error.message, 'Error occured', {
  490. closeButton: true,
  491. progressBar: true,
  492. newestOnTop: false,
  493. showDuration: '100',
  494. hideDuration: '100',
  495. timeOut: '3000',
  496. });
  497. }
  498. // request to server so the client to join a room for each page
  499. emitJoinPageRoomRequest() {
  500. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  501. const socket = socketIoContainer.getSocket();
  502. socket.emit('join:page', { socketId: socket.id, pageId: this.state.pageId });
  503. }
  504. addWebSocketEventHandlers() {
  505. // eslint-disable-next-line @typescript-eslint/no-this-alias
  506. const pageContainer = this;
  507. const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
  508. const socket = socketIoContainer.getSocket();
  509. socket.on('page:create', (data) => {
  510. logger.debug({ obj: data }, `websocket on 'page:create'`); // eslint-disable-line quotes
  511. // update remote page data
  512. const { s2cMessagePageUpdated } = data;
  513. if (s2cMessagePageUpdated.pageId === pageContainer.state.pageId) {
  514. pageContainer.setLatestRemotePageData(s2cMessagePageUpdated);
  515. }
  516. });
  517. socket.on('page:update', (data) => {
  518. logger.debug({ obj: data }, `websocket on 'page:update'`); // eslint-disable-line quotes
  519. // update remote page data
  520. const { s2cMessagePageUpdated } = data;
  521. if (s2cMessagePageUpdated.pageId === pageContainer.state.pageId) {
  522. pageContainer.setLatestRemotePageData(s2cMessagePageUpdated);
  523. }
  524. });
  525. socket.on('page:delete', (data) => {
  526. logger.debug({ obj: data }, `websocket on 'page:delete'`); // eslint-disable-line quotes
  527. // update remote page data
  528. const { s2cMessagePageUpdated } = data;
  529. if (s2cMessagePageUpdated.pageId === pageContainer.state.pageId) {
  530. pageContainer.setLatestRemotePageData(s2cMessagePageUpdated);
  531. }
  532. });
  533. socket.on('page:editingWithHackmd', (data) => {
  534. logger.debug({ obj: data }, `websocket on 'page:editingWithHackmd'`); // eslint-disable-line quotes
  535. // update isHackmdDraftUpdatingInRealtime
  536. const { s2cMessagePageUpdated } = data;
  537. if (s2cMessagePageUpdated.pageId === pageContainer.state.pageId) {
  538. pageContainer.setState({ isHackmdDraftUpdatingInRealtime: true });
  539. }
  540. });
  541. }
  542. /* TODO GW-325 */
  543. retrieveMyBookmarkList() {
  544. }
  545. async resolveConflict(pageId, revisionId, markdown, optionsToSave) {
  546. if (optionsToSave == null) {
  547. const msg = '\'saveAndReload\' requires the \'optionsToSave\' param';
  548. throw new Error(msg);
  549. }
  550. const { path } = this.state;
  551. const params = Object.assign(optionsToSave, {
  552. page_id: pageId,
  553. revision_id: revisionId,
  554. body: markdown,
  555. });
  556. const res = await apiPost('/pages.update', params);
  557. const editorContainer = this.appContainer.getContainer('EditorContainer');
  558. editorContainer.clearDraft(path);
  559. return res;
  560. }
  561. async resolveConflictAndReload(pageId, revisionId, markdown, optionsToSave) {
  562. await this.resolveConflict(pageId, revisionId, markdown, optionsToSave);
  563. window.location.reload();
  564. }
  565. }