Skip to content

Commit

Permalink
fix: copy template content when creating controlled document (#6441)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Onnikov <[email protected]>
  • Loading branch information
aonnikov authored Aug 29, 2024
1 parent e072f05 commit 46ced93
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
18 changes: 17 additions & 1 deletion dev/doc-import-tool/src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import documents, {
import core, {
AttachedData,
BackupClient,
CollaborativeDoc,
Client as CoreClient,
Data,
MeasureContext,
Expand Down Expand Up @@ -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')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -160,7 +160,9 @@
docObject,
_space,
$locationStep.project,
$locationStep.parent
$locationStep.parent,
documents.class.ControlledDocument,
copyDocument
)
if (!success) {
Expand Down
21 changes: 18 additions & 3 deletions plugins/controlled-documents/src/docutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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,
Expand All @@ -109,7 +113,8 @@ export async function createControlledDocFromTemplate (
prefix,
seqNumber,
path,
docClass
docClass,
_copyContent
)
}

Expand All @@ -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

Expand Down Expand Up @@ -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 }
}

Expand Down

0 comments on commit 46ced93

Please sign in to comment.