Shun Miyazawa 1 rok temu
rodzic
commit
ec8e5618fb

+ 2 - 7
apps/app/src/client/services/side-effects/yjs.ts

@@ -7,11 +7,7 @@ import { useCurrentPageYjsData } from '~/stores/yjs';
 
 export const useCurrentPageYjsDataEffect = (): void => {
   const { data: socket } = useGlobalSocket();
-  const { updateHasDraft, updateHasRevisionBodyDiff, updateAwarenessStateSize } = useCurrentPageYjsData();
-
-  const hasDraftUpdateHandler = useCallback((hasDraft: boolean) => {
-    updateHasDraft(hasDraft);
-  }, [updateHasDraft]);
+  const { updateHasRevisionBodyDiff, updateAwarenessStateSize } = useCurrentPageYjsData();
 
   const hasRevisionBodyDiffUpdateHandler = useCallback((hasRevisionBodyDiff: boolean) => {
     updateHasRevisionBodyDiff(hasRevisionBodyDiff);
@@ -25,7 +21,6 @@ export const useCurrentPageYjsDataEffect = (): void => {
 
     if (socket == null) { return }
 
-    socket.on(SocketEventName.YjsHasDraftUpdated, hasDraftUpdateHandler);
     socket.on(SocketEventName.YjsHasRevisionBodyDiffUpdated, hasRevisionBodyDiffUpdateHandler);
     socket.on(SocketEventName.YjsAwarenessStateSizeUpdated, awarenessStateSizeUpdateHandler);
 
@@ -35,5 +30,5 @@ export const useCurrentPageYjsDataEffect = (): void => {
       socket.off(SocketEventName.YjsAwarenessStateSizeUpdated, awarenessStateSizeUpdateHandler);
     };
 
-  }, [socket, awarenessStateSizeUpdateHandler, hasRevisionBodyDiffUpdateHandler, hasDraftUpdateHandler]);
+  }, [socket, awarenessStateSizeUpdateHandler, hasRevisionBodyDiffUpdateHandler]);
 };

+ 2 - 6
apps/app/src/components/PageEditor/PageEditor.tsx

@@ -46,7 +46,6 @@ import {
 } from '~/stores/ui';
 import { useEditingUsers } from '~/stores/use-editing-users';
 import { useNextThemes } from '~/stores/use-next-themes';
-import { useCurrentPageYjsData } from '~/stores/yjs';
 import loggerFactory from '~/utils/logger';
 
 import { EditorNavbar } from './EditorNavbar';
@@ -108,7 +107,6 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
   const { data: editorSettings } = useEditorSettings();
   const { mutate: mutateIsGrantNormalized } = useSWRxCurrentGrantData(currentPage?._id);
   const { data: user } = useCurrentUser();
-  const { data: yjsData } = useCurrentPageYjsData();
   const { onEditorsUpdated } = useEditingUsers();
   const onConflict = useConflictResolver();
 
@@ -152,9 +150,7 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
     initialValueRef.current = initialValue;
   }, [initialValue]);
 
-  const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
-
-  const [markdownToPreview, setMarkdownToPreview] = useState<string>(codeMirrorEditor?.getDoc() ?? '');
+  const [markdownToPreview, setMarkdownToPreview] = useState<string>(initialValue);
   const setMarkdownPreviewWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string) => {
     setMarkdownToPreview(value);
   })), []);
@@ -163,6 +159,7 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
     setMarkdownPreviewWithDebounce(value);
   }, [setMarkdownPreviewWithDebounce]);
 
+  const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
 
   const { scrollEditorHandler, scrollPreviewHandler } = useScrollSync(GlobalCodeMirrorEditorKey.MAIN, previewRef);
 
@@ -356,7 +353,6 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
       <div className={`flex-expand-horiz ${props.visibility ? '' : 'd-none'}`}>
         <div className="page-editor-editor-container flex-expand-vert border-end">
           <CodeMirrorEditorMain
-            hasYjsDraft={yjsData?.hasYjsDraft ?? false}
             isEditorMode={editorMode === EditorMode.Editor}
             onChange={markdownChangedHandler}
             onSave={saveWithShortcut}

+ 0 - 1
apps/app/src/interfaces/websocket.ts

@@ -50,7 +50,6 @@ export const SocketEventName = {
   PageDeleted: 'page:delete',
 
   // Yjs
-  YjsHasDraftUpdated: 'yjs:has-draft-update',
   YjsAwarenessStateSizeUpdated: 'yjs:awareness-state-size-update',
   YjsHasRevisionBodyDiffUpdated: 'yjs:has-revision-body-diff-update',
 } as const;

+ 0 - 1
apps/app/src/interfaces/yjs.ts

@@ -1,5 +1,4 @@
 export type CurrentPageYjsData = {
-  hasYjsDraft?: boolean,
   hasRevisionBodyDiff?: boolean,
   awarenessStateSize?: number,
 }

+ 0 - 1
apps/app/src/server/service/page/index.ts

@@ -4456,7 +4456,6 @@ class PageService implements IPageService {
     const hasRevisionBodyDiff = await this.hasRevisionBodyDiff(pageId, yjsDraft);
 
     return {
-      hasYjsDraft: yjsDraft != null,
       hasRevisionBodyDiff,
       awarenessStateSize: currentYdoc?.awareness.states.size,
     };

+ 0 - 4
apps/app/src/server/service/socket-io.js

@@ -192,10 +192,6 @@ class SocketIoService {
       });
 
       socket.on(GlobalSocketEventName.YDocSync, async({ pageId, initialValue }) => {
-        this.io
-          .in(getRoomNameWithId(RoomPrefix.PAGE, pageId))
-          .emit('hasYjsDraft', true);
-
         try {
           await yjsConnectionManager.handleYDocSync(pageId, initialValue);
         }

+ 2 - 2
apps/app/src/server/service/yjs-connection-manager.ts

@@ -48,7 +48,7 @@ class YjsConnectionManager {
     return this.instance;
   }
 
-  public async handleYDocSync(pageId: string, initialValue?: string): Promise<void> {
+  public async handleYDocSync(pageId: string, initialValue: string): Promise<void> {
     const currentYdoc = this.getCurrentYdoc(pageId);
     if (currentYdoc == null) {
       return;
@@ -62,7 +62,7 @@ class YjsConnectionManager {
     const persistedCodeMirrorText = persistedYdoc.getText('codemirror').toString();
     const currentCodeMirrorText = currentYdoc.getText('codemirror').toString();
 
-    if (persistedCodeMirrorText === '' && currentCodeMirrorText === '' && initialValue != null) {
+    if (persistedCodeMirrorText === '' && currentCodeMirrorText === '') {
       currentYdoc.getText('codemirror').insert(0, initialValue);
     }
 

+ 1 - 6
apps/app/src/stores/yjs.ts

@@ -10,7 +10,6 @@ import type { CurrentPageYjsData } from '~/interfaces/yjs';
 import { useCurrentPageId } from './page';
 
 type CurrentPageYjsDataUtils = {
-  updateHasDraft(hasYjsDraft: boolean): void
   updateHasRevisionBodyDiff(hasRevisionBodyDiff: boolean): void
   updateAwarenessStateSize(awarenessStateSize: number): void
 }
@@ -18,10 +17,6 @@ type CurrentPageYjsDataUtils = {
 export const useCurrentPageYjsData = (): SWRResponse<CurrentPageYjsData, Error> & CurrentPageYjsDataUtils => {
   const swrResponse = useSWRStatic<CurrentPageYjsData, Error>('currentPageYjsData', undefined);
 
-  const updateHasDraft = useCallback((hasYjsDraft: boolean) => {
-    swrResponse.mutate({ ...swrResponse.data, hasYjsDraft });
-  }, [swrResponse]);
-
   const updateHasRevisionBodyDiff = useCallback((hasRevisionBodyDiff: boolean) => {
     swrResponse.mutate({ ...swrResponse.data, hasRevisionBodyDiff });
   }, [swrResponse]);
@@ -31,7 +26,7 @@ export const useCurrentPageYjsData = (): SWRResponse<CurrentPageYjsData, Error>
   }, [swrResponse]);
 
   return {
-    ...swrResponse, updateHasDraft, updateHasRevisionBodyDiff, updateAwarenessStateSize,
+    ...swrResponse, updateHasRevisionBodyDiff, updateAwarenessStateSize,
   };
 };
 

+ 2 - 3
packages/editor/src/components/CodeMirrorEditorMain.tsx

@@ -22,19 +22,18 @@ type Props = CodeMirrorEditorProps & {
   pageId?: string,
   initialValue?: string,
   isEditorMode: boolean,
-  hasYjsDraft: boolean,
   onEditorsUpdated?: (userList: IUserHasId[]) => void,
 }
 
 export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
   const {
-    user, pageId, initialValue, isEditorMode, hasYjsDraft,
+    user, pageId, initialValue, isEditorMode,
     onSave, onEditorsUpdated, ...otherProps
   } = props;
 
   const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
 
-  useCollaborativeEditorMode(isEditorMode, hasYjsDraft, user, pageId, initialValue, onEditorsUpdated, codeMirrorEditor);
+  useCollaborativeEditorMode(isEditorMode, user, pageId, initialValue, onEditorsUpdated, codeMirrorEditor);
 
   // setup additional extensions
   useEffect(() => {

+ 0 - 1
packages/editor/src/components/playground/Playground.tsx

@@ -70,7 +70,6 @@ export const Playground = (): JSX.Element => {
       <div className="flex-expand-horiz">
         <div className="flex-expand-vert">
           <CodeMirrorEditorMain
-            hasYjsDraft={false}
             isEditorMode
             onSave={saveHandler}
             onChange={setMarkdownToPreview}

+ 2 - 3
packages/editor/src/stores/use-collaborative-editor-mode.ts

@@ -19,7 +19,6 @@ type UserLocalState = {
 
 export const useCollaborativeEditorMode = (
     isEnabled: boolean,
-    hasDraft: boolean,
     user?: IUserHasId,
     pageId?: string,
     initialValue?: string,
@@ -94,7 +93,7 @@ export const useCollaborativeEditorMode = (
     socketIOProvider.on('sync', (isSync: boolean) => {
       if (isSync) {
         // If no draft exists, insert initial value
-        socket.emit(GlobalSocketEventName.YDocSync, { pageId, initialValue: hasDraft ? undefined : initialValue });
+        socket.emit(GlobalSocketEventName.YDocSync, { pageId, initialValue });
 
         const userList: IUserHasId[] = Array.from(socketIOProvider.awareness.states.values(), value => value.user.user && value.user.user);
         onEditorsUpdated(userList);
@@ -111,7 +110,7 @@ export const useCollaborativeEditorMode = (
     });
 
     setProvider(socketIOProvider);
-  }, [hasDraft, initialValue, onEditorsUpdated, pageId, provider, socket, user, ydoc]);
+  }, [initialValue, onEditorsUpdated, pageId, provider, socket, user, ydoc]);
 
   // Setup Ydoc Extensions
   useEffect(() => {