api.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { Graph, Cell, ModifierKey } from '@antv/x6'
  2. import { Selection } from './index'
  3. import { SelectionImpl } from './selection'
  4. declare module '@antv/x6/lib/graph/graph' {
  5. interface Graph {
  6. isSelectionEnabled: () => boolean
  7. enableSelection: () => Graph
  8. disableSelection: () => Graph
  9. toggleSelection: (enabled?: boolean) => Graph
  10. isMultipleSelection: () => boolean
  11. enableMultipleSelection: () => Graph
  12. disableMultipleSelection: () => Graph
  13. toggleMultipleSelection: (multiple?: boolean) => Graph
  14. isSelectionMovable: () => boolean
  15. enableSelectionMovable: () => Graph
  16. disableSelectionMovable: () => Graph
  17. toggleSelectionMovable: (movable?: boolean) => Graph
  18. isRubberbandEnabled: () => boolean
  19. enableRubberband: () => Graph
  20. disableRubberband: () => Graph
  21. toggleRubberband: (enabled?: boolean) => Graph
  22. isStrictRubberband: () => boolean
  23. enableStrictRubberband: () => Graph
  24. disableStrictRubberband: () => Graph
  25. toggleStrictRubberband: (strict?: boolean) => Graph
  26. setRubberbandModifiers: (modifiers?: string | ModifierKey[] | null) => Graph
  27. setSelectionFilter: (filter?: Selection.Filter) => Graph
  28. setSelectionDisplayContent: (content?: Selection.Content) => Graph
  29. isSelectionEmpty: () => boolean
  30. cleanSelection: (options?: Selection.SetOptions) => Graph
  31. resetSelection: (
  32. cells?: Cell | string | (Cell | string)[],
  33. options?: Selection.SetOptions,
  34. ) => Graph
  35. getSelectedCells: () => Cell[]
  36. getSelectedCellCount: () => number
  37. isSelected: (cell: Cell | string) => boolean
  38. select: (
  39. cells: Cell | string | (Cell | string)[],
  40. options?: Selection.AddOptions,
  41. ) => Graph
  42. unselect: (
  43. cells: Cell | string | (Cell | string)[],
  44. options?: Selection.RemoveOptions,
  45. ) => Graph
  46. }
  47. }
  48. declare module '@antv/x6/lib/graph/events' {
  49. interface EventArgs extends SelectionImpl.SelectionEventArgs {}
  50. }
  51. Graph.prototype.isSelectionEnabled = function () {
  52. const selection = this.getPlugin('selection') as Selection
  53. if (selection) {
  54. return selection.isEnabled()
  55. }
  56. return false
  57. }
  58. Graph.prototype.enableSelection = function () {
  59. const selection = this.getPlugin('selection') as Selection
  60. if (selection) {
  61. selection.enable()
  62. }
  63. return this
  64. }
  65. Graph.prototype.disableSelection = function () {
  66. const selection = this.getPlugin('selection') as Selection
  67. if (selection) {
  68. selection.disable()
  69. }
  70. return this
  71. }
  72. Graph.prototype.toggleSelection = function (enabled?: boolean) {
  73. const selection = this.getPlugin('selection') as Selection
  74. if (selection) {
  75. selection.toggleEnabled(enabled)
  76. }
  77. return this
  78. }
  79. Graph.prototype.isMultipleSelection = function () {
  80. const selection = this.getPlugin('selection') as Selection
  81. if (selection) {
  82. return selection.isMultipleSelection()
  83. }
  84. return false
  85. }
  86. Graph.prototype.enableMultipleSelection = function () {
  87. const selection = this.getPlugin('selection') as Selection
  88. if (selection) {
  89. selection.enableMultipleSelection()
  90. }
  91. return this
  92. }
  93. Graph.prototype.disableMultipleSelection = function () {
  94. const selection = this.getPlugin('selection') as Selection
  95. if (selection) {
  96. selection.disableMultipleSelection()
  97. }
  98. return this
  99. }
  100. Graph.prototype.toggleMultipleSelection = function (multiple?: boolean) {
  101. const selection = this.getPlugin('selection') as Selection
  102. if (selection) {
  103. selection.toggleMultipleSelection(multiple)
  104. }
  105. return this
  106. }
  107. Graph.prototype.isSelectionMovable = function () {
  108. const selection = this.getPlugin('selection') as Selection
  109. if (selection) {
  110. return selection.isSelectionMovable()
  111. }
  112. return false
  113. }
  114. Graph.prototype.enableSelectionMovable = function () {
  115. const selection = this.getPlugin('selection') as Selection
  116. if (selection) {
  117. selection.enableSelectionMovable()
  118. }
  119. return this
  120. }
  121. Graph.prototype.disableSelectionMovable = function () {
  122. const selection = this.getPlugin('selection') as Selection
  123. if (selection) {
  124. selection.disableSelectionMovable()
  125. }
  126. return this
  127. }
  128. Graph.prototype.toggleSelectionMovable = function (movable?: boolean) {
  129. const selection = this.getPlugin('selection') as Selection
  130. if (selection) {
  131. selection.toggleSelectionMovable(movable)
  132. }
  133. return this
  134. }
  135. Graph.prototype.isRubberbandEnabled = function () {
  136. const selection = this.getPlugin('selection') as Selection
  137. if (selection) {
  138. return selection.isRubberbandEnabled()
  139. }
  140. return false
  141. }
  142. Graph.prototype.enableRubberband = function () {
  143. const selection = this.getPlugin('selection') as Selection
  144. if (selection) {
  145. selection.enableRubberband()
  146. }
  147. return this
  148. }
  149. Graph.prototype.disableRubberband = function () {
  150. const selection = this.getPlugin('selection') as Selection
  151. if (selection) {
  152. selection.disableRubberband()
  153. }
  154. return this
  155. }
  156. Graph.prototype.toggleRubberband = function (enabled?: boolean) {
  157. const selection = this.getPlugin('selection') as Selection
  158. if (selection) {
  159. selection.toggleRubberband(enabled)
  160. }
  161. return this
  162. }
  163. Graph.prototype.isStrictRubberband = function () {
  164. const selection = this.getPlugin('selection') as Selection
  165. if (selection) {
  166. return selection.isStrictRubberband()
  167. }
  168. return false
  169. }
  170. Graph.prototype.enableStrictRubberband = function () {
  171. const selection = this.getPlugin('selection') as Selection
  172. if (selection) {
  173. selection.enableStrictRubberband()
  174. }
  175. return this
  176. }
  177. Graph.prototype.disableStrictRubberband = function () {
  178. const selection = this.getPlugin('selection') as Selection
  179. if (selection) {
  180. selection.disableStrictRubberband()
  181. }
  182. return this
  183. }
  184. Graph.prototype.toggleStrictRubberband = function (strict?: boolean) {
  185. const selection = this.getPlugin('selection') as Selection
  186. if (selection) {
  187. selection.toggleStrictRubberband(strict)
  188. }
  189. return this
  190. }
  191. Graph.prototype.setRubberbandModifiers = function (
  192. modifiers?: string | ModifierKey[] | null,
  193. ) {
  194. const selection = this.getPlugin('selection') as Selection
  195. if (selection) {
  196. selection.setRubberbandModifiers(modifiers)
  197. }
  198. return this
  199. }
  200. Graph.prototype.setSelectionFilter = function (filter?: Selection.Filter) {
  201. const selection = this.getPlugin('selection') as Selection
  202. if (selection) {
  203. selection.setSelectionFilter(filter)
  204. }
  205. return this
  206. }
  207. Graph.prototype.setSelectionDisplayContent = function (
  208. content?: Selection.Content,
  209. ) {
  210. const selection = this.getPlugin('selection') as Selection
  211. if (selection) {
  212. selection.setSelectionDisplayContent(content)
  213. }
  214. return this
  215. }
  216. Graph.prototype.isSelectionEmpty = function () {
  217. const selection = this.getPlugin('selection') as Selection
  218. if (selection) {
  219. return selection.isEmpty()
  220. }
  221. return true
  222. }
  223. Graph.prototype.cleanSelection = function (options?: Selection.SetOptions) {
  224. const selection = this.getPlugin('selection') as Selection
  225. if (selection) {
  226. selection.clean(options)
  227. }
  228. return this
  229. }
  230. Graph.prototype.resetSelection = function (
  231. cells?: Cell | string | (Cell | string)[],
  232. options?: Selection.SetOptions,
  233. ) {
  234. const selection = this.getPlugin('selection') as Selection
  235. if (selection) {
  236. selection.reset(cells, options)
  237. }
  238. return this
  239. }
  240. Graph.prototype.getSelectedCells = function () {
  241. const selection = this.getPlugin('selection') as Selection
  242. if (selection) {
  243. return selection.getSelectedCells()
  244. }
  245. return []
  246. }
  247. Graph.prototype.getSelectedCellCount = function () {
  248. const selection = this.getPlugin('selection') as Selection
  249. if (selection) {
  250. return selection.getSelectedCellCount()
  251. }
  252. return 0
  253. }
  254. Graph.prototype.isSelected = function (cell: Cell | string) {
  255. const selection = this.getPlugin('selection') as Selection
  256. if (selection) {
  257. return selection.isSelected(cell)
  258. }
  259. return false
  260. }
  261. Graph.prototype.select = function (
  262. cells: Cell | string | (Cell | string)[],
  263. options?: Selection.AddOptions,
  264. ) {
  265. const selection = this.getPlugin('selection') as Selection
  266. if (selection) {
  267. selection.select(cells, options)
  268. }
  269. return this
  270. }
  271. Graph.prototype.unselect = function (
  272. cells: Cell | string | (Cell | string)[],
  273. options?: Selection.RemoveOptions,
  274. ) {
  275. const selection = this.getPlugin('selection') as Selection
  276. if (selection) {
  277. selection.unselect(cells, options)
  278. }
  279. return this
  280. }