refactor: note editor relationships (#1821)

This commit is contained in:
Mo
2022-10-18 08:59:24 -05:00
committed by GitHub
parent c83dc48d3f
commit 2b66ff82ee
28 changed files with 357 additions and 299 deletions

View File

@@ -1,55 +0,0 @@
import { DecryptedPayloadInterface } from './../../Abstract/Payload/Interfaces/DecryptedPayload'
import { ComponentContent } from '../../Syncable/Component/ComponentContent'
import { ComponentArea } from '@standardnotes/features'
import { ContentType } from '@standardnotes/common'
import { ComponentMutator, SNComponent } from '../../Syncable/Component'
import { CreateDecryptedItemFromPayload } from '../Item/ItemGenerator'
import { ImmutablePayloadCollection } from '../../Runtime/Collection/Payload/ImmutablePayloadCollection'
import { MutationType } from '../../Abstract/Item/Types/MutationType'
import { FullyFormedPayloadInterface } from '../../Abstract/Payload/Interfaces/UnionTypes'
import { isDecryptedPayload } from '../../Abstract/Payload'
import { SyncResolvedPayload } from '../../Runtime/Deltas/Utilities/SyncResolvedPayload'
export type AffectorFunction = (
basePayload: FullyFormedPayloadInterface,
duplicatePayload: FullyFormedPayloadInterface,
baseCollection: ImmutablePayloadCollection<FullyFormedPayloadInterface>,
) => SyncResolvedPayload[]
const NoteDuplicationAffectedPayloads: AffectorFunction = (
basePayload: FullyFormedPayloadInterface,
duplicatePayload: FullyFormedPayloadInterface,
baseCollection: ImmutablePayloadCollection<FullyFormedPayloadInterface>,
) => {
/** If note has editor, maintain editor relationship in duplicate note */
const components = baseCollection
.all(ContentType.Component)
.filter(isDecryptedPayload)
.map((payload) => {
return CreateDecryptedItemFromPayload<ComponentContent, SNComponent>(
payload as DecryptedPayloadInterface<ComponentContent>,
)
})
const editor = components
.filter((c) => c.area === ComponentArea.Editor)
.find((e) => {
return e.isExplicitlyEnabledForItem(basePayload.uuid)
})
if (!editor) {
return []
}
/** Modify the editor to include new note */
const mutator = new ComponentMutator(editor, MutationType.NoUpdateUserTimestamps)
mutator.associateWithItem(duplicatePayload.uuid)
const result = mutator.getResult() as SyncResolvedPayload
return [result]
}
export const AffectorMapping = {
[ContentType.Note]: NoteDuplicationAffectedPayloads,
} as Partial<Record<ContentType, AffectorFunction>>

View File

@@ -2,7 +2,6 @@ import { PayloadSource } from './../../Abstract/Payload/Types/PayloadSource'
import { extendArray, UuidGenerator } from '@standardnotes/utils'
import { ImmutablePayloadCollection } from '../../Runtime/Collection/Payload/ImmutablePayloadCollection'
import { ItemContent } from '../../Abstract/Content/ItemContent'
import { AffectorMapping } from './AffectorFunction'
import { PayloadsByUpdatingReferencingPayloadReferences } from './PayloadsByUpdatingReferencingPayloadReferences'
import { isDecryptedPayload } from '../../Abstract/Payload/Interfaces/TypeCheck'
import { FullyFormedPayloadInterface } from '../../Abstract/Payload/Interfaces/UnionTypes'
@@ -69,13 +68,5 @@ export function PayloadsByDuplicating<C extends ItemContent = ItemContent>(dto:
extendArray(results, updatedReferencing)
}
const affector = AffectorMapping[payload.content_type]
if (affector) {
const affected = affector(payload, copy, baseCollection)
if (affected) {
extendArray(results, affected)
}
}
return results
}