Skip to content

Commit

Permalink
fix: added strong types
Browse files Browse the repository at this point in the history
  • Loading branch information
Palanikannan1437 committed Sep 23, 2024
1 parent 8ce39f5 commit c94fff9
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
20 changes: 8 additions & 12 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 {
DocumentEventsServer,
documentEventResponses,
} from "@plane/editor/lib";

export const getHocusPocusServer = async () => {
const extensions = await getExtensions();
Expand Down Expand Up @@ -38,20 +42,12 @@ export const getHocusPocusServer = async () => {
}
},
async onStateless({ payload, document }) {
if (payload === "lock") {
document.broadcastStateless("locked");
}
if (payload === "unlock") {
document.broadcastStateless("unlocked");
}
if (payload === "archive") {
document.broadcastStateless("archived");
}
if (payload === "unarchive") {
document.broadcastStateless("unarchived");
const response = documentEventResponses[payload as DocumentEventsServer];
if (response) {
document.broadcastStateless(response);
}
},
extensions,
debounce: 10000
debounce: 10000,
});
};
9 changes: 9 additions & 0 deletions packages/editor/src/core/helpers/document-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const documentEventResponses = {
lock: "locked",
unlock: "unlocked",
archive: "archived",
unarchive: "unarchived",
} as const;

export type DocumentEventsServer = keyof typeof documentEventResponses;
export type DocumentEventsClient = (typeof documentEventResponses)[DocumentEventsServer];
3 changes: 0 additions & 3 deletions packages/editor/src/core/hooks/use-collaborative-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
user,
} = props;
// initialize Hocuspocus provider
console.log("afdafd");
const provider = useMemo(
() =>
new HocuspocusProvider({
Expand All @@ -45,7 +44,6 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
if (data.event.code === 1006) serverHandler?.onServerError?.();
},
onSynced: () => {
console.log("ran");
serverHandler?.onSynced?.();
},
}),
Expand Down Expand Up @@ -96,7 +94,6 @@ export const useCollaborativeEditor = (props: TCollaborativeEditorProps) => {
placeholder,
provider,
tabIndex,
provider,
});

return { editor };
Expand Down
8 changes: 3 additions & 5 deletions packages/editor/src/core/hooks/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IMarking, scrollSummary } from "@/helpers/scroll-to-node";
import { CoreEditorProps } from "@/props";
// types
import { EditorRefApi, IMentionHighlight, IMentionSuggestion, TEditorCommands, TFileHandler } from "@/types";
import { HocuspocusProvider } from "@hocuspocus/provider";
import { DocumentEventsServer } from "src/lib";

export interface CustomEditorProps {
editorClassName: string;
Expand All @@ -35,7 +35,6 @@ export interface CustomEditorProps {
};
onChange?: (json: object, html: string) => void;
placeholder?: string | ((isFocused: boolean, value: string) => string);
provider?: HocuspocusProvider;
tabIndex?: number;
// undefined when prop is not passed, null if intentionally passed to stop
// swr syncing
Expand All @@ -57,7 +56,6 @@ export const useEditor = (props: CustomEditorProps) => {
mentionHandler,
onChange,
placeholder,
provider,
tabIndex,
value,
provider,
Expand Down Expand Up @@ -233,7 +231,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 @@ -274,7 +272,7 @@ export const useEditor = (props: CustomEditorProps) => {
if (!document) return;
Y.applyUpdate(document, value);
},
emitRealTimeUpdate: (message: string) => provider?.sendStateless(message),
emitRealTimeUpdate: (message: DocumentEventsServer) => provider?.sendStateless(message),
listenToRealTimeUpdate: () => provider,
}),
[editorRef, savedSelection]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const useReadOnlyCollaborativeEditor = (props: TReadOnlyCollaborativeEdit
user,
} = props;
// initialize Hocuspocus provider
console.log("afdafd read only");
const provider = useMemo(
() =>
new HocuspocusProvider({
Expand All @@ -34,7 +33,6 @@ export const useReadOnlyCollaborativeEditor = (props: TReadOnlyCollaborativeEdit
if (data.event.code === 1006) serverHandler?.onServerError?.();
},
onSynced: () => {
console.log("ran");
serverHandler?.onSynced?.();
},
}),
Expand Down
1 change: 0 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 @@ -12,7 +12,6 @@ import { IMarking, scrollSummary } from "@/helpers/scroll-to-node";
import { CoreReadOnlyEditorProps } from "@/props";
// types
import { EditorReadOnlyRefApi, IMentionHighlight } from "@/types";
import { HocuspocusProvider } from "@hocuspocus/provider";

interface CustomReadOnlyEditorProps {
initialValue?: string;
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/core/types/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
TFileHandler,
TServerHandler,
} from "@/types";
import { DocumentEventsServer } from "src/lib";
import { HocuspocusProvider } from "@hocuspocus/provider";

// editor refs
export type EditorReadOnlyRefApi = {
Expand All @@ -30,8 +32,8 @@ export type EditorReadOnlyRefApi = {
paragraphs: number;
words: number;
};
emitRealTimeUpdate: (message: string) => void;
listenToRealTimeUpdate: () => any;
emitRealTimeUpdate: (message: DocumentEventsServer) => void;
listenToRealTimeUpdate: () => HocuspocusProvider;
};

export interface EditorRefApi extends EditorReadOnlyRefApi {
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "@/extensions/core-without-props";
export * from "@/helpers/document-events";

0 comments on commit c94fff9

Please sign in to comment.