GeezerVison is showing github.com — docs(history): optimize api doc for model/history, and fix related li… · li55/lowcode-engine@fec8805
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/model/history.md +80-14Lines changed: 80 additions & 14 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -12,47 +12,113 @@ sidebar_position: 5 | 12 | 12 | ## 方法签名 | 13 | 13 | ### go | 14 | 14 |
| 15 | | -go(cursor: number) | 16 | | - | 17 | 15 | 历史记录跳转到指定位置 | 18 | 16 |
| 19 | | -### back | | 17 | +```typescript | | 18 | +/** | | 19 | + * 历史记录跳转到指定位置 | | 20 | + * go to a specific history | | 21 | + * @param cursor | | 22 | + */ | | 23 | +go(cursor: number): void; | | 24 | +``` | 20 | 25 |
| 21 | | -back() | | 26 | +### back | 22 | 27 |
| 23 | 28 | 历史记录后退 | 24 | 29 |
| | 30 | +```typescript | | 31 | +/** | | 32 | + * 历史记录后退 | | 33 | + * go backward in history | | 34 | + */ | | 35 | +back(): void; | | 36 | +``` | | 37 | + | 25 | 38 | ### forward | 26 | 39 |
| 27 | 40 | forward() | 28 | 41 |
| 29 | 42 | 历史记录前进 | 30 | | -### savePoint | 31 | 43 |
| 32 | | -savePoint() | | 44 | +```typescript | | 45 | +/** | | 46 | + * 历史记录前进 | | 47 | + * go forward in history | | 48 | + */ | | 49 | +forward(): void; | | 50 | +``` | | 51 | + | | 52 | +### savePoint | 33 | 53 |
| 34 | 54 | 保存当前状态 | 35 | | -### isSavePoint | 36 | 55 |
| 37 | | -isSavePoint() | | 56 | +```typescript | | 57 | +/** | | 58 | + * 保存当前状态 | | 59 | + * do save current change as a record in history | | 60 | + */ | | 61 | +savePoint(): void; | | 62 | +``` | | 63 | + | | 64 | +### isSavePoint | 38 | 65 |
| 39 | 66 | 当前是否是「保存点」,即是否有状态变更但未保存 | 40 | 67 |
| 41 | | -### getState | | 68 | +```typescript | | 69 | +/** | | 70 | + * 当前是否是「保存点」,即是否有状态变更但未保存 | | 71 | + * check if there is unsaved change for history | | 72 | + */ | | 73 | +isSavePoint(): boolean; | | 74 | +``` | 42 | 75 |
| 43 | | -getState() | | 76 | +### getState | 44 | 77 |
| 45 | 78 | 获取 state,判断当前是否为「可回退」、「可前进」的状态 | 46 | 79 |
| | 80 | +```typescript | | 81 | +/** | | 82 | + * 获取 state,判断当前是否为「可回退」、「可前进」的状态 | | 83 | + * get flags in number which indicat current change state | | 84 | + * | | 85 | + * | 1 | 1 | 1 | | | 86 | + * | -------- | -------- | -------- | | | 87 | + * | modified | redoable | undoable | | | 88 | + * eg: | | 89 | + * 7 means : modified && redoable && undoable | | 90 | + * 5 means : modified && undoable | | 91 | + */ | | 92 | +getState(): number; | | 93 | +``` | | 94 | + | 47 | 95 | ## 事件 | 48 | 96 | ### onChangeState | 49 | 97 |
| 50 | | -onChangeState(func: () => any) | 51 | | - | 52 | 98 | 监听 state 变更事件 | 53 | 99 |
| 54 | | -### onChangeCursor | | 100 | +```typescript | | 101 | +/** | | 102 | + * 监听 state 变更事件 | | 103 | + * monitor on stateChange event | | 104 | + * @param func | | 105 | + */ | | 106 | +onChangeState(func: () => any): IPublicTypeDisposable; | | 107 | +``` | 55 | 108 |
| 56 | | -onChangeCursor(func: () => any) | | 109 | +相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts) | | 110 | + | | 111 | +### onChangeCursor | 57 | 112 |
| 58 | 113 | 监听历史记录游标位置变更事件 | | 114 | + | | 115 | +```typescript | | 116 | +/** | | 117 | + * 监听历史记录游标位置变更事件 | | 118 | + * monitor on cursorChange event | | 119 | + * @param func | | 120 | + */ | | 121 | +onChangeCursor(func: () => any): IPublicTypeDisposable; | | 122 | +``` | | 123 | + | | 124 | +相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts) |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/document/history.ts +19-8Lines changed: 19 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { reaction, untracked, globalContext, IEventBus, createModuleEventBus } from '@alilc/lowcode-editor-core'; | 2 | 2 | import { IPublicTypeNodeSchema, IPublicModelHistory } from '@alilc/lowcode-types'; | | 3 | +import { Logger } from '@alilc/lowcode-utils'; | | 4 | + | | 5 | +const logger = new Logger({ level: 'warn', bizName: 'history' }); | 3 | 6 |
| 4 | 7 | export interface Serialization<K = IPublicTypeNodeSchema, T = string> { | 5 | 8 | serialize(data: K): T; | @@ -30,11 +33,15 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory { | 30 | 33 | }, | 31 | 34 | }; | 32 | 35 |
| 33 | | - setSerialization(serialization: Serialization<T, string>) { | 34 | | - this.currentSerialization = serialization; | | 36 | + get hotData() { | | 37 | + return this.session.data; | 35 | 38 | } | 36 | 39 |
| 37 | | - constructor(dataFn: () => T | null, private redoer: (data: T) => void, private timeGap: number = 1000) { | | 40 | + constructor( | | 41 | + dataFn: () => T | null, | | 42 | + private redoer: (data: T) => void, | | 43 | + private timeGap: number = 1000, | | 44 | + ) { | 38 | 45 | this.session = new Session(0, null, this.timeGap); | 39 | 46 | this.records = [this.session]; | 40 | 47 |
| @@ -68,8 +75,8 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory { | 68 | 75 | }, { fireImmediately: true }); | 69 | 76 | } | 70 | 77 |
| 71 | | - get hotData() { | 72 | | - return this.session.data; | | 78 | + setSerialization(serialization: Serialization<T, string>) { | | 79 | + this.currentSerialization = serialization; | 73 | 80 | } | 74 | 81 |
| 75 | 82 | isSavePoint(): boolean { | @@ -84,16 +91,18 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory { | 84 | 91 | this.asleep = false; | 85 | 92 | } | 86 | 93 |
| 87 | | - go(cursor: number) { | | 94 | + go(originalCursor: number) { | 88 | 95 | this.session.end(); | 89 | 96 |
| 90 | | - const currentCursor = this.session.cursor; | | 97 | + let cursor = originalCursor; | 91 | 98 | cursor = +cursor; | 92 | 99 | if (cursor < 0) { | 93 | 100 | cursor = 0; | 94 | 101 | } else if (cursor >= this.records.length) { | 95 | 102 | cursor = this.records.length - 1; | 96 | 103 | } | | 104 | + | | 105 | + const currentCursor = this.session.cursor; | 97 | 106 | if (cursor === currentCursor) { | 98 | 107 | return; | 99 | 108 | } | @@ -106,7 +115,7 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory { | 106 | 115 | this.redoer(this.currentSerialization.unserialize(hotData)); | 107 | 116 | this.emitter.emit('cursor', hotData); | 108 | 117 | } catch (e) /* istanbul ignore next */ { | 109 | | - console.error(e); | | 118 | + logger.error(e); | 110 | 119 | } | 111 | 120 |
| 112 | 121 | this.wakeup(); | @@ -174,6 +183,7 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory { | 174 | 183 | } | 175 | 184 | return state; | 176 | 185 | } | | 186 | + | 177 | 187 | /** | 178 | 188 | * 监听 state 变更事件 | 179 | 189 | * @param func | @@ -209,6 +219,7 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory { | 209 | 219 | this.emitter.removeAllListeners(); | 210 | 220 | this.records = []; | 211 | 221 | } | | 222 | + | 212 | 223 | /** | 213 | 224 | * | 214 | 225 | * @deprecated | |
Collapse file Copy file name to clipboardExpand all lines: packages/editor-core/src/utils/preference.ts +4-8Lines changed: 4 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,15 +2,12 @@ import store from 'store'; | 2 | 2 | import { getLogger } from './logger'; | 3 | 3 | import { IPublicModelPreference } from '@alilc/lowcode-types'; | 4 | 4 |
| 5 | | -const logger = getLogger({ level: 'log', bizName: 'Preference' }); | | 5 | +const logger = getLogger({ level: 'warn', bizName: 'Preference' }); | 6 | 6 | const STORAGE_KEY_PREFIX = 'ale'; | 7 | 7 |
| 8 | | - | 9 | 8 | /** | 10 | 9 | * used to store user preferences, such as pinned status of a pannel. | 11 | 10 | * save to local storage. | 12 | | - * | 13 | | - * @class PreferenceStore | 14 | 11 | */ | 15 | 12 | export default class Preference implements IPublicModelPreference { | 16 | 13 | getStorageKey(key: string, module?: string): string { | @@ -24,7 +21,7 @@ export default class Preference implements IPublicModelPreference { | 24 | 21 | return; | 25 | 22 | } | 26 | 23 | const storageKey = this.getStorageKey(key, module); | 27 | | - logger.log('storageKey:', storageKey, 'set with value:', value); | | 24 | + logger.debug('storageKey:', storageKey, 'set with value:', value); | 28 | 25 | store.set(storageKey, value); | 29 | 26 | } | 30 | 27 |
| @@ -35,16 +32,15 @@ export default class Preference implements IPublicModelPreference { | 35 | 32 | } | 36 | 33 | const storageKey = this.getStorageKey(key, module); | 37 | 34 | const result = store.get(storageKey); | 38 | | - logger.log('storageKey:', storageKey, 'get with result:', result); | | 35 | + logger.debug('storageKey:', storageKey, 'get with result:', result); | 39 | 36 | return result; | 40 | 37 | } | | 38 | + | 41 | 39 | /** | 42 | 40 | * check if local storage contain certain key | 43 | 41 | * | 44 | 42 | * @param {string} key | 45 | 43 | * @param {string} module | 46 | | - * @returns {boolean} | 47 | | - * @memberof Preference | 48 | 44 | */ | 49 | 45 | contains(key: string, module: string): boolean { | 50 | 46 | if (!key || typeof key !== 'string' || key.length === 0) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shell/src/model/history.ts +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { DocumentModel as InnerDocumentModel } from '@alilc/lowcode-designer'; | 2 | 2 | import { historySymbol, documentSymbol } from '../symbols'; | 3 | | -import { IPublicModelHistory } from '@alilc/lowcode-types'; | | 3 | +import { IPublicModelHistory, IPublicTypeDisposable } from '@alilc/lowcode-types'; | 4 | 4 |
| 5 | 5 | export class History implements IPublicModelHistory { | 6 | 6 | private readonly [documentSymbol]: InnerDocumentModel; | @@ -54,7 +54,7 @@ export class History implements IPublicModelHistory { | 54 | 54 | * 获取 state,判断当前是否为「可回退」、「可前进」的状态 | 55 | 55 | * @returns | 56 | 56 | */ | 57 | | - getState(): any { | | 57 | + getState(): number { | 58 | 58 | return this[historySymbol].getState(); | 59 | 59 | } | 60 | 60 |
| @@ -63,7 +63,7 @@ export class History implements IPublicModelHistory { | 63 | 63 | * @param func | 64 | 64 | * @returns | 65 | 65 | */ | 66 | | - onChangeState(func: () => any): () => void { | | 66 | + onChangeState(func: () => any): IPublicTypeDisposable { | 67 | 67 | return this[historySymbol].onStateChange(func); | 68 | 68 | } | 69 | 69 |
| @@ -72,7 +72,7 @@ export class History implements IPublicModelHistory { | 72 | 72 | * @param func | 73 | 73 | * @returns | 74 | 74 | */ | 75 | | - onChangeCursor(func: () => any): () => void { | | 75 | + onChangeCursor(func: () => any): IPublicTypeDisposable { | 76 | 76 | return this[historySymbol].onCursor(func); | 77 | 77 | } | 78 | 78 | } |
Collapse file Copy file name to clipboard +20-7Lines changed: 20 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { IPublicTypeDisposable } from '../type'; | | 2 | + | 1 | 3 | export interface IPublicModelHistory { | 2 | 4 |
| 3 | 5 | /** | 4 | 6 | * 历史记录跳转到指定位置 | | 7 | + * go to a specific history | 5 | 8 | * @param cursor | 6 | 9 | */ | 7 | 10 | go(cursor: number): void; | 8 | 11 |
| 9 | 12 | /** | 10 | 13 | * 历史记录后退 | | 14 | + * go backward in history | 11 | 15 | */ | 12 | 16 | back(): void; | 13 | 17 |
| 14 | 18 | /** | 15 | 19 | * 历史记录前进 | | 20 | + * go forward in history | 16 | 21 | */ | 17 | 22 | forward(): void; | 18 | 23 |
| 19 | 24 | /** | 20 | 25 | * 保存当前状态 | | 26 | + * do save current change as a record in history | 21 | 27 | */ | 22 | 28 | savePoint(): void; | 23 | 29 |
| 24 | 30 | /** | 25 | 31 | * 当前是否是「保存点」,即是否有状态变更但未保存 | 26 | | - * @returns | | 32 | + * check if there is unsaved change for history | 27 | 33 | */ | 28 | 34 | isSavePoint(): boolean; | 29 | 35 |
| 30 | 36 | /** | 31 | 37 | * 获取 state,判断当前是否为「可回退」、「可前进」的状态 | 32 | | - * @returns | | 38 | + * get flags in number which indicat current change state | | 39 | + * | | 40 | + * | 1 | 1 | 1 | | | 41 | + * | -------- | -------- | -------- | | | 42 | + * | modified | redoable | undoable | | | 43 | + * eg: | | 44 | + * 7 means : modified && redoable && undoable | | 45 | + * 5 means : modified && undoable | 33 | 46 | */ | 34 | | - getState(): any; | | 47 | + getState(): number; | 35 | 48 |
| 36 | 49 | /** | 37 | 50 | * 监听 state 变更事件 | | 51 | + * monitor on stateChange event | 38 | 52 | * @param func | 39 | | - * @returns | 40 | 53 | */ | 41 | | - onChangeState(func: () => any): () => void; | | 54 | + onChangeState(func: () => any): IPublicTypeDisposable; | 42 | 55 |
| 43 | 56 | /** | 44 | 57 | * 监听历史记录游标位置变更事件 | | 58 | + * monitor on cursorChange event | 45 | 59 | * @param func | 46 | | - * @returns | 47 | 60 | */ | 48 | | - onChangeCursor(func: () => any): () => void; | | 61 | + onChangeCursor(func: () => any): IPublicTypeDisposable; | 49 | 62 | } |
|