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

[PE-31] feat: Add lock unlock archive restore realtime sync #5629

Merged
merged 44 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
44 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
2a22f01
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Oct 30, 2024
9f0ca0d
fix: multiplexing socket connection in read only editor as well
Palanikannan1437 Nov 7, 2024
fa53baf
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Nov 7, 2024
05474e9
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Nov 18, 2024
e846f6f
fix: remove single socket logic
Palanikannan1437 Nov 18, 2024
bb45ad2
fix: fixing the cleanup deps for the provider and localprovider
Palanikannan1437 Nov 18, 2024
a4e1b67
fix: add a better data structure for managing events
Palanikannan1437 Nov 19, 2024
d16aff9
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Nov 19, 2024
842564a
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Nov 21, 2024
bb715d2
chore: refactored realtime events into hooks
Palanikannan1437 Nov 22, 2024
5362a2c
feat: fetch page meta while focusing tabs
Palanikannan1437 Nov 22, 2024
37197c1
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Nov 26, 2024
e8d4500
fix: cycling through items on slash command item in down arrow
Palanikannan1437 Nov 26, 2024
7bc9ff2
fix: better naming convention for realtime events
Palanikannan1437 Nov 26, 2024
0626f16
fix: simplified localprovider initialization and cleaning
Palanikannan1437 Nov 26, 2024
2d648c7
fix: types from ui
Palanikannan1437 Nov 26, 2024
6c9bcf1
fix: abstracted away from exposing the provider directly
Palanikannan1437 Nov 26, 2024
173e93f
fix: coderabbit suggestions
Palanikannan1437 Nov 26, 2024
58178cf
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Nov 26, 2024
1a66f3c
regression: pass user in dependency array
Palanikannan1437 Nov 26, 2024
429bbc4
fix: removed page action api calls by the other users the document is…
Palanikannan1437 Nov 26, 2024
bc9a09d
Merge branch 'preview' into fix/lock-unlock-realtime
Palanikannan1437 Dec 2, 2024
cd26592
chore: removed unused imports
Palanikannan1437 Dec 2, 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
12 changes: 12 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 {
DocumentCollaborativeEvents,
TDocumentEventsServer,
} from "@plane/editor/lib";
// editor types
import { TUserDetails } from "@plane/editor";
// types
Expand Down Expand Up @@ -55,6 +59,14 @@ export const getHocusPocusServer = async () => {
throw Error("Authentication unsuccessful!");
}
},
async onStateless({ payload, document }) {
// broadcast the client event (derived from the server event) to all the clients so that they can update their state
const response =
DocumentCollaborativeEvents[payload as TDocumentEventsServer].client;
if (response) {
document.broadcastStateless(response);
}
},
extensions,
debounce: 10000,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DocumentCollaborativeEvents = {
Lock: { client: "locked", server: "Lock" },
Unlock: { client: "unlocked", server: "Unlock" },
Archive: { client: "archived", server: "Archive" },
Unarchive: { client: "unarchived", server: "Unarchive" },
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
height: `${Math.round(initialHeight)}px` satisfies Pixel,
aspectRatio: aspectRatioCalculated,
};

setSize(initialComputedSize);
updateAttributesSafely(
initialComputedSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ export const CustomImageNode = (props: CustomImageNodeProps) => {

useEffect(() => {
const closestEditorContainer = imageComponentRef.current?.closest(".editor-container");
if (!closestEditorContainer) {
console.error("Editor container not found");
return;
if (closestEditorContainer) {
setEditorContainer(closestEditorContainer as HTMLDivElement);
}

setEditorContainer(closestEditorContainer as HTMLDivElement);
}, []);

// the image is already uploaded if the image-component node has src attribute
Expand All @@ -55,7 +52,7 @@ export const CustomImageNode = (props: CustomImageNodeProps) => {
setResolvedSrc(url as string);
};
getImageSource();
}, [imageFromFileSystem, node.attrs.src]);
}, [imgNodeSrc]);

return (
<NodeViewWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const SlashCommandsMenu = (props: SlashCommandsMenuProps) => {
if (nextItem < 0) {
nextSection = currentSection - 1;
if (nextSection < 0) nextSection = sections.length - 1;
nextItem = sections[nextSection].items.length - 1;
nextItem = sections[nextSection]?.items.length - 1;
Palanikannan1437 marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (e.key === "ArrowDown") {
Expand Down
11 changes: 11 additions & 0 deletions packages/editor/src/core/helpers/get-document-server-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DocumentCollaborativeEvents } from "@/constants/document-collaborative-events";
import { TDocumentEventKey, TDocumentEventsClient, TDocumentEventsServer } from "@/types/document-collaborative-events";

export const getServerEventName = (clientEvent: TDocumentEventsClient): TDocumentEventsServer | undefined => {
for (const key in DocumentCollaborativeEvents) {
if (DocumentCollaborativeEvents[key as TDocumentEventKey].client === clientEvent) {
return DocumentCollaborativeEvents[key as TDocumentEventKey].server;
}
}
return undefined;
};
22 changes: 10 additions & 12 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, useMemo, useState } from "react";
import { HocuspocusProvider } from "@hocuspocus/provider";
import Collaboration from "@tiptap/extension-collaboration";
import { IndexeddbPersistence } from "y-indexeddb";
Expand Down Expand Up @@ -58,21 +58,19 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
[id, realtimeConfig, serverHandler, user]
);

// destroy and disconnect connection on unmount
const localProvider = useMemo(
() => (id ? new IndexeddbPersistence(id, provider.document) : undefined),
[id, provider]
);

// destroy and disconnect all providers connection on unmount
useEffect(
() => () => {
provider.destroy();
provider.disconnect();
provider?.destroy();
localProvider?.destroy();
},
[provider]
[provider, localProvider]
);
// indexed db integration for offline support
useLayoutEffect(() => {
const localProvider = new IndexeddbPersistence(id, provider.document);
return () => {
localProvider?.destroy();
};
}, [provider, id]);

const editor = useEditor({
id,
Expand Down
16 changes: 13 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,15 @@ import { IMarking, scrollSummary, scrollToNodeViaDOMCoordinates } from "@/helper
// props
import { CoreEditorProps } from "@/props";
// types
import { EditorRefApi, IMentionHighlight, IMentionSuggestion, TEditorCommands, TFileHandler } from "@/types";
import type {
TDocumentEventsServer,
EditorRefApi,
IMentionHighlight,
IMentionSuggestion,
TEditorCommands,
TFileHandler,
TDocumentEventEmitter,
} from "@/types";

export interface CustomEditorProps {
editorClassName: string;
Expand Down Expand Up @@ -58,9 +66,9 @@ export const useEditor = (props: CustomEditorProps) => {
onChange,
onTransaction,
placeholder,
provider,
tabIndex,
value,
provider,
autofocus = false,
} = props;
// states
Expand Down Expand Up @@ -247,7 +255,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 @@ -288,6 +296,8 @@ export const useEditor = (props: CustomEditorProps) => {
if (!document) return;
Y.applyUpdate(document, value);
},
emitRealTimeUpdate: (message: TDocumentEventsServer) => provider?.sendStateless(message),
listenToRealTimeUpdate: () => provider && { on: provider.on.bind(provider), off: provider.off.bind(provider) },
}),
[editorRef, savedSelection]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const useReadOnlyCollaborativeEditor = (props: TReadOnlyCollaborativeEdit
const provider = useMemo(
() =>
new HocuspocusProvider({
url: realtimeConfig.url,
name: id,
url: realtimeConfig.url,
token: JSON.stringify(user),
parameters: realtimeConfig.queryParams,
onAuthenticationFailed: () => {
Expand All @@ -47,23 +47,23 @@ export const useReadOnlyCollaborativeEditor = (props: TReadOnlyCollaborativeEdit
},
onSynced: () => setHasServerSynced(true),
}),
[id, realtimeConfig, user]
[id, realtimeConfig, serverHandler, user.id]
Palanikannan1437 marked this conversation as resolved.
Show resolved Hide resolved
);

// indexed db integration for offline support
const localProvider = useMemo(
() => (id ? new IndexeddbPersistence(id, provider.document) : undefined),
[id, provider]
Palanikannan1437 marked this conversation as resolved.
Show resolved Hide resolved
);

// destroy and disconnect connection on unmount
useEffect(
() => () => {
provider.destroy();
provider.disconnect();
localProvider?.destroy();
},
[provider]
[provider, localProvider]
);
// indexed db integration for offline support
useLayoutEffect(() => {
const localProvider = new IndexeddbPersistence(id, provider.document);
return () => {
localProvider?.destroy();
};
}, [provider, id]);

const editor = useReadOnlyEditor({
editorProps,
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 && { on: provider.on.bind(provider), off: provider.off.bind(provider) },
getHeadings: () => editorRef?.current?.storage.headingList.headings,
}));

Expand Down
10 changes: 10 additions & 0 deletions packages/editor/src/core/types/document-collaborative-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DocumentCollaborativeEvents } from "@/constants/document-collaborative-events";

export type TDocumentEventKey = keyof typeof DocumentCollaborativeEvents;
export type TDocumentEventsClient = (typeof DocumentCollaborativeEvents)[TDocumentEventKey]["client"];
export type TDocumentEventsServer = (typeof DocumentCollaborativeEvents)[TDocumentEventKey]["server"];

export type TDocumentEventEmitter = {
on: (event: string, callback: (message: { payload: TDocumentEventsClient }) => void) => void;
off: (event: string, callback: (message: { payload: TDocumentEventsClient }) => void) => void;
};
Palanikannan1437 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion packages/editor/src/core/types/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
IMentionSuggestion,
TAIHandler,
TDisplayConfig,
TDocumentEventEmitter,
TDocumentEventsServer,
TEmbedConfig,
TExtensions,
TFileHandler,
Expand Down Expand Up @@ -83,6 +85,8 @@ export type EditorReadOnlyRefApi = {
};
onHeadingChange: (callback: (headings: IMarking[]) => void) => () => void;
getHeadings: () => IMarking[];
emitRealTimeUpdate: (action: TDocumentEventsServer) => void;
listenToRealTimeUpdate: () => TDocumentEventEmitter | undefined;
};

export interface EditorRefApi extends EditorReadOnlyRefApi {
Expand Down Expand Up @@ -121,7 +125,7 @@ export interface IEditorProps {
onEnterKeyPress?: (e?: any) => void;
placeholder?: string | ((isFocused: boolean, value: string) => string);
tabIndex?: number;
value?: string | null;
value?: string | null;
}
export interface ILiteTextEditor extends IEditorProps {
extensions?: any[];
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-collaborative-events";
3 changes: 3 additions & 0 deletions packages/editor/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "@/extensions/core-without-props";
export * from "@/constants/document-collaborative-events";
export * from "@/helpers/get-document-server-event";
export * from "@/types/document-collaborative-events";
1 change: 1 addition & 0 deletions 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";
Palanikannan1437 marked this conversation as resolved.
Show resolved Hide resolved
export * from "./cycle";
export * from "./module";
export * from "./state";
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const PageDetailsPage = observer(() => {
? () => getPageById(workspaceSlug?.toString(), projectId?.toString(), pageId.toString())
: null,
{
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: true,
revalidateOnFocus: true,
revalidateOnReconnect: true,
}
);

Expand Down
2 changes: 1 addition & 1 deletion web/core/components/pages/editor/editor-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {
onConnect: handleServerConnect,
onServerError: handleServerError,
}),
[]
[handleServerConnect, handleServerError]
);

const realtimeConfig: TRealtimeConfig | undefined = useMemo(() => {
Expand Down
Loading
Loading