diff --git a/packages/api-client/src/client.ts b/packages/api-client/src/client.ts index d4b223ad34..a39c0bc8df 100644 --- a/packages/api-client/src/client.ts +++ b/packages/api-client/src/client.ts @@ -34,15 +34,22 @@ import { AttachedData, Mixin, MixinUpdate, - MixinData + MixinData, + generateId } from '@hcengineering/core' import client, { clientId } from '@hcengineering/client' import { addLocation, getResource } from '@hcengineering/platform' import { login, selectWorkspace } from './account' import { type ServerConfig, loadServerConfig } from './config' -import { type MarkupOperations, type MarkupFormat, type MarkupRef, createMarkupOperations } from './markup' -import { type PlatformClient, type ConnectOptions } from './types' +import { + type MarkupFormat, + type MarkupOperations, + type MarkupRef, + MarkupContent, + createMarkupOperations +} from './markup' +import { type PlatformClient, type ConnectOptions, WithMarkup } from './types' /** * Create platform client @@ -121,41 +128,62 @@ class PlatformClientImpl implements PlatformClient { await this.connection.close() } - // TxOperations + private async processMarkup(id: Ref, data: WithMarkup): Promise { + const result: any = {} + + for (const [key, value] of Object.entries(data)) { + if (value instanceof MarkupContent) { + result[key] = this.markup.uploadMarkup(id, key, value.content, value.kind) + } else { + result[key] = value + } + } + + return result as T + } + + // DocOperations async createDoc( _class: Ref>, space: Ref, - attributes: Data, + attributes: WithMarkup>, id?: Ref ): Promise> { - return await this.client.createDoc(_class, space, attributes, id) + id ??= generateId() + const data = await this.processMarkup>(id, attributes) + return await this.client.createDoc(_class, space, data, id) } async updateDoc( _class: Ref>, space: Ref, objectId: Ref, - operations: DocumentUpdate, + operations: WithMarkup>, retrieve?: boolean ): Promise { - return await this.client.updateDoc(_class, space, objectId, operations, retrieve) + const update = await this.processMarkup>(objectId, operations) + return await this.client.updateDoc(_class, space, objectId, update, retrieve) } async removeDoc(_class: Ref>, space: Ref, objectId: Ref): Promise { return await this.client.removeDoc(_class, space, objectId) } + // CollectionOperations + async addCollection( _class: Ref>, space: Ref, attachedTo: Ref, attachedToClass: Ref>, collection: Extract | string, - attributes: AttachedData

, + attributes: WithMarkup>, id?: Ref

): Promise> { - return await this.client.addCollection(_class, space, attachedTo, attachedToClass, collection, attributes, id) + id ??= generateId() + const data = await this.processMarkup>(id, attributes) + return await this.client.addCollection(_class, space, attachedTo, attachedToClass, collection, data, id) } async updateCollection( @@ -165,9 +193,10 @@ class PlatformClientImpl implements PlatformClient { attachedTo: Ref, attachedToClass: Ref>, collection: Extract | string, - operations: DocumentUpdate

, + operations: WithMarkup>, retrieve?: boolean ): Promise> { + const update = await this.processMarkup>(objectId, operations) return await this.client.updateCollection( _class, space, @@ -175,7 +204,7 @@ class PlatformClientImpl implements PlatformClient { attachedTo, attachedToClass, collection, - operations, + update, retrieve ) } @@ -191,14 +220,17 @@ class PlatformClientImpl implements PlatformClient { return await this.client.removeCollection(_class, space, objectId, attachedTo, attachedToClass, collection) } + // MixinOperations + async createMixin( objectId: Ref, objectClass: Ref>, objectSpace: Ref, mixin: Ref>, - attributes: MixinData + attributes: WithMarkup> ): Promise { - return await this.client.createMixin(objectId, objectClass, objectSpace, mixin, attributes) + const data = await this.processMarkup>(objectId, attributes) + return await this.client.createMixin(objectId, objectClass, objectSpace, mixin, data) } async updateMixin( @@ -206,9 +238,10 @@ class PlatformClientImpl implements PlatformClient { objectClass: Ref>, objectSpace: Ref, mixin: Ref>, - attributes: MixinUpdate + attributes: WithMarkup> ): Promise { - return await this.client.updateMixin(objectId, objectClass, objectSpace, mixin, attributes) + const update = await this.processMarkup>(objectId, attributes) + return await this.client.updateMixin(objectId, objectClass, objectSpace, mixin, update) } // Markup diff --git a/packages/api-client/src/markup/types.ts b/packages/api-client/src/markup/types.ts index cd1d16daf0..a641f9fd06 100644 --- a/packages/api-client/src/markup/types.ts +++ b/packages/api-client/src/markup/types.ts @@ -21,6 +21,24 @@ export type MarkupRef = CollaborativeDoc /** @public */ export type MarkupFormat = 'markup' | 'html' | 'markdown' +/** @public */ +export class MarkupContent { + constructor ( + readonly content: string, + readonly kind: MarkupFormat + ) {} +} + +/** @public */ +export function html (content: string): MarkupContent { + return new MarkupContent(content, 'html') +} + +/** @public */ +export function markdown (content: string): MarkupContent { + return new MarkupContent(content, 'markdown') +} + /** * Provides operations for managing markup (rich-text) content. * @public */ diff --git a/packages/api-client/src/types.ts b/packages/api-client/src/types.ts index 820ae9c48f..4a7cd0a66a 100644 --- a/packages/api-client/src/types.ts +++ b/packages/api-client/src/types.ts @@ -15,6 +15,7 @@ import { type ClientSocketFactory } from '@hcengineering/client' import { + CollaborativeDoc, type AttachedData, type AttachedDoc, type Class, @@ -34,7 +35,18 @@ import { type TxResult, type WithLookup } from '@hcengineering/core' -import { type MarkupOperations } from './markup' +import { type MarkupContent, type MarkupOperations } from './markup' + +type WithPropertyType = { + [P in keyof T]: T[P] extends X ? Y : T[P] +} + +/** @public */ +export type WithMarkup = WithPropertyType< +WithPropertyType, +CollaborativeDoc, +MarkupContent +> /** * Platform API client @@ -77,7 +89,7 @@ export interface DocOperations { createDoc: ( _class: Ref>, space: Ref, - attributes: Data, + attributes: WithMarkup>, id?: Ref ) => Promise> @@ -85,7 +97,7 @@ export interface DocOperations { _class: Ref>, space: Ref, objectId: Ref, - operations: DocumentUpdate, + operations: WithMarkup>, retrieve?: boolean ) => Promise @@ -102,7 +114,7 @@ export interface CollectionOperations { attachedTo: Ref, attachedToClass: Ref>, collection: Extract | string, - attributes: AttachedData

, + attributes: WithMarkup>, id?: Ref

) => Promise> @@ -113,7 +125,7 @@ export interface CollectionOperations { attachedTo: Ref, attachedToClass: Ref>, collection: Extract | string, - operations: DocumentUpdate

, + operations: WithMarkup>, retrieve?: boolean ) => Promise> @@ -136,7 +148,7 @@ export interface MixinOperations { objectClass: Ref>, objectSpace: Ref, mixin: Ref>, - attributes: MixinData + attributes: WithMarkup> ) => Promise updateMixin: ( @@ -144,7 +156,7 @@ export interface MixinOperations { objectClass: Ref>, objectSpace: Ref, mixin: Ref>, - attributes: MixinUpdate + attributes: WithMarkup> ) => Promise }