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 page create in document editor
Hakan Shehudca335f2a927
diff --git a/desktop/src/main/handlers/mutations/page-create.ts b/desktop/src/main/handlers/mutations/page-create.tsindex 26951809..d42ef658 100644--- a/desktop/src/main/handlers/mutations/page-create.ts+++ b/desktop/src/main/handlers/mutations/page-create.ts@@ -16,18 +16,23 @@ export class PageCreateMutationHandlerinput.userId,);- const siblings = await workspaceDatabase- .selectFrom('nodes')- .selectAll()- .where('parent_id', '=', input.spaceId)- .execute();+ let index: string | undefined = undefined;+ if (input.generateIndex) {+ const siblings = await workspaceDatabase+ .selectFrom('nodes')+ .selectAll()+ .where('parent_id', '=', input.parentId)+ .execute();++ const maxIndex =+ siblings.length > 0+ ? siblings.sort((a, b) => compareString(a.index, b.index))[+ siblings.length - 1+ ].index+ : null;- const maxIndex =- siblings.length > 0- ? siblings.sort((a, b) => compareString(a.index, b.index))[- siblings.length - 1- ].index- : null;+ index = generateNodeIndex(maxIndex, null);+ }const id = generateId(IdType.Page);await workspaceDatabase@@ -38,8 +43,8 @@ export class PageCreateMutationHandlerid: id,attributes: {type: NodeTypes.Page,- parentId: input.spaceId,- index: generateNodeIndex(maxIndex, null),+ parentId: input.parentId,+ index: index,},},input.userId,diff --git a/desktop/src/main/handlers/queries/node-get.ts b/desktop/src/main/handlers/queries/node-get.tsindex d05ca4c6..58fde4c5 100644--- a/desktop/src/main/handlers/queries/node-get.ts+++ b/desktop/src/main/handlers/queries/node-get.ts@@ -15,8 +15,9 @@ export class NodeGetQueryHandler implements QueryHandler<NodeGetQueryInput> {input: NodeGetQueryInput,): Promise<QueryResult<NodeGetQueryInput>> {const row = await this.fetchNode(input);+return {- output: mapNode(row),+ output: row ? mapNode(row) : null,state: {row,},@@ -51,7 +52,7 @@ export class NodeGetQueryHandler implements QueryHandler<NodeGetQueryInput> {return {hasChanges: true,result: {- output: mapNode(row),+ output: row ? mapNode(row) : null,state: {row,},diff --git a/desktop/src/operations/mutations/page-create.ts b/desktop/src/operations/mutations/page-create.tsindex ea7e2402..fe34eb5f 100644--- a/desktop/src/operations/mutations/page-create.ts+++ b/desktop/src/operations/mutations/page-create.ts@@ -1,8 +1,9 @@export type PageCreateMutationInput = {type: 'page_create';userId: string;- spaceId: string;+ parentId: string;name: string;+ generateIndex: boolean;};export type PageCreateMutationOutput = {diff --git a/desktop/src/operations/queries/node-get.ts b/desktop/src/operations/queries/node-get.tsindex c96831b5..0625b936 100644--- a/desktop/src/operations/queries/node-get.ts+++ b/desktop/src/operations/queries/node-get.ts@@ -10,7 +10,7 @@ declare module '@/operations/queries' {interface QueryMap {node_get: {input: NodeGetQueryInput;- output: LocalNode;+ output: LocalNode | null;};}}diff --git a/desktop/src/renderer/components/documents/document-editor.tsx b/desktop/src/renderer/components/documents/document-editor.tsxindex a6610fc8..09d6cc5d 100644--- a/desktop/src/renderer/components/documents/document-editor.tsx+++ b/desktop/src/renderer/components/documents/document-editor.tsx@@ -126,7 +126,10 @@ export const DocumentEditor = ({DividerCommand,TodoCommand,],- context: {},+ context: {+ documentId,+ userId: workspace.userId,+ },}),BoldMark,ItalicMark,diff --git a/desktop/src/renderer/components/pages/page-create-dialog.tsx b/desktop/src/renderer/components/pages/page-create-dialog.tsxindex 9fd00f2a..ff9e8d32 100644--- a/desktop/src/renderer/components/pages/page-create-dialog.tsx+++ b/desktop/src/renderer/components/pages/page-create-dialog.tsx@@ -58,8 +58,9 @@ export const PageCreateDialog = ({mutate({input: {type: 'page_create',- spaceId: spaceId,+ parentId: spaceId,name: values.name,+ generateIndex: true,userId: workspace.userId,},onSuccess(output) {diff --git a/desktop/src/renderer/editor/commands/page.tsx b/desktop/src/renderer/editor/commands/page.tsxindex bfb101e8..8f9f6633 100644--- a/desktop/src/renderer/editor/commands/page.tsx+++ b/desktop/src/renderer/editor/commands/page.tsx@@ -1,6 +1,5 @@import { EditorCommand } from '@/types/editor';import { NodeTypes } from '@/lib/constants';-import { generateId, IdType } from '@/lib/id';const PageCommand: EditorCommand = {key: 'page',@@ -9,11 +8,24 @@ const PageCommand: EditorCommand = {keywords: ['page'],icon: 'draft-line',disabled: false,- handler({ editor, range, context }) {+ async handler({ editor, range, context }) {if (context == null) {return;}+ const { userId, documentId } = context;+ const output = await window.neuron.executeMutation({+ type: 'page_create',+ name: 'Untitled',+ userId,+ parentId: documentId,+ generateIndex: false,+ });++ if (!output.id) {+ return;+ }+editor.chain().focus()@@ -21,7 +33,7 @@ const PageCommand: EditorCommand = {.insertContent({type: NodeTypes.Page,attrs: {- id: generateId(IdType.Page),+ id: output.id,},}).run();diff --git a/desktop/src/renderer/editor/extensions/commander.tsx b/desktop/src/renderer/editor/extensions/commander.tsxindex d3da0928..daba04fc 100644--- a/desktop/src/renderer/editor/extensions/commander.tsx+++ b/desktop/src/renderer/editor/extensions/commander.tsx@@ -193,7 +193,7 @@ export const CommanderExtension = Extension.create<CommanderOptions>({Suggestion({editor: this.editor,char: '/',- command: ({+ command: async ({editor,range,props,@@ -202,7 +202,15 @@ export const CommanderExtension = Extension.create<CommanderOptions>({range: Range;props: EditorCommand;}) => {- props.handler({ editor, range, context: this.options.context });+ const result = props.handler({+ editor,+ range,+ context: this.options.context,+ });++ if (result instanceof Promise) {+ await result;+ }},items: ({ query }: { query: string }) =>filterCommands({ query, commands: this.options.commands }),diff --git a/desktop/src/renderer/editor/views/page.tsx b/desktop/src/renderer/editor/views/page.tsxindex 4ee0573f..00b57f61 100644--- a/desktop/src/renderer/editor/views/page.tsx+++ b/desktop/src/renderer/editor/views/page.tsx@@ -3,17 +3,24 @@ import { type NodeViewProps } from '@tiptap/core';import { NodeViewWrapper } from '@tiptap/react';import { Avatar } from '@/renderer/components/avatars/avatar';import { useWorkspace } from '@/renderer/contexts/workspace';+import { useQuery } from '@/renderer/hooks/use-query';export const PageNodeView = ({ node }: NodeViewProps) => {const workspace = useWorkspace();const id = node.attrs.id;- const name = node.attrs.name ?? 'Unnamed';- const avatar = node.attrs.avatar;+ const { data, isPending } = useQuery({+ type: 'node_get',+ nodeId: id,+ userId: workspace.userId,+ });if (!id) {return null;}+ const name = data?.attributes.name ?? 'Unnamed';+ const avatar = data?.attributes.avatar;+return (<NodeViewWrapperdata-block-id={node.attrs.blockId}diff --git a/desktop/src/types/editor.ts b/desktop/src/types/editor.tsindex 0016d22c..368273bf 100644--- a/desktop/src/types/editor.ts+++ b/desktop/src/types/editor.ts@@ -7,7 +7,10 @@ export type EditorCommandProps = {context: EditorCommandContext | null;};-export type EditorCommandContext = {};+export type EditorCommandContext = {+ documentId: string;+ userId: string;+};export type EditorCommand = {key: string;@@ -15,7 +18,7 @@ export type EditorCommand = {description: string;keywords?: string[];icon: string;- handler: (props: EditorCommandProps) => void;+ handler: (props: EditorCommandProps) => void | Promise<void>;disabled?: boolean;};