index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import mitt from 'mitt'
  2. import {
  3. text,
  4. table,
  5. compass,
  6. title,
  7. bgImage
  8. } from './shape'
  9. import Layer from './editCAD/Layer'
  10. import { history } from './editCAD/History/History.js'
  11. import { uploadFile } from 'api'
  12. // const toStore = (refs) => {
  13. // return {
  14. // bgImage: refs.baseMap?.data || null,
  15. // shapes: refs.shapes.map(shape => shape.data)
  16. // }
  17. // }
  18. export const create = (store, canvas) => {
  19. const refs = {
  20. ctx: canvas.getContext('2d'),
  21. bus: mitt(),
  22. shapes: [],
  23. baseMap: null
  24. }
  25. const layer = new Layer()
  26. console.log(store)
  27. layer.start(canvas, store)
  28. layer.uiControl.bus.on('showAttribute', ({type, value: data}) => {
  29. const shape = {
  30. data: { type },
  31. delete: () => {
  32. layer.uiControl.clearUI()
  33. layer.uiControl.setAttributes(type, 'delete')
  34. }
  35. }
  36. const update = (newData) => {
  37. layer.uiControl.setAttributes(type, 'update', newData)
  38. }
  39. switch (type) {
  40. case table: {
  41. data = data || [
  42. { width: 160, height: 60, value: '', colIndex: 0, rowIndex: 0 },
  43. { width: 160, height: 60, value: '', colIndex: 1, rowIndex: 0 },
  44. ]
  45. shape.data.content = data
  46. shape.setContent = (newData) => {
  47. shape.data.content = newData
  48. update(newData)
  49. }
  50. break;
  51. }
  52. case title:
  53. case text: {
  54. console.log(data)
  55. data = data || ''
  56. shape.data.text = data
  57. shape.setText = (newData) => {
  58. shape.data.text = newData
  59. update(newData)
  60. }
  61. break;
  62. }
  63. case compass: {
  64. data = data || 0
  65. console.log(data)
  66. shape.data.rotate = data
  67. shape.setRotate = (newData) => {
  68. shape.data.rotate = newData
  69. update(newData)
  70. }
  71. }
  72. }
  73. refs.bus.emit('selectShape', shape)
  74. })
  75. layer.uiControl.bus.on('hideAttribute', () => refs.bus.emit('selectShape', null))
  76. history.bus.on('undoAvailable', availabe => refs.bus.emit('backDisabled', !availabe))
  77. history.bus.on('redoAvailable', availabe => refs.bus.emit('forwardDisabled', !availabe))
  78. setTimeout(() => {
  79. // layer.uiControl.bus.emit('showAttribute', { type: compass, data: 0 })
  80. // history.bus.emit('undoAvailable', true)
  81. // history.bus.emit('redoAvailable', true)
  82. }, 100)
  83. const board = {
  84. bus: refs.bus,
  85. el: canvas,
  86. async getStore() {
  87. const data = layer.uiControl.exportJSON()
  88. const floor = data.floors[0]
  89. const shapes = []
  90. if (floor) {
  91. const bgImage = floor.image.src
  92. if (bgImage && bgImage.includes('blob:')) {
  93. const url = await fetch(bgImage)
  94. .then(res => res.blob())
  95. .then(blob => uploadFile(new File([blob], (store.id || 'image') + '.png')))
  96. floor.image.src = url
  97. console.log(url)
  98. }
  99. shapes.push({ type: title, text: floor.title.value })
  100. }
  101. return {
  102. id: store.id,
  103. shapes,
  104. ...layer.uiControl.exportJSON()
  105. }
  106. },
  107. viewInit() {
  108. layer.uiControl.menu_flex()
  109. },
  110. unSelectShape() {
  111. layer.uiControl.clearUI()
  112. },
  113. setDefaultTable(rbTable, rtTable) {
  114. if (rbTable) {
  115. layer.uiControl.initDownTable(rbTable)
  116. }
  117. if (rtTable) {
  118. layer.uiControl.initTopTable(rtTable)
  119. }
  120. },
  121. initHistory() {
  122. history.init()
  123. },
  124. readyAddShape(shapeType, onFine) {
  125. layer.uiControl.selectUI = shapeType
  126. layer.uiControl.updateEventNameForSelectUI()
  127. const finePack = () => {
  128. layer.uiControl.bus.off('hideUI', finePack)
  129. onFine()
  130. }
  131. layer.uiControl.bus.on('hideUI', finePack)
  132. },
  133. back() {
  134. history.handleUndo()
  135. },
  136. forward() {
  137. history.handleRedo()
  138. },
  139. setImage(url) {
  140. layer.uiControl.setAttributes(bgImage, 'update', url)
  141. },
  142. export() {
  143. const $canvas = document.createElement('canvas')
  144. $canvas.width = canvas.width
  145. $canvas.height = canvas.height
  146. const cctx = $canvas.getContext('2d');
  147. cctx.rect(0, 0, $canvas.width, $canvas.height)
  148. cctx.fillStyle = '#fff'
  149. cctx.fill()
  150. cctx.drawImage(canvas, 0, 0, $canvas.width, $canvas.height);
  151. return new Promise(resolve => {
  152. // resolve(layer.uiControl.menu_screenShot())
  153. $canvas.toBlob(resolve, "image/jpeg", 1)
  154. })
  155. },
  156. calcTableShape(data) {
  157. return new Promise(resolve => {
  158. const tableData = {
  159. data: {
  160. type: table,
  161. content: data.map((cols, index) => [
  162. {
  163. rowIndex: index,
  164. colIndex: 0,
  165. width: 0,
  166. height: 0,
  167. value: cols[0],
  168. },
  169. {
  170. rowIndex: index,
  171. colIndex: 1,
  172. width: 0,
  173. height: 0,
  174. value: cols[1],
  175. }
  176. ]).flat()
  177. },
  178. setContent(newData) {
  179. board.bus.emit('selectShape', null)
  180. resolve({type: table, content: newData})
  181. },
  182. autoSet: true
  183. }
  184. board.bus.emit('selectShape', tableData)
  185. })
  186. },
  187. destroy() {
  188. }
  189. }
  190. return board
  191. }
  192. export * from './shape'
  193. export default create