Undo1.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div>
  3. <el-button @click="addCircle">Circle</el-button>
  4. <el-button @click="addRect">Rect</el-button>
  5. <el-button @click="deleteItem">Delete</el-button>
  6. <el-button @click="undo">Undo</el-button>
  7. <el-button @click="redo">Redo</el-button>
  8. <el-button @click="log">日志</el-button>
  9. <canvas id="undoFrame" width="800" height="400"/>
  10. </div>
  11. </template>
  12. <script lang="ts">
  13. import { SMouseEvent, SUndoStack } from "@persagy-web/base";
  14. import { SColor, SPainter, SRect } from "@persagy-web/draw";
  15. import { SGraphAddCommand, SGraphItem, SGraphMoveCommand, SGraphScene, SGraphView } from "@persagy-web/graph";
  16. import { SPoint } from "@persagy-web/draw/lib";
  17. /**
  18. * Undo/Redo
  19. *
  20. * @author 郝洁 <haojie@persagy.com>
  21. */
  22. class RectItem extends SGraphItem {
  23. /** 宽度 */
  24. width = 100;
  25. /** 高度 */
  26. height = 100;
  27. /** 图 Id */
  28. id = new Date().getTime() + '';
  29. /**
  30. * 构造函数
  31. *
  32. * @param parent Item 图像引擎
  33. */
  34. constructor(parent: SGraphItem | null) {
  35. super(parent);
  36. this.moveable = true;
  37. this.selectable = true;
  38. this.selected = true;
  39. this.isTransform = true;
  40. this.rotate = 60;
  41. }
  42. /**
  43. * 矩形数据类型绘制
  44. *
  45. * @return 边界区域
  46. */
  47. boundingRect(): SRect {
  48. return new SRect(0, 0, this.width, this.height);
  49. }
  50. /**
  51. * Item 绘制操作
  52. * @param canvas 绘制对象
  53. */
  54. onDraw(canvas: SPainter): void {
  55. canvas.pen.color = SColor.Blue;
  56. canvas.pen.lineWidth = 5;
  57. canvas.brush.color = this.selected ? SColor.Red : SColor.Green;
  58. canvas.drawRect(0, 0, this.width, this.height)
  59. }
  60. }
  61. class CircleItem extends SGraphItem {
  62. /** 半径 */
  63. r = 50;
  64. /** 图 Id */
  65. id = new Date().getTime() + ""
  66. /**
  67. * 构造函数
  68. *
  69. * @param parent Item 图像引擎
  70. */
  71. constructor(parent: SGraphItem | null) {
  72. super(parent);
  73. this.moveable = true;
  74. this.selectable = true;
  75. this.isTransform = false;
  76. }
  77. /**
  78. * 矩形数据类型绘制
  79. */
  80. boundingRect(): SRect {
  81. return new SRect(0, 0, this.r * 2, this.r * 2);
  82. }
  83. /**
  84. * Item 绘制操作
  85. * @param canvas 绘制对象
  86. */
  87. onDraw(canvas: SPainter): void {
  88. canvas.pen.color = SColor.Blue;
  89. canvas.pen.lineWidth = 5;
  90. canvas.brush.color = this.selected ? SColor.Red : SColor.Green;
  91. canvas.drawCircle(this.r, this.r, this.r);
  92. }
  93. }
  94. class SScene extends SGraphScene {
  95. /** 实例化 */
  96. undoStack = new SUndoStack();
  97. /** 定义命令 */
  98. cmd = 0;
  99. /**
  100. * 构造函数
  101. */
  102. constructor() {
  103. super();
  104. }
  105. /**
  106. * 鼠标抬起事件
  107. *
  108. * @param event 事件参数
  109. * @return 是否处理
  110. */
  111. onMouseUp(event: SMouseEvent): boolean {
  112. switch (this.cmd) {
  113. case 1:
  114. this.addCircle(event.x, event.y);
  115. break;
  116. case 2:
  117. this.addRect(event.x, event.y);
  118. break;
  119. default:
  120. super.onMouseUp(event);
  121. }
  122. this.cmd = 0;
  123. return false
  124. }
  125. /**
  126. * 添加圆形
  127. * @param x x轴
  128. * @param y y轴
  129. */
  130. private addCircle(x: number, y: number): void {
  131. let item = new CircleItem(null);
  132. item.moveTo(x - 50, y - 50);
  133. this.addItem(item);
  134. this.undoStack.push(new SGraphAddCommand(this, item));
  135. item.connect("onMove", this, this.onItemMove.bind(this));
  136. }
  137. /**
  138. * 添加矩形
  139. * @param x x轴
  140. * @param y y轴
  141. */
  142. private addRect(x: number, y: number): void {
  143. let item = new RectItem(null);
  144. item.moveTo(x - 50, y - 50);
  145. this.addItem(item);
  146. this.undoStack.push(new SGraphAddCommand(this, item));
  147. item.connect("onMove", this, this.onItemMove.bind(this));
  148. }
  149. /**
  150. * 移动操作
  151. * @param item 图像引擎
  152. * @param arg 数组
  153. */
  154. onItemMove(item: SGraphItem, ...arg: any): void {
  155. this.undoStack.push(new SGraphMoveCommand(this, item, arg[0][0] as SPoint, arg[0][1] as SPoint));
  156. }
  157. }
  158. class TestView extends SGraphView {
  159. /**
  160. * 构造函数
  161. *
  162. * @param undoFrame 绘制引擎
  163. */
  164. constructor(undoFrame) {
  165. super(undoFrame);
  166. }
  167. }
  168. export default {
  169. data() {
  170. return {
  171. /** 命令所属的场景类 */
  172. scene: new SScene(),
  173. }
  174. },
  175. /**
  176. * 页面挂载
  177. */
  178. mounted(): void {
  179. let view = new TestView('undoFrame');
  180. view.scene = this.scene;
  181. },
  182. methods: {
  183. /**
  184. * 定义圆形命令
  185. */
  186. addCircle() {
  187. this.scene.cmd = 1;
  188. },
  189. /**
  190. * 定义矩形命令
  191. */
  192. addRect() {
  193. this.scene.cmd = 2;
  194. },
  195. /**
  196. * 删除操作
  197. */
  198. deleteItem() {
  199. // do something ...
  200. },
  201. /**
  202. * 取消操作
  203. */
  204. undo() {
  205. this.scene.undoStack.undo();
  206. },
  207. /**
  208. * 重做操作
  209. */
  210. redo() {
  211. this.scene.undoStack.redo();
  212. },
  213. /**
  214. * 将命令堆栈转为日志(命令数组)
  215. */
  216. log() {
  217. let str = this.scene.undoStack.toLog();
  218. console.log(str)
  219. }
  220. }
  221. }
  222. </script>
  223. <style scoped>
  224. </style>