edittext.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <div class="edit-line">
  3. <!-- 所有按钮 -->
  4. <div class="btn-list">
  5. </div>
  6. <div class="content">
  7. <div class="left" id="left">
  8. <!-- <textarea name="" class="text" cols="30" rows="10"></textarea> -->
  9. <canvas id="edit_polygon" width="700" height="460" tabindex="0"/>
  10. </div>
  11. <div class="line-property">
  12. <el-card shadow="always">
  13. <div slot="header" class="clearfix">
  14. <span>属性修改</span>
  15. </div>
  16. <div class="always-item">
  17. <span>字号:</span>
  18. <el-input-number size="small" v-model="size" @change="updateSize" :min="1"
  19. :max="50"></el-input-number>
  20. </div>
  21. <div class="always-item">
  22. <span>文本框:</span>
  23. <el-input
  24. v-model="text"
  25. @change="handleChangeText"
  26. type="textarea"
  27. :autosize="{ minRows: 1, maxRows: 1}"
  28. placeholder="请输入内容"
  29. style="width:200px;"
  30. ></el-input>
  31. </div>
  32. <div class="always-item">
  33. <span>线颜色:</span>
  34. <el-color-picker v-model="color" @change="changeColor" show-alpha></el-color-picker>
  35. </div>
  36. </el-card>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import { SGraphMoveCommand, SGraphPropertyCommand, SGraphScene, SGraphView } from "@persagy-web/graph/";
  43. import { SUndoStack } from "@persagy-web/base/lib";
  44. import { SFont } from "@persagy-web/draw/lib";
  45. //注: 开发者引入 SImageItem 包为: import {SImageItem} from "@persagy-web/edit/";
  46. import { EditText } from "./../../../../../guides/edit/items/src/EditText";
  47. import { hexify } from "./../../../../public/until/rgbaUtil";
  48. class SScene extends SGraphScene {
  49. undoStack = new SUndoStack();
  50. textItem = new EditText(null);
  51. /**
  52. * 构造函数
  53. */
  54. constructor() {
  55. super();
  56. this.textItem.moveable = true;
  57. this.textItem.x = 100;
  58. this.textItem.y = 100;
  59. this.addItem(this.textItem);
  60. this.textItem.connect("onMove", this, this.onItemMove.bind(this));
  61. }
  62. updateText(str) {
  63. if (this.textItem.text !== str) {
  64. let old = this.textItem.text;
  65. this.textItem.text = str;
  66. this.undoStack.push(
  67. new SGraphPropertyCommand(this, this.textItem, "text", old, str)
  68. );
  69. }
  70. }
  71. updateColor(color) {
  72. if (this.textItem.color !== color) {
  73. let old = this.textItem.color;
  74. this.textItem.color = color;
  75. this.undoStack.push(
  76. new SGraphPropertyCommand(this, this.textItem, "color", old, color)
  77. );
  78. }
  79. }
  80. updateSize(size) {
  81. if (this.textItem.font.size !== size) {
  82. let old = new SFont(this.textItem.font);
  83. let font = new SFont(this.textItem.font);
  84. font.size = size;
  85. this.textItem.font = font;
  86. this.undoStack.push(
  87. new SGraphPropertyCommand(this, this.textItem, "font", old, font)
  88. );
  89. }
  90. }
  91. onItemMove(item, ...arg) {
  92. this.undoStack.push(
  93. new SGraphMoveCommand(this, item, arg[0][0], arg[0][1])
  94. );
  95. }
  96. }
  97. class TestView extends SGraphView {
  98. /**
  99. * 构造函数
  100. */
  101. constructor() {
  102. super("edit_polygon");
  103. }
  104. }
  105. export default {
  106. name: "edittext",
  107. data() {
  108. return {
  109. scene: new SScene(),
  110. text: "测试文本",
  111. size: 20,
  112. color: "#333333"
  113. };
  114. },
  115. mounted() {
  116. let view = new TestView();
  117. this.scene.updateText(this.text);
  118. this.scene.updateColor(this.color);
  119. this.scene.updateSize(this.size);
  120. view.scene = this.scene;
  121. // this.scene.onMouseUp = this.onMouseUp;
  122. },
  123. methods: {
  124. handleChangeText(text) {
  125. this.scene.updateText(text);
  126. },
  127. handleChangeColor(color) {
  128. this.scene.updateColor(color);
  129. },
  130. updateSize(size) {
  131. this.scene.updateSize(size);
  132. },
  133. undo() {
  134. this.scene.undoStack.undo();
  135. },
  136. redo() {
  137. this.scene.undoStack.redo();
  138. },
  139. reset() {
  140. this.text = "测试文本";
  141. this.size = 20;
  142. this.color = "#333333";
  143. this.scene.updateText(this.text);
  144. this.scene.updateColor(this.color);
  145. this.scene.updateSize(this.size);
  146. },
  147. // 改变颜色
  148. changeColor(val) {
  149. this.scene.updateColor(hexify(val));
  150. },
  151. ///////////////////////////////////
  152. //// 以下为测试代码,请忽略
  153. // 设置tooltip位置
  154. /**
  155. * raduis 灵敏范围
  156. * e 鼠标事件对象
  157. * tipDom 浮动得dom
  158. * boxDom 最外层盒子
  159. * offset 偏移量
  160. */
  161. toolTipPosition(radius, e, tipDom, offset = 0) {
  162. // 滚动高度
  163. const screenTop =
  164. document.documentElement.scrollTop || document.body.scrollTop;
  165. const screenLeft =
  166. document.documentElement.scrollLeft || document.body.scrollLeft;
  167. radius = radius + offset;
  168. const mapBox = document.getElementById("edit_polygon");
  169. // 测试边界mousePosition = 1(右下) 2 (右上)3 (左上) 4 (左下)
  170. const mousePosition = this.Boxboundary(radius, e, mapBox);
  171. const absPosition = this.offsetTL(mapBox);
  172. const absPositionLeft = absPosition.left;
  173. const absPositionTop = absPosition.top;
  174. const mClientY = e.clientY + screenTop;
  175. const mClientX = e.clientX + screenLeft;
  176. const fuzzy_model_width = tipDom.offsetWidth;
  177. const fuzzy_model_height = tipDom.offsetHeight;
  178. // let offsetTL
  179. if (mousePosition == 2) {
  180. tipDom.style.left = `${mClientX - absPositionLeft + offset}px`;
  181. tipDom.style.top = `${mClientY -
  182. fuzzy_model_height -
  183. absPositionTop -
  184. offset}px`;
  185. } else if (mousePosition == 3) {
  186. tipDom.style.left = `${mClientX -
  187. fuzzy_model_width -
  188. absPositionLeft -
  189. offset}px`;
  190. tipDom.style.top = `${mClientY -
  191. fuzzy_model_height -
  192. absPositionTop -
  193. offset}px`;
  194. } else if (mousePosition == 4) {
  195. tipDom.style.left = `${mClientX -
  196. fuzzy_model_width -
  197. absPositionLeft -
  198. offset}px`;
  199. tipDom.style.top = `${mClientY - absPositionTop + offset}px`;
  200. } else {
  201. tipDom.style.left = `${mClientX - absPositionLeft + offset}px`;
  202. tipDom.style.top = `${mClientY - absPositionTop + offset}px`;
  203. }
  204. },
  205. Boxboundary(radius, event, box) {
  206. const screenTop =
  207. document.documentElement.scrollTop || document.body.scrollTop;
  208. const boxWidth = Number(box.style.width.slice(0, -2));
  209. const boxHeight = Number(box.style.height.slice(0, -2));
  210. const absPosition = this.offsetTL(box);
  211. const boxClientX = absPosition.left;
  212. const boxClientY = absPosition.top;
  213. const mouseX = event.clientX;
  214. const mouseY = event.clientY + screenTop;
  215. if (mouseY >= boxClientY + boxHeight - radius) {
  216. if (mouseX <= radius + boxClientX) {
  217. return 2;
  218. } else if (mouseX >= boxClientX + boxWidth - radius) {
  219. return 3;
  220. } else {
  221. return 2;
  222. }
  223. } else if (mouseX >= boxClientX + boxWidth - radius) {
  224. return 4;
  225. } else {
  226. return 1;
  227. }
  228. },
  229. offsetTL(obj) {
  230. //获取到body的offsetTop和offsetLeft
  231. var t = 0,
  232. l = 0;
  233. while (obj) {
  234. t = t + obj.offsetTop;
  235. l = l + obj.offsetLeft;
  236. obj = obj.offsetParent;
  237. }
  238. return {
  239. top: t,
  240. left: l
  241. };
  242. },
  243. onMouseUp(event) {
  244. const dom = "<textarea autofocus ref='textarea' class='text'></textarea>";
  245. let parentDom = document.getElementById("left");
  246. parentDom.innerHTML = parentDom.innerHTML + dom;
  247. const textareaDom = this.$refs.textarea;
  248. // console.log('textareaDom',parentDom.children[0])
  249. this.toolTipPosition(1, event, textareaDom, 0);
  250. }
  251. },
  252. watch: {
  253. // cmdStatus(val) {
  254. // this.scene.cmdStatus = val;
  255. // }
  256. }
  257. };
  258. </script>
  259. <style scoped lang="less">
  260. .text {
  261. overflow: auto;
  262. word-break: break-all;
  263. outline: none;
  264. // background: #409eff;
  265. position: absolute;
  266. left: 0;
  267. top: 0;
  268. }
  269. .edit-line {
  270. width: 100%;
  271. height: 500px;
  272. position: relative;
  273. .content {
  274. display: flex;
  275. justify-content: flex-start;
  276. .left {
  277. margin-right: 20px;
  278. }
  279. .line-property {
  280. width: 300px;
  281. margin-top: 20px;
  282. .always {
  283. width: 100%;
  284. height: 100%;
  285. }
  286. .always-item {
  287. display: flex;
  288. margin-top: 10px;
  289. justify-content: space-between;
  290. }
  291. }
  292. }
  293. .heightLight {
  294. color: #409eff;
  295. border-color: #c6e2ff;
  296. background-color: #ecf5ff;
  297. }
  298. }
  299. </style>