brush.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { registerAction, registerInteraction } from '@antv/g2';
  2. import { ButtonAction } from './actions/reset-button';
  3. registerAction('brush-reset-button', ButtonAction, {
  4. name: 'brush-reset-button',
  5. });
  6. registerInteraction('filter-action', {});
  7. /**
  8. * G2 已经内置了 brush、brush-x、brush-y 等交互,其它:
  9. *
  10. * 1. element-range-highlight 是否可用重命名为 brush-highlight?(mask 可以移动)
  11. * 2. brush-visible 与 brush 的区别是?
  12. */
  13. function isPointInView(context) {
  14. return context.isInPlot();
  15. }
  16. /**
  17. * 获取 交互 start 阶段的相关配置
  18. */
  19. export function getInteractionCfg(interactionType, brushType, options) {
  20. var _a = options || {}, mask = _a.mask, isStartEnable = _a.isStartEnable;
  21. var maskType = brushType || 'rect';
  22. switch (interactionType) {
  23. case 'brush':
  24. return {
  25. showEnable: [
  26. { trigger: 'plot:mouseenter', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  27. { trigger: 'plot:mouseleave', action: 'cursor:default' },
  28. ],
  29. start: [
  30. {
  31. trigger: 'mousedown',
  32. isEnable: isStartEnable || isPointInView,
  33. action: ['brush:start', "".concat(maskType, "-mask:start"), "".concat(maskType, "-mask:show")],
  34. // 对应第二个action的参数
  35. arg: [null, { maskStyle: mask === null || mask === void 0 ? void 0 : mask.style }],
  36. },
  37. ],
  38. processing: [
  39. {
  40. trigger: 'mousemove',
  41. isEnable: isPointInView,
  42. action: ["".concat(maskType, "-mask:resize")],
  43. },
  44. ],
  45. end: [
  46. {
  47. trigger: 'mouseup',
  48. isEnable: isPointInView,
  49. action: [
  50. 'brush:filter',
  51. 'brush:end',
  52. "".concat(maskType, "-mask:end"),
  53. "".concat(maskType, "-mask:hide"),
  54. 'brush-reset-button:show',
  55. ],
  56. },
  57. ],
  58. rollback: [
  59. {
  60. trigger: 'brush-reset-button:click',
  61. action: ['brush:reset', 'brush-reset-button:hide', 'cursor:crosshair'],
  62. },
  63. ],
  64. };
  65. case 'brush-highlight':
  66. return {
  67. showEnable: [
  68. { trigger: 'plot:mouseenter', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  69. { trigger: 'plot:mousemove', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  70. {
  71. trigger: 'plot:mousemove',
  72. action: 'cursor:default',
  73. isEnable: function (context) { return (isStartEnable ? !isStartEnable(context) : false); },
  74. },
  75. { trigger: 'mask:mouseenter', action: 'cursor:move', isEnable: isStartEnable || (function () { return true; }) },
  76. { trigger: 'plot:mouseleave', action: 'cursor:default' },
  77. { trigger: 'mask:mouseleave', action: 'cursor:crosshair' },
  78. ],
  79. start: [
  80. {
  81. trigger: 'plot:mousedown',
  82. isEnable: isStartEnable ||
  83. (function (context) {
  84. // 不要点击在 mask 上重新开始
  85. return !context.isInShape('mask');
  86. }),
  87. action: ["".concat(maskType, "-mask:start"), "".concat(maskType, "-mask:show")],
  88. // 对应第 1 个action的参数
  89. arg: [{ maskStyle: mask === null || mask === void 0 ? void 0 : mask.style }],
  90. },
  91. {
  92. trigger: 'mask:dragstart',
  93. action: ["".concat(maskType, "-mask:moveStart")],
  94. },
  95. ],
  96. processing: [
  97. {
  98. trigger: 'plot:mousemove',
  99. action: ["".concat(maskType, "-mask:resize")],
  100. },
  101. {
  102. trigger: 'mask:drag',
  103. action: ["".concat(maskType, "-mask:move")],
  104. },
  105. {
  106. trigger: 'mask:change',
  107. action: ['element-range-highlight:highlight'],
  108. },
  109. ],
  110. end: [
  111. { trigger: 'plot:mouseup', action: ["".concat(maskType, "-mask:end")] },
  112. { trigger: 'mask:dragend', action: ["".concat(maskType, "-mask:moveEnd")] },
  113. {
  114. trigger: 'document:mouseup',
  115. isEnable: function (context) {
  116. return !context.isInPlot();
  117. },
  118. action: ['element-range-highlight:clear', "".concat(maskType, "-mask:end"), "".concat(maskType, "-mask:hide")],
  119. },
  120. ],
  121. rollback: [{ trigger: 'dblclick', action: ['element-range-highlight:clear', "".concat(maskType, "-mask:hide")] }],
  122. };
  123. case 'brush-x':
  124. return {
  125. showEnable: [
  126. { trigger: 'plot:mouseenter', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  127. { trigger: 'plot:mouseleave', action: 'cursor:default' },
  128. ],
  129. start: [
  130. {
  131. trigger: 'mousedown',
  132. isEnable: isStartEnable || isPointInView,
  133. action: ['brush-x:start', "".concat(maskType, "-mask:start"), "".concat(maskType, "-mask:show")],
  134. // 对应第二个action的参数
  135. arg: [null, { maskStyle: mask === null || mask === void 0 ? void 0 : mask.style }],
  136. },
  137. ],
  138. processing: [
  139. {
  140. trigger: 'mousemove',
  141. isEnable: isPointInView,
  142. action: ["".concat(maskType, "-mask:resize")],
  143. },
  144. ],
  145. end: [
  146. {
  147. trigger: 'mouseup',
  148. isEnable: isPointInView,
  149. action: ['brush-x:filter', 'brush-x:end', "".concat(maskType, "-mask:end"), "".concat(maskType, "-mask:hide")],
  150. },
  151. ],
  152. rollback: [{ trigger: 'dblclick', action: ['brush-x:reset'] }],
  153. };
  154. case 'brush-x-highlight':
  155. return {
  156. showEnable: [
  157. { trigger: 'plot:mouseenter', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  158. { trigger: 'mask:mouseenter', action: 'cursor:move', isEnable: isStartEnable || (function () { return true; }) },
  159. { trigger: 'plot:mouseleave', action: 'cursor:default' },
  160. { trigger: 'mask:mouseleave', action: 'cursor:crosshair' },
  161. ],
  162. start: [
  163. {
  164. trigger: 'plot:mousedown',
  165. isEnable: isStartEnable ||
  166. (function (context) {
  167. // 不要点击在 mask 上重新开始
  168. return !context.isInShape('mask');
  169. }),
  170. action: ["".concat(maskType, "-mask:start"), "".concat(maskType, "-mask:show")],
  171. // 对应第 1 个action的参数
  172. arg: [{ maskStyle: mask === null || mask === void 0 ? void 0 : mask.style }],
  173. },
  174. {
  175. trigger: 'mask:dragstart',
  176. action: ["".concat(maskType, "-mask:moveStart")],
  177. },
  178. ],
  179. processing: [
  180. {
  181. trigger: 'plot:mousemove',
  182. action: ["".concat(maskType, "-mask:resize")],
  183. },
  184. {
  185. trigger: 'mask:drag',
  186. action: ["".concat(maskType, "-mask:move")],
  187. },
  188. {
  189. trigger: 'mask:change',
  190. action: ['element-range-highlight:highlight'],
  191. },
  192. ],
  193. end: [
  194. { trigger: 'plot:mouseup', action: ["".concat(maskType, "-mask:end")] },
  195. { trigger: 'mask:dragend', action: ["".concat(maskType, "-mask:moveEnd")] },
  196. {
  197. trigger: 'document:mouseup',
  198. isEnable: function (context) {
  199. return !context.isInPlot();
  200. },
  201. action: ['element-range-highlight:clear', "".concat(maskType, "-mask:end"), "".concat(maskType, "-mask:hide")],
  202. },
  203. ],
  204. rollback: [{ trigger: 'dblclick', action: ['element-range-highlight:clear', "".concat(maskType, "-mask:hide")] }],
  205. };
  206. case 'brush-y':
  207. return {
  208. showEnable: [
  209. { trigger: 'plot:mouseenter', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  210. { trigger: 'plot:mouseleave', action: 'cursor:default' },
  211. ],
  212. start: [
  213. {
  214. trigger: 'mousedown',
  215. isEnable: isStartEnable || isPointInView,
  216. action: ['brush-y:start', "".concat(maskType, "-mask:start"), "".concat(maskType, "-mask:show")],
  217. // 对应第二个action的参数
  218. arg: [null, { maskStyle: mask === null || mask === void 0 ? void 0 : mask.style }],
  219. },
  220. ],
  221. processing: [
  222. {
  223. trigger: 'mousemove',
  224. isEnable: isPointInView,
  225. action: ["".concat(maskType, "-mask:resize")],
  226. },
  227. ],
  228. end: [
  229. {
  230. trigger: 'mouseup',
  231. isEnable: isPointInView,
  232. action: ['brush-y:filter', 'brush-y:end', "".concat(maskType, "-mask:end"), "".concat(maskType, "-mask:hide")],
  233. },
  234. ],
  235. rollback: [{ trigger: 'dblclick', action: ['brush-y:reset'] }],
  236. };
  237. case 'brush-y-highlight':
  238. return {
  239. showEnable: [
  240. { trigger: 'plot:mouseenter', action: 'cursor:crosshair', isEnable: isStartEnable || (function () { return true; }) },
  241. { trigger: 'mask:mouseenter', action: 'cursor:move', isEnable: isStartEnable || (function () { return true; }) },
  242. { trigger: 'plot:mouseleave', action: 'cursor:default' },
  243. { trigger: 'mask:mouseleave', action: 'cursor:crosshair' },
  244. ],
  245. start: [
  246. {
  247. trigger: 'plot:mousedown',
  248. isEnable: isStartEnable ||
  249. (function (context) {
  250. // 不要点击在 mask 上重新开始
  251. return !context.isInShape('mask');
  252. }),
  253. action: ["".concat(maskType, "-mask:start"), "".concat(maskType, "-mask:show")],
  254. // 对应第 1 个action的参数
  255. arg: [{ maskStyle: mask === null || mask === void 0 ? void 0 : mask.style }],
  256. },
  257. {
  258. trigger: 'mask:dragstart',
  259. action: ["".concat(maskType, "-mask:moveStart")],
  260. },
  261. ],
  262. processing: [
  263. {
  264. trigger: 'plot:mousemove',
  265. action: ["".concat(maskType, "-mask:resize")],
  266. },
  267. {
  268. trigger: 'mask:drag',
  269. action: ["".concat(maskType, "-mask:move")],
  270. },
  271. {
  272. trigger: 'mask:change',
  273. action: ['element-range-highlight:highlight'],
  274. },
  275. ],
  276. end: [
  277. { trigger: 'plot:mouseup', action: ["".concat(maskType, "-mask:end")] },
  278. { trigger: 'mask:dragend', action: ["".concat(maskType, "-mask:moveEnd")] },
  279. {
  280. trigger: 'document:mouseup',
  281. isEnable: function (context) {
  282. return !context.isInPlot();
  283. },
  284. action: ['element-range-highlight:clear', "".concat(maskType, "-mask:end"), "".concat(maskType, "-mask:hide")],
  285. },
  286. ],
  287. rollback: [{ trigger: 'dblclick', action: ['element-range-highlight:clear', "".concat(maskType, "-mask:hide")] }],
  288. };
  289. default:
  290. return {};
  291. }
  292. }
  293. // 直接拷贝过来的
  294. registerInteraction('brush', getInteractionCfg('brush'));
  295. // 复写 element-range-highlight interaction
  296. registerInteraction('brush-highlight', getInteractionCfg('brush-highlight'));
  297. // 复写
  298. registerInteraction('brush-x', getInteractionCfg('brush-x', 'x-rect'));
  299. // 复写
  300. registerInteraction('brush-y', getInteractionCfg('brush-y', 'y-rect'));
  301. // 新增, x 框选高亮
  302. registerInteraction('brush-x-highlight', getInteractionCfg('brush-x-highlight', 'x-rect'));
  303. // 新增, y 框选高亮
  304. registerInteraction('brush-y-highlight', getInteractionCfg('brush-y-highlight', 'y-rect'));
  305. //# sourceMappingURL=brush.js.map