Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add lock unlock archive restore realtime sync #5629

Draft
wants to merge 21 commits into
base: preview
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
448a615
fix: add lock unlock archive restore realtime sync
Palanikannan1437 Sep 17, 2024
cd29430
fix: show only after editor loads
Palanikannan1437 Sep 17, 2024
8ce39f5
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Sep 23, 2024
c94fff9
fix: added strong types
Palanikannan1437 Sep 23, 2024
2c2dd62
fix: live events fixed
Palanikannan1437 Sep 24, 2024
c322122
fix: remove unused vars and logs
Palanikannan1437 Sep 24, 2024
e3d3ab5
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Oct 1, 2024
83933ec
fix: converted objects to enum
Palanikannan1437 Oct 3, 2024
6f13c19
fix: error handling and removing the events in read only mode
Palanikannan1437 Oct 3, 2024
5a835e4
fix: added check to only update if the image aspect ratio is not pres…
Palanikannan1437 Oct 3, 2024
1732587
fix: imports
Palanikannan1437 Oct 3, 2024
20861a6
fix: props order
Palanikannan1437 Oct 3, 2024
ed751e3
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Oct 3, 2024
2384f0b
revert: no need of these changes anymore
Palanikannan1437 Oct 3, 2024
bbe7e62
fix: updated type names
Palanikannan1437 Oct 3, 2024
b0bf242
fix: order of things
Palanikannan1437 Oct 3, 2024
b8c76eb
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Oct 8, 2024
b42f552
fix: fixed types and renamed variables
Palanikannan1437 Oct 8, 2024
702236e
fix: better typing for the real time updates
Palanikannan1437 Oct 8, 2024
ee9e7f5
fix: trying multiplexing our socket connection
Palanikannan1437 Oct 8, 2024
b45761e
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions live/src/core/hocuspocus-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { v4 as uuidv4 } from "uuid";
import { handleAuthentication } from "@/core/lib/authentication.js";
// extensions
import { getExtensions } from "@/core/extensions/index.js";
import {
DocumentEventResponses,
TDocumentEventsServer,
} from "@plane/editor/lib";

export const getHocusPocusServer = async () => {
const extensions = await getExtensions();
Expand Down Expand Up @@ -31,6 +35,12 @@ export const getHocusPocusServer = async () => {
throw Error("Authentication unsuccessful!");
}
},
async onStateless({ payload, document }) {
const response = DocumentEventResponses[payload as TDocumentEventsServer];
if (response) {
document.broadcastStateless(response);
}
},
extensions,
debounce: 10000,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EditorRefApi, ICollaborativeDocumentEditor } from "@/types";

const CollaborativeDocumentEditor = (props: ICollaborativeDocumentEditor) => {
const {
socket,
aiHandler,
containerClassName,
disabledExtensions,
Expand Down Expand Up @@ -47,6 +48,7 @@ const CollaborativeDocumentEditor = (props: ICollaborativeDocumentEditor) => {
editorClassName,
embedHandler,
extensions,
socket,
fileHandler,
forwardedRef,
handleEditorReady,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
height: `${Math.round(initialHeight)}px` satisfies Pixel,
aspectRatio: aspectRatioCalculated,
};

setSize(initialComputedSize);
updateAttributesSafely(
initialComputedSize,
Expand Down
6 changes: 6 additions & 0 deletions packages/editor/src/core/helpers/document-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum DocumentEventResponses {
Lock = "locked",
Unlock = "unlocked",
Archive = "archived",
Unarchive = "unarchived",
}
25 changes: 14 additions & 11 deletions packages/editor/src/core/hooks/use-collaborative-editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useLayoutEffect, useMemo, useState } from "react";
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { HocuspocusProvider } from "@hocuspocus/provider";
import Collaboration from "@tiptap/extension-collaboration";
import { IndexeddbPersistence } from "y-indexeddb";
Expand Down Expand Up @@ -28,6 +28,7 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
serverHandler,
tabIndex,
user,
socket,
} = props;
// states
const [hasServerConnectionFailed, setHasServerConnectionFailed] = useState(false);
Expand All @@ -36,11 +37,12 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
const provider = useMemo(
() =>
new HocuspocusProvider({
websocketProvider: socket,
name: id,
parameters: realtimeConfig.queryParams,
// parameters: realtimeConfig.queryParams,
// using user id as a token to verify the user on the server
token: user.id,
url: realtimeConfig.url,
// url: realtimeConfig.url,
onAuthenticationFailed: () => {
serverHandler?.onServerError?.();
setHasServerConnectionFailed(true);
Expand All @@ -57,14 +59,15 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
[id, realtimeConfig, serverHandler, user.id]
);

// destroy and disconnect connection on unmount
useEffect(
() => () => {
provider.destroy();
provider.disconnect();
},
[provider]
);
// // destroy and disconnect connection on unmount
// useEffect(
// () => () => {
// provider.destroy();
// provider.disconnect();
// },
// [provider]
// );

// indexed db integration for offline support
useLayoutEffect(() => {
const localProvider = new IndexeddbPersistence(id, provider.document);
Expand Down
15 changes: 12 additions & 3 deletions packages/editor/src/core/hooks/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import { IMarking, scrollSummary } from "@/helpers/scroll-to-node";
// props
import { CoreEditorProps } from "@/props";
// types
import { EditorRefApi, IMentionHighlight, IMentionSuggestion, TEditorCommands, TFileHandler } from "@/types";
import type {
TDocumentEventsServer,
EditorRefApi,
IMentionHighlight,
IMentionSuggestion,
TEditorCommands,
TFileHandler,
} from "@/types";

export interface CustomEditorProps {
editorClassName: string;
Expand Down Expand Up @@ -55,9 +62,9 @@ export const useEditor = (props: CustomEditorProps) => {
mentionHandler,
onChange,
placeholder,
provider,
tabIndex,
value,
provider,
} = props;
// states

Expand Down Expand Up @@ -236,7 +243,7 @@ export const useEditor = (props: CustomEditorProps) => {
if (empty) return null;

const nodesArray: string[] = [];
state.doc.nodesBetween(from, to, (node, pos, parent) => {
state.doc.nodesBetween(from, to, (node, _pos, parent) => {
if (parent === state.doc && editorRef.current) {
const serializer = DOMSerializer.fromSchema(editorRef.current?.schema);
const dom = serializer.serializeNode(node);
Expand Down Expand Up @@ -277,6 +284,8 @@ export const useEditor = (props: CustomEditorProps) => {
if (!document) return;
Y.applyUpdate(document, value);
},
emitRealTimeUpdate: (message: TDocumentEventsServer) => provider?.sendStateless(message),
listenToRealTimeUpdate: () => provider,
}),
[editorRef, savedSelection]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export const useReadOnlyCollaborativeEditor = (props: TReadOnlyCollaborativeEdit
},
onSynced: () => setHasServerSynced(true),
}),
[id, realtimeConfig, user.id]
[id, realtimeConfig, serverHandler, user.id]
);

// destroy and disconnect connection on unmount
useEffect(
() => () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/core/hooks/use-read-only-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IMarking, scrollSummary } from "@/helpers/scroll-to-node";
// props
import { CoreReadOnlyEditorProps } from "@/props";
// types
import { EditorReadOnlyRefApi, IMentionHighlight, TFileHandler } from "@/types";
import { EditorReadOnlyRefApi, IMentionHighlight, TDocumentEventsServer, TFileHandler } from "@/types";

interface CustomReadOnlyEditorProps {
initialValue?: string;
Expand Down Expand Up @@ -117,6 +117,8 @@ export const useReadOnlyEditor = (props: CustomReadOnlyEditorProps) => {
editorRef.current?.off("update");
};
},
emitRealTimeUpdate: (message: TDocumentEventsServer) => provider?.sendStateless(message),
listenToRealTimeUpdate: () => provider,
getHeadings: () => editorRef?.current?.storage.headingList.headings,
}));

Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/core/types/collaboration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TRealtimeConfig,
TUserDetails,
} from "@/types";
import { HocuspocusProviderWebsocket } from "@hocuspocus/provider";

export type TServerHandler = {
onConnect?: () => void;
Expand Down Expand Up @@ -41,6 +42,7 @@ export type TCollaborativeEditorProps = TCollaborativeEditorHookProps & {
forwardedRef?: React.MutableRefObject<EditorRefApi | null>;
placeholder?: string | ((isFocused: boolean, value: string) => string);
tabIndex?: number;
socket?: HocuspocusProviderWebsocket;
};

export type TReadOnlyCollaborativeEditorProps = TCollaborativeEditorHookProps & {
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/core/types/document-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DocumentEventResponses } from "@/helpers/document-events";

export type TDocumentEventsServer = keyof typeof DocumentEventResponses;
export type TDocumentEventsClient = `${DocumentEventResponses}`;
9 changes: 7 additions & 2 deletions packages/editor/src/core/types/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
TFileHandler,
TNonColorEditorCommands,
TServerHandler,
TDocumentEventsServer,
} from "@/types";

import { HocuspocusProvider, HocuspocusProviderWebsocket } from "@hocuspocus/provider";

// editor refs
export type EditorReadOnlyRefApi = {
getMarkDown: () => string;
Expand All @@ -31,8 +35,8 @@ export type EditorReadOnlyRefApi = {
paragraphs: number;
words: number;
};
onHeadingChange: (callback: (headings: IMarking[]) => void) => () => void;
getHeadings: () => IMarking[];
emitRealTimeUpdate: (message: TDocumentEventsServer) => void;
listenToRealTimeUpdate: () => HocuspocusProvider;
};

export interface EditorRefApi extends EditorReadOnlyRefApi {
Expand Down Expand Up @@ -101,6 +105,7 @@ export interface ICollaborativeDocumentEditor
realtimeConfig: TRealtimeConfig;
serverHandler?: TServerHandler;
user: TUserDetails;
socket: HocuspocusProviderWebsocket;
}

// read only editor props
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/core/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./image";
export * from "./mention-suggestion";
export * from "./slash-commands-suggestion";
export * from "@/plane-editor/types";
export * from "./document-events";
2 changes: 2 additions & 0 deletions packages/editor/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "@/extensions/core-without-props";
export * from "@/helpers/document-events";
export * from "@/types/document-events";
3 changes: 2 additions & 1 deletion packages/ui/src/icons/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { ISvgIcons } from "./type";
export * from "./cycle";
export * from "./module";
export * from "./state";
Expand Down Expand Up @@ -31,4 +32,4 @@ export * from "./favorite-folder-icon";
export * from "./planned-icon";
export * from "./in-progress-icon";
export * from "./done-icon";
export * from "./pending-icon";
export * from "./pending-icon";
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { HocuspocusProviderWebsocket } from "@hocuspocus/provider";
import { observer } from "mobx-react";
import Link from "next/link";
import { useParams } from "next/navigation";
Expand Down
10 changes: 8 additions & 2 deletions web/core/components/pages/editor/editor-body.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useMemo } from "react";
import { useCallback, useMemo, useState } from "react";
import { HocuspocusProviderWebsocket } from "@hocuspocus/provider";
import { observer } from "mobx-react";
import { useParams } from "next/navigation";
// document-editor
Expand Down Expand Up @@ -35,6 +36,7 @@ import { useIssueEmbed } from "@/plane-web/hooks/use-issue-embed";
import { FileService } from "@/services/file.service";
// store
import { IPage } from "@/store/pages/page";
import { getSocketConnection } from "./socket";

// services init
const fileService = new FileService();
Expand Down Expand Up @@ -123,7 +125,7 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {
onConnect: handleServerConnect,
onServerError: handleServerError,
}),
[]
[handleServerConnect, handleServerError]
);

const realtimeConfig: TRealtimeConfig | undefined = useMemo(() => {
Expand Down Expand Up @@ -151,6 +153,9 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {

if (pageId === undefined || !realtimeConfig) return <PageContentLoader />;

const socket = useMemo(() => getSocketConnection(realtimeConfig, currentUser?.id), [realtimeConfig]);
console.log("socket connection", socket);

return (
<div className="flex items-center h-full w-full overflow-y-auto">
<Row
Expand Down Expand Up @@ -181,6 +186,7 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {
</div>
{isContentEditable ? (
<CollaborativeDocumentEditorWithRef
socket={socket as HocuspocusProviderWebsocket}
id={pageId}
fileHandler={getEditorFileHandlers({
maxFileSize,
Expand Down
Loading
Loading