index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import mitt from 'mitt'
  2. import {
  3. text,
  4. table,
  5. compass
  6. } from './shape'
  7. import Layer from './editCAD/Layer'
  8. import { history } from './editCAD/History/History.js'
  9. const toStore = (refs) => {
  10. return {
  11. bgImage: refs.baseMap?.data || null,
  12. shapes: refs.shapes.map(shape => shape.data)
  13. }
  14. }
  15. export const create = (store, canvas) => {
  16. const refs = {
  17. ctx: canvas.getContext('2d'),
  18. bus: mitt(),
  19. shapes: [],
  20. baseMap: null
  21. }
  22. const layer = new Layer()
  23. layer.start(canvas, store)
  24. layer.uiControl.bus.on('showAttribute', ({type, value: data}) => {
  25. const shape = {
  26. data: { type },
  27. delete: () => {
  28. layer.uiControl.clearUI()
  29. layer.uiControl.setAttributes(type, 'delete')
  30. }
  31. }
  32. const update = (newData) => {
  33. layer.uiControl.setAttributes(type, 'update', newData)
  34. }
  35. switch (type) {
  36. case table: {
  37. data = data || [
  38. { width: 160, height: 60, value: '', colIndex: 0, rowIndex: 0 },
  39. { width: 160, height: 60, value: '', colIndex: 1, rowIndex: 0 },
  40. ]
  41. shape.data.content = data
  42. shape.setContent = (newData) => {
  43. shape.data.content = newData
  44. update(newData)
  45. }
  46. break;
  47. }
  48. case text: {
  49. data = data || ''
  50. shape.data.text = data
  51. shape.setText = (newData) => {
  52. shape.data.text = newData
  53. update(newData)
  54. }
  55. break;
  56. }
  57. case compass: {
  58. data = data || 0
  59. shape.data.rotate = 0
  60. shape.setRotate = (newData) => {
  61. console.log(newData)
  62. shape.data.rotate = newData
  63. update(newData)
  64. }
  65. }
  66. }
  67. console.log(shape)
  68. refs.bus.emit('selectShape', shape)
  69. })
  70. layer.uiControl.bus.on('hideAttribute', () => refs.bus.emit('selectShape', null))
  71. history.bus.on('undoAvailable', availabe => refs.bus.emit('backDisabled', !availabe))
  72. history.bus.on('redoAvailable', availabe => refs.bus.emit('forwardDisabled', !availabe))
  73. setTimeout(() => {
  74. // layer.uiControl.bus.emit('showAttribute', { type: compass, data: 0 })
  75. // history.bus.emit('undoAvailable', true)
  76. // history.bus.emit('redoAvailable', true)
  77. }, 100)
  78. const board = {
  79. bus: refs.bus,
  80. el: canvas,
  81. getCurrentStore:() => ({ ...store, ...toStore(refs) }),
  82. drawStore(newStore) {
  83. refs.ctx.clearRect(0,0, canvas.width, canvas.height)
  84. refs.shapes = []
  85. refs.baseMap = null
  86. generateRefs(newStore)
  87. },
  88. getStore() {
  89. return store
  90. },
  91. unSelectShape() {
  92. layer.uiControl.clearUI()
  93. },
  94. readyAddShape(shapeType, onFine) {
  95. layer.uiControl.selectUI = shapeType
  96. layer.uiControl.updateEventNameForSelectUI()
  97. const finePack = () => {
  98. layer.uiControl.bus.off('hideUI', finePack)
  99. onFine()
  100. }
  101. layer.uiControl.bus.on('hideUI', finePack)
  102. },
  103. back() {
  104. history.handleUndo()
  105. },
  106. forward() {
  107. history.handleRedo()
  108. },
  109. setImage(url) {
  110. refs.baseMap.changeImage(url)
  111. refs.bus.emit('storeChange')
  112. },
  113. export() {
  114. return new Promise(resolve => canvas.toBlob(resolve))
  115. },
  116. destroy() {
  117. }
  118. }
  119. return board
  120. }
  121. export * from './shape'
  122. export default create