1
1
import { ComponentType , createElement as h , render } from 'preact/compat'
2
- import { cloneDeep , forEach } from 'lodash-es'
2
+ import { cloneDeep , forEach , indexOf } from 'lodash-es'
3
3
import { observer } from '.'
4
4
import { Options as LFOptions } from './options'
5
5
import * as _Model from './model'
@@ -24,8 +24,9 @@ import History from './history'
24
24
import { EventCallback , CallbackArgs , EventArgs } from './event/eventEmitter'
25
25
import { ElementType , EventType , SegmentDirection } from './constant'
26
26
import { initDefaultShortcut } from './keyboard/shortcut'
27
+
27
28
import Extension = LogicFlow . Extension
28
- import RegisteredExtension = LogicFlow . RegisteredExtension
29
+ import ExtensionConfig = LogicFlow . ExtensionConfig
29
30
import ExtensionConstructor = LogicFlow . ExtensionConstructor
30
31
import GraphConfigData = LogicFlow . GraphConfigData
31
32
import NodeConfig = LogicFlow . NodeConfig
@@ -38,11 +39,13 @@ import RegisterParam = LogicFlow.RegisterParam
38
39
import GraphElements = LogicFlow . GraphElements
39
40
import Position = LogicFlow . Position
40
41
import PointTuple = LogicFlow . PointTuple
41
- import ExtensionRender = LogicFlow . ExtensionRender
42
+ import ExtensionRenderFunc = LogicFlow . ExtensionRenderFunc
42
43
import RegisterElementFunc = LogicFlow . RegisterElementFunc
43
44
import PropertiesType = LogicFlow . PropertiesType
44
45
import BaseNodeModelCtor = LogicFlow . BaseNodeModelCtor
45
46
import ClientPosition = LogicFlow . ClientPosition
47
+ import ExtensionDefinition = LogicFlow . ExtensionDefinition
48
+ import ExtensionType = LogicFlow . ExtensionType
46
49
47
50
const pluginFlag = Symbol ( 'plugin registered by Logicflow.use' )
48
51
@@ -61,13 +64,13 @@ export class LogicFlow {
61
64
tool : Tool
62
65
snaplineModel ?: SnaplineModel
63
66
64
- components : ExtensionRender [ ] = [ ]
67
+ components : ExtensionRenderFunc [ ] = [ ]
65
68
// 个性配置的插件,覆盖全局配置的插件
66
- readonly plugins : ExtensionConstructor [ ]
69
+ readonly plugins : ExtensionType [ ]
67
70
// 全局配置的插件,所有的LogicFlow示例都会使用
68
- static extensions : Map < string , RegisteredExtension > = new Map ( )
71
+ static extensions : Map < string , ExtensionConfig > = new Map ( )
69
72
// 插件扩展方法
70
- extension : Record < string , Extension > = { }
73
+ extension : Record < string , Extension | ExtensionDefinition > = { }
71
74
72
75
readonly width ?: number // 只读:画布宽度
73
76
readonly height ?: number // 只读:画布高度
@@ -1292,19 +1295,14 @@ export class LogicFlow {
1292
1295
* @param props
1293
1296
*/
1294
1297
static use (
1295
- extension : ExtensionConstructor ,
1298
+ extension : ExtensionConstructor | ExtensionDefinition ,
1296
1299
props ?: Record < string , unknown > ,
1297
1300
) : void {
1298
- let { pluginName } = extension
1301
+ const { pluginName } = extension
1299
1302
if ( ! pluginName ) {
1300
- console . warn (
1301
- `请给插件${
1302
- extension . name || extension . constructor . name
1303
- } 指定pluginName!`,
1304
- )
1305
- pluginName = extension . name // 兼容以前name的情况,1.0版本去掉。
1303
+ throw new Error ( `请给插件指定 pluginName!` )
1306
1304
}
1307
- // TODO: 应该在何时进行插件的销毁
1305
+ // TODO: 应该在何时进行插件的销毁???
1308
1306
// const preExtension = this.extensions.get(pluginName)?.extension
1309
1307
// preExtension?.destroy?.() // 该代码应该有问题,因为 preExtension 直接用的是 Constructor,没有实例化。无法访问实例方法 destroy
1310
1308
@@ -1316,19 +1314,26 @@ export class LogicFlow {
1316
1314
}
1317
1315
1318
1316
private installPlugins ( disabledPlugins : string [ ] = [ ] ) {
1317
+ const extensionsAddByUse = Array . from (
1318
+ LogicFlow . extensions ,
1319
+ ( [ , extension ] ) => extension ,
1320
+ )
1319
1321
// 安装插件,优先使用个性插件
1320
- const extensions = this . plugins ?? LogicFlow . extensions
1321
- extensions . forEach ( ( ext : any ) => {
1322
- let extension : any
1323
- let props = null
1324
- if ( ext [ pluginFlag ] ) {
1322
+ const extensions = [ ...this . plugins , ...extensionsAddByUse ]
1323
+ forEach ( extensions , ( ext ) => {
1324
+ let extension : ExtensionConstructor | ExtensionDefinition
1325
+ let props : Record < string , any > | undefined
1326
+
1327
+ if ( pluginFlag in ext ) {
1325
1328
extension = ext . extension
1326
1329
props = ext . props
1327
1330
} else {
1328
1331
extension = ext
1329
1332
}
1330
- const pluginName = extension ?. pluginName || extension ?. name
1331
- if ( disabledPlugins . indexOf ( pluginName ) === - 1 ) {
1333
+
1334
+ const pluginName = extension ?. pluginName
1335
+
1336
+ if ( indexOf ( disabledPlugins , pluginName ) === - 1 ) {
1332
1337
this . installPlugin ( extension , props )
1333
1338
}
1334
1339
} )
@@ -1337,31 +1342,38 @@ export class LogicFlow {
1337
1342
/**
1338
1343
* 加载插件-内部方法
1339
1344
*/
1340
- private installPlugin ( extension : Extension , props : any ) {
1341
- if ( typeof extension === 'object' ) {
1342
- const { pluginName, install, render : renderComponent } = extension
1345
+ private installPlugin (
1346
+ extension : ExtensionConstructor | ExtensionDefinition ,
1347
+ props ?: Record < string , any > ,
1348
+ ) {
1349
+ if ( 'pluginName' in extension && 'install' in extension ) {
1350
+ const { pluginName, install, render } = extension
1343
1351
if ( pluginName ) {
1344
1352
install && install . call ( extension , this , LogicFlow )
1345
- renderComponent && this . components . push ( renderComponent . bind ( extension ) )
1353
+ render && this . components . push ( render . bind ( extension ) )
1346
1354
this . extension [ pluginName ] = extension
1347
1355
}
1348
1356
return
1349
1357
}
1350
- const ExtensionCls = extension as ExtensionConstructor
1351
- const extensionInstance = new ExtensionCls ( {
1358
+
1359
+ const ExtensionCtor = extension as ExtensionConstructor
1360
+ const extensionIns = new ExtensionCtor ( {
1352
1361
lf : this ,
1353
1362
LogicFlow,
1354
- options : this . options . pluginsOptions ?? { } ,
1355
1363
props,
1364
+ options : this . options . pluginsOptions ?? { } ,
1356
1365
} )
1357
- extensionInstance . render &&
1358
- this . components . push ( extensionInstance . render . bind ( extensionInstance ) )
1359
- this . extension [ ExtensionCls . pluginName ] = extensionInstance
1366
+ extensionIns . render &&
1367
+ this . components . push ( extensionIns . render . bind ( extensionIns ) )
1368
+ this . extension [ ExtensionCtor . pluginName ] = extensionIns
1360
1369
}
1361
1370
}
1362
1371
1363
1372
// Option
1364
1373
export namespace LogicFlow {
1374
+ /**
1375
+ * LogicFlow init Options
1376
+ */
1365
1377
export interface Options extends LFOptions . Common { }
1366
1378
1367
1379
export type DomAttributes = {
@@ -1376,6 +1388,7 @@ export namespace LogicFlow {
1376
1388
ry ?: number
1377
1389
} & Record < string , any >
1378
1390
export type AttributesType = Record < string , any >
1391
+
1379
1392
export type VectorData = {
1380
1393
deltaX : number
1381
1394
deltaY : number
@@ -1755,8 +1768,14 @@ export namespace LogicFlow {
1755
1768
1756
1769
// Render or Functions
1757
1770
export namespace LogicFlow {
1758
- type FocusOnById = { id : string ; coordinate ?: never }
1759
- type FocusOnByCoordinate = { id ?: string ; coordinate : Position }
1771
+ type FocusOnById = {
1772
+ id : string
1773
+ coordinate ?: never
1774
+ }
1775
+ type FocusOnByCoordinate = {
1776
+ id ?: string
1777
+ coordinate : Position
1778
+ }
1760
1779
export type FocusOnArgsType = FocusOnById | FocusOnByCoordinate
1761
1780
1762
1781
export type BaseNodeModelCtor = typeof BaseNodeModel
@@ -1794,32 +1813,43 @@ export namespace LogicFlow {
1794
1813
new ( options : LFOptions . Definition ) : LogicFlow
1795
1814
}
1796
1815
1797
- export type RegisteredExtension = {
1816
+ /**
1817
+ * Extension 插件类型
1818
+ */
1819
+ export type ExtensionType = ExtensionConstructor | ExtensionDefinition
1820
+ export type ExtensionConfig = {
1798
1821
[ pluginFlag ] : symbol
1799
- extension : ExtensionConstructor
1800
- props ?: Record < string , unknown >
1822
+ extension : ExtensionType
1823
+ props ?: Record < string , any > // TODO: 看这类型是否可以更精确
1801
1824
}
1802
1825
1803
- export type ExtensionProps = {
1826
+ export type IExtensionProps = {
1804
1827
lf : LogicFlow
1805
1828
LogicFlow : LogicFlowConstructor
1806
- options : Record < string , unknown >
1807
1829
props ?: Record < string , unknown >
1830
+ options : Record < string , unknown >
1808
1831
}
1809
1832
1810
1833
export interface ExtensionConstructor {
1811
1834
pluginName : string
1812
-
1813
- new ( props : ExtensionProps ) : Extension
1835
+ new ( props : IExtensionProps ) : Extension
1814
1836
}
1815
1837
1816
- export type ExtensionRender = ( lf : LogicFlow , container : HTMLElement ) => void
1838
+ export type ExtensionRenderFunc = (
1839
+ lf : LogicFlow ,
1840
+ container : HTMLElement ,
1841
+ ) => void
1842
+
1843
+ // 对象形式定义的插件
1844
+ export type ExtensionDefinition = {
1845
+ pluginName : string
1846
+ install ?: ( lf : LogicFlow , LFCtor : LogicFlowConstructor ) => void
1847
+ render ?: ExtensionRenderFunc
1848
+ }
1817
1849
1818
1850
export interface Extension {
1819
- readonly pluginName ?: string // 插件名称,只用用于插件覆盖和细粒度控制加载哪些插件
1820
- install ?: ( lf : LogicFlow , logicFlow : LogicFlowConstructor ) => void
1821
- render ?: ExtensionRender
1822
- destroy ?: ( ) => void
1851
+ render : ExtensionRenderFunc
1852
+ destroy ?: ( ) => void // TODO: 确认插件销毁函数参数类型
1823
1853
}
1824
1854
}
1825
1855
0 commit comments