Handler RefTo to display presenter (#547)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2021-12-06 22:15:53 +07:00 committed by GitHub
parent 696463712b
commit 570f373af7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 17 deletions

View File

@ -15,13 +15,13 @@
-->
<script lang="ts">
import type { AttachedDoc, AttachedDoc, Doc } from '@anticrm/core'
import type { AttachedDoc, Class, Doc, Ref } from '@anticrm/core'
import core from '@anticrm/core'
import { getResource } from '@anticrm/platform'
import type { AnySvelteComponent } from '@anticrm/ui'
import { CircleButton, Label } from '@anticrm/ui'
import view from '@anticrm/view'
import { getClient } from '../utils'
import { getAttributePresenterClass, getClient } from '../utils'
// export let _class: Ref<Class<Doc>>
export let key: string
@ -34,13 +34,13 @@
const _class = object._class
const client = getClient()
const hierarchy = client.getHierarchy()
const attribute = hierarchy.getAttribute(_class, key)
const typeClassId = attribute?.type._class
$: attribute = hierarchy.getAttribute(_class, key)
$: typeClassId = (attribute !== undefined) ? getAttributePresenterClass(attribute) : undefined
let editor: Promise<AnySvelteComponent> | undefined
if (typeClassId !== undefined) {
$: if (typeClassId !== undefined) {
const typeClass = hierarchy.getClass(typeClassId)
const editorMixin = hierarchy.as(typeClass, view.mixin.AttributeEditor)
editor = getResource(editorMixin.editor)

View File

@ -15,11 +15,11 @@
-->
<script lang="ts">
import type { Ref, Class, Doc } from '@anticrm/core'
import type { Class, Doc, Ref } from '@anticrm/core'
import { getResource } from '@anticrm/platform'
import type { AnySvelteComponent } from '@anticrm/ui'
import { getClient } from '../utils'
import view from '@anticrm/view'
import { getAttributePresenterClass, getClient } from '../utils'
export let _class: Ref<Class<Doc>>
export let key: string
@ -29,19 +29,19 @@
const client = getClient()
const hierarchy = client.getHierarchy()
const attribute = hierarchy.getAttribute(_class, key)
const typeClassId = attribute?.type._class
$: attribute = hierarchy.getAttribute(_class, key)
$: typeClassId = (attribute !== undefined) ? getAttributePresenterClass(attribute) : undefined
let editor: Promise<AnySvelteComponent> | undefined
if (typeClassId !== undefined) {
$: if (typeClassId !== undefined) {
const typeClass = hierarchy.getClass(typeClassId)
const editorMixin = hierarchy.as(typeClass, view.mixin.AttributeEditor)
editor = getResource(editorMixin.editor)
}
function onChange(value: any) {
function onChange (value: any) {
const doc = object as Doc
client.updateDoc(_class, doc.space, doc._id, { [key]: value })
}

View File

@ -16,8 +16,8 @@
import { onDestroy } from 'svelte'
import { Doc, Ref, Class, DocumentQuery, FindOptions, Client, Hierarchy, Tx, getCurrentAccount, ModelDb, TxResult } from '@anticrm/core'
import { TxOperations } from '@anticrm/core'
import { Doc, Ref, Class, DocumentQuery, FindOptions, Client, Hierarchy, Tx, getCurrentAccount, ModelDb, TxResult, TxOperations, AnyAttribute, RefTo } from '@anticrm/core'
import core from '@anticrm/core'
import { LiveQuery as LQ } from '@anticrm/query'
import { getMetadata } from '@anticrm/platform'
@ -79,3 +79,14 @@ export function getFileUrl(file: string): string {
const url = `${uploadUrl}?file=${file}&token=${token}`
return url
}
/**
* @public
*/
export function getAttributePresenterClass (attribute: AnyAttribute): Ref<Class<Doc>> {
let attrClass = attribute.type._class
if (attrClass === core.class.RefTo) {
attrClass = (attribute.type as RefTo<Doc>).to
}
return attrClass
}

View File

@ -14,13 +14,13 @@
// limitations under the License.
//
import type { Class, Client, Doc, FindOptions, FindResult, Obj, Ref, AttachedDoc, TxOperations, Collection } from '@anticrm/core'
import core, { Class, Client, Doc, FindOptions, FindResult, Obj, Ref, AttachedDoc, TxOperations, Collection } from '@anticrm/core'
import type { IntlString } from '@anticrm/platform'
import { getResource } from '@anticrm/platform'
import { getAttributePresenterClass } from '@anticrm/presentation'
import type { AnyComponent } from '@anticrm/ui'
import type { Action, ActionTarget, BuildModelOptions } from '@anticrm/view'
import view, { AttributeModel } from '@anticrm/view'
import core from '@anticrm/core'
/**
* @public
@ -46,7 +46,7 @@ export async function getObjectPresenter (client: Client, _class: Ref<Class<Obj>
async function getAttributePresenter (client: Client, _class: Ref<Class<Obj>>, key: string, preserveKey: string): Promise<AttributeModel> {
const attribute = client.getHierarchy().getAttribute(_class, key)
let attrClass = attribute.type._class
let attrClass = getAttributePresenterClass(attribute)
const clazz = client.getHierarchy().getClass(attrClass)
let presenterMixin = client.getHierarchy().as(clazz, view.mixin.AttributePresenter)
let parent = clazz.extends
@ -152,4 +152,4 @@ export async function deleteObject (client: Client & TxOperations, object: Doc)
} else {
client.removeDoc(object._class, object.space, object._id).catch(err => console.error(err))
}
}
}