Skip to content

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

Refactor node to editor json content mapping

Hakan Shehu04b0fa0c1729
diff --git a/desktop/src/components/messages/message-create.tsx b/desktop/src/components/messages/message-create.tsx
index ba5e8ea3..f1d528e9 100644
--- a/desktop/src/components/messages/message-create.tsx
+++ b/desktop/src/components/messages/message-create.tsx
@@ -6,7 +6,7 @@ import { JSONContent } from '@tiptap/core';
import { Node } from '@/types/nodes';
import { NeuronId } from '@/lib/id';
import { NodeTypes } from '@/lib/constants';
-import { buildChildNodes } from '@/lib/nodes';
+import { buildNodes } from '@/editor/utils';
interface MessageCreateProps {
nodeId: string;
@@ -29,7 +29,7 @@ export const MessageCreate = observer(({ nodeId }: MessageCreateProps) => {
versionId: NeuronId.generate(NeuronId.Type.Version),
};
- const childNodes = buildChildNodes(message, content);
+ const childNodes = buildNodes(workspace, message, content.content, []);
const newNodes = [message, ...childNodes];
await workspace.addNodes(newNodes);
diff --git a/desktop/src/components/pages/page-editor.tsx b/desktop/src/components/pages/page-editor.tsx
index a088f9e6..3d1f5a92 100644
--- a/desktop/src/components/pages/page-editor.tsx
+++ b/desktop/src/components/pages/page-editor.tsx
@@ -1,8 +1,8 @@
import React from 'react';
import type { JSONContent } from '@tiptap/core';
-import '@/styles/editor.css'
+import '@/styles/editor.css';
import { useEditor, EditorContent } from '@tiptap/react';
-import {Node} from '@/types/nodes'
+import { Node } from '@/types/nodes';
import {
ParagraphCommand,
BlockquoteCommand,
@@ -47,15 +47,13 @@ import {
} from '@/editor/extensions';
import { EditorBubbleMenu } from '@/editor/menu/bubble-menu';
-import { EditorCommandContext } from '@/types/editor';
-import {observer} from "mobx-react-lite";
+import { observer } from 'mobx-react-lite';
interface PageEditorProps {
node: Node;
}
export const PageEditor = observer(({ node }: PageEditorProps) => {
-
const editor = useEditor(
{
extensions: [
@@ -92,7 +90,7 @@ export const PageEditor = observer(({ node }: PageEditorProps) => {
CodeBlockCommand,
OrderedListCommand,
DividerCommand,
- TodoCommand
+ TodoCommand,
],
context: {},
}),
@@ -112,10 +110,10 @@ export const PageEditor = observer(({ node }: PageEditorProps) => {
},
},
shouldRerenderOnTransaction: false,
- onUpdate: (e) => {
- console.log(e.editor.getJSON());
+ onUpdate: ({ editor }) => {
+ console.log(editor.getJSON());
},
- autofocus: 'start'
+ autofocus: 'start',
},
[node.id],
);
@@ -130,4 +128,4 @@ export const PageEditor = observer(({ node }: PageEditorProps) => {
<EditorContent editor={editor} />
</div>
);
-});
\ No newline at end of file
+});
diff --git a/desktop/src/editor/utils.ts b/desktop/src/editor/utils.ts
new file mode 100644
index 00000000..9cc9beeb
--- /dev/null
+++ b/desktop/src/editor/utils.ts
@@ -0,0 +1,158 @@
+import { Node, NodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
+import { NeuronId } from '@/lib/id';
+import { generateKeyBetween } from 'fractional-indexing-jittered';
+import { Workspace } from '@/types/workspaces';
+
+export const mapToEditorContent = (
+ parent: Node,
+ nodes: Node[],
+): JSONContent => {
+ const parentContent: JSONContent = {
+ type: parent.type,
+ attrs: {
+ id: parent.id,
+ index: parent.index,
+ },
+ };
+
+ const childrenNodes = nodes
+ .filter((node) => node.parentId === parent.id)
+ .sort((a, b) => {
+ if (a.index < b.index) {
+ return -1;
+ } else if (a.index > b.index) {
+ return 1;
+ }
+
+ return 0;
+ });
+
+ if (childrenNodes.length > 0) {
+ parentContent.content = parentContent.content || [];
+ childrenNodes.forEach((child) => {
+ parentContent.content.push(mapToEditorContent(child, nodes));
+ });
+ }
+
+ if (parent.content && parent.content.length > 0) {
+ parentContent.content = parentContent.content || [];
+ parent.content.forEach((child) => {
+ parentContent.content.push({
+ type: child.type,
+ marks: child.marks,
+ text: child.text,
+ });
+ });
+ }
+
+ return parentContent;
+};
+
+export const buildNodes = (
+ workspace: Workspace,
+ parent: Node,
+ content: JSONContent[],
+ existingNodes: Node[],
+): Node[] => {
+ const nodes: Node[] = [];
+ content.forEach((c) => {
+ const nodeWithChildren = buildNodeWithChildren(
+ workspace,
+ parent,
+ c,
+ existingNodes,
+ );
+ enumerateNodes(nodeWithChildren, nodes);
+ });
+
+ return nodes;
+};
+
+const buildNodeWithChildren = (
+ workspace: Workspace,
+ parent: Node,
+ content: JSONContent,
+ existingNodes: Node[],
+): NodeWithChildren => {
+ const id =
+ content.attrs?.id ??
+ NeuronId.generate(NeuronId.getIdTypeFromNode(content.type));
+ let node = existingNodes.find((n) => n.id === id);
+ if (!node) {
+ node = {
+ id: id,
+ workspaceId: workspace.id,
+ parentId: parent.id,
+ type: content.type,
+ index: content.attrs?.index,
+ content: [],
+ attrs: {
+ ...content.attrs,
+ id: undefined,
+ },
+ createdAt: new Date(),
+ createdBy: parent.createdBy,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ };
+ } else {
+ node.attrs = {
+ ...content.attrs,
+ id: undefined,
+ };
+ }
+
+ const children: NodeWithChildren[] = [];
+ if (isLeafNode(content.type) && content.content) {
+ node.content = content.content.map((child) => {
+ return {
+ type: child.type,
+ text: child.text,
+ marks: child.marks?.map((mark) => {
+ return {
+ type: mark.type,
+ attrs: mark.attrs,
+ };
+ }),
+ };
+ });
+ } else if (content.content && content.content.length > 0) {
+ for (let i = 0; i < content.content.length; i++) {
+ const childNode = buildNodeWithChildren(
+ workspace,
+ node,
+ content.content[i],
+ existingNodes,
+ );
+
+ const beforeIndex = i === 0 ? null : content.content[i - 1].index;
+ const afterIndex =
+ i === content.content.length - 1 ? null : content.content[i + 1].index;
+ const currentIndex = childNode.index;
+
+ if (
+ currentIndex == null ||
+ currentIndex < beforeIndex ||
+ currentIndex > afterIndex
+ ) {
+ childNode.index = generateKeyBetween(beforeIndex, afterIndex);
+ }
+ }
+ }
+
+ return {
+ ...node,
+ children,
+ };
+};
+
+const enumerateNodes = (nodeWithChildren: NodeWithChildren, nodes: Node[]) => {
+ nodes.push(nodeWithChildren);
+ nodeWithChildren.children.forEach((child) => {
+ enumerateNodes(child, nodes);
+ });
+};
+
+const isLeafNode = (type: string) => {
+ return type === 'paragraph' || type === 'heading' || type === 'blockquote';
+};
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index 53f4d088..ea4612bf 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -1,7 +1,4 @@
-import { Node, NodeWithChildren, NodeBlock } from '@/types/nodes';
-import { JSONContent } from '@tiptap/core';
-import { NeuronId } from '@/lib/id';
-import { generateKeyBetween } from 'fractional-indexing-jittered';
+import { Node, NodeWithChildren } from '@/types/nodes';
export const buildNodeWithChildren = (
node: Node,
@@ -16,77 +13,3 @@ export const buildNodeWithChildren = (
children: children,
};
};
-
-export const buildChildNodes = (parent: Node, content: JSONContent): Node[] => {
- const nodes: Node[] = [];
-
- if (content.content && content.content.length > 0) {
- let index: string | null = null;
- for (const child of content.content) {
- index = generateKeyBetween(index, null);
- buildChildNode(parent, child, nodes, index);
- }
- }
-
- return nodes;
-};
-
-const buildChildNode = (
- parent: Node,
- content: JSONContent,
- nodes: Node[],
- index?: string | null,
-): void => {
- let id = content.attrs.id;
- if (id) {
- delete content.attrs.id;
- } else {
- id = NeuronId.generate(NeuronId.getIdTypeFromNode(content.type));
- }
-
- const node: Node = {
- id: id,
- workspaceId: parent.workspaceId,
- parentId: parent.id,
- type: content.type,
- index: index,
- content: [],
- attrs: content.attrs,
- createdAt: new Date(),
- createdBy: parent.createdBy,
- versionId: NeuronId.generate(NeuronId.Type.Version),
- };
-
- nodes.push(node);
-
- if (content.content && content.content.length > 0) {
- let index = null;
- for (const child of content.content) {
- if (isNodeBlock(child)) {
- const nodeBlock: NodeBlock = {
- type: child.type,
- text: child.text,
- marks: [],
- };
-
- if (child.marks) {
- child.marks.forEach((mark) => {
- nodeBlock.marks.push({
- type: mark.type,
- attrs: mark.attrs,
- });
- });
- }
-
- node.content.push(nodeBlock);
- } else {
- index = generateKeyBetween(index, null);
- buildChildNode(node, child, nodes, index);
- }
- }
- }
-};
-
-const isNodeBlock = (content: JSONContent): boolean => {
- return content.type === 'text';
-};