undo.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div class="edit-line">
  3. <div class="btn-list">
  4. <el-button
  5. :class="[cmdStatus=='createLine' ? 'heightLight' : '']"
  6. size="small"
  7. @click="create('createLine')"
  8. >画线
  9. </el-button>
  10. <el-button
  11. :class="[cmdStatus=='createText' ? 'heightLight' : '']"
  12. size="small"
  13. @click="create('createText')"
  14. >文本
  15. </el-button>
  16. <el-button
  17. :class="[cmdStatus=='createImage' ? 'heightLight' : '']"
  18. size="small"
  19. @click="create('createImage')"
  20. >图片
  21. </el-button>
  22. <el-button size="small" @click="undo">undo</el-button>
  23. <el-button size="small" @click="redo">redo</el-button>
  24. <!-- <el-tooltip class="item" effect="dark" content="双击 item 也可进入编辑状态" placement="top-start"> -->
  25. <!-- <el-button :class="[cmdStatus=='edit' ? 'heightLight' : '']" size="small" @click="edit">编辑</el-button> -->
  26. <!-- </el-tooltip> -->
  27. <el-button
  28. :class="[cmdStatus=='deleteItem' ? 'heightLight' : '']"
  29. size="small"
  30. @click="deleteItem"
  31. >删除
  32. </el-button>
  33. </div>
  34. <div class="content">
  35. <div class="left">
  36. <canvas id="edit_line" width="700" height="460" tabindex="0"/>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import { SGraphScene, SGraphView } from "@persagy-web/graph/";
  43. import { SItemStatus } from "@persagy-web/big/lib/enums/SItemStatus";
  44. import { SUndoStack } from "@persagy-web/base/lib";
  45. import { EditLineItem } from "./../../../../guides/edit/items/src/EditLineItem";
  46. import { EditImageItem } from "./../../../../guides/edit/items/src/EditImageItem";
  47. import { EditText } from "./../../../../guides/edit/items/src/EditText";
  48. import { SGraphAddCommand } from "./../../../../guides/edit/undo/src/SGraphAddCommand";
  49. class SScene extends SGraphScene {
  50. undoStack = new SUndoStack();
  51. editCmd = "";
  52. /**
  53. * 构造函数
  54. */
  55. constructor() {
  56. super();
  57. this.editCmd = "";
  58. this.selectContainer.connect("listChange", this, this.listChange);
  59. }
  60. /**
  61. * 鼠标按下事件
  62. * @param event 鼠标事件
  63. */
  64. onMouseDown(event) {
  65. if (this.editCmd == "createLine") {
  66. this.addLine(event);
  67. this.clearCmdStatus();
  68. } else if (this.editCmd == "createText") {
  69. this.addText(event);
  70. this.clearCmdStatus();
  71. } else if (this.editCmd == "createImage") {
  72. this.addImage(event);
  73. this.clearCmdStatus();
  74. } else if (this.editCmd == "") {
  75. super.onMouseDown(event);
  76. }
  77. }
  78. /**
  79. * 清空命令
  80. */
  81. clearCmdStatus() {
  82. this.editCmd = "";
  83. }
  84. /**
  85. * 新增线
  86. *
  87. * @param event 事件
  88. */
  89. addLine(event) {
  90. const lineItem = new EditLineItem(null, []);
  91. lineItem.status = SItemStatus.Create;
  92. lineItem.selectable = true;
  93. lineItem.x = event.x;
  94. lineItem.y = event.y;
  95. lineItem.connect("finishCreated", this, this.finishCreated);
  96. this.addItem(lineItem);
  97. this.undoStack.push(new SGraphAddCommand(this, lineItem));
  98. this.grabItem = lineItem;
  99. }
  100. /**
  101. * 新增文本
  102. *
  103. * @param event 事件
  104. */
  105. addText(event) {
  106. const textItem = new EditText(null);
  107. textItem.text = "测试文本";
  108. textItem.selectable = true;
  109. textItem.moveable = true;
  110. textItem.x = event.x;
  111. textItem.y = event.y;
  112. this.addItem(textItem);
  113. this.undoStack.push(new SGraphAddCommand(this, textItem));
  114. this.grabItem = textItem;
  115. textItem.connect("finishCreated", this, this.finishCreated);
  116. }
  117. /**
  118. * 新增图片
  119. *
  120. * @param event 事件
  121. */
  122. addImage(event) {
  123. const imageItem = new EditImageItem(null);
  124. imageItem.url =
  125. "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591685374103&di=cd5a56b2e3f5e12d7145b967afbcfb7a&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201408%2F05%2F20140805191039_cuTPn.jpeg";
  126. imageItem.width = 100;
  127. imageItem.height = 100;
  128. imageItem.selectable = true;
  129. imageItem.moveable = true;
  130. imageItem.x = event.x;
  131. imageItem.y = event.y;
  132. this.addItem(imageItem);
  133. this.undoStack.push(new SGraphAddCommand(this, imageItem));
  134. }
  135. /**
  136. * 完成item绘制
  137. */
  138. finishCreated() {
  139. this.grabItem = null;
  140. }
  141. /**
  142. * 还原操作
  143. */
  144. undo() {
  145. this.undoStack.undo();
  146. this.view.update();
  147. }
  148. /**
  149. * 重做
  150. */
  151. redo() {
  152. this.undoStack.redo();
  153. this.view.update();
  154. }
  155. listChange(list) {
  156. this.emitChoice(list);
  157. }
  158. /**
  159. * 删除
  160. *
  161. * @returns Item 类
  162. */
  163. delete() {
  164. let itemList = this.selectContainer.itemList;
  165. itemList.forEach((element) => {
  166. this.removeItem(element);
  167. });
  168. if (this.view) {
  169. this.view.update();
  170. }
  171. return itemList;
  172. }
  173. }
  174. //注: 开发者引入 SWallItem 包为: import {SWallItem} from "@persagy-web/edit/";
  175. export default {
  176. name: "editLine",
  177. data() {
  178. return {
  179. scene: null,
  180. view: null,
  181. isCreated: false, //是否创建完成
  182. cmdStatus: "", //选中状态
  183. };
  184. },
  185. mounted() {
  186. console.log(SGraphAddCommand);
  187. this.view = new SGraphView("edit_line");
  188. this.scene = new SScene();
  189. this.view.scene = this.scene;
  190. },
  191. methods: {
  192. create(val) {
  193. this.scene.editCmd = val;
  194. },
  195. undo() {
  196. this.scene.undo();
  197. },
  198. redo() {
  199. this.scene.redo();
  200. },
  201. deleteItem() {
  202. this.scene.delete();
  203. },
  204. },
  205. watch: {},
  206. beforeCreate() {
  207. },
  208. };
  209. </script>
  210. <style scoped lang="less">
  211. .edit-line {
  212. width: 100%;
  213. height: 500px;
  214. .content {
  215. display: flex;
  216. justify-content: flex-start;
  217. .left {
  218. margin-right: 20px;
  219. }
  220. .line-property {
  221. width: 300px;
  222. margin-top: 20px;
  223. .always {
  224. width: 100%;
  225. height: 100%;
  226. }
  227. .always-item {
  228. display: flex;
  229. margin-top: 10px;
  230. justify-content: space-between;
  231. }
  232. }
  233. }
  234. .heightLight {
  235. color: #409eff;
  236. border-color: #c6e2ff;
  237. background-color: #ecf5ff;
  238. }
  239. }
  240. </style>