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
Fix null value in crdt and document updates handlers
Hakan Shehu19de9bf56cf5
diff --git a/apps/desktop/src/main/mutations/pages/page-content-update.ts b/apps/desktop/src/main/mutations/pages/page-content-update.tsindex bf86608a..3dd1a2d6 100644--- a/apps/desktop/src/main/mutations/pages/page-content-update.ts+++ b/apps/desktop/src/main/mutations/pages/page-content-update.ts@@ -1,4 +1,5 @@-import { Block, PageAttributes } from '@colanode/core';+import { PageAttributes } from '@colanode/core';+import { isEqual } from 'lodash-es';import { entryService } from '@/main/services/entry-service';import { MutationHandler } from '@/main/types';@@ -19,27 +20,47 @@ export class PageContentUpdateMutationHandlerinput.pageId,input.userId,(attributes) => {- const blocksMap = new Map<string, Block>();+ const indexMap = new Map<string, string>();if (attributes.content) {for (const [key, value] of Object.entries(attributes.content)) {- blocksMap.set(key, value);+ indexMap.set(key, value.index);}}- const blocks = mapContentsToBlocks(+ const beforeBlocks = mapContentsToBlocks(input.pageId,- input.content.content ?? [],- blocksMap+ input.before.content ?? [],+ indexMap);- attributes.content = blocks.reduce(- (acc, block) => {- acc[block.id] = block;- return acc;- },- {} as Record<string, Block>+ const afterBlocks = mapContentsToBlocks(+ input.pageId,+ input.after.content ?? [],+ indexMap);+ const content = attributes.content ?? {};+ for (const afterBlock of afterBlocks) {+ const beforeBlock = beforeBlocks.find(+ (block) => block.id === afterBlock.id+ );++ if (!isEqual(beforeBlock, afterBlock)) {+ content[afterBlock.id] = afterBlock;+ }+ }++ for (const beforeBlock of beforeBlocks) {+ const afterBlock = afterBlocks.find(+ (block) => block.id === beforeBlock.id+ );++ if (!afterBlock) {+ delete content[beforeBlock.id];+ }+ }++ attributes.content = content;return attributes;});diff --git a/apps/desktop/src/main/mutations/records/record-content-update.ts b/apps/desktop/src/main/mutations/records/record-content-update.tsindex da2f61a3..0e0a851b 100644--- a/apps/desktop/src/main/mutations/records/record-content-update.ts+++ b/apps/desktop/src/main/mutations/records/record-content-update.ts@@ -1,4 +1,5 @@-import { Block, RecordAttributes } from '@colanode/core';+import { RecordAttributes } from '@colanode/core';+import { isEqual } from 'lodash-es';import { entryService } from '@/main/services/entry-service';import { MutationHandler } from '@/main/types';@@ -19,27 +20,43 @@ export class RecordContentUpdateMutationHandlerinput.recordId,input.userId,(attributes) => {- const blocksMap = new Map<string, Block>();+ const indexMap = new Map<string, string>();if (attributes.content) {for (const [key, value] of Object.entries(attributes.content)) {- blocksMap.set(key, value);+ indexMap.set(key, value.index);}}- const blocks = mapContentsToBlocks(+ const beforeBlocks = mapContentsToBlocks(input.recordId,- input.content.content ?? [],- blocksMap+ input.before.content ?? [],+ indexMap);- attributes.content = blocks.reduce(- (acc, block) => {- acc[block.id] = block;- return acc;- },- {} as Record<string, Block>+ const afterBlocks = mapContentsToBlocks(+ input.recordId,+ input.after.content ?? [],+ indexMap);+ const content = attributes.content ?? {};+ for (const afterBlock of afterBlocks) {+ const beforeBlock = beforeBlocks.find(+ (block) => block.id === afterBlock.id+ );++ if (!isEqual(beforeBlock, afterBlock)) {+ content[afterBlock.id] = afterBlock;+ }+ }++ for (const beforeBlock of beforeBlocks) {+ if (!content[beforeBlock.id]) {+ delete content[beforeBlock.id];+ }+ }++ attributes.content = content;return attributes;});diff --git a/apps/desktop/src/renderer/components/documents/document-editor.tsx b/apps/desktop/src/renderer/components/documents/document-editor.tsxindex 1303f055..464f758e 100644--- a/apps/desktop/src/renderer/components/documents/document-editor.tsx+++ b/apps/desktop/src/renderer/components/documents/document-editor.tsx@@ -68,7 +68,7 @@ interface DocumentEditorProps {content: JSONContent;transactionId: string;canEdit: boolean;- onUpdate: (content: JSONContent) => void;+ onUpdate: (before: JSONContent, after: JSONContent) => void;autoFocus?: FocusPosition;}@@ -85,11 +85,18 @@ export const DocumentEditor = ({const hasPendingChanges = React.useRef(false);const transactionIdRef = React.useRef(transactionId);+ const contentRef = React.useRef(content);+const debouncedSave = React.useMemo(() =>debounce((content: JSONContent) => {+ const before = contentRef.current;+ const after = content;++ contentRef.current = content;hasPendingChanges.current = false;- onUpdate(content);++ onUpdate(before, after);}, 500),[onUpdate]);@@ -196,6 +203,7 @@ export const DocumentEditor = ({}transactionIdRef.current = transactionId;+ contentRef.current = content;}, [content, transactionId]);return (diff --git a/apps/desktop/src/renderer/components/documents/document.tsx b/apps/desktop/src/renderer/components/documents/document.tsxindex 8d832344..08636384 100644--- a/apps/desktop/src/renderer/components/documents/document.tsx+++ b/apps/desktop/src/renderer/components/documents/document.tsx@@ -10,7 +10,7 @@ interface DocumentProps {content?: Record<string, Block> | null;transactionId: string;canEdit: boolean;- onUpdate: (content: JSONContent) => void;+ onUpdate: (before: JSONContent, after: JSONContent) => void;autoFocus?: FocusPosition;}diff --git a/apps/desktop/src/renderer/components/pages/page-body.tsx b/apps/desktop/src/renderer/components/pages/page-body.tsxindex 390761c6..4a827998 100644--- a/apps/desktop/src/renderer/components/pages/page-body.tsx+++ b/apps/desktop/src/renderer/components/pages/page-body.tsx@@ -21,13 +21,14 @@ export const PageBody = ({ page, role }: PageBodyProps) => {const canEdit = hasEntryRole(role, 'editor');const handleUpdate = useCallback(- (content: JSONContent) => {+ (before: JSONContent, after: JSONContent) => {mutate({input: {type: 'page_content_update',userId: workspace.userId,pageId: page.id,- content,+ before,+ after,},onError(error) {toast({diff --git a/apps/desktop/src/renderer/components/records/record-body.tsx b/apps/desktop/src/renderer/components/records/record-body.tsxindex 0379b353..dc1940da 100644--- a/apps/desktop/src/renderer/components/records/record-body.tsx+++ b/apps/desktop/src/renderer/components/records/record-body.tsx@@ -33,13 +33,14 @@ export const RecordBody = ({ record, database, role }: RecordBodyProps) => {record.createdBy === workspace.userId || hasEntryRole(role, 'editor');const handleUpdate = useCallback(- (content: JSONContent) => {+ (before: JSONContent, after: JSONContent) => {mutate({input: {type: 'record_content_update',userId: workspace.userId,recordId: record.id,- content,+ before,+ after,},onError(error) {toast({diff --git a/apps/desktop/src/shared/lib/editor.ts b/apps/desktop/src/shared/lib/editor.tsindex 71d04d99..f88fa46b 100644--- a/apps/desktop/src/shared/lib/editor.ts+++ b/apps/desktop/src/shared/lib/editor.ts@@ -21,10 +21,10 @@ const leafBlockTypes = new Set([export const mapContentsToBlocks = (parentId: string,contents: JSONContent[],- blocksMap: Map<string, Block>+ indexMap: Map<string, string>): Block[] => {const blocks: Block[] = [];- mapAndPushContentsToBlocks(contents, parentId, blocks, blocksMap);+ mapAndPushContentsToBlocks(contents, parentId, blocks, indexMap);validateBlocksIndexes(blocks);return blocks;};@@ -33,13 +33,13 @@ const mapAndPushContentsToBlocks = (contents: JSONContent[] | null | undefined,parentId: string,blocks: Block[],- blocksMap: Map<string, Block>+ indexMap: Map<string, string>): void => {if (!contents) {return;}contents.map((content) => {- mapAndPushContentToBlock(content, parentId, blocks, blocksMap);+ mapAndPushContentToBlock(content, parentId, blocks, indexMap);});};@@ -47,14 +47,14 @@ const mapAndPushContentToBlock = (content: JSONContent,parentId: string,blocks: Block[],- blocksMap: Map<string, Block>+ indexMap: Map<string, string>): void => {if (!content.type) {throw new Error('Invalid content type');}const id = getIdFromContent(content);- const index = blocksMap.get(id)?.index;+ const index = indexMap.get(id);const attrs =(content.attrs &&Object.entries(content.attrs).filter(([key]) => key !== 'id')) ??@@ -75,7 +75,7 @@ const mapAndPushContentToBlock = (});if (!isLeafBlock && content.content) {- mapAndPushContentsToBlocks(content.content, id, blocks, blocksMap);+ mapAndPushContentsToBlocks(content.content, id, blocks, indexMap);}};diff --git a/apps/desktop/src/shared/mutations/pages/page-content-update.ts b/apps/desktop/src/shared/mutations/pages/page-content-update.tsindex 90308108..609f766c 100644--- a/apps/desktop/src/shared/mutations/pages/page-content-update.ts+++ b/apps/desktop/src/shared/mutations/pages/page-content-update.ts@@ -4,7 +4,8 @@ export type PageContentUpdateMutationInput = {type: 'page_content_update';userId: string;pageId: string;- content: JSONContent;+ before: JSONContent;+ after: JSONContent;};export type PageContentUpdateMutationOutput = {diff --git a/apps/desktop/src/shared/mutations/records/record-content-update.ts b/apps/desktop/src/shared/mutations/records/record-content-update.tsindex 7ad851a0..aa1d8116 100644--- a/apps/desktop/src/shared/mutations/records/record-content-update.ts+++ b/apps/desktop/src/shared/mutations/records/record-content-update.ts@@ -4,7 +4,8 @@ export type RecordContentUpdateMutationInput = {type: 'record_content_update';userId: string;recordId: string;- content: JSONContent;+ before: JSONContent;+ after: JSONContent;};export type RecordContentUpdateMutationOutput = {diff --git a/packages/core/src/registry/page.ts b/packages/core/src/registry/page.tsindex 3f69f6ed..419b7396 100644--- a/packages/core/src/registry/page.ts+++ b/packages/core/src/registry/page.ts@@ -1,7 +1,6 @@import { z } from 'zod';import { blockSchema } from './block';-import { entryRoleEnum } from './core';export const pageAttributesSchema = z.object({type: z.literal('page'),@@ -9,7 +8,6 @@ export const pageAttributesSchema = z.object({avatar: z.string().nullable().optional(),parentId: z.string(),content: z.record(blockSchema),- collaborators: z.record(z.string(), entryRoleEnum).nullable().optional(),});export type PageAttributes = z.infer<typeof pageAttributesSchema>;diff --git a/packages/crdt/src/index.ts b/packages/crdt/src/index.tsindex 92fe4ec5..90e98cef 100644--- a/packages/crdt/src/index.ts+++ b/packages/crdt/src/index.ts@@ -108,7 +108,11 @@ export class YDoc {) {for (const [key, value] of Object.entries(attributes)) {if (value === null) {- yMap.set(key, null);+ const currentValue = yMap.get(key);+ if (currentValue !== null) {+ yMap.set(key, null);+ }+continue;}@@ -191,8 +195,12 @@ export class YDoc {const item = value[i];if (item === null) {- yArray.delete(i, 1);- yArray.insert(i, [null]);+ const currentItem = yArray.get(i);+ if (currentItem !== null) {+ yArray.delete(i, 1);+ yArray.insert(i, [null]);+ }+continue;}@@ -264,7 +272,11 @@ export class YDoc {const valueSchema = this.extractType(schemaField.valueSchema, record);for (const [key, value] of Object.entries(record)) {if (value === null) {- yMap.set(key, null);+ const currentValue = yMap.get(key);+ if (currentValue !== null) {+ yMap.set(key, null);+ }+continue;}