Skip to content

Commit ff5c450

Browse files
sunflatmugi-unohidakatsuya
committed
Add Vue store
Co-authored-by: mugi-uno <mugi.uno@gmail.com> Co-authored-by: Katsuya HIDAKA <hidakatsuya@gmail.com>
1 parent 680c9d6 commit ff5c450

38 files changed

Lines changed: 3585 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { DeepReadonly } from 'utility-types';
2+
import { GettersBase } from './getters-base';
3+
import { MutationsBase } from './mutations-base';
4+
5+
export class ActionsBase<T, G extends GettersBase<T>, M extends MutationsBase<T>> {
6+
protected readonly stateAccessor: () => DeepReadonly<T>;
7+
protected readonly getters: G;
8+
protected readonly mutations: M;
9+
10+
constructor (stateAccessor: () => DeepReadonly<T>, getters: G, mutations: M) {
11+
this.stateAccessor = stateAccessor;
12+
this.getters = getters;
13+
this.mutations = mutations;
14+
}
15+
16+
protected get state (): DeepReadonly<T> {
17+
return this.stateAccessor() as DeepReadonly<T>;
18+
}
19+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class GettersBase<T> {
2+
protected readonly stateAccessor: () => T;
3+
4+
constructor (stateAccessor: () => T) {
5+
this.stateAccessor = stateAccessor;
6+
}
7+
8+
protected get state (): T {
9+
return this.stateAccessor();
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class MutationsBase<T> {
2+
protected readonly stateAccessor: () => T;
3+
4+
constructor (stateAccessor: () => T) {
5+
this.stateAccessor = stateAccessor;
6+
}
7+
8+
protected get state (): T {
9+
return this.stateAccessor();
10+
}
11+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DeepReadonly } from 'utility-types';
2+
3+
export abstract class StoreBase<T> {
4+
protected readonly stateAccessor: () => T;
5+
6+
constructor (stateAccessor: () => T) {
7+
this.stateAccessor = stateAccessor;
8+
}
9+
10+
get state () {
11+
return this.stateAccessor() as DeepReadonly<T>;
12+
}
13+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ActionsBase } from '../base/actions-base';
2+
import { Getters } from './getters';
3+
import { Mutations } from './mutations';
4+
import { calcPlus, calcMinus } from '@/lib/strict-calculator';
5+
import { Editor, ToolType, CopiedAnyItem } from '@/types';
6+
7+
export class Actions extends ActionsBase<Editor, Getters, Mutations> {
8+
activateTool ({ tool }: { tool: ToolType }) {
9+
this.mutations.setActiveTool({ tool });
10+
}
11+
12+
setClipboard (item: CopiedAnyItem) {
13+
if (!item) return;
14+
15+
this.mutations.setClipboard(item);
16+
}
17+
18+
setZoomRate (rate: number) {
19+
this.mutations.setZoomRate(rate);
20+
}
21+
22+
resetZoom () {
23+
this.mutations.setZoomRate(1.2);
24+
}
25+
26+
zoomIn () {
27+
this.mutations.setZoomRate(calcPlus(this.state.zoomRate, 0.5));
28+
}
29+
30+
zoomOut () {
31+
this.mutations.setZoomRate(calcMinus(this.state.zoomRate, 0.5));
32+
}
33+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GettersBase } from '../base/getters-base';
2+
import { Editor } from '@/types';
3+
4+
export class Getters extends GettersBase<Editor> {
5+
isSelectMode () {
6+
return this.state.activeTool === 'select';
7+
}
8+
9+
isDrawMode () {
10+
return this.state.activeTool !== 'select';
11+
}
12+
13+
zoomRate () {
14+
return this.state.zoomRate;
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { StoreBase } from '../base/store-base';
2+
import { Actions } from './actions';
3+
import { Getters } from './getters';
4+
import { Mutations } from './mutations';
5+
import { Editor } from '@/types';
6+
7+
export class Store extends StoreBase<Editor> {
8+
readonly getters: Getters;
9+
readonly mutations: Mutations;
10+
readonly actions: Actions;
11+
12+
constructor (stateAccessor: () => Editor) {
13+
super(stateAccessor);
14+
this.getters = new Getters(stateAccessor);
15+
this.mutations = new Mutations(stateAccessor);
16+
this.actions = new Actions(stateAccessor, this.getters, this.mutations);
17+
}
18+
19+
static createState (): Editor {
20+
return {
21+
activeTool: 'select',
22+
clipboard: null,
23+
zoomRate: 1.2
24+
};
25+
}
26+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MutationsBase } from '../base/mutations-base';
2+
import { CopiedAnyItem, Editor, ToolType } from '@/types';
3+
4+
export class Mutations extends MutationsBase<Editor> {
5+
setActiveTool ({ tool }: { tool: ToolType}) {
6+
this.state.activeTool = tool;
7+
}
8+
9+
setClipboard (item: CopiedAnyItem) {
10+
this.state.clipboard = item;
11+
}
12+
13+
setZoomRate (rate: number) {
14+
this.state.zoomRate = Math.max(rate, 0.5);
15+
}
16+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { DeepReadonly } from 'utility-types';
2+
import { ActionsBase } from '../base/actions-base';
3+
import { Getters } from './getters';
4+
import { Mutations } from './mutations';
5+
import { root } from '..';
6+
import { History, Report } from '@/types';
7+
8+
export class Actions extends ActionsBase<History, Getters, Mutations> {
9+
push (report: DeepReadonly<Report>) {
10+
this.mutations.push(JSON.stringify(report));
11+
}
12+
13+
replaceCurrentPointer (report: DeepReadonly<Report>) {
14+
if (this.state.pointer === null) return;
15+
16+
this.mutations.replaceCurrentPointer(JSON.stringify(report));
17+
}
18+
19+
undo () {
20+
if (this.state.pointer === null || this.state.pointer === 0) return;
21+
22+
const history = this.state.histories[this.state.pointer - 1];
23+
root.mutations.setReport(JSON.parse(history));
24+
25+
this.mutations.backPointer();
26+
}
27+
28+
redo () {
29+
if (this.state.pointer === null || this.state.histories.length <= this.state.pointer + 1) return;
30+
31+
const history = this.state.histories[this.state.pointer + 1];
32+
root.mutations.setReport(JSON.parse(history));
33+
34+
this.mutations.forwardPointer();
35+
}
36+
37+
reset () {
38+
this.mutations.clear();
39+
}
40+
41+
set (report: DeepReadonly<Report>) {
42+
this.reset();
43+
this.push(report);
44+
}
45+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { GettersBase } from '../base/getters-base';
2+
import { History } from '@/types';
3+
4+
export class Getters extends GettersBase<History> {
5+
undoable (): boolean {
6+
return this.state.pointer !== null && this.state.pointer > 0;
7+
}
8+
9+
redoable (): boolean {
10+
return this.state.pointer !== null && this.state.histories.length > this.state.pointer + 1;
11+
}
12+
}

0 commit comments

Comments
 (0)