Skip to content

Commit

Permalink
feat(sheet): support sheet default style
Browse files Browse the repository at this point in the history
  • Loading branch information
VicKun4937 committed Sep 20, 2024
1 parent 7b89f27 commit c27990b
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 222 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/common/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const DOCS_ZEN_EDITOR_UNIT_ID_KEY = `${PREFIX}ZEN_EDITOR`;

export const DEFAULT_EMPTY_DOCUMENT_VALUE = '\r\n';

export const IS_ROW_STYLE_PRECEDE_COLUMN_STYLE = 'isRowStylePrecedeColumnStyle';

export function createInternalEditorID(id: string) {
return `${PREFIX}${id}`;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export {
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
DOCS_ZEN_EDITOR_UNIT_ID_KEY,
IS_ROW_STYLE_PRECEDE_COLUMN_STYLE,
isInternalEditorID,
} from './common/const';
export * from './common/di';
Expand Down
151 changes: 89 additions & 62 deletions packages/core/src/shared/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/

import { customAlphabet, nanoid } from 'nanoid';

import mergeWith from 'lodash.mergewith';

import { customAlphabet, nanoid } from 'nanoid';
import { isLegalUrl, normalizeUrl } from '../common/url';
import type { IKeyValue } from './types';
import type { IStyleData } from '../types/interfaces';
import type { IKeyValue, Nullable } from './types';

const rmsPrefix = /^-ms-/;
const rDashAlpha = /-([a-z])/g;
Expand Down Expand Up @@ -52,6 +53,60 @@ const alphabets = [
'Z',
];

function diffValue(oneValue: any, twoValue: any) {
const oneType = Tools.getValueType(oneValue);
const twoType = Tools.getValueType(twoValue);
if (oneType !== twoType) {
return false;
}
if (Tools.isArray(oneValue)) {
return diffArrays(oneValue, twoValue);
}
if (Tools.isObject(oneValue)) {
return diffObject(oneValue as object, twoValue);
}
if (Tools.isDate(oneValue)) {
return (oneValue as Date).getTime() === twoValue.getTime();
}
if (Tools.isRegExp(oneValue)) {
return (oneValue as unknown as string).toString() === twoValue.toString();
}
return oneValue === twoValue;
}

function diffArrays(oneArray: any[], twoArray: any[]) {
if (oneArray.length !== twoArray.length) {
return false;
}
for (let i = 0, len = oneArray.length; i < len; i++) {
const oneValue = oneArray[i];
const twoValue = twoArray[i];
if (!diffValue(oneValue, twoValue)) {
return false;
}
}
return true;
}

function diffObject(oneObject: IKeyValue, twoObject: IKeyValue) {
const oneKeys = Object.keys(oneObject);
const twoKeys = Object.keys(twoObject);
if (oneKeys.length !== twoKeys.length) {
return false;
}
for (const key of oneKeys) {
if (!twoKeys.includes(key)) {
return false;
}
const oneValue = oneObject[key];
const twoValue = twoObject[key];
if (!diffValue(oneValue, twoValue)) {
return false;
}
}
return true;
}

/**
* Universal tool library
*/
Expand Down Expand Up @@ -247,62 +302,8 @@ export class Tools {
return Number(Number(value).toFixed(digit));
}

static diffValue(one: any, tow: any) {
function diffValue(oneValue: any, towValue: any) {
const oneType = Tools.getValueType(oneValue);
const towType = Tools.getValueType(towValue);
if (oneType !== towType) {
return false;
}
if (Tools.isArray(oneValue)) {
return diffArrays(oneValue, towValue);
}
if (Tools.isObject(oneValue)) {
return diffObject(oneValue as object, towValue);
}
if (Tools.isDate(oneValue)) {
return (oneValue as Date).getTime() === towValue.getTime();
}
if (Tools.isRegExp(oneValue)) {
return (oneValue as unknown as string).toString() === towValue.toString();
}
return oneValue === towValue;
}

function diffArrays(oneArray: any[], towArray: any[]) {
if (one.length !== tow.length) {
return false;
}
for (let i = 0, len = oneArray.length; i < len; i++) {
const oneValue = oneArray[i];
const towValue = towArray[i];
if (!diffValue(oneValue, towValue)) {
return false;
}
}
return true;
}

function diffObject(oneObject: IKeyValue, towObject: IKeyValue) {
const oneKeys = Object.keys(oneObject);
const towKeys = Object.keys(towObject);
if (oneKeys.length !== towKeys.length) {
return false;
}
for (const key of oneKeys) {
if (!towKeys.includes(key)) {
return false;
}
const oneValue = oneObject[key];
const towValue = towObject[key];
if (!diffValue(oneValue, towValue)) {
return false;
}
}
return true;
}

return diffValue(one, tow);
static diffValue(one: any, two: any) {
return diffValue(one, two);
}

static deepClone<T = unknown>(value: T): T {
Expand Down Expand Up @@ -719,12 +720,12 @@ export class Tools {
* @static
* @param {unknown} object Modify the property while leaving the reference unchanged.
* @param {unknown} source The source being merged in object.
* @param {(value: unknown, originValue: unknown, key: string, object: unknown, source: unknown, stack: string[]) => {}} [customizer]
* @param {(value: unknown, originValue: unknown, key: string, object: unknown, source: unknown, stack: string[]) => {}} [customize]
* @return {*}
* @memberof Tools
*/
static mergeWith(object: unknown, source: unknown, customizer?: (value: unknown, originValue: unknown, key: string, object: unknown, source: unknown, stack: string[]) => {}) {
return mergeWith(object, source, customizer);
static mergeWith(object: unknown, source: unknown, customize?: (value: unknown, originValue: unknown, key: string, object: unknown, source: unknown, stack: string[]) => {}) {
return mergeWith(object, source, customize);
}
}

Expand All @@ -735,3 +736,29 @@ export function generateRandomId(n: number = 21, alphabet?: string): string {

return nanoid(n);
}

interface IStyleDataObject {
[key: string]: unknown;
}

/**
* compose styles by priority, the latter will overwrite the former
* @param { Nullable<IStyleData>[]} styles the styles to be composed
* @returns { Nullable<IStyleData>[]}
*/
export function composeStyles(...styles: Nullable<IStyleData>[]): IStyleData {
const result: IStyleData = {};
const length = styles.length;
for (let i = length - 1; i >= 0; i--) {
const style = styles[i];
if (style) {
const keys = Object.keys(style);
for (const key of keys) {
if ((result as IStyleDataObject)[key] === undefined) {
(result as IStyleDataObject)[key] = (style as IStyleDataObject)[key];
}
}
}
}
return result;
}
18 changes: 18 additions & 0 deletions packages/core/src/sheets/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export interface IWorkbookData {
*/
sheets: { [sheetId: string]: Partial<IWorksheetData> };

/**
* @property {string|Nullable<IStyleData>} [defaultStyle] - Default style id or style data of Workbook.
*/
defaultStyle?: Nullable<IStyleData> | string;

/**
* Resources of the Univer Sheet. It is used to store the data of other plugins.
*/
Expand Down Expand Up @@ -117,6 +122,11 @@ export interface IWorksheetData {
rowData: IObjectArrayPrimitiveType<Partial<IRowData>>;
columnData: IObjectArrayPrimitiveType<Partial<IColumnData>>;

/**
* @property {string|Nullable<IStyleData>} [defaultStyle] - Default style id or style data of Worksheet.
*/
defaultStyle?: Nullable<IStyleData> | string;

rowHeader: {
width: number;
hidden?: BooleanNumber;
Expand Down Expand Up @@ -154,6 +164,10 @@ export interface IRowData {
* hidden
*/
hd?: BooleanNumber;
/**
* style id
*/
s?: Nullable<IStyleData | string>;
}

export interface IRowAutoHeightInfo {
Expand All @@ -174,6 +188,10 @@ export interface IColumnData {
* hidden
*/
hd?: BooleanNumber;
/**
* style id
*/
s?: Nullable<IStyleData | string>;
}

/**
Expand Down
89 changes: 88 additions & 1 deletion packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { mergeWorksheetSnapshotWithDefault } from './sheet-snapshot-utils';
import { SpanModel } from './span-model';
import { SheetViewModel } from './view-model';
import type { IObjectMatrixPrimitiveType, Nullable } from '../shared';
import type { IStyleData } from '../types/interfaces';
import type { Styles } from './styles';
import type { ICellData, ICellDataForSheetInterceptor, IFreeze, IRange, ISelectionCell, IWorksheetData } from './typedef';

Expand Down Expand Up @@ -86,9 +87,95 @@ export class Worksheet {
return this._spanModel;
}

/**
* Get the style of the column.
* @param {number} column The column index
* @returns {Nullable<IStyleData>|string} The style of the column
*/
getColumnStyle(column: number): Nullable<IStyleData> | string {
return this._styles.get(this._snapshot.columnData[column]?.s);
}

getColumnStyleInternal(column: number): Nullable<IStyleData> {
const style = this._snapshot.columnData[column]?.s;
if (typeof style === 'string') {
return this._styles.get(style);
}
return style;
}

/**
* Set the style of the column.
* @param {number} column The column index
* @param {string|Nullable<IStyleData>} style The style to be set
*/
setColumnStyle(column: number, style: string | Nullable<IStyleData>): void {
const columnData = this._snapshot.columnData[column];
if (!columnData) {
this._snapshot.columnData[column] = { s: style };
} else {
columnData.s = style;
}
}

/**
* Get the style of the row.
* @param {number} row The row index
* @returns {Nullable<IStyleData>} The style of the row
*/
getRowStyle(row: number): Nullable<IStyleData> {
return this._styles.get(this._snapshot.rowData[row]?.s);
}

getRowStyleInternal(row: number): Nullable<IStyleData> {
const style = this._snapshot.rowData[row]?.s;
if (typeof style === 'string') {
return this._styles.get(style);
}
return style;
}

/**
* Set the style of the row.
* @param {number} row
* @param {string|Nullable<IStyleData>} style The style to be set
*/
setRowStyle(row: number, style: string | Nullable<IStyleData>): void {
const rowData = this._snapshot.rowData[row];
if (!rowData) {
this._snapshot.rowData[row] = { s: style };
} else {
rowData.s = style;
}
}

/**
* Get the default style of the worksheet.
* @returns {Nullable<IStyleData>} Default Style
*/
getDefaultCellStyle(): Nullable<IStyleData> | string {
return this._snapshot.defaultStyle;
}

getDefaultCellStyleInternal(): Nullable<IStyleData> {
const style = this._snapshot.defaultStyle;
if (typeof style === 'string') {
return this._styles.get(style);
}
return style;
}

/**
* Set Default Style, if the style has been set, all cells style will be base on this style.
* @param {Nullable<IStyleData>} style The style to be set as default style
*/
setDefaultCellStyle(style: Nullable<IStyleData> | string): void {
this._snapshot.defaultStyle = style;
}

/**
* Returns WorkSheet Cell Data Matrix
* @returns
* @returns {ObjectMatrix<Nullable<ICellData>>} Cell Data Matrix
*/
getCellMatrix(): ObjectMatrix<Nullable<ICellData>> {
return this._cellData;
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types/const/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,3 @@ export const DEFAULT_SLIDE = {
height: 300,
},
};

Loading

0 comments on commit c27990b

Please sign in to comment.