Skip to content

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

Change the way some nodes are stored

Hakan Shehu70de5cdb5ff6
diff --git a/desktop/src/lib/constants.ts b/desktop/src/lib/constants.ts
index e793666a..d40d0137 100644
--- a/desktop/src/lib/constants.ts
+++ b/desktop/src/lib/constants.ts
@@ -5,17 +5,6 @@ export const NodeTypes = {
Channel: 'channel',
Chat: 'chat',
Message: 'message',
- Paragraph: 'paragraph',
- Heading1: 'heading1',
- Heading2: 'heading2',
- Heading3: 'heading3',
- Blockquote: 'blockquote',
- BulletList: 'bullet_list',
- CodeBlock: 'code_block',
- ListItem: 'list_item',
- OrderedList: 'ordered_list',
- TaskList: 'task_list',
- TaskItem: 'task_item',
HorizontalRule: 'horizontal_rule',
Database: 'database',
DatabaseReplica: 'database_replica',
@@ -44,22 +33,6 @@ export const EditorNodeTypes = {
Page: 'page',
};
-export const LeafNodeTypes: string[] = [
- NodeTypes.Paragraph,
- NodeTypes.Heading1,
- NodeTypes.Heading2,
- NodeTypes.Heading3,
- NodeTypes.HorizontalRule,
- NodeTypes.CodeBlock,
-];
-
-export const RootNodeTypes: string[] = [
- NodeTypes.Space,
- NodeTypes.Message,
- NodeTypes.Page,
- NodeTypes.Channel,
-];
-
export const ViewNodeTypes: string[] = [
NodeTypes.TableView,
NodeTypes.BoardView,
diff --git a/desktop/src/lib/editor.ts b/desktop/src/lib/editor.ts
deleted file mode 100644
index f228a375..00000000
--- a/desktop/src/lib/editor.ts
+++ /dev/null
@@ -1,644 +0,0 @@
-import { EditorNodeTypes, NodeTypes } from '@/lib/constants';
-import { generateId, getIdTypeFromNode } from '@/lib/id';
-import { generateNodeIndex } from '@/lib/nodes';
-import { compareString } from '@/lib/utils';
-import { EditorNode } from '@/types/editor';
-import { LocalNode, NodeBlock } from '@/types/nodes';
-import { JSONContent } from '@tiptap/core';
-
-export const mapContentsToEditorNodes = (
- contents: JSONContent[],
- parentId: string,
- nodesMap: Map<string, LocalNode>,
-): EditorNode[] => {
- const editorNodes: EditorNode[] = [];
- mapAndPushContentsToNodes(contents, parentId, editorNodes, nodesMap);
- validateNodesIndexes(editorNodes);
- return editorNodes;
-};
-
-const mapAndPushContentsToNodes = (
- contents: JSONContent[] | null | undefined,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- if (!contents) {
- return;
- }
-
- contents.map((content) => {
- mapAndPushContentToNode(content, parentId, editorNodes, nodesMap);
- });
-};
-
-export const mapNodesToContents = (
- parentId: string,
- nodes: LocalNode[],
-): JSONContent[] => {
- const contents: JSONContent[] = [];
- const children = nodes
- .filter((node) => node.parentId === parentId)
- .sort((a, b) => compareString(a.index, b.index));
-
- for (const child of children) {
- contents.push(mapNodeToContent(child, nodes));
- }
- return contents;
-};
-
-const validateNodesIndexes = (editorNodes: EditorNode[]) => {
- //group by parentId
- const groupedNodes: { [key: string]: EditorNode[] } = {};
- for (const node of editorNodes) {
- if (!groupedNodes[node.attributes.parentId]) {
- groupedNodes[node.attributes.parentId] = [];
- }
-
- groupedNodes[node.attributes.parentId].push(node);
- }
-
- for (const parentId in groupedNodes) {
- const nodes = groupedNodes[parentId];
-
- for (let i = 0; i < nodes.length; i++) {
- const currentIndex = nodes[i].attributes.index;
- const beforeIndex = i === 0 ? null : nodes[i - 1].attributes.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].attributes.index;
- for (let j = i + 1; j < nodes.length; j++) {
- if (nodes[j].attributes.index < afterIndex) {
- afterIndex = nodes[j].attributes.index;
- break;
- }
- }
-
- // extra check to make sure that the beforeIndex is less than the afterIndex
- // because otherwise the fractional index library will throw an error
- if (afterIndex < beforeIndex) {
- afterIndex = generateNodeIndex(null, beforeIndex);
- } else if (beforeIndex === afterIndex) {
- afterIndex = generateNodeIndex(beforeIndex, null);
- }
-
- if (
- !currentIndex ||
- currentIndex <= beforeIndex ||
- currentIndex > afterIndex
- ) {
- nodes[i].attributes.index = generateNodeIndex(beforeIndex, afterIndex);
- }
- }
- }
-};
-
-const mapAndPushContentToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- switch (content.type) {
- case EditorNodeTypes.Paragraph:
- mapParagraphToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.Heading1:
- mapHeading1ToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.Heading2:
- mapHeading2ToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.Heading3:
- mapHeading3ToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.BulletList:
- mapBulletListToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.OrderedList:
- mapOrderedListToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.ListItem:
- mapListItemToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.Blockquote:
- mapBlockquoteToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.CodeBlock:
- mapCodeBlockToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.HorizontalRule:
- mapHorizontalRuleToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.TaskList:
- mapTaskListToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.TaskItem:
- mapTaskItemToNode(content, parentId, editorNodes, nodesMap);
- break;
- case EditorNodeTypes.Page:
- mapPageToNode(content, parentId, editorNodes, nodesMap);
- break;
- }
-};
-
-const mapNodeToContent = (node: LocalNode, nodes: LocalNode[]): JSONContent => {
- switch (node.type) {
- case NodeTypes.Paragraph:
- return mapParagraphToContent(node);
- case NodeTypes.Heading1:
- return mapHeading1ToContent(node);
- case NodeTypes.Heading2:
- return mapHeading2ToContent(node);
- case NodeTypes.Heading3:
- return mapHeading3ToContent(node);
- case NodeTypes.BulletList:
- return mapBulletListToContent(node, nodes);
- case NodeTypes.OrderedList:
- return mapOrderedListToContent(node, nodes);
- case NodeTypes.ListItem:
- return mapListItemToContent(node, nodes);
- case NodeTypes.Blockquote:
- return mapBlockquoteToContent(node, nodes);
- case NodeTypes.CodeBlock:
- return mapCodeBlockToContent(node);
- case NodeTypes.HorizontalRule:
- return mapHorizontalRuleToContent(node);
- case NodeTypes.TaskList:
- return mapTaskListToContent(node, nodes);
- case NodeTypes.TaskItem:
- return mapTaskItemToContent(node, nodes);
- case NodeTypes.Page:
- return mapPageToContent(node);
- }
-};
-
-const mapParagraphToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.Paragraph,
- parentId: parentId,
- index: index,
- content: mapContentsToNodeBlocks(content.content),
- },
- });
-};
-
-const mapParagraphToContent = (node: LocalNode): JSONContent => {
- return {
- type: EditorNodeTypes.Paragraph,
- attrs: {
- id: node.id,
- },
- content: mapNodeBlocksToContents(node.attributes.content),
- };
-};
-
-const mapHeading1ToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.Heading1,
- parentId: parentId,
- index: index,
- content: mapContentsToNodeBlocks(content.content),
- },
- });
-};
-
-const mapHeading1ToContent = (node: LocalNode): JSONContent => {
- return {
- type: EditorNodeTypes.Heading1,
- attrs: {
- id: node.id,
- },
- content: mapNodeBlocksToContents(node.attributes.content),
- };
-};
-
-const mapHeading2ToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.Heading2,
- parentId: parentId,
- index: index,
- content: mapContentsToNodeBlocks(content.content),
- },
- });
-};
-
-const mapHeading2ToContent = (node: LocalNode): JSONContent => {
- return {
- type: EditorNodeTypes.Heading2,
- attrs: {
- id: node.id,
- },
- content: mapNodeBlocksToContents(node.attributes.content),
- };
-};
-
-const mapHeading3ToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.Heading3,
- parentId: parentId,
- index: index,
- content: mapContentsToNodeBlocks(content.content),
- },
- });
-};
-
-const mapHeading3ToContent = (node: LocalNode): JSONContent => {
- return {
- type: EditorNodeTypes.Heading3,
- attrs: {
- id: node.id,
- },
- content: mapNodeBlocksToContents(node.attributes.content),
- };
-};
-
-const mapBulletListToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
-
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.BulletList,
- parentId: parentId,
- index: index,
- },
- });
-
- mapAndPushContentsToNodes(content.content, id, editorNodes, nodesMap);
-};
-
-const mapBulletListToContent = (
- node: LocalNode,
- nodes: LocalNode[],
-): JSONContent => {
- return {
- type: EditorNodeTypes.BulletList,
- attrs: {
- id: node.id,
- },
- content: mapNodesToContents(node.id, nodes),
- };
-};
-
-const mapOrderedListToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.OrderedList,
- parentId: parentId,
- index: index,
- },
- });
-
- mapAndPushContentsToNodes(content.content, id, editorNodes, nodesMap);
-};
-
-const mapOrderedListToContent = (
- node: LocalNode,
- nodes: LocalNode[],
-): JSONContent => {
- return {
- type: EditorNodeTypes.OrderedList,
- attrs: {
- id: node.id,
- },
- content: mapNodesToContents(node.id, nodes),
- };
-};
-
-const mapListItemToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.ListItem,
- parentId: parentId,
- index: index,
- },
- });
-
- mapAndPushContentsToNodes(content.content, id, editorNodes, nodesMap);
-};
-
-const mapListItemToContent = (
- node: LocalNode,
- nodes: LocalNode[],
-): JSONContent => {
- return {
- type: EditorNodeTypes.ListItem,
- attrs: {
- id: node.id,
- },
- content: mapNodesToContents(node.id, nodes),
- };
-};
-
-const mapBlockquoteToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.Blockquote,
- parentId: parentId,
- index: index,
- },
- });
-
- mapAndPushContentsToNodes(content.content, id, editorNodes, nodesMap);
-};
-
-const mapBlockquoteToContent = (
- node: LocalNode,
- nodes: LocalNode[],
-): JSONContent => {
- return {
- type: EditorNodeTypes.Blockquote,
- attrs: {
- id: node.id,
- },
- content: mapNodesToContents(node.id, nodes),
- };
-};
-
-const mapCodeBlockToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
-
- const language = content.attrs?.language;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.CodeBlock,
- parentId: parentId,
- index: index,
- language: language,
- content: mapContentsToNodeBlocks(content.content),
- },
- });
-};
-
-const mapCodeBlockToContent = (node: LocalNode): JSONContent => {
- const language = node.attributes.language;
- return {
- type: EditorNodeTypes.CodeBlock,
- attrs: {
- id: node.id,
- language: language,
- },
- content: mapNodeBlocksToContents(node.attributes.content),
- };
-};
-
-const mapHorizontalRuleToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.HorizontalRule,
- parentId: parentId,
- index: index,
- },
- });
-};
-
-const mapHorizontalRuleToContent = (node: LocalNode): JSONContent => {
- return {
- type: EditorNodeTypes.HorizontalRule,
- attrs: {
- id: node.id,
- },
- content: null,
- };
-};
-
-const mapTaskListToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.TaskList,
- parentId: parentId,
- index: index,
- },
- });
-
- mapAndPushContentsToNodes(content.content, id, editorNodes, nodesMap);
-};
-
-const mapTaskListToContent = (
- node: LocalNode,
- nodes: LocalNode[],
-): JSONContent => {
- return {
- type: EditorNodeTypes.TaskList,
- attrs: {
- id: node.id,
- },
- content: mapNodesToContents(node.id, nodes),
- };
-};
-
-const mapTaskItemToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.TaskItem,
- parentId: parentId,
- index: index,
- checked: content.attrs?.checked ?? false,
- },
- });
-
- mapAndPushContentsToNodes(content.content, id, editorNodes, nodesMap);
-};
-
-const mapTaskItemToContent = (
- node: LocalNode,
- nodes: LocalNode[],
-): JSONContent => {
- const checked = node.attributes.checked === true;
- return {
- type: EditorNodeTypes.TaskItem,
- attrs: {
- id: node.id,
- checked: checked,
- },
- content: mapNodesToContents(node.id, nodes),
- };
-};
-
-const mapPageToNode = (
- content: JSONContent,
- parentId: string,
- editorNodes: EditorNode[],
- nodesMap: Map<string, LocalNode>,
-): void => {
- const id = getIdFromContent(content);
- const index = nodesMap.get(id)?.index;
-
- editorNodes.push({
- id: id,
- attributes: {
- type: NodeTypes.Page,
- parentId: parentId,
- index: index,
- },
- });
-};
-
-const mapPageToContent = (node: LocalNode): JSONContent => {
- return {
- type: EditorNodeTypes.Page,
- attrs: {
- id: node.id,
- name: node.attributes.name,
- avatar: node.attributes.avatar,
- },
- content: null,
- };
-};
-
-const getIdFromContent = (content: JSONContent): string => {
- return content.attrs?.id ?? generateId(getIdTypeFromNode(content.type));
-};
-
-const mapContentsToNodeBlocks = (
- contents?: JSONContent[],
-): NodeBlock[] | null => {
- if (contents == null) {
- return null;
- }
-
- const nodeBlocks: NodeBlock[] = [];
- for (const content of contents) {
- nodeBlocks.push({
- type: content.type,
- text: content.text,
- marks:
- content.marks?.map((mark) => {
- return {
- type: mark.type,
- attrs: mark.attrs,
- };
- }) ?? null,
- });
- }
-
- return nodeBlocks;
-};
-
-const mapNodeBlocksToContents = (
- nodeBlocks: NodeBlock[] | null,
-): JSONContent[] | null => {
- if (nodeBlocks == null) {
- return null;
- }
-
- const contents: JSONContent[] = [];
- for (const nodeBlock of nodeBlocks) {
- contents.push({
- type: nodeBlock.type,
- text: nodeBlock.text,
- marks:
- nodeBlock.marks?.map((mark) => {
- return {
- type: mark.type,
- attrs: mark.attrs,
- };
- }) ?? null,
- });
- }
-
- return contents;
-};
diff --git a/desktop/src/main/handlers/mutations/document-save.ts b/desktop/src/main/handlers/mutations/document-save.ts
index 55fd370c..e34cd285 100644
--- a/desktop/src/main/handlers/mutations/document-save.ts
+++ b/desktop/src/main/handlers/mutations/document-save.ts
@@ -1,21 +1,14 @@
import * as Y from 'yjs';
-import { CompiledQuery, sql } from 'kysely';
import { fromUint8Array, toUint8Array } from 'js-base64';
-import { isEqual } from 'lodash';
-import { NodeTypes } from '@/lib/constants';
import { mapNode } from '@/lib/nodes';
import { databaseManager } from '@/main/data/database-manager';
-import { EditorNode } from '@/types/editor';
-import { SelectNode } from '@/main/data/workspace/schema';
import {
MutationChange,
MutationHandler,
MutationResult,
} from '@/operations/mutations';
import { DocumentSaveMutationInput } from '@/operations/mutations/document-save';
-import { LocalNode } from '@/types/nodes';
import { generateId, IdType } from '@/lib/id';
-import { mapContentsToEditorNodes } from '@/lib/editor';
export class DocumentSaveMutationHandler
implements MutationHandler<DocumentSaveMutationInput>
@@ -27,219 +20,46 @@ export class DocumentSaveMutationHandler
input.userId,
);
- const query = sql<SelectNode>`
- WITH RECURSIVE document_nodes AS (
- SELECT *
- FROM nodes
- WHERE parent_id = ${input.documentId}
-
- UNION ALL
-
- SELECT child.*
- FROM nodes child
- INNER JOIN document_nodes parent ON child.parent_id = parent.id
- WHERE parent.type NOT IN (${NodeTypes.Page})
- )
- SELECT *
- FROM document_nodes
- `.compile(workspaceDatabase);
-
- const result = await workspaceDatabase.executeQuery(query);
- const nodes = result.rows;
- const map = new Map<string, LocalNode>();
- nodes.forEach((node) => {
- map.set(node.id, mapNode(node));
- });
-
- const editorNodes = mapContentsToEditorNodes(
- input.content.content,
- input.documentId,
- map,
- );
-
- const editorNodesMap = new Map<string, EditorNode>();
- for (const node of editorNodes) {
- editorNodesMap.set(node.id, node);
- }
-
- const allNodeIds = new Set([
- ...map.keys(),
- ...editorNodes.map((node) => node.id),
- ]);
-
- const insertQueries: CompiledQuery[] = [];
- const updateQueries: CompiledQuery[] = [];
- const deleteQueries: CompiledQuery[] = [];
-
- for (const nodeId of allNodeIds) {
- const existingNode = map.get(nodeId);
- const editorNode = editorNodesMap.get(nodeId);
-
- if (!existingNode) {
- const doc = new Y.Doc({
- guid: editorNode.id,
- });
-
- const attributesMap = doc.getMap('attributes');
-
- doc.transact(() => {
- for (const [key, value] of Object.entries(editorNode.attributes)) {
- if (value !== undefined) {
- attributesMap.set(key, value);
- }
- }
- });
-
- const attributes = attributesMap.toJSON();
- const state = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- const newNode: LocalNode = {
- id: editorNode.id,
- type: editorNode.attributes.type,
- parentId: editorNode.attributes.parentId,
- index: editorNode.attributes.index,
- attributes: editorNode.attributes,
- state: state,
- createdAt: new Date().toISOString(),
- createdBy: input.userId,
- versionId: generateId(IdType.Version),
- updatedAt: null,
- updatedBy: null,
- serverCreatedAt: null,
- serverUpdatedAt: null,
- serverVersionId: null,
- };
-
- const insertNodeQuery = workspaceDatabase
- .insertInto('nodes')
- .values({
- id: editorNode.id,
- attributes: JSON.stringify(attributes),
- state: state,
- created_at: new Date().toISOString(),
- created_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .compile();
-
- insertQueries.push(insertNodeQuery);
- } else if (!editorNode) {
- const query = workspaceDatabase
- .deleteFrom('nodes')
- .where('id', '=', existingNode.id)
- .compile();
-
- deleteQueries.push(query);
- } else {
- if (!isEqual(existingNode.attributes, editorNode.attributes)) {
- const doc = new Y.Doc({
- guid: existingNode.id,
- });
-
- Y.applyUpdate(doc, toUint8Array(existingNode.state));
- const attributesMap = doc.getMap('attributes');
-
- let hasChanges = false;
- doc.transact(() => {
- if (existingNode.attributes.type !== editorNode.attributes.type) {
- attributesMap.set('type', editorNode.attributes.type);
- hasChanges = true;
- }
-
- if (
- existingNode.attributes.parentId !==
- editorNode.attributes.parentId
- ) {
- attributesMap.set('parentId', editorNode.attributes.parentId);
- hasChanges = true;
- }
-
- if (existingNode.attributes.index !== editorNode.attributes.index) {
- attributesMap.set('index', editorNode.attributes.index);
- hasChanges = true;
- }
-
- if (
- !isEqual(
- existingNode.attributes.content,
- editorNode.attributes.content,
- )
- ) {
- attributesMap.set('content', editorNode.attributes.content);
- hasChanges = true;
- }
-
- for (const [key, value] of Object.entries(editorNode.attributes)) {
- const existingValue = attributesMap.get(key);
- if (!isEqual(existingValue, value)) {
- attributesMap.set(key, value);
- hasChanges = true;
- }
- }
-
- const existingAttributes = existingNode.attributes;
- const editorAttributes = editorNode.attributes;
- for (const key of Object.keys(existingAttributes)) {
- if (!(key in editorAttributes)) {
- attributesMap.delete(key);
- hasChanges = true;
- }
- }
- });
-
- if (!hasChanges) {
- continue;
- }
-
- const attributes = attributesMap.toJSON();
- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- existingNode.type = editorNode.attributes.type;
- existingNode.parentId = editorNode.attributes.parentId;
- existingNode.index = editorNode.attributes.index;
- existingNode.attributes = editorNode.attributes;
- existingNode.updatedAt = new Date().toISOString();
- existingNode.updatedBy = input.userId;
- existingNode.versionId = generateId(IdType.Version);
-
- const query = workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: JSON.stringify(attributes),
- state: encodedState,
- updated_at: existingNode.updatedAt,
- updated_by: existingNode.updatedBy,
- version_id: existingNode.versionId,
- })
- .where('id', '=', existingNode.id)
- .compile();
-
- updateQueries.push(query);
- }
- }
+ const document = await workspaceDatabase
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', input.documentId)
+ .executeTakeFirst();
+
+ if (!document) {
+ return {
+ output: {
+ success: false,
+ },
+ changes: [],
+ };
}
- const hasChanges =
- insertQueries.length > 0 ||
- updateQueries.length > 0 ||
- deleteQueries.length > 0;
-
- if (hasChanges) {
- await workspaceDatabase.transaction().execute(async (trx) => {
- for (const query of insertQueries) {
- await trx.executeQuery(query);
- }
-
- for (const query of updateQueries) {
- await trx.executeQuery(query);
- }
-
- for (const query of deleteQueries) {
- await trx.executeQuery(query);
- }
- });
- }
+ const node = mapNode(document);
+ const doc = new Y.Doc({
+ guid: node.id,
+ });
+ Y.applyUpdate(doc, toUint8Array(node.state));
+ const attributesMap = doc.getMap('attributes');
+ attributesMap.set('content', input.content.content);
+
+ const attributes = attributesMap.toJSON();
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const result = await workspaceDatabase
+ .updateTable('nodes')
+ .set({
+ attributes: JSON.stringify(attributes),
+ state: encodedState,
+ updated_at: new Date().toISOString(),
+ updated_by: input.userId,
+ version_id: generateId(IdType.Version),
+ })
+ .where('id', '=', input.documentId)
+ .execute();
+
+ const hasChanges = result.length > 0 && result[0].numUpdatedRows > 0;
const changes: MutationChange[] = hasChanges
? [
{
diff --git a/desktop/src/main/handlers/mutations/message-create.ts b/desktop/src/main/handlers/mutations/message-create.ts
index 5ffe6858..0a0835ed 100644
--- a/desktop/src/main/handlers/mutations/message-create.ts
+++ b/desktop/src/main/handlers/mutations/message-create.ts
@@ -1,12 +1,9 @@
-import { mapContentsToEditorNodes } from '@/lib/editor';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
import { buildCreateNode } from '@/lib/nodes';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { MessageCreateMutationInput } from '@/operations/mutations/message-create';
-import { LocalNode } from '@/types/nodes';
-import { CreateNode } from '@/main/data/workspace/schema';
export class MessageCreateMutationHandler
implements MutationHandler<MessageCreateMutationInput>
@@ -19,38 +16,22 @@ export class MessageCreateMutationHandler
);
const id = generateId(IdType.Message);
- const editorNodes = mapContentsToEditorNodes(
- input.content.content,
- id,
- new Map<string, LocalNode>(),
- );
-
- const nodesToCreate: CreateNode[] = [
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Message,
- parentId: input.conversationId,
- },
- },
- input.userId,
- ),
- ];
-
- for (const editorNode of editorNodes) {
- nodesToCreate.push(
+ await workspaceDatabase
+ .insertInto('nodes')
+ .values(
buildCreateNode(
{
- id: editorNode.id,
- attributes: editorNode.attributes,
+ id: id,
+ attributes: {
+ type: NodeTypes.Message,
+ parentId: input.conversationId,
+ content: input.content.content,
+ },
},
input.userId,
),
- );
- }
-
- await workspaceDatabase.insertInto('nodes').values(nodesToCreate).execute();
+ )
+ .execute();
return {
output: {
diff --git a/desktop/src/main/handlers/queries/document-get.ts b/desktop/src/main/handlers/queries/document-get.ts
index 61a2cec0..9f6cd4b0 100644
--- a/desktop/src/main/handlers/queries/document-get.ts
+++ b/desktop/src/main/handlers/queries/document-get.ts
@@ -1,4 +1,3 @@
-import { sql } from 'kysely';
import { sha256 } from 'js-sha256';
import { isEqual } from 'lodash';
import {
@@ -14,9 +13,7 @@ import {
import { SelectNode } from '@/main/data/workspace/schema';
import { mapNode } from '@/lib/nodes';
-import { NodeTypes } from '@/lib/constants';
import { MutationChange } from '@/operations/mutations';
-import { mapNodesToContents } from '@/lib/editor';
export class DocumentGetQueryHandler
implements QueryHandler<DocumentGetQueryInput>
@@ -24,11 +21,11 @@ export class DocumentGetQueryHandler
public async handleQuery(
input: DocumentGetQueryInput,
): Promise<QueryResult<DocumentGetQueryInput>> {
- const rows = await this.fetchNodes(input);
+ const document = await this.fetchDocument(input);
return {
- output: this.buildOutput(input.documentId, rows),
+ output: this.buildOutput(document),
state: {
- rows,
+ document,
},
};
}
@@ -51,8 +48,8 @@ export class DocumentGetQueryHandler
};
}
- const rows = await this.fetchNodes(input);
- if (isEqual(rows, state.rows)) {
+ const document = await this.fetchDocument(input);
+ if (isEqual(document, state.document)) {
return {
hasChanges: false,
};
@@ -61,48 +58,31 @@ export class DocumentGetQueryHandler
return {
hasChanges: true,
result: {
- output: this.buildOutput(input.documentId, rows),
+ output: this.buildOutput(document),
state: {
- rows,
+ document,
},
},
};
}
- private async fetchNodes(
+ private async fetchDocument(
input: DocumentGetQueryInput,
- ): Promise<SelectNode[]> {
+ ): Promise<SelectNode | null> {
const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
input.userId,
);
- const query = sql<SelectNode>`
- WITH RECURSIVE document_nodes AS (
- SELECT *
- FROM nodes
- WHERE parent_id = ${input.documentId}
-
- UNION ALL
-
- SELECT child.*
- FROM nodes child
- INNER JOIN document_nodes parent ON child.parent_id = parent.id
- WHERE parent.type NOT IN (${NodeTypes.Page})
- )
- SELECT *
- FROM document_nodes
- `.compile(workspaceDatabase);
-
- const result = await workspaceDatabase.executeQuery(query);
- return result.rows;
+ return workspaceDatabase
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', input.documentId)
+ .executeTakeFirst();
}
- private buildOutput(
- documentId: string,
- rows: SelectNode[],
- ): DocumentGetQueryOutput {
- const nodes = rows.map(mapNode);
- const contents = mapNodesToContents(documentId, nodes);
+ private buildOutput(document: SelectNode | null): DocumentGetQueryOutput {
+ const node = mapNode(document);
+ const contents = node.attributes?.content ?? [];
if (!contents.length) {
contents.push({
type: 'paragraph',
diff --git a/desktop/src/main/handlers/queries/message-list.ts b/desktop/src/main/handlers/queries/message-list.ts
index b7b7ba58..dae48974 100644
--- a/desktop/src/main/handlers/queries/message-list.ts
+++ b/desktop/src/main/handlers/queries/message-list.ts
@@ -10,7 +10,7 @@ import { SelectNode } from '@/main/data/workspace/schema';
import { sql } from 'kysely';
import { NodeTypes } from '@/lib/constants';
import { MessageNode, MessageReactionCount } from '@/types/messages';
-import { buildNodeWithChildren, mapNode } from '@/lib/nodes';
+import { mapNode } from '@/lib/nodes';
import { UserNode } from '@/types/users';
import { compareString } from '@/lib/utils';
import { isEqual } from 'lodash';
@@ -89,15 +89,6 @@ export class MessageListQueryHandler
LIMIT ${sql.lit(input.count)}
OFFSET ${sql.lit(offset)}
),
- descendant_nodes AS (
- SELECT *
- FROM nodes
- WHERE parent_id IN (SELECT id FROM message_nodes)
- UNION ALL
- SELECT child.*
- FROM nodes child
- INNER JOIN descendant_nodes parent ON child.parent_id = parent.id
- ),
author_nodes AS (
SELECT *
FROM nodes
@@ -106,8 +97,6 @@ export class MessageListQueryHandler
all_nodes AS (
SELECT * FROM message_nodes
UNION ALL
- SELECT * FROM descendant_nodes
- UNION ALL
SELECT * FROM author_nodes
),
message_reaction_counts AS (
@@ -151,11 +140,6 @@ export class MessageListQueryHandler
private buildMessages = (rows: MessageRow[]): MessageNode[] => {
const messageRows = rows.filter((row) => row.type === NodeTypes.Message);
const authorRows = rows.filter((row) => row.type === NodeTypes.User);
- const contentNodes = rows
- .filter(
- (row) => row.type !== NodeTypes.User && row.type !== NodeTypes.Message,
- )
- .map(mapNode);
const messages: MessageNode[] = [];
const authorMap = new Map<string, UserNode>();
@@ -176,9 +160,6 @@ export class MessageListQueryHandler
for (const messageRow of messageRows) {
const messageNode = mapNode(messageRow);
const author = authorMap.get(messageNode.createdBy);
- const children = contentNodes
- .filter((n) => n.parentId === messageNode.id)
- .map((n) => buildNodeWithChildren(n, contentNodes));
const reactionCounts: MessageReactionCount[] = JSON.parse(
messageRow.reaction_counts,
@@ -194,7 +175,7 @@ export class MessageListQueryHandler
avatar: null,
},
- content: children,
+ content: messageNode.attributes.content,
reactionCounts,
userReactions,
};
diff --git a/desktop/src/renderer/components/messages/message.tsx b/desktop/src/renderer/components/messages/message.tsx
index 181ce58f..d2d9240a 100644
--- a/desktop/src/renderer/components/messages/message.tsx
+++ b/desktop/src/renderer/components/messages/message.tsx
@@ -130,7 +130,11 @@ export const Message = ({ message, previousMessage }: MessageProps) => {
</ul>
<div className="text-foreground">
{message.content.map((node) => (
- <NodeRenderer key={node.id} node={node} keyPrefix={node.id} />
+ <NodeRenderer
+ key={node.attrs?.id}
+ node={node}
+ keyPrefix={node.attrs?.id}
+ />
))}
</div>
{message.reactionCounts.length > 0 && (
diff --git a/desktop/src/renderer/editor/renderers/blockquote.tsx b/desktop/src/renderer/editor/renderers/blockquote.tsx
index fc003de1..1ee5a627 100644
--- a/desktop/src/renderer/editor/renderers/blockquote.tsx
+++ b/desktop/src/renderer/editor/renderers/blockquote.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface BlockquoteRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/bullet-list.tsx b/desktop/src/renderer/editor/renderers/bullet-list.tsx
index 94236c46..5fc4793e 100644
--- a/desktop/src/renderer/editor/renderers/bullet-list.tsx
+++ b/desktop/src/renderer/editor/renderers/bullet-list.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface BulletListRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/code-block.tsx b/desktop/src/renderer/editor/renderers/code-block.tsx
index 7abb5b39..6714c400 100644
--- a/desktop/src/renderer/editor/renderers/code-block.tsx
+++ b/desktop/src/renderer/editor/renderers/code-block.tsx
@@ -2,11 +2,11 @@ import React from 'react';
import { Icon } from '@/renderer/components/ui/icon';
import { defaultClasses } from '@/renderer/editor/classes';
-import { LocalNodeWithChildren } from '@/types/nodes';
import { highlightCode, languages } from '@/lib/lowlight';
+import { JSONContent } from '@tiptap/core';
interface CodeBlockRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/document.tsx b/desktop/src/renderer/editor/renderers/document.tsx
index f72146ce..27fd072a 100644
--- a/desktop/src/renderer/editor/renderers/document.tsx
+++ b/desktop/src/renderer/editor/renderers/document.tsx
@@ -1,9 +1,9 @@
import React from 'react';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface DocumentRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/heading1.tsx b/desktop/src/renderer/editor/renderers/heading1.tsx
index 012e735b..3d38a982 100644
--- a/desktop/src/renderer/editor/renderers/heading1.tsx
+++ b/desktop/src/renderer/editor/renderers/heading1.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface Heading1RendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/heading2.tsx b/desktop/src/renderer/editor/renderers/heading2.tsx
index 6348dc69..f87c6cc7 100644
--- a/desktop/src/renderer/editor/renderers/heading2.tsx
+++ b/desktop/src/renderer/editor/renderers/heading2.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface Heading2RendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/heading3.tsx b/desktop/src/renderer/editor/renderers/heading3.tsx
index f6331e2f..537509d8 100644
--- a/desktop/src/renderer/editor/renderers/heading3.tsx
+++ b/desktop/src/renderer/editor/renderers/heading3.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface Heading3RendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/list-item.tsx b/desktop/src/renderer/editor/renderers/list-item.tsx
index c53cd27e..45a0fb2a 100644
--- a/desktop/src/renderer/editor/renderers/list-item.tsx
+++ b/desktop/src/renderer/editor/renderers/list-item.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface ListItemRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/mark.tsx b/desktop/src/renderer/editor/renderers/mark.tsx
index 8ea473e6..dedc3711 100644
--- a/desktop/src/renderer/editor/renderers/mark.tsx
+++ b/desktop/src/renderer/editor/renderers/mark.tsx
@@ -1,16 +1,16 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
-import { NodeBlock } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface MarkRendererProps {
- node: NodeBlock;
+ node: JSONContent;
children: any | any[];
}
export const MarkRenderer = ({ node, children }: MarkRendererProps) => {
let result = <React.Fragment>{children}</React.Fragment>;
- if (node.marks) {
+ if (node.marks && node.marks.length > 0) {
node.marks.forEach((mark) => {
if (mark.type === 'bold') {
result = <strong>{result}</strong>;
diff --git a/desktop/src/renderer/editor/renderers/message.tsx b/desktop/src/renderer/editor/renderers/message.tsx
index fbe9ed3a..7feda466 100644
--- a/desktop/src/renderer/editor/renderers/message.tsx
+++ b/desktop/src/renderer/editor/renderers/message.tsx
@@ -1,9 +1,9 @@
import React from 'react';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface MessageRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/node-block.tsx b/desktop/src/renderer/editor/renderers/node-block.tsx
deleted file mode 100644
index 72900916..00000000
--- a/desktop/src/renderer/editor/renderers/node-block.tsx
+++ /dev/null
@@ -1,17 +0,0 @@
-import React from 'react';
-import { NodeBlock } from '@/types/nodes';
-import { TextRenderer } from '@/renderer/editor/renderers/text';
-import { MarkRenderer } from '@/renderer/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/renderer/editor/renderers/node-children.tsx b/desktop/src/renderer/editor/renderers/node-children.tsx
index b2271b08..e801b55b 100644
--- a/desktop/src/renderer/editor/renderers/node-children.tsx
+++ b/desktop/src/renderer/editor/renderers/node-children.tsx
@@ -1,11 +1,10 @@
import React from 'react';
-import { LocalNodeWithChildren } from '@/types/nodes';
-import { NodeBlockRenderer } from '@/renderer/editor/renderers/node-block';
import { NodeRenderer } from '@/renderer/editor/renderers/node';
+import { JSONContent } from '@tiptap/core';
interface NodeChildrenRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
@@ -13,20 +12,18 @@ export const NodeChildrenRenderer = ({
node,
keyPrefix,
}: NodeChildrenRendererProps) => {
- if (node.children && node.children.length > 0) {
- return (
- <React.Fragment>
- {node.children.map((nodeChild, index) => (
- <NodeRenderer node={nodeChild} keyPrefix={keyPrefix} key={index} />
- ))}
- </React.Fragment>
- );
+ if (!node.content || node.content.length === 0) {
+ return null;
}
return (
<React.Fragment>
- {node.attributes.content?.map((nodeBlock, index) => (
- <NodeBlockRenderer node={nodeBlock} keyPrefix={keyPrefix} key={index} />
+ {node.content.map((nodeChild, index) => (
+ <NodeRenderer
+ node={nodeChild}
+ keyPrefix={`${keyPrefix}-${index}`}
+ key={`${keyPrefix}-${index}`}
+ />
))}
</React.Fragment>
);
diff --git a/desktop/src/renderer/editor/renderers/node.tsx b/desktop/src/renderer/editor/renderers/node.tsx
index 43fcaaf4..9cbfa199 100644
--- a/desktop/src/renderer/editor/renderers/node.tsx
+++ b/desktop/src/renderer/editor/renderers/node.tsx
@@ -13,11 +13,12 @@ import { ParagraphRenderer } from '@/renderer/editor/renderers/paragraph';
import { TaskItemRenderer } from '@/renderer/editor/renderers/task-item';
import { TaskListRenderer } from '@/renderer/editor/renderers/task-list';
import { TextRenderer } from '@/renderer/editor/renderers/text';
-import { LocalNodeWithChildren } from '@/types/nodes';
import { match } from 'ts-pattern';
+import { JSONContent } from '@tiptap/core';
+import { MarkRenderer } from '@/renderer/editor/renderers/mark';
interface NodeRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
@@ -25,44 +26,50 @@ export const NodeRenderer = ({
node,
keyPrefix,
}: NodeRendererProps): ReactElement => {
- return match(node.type)
- .with('message', () => (
- <MessageRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('doc', () => <DocumentRenderer node={node} keyPrefix={keyPrefix} />)
- .with('text', () => <TextRenderer node={node} />)
- .with('paragraph', () => (
- <ParagraphRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('heading1', () => (
- <Heading1Renderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('heading2', () => (
- <Heading2Renderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('heading3', () => (
- <Heading3Renderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('blockquote', () => (
- <BlockquoteRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('bulletList', () => (
- <BulletListRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('orderedList', () => (
- <OrderedListRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('listItem', () => (
- <ListItemRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('taskList', () => (
- <TaskListRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('taskItem', () => (
- <TaskItemRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .with('codeBlock', () => (
- <CodeBlockRenderer node={node} keyPrefix={keyPrefix} />
- ))
- .otherwise(null);
+ return (
+ <MarkRenderer node={node}>
+ {match(node.type)
+ .with('message', () => (
+ <MessageRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('doc', () => (
+ <DocumentRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('text', () => <TextRenderer node={node} />)
+ .with('paragraph', () => (
+ <ParagraphRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('heading1', () => (
+ <Heading1Renderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('heading2', () => (
+ <Heading2Renderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('heading3', () => (
+ <Heading3Renderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('blockquote', () => (
+ <BlockquoteRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('bulletList', () => (
+ <BulletListRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('orderedList', () => (
+ <OrderedListRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('listItem', () => (
+ <ListItemRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('taskList', () => (
+ <TaskListRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('taskItem', () => (
+ <TaskItemRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .with('codeBlock', () => (
+ <CodeBlockRenderer node={node} keyPrefix={keyPrefix} />
+ ))
+ .otherwise(null)}
+ </MarkRenderer>
+ );
};
diff --git a/desktop/src/renderer/editor/renderers/ordered-list.tsx b/desktop/src/renderer/editor/renderers/ordered-list.tsx
index 25884026..6235a797 100644
--- a/desktop/src/renderer/editor/renderers/ordered-list.tsx
+++ b/desktop/src/renderer/editor/renderers/ordered-list.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface OrderedListRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/paragraph.tsx b/desktop/src/renderer/editor/renderers/paragraph.tsx
index 7a293d37..4b6c7f38 100644
--- a/desktop/src/renderer/editor/renderers/paragraph.tsx
+++ b/desktop/src/renderer/editor/renderers/paragraph.tsx
@@ -1,11 +1,11 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
import { cn } from '@/lib/utils';
+import { JSONContent } from '@tiptap/core';
interface ParagraphRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
@@ -13,10 +13,6 @@ export const ParagraphRenderer = ({
node,
keyPrefix,
}: ParagraphRendererProps) => {
- if (!node.attributes.content || node.attributes.content.length === 0) {
- return null;
- }
-
return (
<p className={cn(defaultClasses.paragraph, 'py-0.5')}>
<NodeChildrenRenderer node={node} keyPrefix={keyPrefix} />
diff --git a/desktop/src/renderer/editor/renderers/task-item.tsx b/desktop/src/renderer/editor/renderers/task-item.tsx
index dcee4aeb..63a33209 100644
--- a/desktop/src/renderer/editor/renderers/task-item.tsx
+++ b/desktop/src/renderer/editor/renderers/task-item.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface TaskItemRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/task-list.tsx b/desktop/src/renderer/editor/renderers/task-list.tsx
index 6f02ed29..fc000aeb 100644
--- a/desktop/src/renderer/editor/renderers/task-list.tsx
+++ b/desktop/src/renderer/editor/renderers/task-list.tsx
@@ -1,10 +1,10 @@
import React from 'react';
import { defaultClasses } from '@/renderer/editor/classes';
import { NodeChildrenRenderer } from '@/renderer/editor/renderers/node-children';
-import { LocalNodeWithChildren } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
interface TaskListRendererProps {
- node: LocalNodeWithChildren;
+ node: JSONContent;
keyPrefix: string | null;
}
diff --git a/desktop/src/renderer/editor/renderers/text.tsx b/desktop/src/renderer/editor/renderers/text.tsx
index a48ef6e5..b747f20b 100644
--- a/desktop/src/renderer/editor/renderers/text.tsx
+++ b/desktop/src/renderer/editor/renderers/text.tsx
@@ -1,6 +1,10 @@
import React from 'react';
-import { NodeBlock } from '@/types/nodes';
+import { JSONContent } from '@tiptap/core';
-export const TextRenderer = ({ node }: { node: NodeBlock }) => {
+interface TextRendererProps {
+ node: JSONContent;
+}
+
+export const TextRenderer = ({ node }: TextRendererProps) => {
return <React.Fragment>{node.text ?? ''}</React.Fragment>;
};
diff --git a/desktop/src/types/messages.ts b/desktop/src/types/messages.ts
index cef2eed2..38e73664 100644
--- a/desktop/src/types/messages.ts
+++ b/desktop/src/types/messages.ts
@@ -1,9 +1,9 @@
-import { LocalNodeWithChildren } from '@/types/nodes';
import { UserNode } from '@/types/users';
+import { JSONContent } from '@tiptap/core';
export type MessageNode = {
id: string;
- content: LocalNodeWithChildren[];
+ content: JSONContent[];
createdAt: string;
author: UserNode;
reactionCounts: MessageReactionCount[];
diff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.ts
index ab912583..bebc579b 100644
--- a/desktop/src/types/nodes.ts
+++ b/desktop/src/types/nodes.ts
@@ -1,3 +1,5 @@
+import { JSONContent } from '@tiptap/core';
+
export type LocalNode = {
id: string;
parentId: string | null;
@@ -19,7 +21,7 @@ export type LocalNodeAttributes = {
type: string;
parentId?: string | null;
index?: string | null;
- content?: NodeBlock[] | null;
+ content?: JSONContent[] | null;
[key: string]: any;
};
diff --git a/server/src/consumers/node-reaction-cdc.ts b/server/src/consumers/node-reaction-cdc.ts
index 8c8667c1..249cf31b 100644
--- a/server/src/consumers/node-reaction-cdc.ts
+++ b/server/src/consumers/node-reaction-cdc.ts
@@ -121,7 +121,7 @@ const getDeviceIds = async (workspaceId: string) => {
const mapNodeReaction = (reaction: NodeReactionCdcData): ServerNodeReaction => {
return {
nodeId: reaction.node_id,
- reactorId: reaction.actor_id,
+ actorId: reaction.actor_id,
reaction: reaction.reaction,
workspaceId: reaction.workspace_id,
createdAt: new Date(reaction.created_at),
diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.ts
index 0f6de5fd..d77f44c3 100644
--- a/server/src/routes/sync.ts
+++ b/server/src/routes/sync.ts
@@ -51,7 +51,7 @@ syncRouter.get(
(nodeReaction) => {
return {
nodeId: nodeReaction.node_id,
- reactorId: nodeReaction.actor_id,
+ actorId: nodeReaction.actor_id,
reaction: nodeReaction.reaction,
workspaceId: nodeReaction.workspace_id,
createdAt: nodeReaction.created_at,
diff --git a/server/src/types/nodes.ts b/server/src/types/nodes.ts
index 7a6c2a4d..2b86dcf9 100644
--- a/server/src/types/nodes.ts
+++ b/server/src/types/nodes.ts
@@ -39,7 +39,7 @@ export type ServerNodeCollaborator = {
export type ServerNodeReaction = {
nodeId: string;
- reactorId: string;
+ actorId: string;
reaction: string;
workspaceId: string;
createdAt: Date;