-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ext-shapes.js
162 lines (136 loc) · 4.49 KB
/
ext-shapes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @file ext-shapes.js
*
* @license MIT
*
* @copyright 2010 Christian Tzurcanu, 2010 Alexis Deveria
*
*/
const name = 'shapes'
const loadExtensionTranslation = async function (svgEditor) {
let translationModule
const lang = svgEditor.configObj.pref('lang')
try {
translationModule = await import(`./locale/${lang}.js`)
} catch (_error) {
console.warn(`Missing translation (${lang}) for ${name} - using 'en'`)
translationModule = await import('./locale/en.js')
}
svgEditor.i18next.addResourceBundle(lang, name, translationModule.default)
}
export default {
name,
async init () {
const svgEditor = this
const canv = svgEditor.svgCanvas
const { $id, $click } = canv
const svgroot = canv.getSvgRoot()
let lastBBox = {}
await loadExtensionTranslation(svgEditor)
const modeId = 'shapelib'
const startClientPos = {}
let curShape
let startX
let startY
return {
callback () {
if ($id('tool_shapelib') === null) {
const extPath = svgEditor.configObj.curConfig.extPath
const buttonTemplate = `
<se-explorerbutton id="tool_shapelib" title="${svgEditor.i18next.t(`${name}:buttons.0.title`)}" lib="${extPath}/ext-shapes/shapelib/"
src="shapelib.svg"></se-explorerbutton>
`
canv.insertChildAtIndex($id('tools_left'), buttonTemplate, 9)
$click($id('tool_shapelib'), () => {
if (this.leftPanel.updateLeftPanel('tool_shapelib')) {
canv.setMode(modeId)
}
})
}
},
mouseDown (opts) {
const mode = canv.getMode()
if (mode !== modeId) { return undefined }
const currentD = document.getElementById('tool_shapelib').dataset.draw
startX = opts.start_x
const x = startX
startY = opts.start_y
const y = startY
const curStyle = canv.getStyle()
startClientPos.x = opts.event.clientX
startClientPos.y = opts.event.clientY
curShape = canv.addSVGElementsFromJson({
element: 'path',
curStyles: true,
attr: {
d: currentD,
id: canv.getNextId(),
opacity: curStyle.opacity / 2,
style: 'pointer-events:none'
}
})
curShape.setAttribute('transform', 'translate(' + x + ',' + y + ') scale(0.005) translate(' + -x + ',' + -y + ')')
canv.recalculateDimensions(curShape)
lastBBox = curShape.getBBox()
return {
started: true
}
},
mouseMove (opts) {
const mode = canv.getMode()
if (mode !== modeId) { return }
const zoom = canv.getZoom()
const evt = opts.event
const x = opts.mouse_x / zoom
const y = opts.mouse_y / zoom
const tlist = curShape.transform.baseVal
const box = curShape.getBBox()
const left = box.x; const top = box.y
const newbox = {
x: Math.min(startX, x),
y: Math.min(startY, y),
width: Math.abs(x - startX),
height: Math.abs(y - startY)
}
let sx = (newbox.width / lastBBox.width) || 1
let sy = (newbox.height / lastBBox.height) || 1
// Not perfect, but mostly works...
let tx = 0
if (x < startX) {
tx = lastBBox.width
}
let ty = 0
if (y < startY) {
ty = lastBBox.height
}
// update the transform list with translate,scale,translate
const translateOrigin = svgroot.createSVGTransform()
const scale = svgroot.createSVGTransform()
const translateBack = svgroot.createSVGTransform()
translateOrigin.setTranslate(-(left + tx), -(top + ty))
if (!evt.shiftKey) {
const max = Math.min(Math.abs(sx), Math.abs(sy))
sx = max * (sx < 0 ? -1 : 1)
sy = max * (sy < 0 ? -1 : 1)
}
scale.setScale(sx, sy)
translateBack.setTranslate(left + tx, top + ty)
tlist.appendItem(translateBack)
tlist.appendItem(scale)
tlist.appendItem(translateOrigin)
canv.recalculateDimensions(curShape)
lastBBox = curShape.getBBox()
},
mouseUp (opts) {
const mode = canv.getMode()
if (mode !== modeId) { return undefined }
const keepObject = (opts.event.clientX !== startClientPos.x && opts.event.clientY !== startClientPos.y)
return {
keep: keepObject,
element: curShape,
started: false
}
}
}
}
}