@vnejs/plugins.canvas.child.fill 0.1.1
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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +13 -0
- package/dist/modules/change.d.ts +9 -0
- package/dist/modules/change.js +26 -0
- package/dist/modules/hide.d.ts +10 -0
- package/dist/modules/hide.js +24 -0
- package/dist/modules/show.d.ts +10 -0
- package/dist/modules/show.js +27 -0
- package/dist/modules/state.d.ts +23 -0
- package/dist/modules/state.js +68 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/dist/utils/fill.d.ts +47 -0
- package/dist/utils/fill.js +13 -0
- package/package.json +49 -0
- package/src/index.ts +16 -0
- package/src/modules/change.ts +39 -0
- package/src/modules/hide.ts +35 -0
- package/src/modules/show.ts +41 -0
- package/src/modules/state.ts +94 -0
- package/src/types.ts +13 -0
- package/src/utils/fill.ts +61 -0
- package/tsconfig.json +15 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@vnejs/contracts.canvas.child.fill";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "@vnejs/contracts.canvas.child.fill";
|
|
2
|
+
import { regPlugin } from "@vnejs/shared";
|
|
3
|
+
import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.canvas.child.fill";
|
|
4
|
+
import { LayerFillChange } from "./modules/change.js";
|
|
5
|
+
import { LayerFillHide } from "./modules/hide.js";
|
|
6
|
+
import { LayerFillShow } from "./modules/show.js";
|
|
7
|
+
import { LayerFillState } from "./modules/state.js";
|
|
8
|
+
regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS }, [
|
|
9
|
+
LayerFillShow,
|
|
10
|
+
LayerFillHide,
|
|
11
|
+
LayerFillChange,
|
|
12
|
+
LayerFillState,
|
|
13
|
+
]);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
3
|
+
import type { FillChangePayload } from "../utils/fill.js";
|
|
4
|
+
export declare class LayerFillChange extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams> {
|
|
5
|
+
name: string;
|
|
6
|
+
subscribe: () => void;
|
|
7
|
+
onChange: ({ id, layer, color, duration }?: FillChangePayload) => Promise<unknown[] | undefined>;
|
|
8
|
+
emitChildChange: (layer: string, childId: string, duration: number, props: Record<string, unknown>) => Promise<unknown[]> | undefined;
|
|
9
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
export class LayerFillChange extends ModuleCore {
|
|
4
|
+
name = "layer.fill.change";
|
|
5
|
+
subscribe = () => {
|
|
6
|
+
this.on(this.EVENTS.LAYER_FILL.CHANGE, this.onChange);
|
|
7
|
+
};
|
|
8
|
+
onChange = async ({ id, layer, color, duration } = {}) => {
|
|
9
|
+
if (!id || !layer || !color)
|
|
10
|
+
return;
|
|
11
|
+
const old = (await this.emitOne(this.EVENTS.LAYER_FILL.STATE_GET, { id }));
|
|
12
|
+
if (!old)
|
|
13
|
+
return;
|
|
14
|
+
const changeArgs = { background: color };
|
|
15
|
+
await this.emit(this.EVENTS.LAYER_FILL.STATE_UPDATE, { id, color });
|
|
16
|
+
if (!duration || this.shared.viewForceAnimationSources.isNotEmpty())
|
|
17
|
+
return this.emitChildChange(layer, id, 0, { ...changeArgs, transition: 0 });
|
|
18
|
+
const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
|
|
19
|
+
return this.emit(this.EVENTS.INTERACT.PUSH_POP, {
|
|
20
|
+
key: `${this.name}_${layer}_${id}`,
|
|
21
|
+
skipHandler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve),
|
|
22
|
+
handler: () => Promise.race([this.emitChildChange(layer, id, Number(duration), { ...changeArgs, transition: duration }), skipPromise]),
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
emitChildChange = (layer, childId, duration, props) => this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration, props });
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { FillTransitionProps } from "@vnejs/contracts.canvas.child.fill";
|
|
3
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
4
|
+
import type { FillHidePayload } from "../utils/fill.js";
|
|
5
|
+
export declare class LayerFillHide extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams> {
|
|
6
|
+
name: string;
|
|
7
|
+
subscribe: () => void;
|
|
8
|
+
onHide: ({ id, layer, duration }?: FillHidePayload) => Promise<unknown[] | undefined>;
|
|
9
|
+
getTransitionProps: (duration: number) => FillTransitionProps;
|
|
10
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
export class LayerFillHide extends ModuleCore {
|
|
4
|
+
name = "layer.fill.hide";
|
|
5
|
+
subscribe = () => {
|
|
6
|
+
this.on(this.EVENTS.LAYER_FILL.HIDE, this.onHide);
|
|
7
|
+
};
|
|
8
|
+
onHide = async ({ id, layer, duration } = {}) => {
|
|
9
|
+
if (!id || !layer)
|
|
10
|
+
return;
|
|
11
|
+
await this.emit(this.EVENTS.LAYER_FILL.STATE_RM, { id });
|
|
12
|
+
if (!duration || this.shared.viewForceAnimationSources.isNotEmpty())
|
|
13
|
+
return this.emit(this.EVENTS.LAYER.CHILD_RM, { layer, childId: id });
|
|
14
|
+
const { initialProps = {}, finishProps = {} } = this.getTransitionProps(Number(duration));
|
|
15
|
+
const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
|
|
16
|
+
const key = `${this.name}_${layer}_${id}`;
|
|
17
|
+
await this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId: id, duration: 0, props: initialProps });
|
|
18
|
+
await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
|
|
19
|
+
await Promise.race([this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId: id, duration, props: finishProps }), skipPromise]);
|
|
20
|
+
await this.emit(this.EVENTS.INTERACT.POP, { key });
|
|
21
|
+
return this.emit(this.EVENTS.LAYER.CHILD_RM, { layer, childId: id });
|
|
22
|
+
};
|
|
23
|
+
getTransitionProps = (duration) => this.PARAMS.LAYER_FILL.TRANSITIONS_HIDE.opacity(duration);
|
|
24
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { FillTransitionProps } from "@vnejs/contracts.canvas.child.fill";
|
|
3
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
4
|
+
import type { FillShowPayload } from "../utils/fill.js";
|
|
5
|
+
export declare class LayerFillShow extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams> {
|
|
6
|
+
name: string;
|
|
7
|
+
subscribe: () => void;
|
|
8
|
+
onShow: ({ id, layer, color, duration }?: FillShowPayload) => Promise<unknown[] | undefined>;
|
|
9
|
+
getTransitionProps: (duration: number) => FillTransitionProps;
|
|
10
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
import { assertIdNotExists, buildChild } from "../utils/fill.js";
|
|
4
|
+
export class LayerFillShow extends ModuleCore {
|
|
5
|
+
name = "layer.fill.show";
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.LAYER_FILL.SHOW, this.onShow);
|
|
8
|
+
};
|
|
9
|
+
onShow = async ({ id, layer, color, duration } = {}) => {
|
|
10
|
+
if (!id || !layer || !color)
|
|
11
|
+
return;
|
|
12
|
+
const existing = (await this.emitOne(this.EVENTS.LAYER_FILL.STATE_GET, { id }));
|
|
13
|
+
assertIdNotExists(existing, id, layer);
|
|
14
|
+
const childCommon = buildChild({ type: this.CONST.LAYER_FILL.LAYER_CHILD_TYPE, id, color });
|
|
15
|
+
await this.emit(this.EVENTS.LAYER_FILL.STATE_ADD, { id, layer, color });
|
|
16
|
+
if (!duration || this.shared.viewForceAnimationSources.isNotEmpty())
|
|
17
|
+
return this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: childCommon });
|
|
18
|
+
const { initialProps = {}, finishProps = {} } = this.getTransitionProps(Number(duration));
|
|
19
|
+
const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
|
|
20
|
+
const [key, changeArgs] = [`${this.name}_${layer}_${id}`, { layer, childId: id, duration, props: finishProps }];
|
|
21
|
+
await this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: { ...childCommon, ...initialProps } });
|
|
22
|
+
await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
|
|
23
|
+
await Promise.race([this.emit(this.EVENTS.LAYER.CHILD_CHANGE, changeArgs), skipPromise]);
|
|
24
|
+
await this.emit(this.EVENTS.INTERACT.POP, { key });
|
|
25
|
+
};
|
|
26
|
+
getTransitionProps = (duration) => this.PARAMS.LAYER_FILL.TRANSITIONS_SHOW.opacity(duration);
|
|
27
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ModuleCore, type ModuleGlobalStateRegistry } from "@vnejs/module.core";
|
|
2
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
3
|
+
import type { FillRecord, FillState, FillStateIdPayload } from "../utils/fill.js";
|
|
4
|
+
export declare class LayerFillState extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams, FillState> {
|
|
5
|
+
name: string;
|
|
6
|
+
subscribe: () => void;
|
|
7
|
+
onFillStateUpdate: ({ id, ...fill }?: FillStateIdPayload) => void;
|
|
8
|
+
onFillStateAdd: ({ id, layer, color }?: FillStateIdPayload) => void;
|
|
9
|
+
onFillStateRm: ({ id }?: FillStateIdPayload) => void;
|
|
10
|
+
onFillStateGet: ({ id }?: FillStateIdPayload) => FillRecord | undefined;
|
|
11
|
+
onStateSet: (payload: ModuleGlobalStateRegistry) => Promise<void>;
|
|
12
|
+
onStateClear: () => Promise<void>;
|
|
13
|
+
clearState: () => Promise<void>;
|
|
14
|
+
cloneFillAndChange: (fill: FillRecord) => Promise<unknown>;
|
|
15
|
+
cloneFillAndHide: (fill: FillRecord) => Promise<unknown>;
|
|
16
|
+
cloneFillAndShow: (fill: FillRecord) => Promise<unknown>;
|
|
17
|
+
emitFillChange: (fill: FillRecord) => Promise<unknown[]> | undefined;
|
|
18
|
+
emitFillHide: (fill: FillRecord) => Promise<unknown[]> | undefined;
|
|
19
|
+
emitFillShow: (fill: FillRecord) => Promise<unknown[]> | undefined;
|
|
20
|
+
emitEventWithFill: (fill: FillRecord, cb: (fill: FillRecord) => unknown) => unknown;
|
|
21
|
+
getFillsByLayerFromState: (acc: Record<string, FillRecord[]>, fill: FillRecord) => Record<string, FillRecord[]>;
|
|
22
|
+
getDefaultState: () => FillState;
|
|
23
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class LayerFillState extends ModuleCore {
|
|
3
|
+
name = "layer.fill.state";
|
|
4
|
+
subscribe = () => {
|
|
5
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_UPDATE, this.onFillStateUpdate);
|
|
6
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_GET, this.onFillStateGet);
|
|
7
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_ADD, this.onFillStateAdd);
|
|
8
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_RM, this.onFillStateRm);
|
|
9
|
+
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
10
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
|
|
11
|
+
};
|
|
12
|
+
onFillStateUpdate = ({ id, ...fill } = {}) => {
|
|
13
|
+
this.state.fills = this.state.fills.map((item) => (item.id === id ? { ...item, ...fill } : item));
|
|
14
|
+
};
|
|
15
|
+
onFillStateAdd = ({ id, layer, color } = {}) => {
|
|
16
|
+
this.state.fills.push({ id: id, layer, color });
|
|
17
|
+
};
|
|
18
|
+
onFillStateRm = ({ id } = {}) => {
|
|
19
|
+
this.state.fills = this.state.fills.filter((item) => item.id !== id);
|
|
20
|
+
};
|
|
21
|
+
onFillStateGet = ({ id } = {}) => this.state.fills.find((item) => item.id === id);
|
|
22
|
+
onStateSet = async (payload) => {
|
|
23
|
+
const clonedState = await this.emitClone(this.getStateSlice(payload));
|
|
24
|
+
const oldFillsByLayer = this.state.fills.reduce(this.getFillsByLayerFromState, {});
|
|
25
|
+
const newFillsByLayer = clonedState.fills.reduce(this.getFillsByLayerFromState, {});
|
|
26
|
+
const [promises, uniqLayers, oldLayers, newLayers] = [
|
|
27
|
+
[],
|
|
28
|
+
new Set(),
|
|
29
|
+
Object.keys(oldFillsByLayer),
|
|
30
|
+
Object.keys(newFillsByLayer),
|
|
31
|
+
];
|
|
32
|
+
oldLayers.forEach((layer) => uniqLayers.add(layer));
|
|
33
|
+
newLayers.forEach((layer) => uniqLayers.add(layer));
|
|
34
|
+
Array.from(uniqLayers).forEach((layer) => {
|
|
35
|
+
const oldFills = oldFillsByLayer[layer] || [];
|
|
36
|
+
const newFills = newFillsByLayer[layer] || [];
|
|
37
|
+
newFills.forEach((fill) => {
|
|
38
|
+
if (!oldFills.find((item) => item.id === fill.id))
|
|
39
|
+
return promises.push(this.cloneFillAndShow(fill));
|
|
40
|
+
promises.push(this.cloneFillAndChange(fill));
|
|
41
|
+
});
|
|
42
|
+
oldFills.forEach((fill) => !newFills.find((item) => item.id === fill.id) && promises.push(this.cloneFillAndHide(fill)));
|
|
43
|
+
});
|
|
44
|
+
this.shared.viewForceAnimationSources.add(`${this.name}.set`);
|
|
45
|
+
await Promise.all(promises);
|
|
46
|
+
this.shared.viewForceAnimationSources.rm(`${this.name}.set`);
|
|
47
|
+
};
|
|
48
|
+
onStateClear = () => this.clearState();
|
|
49
|
+
clearState = async () => {
|
|
50
|
+
this.shared.viewForceAnimationSources.add(`${this.name}.clear`);
|
|
51
|
+
await Promise.all(this.state.fills.map(this.cloneFillAndHide));
|
|
52
|
+
this.shared.viewForceAnimationSources.rm(`${this.name}.clear`);
|
|
53
|
+
};
|
|
54
|
+
cloneFillAndChange = async (fill) => this.emitEventWithFill(await this.emitClone(fill), this.emitFillChange);
|
|
55
|
+
cloneFillAndHide = async (fill) => this.emitEventWithFill(await this.emitClone(fill), this.emitFillHide);
|
|
56
|
+
cloneFillAndShow = async (fill) => this.emitEventWithFill(await this.emitClone(fill), this.emitFillShow);
|
|
57
|
+
emitFillChange = (fill) => this.emit(this.EVENTS.LAYER_FILL.CHANGE, { ...fill, duration: 0 });
|
|
58
|
+
emitFillHide = (fill) => this.emit(this.EVENTS.LAYER_FILL.HIDE, fill);
|
|
59
|
+
emitFillShow = (fill) => this.emit(this.EVENTS.LAYER_FILL.SHOW, fill);
|
|
60
|
+
emitEventWithFill = (fill, cb) => cb(fill);
|
|
61
|
+
getFillsByLayerFromState = (acc, fill) => {
|
|
62
|
+
if (!acc[fill.layer])
|
|
63
|
+
acc[fill.layer] = [];
|
|
64
|
+
acc[fill.layer].push(fill);
|
|
65
|
+
return acc;
|
|
66
|
+
};
|
|
67
|
+
getDefaultState = () => ({ fills: [] });
|
|
68
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { Constants, Params, PluginName, SubscribeEvents } from "@vnejs/contracts.canvas.child.fill";
|
|
3
|
+
import type { PluginName as LayerPluginName, SettingsKeys as LayerSettingsKeys, SubscribeEvents as LayerSubscribeEvents } from "@vnejs/contracts.canvas.layer";
|
|
4
|
+
export type FillPluginEvents = ModuleCoreEvents & Record<PluginName, SubscribeEvents> & Record<LayerPluginName, LayerSubscribeEvents>;
|
|
5
|
+
export type FillPluginConstants = ModuleCoreConstants & Record<PluginName, Constants>;
|
|
6
|
+
export type FillPluginSettings = ModuleCoreSettings & Record<LayerPluginName, LayerSettingsKeys>;
|
|
7
|
+
export type FillPluginParams = ModuleCoreParams & Record<PluginName, Params>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type FillRecord = {
|
|
2
|
+
id: string;
|
|
3
|
+
layer?: string;
|
|
4
|
+
color?: string;
|
|
5
|
+
};
|
|
6
|
+
export type FillState = {
|
|
7
|
+
fills: FillRecord[];
|
|
8
|
+
};
|
|
9
|
+
export type FillShowPayload = {
|
|
10
|
+
id?: string;
|
|
11
|
+
layer?: string;
|
|
12
|
+
color?: string;
|
|
13
|
+
duration?: number;
|
|
14
|
+
};
|
|
15
|
+
export type FillHidePayload = {
|
|
16
|
+
id?: string;
|
|
17
|
+
layer?: string;
|
|
18
|
+
duration?: number;
|
|
19
|
+
};
|
|
20
|
+
export type FillChangePayload = {
|
|
21
|
+
id?: string;
|
|
22
|
+
layer?: string;
|
|
23
|
+
color?: string;
|
|
24
|
+
duration?: number;
|
|
25
|
+
};
|
|
26
|
+
export type FillStateIdPayload = {
|
|
27
|
+
id?: string;
|
|
28
|
+
layer?: string;
|
|
29
|
+
color?: string;
|
|
30
|
+
};
|
|
31
|
+
export type LayerFillChild = {
|
|
32
|
+
type: string;
|
|
33
|
+
id: string;
|
|
34
|
+
background?: string;
|
|
35
|
+
top?: number;
|
|
36
|
+
right?: number;
|
|
37
|
+
bottom?: number;
|
|
38
|
+
left?: number;
|
|
39
|
+
opacity?: number;
|
|
40
|
+
transition?: number;
|
|
41
|
+
};
|
|
42
|
+
export declare const buildChild: ({ type, id, color }: {
|
|
43
|
+
type: string;
|
|
44
|
+
id: string;
|
|
45
|
+
color: string;
|
|
46
|
+
}) => LayerFillChild;
|
|
47
|
+
export declare const assertIdNotExists: (existing: FillRecord | undefined, id: string, layer: string) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const buildChild = ({ type, id, color }) => ({
|
|
2
|
+
type,
|
|
3
|
+
id,
|
|
4
|
+
background: color,
|
|
5
|
+
top: 0,
|
|
6
|
+
right: 0,
|
|
7
|
+
bottom: 0,
|
|
8
|
+
left: 0,
|
|
9
|
+
});
|
|
10
|
+
export const assertIdNotExists = (existing, id, layer) => {
|
|
11
|
+
if (existing)
|
|
12
|
+
throw new Error(`Layer fill "${id}" already exists on layer "${layer}". Use "${layer}.fill change" instead.`);
|
|
13
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.canvas.child.fill",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@vnejs/contracts.canvas.child.fill": "~0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/helpers": "~0.1.0",
|
|
37
|
+
"@vnejs/module.core": "~0.1.0",
|
|
38
|
+
"@vnejs/contracts.canvas.layer": "~0.1.0",
|
|
39
|
+
"@vnejs/shared": "~0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@vnejs/configs.ts-common": "~0.1.0",
|
|
43
|
+
"@vnejs/contracts.canvas.layer": "~0.1.0",
|
|
44
|
+
"@vnejs/contracts.canvas.child.fill": "~0.1.0",
|
|
45
|
+
"@vnejs/helpers": "~0.1.0",
|
|
46
|
+
"@vnejs/module.core": "~0.1.0",
|
|
47
|
+
"@vnejs/shared": "~0.1.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "@vnejs/contracts.canvas.child.fill";
|
|
2
|
+
|
|
3
|
+
import { regPlugin } from "@vnejs/shared";
|
|
4
|
+
import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.canvas.child.fill";
|
|
5
|
+
|
|
6
|
+
import { LayerFillChange } from "./modules/change.js";
|
|
7
|
+
import { LayerFillHide } from "./modules/hide.js";
|
|
8
|
+
import { LayerFillShow } from "./modules/show.js";
|
|
9
|
+
import { LayerFillState } from "./modules/state.js";
|
|
10
|
+
|
|
11
|
+
regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS }, [
|
|
12
|
+
LayerFillShow,
|
|
13
|
+
LayerFillHide,
|
|
14
|
+
LayerFillChange,
|
|
15
|
+
LayerFillState,
|
|
16
|
+
]);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
|
|
4
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
5
|
+
import type { FillChangePayload, FillRecord } from "../utils/fill.js";
|
|
6
|
+
|
|
7
|
+
export class LayerFillChange extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams> {
|
|
8
|
+
name = "layer.fill.change";
|
|
9
|
+
|
|
10
|
+
subscribe = () => {
|
|
11
|
+
this.on(this.EVENTS.LAYER_FILL.CHANGE, this.onChange);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
onChange = async ({ id, layer, color, duration }: FillChangePayload = {}) => {
|
|
15
|
+
if (!id || !layer || !color) return;
|
|
16
|
+
|
|
17
|
+
const old = (await this.emitOne(this.EVENTS.LAYER_FILL.STATE_GET, { id })) as FillRecord | undefined;
|
|
18
|
+
|
|
19
|
+
if (!old) return;
|
|
20
|
+
|
|
21
|
+
const changeArgs = { background: color };
|
|
22
|
+
|
|
23
|
+
await this.emit(this.EVENTS.LAYER_FILL.STATE_UPDATE, { id, color });
|
|
24
|
+
|
|
25
|
+
if (!duration || this.shared.viewForceAnimationSources.isNotEmpty())
|
|
26
|
+
return this.emitChildChange(layer, id, 0, { ...changeArgs, transition: 0 });
|
|
27
|
+
|
|
28
|
+
const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
|
|
29
|
+
|
|
30
|
+
return this.emit(this.EVENTS.INTERACT.PUSH_POP, {
|
|
31
|
+
key: `${this.name}_${layer}_${id}`,
|
|
32
|
+
skipHandler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve),
|
|
33
|
+
handler: () => Promise.race([this.emitChildChange(layer, id, Number(duration), { ...changeArgs, transition: duration }), skipPromise]),
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
emitChildChange = (layer: string, childId: string, duration: number, props: Record<string, unknown>) =>
|
|
38
|
+
this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration, props });
|
|
39
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
import type { FillTransitionProps } from "@vnejs/contracts.canvas.child.fill";
|
|
4
|
+
|
|
5
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
6
|
+
import type { FillHidePayload } from "../utils/fill.js";
|
|
7
|
+
|
|
8
|
+
export class LayerFillHide extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams> {
|
|
9
|
+
name = "layer.fill.hide";
|
|
10
|
+
|
|
11
|
+
subscribe = () => {
|
|
12
|
+
this.on(this.EVENTS.LAYER_FILL.HIDE, this.onHide);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
onHide = async ({ id, layer, duration }: FillHidePayload = {}) => {
|
|
16
|
+
if (!id || !layer) return;
|
|
17
|
+
|
|
18
|
+
await this.emit(this.EVENTS.LAYER_FILL.STATE_RM, { id });
|
|
19
|
+
|
|
20
|
+
if (!duration || this.shared.viewForceAnimationSources.isNotEmpty()) return this.emit(this.EVENTS.LAYER.CHILD_RM, { layer, childId: id });
|
|
21
|
+
|
|
22
|
+
const { initialProps = {}, finishProps = {} } = this.getTransitionProps(Number(duration));
|
|
23
|
+
const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
|
|
24
|
+
const key = `${this.name}_${layer}_${id}`;
|
|
25
|
+
|
|
26
|
+
await this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId: id, duration: 0, props: initialProps });
|
|
27
|
+
await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
|
|
28
|
+
await Promise.race([this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId: id, duration, props: finishProps }), skipPromise]);
|
|
29
|
+
await this.emit(this.EVENTS.INTERACT.POP, { key });
|
|
30
|
+
|
|
31
|
+
return this.emit(this.EVENTS.LAYER.CHILD_RM, { layer, childId: id });
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
getTransitionProps = (duration: number): FillTransitionProps => this.PARAMS.LAYER_FILL.TRANSITIONS_HIDE.opacity(duration);
|
|
35
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
import type { FillTransitionProps } from "@vnejs/contracts.canvas.child.fill";
|
|
4
|
+
|
|
5
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
6
|
+
import type { FillRecord, FillShowPayload } from "../utils/fill.js";
|
|
7
|
+
import { assertIdNotExists, buildChild } from "../utils/fill.js";
|
|
8
|
+
|
|
9
|
+
export class LayerFillShow extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams> {
|
|
10
|
+
name = "layer.fill.show";
|
|
11
|
+
|
|
12
|
+
subscribe = () => {
|
|
13
|
+
this.on(this.EVENTS.LAYER_FILL.SHOW, this.onShow);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
onShow = async ({ id, layer, color, duration }: FillShowPayload = {}) => {
|
|
17
|
+
if (!id || !layer || !color) return;
|
|
18
|
+
|
|
19
|
+
const existing = (await this.emitOne(this.EVENTS.LAYER_FILL.STATE_GET, { id })) as FillRecord | undefined;
|
|
20
|
+
|
|
21
|
+
assertIdNotExists(existing, id, layer);
|
|
22
|
+
|
|
23
|
+
const childCommon = buildChild({ type: this.CONST.LAYER_FILL.LAYER_CHILD_TYPE, id, color });
|
|
24
|
+
|
|
25
|
+
await this.emit(this.EVENTS.LAYER_FILL.STATE_ADD, { id, layer, color });
|
|
26
|
+
|
|
27
|
+
if (!duration || this.shared.viewForceAnimationSources.isNotEmpty())
|
|
28
|
+
return this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: childCommon });
|
|
29
|
+
|
|
30
|
+
const { initialProps = {}, finishProps = {} } = this.getTransitionProps(Number(duration));
|
|
31
|
+
const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
|
|
32
|
+
const [key, changeArgs] = [`${this.name}_${layer}_${id}`, { layer, childId: id, duration, props: finishProps }];
|
|
33
|
+
|
|
34
|
+
await this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: { ...childCommon, ...initialProps } });
|
|
35
|
+
await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
|
|
36
|
+
await Promise.race([this.emit(this.EVENTS.LAYER.CHILD_CHANGE, changeArgs), skipPromise]);
|
|
37
|
+
await this.emit(this.EVENTS.INTERACT.POP, { key });
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
getTransitionProps = (duration: number): FillTransitionProps => this.PARAMS.LAYER_FILL.TRANSITIONS_SHOW.opacity(duration);
|
|
41
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ModuleCore, type ModuleGlobalStateRegistry } from "@vnejs/module.core";
|
|
2
|
+
|
|
3
|
+
import type { FillPluginConstants, FillPluginEvents, FillPluginParams, FillPluginSettings } from "../types.js";
|
|
4
|
+
import type { FillRecord, FillState, FillStateIdPayload } from "../utils/fill.js";
|
|
5
|
+
|
|
6
|
+
export class LayerFillState extends ModuleCore<FillPluginEvents, FillPluginConstants, FillPluginSettings, FillPluginParams, FillState> {
|
|
7
|
+
name = "layer.fill.state";
|
|
8
|
+
|
|
9
|
+
subscribe = () => {
|
|
10
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_UPDATE, this.onFillStateUpdate);
|
|
11
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_GET, this.onFillStateGet);
|
|
12
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_ADD, this.onFillStateAdd);
|
|
13
|
+
this.on(this.EVENTS.LAYER_FILL.STATE_RM, this.onFillStateRm);
|
|
14
|
+
|
|
15
|
+
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
16
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
onFillStateUpdate = ({ id, ...fill }: FillStateIdPayload = {}) => {
|
|
20
|
+
this.state.fills = this.state.fills.map((item) => (item.id === id ? { ...item, ...fill } : item));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
onFillStateAdd = ({ id, layer, color }: FillStateIdPayload = {}) => {
|
|
24
|
+
this.state.fills.push({ id: id!, layer, color });
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
onFillStateRm = ({ id }: FillStateIdPayload = {}) => {
|
|
28
|
+
this.state.fills = this.state.fills.filter((item) => item.id !== id);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
onFillStateGet = ({ id }: FillStateIdPayload = {}) => this.state.fills.find((item) => item.id === id);
|
|
32
|
+
|
|
33
|
+
onStateSet = async (payload: ModuleGlobalStateRegistry) => {
|
|
34
|
+
const clonedState = await this.emitClone<FillState>(this.getStateSlice(payload));
|
|
35
|
+
|
|
36
|
+
const oldFillsByLayer = this.state.fills.reduce(this.getFillsByLayerFromState, {} as Record<string, FillRecord[]>);
|
|
37
|
+
const newFillsByLayer = clonedState.fills.reduce(this.getFillsByLayerFromState, {} as Record<string, FillRecord[]>);
|
|
38
|
+
|
|
39
|
+
const [promises, uniqLayers, oldLayers, newLayers] = [
|
|
40
|
+
[] as Promise<unknown>[],
|
|
41
|
+
new Set<string>(),
|
|
42
|
+
Object.keys(oldFillsByLayer),
|
|
43
|
+
Object.keys(newFillsByLayer),
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
oldLayers.forEach((layer) => uniqLayers.add(layer));
|
|
47
|
+
newLayers.forEach((layer) => uniqLayers.add(layer));
|
|
48
|
+
|
|
49
|
+
Array.from(uniqLayers).forEach((layer) => {
|
|
50
|
+
const oldFills = oldFillsByLayer[layer] || [];
|
|
51
|
+
const newFills = newFillsByLayer[layer] || [];
|
|
52
|
+
|
|
53
|
+
newFills.forEach((fill) => {
|
|
54
|
+
if (!oldFills.find((item) => item.id === fill.id)) return promises.push(this.cloneFillAndShow(fill));
|
|
55
|
+
|
|
56
|
+
promises.push(this.cloneFillAndChange(fill));
|
|
57
|
+
});
|
|
58
|
+
oldFills.forEach((fill) => !newFills.find((item) => item.id === fill.id) && promises.push(this.cloneFillAndHide(fill)));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.shared.viewForceAnimationSources.add(`${this.name}.set`);
|
|
62
|
+
await Promise.all(promises);
|
|
63
|
+
this.shared.viewForceAnimationSources.rm(`${this.name}.set`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
onStateClear = () => this.clearState();
|
|
67
|
+
|
|
68
|
+
clearState = async () => {
|
|
69
|
+
this.shared.viewForceAnimationSources.add(`${this.name}.clear`);
|
|
70
|
+
await Promise.all(this.state.fills.map(this.cloneFillAndHide));
|
|
71
|
+
this.shared.viewForceAnimationSources.rm(`${this.name}.clear`);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
cloneFillAndChange = async (fill: FillRecord) => this.emitEventWithFill(await this.emitClone<FillRecord>(fill), this.emitFillChange);
|
|
75
|
+
|
|
76
|
+
cloneFillAndHide = async (fill: FillRecord) => this.emitEventWithFill(await this.emitClone<FillRecord>(fill), this.emitFillHide);
|
|
77
|
+
|
|
78
|
+
cloneFillAndShow = async (fill: FillRecord) => this.emitEventWithFill(await this.emitClone<FillRecord>(fill), this.emitFillShow);
|
|
79
|
+
|
|
80
|
+
emitFillChange = (fill: FillRecord) => this.emit(this.EVENTS.LAYER_FILL.CHANGE, { ...fill, duration: 0 });
|
|
81
|
+
emitFillHide = (fill: FillRecord) => this.emit(this.EVENTS.LAYER_FILL.HIDE, fill);
|
|
82
|
+
emitFillShow = (fill: FillRecord) => this.emit(this.EVENTS.LAYER_FILL.SHOW, fill);
|
|
83
|
+
|
|
84
|
+
emitEventWithFill = (fill: FillRecord, cb: (fill: FillRecord) => unknown) => cb(fill);
|
|
85
|
+
|
|
86
|
+
getFillsByLayerFromState = (acc: Record<string, FillRecord[]>, fill: FillRecord) => {
|
|
87
|
+
if (!acc[fill.layer!]) acc[fill.layer!] = [];
|
|
88
|
+
acc[fill.layer!].push(fill);
|
|
89
|
+
|
|
90
|
+
return acc;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
getDefaultState = (): FillState => ({ fills: [] });
|
|
94
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { Constants, Params, PluginName, SubscribeEvents } from "@vnejs/contracts.canvas.child.fill";
|
|
3
|
+
import type { PluginName as LayerPluginName, SettingsKeys as LayerSettingsKeys, SubscribeEvents as LayerSubscribeEvents } from "@vnejs/contracts.canvas.layer";
|
|
4
|
+
|
|
5
|
+
export type FillPluginEvents = ModuleCoreEvents &
|
|
6
|
+
Record<PluginName, SubscribeEvents> &
|
|
7
|
+
Record<LayerPluginName, LayerSubscribeEvents>;
|
|
8
|
+
|
|
9
|
+
export type FillPluginConstants = ModuleCoreConstants & Record<PluginName, Constants>;
|
|
10
|
+
|
|
11
|
+
export type FillPluginSettings = ModuleCoreSettings & Record<LayerPluginName, LayerSettingsKeys>;
|
|
12
|
+
|
|
13
|
+
export type FillPluginParams = ModuleCoreParams & Record<PluginName, Params>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type FillRecord = {
|
|
2
|
+
id: string;
|
|
3
|
+
layer?: string;
|
|
4
|
+
color?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type FillState = {
|
|
8
|
+
fills: FillRecord[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type FillShowPayload = {
|
|
12
|
+
id?: string;
|
|
13
|
+
layer?: string;
|
|
14
|
+
color?: string;
|
|
15
|
+
duration?: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type FillHidePayload = {
|
|
19
|
+
id?: string;
|
|
20
|
+
layer?: string;
|
|
21
|
+
duration?: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type FillChangePayload = {
|
|
25
|
+
id?: string;
|
|
26
|
+
layer?: string;
|
|
27
|
+
color?: string;
|
|
28
|
+
duration?: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type FillStateIdPayload = {
|
|
32
|
+
id?: string;
|
|
33
|
+
layer?: string;
|
|
34
|
+
color?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type LayerFillChild = {
|
|
38
|
+
type: string;
|
|
39
|
+
id: string;
|
|
40
|
+
background?: string;
|
|
41
|
+
top?: number;
|
|
42
|
+
right?: number;
|
|
43
|
+
bottom?: number;
|
|
44
|
+
left?: number;
|
|
45
|
+
opacity?: number;
|
|
46
|
+
transition?: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const buildChild = ({ type, id, color }: { type: string; id: string; color: string }): LayerFillChild => ({
|
|
50
|
+
type,
|
|
51
|
+
id,
|
|
52
|
+
background: color,
|
|
53
|
+
top: 0,
|
|
54
|
+
right: 0,
|
|
55
|
+
bottom: 0,
|
|
56
|
+
left: 0,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const assertIdNotExists = (existing: FillRecord | undefined, id: string, layer: string) => {
|
|
60
|
+
if (existing) throw new Error(`Layer fill "${id}" already exists on layer "${layer}". Use "${layer}.fill change" instead.`);
|
|
61
|
+
};
|
package/tsconfig.json
ADDED