TextItem.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div>
  3. <el-row>
  4. <el-input
  5. v-model="text"
  6. @change="handleChangeText"
  7. type="textarea"
  8. :autosize="{ minRows: 1, maxRows: 1}"
  9. placeholder="请输入内容"
  10. style="width:200px;"
  11. ></el-input>
  12. <el-input-number v-model="size" @change="handleChangeSize" :min="1" label="字号"></el-input-number>
  13. <el-color-picker v-model="color" @change="handleChangeColor" style="top: 15px;"></el-color-picker>
  14. <el-button @click="undo">Undo</el-button>
  15. <el-button @click="redo">Redo</el-button>
  16. <el-button @click="reset">Reset</el-button>
  17. </el-row>
  18. <canvas id="TextItem1" width="740" height="400" tabindex="0"/>
  19. </div>
  20. </template>
  21. <script>
  22. import { SUndoStack } from "@persagy-web/base/lib";
  23. import { SFont } from "@persagy-web/draw/lib";
  24. import { SGraphMoveCommand, SGraphPropertyCommand, SGraphScene, SGraphView, STextItem } from "@persagy-web/graph/lib";
  25. /**
  26. * 文本实例
  27. *
  28. * @author 郝洁 <haojie@persagy.com>
  29. */
  30. class SScene extends SGraphScene {
  31. undoStack = new SUndoStack();
  32. textItem = new STextItem(null);
  33. /**
  34. * 构造函数
  35. */
  36. constructor() {
  37. super();
  38. this.textItem.moveable = true;
  39. this.textItem.x = 100;
  40. this.textItem.y = 100;
  41. this.textItem.selectable = true
  42. this.addItem(this.textItem);
  43. this.textItem.connect("onMove", this, this.onItemMove.bind(this));
  44. }
  45. updateText(str) {
  46. if (this.textItem.text !== str) {
  47. let old = this.textItem.text;
  48. this.textItem.text = str;
  49. this.undoStack.push(
  50. new SGraphPropertyCommand(this, this.textItem, "text", old, str)
  51. );
  52. }
  53. }
  54. updateColor(color) {
  55. if (this.textItem.color !== color) {
  56. let old = this.textItem.color;
  57. this.textItem.color = color;
  58. this.undoStack.push(
  59. new SGraphPropertyCommand(this, this.textItem, "color", old, color)
  60. );
  61. }
  62. }
  63. updateSize(size) {
  64. if (this.textItem.font.size !== size) {
  65. let old = new SFont(this.textItem.font);
  66. let font = new SFont(this.textItem.font);
  67. font.size = size;
  68. this.textItem.font = font;
  69. this.undoStack.push(
  70. new SGraphPropertyCommand(this, this.textItem, "font", old, font)
  71. );
  72. }
  73. }
  74. onItemMove(item, ...arg) {
  75. this.undoStack.push(
  76. new SGraphMoveCommand(this, item, arg[0][0], arg[0][1])
  77. );
  78. }
  79. }
  80. class TestView extends SGraphView {
  81. /**
  82. * 构造函数
  83. */
  84. constructor() {
  85. super("TextItem1");
  86. }
  87. }
  88. export default {
  89. data() {
  90. return {
  91. /** 命令所属的场景类 */
  92. scene: new SScene(),
  93. text: "测试文本",
  94. size: 20,
  95. color: "#333333"
  96. };
  97. },
  98. /**
  99. * 页面挂载
  100. */
  101. mounted() {
  102. let view = new TestView();
  103. this.scene.updateText(this.text);
  104. this.scene.updateColor(this.color);
  105. this.scene.updateSize(this.size);
  106. view.scene = this.scene;
  107. },
  108. methods: {
  109. handleChangeText(text) {
  110. this.scene.updateText(text);
  111. },
  112. handleChangeColor(color) {
  113. this.scene.updateColor(color);
  114. },
  115. handleChangeSize(size) {
  116. this.scene.updateSize(size);
  117. },
  118. undo() {
  119. this.scene.undoStack.undo();
  120. },
  121. redo() {
  122. this.scene.undoStack.redo();
  123. },
  124. reset() {
  125. this.text = "测试文本";
  126. this.size = 20;
  127. this.color = "#333333";
  128. this.scene.updateText(this.text);
  129. this.scene.updateColor(this.color);
  130. this.scene.updateSize(this.size);
  131. }
  132. }
  133. };
  134. </script>
  135. <style scoped>
  136. </style>