index.js 5.2 KB

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