|
@@ -5,31 +5,32 @@ import React, {
|
|
|
import EventEmitter from 'events';
|
|
import EventEmitter from 'events';
|
|
|
|
|
|
|
|
import {
|
|
import {
|
|
|
- envUtils, IPageHasId, PageGrant, pathUtils,
|
|
|
|
|
|
|
+ IPageHasId, PageGrant, pathUtils,
|
|
|
} from '@growi/core';
|
|
} from '@growi/core';
|
|
|
import detectIndent from 'detect-indent';
|
|
import detectIndent from 'detect-indent';
|
|
|
import { useTranslation } from 'next-i18next';
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import { useRouter } from 'next/router';
|
|
import { useRouter } from 'next/router';
|
|
|
import { throttle, debounce } from 'throttle-debounce';
|
|
import { throttle, debounce } from 'throttle-debounce';
|
|
|
|
|
|
|
|
-import { saveOrUpdate } from '~/client/services/page-operation';
|
|
|
|
|
|
|
+import { useSaveOrUpdate } from '~/client/services/page-operation';
|
|
|
import { toastSuccess, toastError } from '~/client/util/apiNotification';
|
|
import { toastSuccess, toastError } from '~/client/util/apiNotification';
|
|
|
import { apiGet, apiPostForm } from '~/client/util/apiv1-client';
|
|
import { apiGet, apiPostForm } from '~/client/util/apiv1-client';
|
|
|
-import { getOptionsToSave } from '~/client/util/editor';
|
|
|
|
|
import { IEditorMethods } from '~/interfaces/editor-methods';
|
|
import { IEditorMethods } from '~/interfaces/editor-methods';
|
|
|
|
|
+import { OptionsToSave } from '~/interfaces/page-operation';
|
|
|
import {
|
|
import {
|
|
|
useCurrentPathname, useCurrentPageId, useIsEnabledAttachTitleHeader, useTemplateBodyData,
|
|
useCurrentPathname, useCurrentPageId, useIsEnabledAttachTitleHeader, useTemplateBodyData,
|
|
|
- useIsEditable, useIsIndentSizeForced, useIsUploadableFile, useIsUploadableImage, useEditingMarkdown, useIsNotFound,
|
|
|
|
|
|
|
+ useIsEditable, useIsUploadableFile, useIsUploadableImage, useIsNotFound, useIsIndentSizeForced,
|
|
|
} from '~/stores/context';
|
|
} from '~/stores/context';
|
|
|
import {
|
|
import {
|
|
|
useCurrentIndentSize, useSWRxSlackChannels, useIsSlackEnabled, useIsTextlintEnabled, usePageTagsForEditors,
|
|
useCurrentIndentSize, useSWRxSlackChannels, useIsSlackEnabled, useIsTextlintEnabled, usePageTagsForEditors,
|
|
|
useIsEnabledUnsavedWarning,
|
|
useIsEnabledUnsavedWarning,
|
|
|
|
|
+ useEditingMarkdown,
|
|
|
} from '~/stores/editor';
|
|
} from '~/stores/editor';
|
|
|
import { useCurrentPagePath, useSWRxCurrentPage } from '~/stores/page';
|
|
import { useCurrentPagePath, useSWRxCurrentPage } from '~/stores/page';
|
|
|
import { usePreviewOptions } from '~/stores/renderer';
|
|
import { usePreviewOptions } from '~/stores/renderer';
|
|
|
import {
|
|
import {
|
|
|
EditorMode,
|
|
EditorMode,
|
|
|
- useEditorMode, useIsMobile, useSelectedGrant,
|
|
|
|
|
|
|
+ useEditorMode, useSelectedGrant,
|
|
|
} from '~/stores/ui';
|
|
} from '~/stores/ui';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
@@ -43,7 +44,10 @@ import scrollSyncHelper from './PageEditor/ScrollSyncHelper';
|
|
|
const logger = loggerFactory('growi:PageEditor');
|
|
const logger = loggerFactory('growi:PageEditor');
|
|
|
|
|
|
|
|
|
|
|
|
|
-declare const globalEmitter: EventEmitter;
|
|
|
|
|
|
|
+declare global {
|
|
|
|
|
+ // eslint-disable-next-line vars-on-top, no-var
|
|
|
|
|
+ var globalEmitter: EventEmitter;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
|
// for scrolling
|
|
// for scrolling
|
|
@@ -68,17 +72,17 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
const { data: templateBodyData } = useTemplateBodyData();
|
|
const { data: templateBodyData } = useTemplateBodyData();
|
|
|
const { data: isEditable } = useIsEditable();
|
|
const { data: isEditable } = useIsEditable();
|
|
|
const { data: editorMode, mutate: mutateEditorMode } = useEditorMode();
|
|
const { data: editorMode, mutate: mutateEditorMode } = useEditorMode();
|
|
|
- const { data: isMobile } = useIsMobile();
|
|
|
|
|
const { data: isSlackEnabled } = useIsSlackEnabled();
|
|
const { data: isSlackEnabled } = useIsSlackEnabled();
|
|
|
const { data: slackChannelsData } = useSWRxSlackChannels(currentPagePath);
|
|
const { data: slackChannelsData } = useSWRxSlackChannels(currentPagePath);
|
|
|
const { data: isTextlintEnabled } = useIsTextlintEnabled();
|
|
const { data: isTextlintEnabled } = useIsTextlintEnabled();
|
|
|
const { data: isIndentSizeForced } = useIsIndentSizeForced();
|
|
const { data: isIndentSizeForced } = useIsIndentSizeForced();
|
|
|
- const { data: indentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
|
|
|
|
|
- const { data: isEnabledUnsavedWarning, mutate: mutateIsEnabledUnsavedWarning } = useIsEnabledUnsavedWarning();
|
|
|
|
|
|
|
+ const { data: currentIndentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
|
|
|
const { data: isUploadableFile } = useIsUploadableFile();
|
|
const { data: isUploadableFile } = useIsUploadableFile();
|
|
|
const { data: isUploadableImage } = useIsUploadableImage();
|
|
const { data: isUploadableImage } = useIsUploadableImage();
|
|
|
|
|
|
|
|
const { data: rendererOptions } = usePreviewOptions();
|
|
const { data: rendererOptions } = usePreviewOptions();
|
|
|
|
|
+ const { mutate: mutateIsEnabledUnsavedWarning } = useIsEnabledUnsavedWarning();
|
|
|
|
|
+ const saveOrUpdate = useSaveOrUpdate();
|
|
|
|
|
|
|
|
const currentRevisionId = currentPage?.revision?._id;
|
|
const currentRevisionId = currentPage?.revision?._id;
|
|
|
|
|
|
|
@@ -106,20 +110,6 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
const editorRef = useRef<IEditorMethods>(null);
|
|
const editorRef = useRef<IEditorMethods>(null);
|
|
|
const previewRef = useRef<HTMLDivElement>(null);
|
|
const previewRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- // const optionsToSave = useMemo(() => {
|
|
|
|
|
- // if (grantData == null) {
|
|
|
|
|
- // return;
|
|
|
|
|
- // }
|
|
|
|
|
- // const slackChannels = slackChannelsData ? slackChannelsData.toString() : '';
|
|
|
|
|
- // const optionsToSave = getOptionsToSave(
|
|
|
|
|
- // isSlackEnabled ?? false, slackChannels,
|
|
|
|
|
- // grantData.grant, grantData.grantedGroup?.id, grantData.grantedGroup?.name,
|
|
|
|
|
- // pageTags || [],
|
|
|
|
|
- // );
|
|
|
|
|
- // return optionsToSave;
|
|
|
|
|
- // }, [grantData, isSlackEnabled, pageTags, slackChannelsData]);
|
|
|
|
|
-
|
|
|
|
|
const setMarkdownWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string, isClean: boolean) => {
|
|
const setMarkdownWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string, isClean: boolean) => {
|
|
|
markdownToSave.current = value;
|
|
markdownToSave.current = value;
|
|
|
setMarkdownToPreview(value);
|
|
setMarkdownToPreview(value);
|
|
@@ -143,16 +133,21 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
const grant = grantData.grant || PageGrant.GRANT_PUBLIC;
|
|
const grant = grantData.grant || PageGrant.GRANT_PUBLIC;
|
|
|
const grantedGroup = grantData?.grantedGroup;
|
|
const grantedGroup = grantData?.grantedGroup;
|
|
|
|
|
|
|
|
- const optionsToSave = Object.assign(
|
|
|
|
|
- getOptionsToSave(isSlackEnabled, slackChannels, grant || 1, grantedGroup?.id, grantedGroup?.name, pageTags || []),
|
|
|
|
|
- { ...opts },
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ const optionsToSave: OptionsToSave = {
|
|
|
|
|
+ isSlackEnabled,
|
|
|
|
|
+ slackChannels,
|
|
|
|
|
+ grant: grant || 1,
|
|
|
|
|
+ pageTags: pageTags || [],
|
|
|
|
|
+ grantUserGroupId: grantedGroup?.id,
|
|
|
|
|
+ grantUserGroupName: grantedGroup?.name,
|
|
|
|
|
+ ...opts,
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
const { page } = await saveOrUpdate(
|
|
const { page } = await saveOrUpdate(
|
|
|
- optionsToSave,
|
|
|
|
|
- { pageId, path: currentPagePath || currentPathname, revisionId: currentRevisionId },
|
|
|
|
|
markdownToSave.current,
|
|
markdownToSave.current,
|
|
|
|
|
+ { pageId, path: currentPagePath || currentPathname, revisionId: currentRevisionId },
|
|
|
|
|
+ optionsToSave,
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
return page;
|
|
return page;
|
|
@@ -172,7 +167,7 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
// eslint-disable-next-line max-len
|
|
|
- }, [grantData, isSlackEnabled, currentPathname, slackChannels, pageTags, pageId, currentPagePath, currentRevisionId]);
|
|
|
|
|
|
|
+ }, [grantData, isSlackEnabled, currentPathname, slackChannels, pageTags, saveOrUpdate, pageId, currentPagePath, currentRevisionId]);
|
|
|
|
|
|
|
|
const saveAndReturnToViewHandler = useCallback(async(opts?: {overwriteScopesOfDescendants: boolean}) => {
|
|
const saveAndReturnToViewHandler = useCallback(async(opts?: {overwriteScopesOfDescendants: boolean}) => {
|
|
|
if (editorMode !== EditorMode.Editor) {
|
|
if (editorMode !== EditorMode.Editor) {
|
|
@@ -183,10 +178,7 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
if (page == null) {
|
|
if (page == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- // The updateFn should be a promise or asynchronous function to handle the remote mutation
|
|
|
|
|
- // it should return updated data. see: https://swr.vercel.app/docs/mutation#optimistic-updates
|
|
|
|
|
- // Moreover, `async() => false` does not work since it's too fast to be calculated.
|
|
|
|
|
- await mutateIsEnabledUnsavedWarning(new Promise(r => setTimeout(() => r(false), 10)), { optimisticData: () => false });
|
|
|
|
|
|
|
+
|
|
|
if (isNotFound) {
|
|
if (isNotFound) {
|
|
|
await router.push(`/${page._id}`);
|
|
await router.push(`/${page._id}`);
|
|
|
}
|
|
}
|
|
@@ -195,7 +187,7 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
await mutateCurrentPage();
|
|
await mutateCurrentPage();
|
|
|
}
|
|
}
|
|
|
mutateEditorMode(EditorMode.View);
|
|
mutateEditorMode(EditorMode.View);
|
|
|
- }, [editorMode, save, mutateIsEnabledUnsavedWarning, isNotFound, mutateEditorMode, router, mutateCurrentPageId, mutateCurrentPage]);
|
|
|
|
|
|
|
+ }, [editorMode, save, isNotFound, mutateEditorMode, router, mutateCurrentPageId, mutateCurrentPage]);
|
|
|
|
|
|
|
|
const saveWithShortcut = useCallback(async() => {
|
|
const saveWithShortcut = useCallback(async() => {
|
|
|
if (editorMode !== EditorMode.Editor) {
|
|
if (editorMode !== EditorMode.Editor) {
|
|
@@ -411,33 +403,21 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
}
|
|
}
|
|
|
}, [editorMode]);
|
|
}, [editorMode]);
|
|
|
|
|
|
|
|
- // Unnecessary code. Delete after PageEditor and PageEditorByHackmd implementation has completed. -- 2022.09.06 Yuki Takei
|
|
|
|
|
- //
|
|
|
|
|
- // set handler to update editor value
|
|
|
|
|
- // useEffect(() => {
|
|
|
|
|
- // const handler = (markdown) => {
|
|
|
|
|
- // if (editorRef.current != null) {
|
|
|
|
|
- // editorRef.current.setValue(markdown);
|
|
|
|
|
- // }
|
|
|
|
|
- // };
|
|
|
|
|
- // globalEmitter.on('updateEditorValue', handler);
|
|
|
|
|
-
|
|
|
|
|
- // return function cleanup() {
|
|
|
|
|
- // globalEmitter.removeListener('updateEditorValue', handler);
|
|
|
|
|
- // };
|
|
|
|
|
- // }, []);
|
|
|
|
|
-
|
|
|
|
|
// Detect indent size from contents (only when users are allowed to change it)
|
|
// Detect indent size from contents (only when users are allowed to change it)
|
|
|
- // useEffect(() => {
|
|
|
|
|
- // const currentPageMarkdown = pageContainer.state.markdown;
|
|
|
|
|
- // if (!isIndentSizeForced && currentPageMarkdown != null) {
|
|
|
|
|
- // const detectedIndent = detectIndent(currentPageMarkdown);
|
|
|
|
|
- // if (detectedIndent.type === 'space' && new Set([2, 4]).has(detectedIndent.amount)) {
|
|
|
|
|
- // mutateCurrentIndentSize(detectedIndent.amount);
|
|
|
|
|
- // }
|
|
|
|
|
- // }
|
|
|
|
|
- // }, [isIndentSizeForced, mutateCurrentIndentSize, pageContainer.state.markdown]);
|
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ // do nothing if the indent size fixed
|
|
|
|
|
+ if (isIndentSizeForced == null || isIndentSizeForced) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ // detect from markdown
|
|
|
|
|
+ if (initialValue != null) {
|
|
|
|
|
+ const detectedIndent = detectIndent(initialValue);
|
|
|
|
|
+ if (detectedIndent.type === 'space' && new Set([2, 4]).has(detectedIndent.amount)) {
|
|
|
|
|
+ mutateCurrentIndentSize(detectedIndent.amount);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [initialValue, isIndentSizeForced, mutateCurrentIndentSize]);
|
|
|
|
|
|
|
|
if (!isEditable) {
|
|
if (!isEditable) {
|
|
|
return <></>;
|
|
return <></>;
|
|
@@ -458,7 +438,7 @@ const PageEditor = React.memo((): JSX.Element => {
|
|
|
isUploadable={isUploadable}
|
|
isUploadable={isUploadable}
|
|
|
isUploadableFile={isUploadableFile}
|
|
isUploadableFile={isUploadableFile}
|
|
|
isTextlintEnabled={isTextlintEnabled}
|
|
isTextlintEnabled={isTextlintEnabled}
|
|
|
- indentSize={indentSize}
|
|
|
|
|
|
|
+ indentSize={currentIndentSize}
|
|
|
onScroll={editorScrolledHandler}
|
|
onScroll={editorScrolledHandler}
|
|
|
onScrollCursorIntoView={editorScrollCursorIntoViewHandler}
|
|
onScrollCursorIntoView={editorScrollCursorIntoViewHandler}
|
|
|
onChange={markdownChangedHandler}
|
|
onChange={markdownChangedHandler}
|