-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathBottomPanel.js
203 lines (187 loc) · 6.08 KB
/
BottomPanel.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import SvgCanvas from '@svgedit/svgcanvas'
import { jGraduate } from '../components/jgraduate/jQuery.jGraduate.js'
import BottomPanelHtml from './BottomPanel.html'
const { $id } = SvgCanvas
/*
* register actions for left panel
*/
/**
* @type {module}
*/
class BottomPanel {
/**
* @param {PlainObject} editor svgedit handler
*/
constructor (editor) {
this.editor = editor
}
/**
* @type {module}
*/
get selectedElement () {
return this.editor.selectedElement
}
/**
* @type {module}
*/
get multiselected () {
return this.editor.multiselected
}
/**
* @type {module}
*/
changeStrokeWidth (e) {
let val = e.target.value
if (val === 0 && this.editor.selectedElement && ['line', 'polyline'].includes(this.editor.selectedElement.nodeName)) {
val = 1
}
this.editor.svgCanvas.setStrokeWidth(val)
}
/**
* @type {module}
*/
changeZoom (value) {
switch (value) {
case 'canvas':
case 'selection':
case 'layer':
case 'content':
this.editor.zoomChanged(window, value)
break
default:
{
const zoomlevel = Number(value) > 0.1 ? Number(value) * 0.01 : 0.1
const zoom = this.editor.svgCanvas.getZoom()
const { workarea } = this.editor
this.editor.zoomChanged(window, {
width: 0,
height: 0,
// center pt of scroll position
x: (workarea.scrollLeft + parseFloat(getComputedStyle(workarea, null).width.replace('px', '')) / 2) / zoom,
y: (workarea.scrollTop + parseFloat(getComputedStyle(workarea, null).height.replace('px', '')) / 2) / zoom,
zoom: zoomlevel
}, true)
}
}
}
/**
* @fires module:svgcanvas.SvgCanvas#event:ext_toolButtonStateUpdate
* @returns {void}
*/
updateToolButtonState () {
const bNoFill = (this.editor.svgCanvas.getColor('fill') === 'none')
const bNoStroke = (this.editor.svgCanvas.getColor('stroke') === 'none')
const buttonsNeedingStroke = ['tool_fhpath', 'tool_line']
const buttonsNeedingFillAndStroke = [
'tools_rect', 'tools_ellipse',
'tool_text', 'tool_path'
]
if (bNoStroke) {
buttonsNeedingStroke.forEach((btn) => {
// if btn is pressed, change to select button
if ($id(btn).pressed) {
this.editor.leftPanel.clickSelect()
}
$id(btn).disabled = true
})
} else {
buttonsNeedingStroke.forEach((btn) => {
$id(btn).disabled = false
})
}
if (bNoStroke && bNoFill) {
buttonsNeedingFillAndStroke.forEach((btn) => {
// if btn is pressed, change to select button
if ($id(btn).pressed) {
this.editor.leftPanel.clickSelect()
}
$id(btn).disabled = true
})
} else {
buttonsNeedingFillAndStroke.forEach((btn) => {
$id(btn).disabled = false
})
}
this.editor.svgCanvas.runExtensions(
'toolButtonStateUpdate',
/** @type {module:svgcanvas.SvgCanvas#event:ext_toolButtonStateUpdate} */ {
nofill: bNoFill,
nostroke: bNoStroke
}
)
}
/**
* @type {module}
*/
handleColorPicker (type, evt) {
const { paint } = evt.detail
this.editor.svgCanvas.setPaint(type, paint)
this.updateToolButtonState()
}
/**
* @type {module}
*/
handleStrokeAttr (type, evt) {
this.editor.svgCanvas.setStrokeAttr(type, evt.detail.value)
}
/**
* @type {module}
*/
handleOpacity (evt) {
const val = Number.parseInt(evt.currentTarget.value.split('%')[0])
this.editor.svgCanvas.setOpacity(val / 100)
}
/**
* @type {module}
*/
handlePalette (e) {
e.preventDefault()
// shift key or right click for stroke
const { picker, color } = e.detail
// Webkit-based browsers returned 'initial' here for no stroke
const paint = color === 'none' ? new jGraduate.Paint() : new jGraduate.Paint({ alpha: 100, solidColor: color.substr(1) })
if (picker === 'fill') {
$id('fill_color').setPaint(paint)
} else {
$id('stroke_color').setPaint(paint)
}
this.editor.svgCanvas.setColor(picker, color)
if (color !== 'none' && this.editor.svgCanvas.getPaintOpacity(picker) !== 1) {
this.editor.svgCanvas.setPaintOpacity(picker, 1.0)
}
this.updateToolButtonState()
}
/**
* @type {module}
*/
init () {
// register actions for Bottom panel
const template = document.createElement('template')
const { i18next } = this.editor
template.innerHTML = BottomPanelHtml
this.editor.$svgEditor.append(template.content.cloneNode(true))
$id('palette').addEventListener('change', this.handlePalette.bind(this))
$id('palette').init(i18next)
const { curConfig } = this.editor.configObj
$id('fill_color').setPaint(new jGraduate.Paint({ alpha: 100, solidColor: curConfig.initFill.color }))
$id('stroke_color').setPaint(new jGraduate.Paint({ alpha: 100, solidColor: curConfig.initStroke.color }))
$id('zoom').addEventListener('change', (e) => this.changeZoom.bind(this)(e.detail.value))
$id('stroke_color').addEventListener('change', (evt) => this.handleColorPicker.bind(this)('stroke', evt))
$id('fill_color').addEventListener('change', (evt) => this.handleColorPicker.bind(this)('fill', evt))
$id('stroke_width').addEventListener('change', this.changeStrokeWidth.bind(this))
$id('stroke_style').addEventListener('change', (evt) => this.handleStrokeAttr.bind(this)('stroke-dasharray', evt))
$id('stroke_linejoin').addEventListener('change', (evt) => this.handleStrokeAttr.bind(this)('stroke-linejoin', evt))
$id('stroke_linecap').addEventListener('change', (evt) => this.handleStrokeAttr.bind(this)('stroke-linecap', evt))
$id('opacity').addEventListener('change', this.handleOpacity.bind(this))
$id('fill_color').init(i18next)
$id('stroke_color').init(i18next)
}
/**
* @type {module}
*/
updateColorpickers (apply) {
$id('fill_color').update(this.editor.svgCanvas, this.editor.selectedElement, apply)
$id('stroke_color').update(this.editor.svgCanvas, this.editor.selectedElement, apply)
}
}
export default BottomPanel