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
Index node texts for searching
Hakan Shehu2ce11577aad2
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.tsindex 19722433..8509472c 100644--- a/apps/desktop/src/main/data/workspace/migrations.ts+++ b/apps/desktop/src/main/data/workspace/migrations.ts@@ -237,65 +237,15 @@ const createNodeNamesTable: Migration = {},};-const createNodeInsertNameTrigger: Migration = {+const createNodeTextsTable: Migration = {up: async (db) => {await sql`- CREATE TRIGGER node_insert_name- AFTER INSERT ON nodes- WHEN json_type(NEW.attributes, '$.name') = 'text'- BEGIN- INSERT INTO node_names (id, name)- VALUES (- NEW.id,- json_extract(NEW.attributes, '$.name')- );- END;- `.execute(db);- },- down: async (db) => {- await sql`- DROP TRIGGER node_insert_name;- `.execute(db);- },-};--const createNodeUpdateNameTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_update_name- AFTER UPDATE ON nodes- WHEN json_extract(OLD.attributes, '$.name') IS NOT json_extract(NEW.attributes, '$.name')- BEGIN- DELETE FROM node_names WHERE id = OLD.id;-- INSERT INTO node_names (id, name)- SELECT- NEW.id,- json_extract(NEW.attributes, '$.name')- WHERE json_type(NEW.attributes, '$.name') = 'text';- END;- `.execute(db);- },- down: async (db) => {- await sql`- DROP TRIGGER node_update_name;- `.execute(db);- },-};--const createNodeDeleteNameTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_delete_name- AFTER DELETE ON nodes- BEGIN- DELETE FROM node_names WHERE id = OLD.id;- END;+ CREATE VIRTUAL TABLE node_texts USING fts5(id UNINDEXED, text);`.execute(db);},down: async (db) => {await sql`- DROP TRIGGER node_delete_name;+ DROP TABLE IF EXISTS node_texts;`.execute(db);},};@@ -325,8 +275,6 @@ export const workspaceDatabaseMigrations: Record<string, Migration> = {'00007_create_interaction_events_table': createInteractionEventsTable,'00008_create_node_paths_table': createNodePathsTable,'00009_create_node_names_table': createNodeNamesTable,- '00010_create_node_insert_name_trigger': createNodeInsertNameTrigger,- '00011_create_node_update_name_trigger': createNodeUpdateNameTrigger,- '00012_create_node_delete_name_trigger': createNodeDeleteNameTrigger,- '00013_create_cursors_table': createCursorsTable,+ '00010_create_node_texts_table': createNodeTextsTable,+ '00011_create_cursors_table': createCursorsTable,};diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.tsindex 634516dc..3dd064e0 100644--- a/apps/desktop/src/main/data/workspace/schema.ts+++ b/apps/desktop/src/main/data/workspace/schema.ts@@ -119,6 +119,24 @@ export type SelectInteractionEvent = Selectable<InteractionEventTable>;export type CreateInteractionEvent = Insertable<InteractionEventTable>;export type UpdateInteractionEvent = Updateable<InteractionEventTable>;+interface NodeNameTable {+ id: ColumnType<string, string, never>;+ name: ColumnType<string, string, string>;+}++export type SelectNodeName = Selectable<NodeNameTable>;+export type CreateNodeName = Insertable<NodeNameTable>;+export type UpdateNodeName = Updateable<NodeNameTable>;++interface NodeTextTable {+ id: ColumnType<string, string, never>;+ text: ColumnType<string, string, string>;+}++export type SelectNodeText = Selectable<NodeTextTable>;+export type CreateNodeText = Insertable<NodeTextTable>;+export type UpdateNodeText = Updateable<NodeTextTable>;+interface CursorTable {type: ColumnType<SyncConsumerType, SyncConsumerType, never>;value: ColumnType<bigint, bigint, bigint>;@@ -135,5 +153,7 @@ export interface WorkspaceDatabaseSchema {downloads: DownloadTable;interactions: InteractionTable;interaction_events: InteractionEventTable;+ node_names: NodeNameTable;+ node_texts: NodeTextTable;cursors: CursorTable;}diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.tsindex 9b71f4ea..445e8242 100644--- a/apps/desktop/src/main/services/node-service.ts+++ b/apps/desktop/src/main/services/node-service.ts@@ -11,7 +11,6 @@ import {ServerUpdateTransaction,} from '@colanode/core';import { decodeState, YDoc } from '@colanode/crdt';-import { sql } from 'kysely';import { createDebugger } from '@/main/debugger';import { SelectWorkspace } from '@/main/data/app/schema';@@ -86,7 +85,7 @@ class NodeService {await workspaceDatabase.transaction().execute(async (transaction) => {for (const inputItem of inputs) {- const model = registry.getNodeModel(inputItem.attributes.type);+ const model = registry.getModel(inputItem.attributes.type);if (!model.schema.safeParse(inputItem.attributes).success) {throw new Error('Invalid attributes');}@@ -121,6 +120,9 @@ class NodeService {const createdAt = new Date().toISOString();const transactionId = generateId(IdType.Transaction);+ const name = model.getName(inputItem.id, inputItem.attributes);+ const text = model.getText(inputItem.id, inputItem.attributes);+const createdNode = await transaction.insertInto('nodes').returningAll()@@ -188,6 +190,20 @@ class NodeService {createdDownloads.push(createdDownload);}++ if (name) {+ await transaction+ .insertInto('node_names')+ .values({ id: inputItem.id, name })+ .execute();+ }++ if (text) {+ await transaction+ .insertInto('node_texts')+ .values({ id: inputItem.id, text })+ .execute();+ }}});@@ -301,7 +317,7 @@ class NodeService {const updatedAt = new Date().toISOString();const updatedAttributes = updater(node.attributes);- const model = registry.getNodeModel(node.type);+ const model = registry.getModel(node.type);if (!model.schema.safeParse(updatedAttributes).success) {return 'invalid_attributes';}@@ -326,6 +342,8 @@ class NodeService {}const update = ydoc.updateAttributes(model.schema, updatedAttributes);+ const name = model.getName(nodeId, updatedAttributes);+ const text = model.getText(nodeId, updatedAttributes);const { updatedNode, createdTransaction } = await workspaceDatabase.transaction()@@ -363,6 +381,28 @@ class NodeService {return { updatedNode, createdTransaction };}+ if (name !== undefined) {+ await trx.deleteFrom('node_names').where('id', '=', nodeId).execute();+ }++ if (name) {+ await trx+ .insertInto('node_names')+ .values({ id: nodeId, name })+ .execute();+ }++ if (text !== undefined) {+ await trx.deleteFrom('node_texts').where('id', '=', nodeId).execute();+ }++ if (text) {+ await trx+ .insertInto('node_texts')+ .values({ id: nodeId, text })+ .execute();+ }+return {updatedNode: undefined,createdTransaction: undefined,@@ -425,7 +465,7 @@ class NodeService {throw new Error('Node not found');}- const model = registry.getNodeModel(node.type);+ const model = registry.getModel(node.type);const context = new NodeMutationContext(workspace.account_id,workspace.workspace_id,@@ -471,6 +511,9 @@ class NodeService {.where('node_id', '=', nodeId).execute();+ await trx.deleteFrom('node_names').where('id', '=', nodeId).execute();+ await trx.deleteFrom('node_texts').where('id', '=', nodeId).execute();+const createdTransaction = await trx.insertInto('transactions').returningAll()@@ -560,10 +603,23 @@ class NodeService {ydoc.applyUpdate(transaction.data);}+ const model = registry.getModel(firstTransaction.nodeType);+ if (!model) {+ return false;+ }+const attributes = ydoc.getAttributes<NodeAttributes>();const attributesJson = JSON.stringify(attributes);+ const name = model.getName(nodeId, attributes);+ const text = model.getText(nodeId, attributes);await workspaceDatabase.transaction().execute(async (trx) => {+ await trx.deleteFrom('nodes').where('id', '=', nodeId).execute();+ await trx+ .deleteFrom('transactions')+ .where('node_id', '=', nodeId)+ .execute();+await trx.insertInto('nodes').values({@@ -581,14 +637,6 @@ class NodeService {: null,transaction_id: lastTransaction.id,})- .onConflict((oc) =>- oc.columns(['id']).doUpdateSet({- attributes: attributesJson,- updated_at: lastTransaction.createdAt,- updated_by: lastTransaction.createdBy,- transaction_id: lastTransaction.id,- })- ).execute();await trx@@ -609,14 +657,24 @@ class NodeService {server_created_at: t.serverCreatedAt,})))- .onConflict((oc) =>- oc.columns(['id']).doUpdateSet({- status: 'synced',- version: sql`excluded.version`,- server_created_at: sql`excluded.server_created_at`,- })- ).execute();++ await trx.deleteFrom('node_names').where('id', '=', nodeId).execute();+ await trx.deleteFrom('node_texts').where('id', '=', nodeId).execute();++ if (name) {+ await trx+ .insertInto('node_names')+ .values({ id: nodeId, name })+ .execute();+ }++ if (text) {+ await trx+ .insertInto('node_texts')+ .values({ id: nodeId, text })+ .execute();+ }});return true;@@ -668,10 +726,17 @@ class NodeService {return;}+ const model = registry.getModel(transaction.nodeType);+ if (!model) {+ return;+ }+const ydoc = new YDoc();ydoc.applyUpdate(transaction.data);- const attributes = ydoc.getAttributes();+ const attributes = ydoc.getAttributes<NodeAttributes>();+ const name = model.getName(transaction.nodeId, attributes);+ const text = model.getText(transaction.nodeId, attributes);const { createdNode } = await workspaceDatabase.transaction()@@ -705,6 +770,20 @@ class NodeService {}).execute();+ if (name) {+ await trx+ .insertInto('node_names')+ .values({ id: transaction.nodeId, name })+ .execute();+ }++ if (text) {+ await trx+ .insertInto('node_texts')+ .values({ id: transaction.nodeId, text })+ .execute();+ }+return { createdNode };});@@ -781,6 +860,11 @@ class NodeService {return;}+ const model = registry.getModel(transaction.nodeType);+ if (!model) {+ return;+ }+const previousTransactions = await workspaceDatabase.selectFrom('transactions').selectAll()@@ -797,7 +881,10 @@ class NodeService {}ydoc.applyUpdate(transaction.data);- const attributes = ydoc.getAttributes();+ const attributes = ydoc.getAttributes<NodeAttributes>();++ const name = model.getName(transaction.nodeId, attributes);+ const text = model.getText(transaction.nodeId, attributes);const { updatedNode } = await workspaceDatabase.transaction()@@ -831,6 +918,34 @@ class NodeService {}).execute();+ if (name !== undefined) {+ await trx+ .deleteFrom('node_names')+ .where('id', '=', transaction.nodeId)+ .execute();+ }++ if (name) {+ await trx+ .insertInto('node_names')+ .values({ id: transaction.nodeId, name })+ .execute();+ }++ if (text !== undefined) {+ await trx+ .deleteFrom('node_texts')+ .where('id', '=', transaction.nodeId)+ .execute();+ }++ if (text) {+ await trx+ .insertInto('node_texts')+ .values({ id: transaction.nodeId, text })+ .execute();+ }+return { updatedNode };});@@ -899,6 +1014,16 @@ class NodeService {.where('node_id', '=', transaction.nodeId).execute();+ await trx+ .deleteFrom('node_names')+ .where('id', '=', transaction.nodeId)+ .execute();++ await trx+ .deleteFrom('node_texts')+ .where('id', '=', transaction.nodeId)+ .execute();+const nodeRow = await trx.deleteFrom('nodes').returningAll()diff --git a/apps/server/src/services/node-service.ts b/apps/server/src/services/node-service.tsindex 22b84023..1cfdfa27 100644--- a/apps/server/src/services/node-service.ts+++ b/apps/server/src/services/node-service.ts@@ -55,7 +55,7 @@ class NodeService {public async createNode(input: CreateNodeInput): Promise<CreateNodeOutput | null> {- const model = registry.getNodeModel(input.attributes.type);+ const model = registry.getModel(input.attributes.type);const ydoc = new YDoc();const update = ydoc.updateAttributes(model.schema, input.attributes);const attributes = ydoc.getAttributes<NodeAttributes>();@@ -164,7 +164,7 @@ class NodeService {return { type: 'error', output: null };}- const model = registry.getNodeModel(updatedAttributes.type);+ const model = registry.getModel(updatedAttributes.type);const update = ydoc.updateAttributes(model.schema, updatedAttributes);const attributes = ydoc.getAttributes<NodeAttributes>();@@ -271,7 +271,7 @@ class NodeService {ancestors);- const model = registry.getNodeModel(attributes.type);+ const model = registry.getModel(attributes.type);if (!model.schema.safeParse(attributes).success) {return null;}@@ -381,7 +381,7 @@ class NodeService {const attributes = ydoc.getAttributes<NodeAttributes>();const attributesJson = JSON.stringify(attributes);- const model = registry.getNodeModel(attributes.type);+ const model = registry.getModel(attributes.type);if (!model.schema.safeParse(attributes).success) {return { type: 'error', output: null };}@@ -517,7 +517,7 @@ class NodeService {return null;}- const model = registry.getNodeModel(node.attributes.type);+ const model = registry.getModel(node.attributes.type);const context = new NodeMutationContext(workspaceUser.account_id,workspaceUser.workspace_id,diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 68f09798..5d35c1cb 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -28,3 +28,4 @@ export * from './types/nodes';export * from './types/servers';export * from './types/sync';export * from './types/workspaces';+export * from './lib/blocks';diff --git a/packages/core/src/lib/blocks.ts b/packages/core/src/lib/blocks.tsnew file mode 100644index 00000000..07e8bfc3--- /dev/null+++ b/packages/core/src/lib/blocks.ts@@ -0,0 +1,46 @@+import { Block } from '../registry/block';++const collectText = (+ blockId: string,+ blocks: Record<string, Block>+): string => {+ const texts: string[] = [];++ // Extract text from the current block's leaf nodes+ const block = blocks[blockId];+ if (block) {+ let text = '';+ if (block.content) {+ for (const leaf of block.content) {+ if (leaf.text) {+ text += leaf.text;+ }+ }+ }+ texts.push(text);+ }++ // Find children and sort them by their index to maintain a stable order+ const children = Object.values(blocks)+ .filter((child) => child.parentId === blockId)+ .sort((a, b) => a.index.localeCompare(b.index));++ // Recursively collect text from children+ for (const child of children) {+ texts.push(collectText(child.id, blocks));+ }++ return texts.join('\n');+};++export const extractText = (+ nodeId: string,+ blocks: Record<string, Block> | undefined | null+): string | null => {+ if (!blocks) {+ return null;+ }++ const result = collectText(nodeId, blocks);+ return result.length > 0 ? result : null;+};diff --git a/packages/core/src/registry/channel.ts b/packages/core/src/registry/channel.tsindex be570aee..645026e8 100644--- a/packages/core/src/registry/channel.ts+++ b/packages/core/src/registry/channel.ts@@ -1,7 +1,7 @@import { isEqual } from 'lodash-es';import { z } from 'zod';-import { CollaborationModel, NodeModel, nodeRoleEnum } from './core';+import { NodeModel, nodeRoleEnum } from './core';export const channelAttributesSchema = z.object({type: z.literal('channel'),@@ -16,6 +16,16 @@ export type ChannelAttributes = z.infer<typeof channelAttributesSchema>;export const channelModel: NodeModel = {type: 'channel',schema: channelAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'channel') {+ return undefined;+ }++ return attributes.name;+ },+ getText: () => {+ return undefined;+ },canCreate: async (context, attributes) => {if (attributes.type !== 'channel') {return false;@@ -52,16 +62,3 @@ export const channelModel: NodeModel = {return context.hasEditorAccess();},};--export const channelCollaborationAttributesSchema = z.object({- type: z.literal('channel'),-});--export type ChannelCollaborationAttributes = z.infer<- typeof channelCollaborationAttributesSchema->;--export const channelCollaborationModel: CollaborationModel = {- type: 'channel',- schema: channelCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/chat.ts b/packages/core/src/registry/chat.tsindex 83772696..1d25961a 100644--- a/packages/core/src/registry/chat.ts+++ b/packages/core/src/registry/chat.ts@@ -1,6 +1,6 @@import { z } from 'zod';-import { CollaborationModel, NodeModel, nodeRoleEnum } from './core';+import { NodeModel, nodeRoleEnum } from './core';export const chatAttributesSchema = z.object({type: z.literal('chat'),@@ -13,6 +13,12 @@ export type ChatAttributes = z.infer<typeof chatAttributesSchema>;export const chatModel: NodeModel = {type: 'chat',schema: chatAttributesSchema,+ getName: () => {+ return undefined;+ },+ getText: () => {+ return undefined;+ },canCreate: async (context, attributes) => {if (attributes.type !== 'chat') {return false;@@ -36,16 +42,3 @@ export const chatModel: NodeModel = {return false;},};--export const chatCollaborationAttributesSchema = z.object({- type: z.literal('chat'),-});--export type ChatCollaborationAttributes = z.infer<- typeof chatCollaborationAttributesSchema->;--export const chatCollaborationModel: CollaborationModel = {- type: 'chat',- schema: chatCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/core.ts b/packages/core/src/registry/core.tsindex ffef6ab3..a2da3191 100644--- a/packages/core/src/registry/core.ts+++ b/packages/core/src/registry/core.ts@@ -72,9 +72,12 @@ export interface NodeModel {attributes: NodeAttributes) => Promise<boolean>;canDelete: (context: NodeMutationContext, node: Node) => Promise<boolean>;+ getName: (+ id: string,+ attributes: NodeAttributes+ ) => string | null | undefined;+ getText: (+ id: string,+ attributes: NodeAttributes+ ) => string | null | undefined;}--export type CollaborationModel = {- type: string;- schema: ZodSchema;-};diff --git a/packages/core/src/registry/database.ts b/packages/core/src/registry/database.tsindex 78ea9e56..5b6fddd6 100644--- a/packages/core/src/registry/database.ts+++ b/packages/core/src/registry/database.ts@@ -1,7 +1,7 @@import { isEqual } from 'lodash-es';import { z } from 'zod';-import { CollaborationModel, NodeModel, nodeRoleEnum } from './core';+import { NodeModel, nodeRoleEnum } from './core';import { fieldAttributesSchema } from './fields';export const viewFieldAttributesSchema = z.object({@@ -84,6 +84,16 @@ export type ViewType = 'table' | 'board' | 'calendar';export const databaseModel: NodeModel = {type: 'database',schema: databaseAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'database') {+ return undefined;+ }++ return attributes.name;+ },+ getText: () => {+ return undefined;+ },canCreate: async (context, attributes) => {if (attributes.type !== 'database') {return false;@@ -111,16 +121,3 @@ export const databaseModel: NodeModel = {return context.hasEditorAccess();},};--export const databaseCollaborationAttributesSchema = z.object({- type: z.literal('database'),-});--export type DatabaseCollaborationAttributes = z.infer<- typeof databaseCollaborationAttributesSchema->;--export const databaseCollaborationModel: CollaborationModel = {- type: 'database',- schema: databaseCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/file.ts b/packages/core/src/registry/file.tsindex bb40faec..88468279 100644--- a/packages/core/src/registry/file.ts+++ b/packages/core/src/registry/file.ts@@ -1,6 +1,6 @@import { z } from 'zod';-import { CollaborationModel, NodeModel } from './core';+import { NodeModel } from './core';export const fileAttributesSchema = z.object({type: z.literal('file'),@@ -22,6 +22,16 @@ export type FileAttributes = z.infer<typeof fileAttributesSchema>;export const fileModel: NodeModel = {type: 'file',schema: fileAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'file') {+ return undefined;+ }++ return attributes.name;+ },+ getText: () => {+ return undefined;+ },canCreate: async (_, __) => {return true;},@@ -32,16 +42,3 @@ export const fileModel: NodeModel = {return true;},};--export const fileCollaborationAttributesSchema = z.object({- type: z.literal('file'),-});--export type FileCollaborationAttributes = z.infer<- typeof fileCollaborationAttributesSchema->;--export const fileCollaborationModel: CollaborationModel = {- type: 'file',- schema: fileCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/folder.ts b/packages/core/src/registry/folder.tsindex 89a32f67..90c202b0 100644--- a/packages/core/src/registry/folder.ts+++ b/packages/core/src/registry/folder.ts@@ -1,7 +1,7 @@import { isEqual } from 'lodash-es';import { z } from 'zod';-import { CollaborationModel, NodeModel, nodeRoleEnum } from './core';+import { NodeModel, nodeRoleEnum } from './core';export const folderAttributesSchema = z.object({type: z.literal('folder'),@@ -16,6 +16,16 @@ export type FolderAttributes = z.infer<typeof folderAttributesSchema>;export const folderModel: NodeModel = {type: 'folder',schema: folderAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'folder') {+ return undefined;+ }++ return attributes.name;+ },+ getText: () => {+ return undefined;+ },canCreate: async (context, attributes) => {if (attributes.type !== 'folder') {return false;@@ -43,16 +53,3 @@ export const folderModel: NodeModel = {return context.hasEditorAccess();},};--export const folderCollaborationAttributesSchema = z.object({- type: z.literal('folder'),-});--export type FolderCollaborationAttributes = z.infer<- typeof folderCollaborationAttributesSchema->;--export const folderCollaborationModel: CollaborationModel = {- type: 'folder',- schema: folderCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/index.ts b/packages/core/src/registry/index.tsindex 61690ca9..12e90ff5 100644--- a/packages/core/src/registry/index.ts+++ b/packages/core/src/registry/index.ts@@ -1,70 +1,15 @@-import {- ChannelAttributes,- ChannelCollaborationAttributes,- channelCollaborationModel,- channelModel,-} from './channel';-import {- ChatAttributes,- ChatCollaborationAttributes,- chatCollaborationModel,- chatModel,-} from './chat';-import { CollaborationModel, NodeModel } from './core';-import {- DatabaseAttributes,- DatabaseCollaborationAttributes,- databaseCollaborationModel,- databaseModel,-} from './database';-import {- FileAttributes,- FileCollaborationAttributes,- fileCollaborationModel,- fileModel,-} from './file';-import {- FolderAttributes,- FolderCollaborationAttributes,- folderCollaborationModel,- folderModel,-} from './folder';-import {- MessageAttributes,- MessageCollaborationAttributes,- messageCollaborationModel,- messageModel,-} from './message';-import {- PageAttributes,- PageCollaborationAttributes,- pageCollaborationModel,- pageModel,-} from './page';-import {- RecordAttributes,- RecordCollaborationAttributes,- recordCollaborationModel,- recordModel,-} from './record';-import {- SpaceAttributes,- SpaceCollaborationAttributes,- spaceCollaborationModel,- spaceModel,-} from './space';-import {- UserAttributes,- UserCollaborationAttributes,- userCollaborationModel,- userModel,-} from './user';-import {- WorkspaceAttributes,- WorkspaceCollaborationAttributes,- workspaceCollaborationModel,- workspaceModel,-} from './workspace';+import { ChannelAttributes, channelModel } from './channel';+import { ChatAttributes, chatModel } from './chat';+import { NodeModel } from './core';+import { DatabaseAttributes, databaseModel } from './database';+import { FileAttributes, fileModel } from './file';+import { FolderAttributes, folderModel } from './folder';+import { MessageAttributes, messageModel } from './message';+import { PageAttributes, pageModel } from './page';+import { RecordAttributes, recordModel } from './record';+import { SpaceAttributes, spaceModel } from './space';+import { UserAttributes, userModel } from './user';+import { WorkspaceAttributes, workspaceModel } from './workspace';type NodeBase = {id: string;@@ -90,111 +35,56 @@ export type ChannelNode = NodeBase & {attributes: ChannelAttributes;};-export type ChannelCollaboration = CollaborationBase & {- type: 'channel';- attributes: ChannelCollaborationAttributes;-};-export type ChatNode = NodeBase & {type: 'chat';attributes: ChatAttributes;};-export type ChatCollaboration = CollaborationBase & {- type: 'chat';- attributes: ChatCollaborationAttributes;-};-export type DatabaseNode = NodeBase & {type: 'database';attributes: DatabaseAttributes;};-export type DatabaseCollaboration = CollaborationBase & {- type: 'database';- attributes: DatabaseCollaborationAttributes;-};-export type FileNode = NodeBase & {type: 'file';attributes: FileAttributes;};-export type FileCollaboration = CollaborationBase & {- type: 'file';- attributes: FileCollaborationAttributes;-};-export type FolderNode = NodeBase & {type: 'folder';attributes: FolderAttributes;};-export type FolderCollaboration = CollaborationBase & {- type: 'folder';- attributes: FolderCollaborationAttributes;-};-export type MessageNode = NodeBase & {type: 'message';attributes: MessageAttributes;};-export type MessageCollaboration = CollaborationBase & {- type: 'message';- attributes: MessageCollaborationAttributes;-};-export type PageNode = NodeBase & {type: 'page';attributes: PageAttributes;};-export type PageCollaboration = CollaborationBase & {- type: 'page';- attributes: PageCollaborationAttributes;-};-export type RecordNode = NodeBase & {type: 'record';attributes: RecordAttributes;};-export type RecordCollaboration = CollaborationBase & {- type: 'record';- attributes: RecordCollaborationAttributes;-};-export type SpaceNode = NodeBase & {type: 'space';attributes: SpaceAttributes;};-export type SpaceCollaboration = CollaborationBase & {- type: 'space';- attributes: SpaceCollaborationAttributes;-};-export type UserNode = NodeBase & {type: 'user';attributes: UserAttributes;};-export type UserCollaboration = CollaborationBase & {- type: 'user';- attributes: UserCollaborationAttributes;-};-export type WorkspaceNode = NodeBase & {type: 'workspace';attributes: WorkspaceAttributes;};-export type WorkspaceCollaboration = CollaborationBase & {- type: 'workspace';- attributes: WorkspaceCollaborationAttributes;-};-export type NodeType =| 'channel'| 'chat'@@ -234,82 +124,25 @@ export type Node =| UserNode| WorkspaceNode;-export type CollaborationAttributes =- | UserCollaborationAttributes- | SpaceCollaborationAttributes- | DatabaseCollaborationAttributes- | ChannelCollaborationAttributes- | ChatCollaborationAttributes- | FileCollaborationAttributes- | FolderCollaborationAttributes- | MessageCollaborationAttributes- | PageCollaborationAttributes- | RecordCollaborationAttributes- | WorkspaceCollaborationAttributes;--export type Collaboration =- | UserCollaboration- | SpaceCollaboration- | DatabaseCollaboration- | ChannelCollaboration- | ChatCollaboration- | FileCollaboration- | FolderCollaboration- | MessageCollaboration- | PageCollaboration- | RecordCollaboration- | WorkspaceCollaboration;-class Registry {- private nodeModels: Map<string, NodeModel> = new Map();- private collaborationModels: Map<string, CollaborationModel> = new Map();+ private models: Map<string, NodeModel> = new Map();constructor() {- this.nodeModels.set('channel', channelModel);- this.collaborationModels.set('channel', channelCollaborationModel);-- this.nodeModels.set('chat', chatModel);- this.collaborationModels.set('chat', chatCollaborationModel);-- this.nodeModels.set('database', databaseModel);- this.collaborationModels.set('database', databaseCollaborationModel);-- this.nodeModels.set('file', fileModel);- this.collaborationModels.set('file', fileCollaborationModel);-- this.nodeModels.set('folder', folderModel);- this.collaborationModels.set('folder', folderCollaborationModel);-- this.nodeModels.set('message', messageModel);- this.collaborationModels.set('message', messageCollaborationModel);-- this.nodeModels.set('page', pageModel);- this.collaborationModels.set('page', pageCollaborationModel);-- this.nodeModels.set('record', recordModel);- this.collaborationModels.set('record', recordCollaborationModel);-- this.nodeModels.set('space', spaceModel);- this.collaborationModels.set('space', spaceCollaborationModel);-- this.nodeModels.set('user', userModel);- this.collaborationModels.set('user', userCollaborationModel);-- this.nodeModels.set('workspace', workspaceModel);- this.collaborationModels.set('workspace', workspaceCollaborationModel);- }-- getNodeModel(type: string): NodeModel {- const model = this.nodeModels.get(type);- if (!model) {- throw new Error(`Model for type ${type} not found`);- }-- return model;+ this.models.set('channel', channelModel);+ this.models.set('chat', chatModel);+ this.models.set('database', databaseModel);+ this.models.set('file', fileModel);+ this.models.set('folder', folderModel);+ this.models.set('message', messageModel);+ this.models.set('page', pageModel);+ this.models.set('record', recordModel);+ this.models.set('space', spaceModel);+ this.models.set('user', userModel);+ this.models.set('workspace', workspaceModel);}- getCollaborationModel(type: string): CollaborationModel {- const model = this.collaborationModels.get(type);+ getModel(type: string): NodeModel {+ const model = this.models.get(type);if (!model) {throw new Error(`Model for type ${type} not found`);}diff --git a/packages/core/src/registry/message.ts b/packages/core/src/registry/message.tsindex b265ff1e..cde1dac3 100644--- a/packages/core/src/registry/message.ts+++ b/packages/core/src/registry/message.ts@@ -2,7 +2,9 @@ import { isEqual } from 'lodash-es';import { z } from 'zod';import { blockSchema } from './block';-import { CollaborationModel, NodeModel } from './core';+import { NodeModel } from './core';++import { extractText } from '../lib/blocks';const standardMessageAttributesSchema = z.object({type: z.literal('message'),@@ -31,6 +33,16 @@ export type MessageAttributes = z.infer<typeof messageAttributesSchema>;export const messageModel: NodeModel = {type: 'message',schema: messageAttributesSchema,+ getName: () => {+ return undefined;+ },+ getText: (id, attributes) => {+ if (attributes.type !== 'message') {+ return undefined;+ }++ return extractText(id, attributes.content);+ },canCreate: async (context, attributes) => {if (attributes.type !== 'message') {return false;@@ -61,16 +73,3 @@ export const messageModel: NodeModel = {return context.hasAdminAccess();},};--export const messageCollaborationAttributesSchema = z.object({- type: z.literal('message'),-});--export type MessageCollaborationAttributes = z.infer<- typeof messageCollaborationAttributesSchema->;--export const messageCollaborationModel: CollaborationModel = {- type: 'message',- schema: messageCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/page.ts b/packages/core/src/registry/page.tsindex ff4d944f..836b5a67 100644--- a/packages/core/src/registry/page.ts+++ b/packages/core/src/registry/page.ts@@ -2,7 +2,9 @@ import { isEqual } from 'lodash-es';import { z } from 'zod';import { blockSchema } from './block';-import { CollaborationModel, NodeModel, nodeRoleEnum } from './core';+import { NodeModel, nodeRoleEnum } from './core';++import { extractText } from '../lib/blocks';export const pageAttributesSchema = z.object({type: z.literal('page'),@@ -18,6 +20,20 @@ export type PageAttributes = z.infer<typeof pageAttributesSchema>;export const pageModel: NodeModel = {type: 'page',schema: pageAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'page') {+ return undefined;+ }++ return attributes.name;+ },+ getText: (id, attributes) => {+ if (attributes.type !== 'page') {+ return undefined;+ }++ return extractText(id, attributes.content);+ },canCreate: async (context, attributes) => {if (attributes.type !== 'page') {return false;@@ -45,15 +61,3 @@ export const pageModel: NodeModel = {return context.hasEditorAccess();},};-export const pageCollaborationAttributesSchema = z.object({- type: z.literal('page'),-});--export type PageCollaborationAttributes = z.infer<- typeof pageCollaborationAttributesSchema->;--export const pageCollaborationModel: CollaborationModel = {- type: 'page',- schema: pageCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/record.ts b/packages/core/src/registry/record.tsindex 09c19f85..72a60541 100644--- a/packages/core/src/registry/record.ts+++ b/packages/core/src/registry/record.ts@@ -1,9 +1,11 @@import { z } from 'zod';import { blockSchema } from './block';-import { CollaborationModel, NodeModel } from './core';+import { NodeModel } from './core';import { fieldValueSchema } from './fields';+import { extractText } from '../lib/blocks';+export const recordAttributesSchema = z.object({type: z.literal('record'),parentId: z.string(),@@ -19,6 +21,20 @@ export type RecordAttributes = z.infer<typeof recordAttributesSchema>;export const recordModel: NodeModel = {type: 'record',schema: recordAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'record') {+ return undefined;+ }++ return attributes.name;+ },+ getText: (id, attributes) => {+ if (attributes.type !== 'record') {+ return undefined;+ }++ return extractText(id, attributes.content);+ },canCreate: async (context, attributes) => {if (attributes.type !== 'record') {return false;@@ -49,16 +65,3 @@ export const recordModel: NodeModel = {return context.hasEditorAccess();},};--export const recordCollaborationAttributesSchema = z.object({- type: z.literal('record'),-});--export type RecordCollaborationAttributes = z.infer<- typeof recordCollaborationAttributesSchema->;--export const recordCollaborationModel: CollaborationModel = {- type: 'record',- schema: recordCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/space.ts b/packages/core/src/registry/space.tsindex c2864095..f76064e1 100644--- a/packages/core/src/registry/space.ts+++ b/packages/core/src/registry/space.ts@@ -1,6 +1,6 @@import { z } from 'zod';-import { CollaborationModel, NodeModel, nodeRoleEnum } from './core';+import { NodeModel, nodeRoleEnum } from './core';export const spaceAttributesSchema = z.object({type: z.literal('space'),@@ -16,6 +16,16 @@ export type SpaceAttributes = z.infer<typeof spaceAttributesSchema>;export const spaceModel: NodeModel = {type: 'space',schema: spaceAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'space') {+ return undefined;+ }++ return attributes.name;+ },+ getText: () => {+ return undefined;+ },canCreate: async (context, __) => {if (context.workspaceRole === 'guest' || context.workspaceRole === 'none') {return false;@@ -30,16 +40,3 @@ export const spaceModel: NodeModel = {return context.hasAdminAccess();},};--export const spaceCollaborationAttributesSchema = z.object({- type: z.literal('space'),-});--export type SpaceCollaborationAttributes = z.infer<- typeof spaceCollaborationAttributesSchema->;--export const spaceCollaborationModel: CollaborationModel = {- type: 'space',- schema: spaceCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/user.ts b/packages/core/src/registry/user.tsindex d415ae49..6cc89583 100644--- a/packages/core/src/registry/user.ts+++ b/packages/core/src/registry/user.ts@@ -1,6 +1,6 @@import { z } from 'zod';-import { CollaborationModel, NodeModel } from './core';+import { NodeModel } from './core';export const userAttributesSchema = z.object({type: z.literal('user'),@@ -17,26 +17,23 @@ export type UserAttributes = z.infer<typeof userAttributesSchema>;export const userModel: NodeModel = {type: 'user',schema: userAttributesSchema,+ getName: (_, attributes) => {+ if (attributes.type !== 'user') {+ return undefined;+ }++ return attributes.name;+ },+ getText: () => {+ return undefined;+ },canCreate: async (_, __) => {- return true;+ return false;},canUpdate: async (_, __, ___) => {- return true;+ return false;},canDelete: async (_, __) => {- return true;+ return false;},};--export const userCollaborationAttributesSchema = z.object({- type: z.literal('user'),-});--export type UserCollaborationAttributes = z.infer<- typeof userCollaborationAttributesSchema->;--export const userCollaborationModel: CollaborationModel = {- type: 'user',- schema: userCollaborationAttributesSchema,-};diff --git a/packages/core/src/registry/workspace.ts b/packages/core/src/registry/workspace.tsindex 13578a7c..6bc53569 100644--- a/packages/core/src/registry/workspace.ts+++ b/packages/core/src/registry/workspace.ts@@ -1,6 +1,6 @@import { z } from 'zod';-import { CollaborationModel, NodeModel } from './core';+import { NodeModel } from './core';export const workspaceAttributesSchema = z.object({type: z.literal('workspace'),@@ -15,26 +15,19 @@ export type WorkspaceAttributes = z.infer<typeof workspaceAttributesSchema>;export const workspaceModel: NodeModel = {type: 'workspace',schema: workspaceAttributesSchema,+ getName: () => {+ return undefined;+ },+ getText: () => {+ return undefined;+ },canCreate: async (_, __) => {- return true;+ return false;},canUpdate: async (_, __, ___) => {- return true;+ return false;},canDelete: async (_, __) => {- return true;+ return false;},};--export const workspaceCollaborationAttributesSchema = z.object({- type: z.literal('workspace'),-});--export type WorkspaceCollaborationAttributes = z.infer<- typeof workspaceCollaborationAttributesSchema->;--export const workspaceCollaborationModel: CollaborationModel = {- type: 'workspace',- schema: workspaceCollaborationAttributesSchema,-};