Skip to content

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

Update node schema

Hakan Shehu01f5b7bc495c
diff --git a/desktop/package-lock.json b/desktop/package-lock.json
index a0eb5ff4..7abf5c60 100644
--- a/desktop/package-lock.json
+++ b/desktop/package-lock.json
@@ -57,6 +57,7 @@
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"electron-squirrel-startup": "^1.0.1",
+ "fractional-indexing-jittered": "^0.9.1",
"is-hotkey": "^0.2.0",
"kysely": "^0.27.4",
"lowlight": "^3.1.0",
@@ -7422,6 +7423,12 @@
"url": "https://github.com/sponsors/rawify"
}
},
+ "node_modules/fractional-indexing-jittered": {
+ "version": "0.9.1",
+ "resolved": "https://registry.npmjs.org/fractional-indexing-jittered/-/fractional-indexing-jittered-0.9.1.tgz",
+ "integrity": "sha512-qyzDZ7JXWf/yZT2rQDpQwFBbIaZS2o+zb0s740vqreXQ6bFQPd8tAy4D1gGN0CUeIcnNHjuvb0EaLnqHhGV/PA==",
+ "license": "CC0-1.0"
+ },
"node_modules/fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
diff --git a/desktop/package.json b/desktop/package.json
index 443a55f2..97696b8e 100644
--- a/desktop/package.json
+++ b/desktop/package.json
@@ -95,6 +95,7 @@
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"electron-squirrel-startup": "^1.0.1",
+ "fractional-indexing-jittered": "^0.9.1",
"is-hotkey": "^0.2.0",
"kysely": "^0.27.4",
"lowlight": "^3.1.0",
diff --git a/desktop/src/components/messages/message.tsx b/desktop/src/components/messages/message.tsx
index 3d9cb3a5..4eecdf02 100644
--- a/desktop/src/components/messages/message.tsx
+++ b/desktop/src/components/messages/message.tsx
@@ -15,7 +15,7 @@ import { MessageDeleteButton } from '@/components/messages/message-delete-button
import { MessageReference } from '@/components/messages/message-reference';
import { MessageReactions } from '@/components/messages/message-reactions';
import { useWorkspace } from '@/contexts/workspace';
-import { buildNodeTree } from '@/lib/nodes';
+import { buildNodeWithChildren } from '@/lib/nodes';
import { NodeRenderer } from '@/editor/renderers/node';
interface MessageProps {
@@ -44,7 +44,7 @@ export const Message = observer(
const canEdit = true;
const canDelete = true;
const canReplyInThread = true;
- const nodeTree = buildNodeTree(message, nodes);
+ const nodeWithChildren = buildNodeWithChildren(message, nodes);
return (
<div
@@ -122,7 +122,7 @@ export const Message = observer(
</ul>
{/*{data.messageReference && <MessageReference />}*/}
<div className="text-foreground">
- <NodeRenderer node={nodeTree} keyPrefix={message.id} />
+ <NodeRenderer node={nodeWithChildren} keyPrefix={message.id} />
</div>
{/*<MessageReactions*/}
{/* message={message}*/}
diff --git a/desktop/src/components/spaces/space-create-dialog.tsx b/desktop/src/components/spaces/space-create-dialog.tsx
index 4b8a7646..55a7ec49 100644
--- a/desktop/src/components/spaces/space-create-dialog.tsx
+++ b/desktop/src/components/spaces/space-create-dialog.tsx
@@ -25,6 +25,7 @@ import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { Node } from '@/types/nodes';
import { NeuronId } from '@/lib/id';
+import { generateKeyBetween } from 'fractional-indexing-jittered';
const formSchema = z.object({
name: z.string().min(3, 'Name must be at least 3 characters long.'),
@@ -58,15 +59,28 @@ export const SpaceCreateDialog = ({
const handleSubmit = async (values: z.infer<typeof formSchema>) => {
setIsPending(true);
- const spaceId = NeuronId.generate(NeuronId.Type.Space);
+ const spaceNode: Node = {
+ id: NeuronId.generate(NeuronId.Type.Space),
+ type: 'space',
+ parentId: null,
+ attrs: {
+ name: values.name,
+ description: values.description,
+ },
+ workspaceId: workspace.id,
+ createdAt: new Date(),
+ createdBy: workspace.userNodeId,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ };
const pageNode: Node = {
id: NeuronId.generate(NeuronId.Type.Page),
type: 'page',
attrs: {
name: 'Home',
},
+ index: generateKeyBetween(null, null),
workspaceId: workspace.id,
- parentId: spaceId,
+ parentId: spaceNode.id,
createdAt: new Date(),
createdBy: workspace.userNodeId,
versionId: NeuronId.generate(NeuronId.Type.Version),
@@ -78,32 +92,9 @@ export const SpaceCreateDialog = ({
attrs: {
name: 'Discussions',
},
+ index: generateKeyBetween(pageNode.index, null),
workspaceId: workspace.id,
- parentId: spaceId,
- createdAt: new Date(),
- createdBy: workspace.userNodeId,
- versionId: NeuronId.generate(NeuronId.Type.Version),
- };
-
- const spaceNode: Node = {
- id: spaceId,
- type: 'space',
- parentId: null,
- attrs: {
- name: values.name,
- description: values.description,
- },
- content: [
- {
- type: 'page',
- id: pageNode.id,
- },
- {
- type: 'channel',
- id: channelNode.id,
- },
- ],
- workspaceId: workspace.id,
+ parentId: spaceNode.id,
createdAt: new Date(),
createdBy: workspace.userNodeId,
versionId: NeuronId.generate(NeuronId.Type.Version),
diff --git a/desktop/src/components/workspaces/sidebar-node-children.tsx b/desktop/src/components/workspaces/sidebar-node-children.tsx
index 892964b2..b4188a8c 100644
--- a/desktop/src/components/workspaces/sidebar-node-children.tsx
+++ b/desktop/src/components/workspaces/sidebar-node-children.tsx
@@ -1,23 +1,35 @@
-import React from 'react'
-import {Node} from '@/types/nodes'
-import { observer } from 'mobx-react-lite'
+import React from 'react';
+import { Node } from '@/types/nodes';
+import { observer } from 'mobx-react-lite';
import { useWorkspace } from '@/contexts/workspace';
-import {SidebarNode} from "@/components/workspaces/sidebar-node";
+import { SidebarNode } from '@/components/workspaces/sidebar-node';
interface SidebarNodeChildrenProps {
node: Node;
}
-export const SidebarNodeChildren = observer(({node}: SidebarNodeChildrenProps) => {
- const workspace = useWorkspace();
- const childrenIds = node.content?.filter(c => !!c.id).map(c => c.id) ?? [];
- const children: Node[] = workspace.getNodes().filter(n => childrenIds.includes(n.id));
+export const SidebarNodeChildren = observer(
+ ({ node }: SidebarNodeChildrenProps) => {
+ const workspace = useWorkspace();
+ const children: Node[] = workspace
+ .getNodes()
+ .filter((n) => n.parentId === node.id)
+ .sort((a, b) => {
+ if (a.index < b.index) {
+ return -1;
+ } else if (a.index > b.index) {
+ return 1;
+ }
- return (
- <React.Fragment>
- {children.map((child) => (
- <SidebarNode key={child.id} node={child} />
- ))}
- </React.Fragment>
- )
-});
+ return 0;
+ });
+
+ return (
+ <React.Fragment>
+ {children.map((child) => (
+ <SidebarNode key={child.id} node={child} />
+ ))}
+ </React.Fragment>
+ );
+ },
+);
diff --git a/desktop/src/editor/renderers/blockquote.tsx b/desktop/src/editor/renderers/blockquote.tsx
index d651cb06..8f856003 100644
--- a/desktop/src/editor/renderers/blockquote.tsx
+++ b/desktop/src/editor/renderers/blockquote.tsx
@@ -1,17 +1,17 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
-import { NodeContentRenderer } from '@/editor/renderers/node-content';
-import { NodeTree } from '@/types/nodes';
+import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
+import { NodeWithChildren } from '@/types/nodes';
interface BlockquoteRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
const BlockquoteRenderer = ({ node, keyPrefix }: BlockquoteRendererProps) => {
return (
<blockquote className={defaultClasses.blockquote}>
- <NodeContentRenderer node={node} keyPrefix={keyPrefix} />
+ <NodeChildrenRenderer node={node} keyPrefix={keyPrefix} />
</blockquote>
);
};
diff --git a/desktop/src/editor/renderers/bullet-list.tsx b/desktop/src/editor/renderers/bullet-list.tsx
index f5f5ca70..826a3a2f 100644
--- a/desktop/src/editor/renderers/bullet-list.tsx
+++ b/desktop/src/editor/renderers/bullet-list.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface BulletListRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/code-block.tsx b/desktop/src/editor/renderers/code-block.tsx
index 46e46ff1..a400ca67 100644
--- a/desktop/src/editor/renderers/code-block.tsx
+++ b/desktop/src/editor/renderers/code-block.tsx
@@ -2,11 +2,11 @@ import React from 'react';
import { Icon } from '@/components/ui/icon';
import { defaultClasses } from '@/editor/classes';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
import { highlightCode, languages } from '@/lib/lowlight';
interface CodeBlockRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/document.tsx b/desktop/src/editor/renderers/document.tsx
index 7fff1737..30dcd477 100644
--- a/desktop/src/editor/renderers/document.tsx
+++ b/desktop/src/editor/renderers/document.tsx
@@ -1,9 +1,9 @@
import React from 'react';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface DocumentRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/heading.tsx b/desktop/src/editor/renderers/heading.tsx
index 2b201d24..6fda8cf0 100644
--- a/desktop/src/editor/renderers/heading.tsx
+++ b/desktop/src/editor/renderers/heading.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface HeadingRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/list-item.tsx b/desktop/src/editor/renderers/list-item.tsx
index bbae6854..e14f21e8 100644
--- a/desktop/src/editor/renderers/list-item.tsx
+++ b/desktop/src/editor/renderers/list-item.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface ListItemRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/mark.tsx b/desktop/src/editor/renderers/mark.tsx
index d6c5887e..0bbc7ced 100644
--- a/desktop/src/editor/renderers/mark.tsx
+++ b/desktop/src/editor/renderers/mark.tsx
@@ -1,9 +1,9 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
-import { NodeContentTree } from '@/types/nodes';
+import { NodeBlock } from '@/types/nodes';
interface MarkRendererProps {
- node: NodeContentTree;
+ node: NodeBlock;
children: any | any[];
}
diff --git a/desktop/src/editor/renderers/message.tsx b/desktop/src/editor/renderers/message.tsx
index f6ff230d..9844e549 100644
--- a/desktop/src/editor/renderers/message.tsx
+++ b/desktop/src/editor/renderers/message.tsx
@@ -1,9 +1,9 @@
import React from 'react';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface MessageRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/node-block.tsx b/desktop/src/editor/renderers/node-block.tsx
new file mode 100644
index 00000000..e11f7c45
--- /dev/null
+++ b/desktop/src/editor/renderers/node-block.tsx
@@ -0,0 +1,17 @@
+import React from 'react';
+import { NodeBlock } from '@/types/nodes';
+import { TextRenderer } from '@/editor/renderers/text';
+import { MarkRenderer } from '@/editor/renderers/mark';
+
+interface NodeBlockRendererProps {
+ node: NodeBlock;
+ keyPrefix: string | null;
+}
+
+export const NodeBlockRenderer = ({ node }: NodeBlockRendererProps) => {
+ return (
+ <MarkRenderer node={node}>
+ <TextRenderer node={node} />
+ </MarkRenderer>
+ );
+};
diff --git a/desktop/src/editor/renderers/node-children.tsx b/desktop/src/editor/renderers/node-children.tsx
index 127f8a41..87c56be8 100644
--- a/desktop/src/editor/renderers/node-children.tsx
+++ b/desktop/src/editor/renderers/node-children.tsx
@@ -1,10 +1,11 @@
import React from 'react';
-import { NodeTree } from '@/types/nodes';
-import { NodeContentRenderer } from '@/editor/renderers/node-content';
+import { NodeWithChildren } from '@/types/nodes';
+import { NodeBlockRenderer } from '@/editor/renderers/node-block';
+import { NodeRenderer } from '@/editor/renderers/node';
interface NodeChildrenRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
@@ -12,18 +13,20 @@ export const NodeChildrenRenderer = ({
node,
keyPrefix,
}: NodeChildrenRendererProps) => {
- if (!node.content) {
- return null;
+ if (node.children && node.children.length > 0) {
+ return (
+ <React.Fragment>
+ {node.children.map((nodeChild, index) => (
+ <NodeRenderer node={nodeChild} keyPrefix={keyPrefix} key={index} />
+ ))}
+ </React.Fragment>
+ );
}
return (
<React.Fragment>
- {node.content.map((innerNode, index) => (
- <NodeContentRenderer
- node={innerNode}
- keyPrefix={keyPrefix}
- key={index}
- />
+ {node.content.map((nodeBlock, index) => (
+ <NodeBlockRenderer node={nodeBlock} keyPrefix={keyPrefix} key={index} />
))}
</React.Fragment>
);
diff --git a/desktop/src/editor/renderers/node-content.tsx b/desktop/src/editor/renderers/node-content.tsx
deleted file mode 100644
index 180aa7f4..00000000
--- a/desktop/src/editor/renderers/node-content.tsx
+++ /dev/null
@@ -1,25 +0,0 @@
-import React from 'react';
-import { NodeContentTree } from '@/types/nodes';
-import { NodeRenderer } from '@/editor/renderers/node';
-import { TextRenderer } from '@/editor/renderers/text';
-import { MarkRenderer } from '@/editor/renderers/mark';
-
-interface NodeContentRendererProps {
- node: NodeContentTree;
- keyPrefix: string | null;
-}
-
-export const NodeContentRenderer = ({
- node,
- keyPrefix,
-}: NodeContentRendererProps) => {
- if (node.node) {
- return <NodeRenderer node={node.node} keyPrefix={keyPrefix} />;
- }
-
- return (
- <MarkRenderer node={node}>
- <TextRenderer node={node} />
- </MarkRenderer>
- );
-};
diff --git a/desktop/src/editor/renderers/node.tsx b/desktop/src/editor/renderers/node.tsx
index d5251fcf..bf495947 100644
--- a/desktop/src/editor/renderers/node.tsx
+++ b/desktop/src/editor/renderers/node.tsx
@@ -11,11 +11,11 @@ import { ParagraphRenderer } from '@/editor/renderers/paragraph';
import { TaskItemRenderer } from '@/editor/renderers/task-item';
import { TaskListRenderer } from '@/editor/renderers/task-list';
import { TextRenderer } from '@/editor/renderers/text';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
import { match } from 'ts-pattern';
interface NodeRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/ordered-list.tsx b/desktop/src/editor/renderers/ordered-list.tsx
index 68f89fe5..6af815e5 100644
--- a/desktop/src/editor/renderers/ordered-list.tsx
+++ b/desktop/src/editor/renderers/ordered-list.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface OrderedListRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/paragraph.tsx b/desktop/src/editor/renderers/paragraph.tsx
index 3c31f50f..0655eb8f 100644
--- a/desktop/src/editor/renderers/paragraph.tsx
+++ b/desktop/src/editor/renderers/paragraph.tsx
@@ -1,11 +1,11 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
import { cn } from '@/lib/utils';
interface ParagraphRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/task-item.tsx b/desktop/src/editor/renderers/task-item.tsx
index 73878c04..2f6532f0 100644
--- a/desktop/src/editor/renderers/task-item.tsx
+++ b/desktop/src/editor/renderers/task-item.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface TaskItemRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/task-list.tsx b/desktop/src/editor/renderers/task-list.tsx
index a6cfdd7a..aeba6d84 100644
--- a/desktop/src/editor/renderers/task-list.tsx
+++ b/desktop/src/editor/renderers/task-list.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/editor/classes';
import { NodeChildrenRenderer } from '@/editor/renderers/node-children';
-import { NodeTree } from '@/types/nodes';
+import { NodeWithChildren } from '@/types/nodes';
interface TaskListRendererProps {
- node: NodeTree;
+ node: NodeWithChildren;
keyPrefix: string | null;
}
diff --git a/desktop/src/editor/renderers/text.tsx b/desktop/src/editor/renderers/text.tsx
index 8ff9f80f..a48ef6e5 100644
--- a/desktop/src/editor/renderers/text.tsx
+++ b/desktop/src/editor/renderers/text.tsx
@@ -1,6 +1,6 @@
import React from 'react';
-import { NodeContentTree } from '@/types/nodes';
+import { NodeBlock } from '@/types/nodes';
-export const TextRenderer = ({ node }: { node: NodeContentTree }) => {
+export const TextRenderer = ({ node }: { node: NodeBlock }) => {
return <React.Fragment>{node.text ?? ''}</React.Fragment>;
};
diff --git a/desktop/src/electron/database/workspace/index.ts b/desktop/src/electron/database/workspace/index.ts
index d802838e..b3c8cc1a 100644
--- a/desktop/src/electron/database/workspace/index.ts
+++ b/desktop/src/electron/database/workspace/index.ts
@@ -44,6 +44,7 @@ export class WorkspaceDatabase {
return nodes.map((node) => ({
id: node.id,
type: node.type,
+ index: node.index,
parentId: node.parent_id,
workspaceId: node.workspace_id,
attrs: JSON.parse(node.attrs),
@@ -62,6 +63,7 @@ export class WorkspaceDatabase {
.values({
id: node.id,
type: node.type,
+ index: node.index,
parent_id: node.parentId,
workspace_id: this.workspaceId,
created_at: new Date(node.createdAt).toISOString(),
@@ -94,6 +96,7 @@ export class WorkspaceDatabase {
nodes.map((node) => ({
id: node.id,
type: node.type,
+ index: node.index,
parent_id: node.parentId,
workspace_id: this.workspaceId,
created_at: node.createdAt.toISOString(),
@@ -124,6 +127,7 @@ export class WorkspaceDatabase {
.updateTable('nodes')
.set({
type: node.type,
+ index: node.index,
parent_id: node.parentId,
updated_at: node.updatedAt.toISOString(),
updated_by: node.updatedBy,
@@ -175,6 +179,7 @@ export class WorkspaceDatabase {
.values({
id: node.id,
type: node.type,
+ index: node.index,
parent_id: node.parentId,
workspace_id: this.workspaceId,
created_at: new Date(node.createdAt).toISOString(),
@@ -196,6 +201,7 @@ export class WorkspaceDatabase {
.updateTable('nodes')
.set({
type: node.type,
+ index: node.index,
parent_id: node.parentId,
updated_at: node.updatedAt.toISOString(),
updated_by: node.updatedBy,
diff --git a/desktop/src/electron/database/workspace/migrations.ts b/desktop/src/electron/database/workspace/migrations.ts
index 035468a1..93f2e88e 100644
--- a/desktop/src/electron/database/workspace/migrations.ts
+++ b/desktop/src/electron/database/workspace/migrations.ts
@@ -10,6 +10,7 @@ const createNodesTable: Migration = {
col.references('nodes.id').onDelete('cascade'),
)
.addColumn('type', 'text', (col) => col.notNull())
+ .addColumn('index', 'text')
.addColumn('attrs', 'text')
.addColumn('content', 'text')
.addColumn('created_at', 'text', (col) => col.notNull())
@@ -17,6 +18,7 @@ const createNodesTable: Migration = {
.addColumn('created_by', 'text', (col) => col.notNull())
.addColumn('updated_by', 'text')
.addColumn('version_id', 'text', (col) => col.notNull())
+ .addColumn('state', 'text')
.execute();
},
down: async (db) => {
diff --git a/desktop/src/electron/database/workspace/schema.ts b/desktop/src/electron/database/workspace/schema.ts
index 1eee0f4c..c13033e2 100644
--- a/desktop/src/electron/database/workspace/schema.ts
+++ b/desktop/src/electron/database/workspace/schema.ts
@@ -3,6 +3,7 @@ export interface NodesTableSchema {
workspace_id: string;
parent_id: string | null;
type: string;
+ index: string | null;
attrs: string | null;
content: string | null;
created_at: string;
@@ -10,6 +11,7 @@ export interface NodesTableSchema {
created_by: string;
updated_by: string | null;
version_id: string;
+ state: string | null;
}
export interface WorkspaceDatabaseSchema {
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index 594cf843..53f4d088 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -1,60 +1,30 @@
-import { Node, NodeContent, NodeTree, NodeContentTree } from '@/types/nodes';
+import { Node, NodeWithChildren, NodeBlock } from '@/types/nodes';
import { JSONContent } from '@tiptap/core';
import { NeuronId } from '@/lib/id';
+import { generateKeyBetween } from 'fractional-indexing-jittered';
-export const buildNodeTree = (node: Node, allNodes: Node[]): NodeTree => {
- const content: NodeContentTree[] = [];
- if (node.content) {
- content.push(...buildNodeContentTreeRecursive(node.content, allNodes));
- }
+export const buildNodeWithChildren = (
+ node: Node,
+ allNodes: Node[],
+): NodeWithChildren => {
+ const children: NodeWithChildren[] = allNodes
+ .filter((n) => n.parentId === node.id)
+ .map((n) => buildNodeWithChildren(n, allNodes));
return {
- id: node.id,
- workspaceId: node.workspaceId,
- parentId: node.parentId,
- type: node.type,
- content: content,
- attrs: node.attrs,
- createdAt: node.createdAt,
- updatedAt: node.updatedAt,
- createdBy: node.createdBy,
- updatedBy: node.updatedBy,
- versionId: node.versionId,
+ ...node,
+ children: children,
};
};
-const buildNodeContentTreeRecursive = (
- content: NodeContent[],
- allNodes: Node[],
-): NodeContentTree[] => {
- const childrenContent: NodeContentTree[] = [];
- for (const child of content) {
- if (child.id) {
- const childNode = allNodes.find((n) => n.id === child.id);
- if (childNode) {
- childrenContent.push({
- type: child.type,
- node: buildNodeTree(childNode, allNodes),
- });
- }
- } else {
- childrenContent.push({
- type: child.type,
- text: child.text,
- marks: child.marks,
- });
- }
- }
-
- return childrenContent;
-};
-
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) {
- buildChildNode(parent, child, nodes);
+ index = generateKeyBetween(index, null);
+ buildChildNode(parent, child, nodes, index);
}
}
@@ -65,6 +35,7 @@ const buildChildNode = (
parent: Node,
content: JSONContent,
nodes: Node[],
+ index?: string | null,
): void => {
let id = content.attrs.id;
if (id) {
@@ -78,6 +49,7 @@ const buildChildNode = (
workspaceId: parent.workspaceId,
parentId: parent.id,
type: content.type,
+ index: index,
content: [],
attrs: content.attrs,
createdAt: new Date(),
@@ -85,21 +57,13 @@ const buildChildNode = (
versionId: NeuronId.generate(NeuronId.Type.Version),
};
- if (!parent.content) {
- parent.content = [];
- }
-
- parent.content.push({
- type: content.type,
- id: node.id,
- });
-
nodes.push(node);
if (content.content && content.content.length > 0) {
+ let index = null;
for (const child of content.content) {
- if (!child.content) {
- const nodeContent: NodeContent = {
+ if (isNodeBlock(child)) {
+ const nodeBlock: NodeBlock = {
type: child.type,
text: child.text,
marks: [],
@@ -107,17 +71,22 @@ const buildChildNode = (
if (child.marks) {
child.marks.forEach((mark) => {
- nodeContent.marks.push({
+ nodeBlock.marks.push({
type: mark.type,
attrs: mark.attrs,
});
});
}
- node.content.push(nodeContent);
+ node.content.push(nodeBlock);
} else {
- buildChildNode(node, child, nodes);
+ index = generateKeyBetween(index, null);
+ buildChildNode(node, child, nodes, index);
}
}
}
};
+
+const isNodeBlock = (content: JSONContent): boolean => {
+ return content.type === 'text';
+};
diff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.ts
index 090c2c74..965f556a 100644
--- a/desktop/src/types/nodes.ts
+++ b/desktop/src/types/nodes.ts
@@ -3,44 +3,28 @@ export type Node = {
workspaceId: string;
parentId?: string | null;
type: string;
+ index?: string | null;
attrs: any;
- content?: NodeContent[] | null;
+ content?: NodeBlock[] | null;
createdAt: Date;
createdBy: string;
updatedAt?: Date | null;
updatedBy?: string | null;
versionId: string;
+ state?: string | null;
};
-export type NodeContent = {
+export type NodeBlock = {
type: string;
- id?: string | null;
text?: string | null;
- marks?: NodeContentMark[];
+ marks?: NodeBlockMark[];
};
-export type NodeContentMark = {
+export type NodeBlockMark = {
type: string;
attrs: any;
};
-export type NodeTree = {
- id: string;
- workspaceId: string;
- parentId?: string;
- type: string;
- attrs: any;
- content: NodeContentTree[] | null;
- createdAt: Date;
- createdBy: string;
- updatedAt?: Date | null;
- updatedBy?: string | null;
- versionId: string;
-};
-
-export type NodeContentTree = {
- type: string;
- text?: string | null;
- marks?: NodeContentMark[];
- node?: NodeTree;
+export type NodeWithChildren = Node & {
+ children: NodeWithChildren[];
};
diff --git a/server/prisma/migrations/20240804091557_init_db/migration.sql b/server/prisma/migrations/20240805101219_init_db/migration.sql
similarity index 97%
rename from server/prisma/migrations/20240804091557_init_db/migration.sql
rename to server/prisma/migrations/20240805101219_init_db/migration.sql
index 24b3890b..f1c4177e 100644
--- a/server/prisma/migrations/20240804091557_init_db/migration.sql
+++ b/server/prisma/migrations/20240805101219_init_db/migration.sql
@@ -53,6 +53,7 @@ CREATE TABLE "nodes" (
"workspace_id" VARCHAR(30) NOT NULL,
"parent_id" VARCHAR(30),
"type" VARCHAR(30) NOT NULL,
+ "index" VARCHAR(30),
"attrs" JSONB,
"content" JSONB,
"created_at" TIMESTAMPTZ(6) NOT NULL,
@@ -60,6 +61,7 @@ CREATE TABLE "nodes" (
"updated_at" TIMESTAMPTZ(6),
"updated_by" VARCHAR(30),
"version_id" VARCHAR(30) NOT NULL,
+ "state" TEXT,
CONSTRAINT "nodes_pkey" PRIMARY KEY ("id")
);
diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma
index 61c0a831..5a3cb5ed 100644
--- a/server/prisma/schema.prisma
+++ b/server/prisma/schema.prisma
@@ -59,6 +59,7 @@ model nodes {
workspaceId String @db.VarChar(30) @map("workspace_id")
parentId String? @db.VarChar(30) @map("parent_id")
type String @db.VarChar(30)
+ index String? @db.VarChar(30)
attrs Json?
content Json?
createdAt DateTime @db.Timestamptz(6) @map("created_at")
@@ -66,6 +67,7 @@ model nodes {
updatedAt DateTime? @db.Timestamptz(6) @map("updated_at")
updatedBy String? @db.VarChar(30) @map("updated_by")
versionId String @db.VarChar(30) @map("version_id")
+ state String? @db.Text()
parent nodes? @relation("node_parent_child", fields: [parentId], references: [id], onDelete: Cascade)
children nodes[] @relation("node_parent_child")
diff --git a/server/src/routes/transactions.ts b/server/src/routes/transactions.ts
index 6cfb77b3..58df340b 100644
--- a/server/src/routes/transactions.ts
+++ b/server/src/routes/transactions.ts
@@ -16,11 +16,13 @@ const applyTransactions = async (req: Request, res: Response) => {
parentId: node.parentId,
workspaceId: node.workspaceId,
type: node.type,
+ index: node.index,
attrs: node.attrs,
createdAt: node.createdAt,
createdBy: node.createdBy,
versionId: node.versionId,
content: JSON.stringify(node.content),
+ state: node.state,
},
});
diff --git a/server/src/routes/workspaces.ts b/server/src/routes/workspaces.ts
index df24ffc0..0bed0101 100644
--- a/server/src/routes/workspaces.ts
+++ b/server/src/routes/workspaces.ts
@@ -334,6 +334,7 @@ const getNodes = async (req: Request, res: Response) => {
parentId: node.parentId,
workspaceId: node.workspaceId,
type: node.type,
+ index: node.index,
attrs: node.attrs,
createdAt: node.createdAt,
createdBy: node.createdBy,
@@ -341,6 +342,7 @@ const getNodes = async (req: Request, res: Response) => {
content: node.content ? JSON.parse(node.content as string) : null,
updatedAt: node.updatedAt,
updatedBy: node.updatedBy,
+ state: node.state,
};
});
diff --git a/server/src/types/nodes.ts b/server/src/types/nodes.ts
index b6284940..4d872dd0 100644
--- a/server/src/types/nodes.ts
+++ b/server/src/types/nodes.ts
@@ -3,23 +3,24 @@ export type Node = {
workspaceId: string;
parentId?: string | null;
type: string;
+ index: string | null;
attrs: any;
- content?: NodeContent[] | null;
+ content?: NodeBlock[] | null;
createdAt: Date;
createdBy: string;
updatedAt?: Date | null;
updatedBy?: string | null;
versionId: string;
+ state: string | null;
};
-export type NodeContent = {
+export type NodeBlock = {
type: string;
- id?: string | null;
text?: string | null;
- marks?: NodeContentMark[];
+ marks?: NodeBlockMark[];
};
-export type NodeContentMark = {
+export type NodeBlockMark = {
type: string;
attrs: any;
};