Skip to content

Commit

Permalink
feat: 添加最大长度maxLength配置项.
Browse files Browse the repository at this point in the history
  • Loading branch information
PinghuaZhuang committed Sep 6, 2023
1 parent 529c824 commit a12120e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ pnpm dev

~~处理程序可以是 Promise.~~

+ rigister?: Record<ActionTypeEnum, handler>
+ ~~mergeRule?: (newActions: Action[], cur: Action, curIndex, otherActions: Action[]) => Actioin[].~~
+ ~~debounce?: 是否开启防抖模式.~~
+ @default: true
+ ~~merge?: 是否允许合并操作.~~
+ @default: true
+ handlers?: Record<ActionTypeEnum, handler>
+ options?: { maxLength: number }

## Methods

Expand Down
2 changes: 2 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ window.addEventListener('DOMContentLoaded', () => {
inp.value = target!.context.oldValue;
console.log('CHANGE undo:', inp.value, target, end);
},
}, {
maxLength: 5,
});
// @ts-ignore
window.cs = cs;
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</style>
</head>
<body>
<h3>example: 文本</h3>
<h3>example: 文本 maxLength = 5</h3>
<input id="inp" disabled />
<br />
<input id="updateInp" />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zstark/command-stack",
"version": "1.1.0",
"version": "1.2.0",
"description": "",
"keywords": [
"undo-redo"
Expand Down
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ export type StackEvent<T> = {
end?: T;
};

export type CommandStackOption = {
/**
* 记录最大长度, 默认不限制
*/
maxLength?: number;
};

export default class CommandStack<
T extends Action<string, any>,
> extends EventEmitter {
static events = {
STACK_CHANGE: 'stack.change',
};

option!: CommandStackOption;
private $stackIdx = -1;
private $event: StackEvent<T> | null = null;
_stack: T[] = [];
Expand All @@ -43,9 +51,11 @@ export default class CommandStack<

constructor(
handlers = {} as Record<T['type'], (event: StackEvent<T>) => void>,
option: CommandStackOption = {},
) {
super();
this._handlers = { ...handlers };
this.option = { ...option };
this.rigister();
}

Expand Down Expand Up @@ -77,9 +87,15 @@ export default class CommandStack<

dispatch(action: T) {
this._stack.splice(this._stackIdx + 1);
this._stack.push(action);
this._stackIdx++;
const length = this._stack.push(action);
this.$event = null;
if (
Number.isSafeInteger(this.option.maxLength) &&
length > (this.option.maxLength as number)
) {
this._stack.shift();
}
this._stackIdx = Math.min(this._stack.length - 1, this._stackIdx + 1);
}

excute(action: T) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"target": "ES2015",
"module": "ES2022",
"lib": [
"ES2022"
"ES2022",
"DOM"
],
"moduleResolution": "Bundler",
"resolveJsonModule": true,
Expand Down

0 comments on commit a12120e

Please sign in to comment.