feat: support use alias import files

This commit is contained in:
zhanning.bzn 2019-12-10 17:54:47 +08:00
parent fc6f66ed32
commit 831c5df4af
9 changed files with 379 additions and 18 deletions

20
jest.config.js Normal file
View File

@ -0,0 +1,20 @@
module.exports = {
runner: 'jest-electron/runner',
testEnvironment: 'jest-electron/environment',
preset: 'ts-jest',
collectCoverage: false,
collectCoverageFrom: [
'src/**/*.{ts,js}',
'!**/node_modules/**',
'!**/vendor/**'
],
testRegex: '/tests/.*-spec\\.ts?$',
moduleDirectories: [ 'node_modules', 'src' ],
moduleFileExtensions: [ 'js', 'ts', 'json' ],
moduleNameMapper: {
'@g6/(.*)': '<rootDir>/src/$1',
'@g6/types': '<rootDir>/types'
}
};

View File

@ -26,7 +26,7 @@
"clean": "rimraf esm lib dist",
"lint": "lint-staged",
"test": "jest",
"test-live": "DEBUG_MODE=1 jest --watch ./tests/unit/behavior/index-spec.ts",
"test-live": "DEBUG_MODE=1 jest --watch ./tests/unit/graph/controller/mode-spec.ts",
"coverage": "jest --coverage",
"ci": "run-s build coverage",
"doc": "rimraf apis && typedoc",
@ -81,18 +81,6 @@
"git add"
]
},
"jest": {
"runner": "jest-electron/runner",
"testEnvironment": "jest-electron/environment",
"preset": "ts-jest",
"collectCoverage": false,
"collectCoverageFrom": [
"src/**/*.{ts,js}",
"!**/node_modules/**",
"!**/vendor/**"
],
"testRegex": "/tests/.*-spec\\.ts?$"
},
"repository": {
"type": "git",
"url": "https://github.com/antvis/g6"

View File

@ -0,0 +1 @@
export { default as Mode } from './mode'

View File

@ -0,0 +1,175 @@
import each from '@antv/util/lib/each'
import isArray from '@antv/util/lib/is-array'
import isString from '@antv/util/lib/is-string'
import Behavior from '@g6/behavior/behavior'
import { IGraph, IMode, IModeType } from '@g6/interface/graph';
import { IBehavior } from 'types';
export default class Mode {
private graph: IGraph
/**
* modes = {
* default: [ 'drag-node', 'zoom-canvas' ],
* edit: [ 'drag-canvas', {
* type: 'brush-select',
* trigger: 'ctrl'
* }]
* }
*
* @private
* @type {IMode}
* @memberof Mode
*/
public modes: IMode
/**
* mode = 'drag-node'
*
* @private
* @type {string}
* @memberof Mode
*/
public mode: string
private currentBehaves: IBehavior[]
constructor(graph: IGraph) {
this.graph = graph
this.modes = graph.get('modes') || {
default: []
}
this.formatModes()
this.mode = graph.get('defaultMode') || 'default'
this.currentBehaves = []
this.setMode(this.mode)
}
private formatModes() {
const modes = this.modes;
each(modes, mode => {
each(mode, (behavior, i) => {
if (isString(behavior)) {
mode[i] = { type: behavior };
}
});
});
}
private setBehaviors(mode: string) {
const graph = this.graph;
const behaviors = this.modes[mode];
const behaves: IBehavior[] = [];
let behave: IBehavior;
each(behaviors, behavior => {
const BehaviorInstance = Behavior.getBehavior(behavior.type)
if (!BehaviorInstance) {
return;
}
behave = new BehaviorInstance(behavior);
if(behave) {
behave.bind(graph)
behaves.push(behave);
}
});
this.currentBehaves = behaves;
}
private mergeBehaviors(modeBehaviors: IModeType[], behaviors: IModeType[]): IModeType[] {
each(behaviors, behavior => {
if (modeBehaviors.indexOf(behavior) < 0) {
if (isString(behavior)) {
behavior = { type: behavior };
}
modeBehaviors.push(behavior);
}
});
return modeBehaviors;
}
private filterBehaviors(modeBehaviors: IModeType[], behaviors: IModeType[]): IModeType[] {
const result: IModeType[] = [];
modeBehaviors.forEach(behavior => {
let type: string = ''
if(isString(behavior)) {
type = behavior
} else {
type = behavior.type
}
if (behaviors.indexOf(type) < 0) {
result.push(behavior);
}
});
return result;
}
public setMode(mode: string): Mode {
const modes = this.modes;
const graph = this.graph;
const behaviors = modes[mode];
if (!behaviors) {
return;
}
graph.emit('beforemodechange', { mode });
each(this.currentBehaves, behave => {
behave.unbind(graph);
});
this.setBehaviors(mode);
graph.emit('aftermodechange', { mode });
this.mode = mode;
return this;
}
/**
* Behavior
*
* @param {IModeType[]} behaviors
* @param {(IModeType[] | IModeType)} modes
* @param {boolean} isAdd
* @returns {Mode}
* @memberof Mode
*/
public manipulateBehaviors(behaviors: IModeType[], modes: IModeType[] | IModeType, isAdd: boolean): Mode {
const self = this
if(!isArray(behaviors)) {
behaviors = [ behaviors ]
}
if(isArray(modes)) {
each(modes, mode => {
if (!self.modes[mode]) {
if (isAdd) {
self.modes[mode] = [].concat(behaviors);
}
} else {
if (isAdd) {
self.modes[mode] = this.mergeBehaviors(self.modes[mode], behaviors);
} else {
self.modes[mode] = this.filterBehaviors(self.modes[mode], behaviors);
}
}
})
return this
}
let currentMode: string = ''
if(!modes) {
currentMode = this.mode
}
if (isAdd) {
self.modes[currentMode] = this.mergeBehaviors(self.modes[currentMode], behaviors);
} else {
self.modes[currentMode] = this.filterBehaviors(self.modes[currentMode], behaviors);
}
self.setMode(this.mode)
return this
}
}

13
src/graph/graph.ts Normal file
View File

@ -0,0 +1,13 @@
import EventEmitter from '@antv/event-emitter'
import { IGraph, IGraphOptions } from '@g6/interface/graph';
export default class Graph extends EventEmitter implements IGraph {
private _cfg: IGraphOptions
constructor(cfg: IGraphOptions) {
super()
this._cfg = cfg
}
public get(key: string) {
}
}

0
src/graph/tree-graph.ts Normal file
View File

View File

@ -1,7 +1,147 @@
import { G6Event } from '../../types'
import EventEmitter from '@antv/event-emitter';
import { G6Event, IModelStyle, IShapeStyle } from '@g6/types'
export interface IGraph {
on: (event: G6Event, handler: () => void) => void;
off: (event: G6Event, handler: () => void) => void;
export interface IModeOption {
type: string;
}
export type IModeType = string | IModeOption
export interface IMode {
default: IModeType[]
[key: string]: IModeType[]
}
export interface ILayoutOptions {
type: string;
}
export interface IGraphOptions {
/**
* DOM DOM id HTML
*/
container: string | HTMLElement;
/**
* 'px'
*/
width: number;
/**
* 'px'
*/
height: number;
/**
* canvas和svg
*/
renderer?: 'canvas' | 'svg';
fitView?: boolean;
layout?: ILayoutOptions;
/**
*
* , fitViewPadding: 20
* fitViewPadding: [20, 40, 50,20]
*
*/
fitViewPadding?: number[] | number;
/**
* false
* true
*/
groupByTypes?: boolean;
groupStyle?: {
style: {
[key: string]: IShapeStyle
};
};
/**
* setAutoPaint()
* true
*/
autoPaint?: boolean;
/**
* G6中的Mode文档
*/
modes?: IMode;
/**
* shape, size, color data
*/
defaultNode?: {
shape?: string,
size?: string,
color?: string,
} & IModelStyle;
/**
* shape, size, color data
*/
defaultEdge?: {
shape?: string,
size?: string,
color?: string,
} & IModelStyle;
nodeStateStyles?: IModelStyle;
edgeStateStyles?: IModelStyle;
/**
* graph plugin
*/
plugins?: any[];
/**
*
*/
animate?: boolean;
/**
* animate为true时有效
*/
animateCfg?: {
/**
*
*/
onFrame?: () => void | null;
/**
*
*/
duration?: number;
/**
*
* easeLinear
*/
easing?: string;
};
/**
*
* 0.2
*/
minZoom?: number;
/**
*
* 10
*/
maxZoom?: number;
/**
*
* 1.0
*/
pixelRatio?: number;
groupType?: string;
/**
* Edge
*/
linkCenter?: boolean;
}
export interface IGraph extends EventEmitter {
get: (key: string) => any;
}

View File

@ -0,0 +1,19 @@
import { Mode } from '../../../../src/graph/controller'
import Graph from '../../../../src/graph/graph'
import { IGraph, IGraphOptions } from '../../../../src/interface/graph';
describe('Mode Controller', () => {
it('signle mode', () => {
const cfg: IGraphOptions = {
container: 'x',
width: 200,
height: 100
}
const graph: IGraph = new Graph(cfg)
const modeController = new Mode(graph)
expect(Object.keys(modeController.modes).length).toBe(1);
expect(modeController.modes.default).not.toBe(undefined);
expect(modeController.modes.default.length).toBe(0);
expect(modeController.mode).toBe('default');
})
})

View File

@ -11,7 +11,12 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"types": ["jest"]
"types": ["jest"],
"baseUrl": ".",
"paths": {
"@g6/*": ["./src/*"],
"@g6/types": ["./types"]
}
},
"include": ["src"],
"typedocOptions": {