123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import mitt from 'mitt'
- import {
- text,
- table,
- compass
- } from './shape'
- import Layer from './editCAD/Layer'
- import { history } from './editCAD/History/History.js'
- const toStore = (refs) => {
- return {
- bgImage: refs.baseMap?.data || null,
- shapes: refs.shapes.map(shape => shape.data)
- }
- }
- export const create = (store, canvas) => {
- const refs = {
- ctx: canvas.getContext('2d'),
- bus: mitt(),
- shapes: [],
- baseMap: null
- }
- const layer = new Layer()
- layer.start(canvas, store)
- layer.uiControl.bus.on('showAttribute', ({type, value: data}) => {
- const shape = {
- data: { type },
- delete: () => {
- layer.uiControl.clearUI()
- layer.uiControl.setAttributes(type, 'delete')
- }
- }
- const update = (newData) => {
- layer.uiControl.setAttributes(type, 'update', newData)
- }
- switch (type) {
- case table: {
- data = data || [
- { width: 160, height: 60, value: '', colIndex: 0, rowIndex: 0 },
- { width: 160, height: 60, value: '', colIndex: 1, rowIndex: 0 },
- ]
- shape.data.content = data
- shape.setContent = (newData) => {
- shape.data.content = newData
- update(newData)
- }
- break;
- }
- case text: {
- data = data || ''
- shape.data.text = data
- shape.setText = (newData) => {
- shape.data.text = newData
- update(newData)
- }
- break;
- }
- case compass: {
- data = data || 0
- shape.data.rotate = 0
- shape.setRotate = (newData) => {
- console.log(newData)
- shape.data.rotate = newData
- update(newData)
- }
- }
- }
- console.log(shape)
- refs.bus.emit('selectShape', shape)
- })
- layer.uiControl.bus.on('hideAttribute', () => refs.bus.emit('selectShape', null))
- history.bus.on('undoAvailable', availabe => refs.bus.emit('backDisabled', !availabe))
- history.bus.on('redoAvailable', availabe => refs.bus.emit('forwardDisabled', !availabe))
- setTimeout(() => {
- // layer.uiControl.bus.emit('showAttribute', { type: compass, data: 0 })
- // history.bus.emit('undoAvailable', true)
- // history.bus.emit('redoAvailable', true)
- }, 100)
- const board = {
- bus: refs.bus,
- el: canvas,
- getCurrentStore:() => ({ ...store, ...toStore(refs) }),
- drawStore(newStore) {
- refs.ctx.clearRect(0,0, canvas.width, canvas.height)
- refs.shapes = []
- refs.baseMap = null
- generateRefs(newStore)
- },
- getStore() {
- return store
- },
- unSelectShape() {
- layer.uiControl.clearUI()
- },
- readyAddShape(shapeType, onFine) {
- layer.uiControl.selectUI = shapeType
- layer.uiControl.updateEventNameForSelectUI()
- const finePack = () => {
- layer.uiControl.bus.off('hideUI', finePack)
- onFine()
- }
- layer.uiControl.bus.on('hideUI', finePack)
- },
- back() {
- history.handleUndo()
- },
- forward() {
- history.handleRedo()
- },
- setImage(url) {
- refs.baseMap.changeImage(url)
- refs.bus.emit('storeChange')
- },
- export() {
- return new Promise(resolve => canvas.toBlob(resolve))
- },
- destroy() {
- }
- }
- return board
- }
- export * from './shape'
- export default create
|