fix: copy template content when creating controlled document (#6441)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov 2024-08-29 19:20:32 +07:00 committed by GitHub
parent e072f05d97
commit 46ced93bc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 8 deletions

View File

@ -15,6 +15,7 @@ import documents, {
import core, {
AttachedData,
BackupClient,
CollaborativeDoc,
Client as CoreClient,
Data,
MeasureContext,
@ -114,7 +115,22 @@ async function createDocument (
console.log('Creating controlled doc from template')
const { success } = await createControlledDocFromTemplate(txops, templateId, docId, data, space, undefined, undefined)
const copyContent = async (source: CollaborativeDoc, target: CollaborativeDoc): Promise<void> => {
// intentionally left empty
// even though the template has some content, it won't be used
}
const { success } = await createControlledDocFromTemplate(
txops,
templateId,
docId,
data,
space,
undefined,
undefined,
documents.class.ControlledDocument,
copyContent
)
if (!success) {
throw new Error('Failed to create controlled document from template')
}

View File

@ -27,7 +27,7 @@
Ref,
SortingOrder
} from '@hcengineering/core'
import { Card, createQuery, getClient } from '@hcengineering/presentation'
import { Card, copyDocument, createQuery, getClient } from '@hcengineering/presentation'
import { createFocusManager, EditBox, FocusHandler } from '@hcengineering/ui'
import { ObjectBox } from '@hcengineering/view-resources'
import {
@ -91,7 +91,17 @@
return
}
await createControlledDocFromTemplate(client, templateId, id, object, space, undefined, undefined, documentClass)
await createControlledDocFromTemplate(
client,
templateId,
id,
object,
space,
undefined,
undefined,
documentClass,
copyDocument
)
dispatch('close', id)
}

View File

@ -24,7 +24,7 @@
type Data,
type Ref
} from '@hcengineering/core'
import { MessageBox, getClient } from '@hcengineering/presentation'
import { MessageBox, copyDocument, getClient } from '@hcengineering/presentation'
import {
AnySvelteComponent,
addNotification,
@ -160,7 +160,9 @@
docObject,
_space,
$locationStep.project,
$locationStep.parent
$locationStep.parent,
documents.class.ControlledDocument,
copyDocument
)
if (!success) {

View File

@ -40,6 +40,7 @@ import {
import documents from './plugin'
import { TEMPLATE_PREFIX } from './utils'
import { setPlatformStatus, unknownError } from '@hcengineering/platform'
async function getParentPath (client: TxOperations, parent: Ref<ProjectDocument>): Promise<Array<Ref<DocumentMeta>>> {
const parentDocObj = await client.findOne(documents.class.ProjectDocument, {
@ -71,7 +72,8 @@ export async function createControlledDocFromTemplate (
space: Ref<DocumentSpace>,
project: Ref<Project> | undefined,
parent: Ref<ProjectDocument> | undefined,
docClass: Ref<Class<ControlledDocument>> = documents.class.ControlledDocument
docClass: Ref<Class<ControlledDocument>> = documents.class.ControlledDocument,
copyContent: (source: CollaborativeDoc, target: CollaborativeDoc) => Promise<void> = async () => {}
): Promise<{ seqNumber: number, success: boolean }> {
if (templateId == null) {
return { seqNumber: -1, success: false }
@ -99,6 +101,8 @@ export async function createControlledDocFromTemplate (
const seqNumber = template.sequence + 1
const prefix = template.docPrefix
const _copyContent = (doc: CollaborativeDoc): Promise<void> => copyContent(template.content, doc)
return await createControlledDoc(
client,
templateId,
@ -109,7 +113,8 @@ export async function createControlledDocFromTemplate (
prefix,
seqNumber,
path,
docClass
docClass,
_copyContent
)
}
@ -123,7 +128,8 @@ async function createControlledDoc (
prefix: string,
seqNumber: number,
path: Ref<DocumentMeta>[] = [],
docClass: Ref<Class<ControlledDocument>> = documents.class.ControlledDocument
docClass: Ref<Class<ControlledDocument>> = documents.class.ControlledDocument,
copyContent: (doc: CollaborativeDoc) => Promise<void>
): Promise<{ seqNumber: number, success: boolean }> {
const projectId = project ?? documents.ids.NoProject
@ -185,6 +191,15 @@ async function createControlledDoc (
const success = await ops.commit()
if (success.result) {
try {
await copyContent(collaborativeDoc)
} catch (err) {
await setPlatformStatus(unknownError(err))
return { seqNumber, success: false }
}
}
return { seqNumber, success: success.result }
}