@antv/g6 5.1.0-beta.0 → 5.1.0
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 +21 -0
- package/README.md +4 -0
- package/README.zh-CN.md +4 -0
- package/dist/g6.min.js +9 -9
- package/dist/lib/plugins/contextmenu/index.d.ts +1 -0
- package/dist/lib/plugins/contextmenu/util.d.ts +1 -1
- package/dist/lib/utils/shortcut.d.ts +7 -0
- package/dist/lib/version.d.ts +1 -1
- package/esm/behaviors/create-edge.js +3 -2
- package/esm/behaviors/create-edge.js.map +1 -1
- package/esm/elements/combos/base-combo.js.map +1 -1
- package/esm/elements/edges/base-edge.js.map +1 -1
- package/esm/elements/nodes/base-node.js.map +1 -1
- package/esm/elements/nodes/image.js +2 -2
- package/esm/elements/nodes/image.js.map +1 -1
- package/esm/plugins/contextmenu/index.d.ts +1 -0
- package/esm/plugins/contextmenu/index.js +20 -3
- package/esm/plugins/contextmenu/index.js.map +1 -1
- package/esm/plugins/contextmenu/util.d.ts +1 -1
- package/esm/plugins/contextmenu/util.js +16 -1
- package/esm/plugins/contextmenu/util.js.map +1 -1
- package/esm/plugins/tooltip.js.map +1 -1
- package/esm/runtime/canvas.js.map +1 -1
- package/esm/runtime/element.js +3 -0
- package/esm/runtime/element.js.map +1 -1
- package/esm/utils/shortcut.d.ts +7 -0
- package/esm/utils/shortcut.js +29 -0
- package/esm/utils/shortcut.js.map +1 -1
- package/esm/utils/vector.js +36 -3
- package/esm/utils/vector.js.map +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/esm/version.js.map +1 -1
- package/lib/behaviors/create-edge.js +3 -2
- package/lib/behaviors/create-edge.js.map +1 -1
- package/lib/elements/combos/base-combo.js.map +1 -1
- package/lib/elements/edges/base-edge.js.map +1 -1
- package/lib/elements/nodes/base-node.js.map +1 -1
- package/lib/elements/nodes/image.js +1 -1
- package/lib/elements/nodes/image.js.map +1 -1
- package/lib/plugins/contextmenu/index.d.ts +1 -0
- package/lib/plugins/contextmenu/index.js +20 -3
- package/lib/plugins/contextmenu/index.js.map +1 -1
- package/lib/plugins/contextmenu/util.d.ts +1 -1
- package/lib/plugins/contextmenu/util.js +16 -1
- package/lib/plugins/contextmenu/util.js.map +1 -1
- package/lib/plugins/tooltip.js.map +1 -1
- package/lib/runtime/canvas.js.map +1 -1
- package/lib/runtime/element.js +3 -0
- package/lib/runtime/element.js.map +1 -1
- package/lib/utils/shortcut.d.ts +7 -0
- package/lib/utils/shortcut.js +29 -0
- package/lib/utils/shortcut.js.map +1 -1
- package/lib/utils/vector.js +35 -2
- package/lib/utils/vector.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/version.js.map +1 -1
- package/package.json +29 -31
- package/src/behaviors/create-edge.ts +3 -2
- package/src/elements/combos/base-combo.ts +1 -2
- package/src/elements/edges/base-edge.ts +1 -2
- package/src/elements/nodes/base-node.ts +1 -2
- package/src/elements/nodes/image.ts +2 -2
- package/src/plugins/contextmenu/index.ts +26 -3
- package/src/plugins/contextmenu/util.ts +16 -1
- package/src/plugins/tooltip.ts +1 -2
- package/src/runtime/canvas.ts +2 -4
- package/src/runtime/element.ts +4 -0
- package/src/types/edge.ts +3 -1
- package/src/utils/shortcut.ts +30 -0
- package/src/utils/vector.ts +38 -5
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antv/g6",
|
|
3
|
-
"version": "5.1.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "A Graph Visualization Framework in JavaScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"antv",
|
|
@@ -31,31 +31,6 @@
|
|
|
31
31
|
"dist",
|
|
32
32
|
"README"
|
|
33
33
|
],
|
|
34
|
-
"scripts": {
|
|
35
|
-
"build": "run-p build:*",
|
|
36
|
-
"build:cjs": "rimraf ./lib && tsc --module commonjs --outDir lib -p tsconfig.build.json",
|
|
37
|
-
"build:dev:watch": "npm run build:esm -- --watch",
|
|
38
|
-
"build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm -p tsconfig.build.json",
|
|
39
|
-
"build:umd": "rimraf ./dist && rollup -c && npm run size",
|
|
40
|
-
"bundle-vis": "cross-env BUNDLE_VIS=1 npm run build:umd",
|
|
41
|
-
"ci": "run-s lint type-check build test",
|
|
42
|
-
"coverage": "jest --coverage",
|
|
43
|
-
"coverage:open": "open coverage/lcov-report/index.html",
|
|
44
|
-
"dev": "vite",
|
|
45
|
-
"fix": "eslint ./src ./__tests__ --fix && prettier ./src __tests__ --write ",
|
|
46
|
-
"jest": "node --expose-gc --max-old-space-size=1024 --unhandled-rejections=strict ../../node_modules/jest/bin/jest --coverage --logHeapUsage --detectOpenHandles",
|
|
47
|
-
"lint": "eslint ./src __tests__ --quiet && prettier ./src __tests__ --check",
|
|
48
|
-
"prepublishOnly": "run-s ci readme",
|
|
49
|
-
"readme": "cp ../../README.* ./",
|
|
50
|
-
"size": "limit-size",
|
|
51
|
-
"start": "rimraf ./lib && tsc --module commonjs --outDir lib --watch",
|
|
52
|
-
"tag": "node ./scripts/tag.mjs",
|
|
53
|
-
"test": "jest",
|
|
54
|
-
"test:integration": "npm run jest __tests__/integration",
|
|
55
|
-
"test:unit": "npm run jest __tests__/unit",
|
|
56
|
-
"type-check": "tsc --noEmit",
|
|
57
|
-
"version": "node ./scripts/version.mjs"
|
|
58
|
-
},
|
|
59
34
|
"dependencies": {
|
|
60
35
|
"@antv/algorithm": "^0.1.26",
|
|
61
36
|
"@antv/component": "^2.1.7",
|
|
@@ -65,7 +40,7 @@
|
|
|
65
40
|
"@antv/g-plugin-dragndrop": "^2.0.38",
|
|
66
41
|
"@antv/graphlib": "^2.0.4",
|
|
67
42
|
"@antv/hierarchy": "^0.7.1",
|
|
68
|
-
"@antv/layout": "2.0.0
|
|
43
|
+
"@antv/layout": "^2.0.0",
|
|
69
44
|
"@antv/util": "^3.3.11",
|
|
70
45
|
"bubblesets-js": "^2.3.4"
|
|
71
46
|
},
|
|
@@ -83,8 +58,7 @@
|
|
|
83
58
|
"xmlserializer": "^0.6.1"
|
|
84
59
|
},
|
|
85
60
|
"publishConfig": {
|
|
86
|
-
"registry": "https://registry.npmjs.org/"
|
|
87
|
-
"tag": "beta"
|
|
61
|
+
"registry": "https://registry.npmjs.org/"
|
|
88
62
|
},
|
|
89
63
|
"limit-size": [
|
|
90
64
|
{
|
|
@@ -96,5 +70,29 @@
|
|
|
96
70
|
"limit": "1.5 Mb",
|
|
97
71
|
"path": "dist/g6.min.js"
|
|
98
72
|
}
|
|
99
|
-
]
|
|
100
|
-
|
|
73
|
+
],
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "run-p build:*",
|
|
76
|
+
"build:cjs": "rimraf ./lib && tsc --module commonjs --outDir lib -p tsconfig.build.json",
|
|
77
|
+
"build:dev:watch": "npm run build:esm -- --watch",
|
|
78
|
+
"build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm -p tsconfig.build.json",
|
|
79
|
+
"build:umd": "rimraf ./dist && rollup -c && npm run size",
|
|
80
|
+
"bundle-vis": "cross-env BUNDLE_VIS=1 npm run build:umd",
|
|
81
|
+
"ci": "run-s lint type-check build test",
|
|
82
|
+
"coverage": "jest --coverage",
|
|
83
|
+
"coverage:open": "open coverage/lcov-report/index.html",
|
|
84
|
+
"dev": "vite",
|
|
85
|
+
"fix": "eslint ./src ./__tests__ --fix && prettier ./src __tests__ --write ",
|
|
86
|
+
"jest": "node --expose-gc --max-old-space-size=1024 --unhandled-rejections=strict ../../node_modules/jest/bin/jest --coverage --logHeapUsage --detectOpenHandles",
|
|
87
|
+
"lint": "eslint ./src __tests__ --quiet && prettier ./src __tests__ --check",
|
|
88
|
+
"readme": "cp ../../README.* ./",
|
|
89
|
+
"size": "limit-size",
|
|
90
|
+
"start": "rimraf ./lib && tsc --module commonjs --outDir lib --watch",
|
|
91
|
+
"tag": "node ./scripts/tag.mjs",
|
|
92
|
+
"test": "jest",
|
|
93
|
+
"test:integration": "npm run jest __tests__/integration",
|
|
94
|
+
"test:unit": "npm run jest __tests__/unit",
|
|
95
|
+
"type-check": "tsc --noEmit",
|
|
96
|
+
"version": "node ./scripts/version.mjs"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -162,8 +162,9 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {
|
|
|
162
162
|
|
|
163
163
|
private updateAssistEdge = async (event: IPointerEvent) => {
|
|
164
164
|
if (!this.source) return;
|
|
165
|
-
const { model, element } = this.context;
|
|
166
|
-
|
|
165
|
+
const { model, element, graph } = this.context;
|
|
166
|
+
const [x, y] = graph.getCanvasByClient([event.client.x, event.client.y]);
|
|
167
|
+
model.translateNodeTo(ASSIST_NODE_ID, [x, y]);
|
|
167
168
|
await element!.draw({ animation: false, silence: true })?.finished;
|
|
168
169
|
};
|
|
169
170
|
|
|
@@ -30,8 +30,7 @@ import { connectImage, dispatchPositionChange } from '../shapes/image';
|
|
|
30
30
|
* <en/> Common style props for combo
|
|
31
31
|
*/
|
|
32
32
|
export interface BaseComboStyleProps
|
|
33
|
-
extends
|
|
34
|
-
BaseNodeStyleProps,
|
|
33
|
+
extends BaseNodeStyleProps,
|
|
35
34
|
Prefix<'collapsed', BaseStyleProps>,
|
|
36
35
|
Prefix<'collapsedMarker', CollapsedMarkerStyleProps> {
|
|
37
36
|
/**
|
|
@@ -33,8 +33,7 @@ import { Badge, Label } from '../shapes';
|
|
|
33
33
|
* <en/> Base style properties of the edge
|
|
34
34
|
*/
|
|
35
35
|
export interface BaseEdgeStyleProps
|
|
36
|
-
extends
|
|
37
|
-
BaseShapeStyleProps,
|
|
36
|
+
extends BaseShapeStyleProps,
|
|
38
37
|
Prefix<'label', EdgeLabelStyleProps>,
|
|
39
38
|
Prefix<'halo', PathStyleProps>,
|
|
40
39
|
Prefix<'badge', EdgeBadgeStyleProps>,
|
|
@@ -36,8 +36,7 @@ import { connectImage, dispatchPositionChange } from '../shapes/image';
|
|
|
36
36
|
* <en/> Base node style props
|
|
37
37
|
*/
|
|
38
38
|
export interface BaseNodeStyleProps
|
|
39
|
-
extends
|
|
40
|
-
BaseShapeStyleProps,
|
|
39
|
+
extends BaseShapeStyleProps,
|
|
41
40
|
Prefix<'label', NodeLabelStyleProps>,
|
|
42
41
|
Prefix<'halo', BaseStyleProps>,
|
|
43
42
|
Prefix<'icon', IconStyleProps>,
|
|
@@ -3,7 +3,7 @@ import { ImageStyleProps as GImageStyleProps, Rect as GRect } from '@antv/g';
|
|
|
3
3
|
import { ICON_SIZE_RATIO } from '../../constants/element';
|
|
4
4
|
import { subStyleProps } from '../../utils/prefix';
|
|
5
5
|
import { mergeOptions } from '../../utils/style';
|
|
6
|
-
import { add } from '../../utils/vector';
|
|
6
|
+
import { add, toVector2 } from '../../utils/vector';
|
|
7
7
|
import type { IconStyleProps } from '../shapes';
|
|
8
8
|
import { Image as ImageShape } from '../shapes';
|
|
9
9
|
import { connectImage, dispatchPositionChange } from '../shapes/image';
|
|
@@ -67,7 +67,7 @@ export class Image extends BaseNode<ImageStyleProps> {
|
|
|
67
67
|
const { fill: keyStyleFill, stroke: keyStyleStroke, ...keyStyle } = this.getShape<GRect>('key').attributes;
|
|
68
68
|
const haloStyle = subStyleProps(this.getGraphicStyle(attributes), 'halo');
|
|
69
69
|
const haloLineWidth = Number(haloStyle.lineWidth);
|
|
70
|
-
const [width, height] = add(this.getSize(attributes), [haloLineWidth, haloLineWidth]);
|
|
70
|
+
const [width, height] = add(toVector2(this.getSize(attributes)), [haloLineWidth, haloLineWidth]);
|
|
71
71
|
const { lineWidth } = haloStyle;
|
|
72
72
|
const recalculate = {
|
|
73
73
|
fill: 'transparent',
|
|
@@ -92,6 +92,10 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
|
|
|
92
92
|
|
|
93
93
|
private targetElement: Element | null = null;
|
|
94
94
|
|
|
95
|
+
private wheelHandler = (e: WheelEvent) => {
|
|
96
|
+
e.stopPropagation();
|
|
97
|
+
};
|
|
98
|
+
|
|
95
99
|
constructor(context: RuntimeContext, options: ContextmenuOptions) {
|
|
96
100
|
super(context, Object.assign({}, Contextmenu.defaultOptions, options));
|
|
97
101
|
|
|
@@ -104,6 +108,9 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
|
|
|
104
108
|
const { className } = this.options;
|
|
105
109
|
if (className) this.$element.classList.add(className);
|
|
106
110
|
|
|
111
|
+
// 阻止滚轮事件冒泡,防止触发画布的 zoom-canvas 拦截逻辑
|
|
112
|
+
this.$element.addEventListener('wheel', this.wheelHandler);
|
|
113
|
+
|
|
107
114
|
const $container = this.context.canvas.getContainer();
|
|
108
115
|
$container!.appendChild(this.$element);
|
|
109
116
|
|
|
@@ -135,12 +142,27 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
|
|
|
135
142
|
}
|
|
136
143
|
|
|
137
144
|
// NOTICE: 为什么事件中的 client 是相对浏览器,而不是画布容器?
|
|
138
|
-
const
|
|
145
|
+
const $container = this.context.graph.getCanvas().getContainer()!;
|
|
146
|
+
const clientRect = $container.getBoundingClientRect();
|
|
147
|
+
|
|
148
|
+
let left = event.client.x - clientRect.left + offset[0];
|
|
149
|
+
let top = event.client.y - clientRect.top + offset[1];
|
|
139
150
|
|
|
140
|
-
this.$element.style.left = `${event.client.x - clientRect.left + offset[0]}px`;
|
|
141
|
-
this.$element.style.top = `${event.client.y - clientRect.top + offset[1]}px`;
|
|
142
151
|
this.$element.style.display = 'block';
|
|
143
152
|
|
|
153
|
+
// 限制菜单位于画布容器范围内
|
|
154
|
+
const menuWidth = this.$element.offsetWidth;
|
|
155
|
+
const menuHeight = this.$element.offsetHeight;
|
|
156
|
+
const containerWidth = clientRect.width;
|
|
157
|
+
const containerHeight = clientRect.height;
|
|
158
|
+
const padding = 4;
|
|
159
|
+
|
|
160
|
+
left = Math.max(padding, Math.min(left, containerWidth - menuWidth - padding));
|
|
161
|
+
top = Math.max(padding, Math.min(top, containerHeight - menuHeight - padding));
|
|
162
|
+
|
|
163
|
+
this.$element.style.left = `${left}px`;
|
|
164
|
+
this.$element.style.top = `${top}px`;
|
|
165
|
+
|
|
144
166
|
this.targetElement = event.target;
|
|
145
167
|
}
|
|
146
168
|
|
|
@@ -210,6 +232,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
|
|
|
210
232
|
graph.off(`combo:${trigger}`, this.onTriggerEvent);
|
|
211
233
|
|
|
212
234
|
document.removeEventListener('click', this.onMenuItemClick);
|
|
235
|
+
this.$element.removeEventListener('wheel', this.wheelHandler);
|
|
213
236
|
}
|
|
214
237
|
|
|
215
238
|
private onTriggerEvent = (event: IElementEvent) => {
|
|
@@ -36,11 +36,26 @@ export const CONTEXTMENU_CSS = `
|
|
|
36
36
|
font-size: 12px;
|
|
37
37
|
background-color: rgba(255, 255, 255, 0.96);
|
|
38
38
|
border-radius: 4px;
|
|
39
|
-
overflow: hidden;
|
|
39
|
+
overflow-x: hidden;
|
|
40
|
+
overflow-y: auto;
|
|
41
|
+
max-height: calc(100% - 8px);
|
|
40
42
|
box-shadow: rgba(0, 0, 0, 0.12) 0px 6px 12px 0px;
|
|
41
43
|
transition: visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1) 0s, left 0.4s cubic-bezier(0.23, 1, 0.32, 1) 0s, top 0.4s cubic-bezier(0.23, 1, 0.32, 1) 0s;
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
.g6-contextmenu::-webkit-scrollbar {
|
|
47
|
+
width: 6px;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.g6-contextmenu::-webkit-scrollbar-thumb {
|
|
51
|
+
background-color: rgba(0, 0, 0, 0.2);
|
|
52
|
+
border-radius: 3px;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.g6-contextmenu::-webkit-scrollbar-thumb:hover {
|
|
56
|
+
background-color: rgba(0, 0, 0, 0.3);
|
|
57
|
+
}
|
|
58
|
+
|
|
44
59
|
.g6-contextmenu-ul {
|
|
45
60
|
max-width: 256px;
|
|
46
61
|
min-width: 96px;
|
package/src/plugins/tooltip.ts
CHANGED
|
@@ -13,8 +13,7 @@ import { BasePlugin } from './base-plugin';
|
|
|
13
13
|
* <en/> Tooltip plugin options
|
|
14
14
|
*/
|
|
15
15
|
export interface TooltipOptions
|
|
16
|
-
extends
|
|
17
|
-
BasePluginOptions,
|
|
16
|
+
extends BasePluginOptions,
|
|
18
17
|
Pick<TooltipStyleProps, 'position' | 'offset' | 'enterable' | 'style' | 'container' | 'title'> {
|
|
19
18
|
/**
|
|
20
19
|
* <zh/> 触发行为,可选 hover | click
|
package/src/runtime/canvas.ts
CHANGED
|
@@ -8,10 +8,8 @@ import type { CanvasLayer, Point } from '../types';
|
|
|
8
8
|
import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
|
|
9
9
|
import { parsePoint, toPointObject } from '../utils/point';
|
|
10
10
|
|
|
11
|
-
export interface CanvasConfig
|
|
12
|
-
GCanvasConfig,
|
|
13
|
-
'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor' | 'background'
|
|
14
|
-
> {
|
|
11
|
+
export interface CanvasConfig
|
|
12
|
+
extends Pick<GCanvasConfig, 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor' | 'background'> {
|
|
15
13
|
/**
|
|
16
14
|
* <zh/> 渲染器
|
|
17
15
|
*
|
package/src/runtime/element.ts
CHANGED
|
@@ -711,6 +711,10 @@ export class ElementController {
|
|
|
711
711
|
|
|
712
712
|
// 重新计算数据 / Recalculate data
|
|
713
713
|
const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
|
|
714
|
+
// 重置动画 / Reset animation
|
|
715
|
+
this.context.animation!.clear();
|
|
716
|
+
this.computeStyle('collapse');
|
|
717
|
+
|
|
714
718
|
if (!data) return;
|
|
715
719
|
const { drawData } = data;
|
|
716
720
|
const { add, remove, update } = drawData;
|
package/src/types/edge.ts
CHANGED
|
@@ -102,7 +102,9 @@ export interface EdgeBadgeStyleProps extends BadgeStyleProps {
|
|
|
102
102
|
* <en/> Edge arrow style properties
|
|
103
103
|
*/
|
|
104
104
|
export interface EdgeArrowStyleProps
|
|
105
|
-
extends PathStyleProps,
|
|
105
|
+
extends PathStyleProps,
|
|
106
|
+
Omit<ImageStyleProps, 'width' | 'height'>,
|
|
107
|
+
Record<string, unknown> {
|
|
106
108
|
/**
|
|
107
109
|
* <zh/> 箭头大小
|
|
108
110
|
*
|
package/src/utils/shortcut.ts
CHANGED
|
@@ -11,6 +11,11 @@ export type ShortcutKey = string[];
|
|
|
11
11
|
|
|
12
12
|
type Handler = (event: any) => void;
|
|
13
13
|
|
|
14
|
+
const MODIFIER_KEYS = new Set(['Control', 'Alt', 'Meta', 'Shift']);
|
|
15
|
+
function isModifierKey(key: string) {
|
|
16
|
+
return MODIFIER_KEYS.has(key);
|
|
17
|
+
}
|
|
18
|
+
|
|
14
19
|
const lowerCaseKeys = (keys: ShortcutKey) => keys.map((key) => (isString(key) ? key.toLocaleLowerCase() : key));
|
|
15
20
|
|
|
16
21
|
export class Shortcut {
|
|
@@ -48,6 +53,11 @@ export class Shortcut {
|
|
|
48
53
|
this.map.clear();
|
|
49
54
|
}
|
|
50
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Check whether the given keys are being held down currently.
|
|
58
|
+
* @param key - array of keys to check
|
|
59
|
+
* @returns true if the given keys are being held down, false otherwise.
|
|
60
|
+
*/
|
|
51
61
|
public match(key: ShortcutKey) {
|
|
52
62
|
// 排序
|
|
53
63
|
const recordKeyList = lowerCaseKeys(Array.from(this.recordKey)).sort();
|
|
@@ -58,6 +68,14 @@ export class Shortcut {
|
|
|
58
68
|
private bindEvents() {
|
|
59
69
|
const { emitter } = this;
|
|
60
70
|
|
|
71
|
+
// These window listeners are added purely to listen to modifier keys at the window level,
|
|
72
|
+
// and the key presses are only recorded into this.recordKey for the purpose of matching
|
|
73
|
+
// in the match() function. This allows just these keypresses alone to be registered
|
|
74
|
+
// before the canvas is focused, which prevents a problem where when shortcuts involving
|
|
75
|
+
// a modifier and clicking on the canvas are bound, match() will return false for that
|
|
76
|
+
// modifier key because the canvas has not been clicked (and therefore focused) yet.
|
|
77
|
+
window.addEventListener(CommonEvent.KEY_DOWN, this.onKeyDownWindow);
|
|
78
|
+
window.addEventListener(CommonEvent.KEY_UP, this.onKeyUpWindow);
|
|
61
79
|
emitter.on(CommonEvent.KEY_DOWN, this.onKeyDown);
|
|
62
80
|
emitter.on(CommonEvent.KEY_UP, this.onKeyUp);
|
|
63
81
|
emitter.on(CommonEvent.WHEEL, this.onWheel);
|
|
@@ -79,6 +97,16 @@ export class Shortcut {
|
|
|
79
97
|
this.recordKey.delete(event.key);
|
|
80
98
|
};
|
|
81
99
|
|
|
100
|
+
private onKeyDownWindow = (event: KeyboardEvent) => {
|
|
101
|
+
if (!isModifierKey(event.key)) return;
|
|
102
|
+
this.recordKey.add(event.key);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
private onKeyUpWindow = (event: KeyboardEvent) => {
|
|
106
|
+
if (!isModifierKey(event.key)) return;
|
|
107
|
+
this.recordKey.delete(event.key);
|
|
108
|
+
};
|
|
109
|
+
|
|
82
110
|
private trigger(event: KeyboardEvent) {
|
|
83
111
|
this.map.forEach((handler, key) => {
|
|
84
112
|
if (this.match(key)) handler(event);
|
|
@@ -127,6 +155,8 @@ export class Shortcut {
|
|
|
127
155
|
this.unbindAll();
|
|
128
156
|
this.emitter.off(CommonEvent.KEY_DOWN, this.onKeyDown);
|
|
129
157
|
this.emitter.off(CommonEvent.KEY_UP, this.onKeyUp);
|
|
158
|
+
window.removeEventListener(CommonEvent.KEY_DOWN, this.onKeyDownWindow);
|
|
159
|
+
window.removeEventListener(CommonEvent.KEY_UP, this.onKeyUpWindow);
|
|
130
160
|
this.emitter.off(CommonEvent.WHEEL, this.onWheel);
|
|
131
161
|
this.emitter.off(CommonEvent.DRAG, this.onDrag);
|
|
132
162
|
this.pinchHandler?.off('pinchmove', this.boundHandlePinch);
|
package/src/utils/vector.ts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
import type { Vector2, Vector3 } from '../types';
|
|
2
|
-
import { isVector2 } from './is';
|
|
2
|
+
import { isVector2, isVector3 } from './is';
|
|
3
|
+
import { format } from './print';
|
|
3
4
|
|
|
4
5
|
const VECTOR_ZERO: Vector3 = [0, 0, 0];
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* <zh/> 填充两个向量至相同维度
|
|
9
|
+
*
|
|
10
|
+
* <en/> Pads two vectors to the same dimension
|
|
11
|
+
* @param a - <zh/> 第一个向量 | <en/> The first vector
|
|
12
|
+
* @param b - <zh/> 第二个向量 | <en/> The second vector
|
|
13
|
+
* @returns <zh/> 两个向量填充后的结果 | <en/> The result of padded vectors
|
|
14
|
+
*/
|
|
15
|
+
function padVectors(a: Vector2 | Vector3, b: Vector2 | Vector3): [Vector2 | Vector3, Vector2 | Vector3] {
|
|
16
|
+
if (a.length == b.length) {
|
|
17
|
+
return [a, b];
|
|
18
|
+
} else {
|
|
19
|
+
if ((isVector3(a) && a[2] !== 0) || (isVector3(b) && b[2] !== 0)) {
|
|
20
|
+
throw new Error(format('Vectors could not operate due to different dimensions.'));
|
|
21
|
+
}
|
|
22
|
+
return [toVector2(a), toVector2(b)];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
6
26
|
/**
|
|
7
27
|
* <zh/> 两个向量求和
|
|
8
28
|
*
|
|
@@ -12,6 +32,7 @@ const VECTOR_ZERO: Vector3 = [0, 0, 0];
|
|
|
12
32
|
* @returns <zh/> 两个向量的和 | <en/> The sum of the two vectors
|
|
13
33
|
*/
|
|
14
34
|
export function add(a: Vector2 | Vector3, b: Vector2 | Vector3): Vector2 | Vector3 {
|
|
35
|
+
[a, b] = padVectors(a, b);
|
|
15
36
|
return a.map((v, i) => v + b[i]) as Vector2 | Vector3;
|
|
16
37
|
}
|
|
17
38
|
|
|
@@ -24,6 +45,7 @@ export function add(a: Vector2 | Vector3, b: Vector2 | Vector3): Vector2 | Vecto
|
|
|
24
45
|
* @returns <zh/> 两个向量的差 | <en/> The difference of the two vectors
|
|
25
46
|
*/
|
|
26
47
|
export function subtract(a: Vector2 | Vector3, b: Vector2 | Vector3): Vector2 | Vector3 {
|
|
48
|
+
[a, b] = padVectors(a, b);
|
|
27
49
|
return a.map((v, i) => v - b[i]) as Vector2 | Vector3;
|
|
28
50
|
}
|
|
29
51
|
|
|
@@ -36,7 +58,8 @@ export function subtract(a: Vector2 | Vector3, b: Vector2 | Vector3): Vector2 |
|
|
|
36
58
|
* @returns <zh/> 两个向量的积或者向量和标量的积 | <en/> The product of the two vectors or the product of the vector and scalar
|
|
37
59
|
*/
|
|
38
60
|
export function multiply(a: Vector2 | Vector3, b: number | Vector2 | Vector3): Vector2 | Vector3 {
|
|
39
|
-
if (typeof b === 'number') return a.map((v) => v * b) as Vector2 | Vector3;
|
|
61
|
+
if (typeof b === 'number') return a.map((v) => v * (b as number)) as Vector2 | Vector3;
|
|
62
|
+
[a, b] = padVectors(a, b);
|
|
40
63
|
return a.map((v, i) => v * b[i]) as Vector2 | Vector3;
|
|
41
64
|
}
|
|
42
65
|
|
|
@@ -49,8 +72,14 @@ export function multiply(a: Vector2 | Vector3, b: number | Vector2 | Vector3): V
|
|
|
49
72
|
* @returns <zh/> 两个向量的商或者向量和标量的商 | <en/> The quotient of the two vectors or the quotient of the vector and scalar
|
|
50
73
|
*/
|
|
51
74
|
export function divide(a: Vector2 | Vector3, b: number | Vector2 | Vector3): Vector2 | Vector3 {
|
|
52
|
-
if (typeof b === 'number') return a.map((v) => v / b) as Vector2 | Vector3;
|
|
53
|
-
|
|
75
|
+
if (typeof b === 'number') return a.map((v) => v / (b as number)) as Vector2 | Vector3;
|
|
76
|
+
[a, b] = padVectors(a, b);
|
|
77
|
+
return a.map((v, i) => {
|
|
78
|
+
if (b[i] == 0) {
|
|
79
|
+
throw new Error(format('Vector could not be divided by zero'));
|
|
80
|
+
}
|
|
81
|
+
return v / b[i];
|
|
82
|
+
}) as Vector2 | Vector3;
|
|
54
83
|
}
|
|
55
84
|
|
|
56
85
|
/**
|
|
@@ -62,6 +91,7 @@ export function divide(a: Vector2 | Vector3, b: number | Vector2 | Vector3): Vec
|
|
|
62
91
|
* @returns <zh/> 两个向量的点积 | <en/> The dot product of the two vectors
|
|
63
92
|
*/
|
|
64
93
|
export function dot(a: Vector2 | Vector3, b: Vector2 | Vector3): number {
|
|
94
|
+
[a, b] = padVectors(a, b);
|
|
65
95
|
return (a as number[]).reduce((sum, v, i) => sum + v * b[i], 0);
|
|
66
96
|
}
|
|
67
97
|
|
|
@@ -100,7 +130,8 @@ export function scale(a: Vector2 | Vector3, s: number): Vector2 | Vector3 {
|
|
|
100
130
|
* @returns <zh/> 两个向量间的距离 | <en/> The distance between the two vectors
|
|
101
131
|
*/
|
|
102
132
|
export function distance(a: Vector2 | Vector3, b: Vector2 | Vector3): number {
|
|
103
|
-
|
|
133
|
+
[a, b] = padVectors(a, b);
|
|
134
|
+
return Math.sqrt((a as number[]).reduce((sum, v, i) => sum + (v - b[i]) ** 2, 0));
|
|
104
135
|
}
|
|
105
136
|
|
|
106
137
|
/**
|
|
@@ -112,6 +143,7 @@ export function distance(a: Vector2 | Vector3, b: Vector2 | Vector3): number {
|
|
|
112
143
|
* @returns <zh/> 两个向量间的距离 | <en/> The distance between the two vectors
|
|
113
144
|
*/
|
|
114
145
|
export function manhattanDistance(a: Vector2 | Vector3, b: Vector2 | Vector3): number {
|
|
146
|
+
[a, b] = padVectors(a, b);
|
|
115
147
|
return (a as number[]).reduce((sum, v, i) => sum + Math.abs(v - b[i]), 0);
|
|
116
148
|
}
|
|
117
149
|
|
|
@@ -137,6 +169,7 @@ export function normalize(a: Vector2 | Vector3): Vector2 | Vector3 {
|
|
|
137
169
|
* @returns <zh/> 弧度值 | <en/> The angle in radians
|
|
138
170
|
*/
|
|
139
171
|
export function angle(a: Vector2 | Vector3, b: Vector2 | Vector3, clockwise = false): number {
|
|
172
|
+
[a, b] = padVectors(a, b);
|
|
140
173
|
const determinant = a[0] * b[1] - a[1] * b[0];
|
|
141
174
|
let angle = Math.acos(
|
|
142
175
|
(multiply(a, b) as number[]).reduce((sum: number, v: number) => sum + v, 0) /
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '5.1.0
|
|
1
|
+
export const version = '5.1.0';
|