Skip to content

Commit

Permalink
refactor: ♻️ 左侧栏重构
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu-Jun committed Nov 23, 2024
1 parent 4e95cc8 commit 79a40f9
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 125 deletions.
8 changes: 4 additions & 4 deletions src/components/ModalSize.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<el-dialog v-model="modal" :title="props.title" footer-hide>
<el-dialog v-model="show" destroy-on-close :title="props.title" footer-hide>
<h3>
{{ $t('editor.importFiles.createDesign.customSize') }}
</h3>
Expand Down Expand Up @@ -67,7 +67,7 @@ const props = defineProps({
default: ''
}
})
const modal = ref(false)
const show = ref(false)
const width = ref(null)
const height = ref(null)
const sizeList = ref([])
Expand All @@ -78,7 +78,7 @@ const showSetSize = (w, h) => {
editorStore.editor.getSizeList().then((res) => {
sizeList.value = res
})
modal.value = true
show.value = true
}
const setSize = (itemString) => {
const [w, h] = itemString.split('x')
Expand All @@ -89,7 +89,7 @@ const setSize = (itemString) => {
const customSizeCreate = async () => {
if (width.value && height.value) {
emit('set', width.value, height.value)
modal.value = false
show.value = false
} else {
ElMessage.warning('请检查尺寸')
}
Expand Down
37 changes: 31 additions & 6 deletions src/constants/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { editorTabs, texts, shapes, codes, files } from '@/enums/editor'
import {
editorTabs,
texts,
shapes,
codes,
files,
DrawTypes
} from '@/enums/editor'

export const tabList = [
{
Expand All @@ -11,11 +18,6 @@ export const tabList = [
icon: 'template',
type: editorTabs.template
},
{
name: 'editor.tab.element',
icon: 'element',
type: editorTabs.element
},
{
name: 'editor.tab.text',
icon: 'text',
Expand Down Expand Up @@ -163,3 +165,26 @@ export const filesList = [
type: files.json
}
]

export const drawList = [
{
name: 'NotImportant',
icon: 'tool-draw1',
type: DrawTypes.line
},
{
name: 'NotImportant',
icon: 'tool-draw2',
type: DrawTypes.arrow
},
{
name: 'NotImportant',
icon: 'tool-draw3',
type: DrawTypes.thinTailArrow
},
{
name: 'NotImportant',
icon: 'editor-brush',
type: DrawTypes.freeDraw
}
]
11 changes: 9 additions & 2 deletions src/enums/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
* @Author: June
* @Description:
* @Date: 2024-09-12 09:43:14
* @LastEditTime: 2024-09-21 09:03:36
* @LastEditTime: 2024-11-23 16:53:30
* @LastEditors: June
* @FilePath: \element-fabric-editor\src\enums\editor.ts
*/
export enum editorTabs {
create = 'CGRATE',
template = 'TEMPLATE',
element = 'ELEMENT',
text = 'TEXT',
material = 'Material',
ai = 'AI',
Expand Down Expand Up @@ -49,3 +48,11 @@ export enum files {
svg = 'SVG',
psd = 'PSD'
}

export enum DrawTypes {
line = 'Line',
arrow = 'Arrow',
thinTailArrow = 'ThinTailArrow',
polygon = 'Polygon',
freeDraw = 'FreeDraw'
}
194 changes: 104 additions & 90 deletions src/lib/core/plugin/DrawLinePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,151 +1,165 @@
/*
* @Author: 秦少卫
* @Date: 2023-06-21 22:09:36
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 17:33:05
* @LastEditors: June
* @LastEditTime: 2024-11-23 16:42:14
* @Description: file content
*/

import { v4 as uuid } from 'uuid';
import { fabric } from 'fabric';
import Arrow from '../objects/Arrow';
import ThinTailArrow from '../objects/ThinTailArrow';
import Editor from '../Editor';
type IEditor = Editor;
import { v4 as uuid } from 'uuid'
import { fabric } from 'fabric'
import Arrow from '../objects/Arrow'
import ThinTailArrow from '../objects/ThinTailArrow'
import Editor from '../Editor'
import { DrawTypes } from '@/enums/editor'
type IEditor = Editor

class DrawLinePlugin implements IPluginTempl {
static pluginName = 'DrawLinePlugin';
static apis = ['setLineType', 'setMode'];
isDrawingLineMode: boolean;
lineType: string;
lineToDraw: any;
pointer: any;
pointerPoints: any;
isDrawingLine: boolean;
constructor(public canvas: fabric.Canvas, public editor: IEditor) {
this.isDrawingLine = false;
this.isDrawingLineMode = false;
this.lineType = '';
this.lineToDraw = null;
this.pointer = null;
this.pointerPoints = null;
this.init();
static pluginName = 'DrawLinePlugin'
static apis = ['setLineType', 'setMode']
isDrawingLineMode: boolean
lineType: DrawTypes | ''
lineToDraw: any
pointer: any
pointerPoints: any
isDrawingLine: boolean
constructor(
public canvas: fabric.Canvas,
public editor: IEditor
) {
this.isDrawingLine = false
this.isDrawingLineMode = false
this.lineType = ''
this.lineToDraw = null
this.pointer = null
this.pointerPoints = null
this.init()
}

init() {
const { canvas } = this;
const { canvas } = this
canvas.on('mouse:down', (o) => {
if (!this.isDrawingLineMode) return;
canvas.discardActiveObject();
if (!this.isDrawingLineMode) return
canvas.discardActiveObject()
canvas.getObjects().forEach((obj) => {
obj.selectable = false;
obj.hasControls = false;
});
canvas.requestRenderAll();
this.isDrawingLine = true;
this.pointer = canvas.getPointer(o.e);
this.pointerPoints = [this.pointer.x, this.pointer.y, this.pointer.x, this.pointer.y];
let NodeHandler;
obj.selectable = false
obj.hasControls = false
})
canvas.requestRenderAll()
this.isDrawingLine = true
this.pointer = canvas.getPointer(o.e)
this.pointerPoints = [
this.pointer.x,
this.pointer.y,
this.pointer.x,
this.pointer.y
]
let NodeHandler
let opts: any = {
strokeWidth: 2,
stroke: '#000000',
id: uuid(),
};
id: uuid()
}
switch (this.lineType) {
case 'line':
NodeHandler = fabric.Line;
break;
case 'arrow':
NodeHandler = Arrow;
break;
case 'thinTailArrow':
NodeHandler = ThinTailArrow;
case DrawTypes.line:
NodeHandler = fabric.Line
break
case DrawTypes.arrow:
NodeHandler = Arrow
break
case DrawTypes.thinTailArrow:
NodeHandler = ThinTailArrow
opts = {
strokeWidth: 2,
stroke: '#ff0000',
fill: '#ff0000',
id: uuid(),
};
break;
id: uuid()
}
break
default:
break;
break
}
if (!NodeHandler) throw new Error('Draw failed: invalid lineType.');
if (!NodeHandler) throw new Error('Draw failed: invalid lineType.')

this.lineToDraw = new NodeHandler(this.pointerPoints, opts);
this.lineToDraw = new NodeHandler(this.pointerPoints, opts)

this.lineToDraw.selectable = false;
this.lineToDraw.evented = false;
this.lineToDraw.strokeUniform = true;
canvas.add(this.lineToDraw);
});
this.lineToDraw.selectable = false
this.lineToDraw.evented = false
this.lineToDraw.strokeUniform = true
canvas.add(this.lineToDraw)
})

canvas.on('mouse:move', (o) => {
if (!this.isDrawingLine || !['line', 'arrow', 'thinTailArrow'].includes(this.lineType))
return;
canvas.discardActiveObject();
const activeObject = canvas.getActiveObject();
if (activeObject) return;
this.pointer = canvas.getPointer(o.e);
if (
!this.isDrawingLine ||
![DrawTypes.line, DrawTypes.arrow, DrawTypes.thinTailArrow].includes(
this.lineType
)
)
return
canvas.discardActiveObject()
const activeObject = canvas.getActiveObject()
if (activeObject) return
this.pointer = canvas.getPointer(o.e)

if (o.e.shiftKey) {
// calc angle
const startX = this.pointerPoints[0];
const startY = this.pointerPoints[1];
const x2 = this.pointer.x - startX;
const y2 = this.pointer.y - startY;
const r = Math.sqrt(x2 * x2 + y2 * y2);
let angle = (Math.atan2(y2, x2) / Math.PI) * 180;
angle = ~~(((angle + 7.5) % 360) / 15) * 15;
const startX = this.pointerPoints[0]
const startY = this.pointerPoints[1]
const x2 = this.pointer.x - startX
const y2 = this.pointer.y - startY
const r = Math.sqrt(x2 * x2 + y2 * y2)
let angle = (Math.atan2(y2, x2) / Math.PI) * 180
angle = ~~(((angle + 7.5) % 360) / 15) * 15

const cosx = r * Math.cos((angle * Math.PI) / 180);
const sinx = r * Math.sin((angle * Math.PI) / 180);
const cosx = r * Math.cos((angle * Math.PI) / 180)
const sinx = r * Math.sin((angle * Math.PI) / 180)

this.lineToDraw.set({
x2: cosx + startX,
y2: sinx + startY,
});
y2: sinx + startY
})
} else {
this.lineToDraw.set({
x2: this.pointer.x,
y2: this.pointer.y,
});
y2: this.pointer.y
})
}

canvas.renderAll();
});
canvas.renderAll()
})

canvas.on('mouse:up', () => {
if (!this.isDrawingLine) return;
this.lineToDraw.setCoords();
this.isDrawingLine = false;
canvas.discardActiveObject();
});
if (!this.isDrawingLine) return
this.lineToDraw.setCoords()
this.isDrawingLine = false
canvas.discardActiveObject()
})
}

setLineType(params: any) {
this.lineType = params;
this.lineType = params
}

setMode(params: any) {
this.isDrawingLineMode = params;
this.isDrawingLineMode = params
if (!this.isDrawingLineMode) {
this.endRest();
this.endRest()
}
}

endRest() {
this.canvas.getObjects().forEach((obj) => {
if (obj.id !== 'workspace') {
obj.selectable = true;
obj.hasControls = true;
obj.selectable = true
obj.hasControls = true
}
});
})
}

destroy() {
console.log('pluginDestroy');
console.log('pluginDestroy')
}
}

export default DrawLinePlugin;
export default DrawLinePlugin
5 changes: 3 additions & 2 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: June
* @Description:
* @Date: 2024-08-07 1ptions)5:41:56
* @LastEditTime: 2024-11-23 13:19:37
* @LastEditTime: 2024-11-23 16:23:29
* @LastEditors: June
* @FilePath: \element-fabric-editor\src\locales\index.ts
*/
Expand Down Expand Up @@ -46,7 +46,8 @@ async function createI18nOptions() {
globalInjection: true, // 全局注册$t方法
silentTranslationWarn: true, // true - warning off
missingWarn: false,
silentFallbackWarn: true
silentFallbackWarn: true,
fallbackWarn: false
}
return await createI18n(options)
}
3 changes: 3 additions & 0 deletions src/locales/langs/en/editor.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"psd": "InsertPSD",
"json": "InsertJSON"
},
"draw": {
"title": "DrawElement"
},
"header": {
"allTemplate": "AllTemplate",
"preview": "Preview",
Expand Down
Loading

0 comments on commit 79a40f9

Please sign in to comment.