@leafer-in/state 1.0.0-rc.18

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-in/state
@@ -0,0 +1,103 @@
1
+ import { State, PointerEvent, UI } from '@leafer-ui/core';
2
+
3
+ function hasFixedState(leaf) {
4
+ return leaf.selected || leaf.disabled || State.isFocus(leaf);
5
+ }
6
+ function restoreStyle(leaf) {
7
+ const style = leaf.__.normalStyle;
8
+ if (style) {
9
+ leaf.set(style);
10
+ leaf.__.normalStyle = undefined;
11
+ }
12
+ }
13
+
14
+ function setStateStyle(leaf, stateType, pointerState) {
15
+ let style;
16
+ const data = leaf.__;
17
+ if (pointerState) {
18
+ style = !hasFixedState(leaf) && data[stateType];
19
+ }
20
+ else {
21
+ switch (stateType) {
22
+ case 'disabledStyle':
23
+ style = data[stateType];
24
+ break;
25
+ case 'focusStyle':
26
+ style = !leaf.disabled && data[stateType];
27
+ break;
28
+ case 'selectedStyle':
29
+ style = !leaf.disabled && !State.isFocus(leaf) && data[stateType];
30
+ break;
31
+ }
32
+ }
33
+ if (style) {
34
+ restoreStyle(leaf);
35
+ leaf.__.normalStyle = leaf.get(style);
36
+ leaf.set(style);
37
+ }
38
+ }
39
+
40
+ function unsetStateStyle(leaf, _stateType, pointerState) {
41
+ if (pointerState) {
42
+ if (!hasFixedState(leaf)) {
43
+ restoreStyle(leaf);
44
+ if (State.isPress(leaf) && leaf.pressStyle) {
45
+ setStateStyle(leaf, 'pressStyle', true);
46
+ }
47
+ else if (State.isHover(leaf) && leaf.hoverStyle) {
48
+ setStateStyle(leaf, 'hoverStyle', true);
49
+ }
50
+ }
51
+ }
52
+ else {
53
+ restoreStyle(leaf);
54
+ if (leaf.disabledStyle && leaf.disabled) {
55
+ setStateStyle(leaf, 'disabledStyle');
56
+ }
57
+ else if (leaf.focusStyle && State.isFocus(leaf)) {
58
+ setStateStyle(leaf, 'focusStyle');
59
+ }
60
+ else if (leaf.selectedStyle && leaf.selected) {
61
+ setStateStyle(leaf, 'selectedStyle');
62
+ }
63
+ }
64
+ }
65
+
66
+ function updateEventStyle(leaf, eventType) {
67
+ switch (eventType) {
68
+ case PointerEvent.ENTER:
69
+ setStateStyle(leaf, 'hoverStyle', true);
70
+ break;
71
+ case PointerEvent.LEAVE:
72
+ unsetStateStyle(leaf, 'hoverStyle', true);
73
+ break;
74
+ case PointerEvent.DOWN:
75
+ setStateStyle(leaf, 'pressStyle', true);
76
+ break;
77
+ case PointerEvent.UP:
78
+ unsetStateStyle(leaf, 'pressStyle', true);
79
+ break;
80
+ }
81
+ }
82
+
83
+ State.isHover = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isHover(leaf); };
84
+ State.isPress = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isPress(leaf); };
85
+ State.isFocus = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isFocus(leaf); };
86
+ State.isDrag = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isDrag(leaf); };
87
+ State.setStyle = function (leaf, stateType, value) { value ? setStateStyle(leaf, stateType) : unsetStateStyle(leaf); };
88
+ State.updateEventStyle = updateEventStyle;
89
+ UI.prototype.focus = function (value = true) {
90
+ this.waitLeafer(() => {
91
+ let { focusData } = this.app.interaction;
92
+ if (value) {
93
+ if (focusData)
94
+ focusData.focus(false);
95
+ focusData = this;
96
+ }
97
+ else {
98
+ focusData = null;
99
+ }
100
+ this.app.interaction.focusData = focusData;
101
+ State.setStyle(this, 'focusStyle', value);
102
+ });
103
+ };
@@ -0,0 +1 @@
1
+ import{State as e,PointerEvent as t,UI as s}from"@leafer-ui/core";function i(t){return t.selected||t.disabled||e.isFocus(t)}function r(e){const t=e.__.normalStyle;t&&(e.set(t),e.__.normalStyle=void 0)}function l(t,s,l){let a;const c=t.__;if(l)a=!i(t)&&c[s];else switch(s){case"disabledStyle":a=c[s];break;case"focusStyle":a=!t.disabled&&c[s];break;case"selectedStyle":a=!t.disabled&&!e.isFocus(t)&&c[s]}a&&(r(t),t.__.normalStyle=t.get(a),t.set(a))}function a(t,s,a){a?i(t)||(r(t),e.isPress(t)&&t.pressStyle?l(t,"pressStyle",!0):e.isHover(t)&&t.hoverStyle&&l(t,"hoverStyle",!0)):(r(t),t.disabledStyle&&t.disabled?l(t,"disabledStyle"):t.focusStyle&&e.isFocus(t)?l(t,"focusStyle"):t.selectedStyle&&t.selected&&l(t,"selectedStyle"))}e.isHover=function(e){return e.leafer&&e.leafer.interaction.isHover(e)},e.isPress=function(e){return e.leafer&&e.leafer.interaction.isPress(e)},e.isFocus=function(e){return e.leafer&&e.leafer.interaction.isFocus(e)},e.isDrag=function(e){return e.leafer&&e.leafer.interaction.isDrag(e)},e.setStyle=function(e,t,s){s?l(e,t):a(e)},e.updateEventStyle=function(e,s){switch(s){case t.ENTER:l(e,"hoverStyle",!0);break;case t.LEAVE:a(e,0,!0);break;case t.DOWN:l(e,"pressStyle",!0);break;case t.UP:a(e,0,!0)}},s.prototype.focus=function(t=!0){this.waitLeafer((()=>{let{focusData:s}=this.app.interaction;t?(s&&s.focus(!1),s=this):s=null,this.app.interaction.focusData=s,e.setStyle(this,"focusStyle",t)}))};
package/dist/state.js ADDED
@@ -0,0 +1,106 @@
1
+ (function (core) {
2
+ 'use strict';
3
+
4
+ function hasFixedState(leaf) {
5
+ return leaf.selected || leaf.disabled || core.State.isFocus(leaf);
6
+ }
7
+ function restoreStyle(leaf) {
8
+ const style = leaf.__.normalStyle;
9
+ if (style) {
10
+ leaf.set(style);
11
+ leaf.__.normalStyle = undefined;
12
+ }
13
+ }
14
+
15
+ function setStateStyle(leaf, stateType, pointerState) {
16
+ let style;
17
+ const data = leaf.__;
18
+ if (pointerState) {
19
+ style = !hasFixedState(leaf) && data[stateType];
20
+ }
21
+ else {
22
+ switch (stateType) {
23
+ case 'disabledStyle':
24
+ style = data[stateType];
25
+ break;
26
+ case 'focusStyle':
27
+ style = !leaf.disabled && data[stateType];
28
+ break;
29
+ case 'selectedStyle':
30
+ style = !leaf.disabled && !core.State.isFocus(leaf) && data[stateType];
31
+ break;
32
+ }
33
+ }
34
+ if (style) {
35
+ restoreStyle(leaf);
36
+ leaf.__.normalStyle = leaf.get(style);
37
+ leaf.set(style);
38
+ }
39
+ }
40
+
41
+ function unsetStateStyle(leaf, _stateType, pointerState) {
42
+ if (pointerState) {
43
+ if (!hasFixedState(leaf)) {
44
+ restoreStyle(leaf);
45
+ if (core.State.isPress(leaf) && leaf.pressStyle) {
46
+ setStateStyle(leaf, 'pressStyle', true);
47
+ }
48
+ else if (core.State.isHover(leaf) && leaf.hoverStyle) {
49
+ setStateStyle(leaf, 'hoverStyle', true);
50
+ }
51
+ }
52
+ }
53
+ else {
54
+ restoreStyle(leaf);
55
+ if (leaf.disabledStyle && leaf.disabled) {
56
+ setStateStyle(leaf, 'disabledStyle');
57
+ }
58
+ else if (leaf.focusStyle && core.State.isFocus(leaf)) {
59
+ setStateStyle(leaf, 'focusStyle');
60
+ }
61
+ else if (leaf.selectedStyle && leaf.selected) {
62
+ setStateStyle(leaf, 'selectedStyle');
63
+ }
64
+ }
65
+ }
66
+
67
+ function updateEventStyle(leaf, eventType) {
68
+ switch (eventType) {
69
+ case core.PointerEvent.ENTER:
70
+ setStateStyle(leaf, 'hoverStyle', true);
71
+ break;
72
+ case core.PointerEvent.LEAVE:
73
+ unsetStateStyle(leaf, 'hoverStyle', true);
74
+ break;
75
+ case core.PointerEvent.DOWN:
76
+ setStateStyle(leaf, 'pressStyle', true);
77
+ break;
78
+ case core.PointerEvent.UP:
79
+ unsetStateStyle(leaf, 'pressStyle', true);
80
+ break;
81
+ }
82
+ }
83
+
84
+ core.State.isHover = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isHover(leaf); };
85
+ core.State.isPress = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isPress(leaf); };
86
+ core.State.isFocus = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isFocus(leaf); };
87
+ core.State.isDrag = function (leaf) { return leaf.leafer && leaf.leafer.interaction.isDrag(leaf); };
88
+ core.State.setStyle = function (leaf, stateType, value) { value ? setStateStyle(leaf, stateType) : unsetStateStyle(leaf); };
89
+ core.State.updateEventStyle = updateEventStyle;
90
+ core.UI.prototype.focus = function (value = true) {
91
+ this.waitLeafer(() => {
92
+ let { focusData } = this.app.interaction;
93
+ if (value) {
94
+ if (focusData)
95
+ focusData.focus(false);
96
+ focusData = this;
97
+ }
98
+ else {
99
+ focusData = null;
100
+ }
101
+ this.app.interaction.focusData = focusData;
102
+ core.State.setStyle(this, 'focusStyle', value);
103
+ });
104
+ };
105
+
106
+ })(LeaferUI);
@@ -0,0 +1 @@
1
+ !function(e){"use strict";function t(t){return t.selected||t.disabled||e.State.isFocus(t)}function s(e){const t=e.__.normalStyle;t&&(e.set(t),e.__.normalStyle=void 0)}function a(a,i,n){let r;const l=a.__;if(n)r=!t(a)&&l[i];else switch(i){case"disabledStyle":r=l[i];break;case"focusStyle":r=!a.disabled&&l[i];break;case"selectedStyle":r=!a.disabled&&!e.State.isFocus(a)&&l[i]}r&&(s(a),a.__.normalStyle=a.get(r),a.set(r))}function i(i,n,r){r?t(i)||(s(i),e.State.isPress(i)&&i.pressStyle?a(i,"pressStyle",!0):e.State.isHover(i)&&i.hoverStyle&&a(i,"hoverStyle",!0)):(s(i),i.disabledStyle&&i.disabled?a(i,"disabledStyle"):i.focusStyle&&e.State.isFocus(i)?a(i,"focusStyle"):i.selectedStyle&&i.selected&&a(i,"selectedStyle"))}e.State.isHover=function(e){return e.leafer&&e.leafer.interaction.isHover(e)},e.State.isPress=function(e){return e.leafer&&e.leafer.interaction.isPress(e)},e.State.isFocus=function(e){return e.leafer&&e.leafer.interaction.isFocus(e)},e.State.isDrag=function(e){return e.leafer&&e.leafer.interaction.isDrag(e)},e.State.setStyle=function(e,t,s){s?a(e,t):i(e)},e.State.updateEventStyle=function(t,s){switch(s){case e.PointerEvent.ENTER:a(t,"hoverStyle",!0);break;case e.PointerEvent.LEAVE:i(t,0,!0);break;case e.PointerEvent.DOWN:a(t,"pressStyle",!0);break;case e.PointerEvent.UP:i(t,0,!0)}},e.UI.prototype.focus=function(t=!0){this.waitLeafer((()=>{let{focusData:s}=this.app.interaction;t?(s&&s.focus(!1),s=this):s=null,this.app.interaction.focusData=s,e.State.setStyle(this,"focusStyle",t)}))}}(LeaferUI);
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@leafer-in/state",
3
+ "version": "1.0.0-rc.18",
4
+ "description": "@leafer-in/state",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "dist/state.esm.js",
8
+ "unpkg": "dist/state.js",
9
+ "jsdelivr": "dist/state.js",
10
+ "types": "types/index.d.ts",
11
+ "files": [
12
+ "src",
13
+ "types",
14
+ "dist"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/leaferjs/in.git"
19
+ },
20
+ "homepage": "https://github.com/leaferjs/in/tree/main/packages/state",
21
+ "bugs": "https://github.com/leaferjs/in/issues",
22
+ "keywords": [
23
+ "leafer state",
24
+ "leafer-state",
25
+ "leafer-in",
26
+ "state",
27
+ "leafer-ui",
28
+ "leaferjs"
29
+ ],
30
+ "dependencies": {
31
+ "@leafer-ui/core": "1.0.0-rc.18"
32
+ },
33
+ "devDependencies": {
34
+ "@leafer-ui/interface": "1.0.0-rc.18",
35
+ "@leafer-in/interface": "1.0.0-rc.18"
36
+ }
37
+ }
package/src/event.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { ILeaf } from '@leafer-ui/interface'
2
+ import { PointerEvent } from '@leafer-ui/core'
3
+
4
+ import { setStateStyle } from './set'
5
+ import { unsetStateStyle } from './unset'
6
+
7
+
8
+ export function updateEventStyle(leaf: ILeaf, eventType: string): void {
9
+ switch (eventType) {
10
+ case PointerEvent.ENTER:
11
+ setStateStyle(leaf, 'hoverStyle', true)
12
+ break
13
+ case PointerEvent.LEAVE:
14
+ unsetStateStyle(leaf, 'hoverStyle', true)
15
+ break
16
+ case PointerEvent.DOWN:
17
+ setStateStyle(leaf, 'pressStyle', true)
18
+ break
19
+ case PointerEvent.UP:
20
+ unsetStateStyle(leaf, 'pressStyle', true)
21
+ break
22
+ }
23
+ }
package/src/helper.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { ILeaf } from '@leafer-ui/interface'
2
+ import { State } from '@leafer-ui/core'
3
+
4
+
5
+ export function hasFixedState(leaf: ILeaf): boolean {
6
+ return leaf.selected || leaf.disabled || State.isFocus(leaf)
7
+ }
8
+
9
+ export function restoreStyle(leaf: ILeaf) {
10
+ const style = leaf.__.normalStyle
11
+ if (style) {
12
+ leaf.set(style)
13
+ leaf.__.normalStyle = undefined
14
+ }
15
+ }
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { ILeaf, IStateStyleType } from '@leafer-ui/interface'
2
+ import { State, UI } from '@leafer-ui/core'
3
+
4
+ import { setStateStyle } from './set'
5
+ import { unsetStateStyle } from './unset'
6
+ import { updateEventStyle } from './event'
7
+
8
+
9
+ State.isHover = function (leaf: ILeaf): boolean { return leaf.leafer && leaf.leafer.interaction.isHover(leaf) }
10
+ State.isPress = function (leaf: ILeaf): boolean { return leaf.leafer && leaf.leafer.interaction.isPress(leaf) }
11
+ State.isFocus = function (leaf: ILeaf): boolean { return leaf.leafer && leaf.leafer.interaction.isFocus(leaf) }
12
+ State.isDrag = function (leaf: ILeaf): boolean { return leaf.leafer && leaf.leafer.interaction.isDrag(leaf) }
13
+ State.setStyle = function (leaf: ILeaf, stateType: IStateStyleType, value: boolean): void { value ? setStateStyle(leaf, stateType) : unsetStateStyle(leaf, stateType) }
14
+ State.updateEventStyle = updateEventStyle
15
+
16
+
17
+ UI.prototype.focus = function (value: boolean = true): void {
18
+ this.waitLeafer(() => {
19
+ let { focusData } = this.app.interaction
20
+ if (value) {
21
+ if (focusData) focusData.focus(false)
22
+ focusData = this
23
+ } else {
24
+ focusData = null
25
+ }
26
+ this.app.interaction.focusData = focusData
27
+ State.setStyle(this, 'focusStyle', value)
28
+ })
29
+ }
package/src/set.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { ILeaf, IObject, IStateStyleType } from '@leafer-ui/interface'
2
+ import { State } from '@leafer-ui/core'
3
+
4
+ import { hasFixedState, restoreStyle } from './helper'
5
+
6
+
7
+ export function setStateStyle(leaf: ILeaf, stateType: IStateStyleType, pointerState?: boolean): void {
8
+
9
+ let style: IObject
10
+ const data = leaf.__ as IObject
11
+
12
+ if (pointerState) {
13
+
14
+ // hover / press
15
+ style = !hasFixedState(leaf) && data[stateType]
16
+
17
+ } else {
18
+
19
+ // disabled > focus > selected
20
+ switch (stateType) {
21
+ case 'disabledStyle':
22
+ style = data[stateType]
23
+ break
24
+ case 'focusStyle':
25
+ style = !leaf.disabled && data[stateType]
26
+ break
27
+ case 'selectedStyle':
28
+ style = !leaf.disabled && !State.isFocus(leaf) && data[stateType]
29
+ break
30
+ }
31
+
32
+ }
33
+
34
+ if (style) {
35
+ restoreStyle(leaf) // 先回到正常状态
36
+ leaf.__.normalStyle = leaf.get(style) as IObject
37
+ leaf.set(style)
38
+ }
39
+
40
+ }
package/src/unset.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { ILeaf, IStateStyleType } from '@leafer/interface'
2
+ import { State } from '@leafer-ui/core'
3
+
4
+ import { setStateStyle } from './set'
5
+ import { hasFixedState, restoreStyle } from './helper'
6
+
7
+
8
+ export function unsetStateStyle(leaf: ILeaf, _stateType: IStateStyleType, pointerState?: boolean): void {
9
+
10
+ if (pointerState) {
11
+
12
+ if (!hasFixedState(leaf)) {
13
+
14
+ restoreStyle(leaf)
15
+
16
+ // press > hover
17
+ if (State.isPress(leaf) && leaf.pressStyle) {
18
+ setStateStyle(leaf, 'pressStyle', true)
19
+ } else if (State.isHover(leaf) && leaf.hoverStyle) {
20
+ setStateStyle(leaf, 'hoverStyle', true)
21
+ }
22
+
23
+ }
24
+
25
+ } else {
26
+
27
+ // disabled > focus > selected
28
+ restoreStyle(leaf)
29
+
30
+ if (leaf.disabledStyle && leaf.disabled) {
31
+ setStateStyle(leaf, 'disabledStyle')
32
+ } else if (leaf.focusStyle && State.isFocus(leaf)) {
33
+ setStateStyle(leaf, 'focusStyle')
34
+ } else if (leaf.selectedStyle && leaf.selected) {
35
+ setStateStyle(leaf, 'selectedStyle')
36
+ }
37
+
38
+ }
39
+
40
+ }
@@ -0,0 +1,2 @@
1
+
2
+ export { }