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

refactor(sheets-ui): cell editor & formula editor #4384

Open
wants to merge 40 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b663b89
refactor(sheets-ui): cell editor 1125 (#4383)
weird94 Dec 28, 2024
9cf6447
fix: start edit with f2
weird94 Dec 28, 2024
7fff84e
Merge branch 'refactor/cell-formula-editor' of github.com:dream-num/u…
weird94 Dec 28, 2024
8c60cb6
feat: update
weird94 Dec 28, 2024
f3bed40
feat: formula-editor emit change
weird94 Dec 28, 2024
ee73d97
feat: range-selector onChange
weird94 Dec 28, 2024
15067ab
fix: hide popup on moving
weird94 Dec 28, 2024
b93a1fd
fix: hide popup on selection move
weird94 Dec 28, 2024
fb79580
fix: ts
weird94 Dec 28, 2024
9df20d2
feat: remove useless code
weird94 Dec 28, 2024
ff1bab7
feat: remove react-mentions
weird94 Dec 28, 2024
9923e61
fix: mobile
weird94 Dec 28, 2024
cf9a491
feat: optimize code
weird94 Dec 28, 2024
5425bb2
Merge branch 'dev' into refactor/cell-formula-editor
weird94 Jan 8, 2025
4d007b0
feat: lock
weird94 Jan 8, 2025
e78c899
fix: https://github.com/dream-num/univer-pro/issues/3873
weird94 Jan 8, 2025
7d2eb5e
fix: https://github.com/dream-num/univer-pro/issues/3878
weird94 Jan 8, 2025
7f6c9f3
feat: update
weird94 Jan 8, 2025
1e742c3
feat: update
weird94 Jan 8, 2025
362521c
feat: update
weird94 Jan 8, 2025
943b1a7
feat: update
weird94 Jan 8, 2025
97e7f71
feat: update
weird94 Jan 8, 2025
8ce5751
feat: update
weird94 Jan 9, 2025
7797bc3
feat: update
weird94 Jan 9, 2025
66585fa
feat: update
weird94 Jan 9, 2025
8aca66a
feat: update
weird94 Jan 9, 2025
f86a8ef
fix: popup
weird94 Jan 9, 2025
81ddb0b
Merge remote-tracking branch 'origin/dev' into refactor/cell-formula-…
weird94 Jan 10, 2025
2a950d9
feat: update
weird94 Jan 10, 2025
83e3854
feat: update
weird94 Jan 11, 2025
6371e67
feat: update
weird94 Jan 11, 2025
5423e3c
feat: update
weird94 Jan 11, 2025
1524f7a
feat: lint
weird94 Jan 11, 2025
a6fd44f
Merge remote-tracking branch 'origin/dev' into refactor/cell-formula-…
weird94 Jan 11, 2025
9179356
feat: update
weird94 Jan 11, 2025
4829293
fix: across selection
weird94 Jan 11, 2025
8c97614
feat: max-height on comment
weird94 Jan 11, 2025
d278c5d
feat: update
weird94 Jan 11, 2025
28eaf82
feat: update
weird94 Jan 11, 2025
f05940f
feat: update
weird94 Jan 11, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { IEditorService } from '@univerjs/docs-ui';
import { ISidebarService } from '@univerjs/ui';
import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
import { TEST_EDITOR_CONTAINER_COMPONENT } from '../../views/test-editor/component-name';

export interface IUIComponentCommandParams {
Expand All @@ -34,25 +34,17 @@ export const SidebarOperation: ICommand = {
const unit = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
switch (params.value) {
case 'open':
editorService.setOperationSheetUnitId(unit.getUnitId());
editorService.setOperationSheetSubUnitId(unit.getActiveSheet()?.getSheetId());
sidebarService.open({
header: { title: 'Sidebar title' },
children: { label: TEST_EDITOR_CONTAINER_COMPONENT },
footer: { title: 'Sidebar Footer' },
onClose: () => {
editorService.setOperationSheetUnitId(null);
editorService.setOperationSheetSubUnitId(null);
editorService.closeRangePrompt();
},
});
break;

case 'close':
default:
editorService.setOperationSheetUnitId(null);
editorService.setOperationSheetSubUnitId(null);
editorService.closeRangePrompt();
sidebarService.close();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { ThemeOperation } from '../commands/operations/theme.operation';
import { ImageDemo } from '../components/Image';
// @ts-ignore
import VueI18nIcon from '../components/VueI18nIcon.vue';
import { TEST_EDITOR_CONTAINER_COMPONENT } from '../views/test-editor/component-name';
import { TestEditorContainer } from '../views/test-editor/TestTextEditor';
// import { TEST_EDITOR_CONTAINER_COMPONENT } from '../views/test-editor/component-name';
// import { TestEditorContainer } from '../views/test-editor/TestTextEditor';
import { RecordController } from './local-save/record.controller';
import { menuSchema } from './menu.schema';

Expand Down Expand Up @@ -83,7 +83,7 @@ export class DebuggerController extends Disposable {

private _initCustomComponents(): void {
const componentManager = this._componentManager;
this.disposeWithMe(componentManager.register(TEST_EDITOR_CONTAINER_COMPONENT, TestEditorContainer));
// this.disposeWithMe(componentManager.register(TEST_EDITOR_CONTAINER_COMPONENT, TestEditorContainer));
this.disposeWithMe(componentManager.register('VueI18nIcon', VueI18nIcon, {
framework: 'vue3',
}));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { IDocumentData, Nullable } from '@univerjs/core';
import type { IUniFormulaPopupInfo } from '../../services/formula-popup.service';
import { BooleanNumber, createInternalEditorID, DEFAULT_EMPTY_DOCUMENT_VALUE, DocumentFlavor, HorizontalAlign, ICommandService, LocaleService, useDependency, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { TextEditor } from '@univerjs/docs-ui';
// import { TextEditor } from '@univerjs/docs-ui';
import { CheckMarkSingle, CloseSingle } from '@univerjs/icons';
import { useObservable } from '@univerjs/ui';
import clsx from 'clsx';
Expand Down Expand Up @@ -123,7 +123,7 @@ function DocFormula(props: { popupInfo: IUniFormulaPopupInfo }) {

return (
<div className={styles.docUiFormulaPopup} onMouseEnter={() => onHovered(true)} onMouseLeave={() => onHovered(false)}>
<TextEditor
{/* <TextEditor
id={UNI_FORMULA_EDITOR_ID}
className={clsx(styles.docUiFormulaPopupEditor, focused && styles.docUiFormulaPopupEditorActivated)}
placeholder={localeService.t('uni-formula.popup.placeholder')}
Expand All @@ -138,7 +138,7 @@ function DocFormula(props: { popupInfo: IUniFormulaPopupInfo }) {
setFocused(true);
}}
onBlur={() => setFocused(false)}
/>
/> */}
<div className={clsx(styles.formulaIcon, { [styles.formulaIconDisable]: !formulaString })}>
<span
className={clsx(styles.iconContainer, styles.iconContainerError)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { Nullable, Workbook } from '@univerjs/core';
import { BooleanNumber, DEFAULT_EMPTY_DOCUMENT_VALUE, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DocumentFlavor, HorizontalAlign, IPermissionService, IUniverInstanceService, Rectangle, ThemeService, UniverInstanceType, useDependency, useObservable, VerticalAlign, WrapStrategy } from '@univerjs/core';
import { TextEditor } from '@univerjs/docs-ui';
// import { TextEditor } from '@univerjs/docs-ui';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { CheckMarkSingle, CloseSingle, FxSingle } from '@univerjs/icons';
import { RangeProtectionPermissionEditPoint, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetSetCellValuePermission } from '@univerjs/sheets';
Expand Down Expand Up @@ -206,7 +206,7 @@ export function FormulaBar() {

return (
<div className={styles.uniFormulaBox}>
<TextEditor
{/* <TextEditor
id={DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY}
isSheetEditor
resizeCallBack={resizeCallBack}
Expand All @@ -215,7 +215,7 @@ export function FormulaBar() {
className={clsx(styles.uniFormulaInput, styles.formulaContent)}
snapshot={INITIAL_SNAPSHOT}
isSingle
/>
/> */}
<div className={clsx(styles.formulaIcon, { [styles.formulaIconDisable]: disable })}>
<span
className={clsx(styles.iconContainer, styles.iconContainerError)}
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/docs/data-model/document-data-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
* limitations under the License.
*/

import { BehaviorSubject } from 'rxjs';
import { UnitModel, UniverInstanceType } from '../../common/unit';
import { Tools } from '../../shared/tools';
import { getEmptySnapshot } from './empty-snapshot';
import { JSONX } from './json-x/json-x';
import { PRESET_LIST_TYPE } from './preset-list-type';
import { getBodySlice, SliceBodyType } from './text-x/utils';
import type { Nullable } from '../../shared';
import type {
IDocumentBody,
Expand All @@ -32,6 +25,13 @@ import type {
} from '../../types/interfaces/i-document-data';
import type { IPaddingData } from '../../types/interfaces/i-style-data';
import type { JSONXActions } from './json-x/json-x';
import { BehaviorSubject } from 'rxjs';
import { UnitModel, UniverInstanceType } from '../../common/unit';
import { Tools } from '../../shared/tools';
import { getEmptySnapshot } from './empty-snapshot';
import { JSONX } from './json-x/json-x';
import { PRESET_LIST_TYPE } from './preset-list-type';
import { getBodySlice, SliceBodyType } from './text-x/utils';

export const DEFAULT_DOC = {
id: 'default_doc',
Expand Down Expand Up @@ -228,6 +228,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {
headerModelMap: Map<string, DocumentDataModel> = new Map();

footerModelMap: Map<string, DocumentDataModel> = new Map();
change$ = new BehaviorSubject<number>(0);

constructor(snapshot: Partial<IDocumentData>) {
super(Tools.isEmptyObject(snapshot) ? getEmptySnapshot() : snapshot);
Expand Down Expand Up @@ -281,6 +282,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {

this.snapshot = { ...DEFAULT_DOC, ...snapshot };
this._initializeHeaderFooterModel();
this.change$.next(this.change$.value + 1);
}

getSelfOrHeaderFooterModel(segmentId?: string) {
Expand Down Expand Up @@ -315,6 +317,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {
this._initializeHeaderFooterModel();
}

this.change$.next(this.change$.value + 1);
return this.snapshot;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { addDrawing } from './drawings';
import { changeParagraphBulletNestLevel, setParagraphBullet, switchParagraphBullet, toggleChecklistParagraph } from './paragraph';
import { fromPlainText, getPlainText, isEmptyDocument } from './parse';
import { isSegmentIntersects, makeSelection, normalizeSelection } from './selection';
import { addCustomRangeTextX, deleteCustomRangeTextX, deleteSelectionTextX, replaceSelectionTextX, retainSelectionTextX } from './text-x-utils';
import { addCustomRangeTextX, deleteCustomRangeTextX, deleteSelectionTextX, replaceSelectionTextRuns, replaceSelectionTextX, retainSelectionTextX } from './text-x-utils';

export class BuildTextUtils {
static customRange = {
Expand All @@ -41,6 +41,7 @@ export class BuildTextUtils {
makeSelection,
normalizeSelection,
delete: deleteSelectionTextX,
replaceTextRuns: replaceSelectionTextRuns,
retain: retainSelectionTextX,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/

import type { ITextRange, ITextRangeParam } from '../../../../sheets/typedef';
import type { CustomRangeType, IDocumentBody } from '../../../../types/interfaces';
import type { CustomRangeType, IDocumentBody, ITextRun } from '../../../../types/interfaces';
import type { DocumentDataModel } from '../../document-data-model';
import type { TextXAction } from '../action-types';
import type { TextXSelection } from '../text-x';
import { type Nullable, Tools, UpdateDocsAttributeType } from '../../../../shared';
import { textDiff } from '../../../../shared/text-diff';
import { TextXActionType } from '../action-types';
import { TextX } from '../text-x';
import { getBodySlice } from '../utils';
import { getBodySlice, getTextRunSlice } from '../utils';
import { excludePointsFromRange, getIntersectingCustomRanges, getSelectionForAddCustomRange } from './custom-range';

export interface IDeleteCustomRangeParam {
Expand Down Expand Up @@ -328,3 +328,73 @@ export const replaceSelectionTextX = (params: IReplaceSelectionTextXParams) => {
textX.push(...actions);
return textX;
};

function isTextRunsEqual(textRuns: ITextRun[] | undefined, oldTextRuns: ITextRun[] | undefined) {
if (textRuns?.length === oldTextRuns?.length && textRuns?.every((textRun, index) => JSON.stringify(textRun) === JSON.stringify(oldTextRuns?.[index]))) {
weird94 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

return false;
}

export const replaceSelectionTextRuns = (params: IReplaceSelectionTextXParams) => {
const { selection, body: insertBody, doc } = params;
const segmentId = selection.segmentId;
const body = doc.getSelfOrHeaderFooterModel(segmentId)?.getBody();
if (!body) return false;

const oldBody = selection.collapsed ? null : getBodySlice(body, selection.startOffset, selection.endOffset);
const diffs = textDiff(oldBody ? oldBody.dataStream : '', insertBody.dataStream);
let cursor = 0;
const actions = diffs.map(([type, text]) => {
switch (type) {
// retain
case 0: {
const textRunsSlice = getTextRunSlice(insertBody, cursor, cursor + text.length, false);
const oldTextRunsSlice = getTextRunSlice(oldBody!, cursor, cursor + text.length, false);
const action: TextXAction = {
t: TextXActionType.RETAIN,
body: isTextRunsEqual(textRunsSlice, oldTextRunsSlice)
? undefined
: {
textRuns: textRunsSlice,
dataStream: '',
},
len: text.length,
};
cursor += text.length;
return action;
}
// insert
case 1: {
const action: TextXAction = {
t: TextXActionType.INSERT,
body: getBodySlice(insertBody, cursor, cursor + text.length),
len: text.length,
};
cursor += text.length;
return action;
}
// delete
default: {
const action: TextXAction = {
t: TextXActionType.DELETE,
len: text.length,
};
return action;
}
}
});

if (actions.every((action) => action.t === TextXActionType.RETAIN && !action.body)) {
return false;
}

const textX = new TextX();
textX.push({
t: TextXActionType.RETAIN,
len: selection.startOffset,
});
textX.push(...actions);
return textX;
};
Loading
Loading