-
-
Notifications
You must be signed in to change notification settings - Fork 537
/
core-editing.mutation.ts
148 lines (126 loc) · 5.97 KB
/
core-editing.mutation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* 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
*
* 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 { CommandType, IUniverInstanceService, JSONX } from '@univerjs/core';
import type { IMutation, IMutationCommonParams, JSONXActions, Nullable } from '@univerjs/core';
import { IRenderManagerService, type ITextRangeWithStyle } from '@univerjs/engine-render';
import { serializeTextRange, TextSelectionManagerService } from '../../services/text-selection-manager.service';
import type { IDocStateChangeParams } from '../../services/doc-state-change-manager.service';
import { DocStateChangeManagerService } from '../../services/doc-state-change-manager.service';
import { IMEInputManagerService } from '../../services/ime-input-manager.service';
import { DocSkeletonManagerService } from '../../services/doc-skeleton-manager.service';
export interface IRichTextEditingMutationParams extends IMutationCommonParams {
unitId: string;
segmentId?: string;
actions: JSONXActions;
textRanges: Nullable<ITextRangeWithStyle[]>;
prevTextRanges?: Nullable<ITextRangeWithStyle[]>;
noNeedSetTextRange?: boolean;
isCompositionEnd?: boolean;
noHistory?: boolean;
}
const RichTextEditingMutationId = 'doc.mutation.rich-text-editing';
/**
* The core mutator to change rich text actions. The execution result would be undo mutation params. Could be directly
* send to undo redo service (will be used by the triggering command).
*/
export const RichTextEditingMutation: IMutation<IRichTextEditingMutationParams, IRichTextEditingMutationParams> = {
id: RichTextEditingMutationId,
type: CommandType.MUTATION,
// eslint-disable-next-line max-lines-per-function
handler: (accessor, params) => {
const {
unitId,
segmentId = '',
actions,
textRanges,
prevTextRanges,
trigger,
noHistory,
isCompositionEnd,
noNeedSetTextRange,
} = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const renderManagerService = accessor.get(IRenderManagerService);
const documentDataModel = univerInstanceService.getUniverDocInstance(unitId);
const documentViewModel = renderManagerService.getRenderById(unitId)?.with(DocSkeletonManagerService).getViewModel();
if (documentDataModel == null || documentViewModel == null) {
throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${unitId}`);
}
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const selections = textSelectionManagerService.getSelections() ?? [];
const serializedSelections = selections.map(serializeTextRange);
const docStateChangeManagerService = accessor.get(DocStateChangeManagerService);
const imeInputManagerService = accessor.get(IMEInputManagerService);
// TODO: `disabled` is only used for read only demo, and will be removed in the future.
const disabled = !!documentDataModel.getSnapshot().disabled;
if (JSONX.isNoop(actions) || (actions && actions.length === 0) || disabled) {
// The actions' length maybe 0 when the mutation is from collaborative editing.
// The return result will not be used.
return {
unitId,
actions: [],
textRanges: serializedSelections,
};
}
// Step 1: Update Doc Data Model.
const undoActions = JSONX.invertWithDoc(actions, documentDataModel.getSnapshot());
documentDataModel.apply(actions);
// Step 2: Update Doc View Model.
const segmentDocumentDataModel = documentDataModel.getSelfOrHeaderFooterModel(segmentId);
const segmentViewModel = documentViewModel.getSelfOrHeaderFooterViewModel(segmentId);
segmentViewModel.reset(segmentDocumentDataModel);
// Step 3: Update cursor & selection.
// Make sure update cursor & selection after doc skeleton is calculated.
if (!noNeedSetTextRange && textRanges && trigger != null) {
queueMicrotask(() => {
textSelectionManagerService.replaceTextRanges(textRanges);
});
}
// Step 4: Emit state change event.
const changeState: IDocStateChangeParams = {
commandId: RichTextEditingMutationId,
unitId,
segmentId,
trigger,
noHistory,
redoState: {
actions,
textRanges,
},
undoState: {
actions: undoActions,
textRanges: prevTextRanges ?? serializedSelections,
},
};
// Handle IME input.
if (isCompositionEnd) {
const historyParams = imeInputManagerService.fetchComposedUndoRedoMutationParams();
if (historyParams == null) {
throw new Error('historyParams is null in RichTextEditingMutation');
}
const { undoMutationParams, redoMutationParams, previousActiveRange } = historyParams;
changeState.redoState.actions = redoMutationParams.actions;
changeState.undoState.actions = undoMutationParams.actions;
changeState.undoState.textRanges = [previousActiveRange];
}
docStateChangeManagerService.setChangeState(changeState);
return {
unitId,
actions: undoActions,
textRanges: serializedSelections,
};
},
};