Skip to content

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

Implement nested page and other improvements

Hakan Shehub60222b0b188
diff --git a/desktop/src/components/documents/document-editor.tsx b/desktop/src/components/documents/document-editor.tsx
index cea1a5ac..aed3e0be 100644
--- a/desktop/src/components/documents/document-editor.tsx
+++ b/desktop/src/components/documents/document-editor.tsx
@@ -12,9 +12,11 @@ import {
OrderedListCommand,
DividerCommand,
TodoCommand,
+ PageCommand,
} from '@/editor/commands';
import {
IdExtension,
+ DocumentNode,
PageNode,
TextNode,
ParagraphNode,
@@ -61,6 +63,7 @@ export const DocumentEditor = ({
{
extensions: [
IdExtension,
+ DocumentNode,
PageNode,
TextNode,
ParagraphNode,
@@ -85,6 +88,7 @@ export const DocumentEditor = ({
CommanderExtension.configure({
commands: [
ParagraphCommand,
+ PageCommand,
BlockquoteCommand,
Heading1Command,
Heading2Command,
diff --git a/desktop/src/components/documents/document.tsx b/desktop/src/components/documents/document.tsx
index 2ce4f049..219b43f9 100644
--- a/desktop/src/components/documents/document.tsx
+++ b/desktop/src/components/documents/document.tsx
@@ -2,7 +2,7 @@ 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 { mapToDocumentContent } from '@/editor/utils';
import { observer } from 'mobx-react-lite';
interface DocumentProps {
@@ -10,12 +10,11 @@ interface DocumentProps {
}
export const Document = observer(({ node }: DocumentProps) => {
- const { isLoading, nodes, onUpdate } = useDocument(node);
-
- if (isLoading) {
+ const { isLoaded, nodes, onUpdate } = useDocument(node);
+ if (!isLoaded) {
return <div>Loading...</div>;
}
- const content = mapToEditorContent(node, nodes);
+ const content = mapToDocumentContent(node.id, nodes);
return <DocumentEditor id={node.id} content={content} onUpdate={onUpdate} />;
});
diff --git a/desktop/src/editor/commands/index.tsx b/desktop/src/editor/commands/index.tsx
index a33e5170..f1101351 100644
--- a/desktop/src/editor/commands/index.tsx
+++ b/desktop/src/editor/commands/index.tsx
@@ -8,6 +8,7 @@ import { Heading3Command } from '@/editor/commands/heading3';
import { OrderedListCommand } from '@/editor/commands/ordered-list';
import { ParagraphCommand } from '@/editor/commands/paragraph';
import { TodoCommand } from '@/editor/commands/todo';
+import { PageCommand } from '@/editor/commands/page';
import { EditorCommand, EditorCommandProps } from '@/types/editor';
export type { EditorCommand, EditorCommandProps };
@@ -22,5 +23,6 @@ export {
OrderedListCommand,
ParagraphCommand,
DividerCommand,
- TodoCommand
+ TodoCommand,
+ PageCommand,
};
diff --git a/desktop/src/editor/commands/page.tsx b/desktop/src/editor/commands/page.tsx
new file mode 100644
index 00000000..4b6f6fe0
--- /dev/null
+++ b/desktop/src/editor/commands/page.tsx
@@ -0,0 +1,31 @@
+import { EditorCommand } from '@/types/editor';
+import { NodeTypes } from '@/lib/constants';
+import { NeuronId } from '@/lib/id';
+
+const PageCommand: EditorCommand = {
+ key: 'page',
+ name: 'Page',
+ description: 'Insert a nested page',
+ keywords: ['page'],
+ icon: 'draft-line',
+ disabled: false,
+ handler({ editor, range, context }) {
+ if (context == null) {
+ return;
+ }
+
+ editor
+ .chain()
+ .focus()
+ .deleteRange(range)
+ .insertContent({
+ type: NodeTypes.Page,
+ attrs: {
+ id: NeuronId.generate(NeuronId.Type.Page),
+ },
+ })
+ .run();
+ },
+};
+
+export { PageCommand };
diff --git a/desktop/src/editor/extensions/page.tsx b/desktop/src/editor/extensions/page.tsx
index 8d8704ee..8fbf246e 100644
--- a/desktop/src/editor/extensions/page.tsx
+++ b/desktop/src/editor/extensions/page.tsx
@@ -1,7 +1,34 @@
-import { Node } from '@tiptap/core';
+import { mergeAttributes, Node } from '@tiptap/core';
+import { ReactNodeViewRenderer } from '@tiptap/react';
+import { PageNodeView } from '@/editor/views';
-export const PageNode = Node.create({
+const PageNode = Node.create({
name: 'page',
- topNode: true,
- content: 'block+',
+ group: 'block',
+ atom: true,
+ defining: true,
+ draggable: true,
+ addAttributes() {
+ return {
+ id: {
+ default: null,
+ },
+ name: {
+ default: null,
+ },
+ avatar: {
+ default: null,
+ },
+ };
+ },
+ renderHTML({ HTMLAttributes }) {
+ return ['page', mergeAttributes(HTMLAttributes)];
+ },
+ addNodeView() {
+ return ReactNodeViewRenderer(PageNodeView, {
+ as: 'page',
+ });
+ },
});
+
+export { PageNode };
diff --git a/desktop/src/editor/utils.ts b/desktop/src/editor/utils.ts
index 1f495de1..cc1a1de4 100644
--- a/desktop/src/editor/utils.ts
+++ b/desktop/src/editor/utils.ts
@@ -1,10 +1,36 @@
import { Node } from '@/types/nodes';
import { JSONContent } from '@tiptap/core';
-export const mapToEditorContent = (
- parent: Node,
+export const mapToDocumentContent = (
+ parentId: string,
nodes: Node[],
): JSONContent => {
+ const contents: JSONContent[] = [];
+ const childrenNodes = nodes.filter((node) => node.parentId === parentId);
+
+ for (const child of childrenNodes) {
+ const content = mapNodeToEditorContent(child, nodes);
+ contents.push(content);
+ }
+
+ if (!contents.length) {
+ contents.push({
+ type: 'paragraph',
+ content: [
+ {
+ type: 'text',
+ },
+ ],
+ });
+ }
+
+ return {
+ type: 'doc',
+ content: contents,
+ };
+};
+
+const mapNodeToEditorContent = (parent: Node, nodes: Node[]): JSONContent => {
const parentContent: JSONContent = {
type: parent.type,
attrs: {
@@ -28,7 +54,7 @@ export const mapToEditorContent = (
if (childrenNodes.length > 0) {
parentContent.content = parentContent.content || [];
childrenNodes.forEach((child) => {
- parentContent.content.push(mapToEditorContent(child, nodes));
+ parentContent.content.push(mapNodeToEditorContent(child, nodes));
});
}
diff --git a/desktop/src/editor/views/index.tsx b/desktop/src/editor/views/index.tsx
index 809aec1a..8f5bee51 100644
--- a/desktop/src/editor/views/index.tsx
+++ b/desktop/src/editor/views/index.tsx
@@ -1,3 +1,4 @@
import { CodeBlockNodeView } from '@/editor/views/code-block';
+import { PageNodeView } from '@/editor/views/page';
-export { CodeBlockNodeView };
+export { CodeBlockNodeView, PageNodeView };
diff --git a/desktop/src/editor/views/page.tsx b/desktop/src/editor/views/page.tsx
new file mode 100644
index 00000000..47ee8ebc
--- /dev/null
+++ b/desktop/src/editor/views/page.tsx
@@ -0,0 +1,31 @@
+import React from 'react';
+import { type NodeViewProps } from '@tiptap/core';
+import { NodeViewWrapper } from '@tiptap/react';
+import { Avatar } from '@/components/ui/avatar';
+import { useWorkspace } from '@/contexts/workspace';
+
+export const PageNodeView = ({ node }: NodeViewProps) => {
+ const workspace = useWorkspace();
+ const id = node.attrs.id;
+ const name = node.attrs.name ?? 'Untitled';
+ const avatar = node.attrs.avatar;
+
+ if (!id) {
+ return null;
+ }
+
+ return (
+ <NodeViewWrapper
+ data-block-id={node.attrs.blockId}
+ className="my-0.5 flex h-12 w-full cursor-pointer flex-row items-center gap-1 rounded-md bg-gray-50 p-2 hover:bg-gray-100"
+ onClick={() => {
+ workspace.navigateToNode(id);
+ }}
+ >
+ <Avatar size="small" id={id} name={name} avatar={avatar} />
+ <div role="presentation" className="flex-grow">
+ {name}
+ </div>
+ </NodeViewWrapper>
+ );
+};
diff --git a/desktop/src/hooks/use-document.tsx b/desktop/src/hooks/use-document.tsx
index 8e27dac7..ae5133e8 100644
--- a/desktop/src/hooks/use-document.tsx
+++ b/desktop/src/hooks/use-document.tsx
@@ -15,7 +15,7 @@ import { generateNodeIndex } from '@/lib/nodes';
import { useEventBus } from '@/hooks/use-event-bus';
interface useNodeResult {
- isLoading: boolean;
+ isLoaded: boolean;
nodes: Node[];
onUpdate: (content: JSONContent) => void;
}
@@ -37,12 +37,10 @@ export const useDocument = (node: Node): useNodeResult => {
React.useEffect(() => {
const fetchNodes = async () => {
- store.setIsLoading(true);
-
const nodes = await workspace.getDocumentNodes(node.id);
store.setNodes(nodes);
- store.setIsLoading(false);
+ store.setIsLoaded(true);
};
fetchNodes();
@@ -92,7 +90,7 @@ export const useDocument = (node: Node): useNodeResult => {
fillIndexesFromNodes(childrenNodes, currentNodes);
validateIndexes(childrenNodes);
- const editorNodes = flatenNodesFromEditor(childrenNodes);
+ const editorNodes = flattenNodesFromEditor(childrenNodes);
for (const editorNode of editorNodes) {
const existingNode = currentNodes[editorNode.id];
if (!existingNode) {
@@ -140,7 +138,7 @@ export const useDocument = (node: Node): useNodeResult => {
);
return {
- isLoading: store.isLoading,
+ isLoaded: store.isLoaded,
nodes: store.getNodes(),
onUpdate,
};
@@ -239,9 +237,27 @@ const fillIndexesFromNodes = (
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;
+ const beforeIndex = i === 0 ? null : nodes[i - 1].index;
+
+ // find the lowest index after the current node
+ // we do this because sometimes nodes can be ordered in such a way that
+ // the current node's index is higher than one of its siblings
+ // after the next sibling
+ // for example: 1, {current}, 4, 3
+ let afterIndex = i === nodes.length - 1 ? null : nodes[i + 1].index;
+ for (let j = i + 1; j < nodes.length; j++) {
+ if (nodes[j].index < afterIndex) {
+ afterIndex = nodes[j].index;
+ break;
+ }
+ }
+
+ // extra check to make sure that the beforeIndex and afterIndex are different
+ // because if they are the same the fractional index library will throw an error
+ if (beforeIndex === afterIndex) {
+ afterIndex = generateNodeIndex(beforeIndex, null);
+ }
if (
!currentIndex ||
@@ -257,7 +273,7 @@ const validateIndexes = (nodes: NodeFromEditor[]) => {
}
};
-const flatenNodesFromEditor = (
+const flattenNodesFromEditor = (
nodesFromEditor: NodeFromEditor[],
): NodeFromEditor[] => {
const nodes: Node[] = [];
diff --git a/desktop/src/store/document.ts b/desktop/src/store/document.ts
index 8d307a43..daeb9249 100644
--- a/desktop/src/store/document.ts
+++ b/desktop/src/store/document.ts
@@ -3,11 +3,11 @@ import { makeAutoObservable } from 'mobx';
export class DocumentStore {
public nodes: Record<string, Node>;
- public isLoading: boolean;
+ public isLoaded: boolean;
constructor() {
this.nodes = {};
- this.isLoading = false;
+ this.isLoaded = false;
makeAutoObservable(this);
}
@@ -26,8 +26,8 @@ export class DocumentStore {
delete this.nodes[nodeId];
}
- public setIsLoading(isLoading: boolean) {
- this.isLoading = isLoading;
+ public setIsLoaded(isLoaded: boolean) {
+ this.isLoaded = isLoaded;
}
public getNode(nodeId: string) {
@@ -37,4 +37,4 @@ export class DocumentStore {
public getNodes() {
return Object.values(this.nodes);
}
-}
\ No newline at end of file
+}