mirror of
https://github.com/dream-num/univer.git
synced 2026-09-24 15:32:18 +08:00
refactor(sheets-data-validation): move list option serialization to sheets (#6949)
This commit is contained in:
+1
-2
@@ -19,8 +19,7 @@ import type { SheetDataValidationModel } from '@univerjs/sheets-data-validation'
|
||||
import type { IDropdownParam, IEditorBridgeService } from '@univerjs/sheets-ui';
|
||||
import { DataValidationRenderMode } from '@univerjs/core';
|
||||
import { DataValidatorDropdownType } from '@univerjs/data-validation';
|
||||
import { SetRangeValuesCommand } from '@univerjs/sheets';
|
||||
import { serializeListOptions } from '@univerjs/sheets-data-validation';
|
||||
import { serializeListOptions, SetRangeValuesCommand } from '@univerjs/sheets';
|
||||
import { SetCellEditVisibleOperation } from '@univerjs/sheets-ui';
|
||||
import { Subject } from 'rxjs';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
@@ -22,8 +22,8 @@ import type { IUniverSheetsDataValidationUIConfig } from '../config/config';
|
||||
import { CellValueType, DataValidationErrorStyle, DataValidationRenderMode, dateKit, Disposable, DisposableCollection, ICommandService, IConfigService, Inject, Injector, IUniverInstanceService, numfmt, UniverInstanceType } from '@univerjs/core';
|
||||
import { DataValidatorDropdownType, DataValidatorRegistryService } from '@univerjs/data-validation';
|
||||
import { DeviceInputEventType } from '@univerjs/engine-render';
|
||||
import { SetRangeValuesCommand, SheetsSelectionsService } from '@univerjs/sheets';
|
||||
import { getCellValueOrigin, getDataValidationCellValue, serializeListOptions, SheetDataValidationModel } from '@univerjs/sheets-data-validation';
|
||||
import { serializeListOptions, SetRangeValuesCommand, SheetsSelectionsService } from '@univerjs/sheets';
|
||||
import { getCellValueOrigin, getDataValidationCellValue, SheetDataValidationModel } from '@univerjs/sheets-data-validation';
|
||||
import { getPatternType } from '@univerjs/sheets-numfmt';
|
||||
import { IEditorBridgeService, ISheetCellDropdownManagerService, SetCellEditVisibleOperation } from '@univerjs/sheets-ui';
|
||||
import { IZenZoneService, KeyCode } from '@univerjs/ui';
|
||||
|
||||
+2
-1
@@ -21,7 +21,8 @@ import { awaitTime, DataValidationType, generateRandomId, isFormulaString, Local
|
||||
import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';
|
||||
import { borderClassName, clsx, DraggableList, Dropdown, FormLayout, Input, Radio, RadioGroup } from '@univerjs/design';
|
||||
import { DeleteIcon, IncreaseIcon, MoreDownIcon, SequenceIcon } from '@univerjs/icons';
|
||||
import { DataValidationFormulaController, deserializeListOptions, serializeListOptions } from '@univerjs/sheets-data-validation';
|
||||
import { deserializeListOptions, serializeListOptions } from '@univerjs/sheets';
|
||||
import { DataValidationFormulaController } from '@univerjs/sheets-data-validation';
|
||||
import { FormulaEditor } from '@univerjs/sheets-formula-ui';
|
||||
import { useDependency, useEvent, useObservable, useSidebarClick } from '@univerjs/ui';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
|
||||
+1
-1
@@ -16,8 +16,8 @@
|
||||
|
||||
import type { FUniver } from '@univerjs/core/facade';
|
||||
import { DataValidationOperator, DataValidationType } from '@univerjs/core';
|
||||
import { deserializeListOptions } from '@univerjs/sheets';
|
||||
import { FDataValidationBuilder } from '@univerjs/sheets-data-validation/facade/f-data-validation-builder.js';
|
||||
import { deserializeListOptions } from '@univerjs/sheets-data-validation/validators/util.js';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { createFacadeTestBed } from './create-test-bed';
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import type { IDataValidationRule, IDataValidationRuleOptions } from '@univerjs/
|
||||
import type { FRange } from '@univerjs/sheets/facade';
|
||||
import { DataValidationErrorStyle, DataValidationOperator, DataValidationType, generateRandomId } from '@univerjs/core';
|
||||
import { serializeRangeToRefString } from '@univerjs/engine-formula';
|
||||
import { serializeListOptions } from '@univerjs/sheets';
|
||||
import { FDataValidation } from './f-data-validation';
|
||||
|
||||
/**
|
||||
@@ -791,7 +792,7 @@ export class FDataValidationBuilder {
|
||||
*/
|
||||
requireValueInList(values: string[], multiple?: boolean, showDropdown?: boolean): FDataValidationBuilder {
|
||||
this._rule.type = multiple ? DataValidationType.LIST_MULTIPLE : DataValidationType.LIST;
|
||||
this._rule.formula1 = JSON.stringify(values.filter(Boolean));
|
||||
this._rule.formula1 = serializeListOptions(values);
|
||||
this._rule.formula2 = undefined;
|
||||
this._rule.showDropDown = showDropdown ?? true;
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ export { DateValidator } from './validators/date-validator';
|
||||
export { getCellValueNumber } from './validators/decimal-validator';
|
||||
export { ListMultipleValidator } from './validators/list-multiple-validator';
|
||||
export { ListValidator } from './validators/list-validator';
|
||||
export { deserializeListOptions, getDataValidationCellValue, serializeListOptions } from './validators/util';
|
||||
export { getDataValidationCellValue } from './validators/util';
|
||||
export { getTransformedFormula } from './validators/util';
|
||||
export const CUSTOM_FORMULA_INPUT_NAME = 'data-validation.custom-formula-input';
|
||||
export const BASE_FORMULA_INPUT_NAME = 'data-validation.formula-input';
|
||||
|
||||
@@ -18,9 +18,9 @@ import type { ICellData, IDataValidationRule, Nullable } from '@univerjs/core';
|
||||
import type { IOtherFormulaResult } from '@univerjs/engine-formula';
|
||||
import { Disposable, Inject, Injector, isFormulaString, numfmt } from '@univerjs/core';
|
||||
import { DataValidationModel } from '@univerjs/data-validation';
|
||||
import { deserializeListOptions } from '@univerjs/sheets';
|
||||
import { isLegalFormulaResult } from '../utils/formula';
|
||||
import { getCellValueOrigin } from '../utils/get-cell-data-origin';
|
||||
import { deserializeListOptions } from '../validators/util';
|
||||
import { DataValidationFormulaService } from './dv-formula.service';
|
||||
|
||||
export interface IListCacheItem {
|
||||
|
||||
@@ -19,11 +19,11 @@ import type { IFormulaResult, IFormulaValidResult, IValidatorCellInfo } from '@u
|
||||
import { DataValidationRenderMode, DataValidationType, isFormulaString, IUniverInstanceService, numfmt, Rectangle, Tools, UniverInstanceType, WrapStrategy } from '@univerjs/core';
|
||||
import { BaseDataValidator } from '@univerjs/data-validation';
|
||||
import { deserializeRangeWithSheet, isReferenceString, LexerTreeBuilder, sequenceNodeType } from '@univerjs/engine-formula';
|
||||
import { deserializeListOptions } from '@univerjs/sheets';
|
||||
import { DataValidationFormulaService } from '../services/dv-formula.service';
|
||||
import { DataValidationListCacheService } from '../services/dv-list-cache.service';
|
||||
import { getFormulaResult, isLegalFormulaResult } from '../utils/formula';
|
||||
import { getCellValueOrigin } from '../utils/get-cell-data-origin';
|
||||
import { deserializeListOptions } from './util';
|
||||
|
||||
// Keep getRuleFormulaResultSet for backward compatibility (used in isValidType)
|
||||
export function getRuleFormulaResultSet(result: Nullable<Nullable<ICellData>[][]>): string[] {
|
||||
|
||||
@@ -18,6 +18,7 @@ import type { ICellData, ISheetDataValidationRule, IUnitRangeName, IUniverInstan
|
||||
import type { LexerTreeBuilder } from '@univerjs/engine-formula';
|
||||
import type { ISheetLocationBase } from '@univerjs/sheets';
|
||||
import { isFormulaString, Range, UniverInstanceType } from '@univerjs/core';
|
||||
import { deserializeListOptions } from '@univerjs/sheets';
|
||||
import { getCellValueOrigin } from '../utils/get-cell-data-origin';
|
||||
|
||||
export function getSheetRangeValueSet(grid: IUnitRangeName, univerInstanceService: IUniverInstanceService, currUnitId: string, currSubUnitId: string) {
|
||||
@@ -49,23 +50,6 @@ export function getSheetRangeValueSet(grid: IUnitRangeName, univerInstanceServic
|
||||
return Array.from(set);
|
||||
}
|
||||
|
||||
export function serializeListOptions(options: string[]) {
|
||||
return JSON.stringify(options.filter(Boolean));
|
||||
}
|
||||
|
||||
export function deserializeListOptions(optionsStr: string) {
|
||||
try {
|
||||
const options = JSON.parse(optionsStr);
|
||||
if (Array.isArray(options) && options.every((option) => typeof option === 'string')) {
|
||||
return options.filter(Boolean);
|
||||
}
|
||||
} catch {
|
||||
// Fallback for data saved by older versions.
|
||||
}
|
||||
|
||||
return optionsStr.split(',').filter(Boolean);
|
||||
}
|
||||
|
||||
export function getDataValidationCellValue(cellData: Nullable<ICellData>) {
|
||||
const cellValue = getCellValueOrigin(cellData);
|
||||
if (cellValue === undefined || cellValue === null) {
|
||||
|
||||
@@ -25,7 +25,9 @@ import {
|
||||
import { borderClassName, borderTopClassName, clsx, scrollbarClassName } from '@univerjs/design';
|
||||
import { CheckMarkIcon } from '@univerjs/icons';
|
||||
import {
|
||||
deserializeListOptions,
|
||||
RangeProtectionPermissionEditPoint,
|
||||
serializeListOptions,
|
||||
SheetPermissionCheckController,
|
||||
WorkbookEditablePermission,
|
||||
WorksheetEditPermission,
|
||||
@@ -33,14 +35,6 @@ import {
|
||||
import { useDependency } from '@univerjs/ui';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
|
||||
function serializeListOptions(options: string[]) {
|
||||
return options.filter(Boolean).join(',');
|
||||
}
|
||||
|
||||
function deserializeListOptions(optionsStr: string) {
|
||||
return optionsStr.split(',').filter(Boolean);
|
||||
}
|
||||
|
||||
interface ISelectListProps {
|
||||
value: string[];
|
||||
onChange: (val: string[]) => void;
|
||||
|
||||
@@ -240,3 +240,20 @@ export function getVisibleRanges(ranges: IRange[], accessor: IAccessor, unitId?:
|
||||
|
||||
return visibleRanges;
|
||||
}
|
||||
|
||||
export function serializeListOptions(options: string[]) {
|
||||
return JSON.stringify(options.filter(Boolean));
|
||||
}
|
||||
|
||||
export function deserializeListOptions(optionsStr: string) {
|
||||
try {
|
||||
const options = JSON.parse(optionsStr);
|
||||
if (Array.isArray(options) && options.every((option) => typeof option === 'string')) {
|
||||
return options.filter(Boolean);
|
||||
}
|
||||
} catch {
|
||||
// Fallback for data saved by older versions.
|
||||
}
|
||||
|
||||
return optionsStr.split(',').filter(Boolean);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ export {
|
||||
} from './basics/selection';
|
||||
export { SplitDelimiterEnum, splitRangeText } from './basics/split-range-text';
|
||||
export { findFirstNonEmptyCell } from './basics/utils';
|
||||
export { discreteRangeToRange, generateNullCell, generateNullCellValue, getVisibleRanges, rangeToDiscreteRange } from './basics/utils';
|
||||
export { deserializeListOptions, discreteRangeToRange, generateNullCell, generateNullCellValue, getVisibleRanges, rangeToDiscreteRange, serializeListOptions } from './basics/utils';
|
||||
export { AddRangeProtectionCommand, type IAddRangeProtectionCommandParams } from './commands/commands/add-range-protection.command';
|
||||
export {
|
||||
addMergeCellsUtil,
|
||||
|
||||
Reference in New Issue
Block a user