editor.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { useCallback, useEffect } from 'react';
  2. import { type Nullable } from '@growi/core';
  3. import { withUtils, type SWRResponseWithUtils, useSWRStatic } from '@growi/core/dist/swr';
  4. import type { EditorSettings } from '@growi/editor';
  5. import useSWR, { type SWRResponse } from 'swr';
  6. import useSWRImmutable from 'swr/immutable';
  7. import { apiGet } from '~/client/util/apiv1-client';
  8. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  9. import type { SlackChannels } from '~/interfaces/user-trigger-notification';
  10. import {
  11. useCurrentUser, useDefaultIndentSize, useIsGuestUser, useIsReadOnlyUser,
  12. } from '~/stores-universal/context';
  13. // import { localStorageMiddleware } from './middlewares/sync-to-storage';
  14. import { useSWRxTagsInfo } from './page';
  15. export const useWaitingSaveProcessing = (): SWRResponse<boolean, Error> => {
  16. return useSWRStatic('waitingSaveProcessing', undefined, { fallbackData: false });
  17. };
  18. export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
  19. return useSWRStatic('editingMarkdown', initialData);
  20. };
  21. type EditorSettingsOperation = {
  22. update: (updateData: Partial<EditorSettings>) => Promise<void>,
  23. }
  24. // TODO: Enable localStorageMiddleware
  25. // - Unabling localStorageMiddleware occurrs a flickering problem when loading theme.
  26. // - see: https://github.com/weseek/growi/pull/6781#discussion_r1000285786
  27. export const useEditorSettings = (): SWRResponseWithUtils<EditorSettingsOperation, EditorSettings, Error> => {
  28. const { data: currentUser } = useCurrentUser();
  29. const { data: isGuestUser } = useIsGuestUser();
  30. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  31. const swrResult = useSWRImmutable(
  32. (isGuestUser || isReadOnlyUser) ? null : ['/personal-setting/editor-settings', currentUser?.username],
  33. ([endpoint]) => {
  34. return apiv3Get(endpoint).then(result => result.data);
  35. },
  36. {
  37. // use: [localStorageMiddleware], // store to localStorage for initialization fastly
  38. // fallbackData: undefined,
  39. },
  40. );
  41. return withUtils<EditorSettingsOperation, EditorSettings, Error>(swrResult, {
  42. update: async(updateData) => {
  43. const { data, mutate } = swrResult;
  44. if (data == null) {
  45. return;
  46. }
  47. mutate({ ...data, ...updateData }, false);
  48. // invoke API
  49. await apiv3Put('/personal-setting/editor-settings', updateData);
  50. },
  51. });
  52. };
  53. export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
  54. const { data: defaultIndentSize } = useDefaultIndentSize();
  55. return useSWRStatic<number, Error>(
  56. defaultIndentSize == null ? null : 'currentIndentSize',
  57. undefined,
  58. { fallbackData: defaultIndentSize },
  59. );
  60. };
  61. /*
  62. * Slack Notification
  63. */
  64. export const useSWRxSlackChannels = (currentPagePath: Nullable<string>): SWRResponse<string[], Error> => {
  65. const shouldFetch: boolean = currentPagePath != null;
  66. return useSWR(
  67. shouldFetch ? ['/pages.updatePost', currentPagePath] : null,
  68. ([endpoint, path]) => apiGet(endpoint, { path }).then((response: SlackChannels) => response.updatePost),
  69. {
  70. revalidateOnFocus: false,
  71. fallbackData: [''],
  72. },
  73. );
  74. };
  75. export const useIsSlackEnabled = (): SWRResponse<boolean, Error> => {
  76. return useSWRStatic(
  77. 'isSlackEnabled',
  78. undefined,
  79. { fallbackData: false },
  80. );
  81. };
  82. export type IPageTagsForEditorsOption = {
  83. sync: (tags?: string[]) => void;
  84. }
  85. export const usePageTagsForEditors = (pageId: Nullable<string>): SWRResponse<string[], Error> & IPageTagsForEditorsOption => {
  86. const { data: tagsInfoData } = useSWRxTagsInfo(pageId);
  87. const swrResult = useSWRStatic<string[], Error>('pageTags', undefined);
  88. const { mutate } = swrResult;
  89. const sync = useCallback((): void => {
  90. mutate(tagsInfoData?.tags || [], false);
  91. }, [mutate, tagsInfoData?.tags]);
  92. return {
  93. ...swrResult,
  94. sync,
  95. };
  96. };
  97. export const useIsEnabledUnsavedWarning = (): SWRResponse<boolean, Error> => {
  98. return useSWRStatic<boolean, Error>('isEnabledUnsavedWarning');
  99. };
  100. export const useReservedNextCaretLine = (initialData?: number): SWRResponse<number> => {
  101. const swrResponse = useSWRStatic('saveNextCaretLine', initialData, { fallbackData: 0 });
  102. const { mutate } = swrResponse;
  103. useEffect(() => {
  104. const handler = (lineNumber: number) => {
  105. mutate(lineNumber);
  106. };
  107. globalEmitter.on('reservedNextCaretLine', handler);
  108. return function cleanup() {
  109. globalEmitter.removeListener('reservedNextCaretLine', handler);
  110. };
  111. }, [mutate]);
  112. return {
  113. ...swrResponse,
  114. };
  115. };