TextItem.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. scene: new SScene(),
  92. text: "测试文本",
  93. size: 20,
  94. color: "#333333"
  95. };
  96. },
  97. mounted() {
  98. let view = new TestView();
  99. this.scene.updateText(this.text);
  100. this.scene.updateColor(this.color);
  101. this.scene.updateSize(this.size);
  102. view.scene = this.scene;
  103. },
  104. methods: {
  105. handleChangeText(text) {
  106. this.scene.updateText(text);
  107. },
  108. handleChangeColor(color) {
  109. this.scene.updateColor(color);
  110. },
  111. handleChangeSize(size) {
  112. this.scene.updateSize(size);
  113. },
  114. undo() {
  115. this.scene.undoStack.undo();
  116. },
  117. redo() {
  118. this.scene.undoStack.redo();
  119. },
  120. reset() {
  121. this.text = "测试文本";
  122. this.size = 20;
  123. this.color = "#333333";
  124. this.scene.updateText(this.text);
  125. this.scene.updateColor(this.color);
  126. this.scene.updateSize(this.size);
  127. }
  128. }
  129. };
  130. </script>
  131. <style scoped>
  132. </style>