Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Improve document editor
Hakan Shehu1630849efc83
diff --git a/desktop/src/components/pages/page-editor.tsx b/desktop/src/components/documents/document-editor.tsxsimilarity index 88%rename from desktop/src/components/pages/page-editor.tsxrename to desktop/src/components/documents/document-editor.tsxindex 7910dcb8..cea1a5ac 100644--- a/desktop/src/components/pages/page-editor.tsx+++ b/desktop/src/components/documents/document-editor.tsx@@ -1,7 +1,6 @@import React from 'react';import '@/styles/editor.css';-import { useEditor, EditorContent } from '@tiptap/react';-import { Node } from '@/types/nodes';+import { useEditor, EditorContent, JSONContent } from '@tiptap/react';import {ParagraphCommand,BlockquoteCommand,@@ -46,16 +45,18 @@ import {} from '@/editor/extensions';import { EditorBubbleMenu } from '@/editor/menu/bubble-menu';-import { observer } from 'mobx-react-lite';-import { useDocument } from '@/hooks/use-document';-interface PageEditorProps {- node: Node;+interface DocumentEditorProps {+ id: string;+ content: JSONContent;+ onUpdate: (content: JSONContent) => void;}-export const PageEditor = observer(({ node }: PageEditorProps) => {- const { content, onUpdate } = useDocument(node);-+export const DocumentEditor = ({+ id,+ content,+ onUpdate,+}: DocumentEditorProps) => {const editor = useEditor({extensions: [@@ -120,7 +121,7 @@ export const PageEditor = observer(({ node }: PageEditorProps) => {},autofocus: 'start',},- [node.id],+ [id],);return (@@ -133,4 +134,4 @@ export const PageEditor = observer(({ node }: PageEditorProps) => {<EditorContent editor={editor} /></div>);-});+};diff --git a/desktop/src/components/documents/document.tsx b/desktop/src/components/documents/document.tsxnew file mode 100644index 00000000..2ce4f049--- /dev/null+++ b/desktop/src/components/documents/document.tsx@@ -0,0 +1,21 @@+import React from 'react';+import { useDocument } from '@/hooks/use-document';+import { Node } from '@/types/nodes';+import { DocumentEditor } from './document-editor';+import { mapToEditorContent } from '@/editor/utils';+import { observer } from 'mobx-react-lite';++interface DocumentProps {+ node: Node;+}++export const Document = observer(({ node }: DocumentProps) => {+ const { isLoading, nodes, onUpdate } = useDocument(node);++ if (isLoading) {+ return <div>Loading...</div>;+ }++ const content = mapToEditorContent(node, nodes);+ return <DocumentEditor id={node.id} content={content} onUpdate={onUpdate} />;+});diff --git a/desktop/src/components/pages/page-container-node.tsx b/desktop/src/components/pages/page-container-node.tsxindex 84368d62..0aec5a98 100644--- a/desktop/src/components/pages/page-container-node.tsx+++ b/desktop/src/components/pages/page-container-node.tsx@@ -1,16 +1,16 @@import React from 'react';-import {ScrollArea} from "@/components/ui/scroll-area";-import {Node} from '@/types/nodes'-import {PageEditor} from "@/components/pages/page-editor";+import { ScrollArea } from '@/components/ui/scroll-area';+import { Node } from '@/types/nodes';+import { Document } from '@/components/documents/document';interface PageContainerNodeProps {node: Node;}-export const PageContainerNode = ({node}: PageContainerNodeProps) => {+export const PageContainerNode = ({ node }: PageContainerNodeProps) => {return (<ScrollArea className="h-full max-h-full w-full overflow-y-auto px-10 pb-12">- <PageEditor node={node} />+ <Document node={node} /></ScrollArea>- )-}\ No newline at end of file+ );+};diff --git a/desktop/src/components/spaces/space-create-dialog.tsx b/desktop/src/components/spaces/space-create-dialog.tsxindex 1f434b93..9c31e48c 100644--- a/desktop/src/components/spaces/space-create-dialog.tsx+++ b/desktop/src/components/spaces/space-create-dialog.tsx@@ -25,8 +25,8 @@ import { useForm } from 'react-hook-form';import { zodResolver } from '@hookform/resolvers/zod';import { CreateNodeInput, Node } from '@/types/nodes';import { NeuronId } from '@/lib/id';-import { generateKeyBetween } from 'fractional-indexing-jittered';import { NodeTypes } from '@/lib/constants';+import { generateNodeIndex } from '@/lib/nodes';const formSchema = z.object({name: z.string().min(3, 'Name must be at least 3 characters long.'),@@ -76,7 +76,7 @@ export const SpaceCreateDialog = ({attrs: {name: 'Home',},- index: generateKeyBetween(null, null),+ index: generateNodeIndex(null, null),parentId: spaceInput.id,};@@ -86,7 +86,7 @@ export const SpaceCreateDialog = ({attrs: {name: 'Discussions',},- index: generateKeyBetween(pageInput.index, null),+ index: generateNodeIndex(pageInput.index, null),parentId: spaceInput.id,};diff --git a/desktop/src/components/workspaces/workspace.tsx b/desktop/src/components/workspaces/workspace.tsxindex aeb0de72..538a2960 100644--- a/desktop/src/components/workspaces/workspace.tsx+++ b/desktop/src/components/workspaces/workspace.tsx@@ -37,7 +37,6 @@ export const Workspace = observer(() => {return workspace.getNodes();},updateNode: async (node) => {- workspace.setNode(node);await window.workspaceDb.updateNode(workspace.accountId,workspace.id,@@ -45,13 +44,19 @@ export const Workspace = observer(() => {);},deleteNode: async (nodeId) => {- workspace.deleteNode(nodeId);await window.workspaceDb.deleteNode(workspace.accountId,workspace.id,nodeId,);},+ deleteNodes: async (nodeIds) => {+ await window.workspaceDb.deleteNodes(+ workspace.accountId,+ workspace.id,+ nodeIds,+ );+ },setContainerNode: (nodeId) => {workspace.setContainerNode(nodeId);},@@ -64,6 +69,13 @@ export const Workspace = observer(() => {after,);},+ getDocumentNodes: async (documentId) => {+ return await window.workspaceDb.getDocumentNodes(+ workspace.accountId,+ workspace.id,+ documentId,+ );+ },}}><div className="flex h-screen max-h-screen flex-row">diff --git a/desktop/src/contexts/workspace.ts b/desktop/src/contexts/workspace.tsindex 77e90345..1d141e8a 100644--- a/desktop/src/contexts/workspace.ts+++ b/desktop/src/contexts/workspace.ts@@ -1,16 +1,18 @@import { createContext, useContext } from 'react';import { Workspace } from '@/types/workspaces';-import { CreateNodeInput, Node } from '@/types/nodes';+import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';interface WorkspaceContext extends Workspace {createNode: (input: CreateNodeInput) => Promise<void>;createNodes: (inputs: CreateNodeInput[]) => Promise<void>;getNodes: () => Node[];- updateNode: (node: Node) => Promise<void>;+ updateNode: (input: UpdateNodeInput) => Promise<void>;deleteNode: (nodeId: string) => Promise<void>;+ deleteNodes: (nodeIds: string[]) => Promise<void>;setContainerNode: (nodeId?: string | null) => void;getConversationNodes: (conversationId: string, count: number, after?: string | null) => Promise<Node[]>;+ getDocumentNodes: (documentId: string) => Promise<Node[]>;}export const WorkspaceContext = createContext<WorkspaceContext>(diff --git a/desktop/src/editor/utils.ts b/desktop/src/editor/utils.tsindex 007acaeb..1f495de1 100644--- a/desktop/src/editor/utils.ts+++ b/desktop/src/editor/utils.ts@@ -1,9 +1,5 @@-import { Node, NodeWithChildren } from '@/types/nodes';+import { Node } from '@/types/nodes';import { JSONContent } from '@tiptap/core';-import { NeuronId } from '@/lib/id';-import { generateKeyBetween } from 'fractional-indexing-jittered';-import { Workspace } from '@/types/workspaces';-import { LeafNodeTypes } from '@/lib/constants';export const mapToEditorContent = (parent: Node,@@ -49,114 +45,3 @@ export const mapToEditorContent = (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,- };-};--export const enumerateNodes = (- nodeWithChildren: NodeWithChildren,- nodes: Node[],-) => {- nodes.push(nodeWithChildren);- nodeWithChildren.children.forEach((child) => {- enumerateNodes(child, nodes);- });-};--const isLeafNode = (type: string) => {- return LeafNodeTypes.includes(type);-};diff --git a/desktop/src/electron/database/workspace/index.ts b/desktop/src/electron/database/workspace/index.tsindex 3bdad72e..5b8ae904 100644--- a/desktop/src/electron/database/workspace/index.ts+++ b/desktop/src/electron/database/workspace/index.ts@@ -5,8 +5,9 @@ import { WorkspaceDatabaseSchema } from '@/electron/database/workspace/schema';import { workspaceDatabaseMigrations } from '@/electron/database/workspace/migrations';import * as fs from 'node:fs';import { GlobalDatabase } from '@/electron/database/global';-import { CreateNodeInput, Node } from '@/types/nodes';+import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';import { NeuronId } from '@/lib/id';+import { LeafNodeTypes, RootNodeTypes } from '@/lib/constants';export class WorkspaceDatabase {accountId: string;@@ -150,47 +151,90 @@ export class WorkspaceDatabase {});}- updateNode = async (node: Node) => {- await this.database+ updateNode = async (input: UpdateNodeInput) => {+ let updateDefinition = this.database.updateTable('nodes').set({- type: node.type,- index: node.index,- parent_id: node.parentId,- updated_at: node.updatedAt.toISOString(),- updated_by: node.updatedBy,- version_id: node.versionId,- attrs: JSON.stringify(node.attrs),- content: JSON.stringify(node.content),- })- .where('id', '=', node.id)+ updated_at: new Date().toISOString(),+ updated_by: this.userId,+ version_id: NeuronId.generate(NeuronId.Type.Version),+ });++ if (input.attrs !== undefined) {+ updateDefinition = updateDefinition.set('attrs', JSON.stringify(input.attrs));+ }++ if (input.content !== undefined) {+ updateDefinition = updateDefinition.set('content', JSON.stringify(input.content));+ }++ if (input.index !== undefined) {+ updateDefinition = updateDefinition.set('index', input.index);+ }++ if (input.parentId !== undefined) {+ updateDefinition = updateDefinition.set('parent_id', input.parentId);+ }++ const updatedRow = await updateDefinition+ .where('id', '=', input.id)+ .returningAll().executeTakeFirst();++ const node: Node = {+ id: updatedRow.id,+ type: updatedRow.type,+ index: updatedRow.index,+ parentId: updatedRow.parent_id,+ workspaceId: updatedRow.workspace_id,+ attrs: updatedRow.attrs && JSON.parse(updatedRow.attrs),+ content: updatedRow.content && JSON.parse(updatedRow.content),+ createdAt: new Date(updatedRow.created_at),+ createdBy: updatedRow.created_by,+ updatedAt: updatedRow.updated_at ? new Date(updatedRow.updated_at) : null,+ updatedBy: updatedRow.updated_by,+ versionId: updatedRow.version_id,+ };- // await this.globalDatabase.addTransaction({- // id: NeuronId.generate(NeuronId.Type.Transaction),- // nodeId: node.id,- // type: 'update_node',- // workspaceId: node.workspaceId,- // accountId: this.accountId,- // input: JSON.stringify(node),- // createdAt: new Date(),- // });+ await this.globalDatabase.addTransaction({+ id: NeuronId.generate(NeuronId.Type.Transaction),+ type: 'update_node',+ workspaceId: node.workspaceId,+ accountId: this.accountId,+ userId: this.userId,+ input: JSON.stringify(input),+ createdAt: new Date(),+ });};deleteNode = async (nodeId: string) => {await this.database.deleteFrom('nodes').where('id', '=', nodeId).execute();- // await this.globalDatabase.addTransaction({- // id: NeuronId.generate(NeuronId.Type.Transaction),- // nodeId: nodeId,- // type: 'delete_node',- // workspaceId: this.workspaceId,- // accountId: this.accountId,- // input: JSON.stringify({ nodeId }),- // createdAt: new Date(),- // });+ await this.globalDatabase.addTransaction({+ id: NeuronId.generate(NeuronId.Type.Transaction),+ type: 'delete_node',+ workspaceId: this.workspaceId,+ accountId: this.accountId,+ userId: this.userId,+ input: nodeId,+ createdAt: new Date(),+ });};+ deleteNodes = async (nodeIds: string[]) => {+ await this.database.deleteFrom('nodes').where('id', 'in', nodeIds).execute();++ await this.globalDatabase.addTransaction({+ id: NeuronId.generate(NeuronId.Type.Transaction),+ type: 'delete_nodes',+ workspaceId: this.workspaceId,+ accountId: this.accountId,+ userId: this.userId,+ input: JSON.stringify(nodeIds),+ createdAt: new Date(),+ });+ }+syncNodes = async (nodes: Node[]) => {const nodeIds = nodes.map((node) => node.id);const existingNodes = await this.database@@ -278,7 +322,12 @@ export class WorkspaceDatabase {nodes.push(...childNodes);parentIds.splice(0, parentIds.length);- parentIds.push(...childNodes.map((node) => node.id));++ const newParentIds = childNodes+ .filter((node) => !RootNodeTypes.includes(node.type) && !LeafNodeTypes.includes(node.type))+ .map((node) => node.id);++ parentIds.push(...newParentIds);}return nodes.map((node) => ({@@ -287,8 +336,52 @@ export class WorkspaceDatabase {index: node.index,parentId: node.parent_id,workspaceId: node.workspace_id,- attrs: JSON.parse(node.attrs),- content: JSON.parse(node.content),+ attrs: node.attrs && JSON.parse(node.attrs),+ content: node.content && JSON.parse(node.content),+ createdAt: new Date(node.created_at),+ createdBy: node.created_by,+ updatedAt: node.updated_at ? new Date(node.updated_at) : null,+ updatedBy: node.updated_by,+ versionId: node.version_id,+ }));+ }++ getDocumentNodes = async (documentId: string): Promise<Node[]> => {+ const nodes = await this.database+ .selectFrom('nodes')+ .selectAll()+ .where('parent_id', '=', documentId)+ .execute();++ const parentIds = nodes+ .filter((node) => !RootNodeTypes.includes(node.type) && !LeafNodeTypes.includes(node.type))+ .map((node) => node.id);++ while (parentIds.length > 0) {+ const newChildNodes = await this.database+ .selectFrom('nodes')+ .selectAll()+ .where('parent_id', 'in', parentIds)+ .execute();++ nodes.push(...newChildNodes);+ parentIds.splice(0, parentIds.length);++ const newParentIds = newChildNodes+ .filter((node) => !RootNodeTypes.includes(node.type) && !LeafNodeTypes.includes(node.type))+ .map((node) => node.id);++ parentIds.push(...newParentIds);+ }++ return nodes.map((node) => ({+ id: node.id,+ type: node.type,+ index: node.index,+ parentId: node.parent_id,+ workspaceId: node.workspace_id,+ attrs: node.attrs && JSON.parse(node.attrs),+ content: node.content && JSON.parse(node.content),createdAt: new Date(node.created_at),createdBy: node.created_by,updatedAt: node.updated_at ? new Date(node.updated_at) : null,diff --git a/desktop/src/electron/event-loop.ts b/desktop/src/electron/event-loop.tsindex 0d4847b1..f29cf3fa 100644--- a/desktop/src/electron/event-loop.ts+++ b/desktop/src/electron/event-loop.ts@@ -5,9 +5,9 @@ import { Node } from '@/types/nodes';export const initEventLoop = () => {setInterval(async () => {- const accounts = await globalDatabase.getAccounts();- await sendTransactions(accounts);- await syncWorkspaces(accounts);+ // const accounts = await globalDatabase.getAccounts();+ // await sendTransactions(accounts);+ // await syncWorkspaces(accounts);}, 10000);};diff --git a/desktop/src/hooks/use-conversation.tsx b/desktop/src/hooks/use-conversation.tsxindex d8ad5c27..8fd62507 100644--- a/desktop/src/hooks/use-conversation.tsx+++ b/desktop/src/hooks/use-conversation.tsx@@ -5,7 +5,7 @@ import { ConversationStore } from '@/store/conversation';import { JSONContent } from '@tiptap/core';import { NeuronId } from '@/lib/id';import { LeafNodeTypes } from '@/lib/constants';-import { generateKeyBetween } from 'fractional-indexing-jittered';+import { generateNodeIndex } from '@/lib/nodes';interface useConversationResult {isLoading: boolean;@@ -101,7 +101,7 @@ const buildMessageCreateInput = (} else {let lastIndex: string | null = null;for (const child of content.content) {- lastIndex = generateKeyBetween(lastIndex, null);+ lastIndex = generateNodeIndex(lastIndex, null);buildMessageCreateInput(child, id, queue, lastIndex);}}diff --git a/desktop/src/hooks/use-document.tsx b/desktop/src/hooks/use-document.tsxindex 9abd32e4..9ec390f2 100644--- a/desktop/src/hooks/use-document.tsx+++ b/desktop/src/hooks/use-document.tsx@@ -1,60 +1,268 @@import React from 'react';-import { debounce } from 'lodash';-import { Node } from '@/types/nodes';+import { debounce, isEqual } from 'lodash';+import {+ CreateNodeInput,+ Node,+ NodeBlock,+ UpdateNodeInput,+} from '@/types/nodes';import { useWorkspace } from '@/contexts/workspace';-import { buildNodes, enumerateNodes, mapToEditorContent } from '@/editor/utils';import { JSONContent } from '@tiptap/core';-import { buildNodeWithChildren } from '@/lib/nodes';+import { DocumentStore } from '@/store/document';+import { NeuronId } from '@/lib/id';+import { LeafNodeTypes } from '@/lib/constants';+import { generateNodeIndex } from '@/lib/nodes';interface useNodeResult {- content: JSONContent;+ isLoading: boolean;+ nodes: Node[];onUpdate: (content: JSONContent) => void;}+export type NodeFromEditor = {+ type: string;+ id: string;+ parentId?: string | null;+ index?: string | null;+ attrs?: any | null;+ content?: NodeBlock[] | null;+ children?: NodeFromEditor[];+};+export const useDocument = (node: Node): useNodeResult => {const workspace = useWorkspace();- const nodes = workspace.getNodes();- const content = mapToEditorContent(node, nodes);+ const store = React.useMemo(() => new DocumentStore(), [node.id]);++ React.useEffect(() => {+ const fetchNode = async () => {+ store.setIsLoading(true);++ const nodes = await workspace.getDocumentNodes(node.id);+ store.setNodes(nodes);++ store.setIsLoading(false);+ };++ fetchNode();+ }, [node.id]);const onUpdate = React.useMemo(() =>- // Update the content of the nodedebounce(async (updatedContent: JSONContent) => {- const nodeWithChildren = buildNodeWithChildren(node, nodes);- const childNodes: Node[] = [];- enumerateNodes(nodeWithChildren, childNodes);-- const newNodes = buildNodes(- workspace,- node,+ const childrenNodes = buildNodesFromEditor(updatedContent.content,- childNodes,+ node.id,);- // for (const newNode of newNodes) {- // const existingNode = childNodes.find((n) => n.id === newNode.id);- // if (existingNode) {- // newNode.updatedBy = workspace.userNodeId;- // newNode.updatedAt = new Date();- // await workspace.updateNode(newNode);- // } else {- // await workspace.addNode(newNode);- // }- // }-- const nodesToDelete = childNodes.filter(- (n) => n.id != node.id && !newNodes.find((nn) => nn.id === n.id),- );+ const nodesToCreate: CreateNodeInput[] = [];+ const nodesToUpdate: UpdateNodeInput[] = [];+ const nodesToDelete: string[] = [];++ const currentNodes = store.nodes;+ fillIndexesFromNodes(childrenNodes, currentNodes);+ validateIndexes(childrenNodes);++ const editorNodes = flatenNodesFromEditor(childrenNodes);+ for (const editorNode of editorNodes) {+ const existingNode = currentNodes[editorNode.id];+ if (!existingNode) {+ nodesToCreate.push({+ id: editorNode.id,+ type: editorNode.type,+ parentId: editorNode.parentId,+ index: editorNode.index,+ attrs: editorNode.attrs,+ content: editorNode.content,+ });+ } else if (hasChanged({ ...existingNode }, editorNode)) {+ nodesToUpdate.push({+ id: editorNode.id,+ parentId: editorNode.parentId,+ index: editorNode.index,+ attrs: editorNode.attrs,+ content: editorNode.content,+ });+ }+ }++ const currentNodeIds = Object.keys(currentNodes);+ for (const nodeId of currentNodeIds) {+ if (!editorNodes.find((node) => node.id === nodeId)) {+ nodesToDelete.push(nodeId);+ }+ }++ if (nodesToCreate.length > 0) {+ await workspace.createNodes(nodesToCreate);+ }- for (const nodeToDelete of nodesToDelete) {- await workspace.deleteNode(nodeToDelete.id);+ if (nodesToUpdate.length > 0) {+ nodesToUpdate.forEach((nodeToUpdate) => {+ workspace.updateNode(nodeToUpdate);+ });+ }++ if (nodesToDelete.length > 0) {+ await workspace.deleteNodes(nodesToDelete);}}, 500),[node.id],);return {- content,+ isLoading: store.isLoading,+ nodes: store.getNodes(),onUpdate,};};++const buildNodesFromEditor = (+ content: JSONContent[],+ parentId: string,+): NodeFromEditor[] => {+ if (!content || content.length === 0) {+ return [];+ }++ const children: NodeFromEditor[] = [];+ for (let i = 0; i < content.length; i++) {+ const child = buildNodeFromEditor(content[i], parentId);+ children.push(child);+ }++ return children;+};++const buildNodeFromEditor = (+ content: JSONContent,+ parentId: string,+): NodeFromEditor => {+ let id = content.attrs?.id;+ if (!id) {+ id = NeuronId.generate(NeuronId.getIdTypeFromNode(content.type));+ }++ const index = content.attrs?.index;+ const attrs = content.attrs ? { ...content.attrs } : {};+ if (attrs.id) {+ delete attrs.id;+ delete attrs.index;+ }++ const nodeFromEditor: NodeFromEditor = {+ id: id,+ type: content.type,+ parentId,+ index: index,+ attrs: attrs,+ };++ if (!content.content || content.content.length === 0) {+ return nodeFromEditor;+ }++ if (LeafNodeTypes.includes(content.type)) {+ nodeFromEditor.content = [];+ for (const child of content.content) {+ nodeFromEditor.content.push({+ type: child.type,+ text: child.text,+ marks: child.marks?.map((mark) => {+ return {+ type: mark.type,+ attrs: mark.attrs,+ };+ }),+ });+ }+ } else {+ nodeFromEditor.children = [];+ for (let i = 0; i < content.content.length; i++) {+ const childNode = buildNodeFromEditor(content.content[i], id);+ nodeFromEditor.children.push(childNode);+ }+ }++ return nodeFromEditor;+};++const fillIndexesFromNodes = (+ nodesFromEditor: NodeFromEditor[],+ nodes: Record<string, Node>,+) => {+ for (const nodeFromEditor of nodesFromEditor) {+ const node = nodes[nodeFromEditor.id];+ if (node) {+ nodeFromEditor.index = node.index;+ }++ if (nodeFromEditor.children) {+ fillIndexesFromNodes(nodeFromEditor.children, nodes);+ }+ }+};++const validateIndexes = (nodes: NodeFromEditor[]) => {+ for (let i = 0; i < nodes.length; i++) {+ const beforeIndex = i === 0 ? null : nodes[i - 1].index;+ const afterIndex = i === nodes.length - 1 ? null : nodes[i + 1].index;+ const currentIndex = nodes[i].index;++ if (+ !currentIndex ||+ currentIndex <= beforeIndex ||+ currentIndex > afterIndex+ ) {+ nodes[i].index = generateNodeIndex(beforeIndex, afterIndex);+ }++ if (nodes[i].children) {+ validateIndexes(nodes[i].children);+ }+ }+};++const flatenNodesFromEditor = (+ nodesFromEditor: NodeFromEditor[],+): NodeFromEditor[] => {+ const nodes: Node[] = [];+ nodesFromEditor.forEach((node) => {+ enumerateNodesFromEditor(node, nodes);+ });++ return nodes;+};++const enumerateNodesFromEditor = (+ node: NodeFromEditor,+ nodes: NodeFromEditor[],+) => {+ nodes.push(node);+ node.children?.forEach((child) => {+ enumerateNodesFromEditor(child, nodes);+ });+};++const hasChanged = (existingNode: Node, editorNode: NodeFromEditor) => {+ if (existingNode.type !== editorNode.type) {+ return true;+ }++ if (existingNode.parentId !== editorNode.parentId) {+ return true;+ }++ if (existingNode.index !== editorNode.index) {+ return true;+ }++ if (!isEqual(existingNode.attrs, editorNode.attrs)) {+ return true;+ }++ if (!isEqual(existingNode.content, editorNode.content)) {+ return true;+ }++ return false;+};diff --git a/desktop/src/lib/constants.ts b/desktop/src/lib/constants.tsindex c129c917..160381e6 100644--- a/desktop/src/lib/constants.ts+++ b/desktop/src/lib/constants.ts@@ -22,3 +22,10 @@ export const LeafNodeTypes: string[] = [NodeTypes.Divider,NodeTypes.CodeBlock,];++export const RootNodeTypes: string[] = [+ NodeTypes.Space,+ NodeTypes.Message,+ NodeTypes.Page,+ NodeTypes.Channel,+];\ No newline at end of filediff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.tsindex ea4612bf..2707965a 100644--- a/desktop/src/lib/nodes.ts+++ b/desktop/src/lib/nodes.ts@@ -1,4 +1,5 @@import { Node, NodeWithChildren } from '@/types/nodes';+import { generateKeyBetween } from 'fractional-indexing-jittered';export const buildNodeWithChildren = (node: Node,@@ -13,3 +14,10 @@ export const buildNodeWithChildren = (children: children,};};++export const generateNodeIndex = (before?: string | null, after?: string | null) => {+ const lower = before === undefined ? null : before;+ const upper = after === undefined ? null : after;++ return generateKeyBetween(lower, upper);+}\ No newline at end of filediff --git a/desktop/src/main.ts b/desktop/src/main.tsindex 4c148e3b..1a2c387e 100644--- a/desktop/src/main.ts+++ b/desktop/src/main.ts@@ -90,12 +90,12 @@ ipcMain.handle('get-nodes', async (_, accountId, workspaceId) => {return workspaceDb.getNodes();});-ipcMain.handle('update-node', async (_, accountId, workspaceId, node) => {+ipcMain.handle('update-node', async (_, accountId, workspaceId, input) => {const workspaceDb = globalDatabase.getWorkspaceDatabase(accountId,workspaceId,);- await workspaceDb.updateNode(node);+ await workspaceDb.updateNode(input);});ipcMain.handle('delete-node', async (_, accountId, workspaceId, nodeId) => {@@ -106,6 +106,14 @@ ipcMain.handle('delete-node', async (_, accountId, workspaceId, nodeId) => {await workspaceDb.deleteNode(nodeId);});+ipcMain.handle('delete-nodes', async (_, accountId, workspaceId, nodeIds) => {+ const workspaceDb = globalDatabase.getWorkspaceDatabase(+ accountId,+ workspaceId,+ );+ await workspaceDb.deleteNodes(nodeIds);+});+ipcMain.handle('get-conversation-nodes', async (_, accountId, workspaceId, conversationId, count, after) => {const workspaceDb = globalDatabase.getWorkspaceDatabase(accountId,@@ -114,4 +122,12 @@ ipcMain.handle('get-conversation-nodes', async (_, accountId, workspaceId, convereturn await workspaceDb.getConversationNodes(conversationId, count, after);});+ipcMain.handle('get-document-nodes', async (_, accountId, workspaceId, documentId) => {+ const workspaceDb = globalDatabase.getWorkspaceDatabase(+ accountId,+ workspaceId,+ );+ return await workspaceDb.getDocumentNodes(documentId);+});+initEventLoop();diff --git a/desktop/src/preload.ts b/desktop/src/preload.tsindex 59234baf..8a82a7e9 100644--- a/desktop/src/preload.ts+++ b/desktop/src/preload.ts@@ -2,7 +2,7 @@ import { contextBridge, ipcRenderer } from 'electron';import { Account } from '@/types/accounts';import { Workspace } from '@/types/workspaces';import { Transaction } from '@/types/transactions';-import { CreateNodeInput, Node } from '@/types/nodes';+import { CreateNodeInput, UpdateNodeInput } from '@/types/nodes';contextBridge.exposeInMainWorld('globalDb', {init: () => ipcRenderer.invoke('init'),@@ -22,11 +22,16 @@ contextBridge.exposeInMainWorld('workspaceDb', {ipcRenderer.invoke('create-nodes', accountId, workspaceId, nodes),getNodes: (accountId: string, workspaceId: string) =>ipcRenderer.invoke('get-nodes', accountId, workspaceId),- updateNode: (accountId: string, workspaceId: string, node: Node) =>- ipcRenderer.invoke('update-node', accountId, workspaceId, node),+ updateNode: (accountId: string, workspaceId: string, input: UpdateNodeInput) =>+ ipcRenderer.invoke('update-node', accountId, workspaceId, input),deleteNode: (accountId: string, workspaceId: string, nodeId: string) =>ipcRenderer.invoke('delete-node', accountId, workspaceId, nodeId),+ deleteNodes: (accountId: string, workspaceId: string, nodeIds: string[]) =>+ ipcRenderer.invoke('delete-nodes', accountId, workspaceId, nodeIds),getConversationNodes: (accountId: string, workspaceId: string, conversationId: string, count: number, after?: string | null) =>ipcRenderer.invoke('get-conversation-nodes', accountId, workspaceId, conversationId, count, after),++ getDocumentNodes: (accountId: string, workspaceId: string, documentId: string) =>+ ipcRenderer.invoke('get-document-nodes', accountId, workspaceId, documentId),});diff --git a/desktop/src/store/document.ts b/desktop/src/store/document.tsnew file mode 100644index 00000000..3497a0f9--- /dev/null+++ b/desktop/src/store/document.ts@@ -0,0 +1,36 @@+import { Node } from '@/types/nodes';+import { makeAutoObservable } from 'mobx';++export class DocumentStore {+ public nodes: Record<string, Node>;+ public isLoading: boolean;++ constructor() {+ this.nodes = {};+ this.isLoading = false;++ makeAutoObservable(this);+ }++ public setNode(node: Node) {+ this.nodes[node.id] = node;+ }++ public setNodes(nodes: Node[]) {+ nodes.forEach((node) => {+ this.nodes[node.id] = node;+ });+ }++ public setIsLoading(isLoading: boolean) {+ this.isLoading = isLoading;+ }++ public getNode(nodeId: string) {+ return this.nodes[nodeId];+ }++ public getNodes() {+ return Object.values(this.nodes);+ }+}\ No newline at end of filediff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.tsindex 5993d178..d6bc29f1 100644--- a/desktop/src/types/nodes.ts+++ b/desktop/src/types/nodes.ts@@ -36,4 +36,12 @@ export type CreateNodeInput = {index?: string | null;attrs: any;content?: NodeBlock[] | null;-};\ No newline at end of file+};++export type UpdateNodeInput = {+ id: string;+ parentId?: string | null;+ index?: string | null;+ attrs?: any | null;+ content?: NodeBlock[] | null;+};diff --git a/desktop/src/types/transactions.ts b/desktop/src/types/transactions.tsindex d29c7b4e..21a444dd 100644--- a/desktop/src/types/transactions.ts+++ b/desktop/src/types/transactions.ts@@ -1,4 +1,4 @@-export type TransactionType = 'create_node' | 'create_nodes' | 'update_node' | 'delete_node';+export type TransactionType = 'create_node' | 'create_nodes' | 'update_node' | 'delete_node' | 'delete_nodes';export type Transaction = {id: string;diff --git a/desktop/src/types/window.d.ts b/desktop/src/types/window.d.tsindex 2ebf2a25..785a1f1c 100644--- a/desktop/src/types/window.d.ts+++ b/desktop/src/types/window.d.ts@@ -1,7 +1,7 @@import { Workspace } from '@/types/workspaces';import { Transaction } from '@/types/transactions';import { Account } from '@/types/accounts';-import { CreateNodeInput, Node } from '@/types/nodes';+import { CreateNodeInput, Node, UpdateNodeInput } from '@/types/nodes';import { GlobalDatabaseData } from '@/types/global';export interface GlobalDbApi {@@ -28,13 +28,18 @@ export interface WorkspaceDbApi {updateNode: (accountId: string,workspaceId: string,- node: Node,+ input: UpdateNodeInput,) => Promise<void>;deleteNode: (accountId: string,workspaceId: string,nodeId: string,) => Promise<void>;+ deleteNodes: (+ accountId: string,+ workspaceId: string,+ nodeIds: string[],+ ) => Promise<void>;getConversationNodes: (accountId: string,@@ -43,6 +48,12 @@ export interface WorkspaceDbApi {count: number,after?: string | null,) => Promise<Node[]>;++ getDocumentNodes: (+ accountId: string,+ workspaceId: string,+ documentId: string,+ ) => Promise<Node[]>;}declare global {diff --git a/server/src/routes/transactions.ts b/server/src/routes/transactions.tsindex 9b91416b..1b594900 100644--- a/server/src/routes/transactions.ts+++ b/server/src/routes/transactions.ts@@ -1,6 +1,6 @@import { Request, Response, Router } from 'express';import { Transaction } from '@/types/transactions';-import { DeleteNodeTransactionInput, Node } from '@/types/nodes';+import { Node } from '@/types/nodes';import { prisma } from '@/data/db';export const transactionsRouter = Router();@@ -65,10 +65,20 @@ transactionsRouter.post('/', async (req: Request, res: Response) => {appliedTransactionIds.push(transaction.id);} else if (transaction.type === 'delete_node') {- const input = JSON.parse(transaction.input) as DeleteNodeTransactionInput;await prisma.nodes.delete({where: {- id: input.id,+ id: transaction.input,+ },+ });++ appliedTransactionIds.push(transaction.id);+ } else if (transaction.type === 'delete_nodes') {+ const input = JSON.parse(transaction.input) as string[];+ await prisma.nodes.deleteMany({+ where: {+ id: {+ in: input,+ },},});diff --git a/server/src/types/nodes.ts b/server/src/types/nodes.tsindex 8b7c5fd4..4d872dd0 100644--- a/server/src/types/nodes.ts+++ b/server/src/types/nodes.ts@@ -24,7 +24,3 @@ export type NodeBlockMark = {type: string;attrs: any;};--export type DeleteNodeTransactionInput = {- id: string;-};\ No newline at end of filediff --git a/server/src/types/transactions.ts b/server/src/types/transactions.tsindex d29c7b4e..21a444dd 100644--- a/server/src/types/transactions.ts+++ b/server/src/types/transactions.ts@@ -1,4 +1,4 @@-export type TransactionType = 'create_node' | 'create_nodes' | 'update_node' | 'delete_node';+export type TransactionType = 'create_node' | 'create_nodes' | 'update_node' | 'delete_node' | 'delete_nodes';export type Transaction = {id: string;