@flowgram.ai/history 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,639 @@
1
+ import * as _flowgram_ai_utils from '@flowgram.ai/utils';
2
+ import { Emitter, Disposable, DisposableCollection } from '@flowgram.ai/utils';
3
+ import { ContainerModule } from 'inversify';
4
+ import * as _flowgram_ai_core from '@flowgram.ai/core';
5
+ import { PluginContext } from '@flowgram.ai/core';
6
+
7
+ declare class HistoryContext {
8
+ /**
9
+ * 所属uri
10
+ */
11
+ uri?: string;
12
+ /**
13
+ * 操作触发的源对象,如编辑器对象
14
+ */
15
+ source?: unknown;
16
+ }
17
+
18
+ declare class HistoryConfig {
19
+ generateId: () => string;
20
+ getSnapshot: () => unknown;
21
+ }
22
+
23
+ declare class OperationService {
24
+ readonly operationRegistry: OperationRegistry;
25
+ readonly context: HistoryContext;
26
+ config: HistoryConfig;
27
+ readonly applyEmitter: Emitter<Operation<unknown>>;
28
+ readonly onApply: _flowgram_ai_utils.Event<Operation<unknown>>;
29
+ private _toDispose;
30
+ init(): void;
31
+ /**
32
+ * 执行操作
33
+ * @param op
34
+ * @returns
35
+ */
36
+ applyOperation(op: Operation, options?: {
37
+ noApply?: boolean;
38
+ }): any;
39
+ /**
40
+ * 根据操作类型获取操作的label
41
+ * @param operation 操作
42
+ * @returns
43
+ */
44
+ getOperationLabel(operation: Operation): string | undefined;
45
+ /**
46
+ * 根据操作类型获取操作的description
47
+ * @param operation 操作
48
+ * @returns
49
+ */
50
+ getOperationDescription(operation: Operation): string | undefined;
51
+ /**
52
+ * 操作取反
53
+ * @param operations
54
+ * @returns
55
+ */
56
+ inverseOperations(operations: Operation[]): Operation<unknown>[];
57
+ inverseOperation(op: Operation): Operation;
58
+ dispose(): void;
59
+ }
60
+
61
+ /**
62
+ * 历史栈,聚合所有历史操作
63
+ */
64
+ declare class HistoryStack {
65
+ historyConfig: HistoryConfig;
66
+ private _items;
67
+ readonly onChangeEmitter: Emitter<HistoryStackChangeEvent>;
68
+ readonly onChange: _flowgram_ai_utils.Event<HistoryStackChangeEvent>;
69
+ private _toDispose;
70
+ limit: number;
71
+ constructor();
72
+ get items(): HistoryItem[];
73
+ add(service: HistoryService, item: HistoryStackItem): HistoryItem;
74
+ findById(id: string): HistoryItem | undefined;
75
+ changeByIndex(index: number, service: HistoryService, item: HistoryStackItem): void;
76
+ addOperation(service: HistoryService, id: string, op: OperationWithId): void;
77
+ updateOperation(service: HistoryService, id: string, op: OperationWithId): void;
78
+ clear(): void;
79
+ dispose(): void;
80
+ private _getHistoryItem;
81
+ private _getHistoryOperation;
82
+ static dateFormat(timestamp: number): string;
83
+ }
84
+
85
+ declare class HistoryManager implements IHistoryManager {
86
+ readonly historyStack: HistoryStack;
87
+ readonly historyConfig: HistoryConfig;
88
+ private _historyServices;
89
+ private _toDispose;
90
+ registerHistoryService(service: HistoryService): void;
91
+ unregisterHistoryService(service: HistoryService): void;
92
+ getHistoryServiceByURI(uri: string): HistoryService | undefined;
93
+ getFirstHistoryService(): HistoryService | undefined;
94
+ dispose(): void;
95
+ _handleMerge(service: HistoryService, event: HistoryMergeEvent): void;
96
+ }
97
+
98
+ declare class HistoryService implements IHistoryService {
99
+ readonly undoRedoService: UndoRedoService;
100
+ readonly operationRegistry: OperationRegistry;
101
+ readonly operationService: OperationService;
102
+ readonly context: HistoryContext;
103
+ readonly config: HistoryConfig;
104
+ historyManager: HistoryManager;
105
+ private _toDispose;
106
+ private _transacting;
107
+ private _transactOperation;
108
+ private _locked;
109
+ private _willDisposeEmitter;
110
+ private _mergeEmitter;
111
+ onWillDispose: _flowgram_ai_utils.Event<HistoryService>;
112
+ onMerge: _flowgram_ai_utils.Event<HistoryMergeEvent>;
113
+ get onApply(): _flowgram_ai_utils.Event<Operation<unknown>>;
114
+ init(): void;
115
+ start(): void;
116
+ stop(): void;
117
+ limit(num: number): void;
118
+ startTransaction(): void;
119
+ endTransaction(): void;
120
+ transact(transaction: () => void): void;
121
+ pushOperation(operation: Operation, options?: PushOperationOptions): any;
122
+ getHistoryOperations(): Operation<unknown>[];
123
+ undo(): Promise<void>;
124
+ redo(): Promise<void>;
125
+ canUndo(): boolean;
126
+ canRedo(): boolean;
127
+ getSnapshot(): unknown;
128
+ getRecords(): Promise<HistoryRecord[]>;
129
+ restore(historyRecord: HistoryRecord): Promise<void>;
130
+ clear(): void;
131
+ dispose(): void;
132
+ private _canPush;
133
+ private _pushStackOperation;
134
+ private _shouldMerge;
135
+ }
136
+
137
+ interface HistoryRecord {
138
+ snapshot: any;
139
+ stack: any[];
140
+ }
141
+ interface HistoryItem extends HistoryStackItem {
142
+ id: string;
143
+ time: string;
144
+ operations: HistoryOperation[];
145
+ }
146
+ /**
147
+ * 历史服务管理
148
+ */
149
+ interface IHistoryManager {
150
+ /**
151
+ * 注册历史服务
152
+ * @param service 历史服务示例
153
+ */
154
+ registerHistoryService(service: IHistoryService): void;
155
+ /**
156
+ * 取消注册历史服务
157
+ * @param service 历史服务示例
158
+ */
159
+ unregisterHistoryService(service: HistoryService): void;
160
+ }
161
+ /**
162
+ * 历史服务
163
+ */
164
+ interface IHistoryService extends Disposable {
165
+ /**
166
+ * 添加操作
167
+ * @param operation 操作
168
+ */
169
+ pushOperation(operation: Operation): void | Promise<void>;
170
+ /**
171
+ * 获取所有历史操作
172
+ */
173
+ getHistoryOperations(): Operation[];
174
+ /**
175
+ * 撤回
176
+ */
177
+ undo(): void | Promise<void>;
178
+ /**
179
+ * 重做
180
+ */
181
+ redo(): void | Promise<void>;
182
+ /**
183
+ * 是否有可撤销的操作
184
+ */
185
+ canUndo(): boolean;
186
+ /**
187
+ * 是否有可重做的操作
188
+ */
189
+ canRedo(): boolean;
190
+ /**
191
+ * 获取历史记录
192
+ */
193
+ getRecords(): Promise<HistoryRecord[]>;
194
+ /**
195
+ * 根据历史版本重新存储历史记录
196
+ * @param historyRecord 历史记录
197
+ */
198
+ restore(historyRecord: HistoryRecord): Promise<void>;
199
+ /**
200
+ * 清空undo/redo
201
+ */
202
+ clear(): void;
203
+ /**
204
+ * 最大数量限制
205
+ * @param num 数量
206
+ */
207
+ limit(num: number): void;
208
+ /**
209
+ * 返回快照
210
+ */
211
+ getSnapshot(): unknown;
212
+ }
213
+ interface IOperationService {
214
+ pushOperation(operation: Operation): void;
215
+ }
216
+ /**
217
+ * UndoRedo服务
218
+ */
219
+ interface IUndoRedoService extends Disposable {
220
+ /**
221
+ * 添加一个undo/redo元素
222
+ * @param element 可undo/redo的元素
223
+ */
224
+ pushElement(element: IUndoRedoElement): void;
225
+ /**
226
+ * 获取最后一个可undo的元素
227
+ */
228
+ getLastElement(): IUndoRedoElement;
229
+ /**
230
+ * 获取undo栈
231
+ */
232
+ getUndoStack(): IUndoRedoElement[];
233
+ /**
234
+ * 获取redo栈
235
+ */
236
+ getRedoStack(): IUndoRedoElement[];
237
+ /**
238
+ * 清空redo栈
239
+ */
240
+ clearRedoStack(): void;
241
+ /**
242
+ * 是否可undo
243
+ */
244
+ canUndo(): boolean;
245
+ /**
246
+ * 执行undo
247
+ */
248
+ undo(): Promise<void> | void;
249
+ /**
250
+ * 是否可redo
251
+ */
252
+ canRedo(): boolean;
253
+ /**
254
+ * 执行redo
255
+ */
256
+ redo(): Promise<void> | void;
257
+ /**
258
+ * 清空 undo和redo栈
259
+ */
260
+ clear(): void;
261
+ }
262
+ /**
263
+ * UndoRedo元素
264
+ */
265
+ interface IUndoRedoElement extends Disposable {
266
+ /**
267
+ * 操作标题
268
+ */
269
+ readonly label?: string;
270
+ /**
271
+ * 操作描述
272
+ */
273
+ readonly description?: string;
274
+ /**
275
+ * 撤销
276
+ */
277
+ undo(): Promise<void> | void;
278
+ /**
279
+ * 重做
280
+ */
281
+ redo(): Promise<void> | void;
282
+ /**
283
+ * 添加一个操作
284
+ * @param operation 操作
285
+ */
286
+ pushOperation(operation: Operation): Operation;
287
+ /**
288
+ * 获取所有操作
289
+ */
290
+ getOperations(): Operation[];
291
+ /**
292
+ * 获取第一个操作
293
+ */
294
+ getFirstOperation(): Operation;
295
+ /**
296
+ * 获取最后一个操作
297
+ */
298
+ getLastOperation(): Operation;
299
+ /**
300
+ * 获取修改的操作
301
+ */
302
+ getChangeOperations(type: UndoRedoChangeType): Operation[];
303
+ }
304
+ /**
305
+ * 操作注册
306
+ */
307
+ interface IOperationRegistry {
308
+ register(type: string, factory: IUndoRedoElementFactory<unknown>): void;
309
+ }
310
+ /**
311
+ * 操作工厂
312
+ */
313
+ type IUndoRedoElementFactory<OperationValue> = (operation: Operation<OperationValue>) => IUndoRedoElement;
314
+ /**
315
+ * undo redo 类型
316
+ */
317
+ declare enum UndoRedoChangeType {
318
+ UNDO = "undo",
319
+ REDO = "redo",
320
+ PUSH = "push",
321
+ CLEAR = "clear"
322
+ }
323
+ /**
324
+ * 带element的事件
325
+ */
326
+ interface UndoRedoChangeElementEvent {
327
+ type: UndoRedoChangeType.PUSH | UndoRedoChangeType.UNDO | UndoRedoChangeType.REDO;
328
+ element: IUndoRedoElement;
329
+ }
330
+ /**
331
+ * 清空事件
332
+ */
333
+ interface UndoRedoClearEvent {
334
+ type: UndoRedoChangeType.CLEAR;
335
+ }
336
+ /**
337
+ * undo redo变化事件
338
+ */
339
+ type UndoRedoChangeEvent = UndoRedoChangeElementEvent | UndoRedoClearEvent;
340
+ interface HistoryStackItem {
341
+ id: string;
342
+ type: UndoRedoChangeType;
343
+ timestamp: number;
344
+ operations: Operation[];
345
+ uri?: string;
346
+ }
347
+ /**
348
+ * 历史栈变化类型
349
+ */
350
+ declare enum HistoryStackChangeType {
351
+ ADD = "add",
352
+ UPDATE = "update",
353
+ CLEAR = "clear",
354
+ ADD_OPERATION = "add_operation",
355
+ UPDATE_OPERATION = "update_operation"
356
+ }
357
+ /**
358
+ * 历史栈变化事件基础
359
+ */
360
+ interface HistoryStackBaseEvent {
361
+ type: HistoryStackChangeType;
362
+ value?: any;
363
+ service: HistoryService;
364
+ }
365
+ /**
366
+ * 添加历史事件
367
+ */
368
+ interface HistoryStackAddEvent extends HistoryStackBaseEvent {
369
+ type: HistoryStackChangeType.ADD;
370
+ value: HistoryItem;
371
+ }
372
+ /**
373
+ * 更新历史事件
374
+ */
375
+ interface HistoryStackUpdateEvent extends HistoryStackBaseEvent {
376
+ type: HistoryStackChangeType.UPDATE;
377
+ value: HistoryItem;
378
+ }
379
+ /**
380
+ * 添加操作事件
381
+ */
382
+ interface HistoryStackAddOperationEvent extends HistoryStackBaseEvent {
383
+ type: HistoryStackChangeType.ADD_OPERATION;
384
+ value: {
385
+ historyItem: HistoryItem;
386
+ operation: HistoryOperation;
387
+ };
388
+ }
389
+ /**
390
+ * 更新操作事件
391
+ */
392
+ interface HistoryStackUpdateOperationEvent extends HistoryStackBaseEvent {
393
+ type: HistoryStackChangeType.UPDATE_OPERATION;
394
+ value: {
395
+ historyItem: HistoryItem;
396
+ operation: HistoryOperation;
397
+ };
398
+ }
399
+ /**
400
+ * 历史记录变化事件
401
+ */
402
+ type HistoryStackChangeEvent = HistoryStackAddEvent | HistoryStackUpdateEvent | HistoryStackAddOperationEvent | HistoryStackUpdateOperationEvent;
403
+ declare enum HistoryMergeEventType {
404
+ ADD = "ADD",
405
+ UPDATE = "UPDATE"
406
+ }
407
+ /**
408
+ * 历史合并事件
409
+ */
410
+ type HistoryMergeEvent = {
411
+ type: HistoryMergeEventType.ADD;
412
+ value: {
413
+ element: IUndoRedoElement;
414
+ operation: Operation;
415
+ };
416
+ } | {
417
+ type: HistoryMergeEventType.UPDATE;
418
+ value: {
419
+ element: IUndoRedoElement;
420
+ operation: Operation;
421
+ value: any;
422
+ };
423
+ };
424
+
425
+ declare class UndoRedoService implements IUndoRedoService {
426
+ private _undoStack;
427
+ private _redoStack;
428
+ private _undoing;
429
+ private _redoing;
430
+ private _limit;
431
+ protected onChangeEmitter: Emitter<UndoRedoChangeEvent>;
432
+ readonly onChange: _flowgram_ai_utils.Event<UndoRedoChangeEvent>;
433
+ readonly _toDispose: DisposableCollection;
434
+ constructor();
435
+ setLimit(limit: number): void;
436
+ pushElement(element: IUndoRedoElement): void;
437
+ getUndoStack(): IUndoRedoElement[];
438
+ getRedoStack(): IUndoRedoElement[];
439
+ getLastElement(): IUndoRedoElement;
440
+ /**
441
+ * 执行undo
442
+ * @returns void
443
+ */
444
+ undo(): Promise<void>;
445
+ /**
446
+ * 执行redo
447
+ * @returns void
448
+ */
449
+ redo(): Promise<void>;
450
+ /**
451
+ * 是否可undo
452
+ * @returns true代表可以,false代表不可以
453
+ */
454
+ canUndo(): boolean;
455
+ /**
456
+ * 是否可redo
457
+ * @returns true代表可以,false代表不可以
458
+ */
459
+ canRedo(): boolean;
460
+ /**
461
+ * 是否可以push
462
+ * @returns true代表可以,false代表不可以
463
+ */
464
+ canPush(): boolean;
465
+ /**
466
+ * 清空
467
+ */
468
+ clear(): void;
469
+ /**
470
+ * 清空redo栈
471
+ */
472
+ clearRedoStack(): void;
473
+ /**
474
+ * 清空undo栈
475
+ */
476
+ clearUndoStack(): void;
477
+ /**
478
+ * 销毁
479
+ */
480
+ dispose(): void;
481
+ private _stackPush;
482
+ private _emitChange;
483
+ }
484
+
485
+ declare class StackOperation implements IUndoRedoElement {
486
+ label?: string | undefined;
487
+ description?: string | undefined;
488
+ private _operations;
489
+ private _toDispose;
490
+ private _timestamp;
491
+ private _operationService;
492
+ private _id;
493
+ get id(): string;
494
+ constructor(operationService: OperationService, operations?: Operation[]);
495
+ getTimestamp(): number;
496
+ pushOperation(operation: Operation): OperationWithId;
497
+ getOperations(): Operation[];
498
+ getChangeOperations(type: UndoRedoChangeType): Operation[];
499
+ getFirstOperation(): Operation;
500
+ getLastOperation(): Operation<unknown>;
501
+ undo(): Promise<void>;
502
+ redo(): Promise<void>;
503
+ revert(type: UndoRedoChangeType): void | Promise<void>;
504
+ private _inverse;
505
+ private _apply;
506
+ private _operation;
507
+ dispose(): void;
508
+ }
509
+
510
+ /**
511
+ * 操作
512
+ */
513
+ interface Operation<OperationValue = unknown> {
514
+ /**
515
+ * 操作的类型 如insert_node, move_node等
516
+ */
517
+ type: string;
518
+ /**
519
+ * 操作的值 外部自定义
520
+ */
521
+ value: OperationValue;
522
+ /**
523
+ * 资源唯一标志
524
+ */
525
+ uri?: string;
526
+ /**
527
+ * 操作触发源头
528
+ */
529
+ origin?: string | Symbol;
530
+ }
531
+ type OperationWithId = Operation & {
532
+ id: string;
533
+ };
534
+ /**
535
+ * push操作配置
536
+ */
537
+ interface PushOperationOptions {
538
+ noApply?: boolean;
539
+ }
540
+ /**
541
+ * 操作历史
542
+ */
543
+ interface HistoryOperation extends Operation {
544
+ /**
545
+ * 唯一id
546
+ */
547
+ id: string;
548
+ /**
549
+ * 显示名称
550
+ */
551
+ label?: string;
552
+ /**
553
+ * 描述
554
+ */
555
+ description?: string;
556
+ /**
557
+ * 时间戳
558
+ */
559
+ timestamp: number;
560
+ }
561
+ /**
562
+ * 操作元数据
563
+ */
564
+ interface OperationMeta<OperationValue = any, Source = any, ApplyResult = any> {
565
+ /**
566
+ * 操作类型 需要唯一
567
+ */
568
+ type: string;
569
+ /**
570
+ * 将一个操作转换成另一个逆操作, 如insert转成delete
571
+ * @param op 操作
572
+ * @returns 逆操作
573
+ */
574
+ inverse: (op: Operation<OperationValue>) => Operation<OperationValue>;
575
+ /**
576
+ * 判断是否可以合并
577
+ * @param op 操作
578
+ * @param prev 上一个操作
579
+ * @returns true表示可以合并 返回一个操作表示直接用新操作替换之前的操作
580
+ */
581
+ shouldMerge?: (op: Operation<OperationValue>, prev: Operation<OperationValue> | undefined, stackItem: StackOperation) => boolean | Operation;
582
+ /**
583
+ * 判断是否需要保存,如选中等操作可以不保存
584
+ * @param op 操作
585
+ * @returns true表示可以保存
586
+ */
587
+ shouldSave?: (op: Operation<OperationValue>) => boolean;
588
+ /**
589
+ * 执行操作
590
+ * @param operation 操作
591
+ */
592
+ apply(operation: Operation<OperationValue>, source: Source): ApplyResult | Promise<ApplyResult>;
593
+ /**
594
+ * 获取标签
595
+ */
596
+ getLabel?: (operation: Operation<OperationValue>, source: Source) => string;
597
+ /**
598
+ * 获取描述
599
+ */
600
+ getDescription?: (operation: Operation<OperationValue>, source: Source) => string;
601
+ /**
602
+ * 获取uri
603
+ */
604
+ getURI?: (operation: Operation<OperationValue>, source: Source) => string | undefined;
605
+ }
606
+
607
+ declare class OperationRegistry {
608
+ private readonly _operationMetas;
609
+ protected readonly contributions: OperationContribution[];
610
+ protected init(): void;
611
+ /**
612
+ * 注册操作的元数据
613
+ * @param operationMeta 操作的元数据
614
+ * @returns 销毁函数
615
+ */
616
+ registerOperationMeta(operationMeta: OperationMeta): Disposable;
617
+ /**
618
+ * 获取操作的元数据
619
+ * @param type 操作类型
620
+ * @returns 操作的元数据
621
+ */
622
+ getOperationMeta(type: string): OperationMeta | undefined;
623
+ private _doRegisterOperationMetaMeta;
624
+ }
625
+
626
+ declare const OperationContribution: unique symbol;
627
+ interface OperationContribution {
628
+ registerOperationMeta?(operationRegistry: OperationRegistry): void;
629
+ }
630
+
631
+ declare const HistoryContainerModule: ContainerModule;
632
+
633
+ interface HistoryPluginOptions<T = PluginContext> {
634
+ enable?: boolean;
635
+ onApply?: (ctx: T, operation: Operation) => void;
636
+ }
637
+ declare const createHistoryPlugin: _flowgram_ai_core.PluginCreator<HistoryPluginOptions<PluginContext>>;
638
+
639
+ export { HistoryConfig, HistoryContainerModule, HistoryContext, type HistoryItem, HistoryManager, type HistoryMergeEvent, HistoryMergeEventType, type HistoryOperation, type HistoryPluginOptions, type HistoryRecord, HistoryService, HistoryStack, type HistoryStackAddEvent, type HistoryStackAddOperationEvent, type HistoryStackBaseEvent, type HistoryStackChangeEvent, HistoryStackChangeType, type HistoryStackItem, type HistoryStackUpdateEvent, type HistoryStackUpdateOperationEvent, type IHistoryManager, type IHistoryService, type IOperationRegistry, type IOperationService, type IUndoRedoElement, type IUndoRedoElementFactory, type IUndoRedoService, type Operation, OperationContribution, type OperationMeta, OperationRegistry, OperationService, type OperationWithId, type PushOperationOptions, StackOperation, type UndoRedoChangeElementEvent, type UndoRedoChangeEvent, UndoRedoChangeType, type UndoRedoClearEvent, UndoRedoService, createHistoryPlugin };