Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sheet-ui): find & replace #1349

Merged
merged 17 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ packages/sheets-ui/src/services/clipboard @yuhongz
packages/sheets-ui/src/services/editor @Jocs
packages/sheets-ui/src/services/editor-bridge.service.ts @Jocs

packages/sheets-find @wzhudev
packages/sheets-find-replace @wzhudev

packages/sheets-numfmt @Gggpound
packages/sheets-imoprt-xlsx @Gggpound

packages/sheets-import-xlsx @Gggpound

packages/sheets-formula @DR-Univer @Dushusir

packages/sheets-zen-editor @Jocs

# slides
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"customd",
"Datas",
"DATEFMTLISG",
"Dushusir",
"endindex",
"esbuild",
"evented",
Expand All @@ -30,6 +31,7 @@
"fract",
"gainsboro",
"GANTT",
"Gggpound",
"Gridlines",
"Hanalei",
"hdmx",
Expand All @@ -41,6 +43,7 @@
"IRPC",
"Jamo",
"jikkai",
"Jocs",
"Kaiti",
"KANBAN",
"kcode",
Expand Down Expand Up @@ -86,7 +89,8 @@
"Xingkai",
"Xinwei",
"Xlookup",
"XMATCH"
"XMATCH",
"yuhongz"
],
"vsicons.presets.angular": false,

Expand Down
4 changes: 2 additions & 2 deletions examples/src/data/sheets/demo/default-workbook-data-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19428,8 +19428,8 @@ export const DEFAULT_WORKBOOK_DATA_DEMO: IWorkbookData = {
defaultRowHeight: 25,
mergeData: [
{
startRow: 37,
endRow: 36,
startRow: 36,
endRow: 37,
startColumn: 1,
endColumn: 2,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/src/data/sheets/demo/default-workbook-data-demo5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2657,8 +2657,8 @@ export const DEFAULT_WORKBOOK_DATA_DEMO5: IWorkbookData = {
defaultRowHeight: 25,
mergeData: [
{
startRow: 37,
endRow: 36,
startRow: 36,
endRow: 37,
startColumn: 1,
endColumn: 2,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { UniverFindReplacePlugin } from '@univerjs/find-replace';
import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { UniverSheetsFindPlugin } from '@univerjs/sheets-find-replace';
import { UniverSheetsFindReplacePlugin } from '@univerjs/sheets-find-replace';
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
Expand Down Expand Up @@ -78,7 +78,7 @@ univer.registerPlugin(UniverRPCMainThreadPlugin, {

// find replace
univer.registerPlugin(UniverFindReplacePlugin);
univer.registerPlugin(UniverSheetsFindPlugin);
univer.registerPlugin(UniverSheetsFindReplacePlugin);

// create univer sheet instance
univer.createUniverSheet(DEFAULT_WORKBOOK_DATA_DEMO);
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/common/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* 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:https://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 { describe, expect, it } from 'vitest';
import { rotate } from '../array';

describe('Test array util function', () => {
it('should "rotate" function work correctly', () => {
const raw = [1, 2, 3, 4, 5];

// The function do not mutate the original array.
expect(rotate(raw, 0)).toEqual([1, 2, 3, 4, 5]);
expect(rotate(raw, 1)).toEqual([2, 3, 4, 5, 1]);
expect(rotate(raw, 2)).toEqual([3, 4, 5, 1, 2]);
expect(rotate(raw, 3)).toEqual([4, 5, 1, 2, 3]);
expect(rotate(raw, 4)).toEqual([5, 1, 2, 3, 4]);
expect(rotate(raw, 5)).toEqual([1, 2, 3, 4, 5]);
expect(rotate(raw, 6)).toEqual([2, 3, 4, 5, 1]);
});
});
33 changes: 33 additions & 0 deletions packages/core/src/common/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,36 @@ export function findLast<T>(arr: T[], callback: (item: T, index: number) => bool

return null;
}

/**
* Rotate an array without mutating the original array.
* @param arr the array to be rotated
* @param steps how many steps to rotate
* @returns the rotated array, it is another array, the original array is not mutated.
*/
export function rotate<T>(arr: Readonly<T[]>, steps: number): readonly T[] {
if (arr.length === 0) {
return arr;
}

const offset = steps % arr.length;
return arr.slice(offset).concat(arr.slice(0, offset));
}

export function groupBy<T>(arr: Readonly<T[]>, keyFn: (v: T) => string): Map<string, T[]> {
const groups = new Map<string, T[]>();

arr.forEach((element) => {
const key = keyFn(element);

let group = groups.get(key);
if (!groups.has(key)) {
group = [];
groups.set(key, group);
}

group!.push(element);
});

return groups;
}
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { installShims } from './common/shims';

export * from './basics';
export { dedupe, remove } from './common/array';
export { dedupe, remove, rotate, groupBy } from './common/array';
export {
DEFAULT_EMPTY_DOCUMENT_VALUE,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
Expand Down Expand Up @@ -100,6 +100,7 @@ export {
UndoCommandId,
} from './services/undoredo/undoredo.service';
export * from './shared';
export { fromCallback } from './shared/rxjs';

// #region sheet
export type { IComposeInterceptors, IInterceptor, InterceptorHandler } from './common/interceptor';
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/services/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { IKeyValue } from '../../shared/types';
import { IContextService } from '../context/context.service';
import { ILogService } from '../log/log.service';

export const enum CommandType {
export enum CommandType {
/** Command could generate some operations or mutations. */
COMMAND = 0,
/** An operation that do not require conflict resolve. */
Expand All @@ -40,7 +40,7 @@ export interface ICommand<P extends object = object, R = boolean> {
readonly id: string;
readonly type: CommandType;

handler(accessor: IAccessor, params?: P): Promise<R> | R;
handler(accessor: IAccessor, params?: P, options?: IExecutionOptions): Promise<R> | R;

/**
* When this command is unregistered, this function would be called.
Expand Down Expand Up @@ -131,9 +131,9 @@ export interface IExecutionOptions {
export type CommandListener = (commandInfo: Readonly<ICommandInfo>, options?: IExecutionOptions) => void;

export interface ICommandService {
registerCommand(command: ICommand): IDisposable;
registerCommand(command: ICommand<object, unknown>): IDisposable;

registerMultipleCommand(command: ICommand): IDisposable;
registerMultipleCommand(command: ICommand<object, unknown>): IDisposable;

executeCommand<P extends object = object, R = boolean>(
id: string,
Expand Down Expand Up @@ -259,7 +259,7 @@ export class CommandService implements ICommandService {
const stackItemDisposable = this._pushCommandExecutionStack(commandInfo);

this._beforeCommandExecutionListeners.forEach((listener) => listener(commandInfo, options));
const result = await this._execute<P, R>(command as ICommand<P, R>, params);
const result = await this._execute<P, R>(command as ICommand<P, R>, params, options);
this._commandExecutedListeners.forEach((listener) => listener(commandInfo, options));

stackItemDisposable.dispose();
Expand Down Expand Up @@ -300,7 +300,7 @@ export class CommandService implements ICommandService {
const stackItemDisposable = this._pushCommandExecutionStack(commandInfo);

this._beforeCommandExecutionListeners.forEach((listener) => listener(commandInfo, options));
const result = this._syncExecute<P, R>(command as ICommand<P, R>, params);
const result = this._syncExecute<P, R>(command as ICommand<P, R>, params, options);
this._commandExecutedListeners.forEach((listener) => listener(commandInfo, options));

stackItemDisposable.dispose();
Expand Down Expand Up @@ -353,7 +353,7 @@ export class CommandService implements ICommandService {
});
}

private async _execute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P): Promise<R> {
private async _execute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P, options?: IExecutionOptions): Promise<R> {
this._logService.debug(
'[CommandService]',
`${'|-'.repeat(Math.max(this._commandExecutingLevel, 0))}executing command "${command.id}"`
Expand All @@ -362,7 +362,7 @@ export class CommandService implements ICommandService {
this._commandExecutingLevel++;
let result: R | boolean;
try {
result = await this._injector.invoke(command.handler, params);
result = await this._injector.invoke(command.handler, params, options);
this._commandExecutingLevel--;
} catch (e) {
result = false;
Expand All @@ -373,7 +373,7 @@ export class CommandService implements ICommandService {
return result;
}

private _syncExecute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P): R {
private _syncExecute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P, options?: IExecutionOptions): R {
this._logService.debug(
'[CommandService]',
`${'|-'.repeat(Math.max(0, this._commandExecutingLevel))}executing command "${command.id}".`
Expand All @@ -382,7 +382,7 @@ export class CommandService implements ICommandService {
this._commandExecutingLevel++;
let result: R | boolean;
try {
result = this._injector.invoke(command.handler, params) as R;
result = this._injector.invoke(command.handler, params, options) as R;
if (result instanceof Promise) {
throw new TypeError('[CommandService]: Command handler should not return a promise.');
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/services/instance/instance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import type { IDocumentData, ISlideData, IWorkbookData } from '../../types/inter
import { FOCUSING_DOC, FOCUSING_SHEET, FOCUSING_SLIDE } from '../context/context';
import { IContextService } from '../context/context.service';

export const enum UniverInstanceType {
export enum UniverInstanceType {
UNKNOWN = 0,

DOC = 1,

SHEET = 2,

SLIDE = 3,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/services/locale/locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@ export class LocaleService extends Disposable {
}

/**
* Load more locales after init
* Load more locales after init.
*
* @param locales - Locale object
*
*/
load(locales: ILocales) {
this._locales = Tools.deepMerge(this._locales ?? {}, locales);
}

/**
* Translate a key to the current locale
*
* @param {string} key the key to translate
* @param {string[]} args optional arguments to replace in the translated string
* @returns {string} the translated string
*
* @example
* const locales = {
* [LocaleType.EN_US]: {
Expand Down
Loading
Loading