Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Fix remote changes updates in document editor

Hakan Shehu9f9de1a1092c
diff --git a/desktop/src/components/documents/document-editor.tsx b/desktop/src/components/documents/document-editor.tsx
index 0d970dc4..ed22c3da 100644
--- a/desktop/src/components/documents/document-editor.tsx
+++ b/desktop/src/components/documents/document-editor.tsx
@@ -49,7 +49,7 @@ import {
import { EditorBubbleMenu } from '@/editor/menu/bubble-menu';
import { Node } from '@/types/nodes';
import {
- editorNodeArrayEquals,
+ editorNodeMapEquals,
mapEditorNodesToJSONContent,
mapJSONContentToEditorNodes,
mapNodesToEditorNodes,
@@ -72,10 +72,9 @@ interface DocumentEditorProps {
export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
const workspace = useWorkspace();
- const databaseNodes = React.useRef<EditorNode[]>(
+ const nodesSnapshot = React.useRef<Map<string, EditorNode>>(
mapNodesToEditorNodes(nodes),
);
- const localNodes = React.useRef<EditorNode[]>(databaseNodes.current);
const editor = useEditor(
{
@@ -134,7 +133,7 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
spellCheck: 'false',
},
},
- content: mapEditorNodesToJSONContent(node.id, localNodes.current),
+ content: mapEditorNodesToJSONContent(node.id, nodesSnapshot.current),
shouldRerenderOnTransaction: false,
autofocus: 'start',
onUpdate: async ({ editor, transaction }) => {
@@ -147,8 +146,7 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
);
React.useEffect(() => {
- console.log('nodes changed', nodes);
- checkForDatabaseChanges(nodes, editor);
+ checkForRemoteChanges(nodes, editor);
}, [editor, nodes]);
const checkForLocalChanges = React.useCallback(
@@ -159,12 +157,7 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
editorContent,
);
- if (editorNodeArrayEquals(newEditorNodes, localNodes.current)) {
- return;
- }
-
- localNodes.current = newEditorNodes;
- if (editorNodeArrayEquals(newEditorNodes, databaseNodes.current)) {
+ if (editorNodeMapEquals(newEditorNodes, nodesSnapshot.current)) {
return;
}
@@ -182,11 +175,8 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
},
};
- for (const newEditorNode of newEditorNodes) {
- const existingEditorNode = databaseNodes.current.find(
- (n) => n.id === newEditorNode.id,
- );
-
+ for (const newEditorNode of newEditorNodes.values()) {
+ const existingEditorNode = nodesSnapshot.current.get(newEditorNode.id);
if (!existingEditorNode) {
createNodesMutation.data.nodes.push({
id: newEditorNode.id,
@@ -218,8 +208,8 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
}
}
- for (const existingEditorNode of databaseNodes.current) {
- if (!newEditorNodes.find((n) => n.id === existingEditorNode.id)) {
+ for (const existingEditorNode of nodesSnapshot.current.values()) {
+ if (!newEditorNodes.has(existingEditorNode.id)) {
deleteNodesMutation.data.ids.push(existingEditorNode.id);
}
}
@@ -237,30 +227,79 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
if (deleteNodesMutation.data.ids.length > 0) {
await workspace.mutate(deleteNodesMutation);
}
- }, 500),
+
+ nodesSnapshot.current = newEditorNodes;
+ }, 100),
[node.id],
);
- const checkForDatabaseChanges = React.useCallback(
- debounce((newDatabaseNodes: Node[], editor: Editor) => {
- const newEditorNodes = mapNodesToEditorNodes(newDatabaseNodes);
- if (editorNodeArrayEquals(newEditorNodes, databaseNodes.current)) {
+ const checkForRemoteChanges = React.useCallback(
+ debounce((remoteNodes: Node[], editor: Editor) => {
+ const remoteEditorNodes = mapNodesToEditorNodes(remoteNodes);
+ if (editorNodeMapEquals(remoteEditorNodes, nodesSnapshot.current)) {
return;
}
- databaseNodes.current = newEditorNodes;
- if (editorNodeArrayEquals(newEditorNodes, localNodes.current)) {
+ const currentEditorContent = editor.getJSON();
+ const currentEditorNodes = mapJSONContentToEditorNodes(
+ node.id,
+ currentEditorContent,
+ );
+
+ const editorNodeIds = new Set<string>([
+ ...currentEditorNodes.keys(),
+ ...remoteEditorNodes.keys(),
+ ]);
+
+ const newEditorNodes: Map<string, EditorNode> = new Map();
+ let hasChanges = false;
+ for (const editorNodeId of editorNodeIds) {
+ const currentEditorNode = currentEditorNodes.get(editorNodeId);
+ const snapshotEditorNode = nodesSnapshot.current.get(editorNodeId);
+ const remoteEditorNode = remoteEditorNodes.get(editorNodeId);
+
+ if (!currentEditorNode) {
+ newEditorNodes.set(editorNodeId, remoteEditorNode);
+ hasChanges = true;
+ continue;
+ }
+
+ if (!snapshotEditorNode) {
+ newEditorNodes.set(editorNodeId, currentEditorNode);
+ continue;
+ }
+
+ if (!remoteEditorNode) {
+ newEditorNodes.set(editorNodeId, currentEditorNode);
+ continue;
+ }
+
+ if (!isEqual(currentEditorNode, snapshotEditorNode)) {
+ newEditorNodes.set(editorNodeId, currentEditorNode);
+ continue;
+ }
+
+ if (!isEqual(snapshotEditorNode, remoteEditorNode)) {
+ newEditorNodes.set(editorNodeId, remoteEditorNode);
+ hasChanges = true;
+ continue;
+ }
+
+ newEditorNodes.set(editorNodeId, currentEditorNode);
+ }
+
+ if (!hasChanges) {
return;
}
- //here we need to update the editor content
const newEditorContent = mapEditorNodesToJSONContent(
node.id,
newEditorNodes,
);
+
editor.commands.setContent(newEditorContent);
- localNodes.current = newEditorNodes;
- }, 500),
+ nodesSnapshot.current = newEditorNodes;
+ }, 100),
[node.id],
);
diff --git a/desktop/src/editor/utils.ts b/desktop/src/editor/utils.ts
index 06888939..7790f0ff 100644
--- a/desktop/src/editor/utils.ts
+++ b/desktop/src/editor/utils.ts
@@ -6,8 +6,16 @@ import { LeafNodeTypes } from '@/lib/constants';
import { isEqual } from 'lodash';
import { generateNodeIndex } from '@/lib/nodes';
-export const mapNodesToEditorNodes = (nodes: Node[]): EditorNode[] => {
- return nodes.map((node) => mapNodeToEditorNode(node));
+export const mapNodesToEditorNodes = (
+ nodes: Node[],
+): Map<string, EditorNode> => {
+ const editorNodes = new Map<string, EditorNode>();
+ for (const node of nodes) {
+ const editorNode = mapNodeToEditorNode(node);
+ editorNodes.set(editorNode.id, editorNode);
+ }
+
+ return editorNodes;
};
export const mapNodeToEditorNode = (node: Node): EditorNode => {
@@ -23,8 +31,9 @@ export const mapNodeToEditorNode = (node: Node): EditorNode => {
export const mapEditorNodesToJSONContent = (
rootId: string,
- editorNodes: EditorNode[],
+ editorNodesMap: Map<string, EditorNode>,
): JSONContent => {
+ const editorNodes = Array.from(editorNodesMap.values());
const contents: JSONContent[] = [];
const childrenNodes = editorNodes
.filter((node) => node.parentId === rootId)
@@ -69,7 +78,7 @@ const mapEditorNodeToJSONContent = (
};
const childrenNodes = nodes
- .filter((node) => node.parentId === node.id)
+ .filter((n) => n.parentId === node.id)
.sort((a, b) => {
if (a.index < b.index) {
return -1;
@@ -85,9 +94,7 @@ const mapEditorNodeToJSONContent = (
childrenNodes.forEach((child) => {
nodeJSONContent.content.push(mapEditorNodeToJSONContent(child, nodes));
});
- }
-
- if (node.content && node.content.length > 0) {
+ } else if (node.content && node.content.length > 0) {
nodeJSONContent.content = nodeJSONContent.content || [];
node.content.forEach((child) => {
const childContent: JSONContent = {
@@ -114,8 +121,8 @@ const mapEditorNodeToJSONContent = (
export const mapJSONContentToEditorNodes = (
rootId: string,
content: JSONContent,
-): EditorNode[] => {
- const nodes: EditorNode[] = [];
+): Map<string, EditorNode> => {
+ const nodes: Map<string, EditorNode> = new Map();
if (content.content && content.content.length > 0) {
for (const child of content.content) {
mapAndPushJSONContentToEditorNode(rootId, child, nodes);
@@ -128,7 +135,7 @@ export const mapJSONContentToEditorNodes = (
const mapAndPushJSONContentToEditorNode = (
parentId: string,
content: JSONContent,
- editorNodes: EditorNode[],
+ editorNodes: Map<string, EditorNode>,
) => {
let id = content.attrs?.id;
if (!id) {
@@ -153,7 +160,11 @@ const mapAndPushJSONContentToEditorNode = (
index: index,
attrs: attrs,
};
- editorNodes.push(editorNode);
+ editorNodes.set(id, editorNode);
+
+ if (!content.content) {
+ return;
+ }
if (LeafNodeTypes.includes(content.type)) {
editorNode.content = [];
@@ -185,10 +196,12 @@ const mapAndPushJSONContentToEditorNode = (
}
};
-const validateEditorNodeIndexes = (editorNodes: EditorNode[]): void => {
+const validateEditorNodeIndexes = (
+ editorNodes: Map<string, EditorNode>,
+): void => {
//group by parentId
const groupedNodes: { [key: string]: EditorNode[] } = {};
- for (const node of editorNodes) {
+ for (const node of editorNodes.values()) {
if (!groupedNodes[node.parentId]) {
groupedNodes[node.parentId] = [];
}
@@ -243,7 +256,7 @@ export const editorNodeArrayEquals = (
}
for (let i = 0; i < a.length; i++) {
- if (!deepEquals(a[i], b[i])) {
+ if (!isEqual(a[i], b[i])) {
return false;
}
}
@@ -251,14 +264,20 @@ export const editorNodeArrayEquals = (
return true;
};
-const deepEquals = (a: any, b: any): boolean => {
- if (a === b) {
+export const editorNodeMapEquals = (
+ a: Map<string, EditorNode>,
+ b: Map<string, EditorNode>,
+): boolean => {
+ if (a.size !== b.size) {
return false;
}
- if ((a === null || a === undefined) && (b === null || b === undefined)) {
- return false;
+ for (const [key, value] of a) {
+ const otherValue = b.get(key);
+ if (!otherValue || !isEqual(value, otherValue)) {
+ return false;
+ }
}
- return !isEqual(a, b);
+ return true;
};