undo.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /**
  50. * undo对象示例
  51. *
  52. * @author 郝洁 <haojie@persagy.com>
  53. */
  54. class SScene extends SGraphScene {
  55. undoStack = new SUndoStack();
  56. editCmd = "";
  57. /**
  58. * 构造函数
  59. */
  60. constructor() {
  61. super();
  62. this.editCmd = "";
  63. this.selectContainer.connect("listChange", this, this.listChange);
  64. }
  65. /**
  66. * 鼠标按下事件
  67. * @param event 鼠标事件
  68. */
  69. onMouseDown(event) {
  70. if (this.editCmd == "createLine") {
  71. this.addLine(event);
  72. this.clearCmdStatus();
  73. } else if (this.editCmd == "createText") {
  74. this.addText(event);
  75. this.clearCmdStatus();
  76. } else if (this.editCmd == "createImage") {
  77. this.addImage(event);
  78. this.clearCmdStatus();
  79. } else if (this.editCmd == "") {
  80. super.onMouseDown(event);
  81. }
  82. }
  83. /**
  84. * 清空命令
  85. */
  86. clearCmdStatus() {
  87. this.editCmd = "";
  88. }
  89. /**
  90. * 新增线
  91. *
  92. * @param event 事件
  93. */
  94. addLine(event) {
  95. const lineItem = new EditLineItem(null, []);
  96. lineItem.status = SItemStatus.Create;
  97. lineItem.selectable = true;
  98. lineItem.x = event.x;
  99. lineItem.y = event.y;
  100. lineItem.connect("finishCreated", this, this.finishCreated);
  101. this.addItem(lineItem);
  102. this.undoStack.push(new SGraphAddCommand(this, lineItem));
  103. this.grabItem = lineItem;
  104. }
  105. /**
  106. * 新增文本
  107. *
  108. * @param event 事件
  109. */
  110. addText(event) {
  111. const textItem = new EditText(null);
  112. textItem.text = "测试文本";
  113. textItem.selectable = true;
  114. textItem.moveable = true;
  115. textItem.x = event.x;
  116. textItem.y = event.y;
  117. this.addItem(textItem);
  118. this.undoStack.push(new SGraphAddCommand(this, textItem));
  119. this.grabItem = textItem;
  120. textItem.connect("finishCreated", this, this.finishCreated);
  121. }
  122. /**
  123. * 新增图片
  124. *
  125. * @param event 事件
  126. */
  127. addImage(event) {
  128. const imageItem = new EditImageItem(null);
  129. imageItem.url =
  130. "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";
  131. imageItem.width = 100;
  132. imageItem.height = 100;
  133. imageItem.selectable = true;
  134. imageItem.moveable = true;
  135. imageItem.x = event.x;
  136. imageItem.y = event.y;
  137. this.addItem(imageItem);
  138. this.undoStack.push(new SGraphAddCommand(this, imageItem));
  139. }
  140. /**
  141. * 完成item绘制
  142. */
  143. finishCreated() {
  144. this.grabItem = null;
  145. }
  146. /**
  147. * 还原操作
  148. */
  149. undo() {
  150. this.undoStack.undo();
  151. this.view.update();
  152. }
  153. /**
  154. * 重做
  155. */
  156. redo() {
  157. this.undoStack.redo();
  158. this.view.update();
  159. }
  160. listChange(list) {
  161. this.emitChoice(list);
  162. }
  163. /**
  164. * 删除
  165. *
  166. * @returns Item 类
  167. */
  168. delete() {
  169. let itemList = this.selectContainer.itemList;
  170. itemList.forEach((element) => {
  171. this.removeItem(element);
  172. });
  173. if (this.view) {
  174. this.view.update();
  175. }
  176. return itemList;
  177. }
  178. }
  179. //注: 开发者引入 SWallItem 包为: import {SWallItem} from "@persagy-web/edit/";
  180. export default {
  181. name: "editLine",
  182. data() {
  183. return {
  184. /** 命令所属的场景类 */
  185. scene: null,
  186. view: null, // 实例化 view
  187. isCreated: false, // 是否创建完成
  188. cmdStatus: "", // 选中状态
  189. };
  190. },
  191. /**
  192. * 页面挂载
  193. */
  194. mounted() {
  195. console.log(SGraphAddCommand);
  196. this.view = new SGraphView("edit_line");
  197. this.scene = new SScene();
  198. this.view.scene = this.scene;
  199. },
  200. methods: {
  201. create(val) {
  202. this.scene.editCmd = val;
  203. },
  204. undo() {
  205. this.scene.undo();
  206. },
  207. redo() {
  208. this.scene.redo();
  209. },
  210. deleteItem() {
  211. this.scene.delete();
  212. },
  213. },
  214. watch: {},
  215. beforeCreate() {
  216. },
  217. };
  218. </script>
  219. <style scoped lang="less">
  220. .edit-line {
  221. width: 100%;
  222. height: 500px;
  223. .content {
  224. display: flex;
  225. justify-content: flex-start;
  226. .left {
  227. margin-right: 20px;
  228. }
  229. .line-property {
  230. width: 300px;
  231. margin-top: 20px;
  232. .always {
  233. width: 100%;
  234. height: 100%;
  235. }
  236. .always-item {
  237. display: flex;
  238. margin-top: 10px;
  239. justify-content: space-between;
  240. }
  241. }
  242. }
  243. .heightLight {
  244. color: #409eff;
  245. border-color: #c6e2ff;
  246. background-color: #ecf5ff;
  247. }
  248. }
  249. </style>