Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

refactor(drawing): extract sheet copy plan (#7547)

siambbb57a47ee59
diff --git a/packages/drawing/src/index.ts b/packages/drawing/src/index.ts
index 580b535bc2..e4dde8c007 100644
--- a/packages/drawing/src/index.ts
+++ b/packages/drawing/src/index.ts
@@ -35,8 +35,9 @@ export type {
export { IDrawingManagerService } from './services/drawing-manager.service';
export { ImageIoService } from './services/image-io-impl.service';
export { URLImageService } from './services/url-image.service';
-export { createDrawingCopyPlan, DRAWING_COPY_CONTEXT_KEY, DRAWING_GROUPABLE_TYPES, getOrCreateDrawingCopyPlan, isGroupableDrawingType } from './utils/drawing-group';
-export type { ICreateDrawingCopyPlanOptions, IDrawingCopyPlan } from './utils/drawing-group';
+export { createDrawingCopyPlan, DRAWING_COPY_CONTEXT_KEY, getOrCreateDrawingCopyPlan } from './utils/drawing-copy-plan';
+export type { ICreateDrawingCopyPlanOptions, IDrawingCopyPlan } from './utils/drawing-copy-plan';
+export { DRAWING_GROUPABLE_TYPES, isGroupableDrawingType } from './utils/drawing-group';
export { getDrawingShapeKeyByDrawingSearch } from './utils/get-image-shape-key';
export { getImageSize } from './utils/get-image-size';
export { resolveDrawingRotateEnabled } from './utils/rotate-enabled';
diff --git a/packages/drawing/src/services/__tests__/drawing-manager-impl.service.spec.ts b/packages/drawing/src/services/__tests__/drawing-manager-impl.service.spec.ts
index ba8d351fce..b85ee730d5 100644
--- a/packages/drawing/src/services/__tests__/drawing-manager-impl.service.spec.ts
+++ b/packages/drawing/src/services/__tests__/drawing-manager-impl.service.spec.ts
@@ -17,7 +17,6 @@
import type { IDrawingParam, IDrawingSearch } from '@univerjs/core';
import { BooleanNumber, DrawingTypeEnum, Injector } from '@univerjs/core';
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import { createDrawingCopyPlan, getOrCreateDrawingCopyPlan, isGroupableDrawingType } from '../../utils/drawing-group';
import { UnitDrawingService } from '../drawing-manager-impl.service';
const unitId = 'unit';
@@ -189,128 +188,6 @@ describe('UnitDrawingService', () => {
]);
});
- it('creates one copy id map for group children and chart drawings', () => {
- let nextId = 0;
- const plan = createDrawingCopyPlan([
- createDrawing('group', { drawingType: DrawingTypeEnum.DRAWING_GROUP }),
- createDrawing('image-child', { groupId: 'group' }),
- createDrawing('chart-child', { drawingType: DrawingTypeEnum.DRAWING_CHART, groupId: 'group' }),
- ], {
- unitId,
- sourceSubUnitId: subUnitId,
- targetSubUnitId: 'copied-sheet',
- generateId: () => `copy-${nextId++}`,
- });
-
- expect(plan.idMap.size).toBe(3);
- expect(plan.idMap.get('group')).toBe('copy-0');
- expect(plan.idMap.get('image-child')).toBe('copy-1');
- expect(plan.idMap.get('chart-child')).toBe('copy-2');
- expect(plan.drawings).toEqual([
- expect.objectContaining({ drawingId: 'copy-0', subUnitId: 'copied-sheet' }),
- expect.objectContaining({ drawingId: 'copy-1', groupId: 'copy-0', subUnitId: 'copied-sheet' }),
- expect.objectContaining({ drawingId: 'copy-2', groupId: 'copy-0', subUnitId: 'copied-sheet' }),
- ]);
- });
-
- it('keeps copied drawing unchanged when the id map cannot resolve its current id', () => {
- let readCount = 0;
- const dynamicDrawing = {
- ...createDrawing('registered-id'),
- get drawingId() {
- readCount += 1;
- return readCount <= 2 ? 'registered-id' : 'unmapped-id';
- },
- } as IDrawingParam;
-
- const plan = createDrawingCopyPlan([dynamicDrawing], {
- unitId,
- sourceSubUnitId: subUnitId,
- targetSubUnitId: 'copied-sheet',
- generateId: () => 'copy-0',
- });
-
- expect(plan.idMap.get('registered-id')).toBe('copy-0');
- expect(plan.drawings[0]).toEqual(expect.objectContaining({
- drawingId: 'unmapped-id',
- subUnitId,
- }));
- });
-
- it('creates uncached copy plans and reuses cached complete copy plans', () => {
- let nextId = 0;
- const options = {
- unitId,
- sourceSubUnitId: subUnitId,
- targetSubUnitId: 'copied-sheet',
- generateId: () => `copy-${nextId++}`,
- };
- const child = createDrawing('child', { groupId: 'missing-group' });
- const uncachedPlan = getOrCreateDrawingCopyPlan(undefined, [child], options);
-
- expect(isGroupableDrawingType(DrawingTypeEnum.DRAWING_TABLE)).toBe(false);
- expect(uncachedPlan.drawings[0]).toEqual(expect.objectContaining({
- drawingId: 'copy-0',
- subUnitId: 'copied-sheet',
- }));
- expect(uncachedPlan.drawings[0].groupId).toBeUndefined();
-
- const copyContext = new Map<string, unknown>();
- const cachedPlan = getOrCreateDrawingCopyPlan(copyContext, [child], options);
-
- expect(getOrCreateDrawingCopyPlan(copyContext, [child], options)).toBe(cachedPlan);
- });
-
- it('extends a cached copy plan with the default id generator when no generator is provided', () => {
- const options = {
- unitId,
- sourceSubUnitId: subUnitId,
- targetSubUnitId: 'copied-sheet',
- };
- const copyContext = new Map<string, unknown>();
- const firstPlan = getOrCreateDrawingCopyPlan(copyContext, [createDrawing('child-1')], options);
-
- const extendedPlan = getOrCreateDrawingCopyPlan(copyContext, [
- createDrawing('child-1'),
- createDrawing('child-2'),
- ], options);
-
- expect(extendedPlan).toBe(firstPlan);
- expect(extendedPlan.idMap.has('child-2')).toBe(true);
- expect(extendedPlan.drawings.map((drawing) => drawing.subUnitId)).toEqual(['copied-sheet', 'copied-sheet']);
- });
-
- it('extends a cached copy plan when a later caller provides more drawing graph nodes', () => {
- let nextId = 0;
- const copyContext = new Map<string, unknown>();
- const options = {
- unitId,
- sourceSubUnitId: subUnitId,
- targetSubUnitId: 'copied-sheet',
- generateId: () => `copy-${nextId++}`,
- };
- const chart = createDrawing('chart-child', { drawingType: DrawingTypeEnum.DRAWING_CHART, groupId: 'group' });
- const firstPlan = getOrCreateDrawingCopyPlan(copyContext, [chart], options);
-
- expect(firstPlan.drawings[0].groupId).toBeUndefined();
-
- const extendedPlan = getOrCreateDrawingCopyPlan(copyContext, [
- createDrawing('group', { drawingType: DrawingTypeEnum.DRAWING_GROUP }),
- createDrawing('image-child', { groupId: 'group' }),
- chart,
- ], options);
-
- expect(extendedPlan).toBe(firstPlan);
- expect(extendedPlan.idMap.get('chart-child')).toBe('copy-0');
- expect(extendedPlan.idMap.get('group')).toBe('copy-1');
- expect(extendedPlan.idMap.get('image-child')).toBe('copy-2');
- expect(extendedPlan.drawings).toEqual([
- expect.objectContaining({ drawingId: 'copy-1', subUnitId: 'copied-sheet' }),
- expect.objectContaining({ drawingId: 'copy-2', groupId: 'copy-1', subUnitId: 'copied-sheet' }),
- expect.objectContaining({ drawingId: 'copy-0', groupId: 'copy-1', subUnitId: 'copied-sheet' }),
- ]);
- });
-
it('should manage focus, refresh and visibility notifications', () => {
const focused: IDrawingParam[][] = [];
const refreshed: IDrawingParam[][] = [];
diff --git a/packages/drawing/src/utils/__tests__/drawing-copy-plan.spec.ts b/packages/drawing/src/utils/__tests__/drawing-copy-plan.spec.ts
new file mode 100644
index 0000000000..1ff0422157
--- /dev/null
+++ b/packages/drawing/src/utils/__tests__/drawing-copy-plan.spec.ts
@@ -0,0 +1,156 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IDrawingParam } from '@univerjs/core';
+import { DrawingTypeEnum } from '@univerjs/core';
+import { describe, expect, it } from 'vitest';
+import { createDrawingCopyPlan, getOrCreateDrawingCopyPlan } from '../drawing-copy-plan';
+
+const unitId = 'unit';
+const subUnitId = 'subUnit';
+
+function createDrawing(drawingId: string, overrides: Partial<IDrawingParam> = {}): IDrawingParam {
+ return {
+ unitId,
+ subUnitId,
+ drawingId,
+ drawingType: DrawingTypeEnum.DRAWING_IMAGE,
+ ...overrides,
+ };
+}
+
+describe('drawing copy plan', () => {
+ it('creates one copy id map for group children and chart drawings', () => {
+ let nextId = 0;
+ const plan = createDrawingCopyPlan([
+ createDrawing('group', { drawingType: DrawingTypeEnum.DRAWING_GROUP }),
+ createDrawing('image-child', { groupId: 'group' }),
+ createDrawing('chart-child', { drawingType: DrawingTypeEnum.DRAWING_CHART, groupId: 'group' }),
+ ], {
+ unitId,
+ sourceSubUnitId: subUnitId,
+ targetSubUnitId: 'copied-sheet',
+ generateId: () => `copy-${nextId++}`,
+ });
+
+ expect(plan.idMap.size).toBe(3);
+ expect(plan.idMap.get('group')).toBe('copy-0');
+ expect(plan.idMap.get('image-child')).toBe('copy-1');
+ expect(plan.idMap.get('chart-child')).toBe('copy-2');
+ expect(plan.drawings).toEqual([
+ expect.objectContaining({ drawingId: 'copy-0', subUnitId: 'copied-sheet' }),
+ expect.objectContaining({ drawingId: 'copy-1', groupId: 'copy-0', subUnitId: 'copied-sheet' }),
+ expect.objectContaining({ drawingId: 'copy-2', groupId: 'copy-0', subUnitId: 'copied-sheet' }),
+ ]);
+ });
+
+ it('keeps copied drawing unchanged when the id map cannot resolve its current id', () => {
+ let readCount = 0;
+ const dynamicDrawing = {
+ ...createDrawing('registered-id'),
+ get drawingId() {
+ readCount += 1;
+ return readCount <= 2 ? 'registered-id' : 'unmapped-id';
+ },
+ } as IDrawingParam;
+
+ const plan = createDrawingCopyPlan([dynamicDrawing], {
+ unitId,
+ sourceSubUnitId: subUnitId,
+ targetSubUnitId: 'copied-sheet',
+ generateId: () => 'copy-0',
+ });
+
+ expect(plan.idMap.get('registered-id')).toBe('copy-0');
+ expect(plan.drawings[0]).toEqual(expect.objectContaining({
+ drawingId: 'unmapped-id',
+ subUnitId,
+ }));
+ });
+
+ it('creates uncached copy plans and reuses cached complete copy plans', () => {
+ let nextId = 0;
+ const options = {
+ unitId,
+ sourceSubUnitId: subUnitId,
+ targetSubUnitId: 'copied-sheet',
+ generateId: () => `copy-${nextId++}`,
+ };
+ const child = createDrawing('child', { groupId: 'missing-group' });
+ const uncachedPlan = getOrCreateDrawingCopyPlan(undefined, [child], options);
+
+ expect(uncachedPlan.drawings[0]).toEqual(expect.objectContaining({
+ drawingId: 'copy-0',
+ subUnitId: 'copied-sheet',
+ }));
+ expect(uncachedPlan.drawings[0].groupId).toBeUndefined();
+
+ const copyContext = new Map<string, unknown>();
+ const cachedPlan = getOrCreateDrawingCopyPlan(copyContext, [child], options);
+
+ expect(getOrCreateDrawingCopyPlan(copyContext, [child], options)).toBe(cachedPlan);
+ });
+
+ it('extends a cached copy plan with the default id generator when no generator is provided', () => {
+ const options = {
+ unitId,
+ sourceSubUnitId: subUnitId,
+ targetSubUnitId: 'copied-sheet',
+ };
+ const copyContext = new Map<string, unknown>();
+ const firstPlan = getOrCreateDrawingCopyPlan(copyContext, [createDrawing('child-1')], options);
+
+ const extendedPlan = getOrCreateDrawingCopyPlan(copyContext, [
+ createDrawing('child-1'),
+ createDrawing('child-2'),
+ ], options);
+
+ expect(extendedPlan).toBe(firstPlan);
+ expect(extendedPlan.idMap.has('child-2')).toBe(true);
+ expect(extendedPlan.drawings.map((drawing) => drawing.subUnitId)).toEqual(['copied-sheet', 'copied-sheet']);
+ });
+
+ it('extends a cached copy plan when a later caller provides more drawing graph nodes', () => {
+ let nextId = 0;
+ const copyContext = new Map<string, unknown>();
+ const options = {
+ unitId,
+ sourceSubUnitId: subUnitId,
+ targetSubUnitId: 'copied-sheet',
+ generateId: () => `copy-${nextId++}`,
+ };
+ const chart = createDrawing('chart-child', { drawingType: DrawingTypeEnum.DRAWING_CHART, groupId: 'group' });
+ const firstPlan = getOrCreateDrawingCopyPlan(copyContext, [chart], options);
+
+ expect(firstPlan.drawings[0].groupId).toBeUndefined();
+
+ const extendedPlan = getOrCreateDrawingCopyPlan(copyContext, [
+ createDrawing('group', { drawingType: DrawingTypeEnum.DRAWING_GROUP }),
+ createDrawing('image-child', { groupId: 'group' }),
+ chart,
+ ], options);
+
+ expect(extendedPlan).toBe(firstPlan);
+ expect(extendedPlan.idMap.get('chart-child')).toBe('copy-0');
+ expect(extendedPlan.idMap.get('group')).toBe('copy-1');
+ expect(extendedPlan.idMap.get('image-child')).toBe('copy-2');
+ expect(extendedPlan.drawings).toEqual([
+ expect.objectContaining({ drawingId: 'copy-1', subUnitId: 'copied-sheet' }),
+ expect.objectContaining({ drawingId: 'copy-2', groupId: 'copy-1', subUnitId: 'copied-sheet' }),
+ expect.objectContaining({ drawingId: 'copy-0', groupId: 'copy-1', subUnitId: 'copied-sheet' }),
+ ]);
+ });
+});
diff --git a/packages/drawing/src/utils/__tests__/drawing-group.spec.ts b/packages/drawing/src/utils/__tests__/drawing-group.spec.ts
new file mode 100644
index 0000000000..a6ae045e8c
--- /dev/null
+++ b/packages/drawing/src/utils/__tests__/drawing-group.spec.ts
@@ -0,0 +1,25 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { DrawingTypeEnum } from '@univerjs/core';
+import { describe, expect, it } from 'vitest';
+import { isGroupableDrawingType } from '../drawing-group';
+
+describe('drawing group', () => {
+ it('rejects drawing types that cannot join a group', () => {
+ expect(isGroupableDrawingType(DrawingTypeEnum.DRAWING_TABLE)).toBe(false);
+ });
+});
diff --git a/packages/drawing/src/utils/drawing-copy-plan.ts b/packages/drawing/src/utils/drawing-copy-plan.ts
new file mode 100644
index 0000000000..2c6eaaa23f
--- /dev/null
+++ b/packages/drawing/src/utils/drawing-copy-plan.ts
@@ -0,0 +1,134 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IDrawingParam } from '@univerjs/core';
+import { generateRandomId } from '@univerjs/core';
+
+/**
+ * A command-scoped cache key shared by sheet-copy interceptors so every drawing model
+ * resolves against the same copied drawing IDs.
+ */
+export const DRAWING_COPY_CONTEXT_KEY = 'univer.drawing.copy-plan';
+
+export interface ICreateDrawingCopyPlanOptions {
+ unitId: string;
+ sourceSubUnitId: string;
+ targetSubUnitId: string;
+ generateId?: () => string;
+}
+
+export interface IDrawingCopyPlan<T extends IDrawingParam = IDrawingParam> {
+ idMap: Map<string, string>;
+ drawings: T[];
+}
+
+function cloneDrawing<T extends IDrawingParam>(drawing: T): T {
+ return JSON.parse(JSON.stringify(drawing)) as T;
+}
+
+function copyDrawingWithIdMap<T extends IDrawingParam>(
+ drawing: T,
+ idMap: Map<string, string>,
+ options: ICreateDrawingCopyPlanOptions
+): T {
+ const copied = cloneDrawing(drawing);
+ const copiedDrawingId = idMap.get(drawing.drawingId);
+
+ if (copiedDrawingId == null) {
+ return copied;
+ }
+
+ copied.unitId = options.unitId;
+ copied.subUnitId = options.targetSubUnitId;
+ copied.drawingId = copiedDrawingId;
+
+ // Remap the parent through the shared plan instead of retaining a relationship to the source sheet.
+ if (copied.groupId) {
+ const copiedGroupId = idMap.get(copied.groupId);
+ if (copiedGroupId) {
+ copied.groupId = copiedGroupId;
+ } else {
+ // The parent is not part of this copy, so avoid leaving a dangling cross-sheet group reference.
+ delete copied.groupId;
+ }
+ }
+
+ return copied;
+}
+
+export function createDrawingCopyPlan<T extends IDrawingParam>(
+ drawings: readonly T[],
+ options: ICreateDrawingCopyPlanOptions
+): IDrawingCopyPlan<T> {
+ const { generateId = () => generateRandomId(10) } = options;
+ const idMap = new Map<string, string>();
+
+ drawings.forEach((drawing) => {
+ if (!idMap.has(drawing.drawingId)) {
+ idMap.set(drawing.drawingId, generateId());
+ }
+ });
+
+ return {
+ idMap,
+ drawings: drawings.map((drawing) => copyDrawingWithIdMap(drawing, idMap, options)),
+ };
+}
+
+export function getOrCreateDrawingCopyPlan<T extends IDrawingParam>(
+ copyContext: Map<string, unknown> | undefined,
+ drawings: readonly T[],
+ options: ICreateDrawingCopyPlanOptions
+): IDrawingCopyPlan<T> {
+ if (copyContext == null) {
+ return createDrawingCopyPlan(drawings, options);
+ }
+
+ const cached = copyContext.get(DRAWING_COPY_CONTEXT_KEY) as IDrawingCopyPlan<T> | undefined;
+ if (cached) {
+ const { generateId = () => generateRandomId(10) } = options;
+ // Feature interceptors may contribute different portions of the drawing graph. Extend the plan in
+ // place to preserve IDs already consumed by earlier interceptors.
+ const hasMissingDrawing = drawings.some((drawing) => !cached.idMap.has(drawing.drawingId));
+ if (!hasMissingDrawing) {
+ return cached;
+ }
+
+ drawings.forEach((drawing) => {
+ if (!cached.idMap.has(drawing.drawingId)) {
+ cached.idMap.set(drawing.drawingId, generateId());
+ }
+ });
+
+ const incomingDrawingIds = new Set(drawings.map((drawing) => drawing.drawingId));
+ const originalIdByCopiedId = new Map(Array.from(cached.idMap.entries()).map(([drawingId, copiedDrawingId]) => [copiedDrawingId, drawingId]));
+ const retainedDrawings = cached.drawings.filter((drawing) => {
+ const originalDrawingId = originalIdByCopiedId.get(drawing.drawingId);
+ return originalDrawingId == null || !incomingDrawingIds.has(originalDrawingId);
+ });
+
+ // Re-copy incoming nodes after extending the map so a child can resolve a group discovered later.
+ cached.drawings = [
+ ...drawings.map((drawing) => copyDrawingWithIdMap(drawing, cached.idMap, options)),
+ ...retainedDrawings,
+ ];
+ return cached;
+ }
+
+ const plan = createDrawingCopyPlan(drawings, options);
+ copyContext.set(DRAWING_COPY_CONTEXT_KEY, plan);
+ return plan;
+}
diff --git a/packages/drawing/src/utils/drawing-group.ts b/packages/drawing/src/utils/drawing-group.ts
index 7f3fb485a0..d1abf7395a 100644
--- a/packages/drawing/src/utils/drawing-group.ts
+++ b/packages/drawing/src/utils/drawing-group.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { DrawingType, IDrawingParam } from '@univerjs/core';
-import { DrawingTypeEnum, generateRandomId } from '@univerjs/core';
+import type { DrawingType } from '@univerjs/core';
+import { DrawingTypeEnum } from '@univerjs/core';
export const DRAWING_GROUPABLE_TYPES = [
DrawingTypeEnum.DRAWING_IMAGE,
@@ -27,110 +27,3 @@ export const DRAWING_GROUPABLE_TYPES = [
export function isGroupableDrawingType(type: DrawingType): boolean {
return (DRAWING_GROUPABLE_TYPES as readonly DrawingType[]).includes(type);
}
-
-export const DRAWING_COPY_CONTEXT_KEY = 'univer.drawing.copy-plan';
-
-export interface ICreateDrawingCopyPlanOptions {
- unitId: string;
- sourceSubUnitId: string;
- targetSubUnitId: string;
- generateId?: () => string;
-}
-
-export interface IDrawingCopyPlan<T extends IDrawingParam = IDrawingParam> {
- idMap: Map<string, string>;
- drawings: T[];
-}
-
-function cloneDrawing<T extends IDrawingParam>(drawing: T): T {
- return JSON.parse(JSON.stringify(drawing)) as T;
-}
-
-function copyDrawingWithIdMap<T extends IDrawingParam>(
- drawing: T,
- idMap: Map<string, string>,
- options: ICreateDrawingCopyPlanOptions
-): T {
- const copied = cloneDrawing(drawing);
- const copiedDrawingId = idMap.get(drawing.drawingId);
-
- if (copiedDrawingId == null) {
- return copied;
- }
-
- copied.unitId = options.unitId;
- copied.subUnitId = options.targetSubUnitId;
- copied.drawingId = copiedDrawingId;
-
- if (copied.groupId) {
- const copiedGroupId = idMap.get(copied.groupId);
- if (copiedGroupId) {
- copied.groupId = copiedGroupId;
- } else {
- delete copied.groupId;
- }
- }
-
- return copied;
-}
-
-export function createDrawingCopyPlan<T extends IDrawingParam>(
- drawings: readonly T[],
- options: ICreateDrawingCopyPlanOptions
-): IDrawingCopyPlan<T> {
- const { generateId = () => generateRandomId(10) } = options;
- const idMap = new Map<string, string>();
-
- drawings.forEach((drawing) => {
- if (!idMap.has(drawing.drawingId)) {
- idMap.set(drawing.drawingId, generateId());
- }
- });
-
- return {
- idMap,
- drawings: drawings.map((drawing) => copyDrawingWithIdMap(drawing, idMap, options)),
- };
-}
-
-export function getOrCreateDrawingCopyPlan<T extends IDrawingParam>(
- copyContext: Map<string, unknown> | undefined,
- drawings: readonly T[],
- options: ICreateDrawingCopyPlanOptions
-): IDrawingCopyPlan<T> {
- if (copyContext == null) {
- return createDrawingCopyPlan(drawings, options);
- }
-
- const cached = copyContext.get(DRAWING_COPY_CONTEXT_KEY) as IDrawingCopyPlan<T> | undefined;
- if (cached) {
- const { generateId = () => generateRandomId(10) } = options;
- const hasMissingDrawing = drawings.some((drawing) => !cached.idMap.has(drawing.drawingId));
- if (!hasMissingDrawing) {
- return cached;
- }
-
- drawings.forEach((drawing) => {
- if (!cached.idMap.has(drawing.drawingId)) {
- cached.idMap.set(drawing.drawingId, generateId());
- }
- });
-
- const incomingDrawingIds = new Set(drawings.map((drawing) => drawing.drawingId));
- const originalIdByCopiedId = new Map(Array.from(cached.idMap.entries()).map(([drawingId, copiedDrawingId]) => [copiedDrawingId, drawingId]));
- const retainedDrawings = cached.drawings.filter((drawing) => {
- const originalDrawingId = originalIdByCopiedId.get(drawing.drawingId);
- return originalDrawingId == null || !incomingDrawingIds.has(originalDrawingId);
- });
-
- cached.drawings = [
- ...drawings.map((drawing) => copyDrawingWithIdMap(drawing, cached.idMap, options)),
- ...retainedDrawings,
- ];
- return cached;
- }
-
- const plan = createDrawingCopyPlan(drawings, options);
- copyContext.set(DRAWING_COPY_CONTEXT_KEY, plan);
- return plan;
-}