index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // panel/index.js, this filename needs to match the one registered in package.json
  2. const Fs = require("fire-fs");
  3. const Path = require("fire-path");
  4. Editor.Panel.extend({
  5. style: Fs.readFileSync(
  6. Editor.url("packages://bezier/panel/index.css"),
  7. "utf-8"
  8. ),
  9. template: Fs.readFileSync(
  10. Editor.url("packages://bezier/panel/index.html"),
  11. "utf-8"
  12. ),
  13. // element and variable binding
  14. $: {
  15. btn: '#btn',
  16. label: '#label',
  17. pointBtn: '#point',
  18. },
  19. // method executed when template and styles are successfully loaded and initialized
  20. ready() {
  21. this.$btn.addEventListener('confirm', () => {
  22. Editor.Ipc.sendToMain('bezier:clicked');
  23. });
  24. // this.$pointBtn.addEventListener('click', () => {
  25. // Editor.log('Button clicked!');
  26. // });
  27. this.addMenuMoveAction_mouse(this.$pointBtn);
  28. },
  29. addMenuMoveAction_mouse(node) {
  30. Editor.log('addMenuMoveAction_mouse');
  31. node.style.position = "absolute";
  32. // 计算移动范围
  33. let doc = document.documentElement;
  34. node.maxX = doc.clientWidth - node.offsetWidth;
  35. node.maxY = doc.clientHeight - node.offsetHeight;
  36. // 是否按下
  37. node.isMouseDown = false;
  38. // 事件
  39. node.addEventListener("mousedown", (e) => {
  40. e.target.isMouseDown = true;
  41. }, false);
  42. node.addEventListener("mousemove", this.moveMenu_mouse, false);
  43. node.addEventListener("mouseup", (e) => {
  44. e.target.isMouseDown = false
  45. }, false);
  46. },
  47. //移动
  48. moveMenu_mouse(event) {
  49. let node = event.target;
  50. if (!node.isMouseDown) {
  51. return;
  52. }
  53. Editor.log('moveMenu_mouse offsetWidth:' + node);
  54. var x = Number(event.clientX) - node.offsetWidth / 2; //页面触点X坐标
  55. var y = Number(event.clientY) - node.offsetHeight / 2; //页面触点Y坐标
  56. //限制x y 区间,
  57. x = x < 0 ? 0 : x;
  58. x = x > node.maxX ? node.maxX : x;
  59. y = y < 0 ? 0 : y;
  60. y = y > node.maxY ? node.maxY : y;
  61. Editor.log(`x:${x},y${y}`);
  62. node.style.left = x + "px";
  63. node.style.top = y + "px";
  64. node.style.color = "#ff0000"
  65. },
  66. // register your ipc messages here
  67. messages: {
  68. // 'bezier:hello'(event) {
  69. // this.$label.innerText = 'Hello!';
  70. // }
  71. }
  72. });