Skip to content

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

Mutation and document editor improvements

Hakan Shehua0846a6cbc13
diff --git a/desktop/src/components/documents/document-editor.tsx b/desktop/src/components/documents/document-editor.tsx
index fd404abe..3e37ac2e 100644
--- a/desktop/src/components/documents/document-editor.tsx
+++ b/desktop/src/components/documents/document-editor.tsx
@@ -59,6 +59,7 @@ import { Editor } from '@tiptap/core';
import { debounce, isEqual } from 'lodash';
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
+import { CompiledQuery } from 'kysely';
interface DocumentEditorProps {
node: LocalNode;
@@ -156,6 +157,7 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
return;
}
+ const queries: CompiledQuery[] = [];
for (const newEditorNode of newEditorNodes.values()) {
const existingEditorNode = nodesSnapshot.current.get(newEditorNode.id);
if (!existingEditorNode) {
@@ -178,7 +180,7 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
})
.compile();
- await workspace.mutate(query);
+ queries.push(query);
} else if (!isEqual(existingEditorNode, newEditorNode)) {
const updateNodeMutation = workspace.schema
.updateTable('nodes')
@@ -190,11 +192,14 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
content: newEditorNode.content
? JSON.stringify(newEditorNode.content)
: null,
+ updated_at: new Date().toISOString(),
+ updated_by: workspace.userId,
+ version_id: NeuronId.generate(NeuronId.Type.Version),
})
.where('id', '=', newEditorNode.id)
.compile();
- await workspace.mutate(updateNodeMutation);
+ queries.push(updateNodeMutation);
}
}
@@ -211,7 +216,11 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
.where('id', 'in', toDeleteIds)
.compile();
- await workspace.mutate(deleteNodesMutation);
+ queries.push(deleteNodesMutation);
+ }
+
+ if (queries.length > 0) {
+ await workspace.mutate(queries);
}
nodesSnapshot.current = newEditorNodes;
@@ -284,11 +293,16 @@ export const DocumentEditor = ({ node, nodes }: DocumentEditorProps) => {
);
const selection = editor.state.selection;
- editor
- .chain()
- .setContent(newEditorContent)
- .setTextSelection(selection)
- .run();
+ if (selection.$anchor != null && selection.$head != null) {
+ editor
+ .chain()
+ .setContent(newEditorContent)
+ .setTextSelection(selection)
+ .run();
+ } else {
+ editor.chain().setContent(newEditorContent).run();
+ }
+
nodesSnapshot.current = newEditorNodes;
}, 500),
[node.id],
diff --git a/desktop/src/contexts/workspace.ts b/desktop/src/contexts/workspace.ts
index 2b8f0cbc..d90718fc 100644
--- a/desktop/src/contexts/workspace.ts
+++ b/desktop/src/contexts/workspace.ts
@@ -5,7 +5,7 @@ import { WorkspaceDatabaseSchema } from '@/data/schemas/workspace';
interface WorkspaceContext extends Workspace {
schema: Kysely<WorkspaceDatabaseSchema>;
- mutate: (mutation: CompiledQuery) => Promise<void>;
+ mutate: (mutation: CompiledQuery | CompiledQuery[]) => Promise<void>;
query: <R>(query: CompiledQuery<R>) => Promise<QueryResult<R>>;
queryAndSubscribe: <R>(
queryId: string,
diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.ts
index 43d3c97a..f3f0e9b4 100644
--- a/desktop/src/data/workspace-manager.ts
+++ b/desktop/src/data/workspace-manager.ts
@@ -99,16 +99,42 @@ export class WorkspaceManager {
return result;
}
- public async executeMutation(mutation: CompiledQuery): Promise<void> {
- const result = await this.database.executeQuery(mutation);
+ public async executeMutation(
+ mutation: CompiledQuery | CompiledQuery[],
+ ): Promise<void> {
+ const affectedTables = new Set<string>();
+ let numAffectedRows = 0n;
+
+ if (Array.isArray(mutation)) {
+ // Execute multiple queries as a transaction
+ await this.database.transaction().execute(async (trx) => {
+ for (const query of mutation) {
+ const result = await trx.executeQuery(query);
+ if (result.numAffectedRows > 0n) {
+ numAffectedRows += result.numAffectedRows;
+ extractTablesFromSql(query.sql).forEach((table) =>
+ affectedTables.add(table),
+ );
+ }
+ }
+ });
+ } else {
+ // Execute single query
+ const result = await this.database.executeQuery(mutation);
+ numAffectedRows = result.numAffectedRows;
+ if (numAffectedRows > 0n) {
+ extractTablesFromSql(mutation.sql).forEach((table) =>
+ affectedTables.add(table),
+ );
+ }
+ }
- if (result.numAffectedRows === 0n) {
+ if (numAffectedRows === 0n) {
return;
}
- const affectedTables = extractTablesFromSql(mutation.sql);
- if (affectedTables.length > 0) {
- this.debouncedNotifyQuerySubscribers(affectedTables);
+ if (affectedTables.size > 0) {
+ this.debouncedNotifyQuerySubscribers(Array.from(affectedTables));
}
}
diff --git a/desktop/src/lib/id.ts b/desktop/src/lib/id.ts
index 6692e516..23770e9c 100644
--- a/desktop/src/lib/id.ts
+++ b/desktop/src/lib/id.ts
@@ -24,7 +24,7 @@ enum IdType {
BulletList = 'bl',
TaskList = 'tl',
TaskItem = 'ti',
- Divider = 'di',
+ HorizontalRule = 'hr',
}
export class NeuronId {
@@ -70,8 +70,8 @@ export class NeuronId {
return IdType.TaskList;
case NodeTypes.TaskItem:
return IdType.TaskItem;
- case NodeTypes.Divider:
- return IdType.Divider;
+ case NodeTypes.HorizontalRule:
+ return IdType.HorizontalRule;
default:
return IdType.Node;
}
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 7fb08058..1900acab 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -104,7 +104,7 @@ ipcMain.handle(
_,
accountId: string,
workspaceId: string,
- mutation: CompiledQuery,
+ mutation: CompiledQuery | CompiledQuery[],
): Promise<void> => {
const accountManager = await appManager.getAccount(accountId);
if (!accountManager) {
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 38415597..b85e957f 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -22,7 +22,7 @@ contextBridge.exposeInMainWorld('neuron', {
executeWorkspaceMutation: (
accountId: string,
workspaceId: string,
- mutation: CompiledQuery,
+ mutation: CompiledQuery | CompiledQuery[],
): Promise<void> =>
ipcRenderer.invoke(
'execute-workspace-mutation',
diff --git a/desktop/src/types/window.d.ts b/desktop/src/types/window.d.ts
index f0ca944a..07d5f836 100644
--- a/desktop/src/types/window.d.ts
+++ b/desktop/src/types/window.d.ts
@@ -16,7 +16,7 @@ interface NeuronApi {
executeWorkspaceMutation: (
accountId: string,
workspaceId: string,
- mutation: CompiledQuery,
+ mutation: CompiledQuery | CompiledQuery[],
) => Promise<void>;
executeWorkspaceQuery: (
diff --git a/server/src/routes/mutations.ts b/server/src/routes/mutations.ts
index 31994ef0..dbc0e8a3 100644
--- a/server/src/routes/mutations.ts
+++ b/server/src/routes/mutations.ts
@@ -107,6 +107,7 @@ const handleUpdateNodeMutation = async (
version_id: nodeData.version_id,
server_updated_at: new Date(),
})
+ .where('id', '=', nodeData.id)
.execute();
};