From 6aaca9f9c82e5e48d736fbec15177a7bca1fd243 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 30 May 2024 21:32:04 +0800 Subject: [PATCH] feat(utils): add mergeOptions util --- packages/g6/__tests__/unit/utils/style.spec.ts | 11 ++++++++++- packages/g6/src/utils/style.ts | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/g6/__tests__/unit/utils/style.spec.ts b/packages/g6/__tests__/unit/utils/style.spec.ts index fcadc9c4cf..02d32ae855 100644 --- a/packages/g6/__tests__/unit/utils/style.spec.ts +++ b/packages/g6/__tests__/unit/utils/style.spec.ts @@ -1,4 +1,4 @@ -import { computeElementCallbackStyle, zIndexOf } from '@/src/utils/style'; +import { computeElementCallbackStyle, mergeOptions, zIndexOf } from '@/src/utils/style'; describe('style', () => { it('computeElementCallbackStyle', () => { @@ -45,4 +45,13 @@ describe('style', () => { expect(zIndexOf({ id: 'node-1', style: { zIndex: 1 } })).toBe(1); expect(zIndexOf({ id: 'node-1', style: { zIndex: -1 } })).toBe(-1); }); + + it('mergeOptions', () => { + expect( + mergeOptions( + { style: { a: 1, b: [1, 2], c: { d: 1 } }, id: '1' }, + { style: { a: 2, b: [2, 3], c: { f: 1 } }, id: '2' }, + ), + ).toEqual({ style: { a: 2, b: [2, 3], c: { f: 1 } }, id: '2' }); + }); }); diff --git a/packages/g6/src/utils/style.ts b/packages/g6/src/utils/style.ts index fd034d8d8d..e5d6a8cd28 100644 --- a/packages/g6/src/utils/style.ts +++ b/packages/g6/src/utils/style.ts @@ -1,3 +1,4 @@ +import type { DisplayObjectConfig } from '@antv/g'; import { isFunction } from '@antv/util'; import type { CallableObject, ElementDatum, StyleIterationContext } from '../types'; import { inferDefaultValue } from './animation'; @@ -36,3 +37,20 @@ export function computeElementCallbackStyle( export function zIndexOf(datum: ElementDatum) { return datum.style?.zIndex ?? (inferDefaultValue('zIndex') as number) ?? 0; } + +/** + * 合并图形配置项 + * + * Merge graphic configuration + * @param options - 图形配置项数组 | graphic configuration array + * @returns 合并后的配置项 | merged configuration + */ +export function mergeOptions(...options: DisplayObjectConfig[]): DisplayObjectConfig { + const option = { style: {} }; + for (const opt of options) { + const { style, ..._ } = opt; + Object.assign(option.style, style); + Object.assign(option, _); + } + return option; +}