editor: add missing select-area module
This commit is contained in:
parent
1db58c00ec
commit
af98cd5452
2 changed files with 128 additions and 1 deletions
|
|
@ -209,7 +209,7 @@ var Editor = class Editor {
|
|||
})
|
||||
|
||||
|
||||
this.selectarea = new SelectArea((elements)=>{
|
||||
this.selectarea = new SelectArea('.widget:not(.not-editable), .tablink', (elements)=>{
|
||||
|
||||
elements = elements.map(e => widgetManager.getWidgetByElement(e, ':not(.not-editable)')).filter(e => e)
|
||||
|
||||
|
|
|
|||
127
src/browser/js/app/editor/select-area.js
Normal file
127
src/browser/js/app/editor/select-area.js
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
class SelectArea {
|
||||
|
||||
constructor(selector, callback){
|
||||
|
||||
this.startX = 0
|
||||
this.startY = 0
|
||||
this.curX = 0
|
||||
this.curY = 0
|
||||
|
||||
this.rectCoords = [0, 0, 0, 0]
|
||||
this.rectSize = [0, 0]
|
||||
this.elements = []
|
||||
|
||||
this.rect = DOM.create('<div class="select-area hidden"></div>')
|
||||
document.body.appendChild(this.rect)
|
||||
|
||||
this.selector = selector
|
||||
this.callback = callback
|
||||
|
||||
this.bindedMousedown = this.mousedown.bind(this)
|
||||
this.bindedMousemove = this.mousemove.bind(this)
|
||||
this.bindedMouseup = this.mouseup.bind(this)
|
||||
|
||||
}
|
||||
|
||||
mousedown(e) {
|
||||
if (!e.ctrlKey) return
|
||||
this.startX = this.curX = e.clientX
|
||||
this.startY = this.curY = e.clientY
|
||||
this.down = true
|
||||
this.updateRect()
|
||||
}
|
||||
|
||||
mousemove(e) {
|
||||
if (!this.down) return
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
this.curX = e.clientX
|
||||
this.curY = e.clientY
|
||||
this.updateRect()
|
||||
}
|
||||
|
||||
mouseup(e) {
|
||||
if (!this.down) return
|
||||
this.down = false
|
||||
|
||||
if (this.rectSize[0] > 0 && this.rectSize[1] > 0) {
|
||||
this.callback(this.getElements())
|
||||
this.rect.classList.add('hidden')
|
||||
}
|
||||
|
||||
this.elements = []
|
||||
|
||||
}
|
||||
|
||||
updateRect() {
|
||||
|
||||
var x0 = Math.min(this.startX, this.curX),
|
||||
y0 = Math.min(this.startY, this.curY),
|
||||
x1 = Math.max(this.startX, this.curX),
|
||||
y1 = Math.max(this.startY, this.curY),
|
||||
w = x1 - x0,
|
||||
h = y1 - y0
|
||||
|
||||
if (w < 1 || h < 1) {
|
||||
this.rect.classList.add('hidden')
|
||||
} else {
|
||||
this.rect.classList.remove('hidden')
|
||||
this.rect.setAttribute('style', `width:${w}px;height:${h}px;left:${x0}px;top:${y0}px`)
|
||||
}
|
||||
|
||||
this.rectSize = [w, h]
|
||||
this.rectCoords = [x0, x1, y0, y1]
|
||||
|
||||
}
|
||||
|
||||
getElements(){
|
||||
|
||||
var [x0, x1, y0, y1] = this.rectCoords,
|
||||
startEl = document.elementFromPoint(this.startX, this.startY),
|
||||
elements = [startEl]
|
||||
|
||||
this.elements = DOM.each(document, this.selector, (el)=>{
|
||||
|
||||
var {left, right, top, bottom} = el.getBoundingClientRect()
|
||||
if (!(
|
||||
left > x1 ||
|
||||
right < x0 ||
|
||||
top > y1 ||
|
||||
bottom < y0
|
||||
)) {
|
||||
if (el !== startEl) elements.push(el)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
// slower implementation...
|
||||
// for (var x = x0; x <= x1; x += 10) {
|
||||
// for (var y = y0; y <= y1; y += 10) {
|
||||
// var el = document.elementFromPoint(x, y)
|
||||
// if (elements.indexOf(el) < 0) elements.push(el)
|
||||
// }
|
||||
// }
|
||||
|
||||
return elements
|
||||
|
||||
}
|
||||
|
||||
enable() {
|
||||
|
||||
document.addEventListener('mousedown', this.bindedMousedown)
|
||||
document.addEventListener('mousemove', this.bindedMousemove, true)
|
||||
document.addEventListener('mouseup', this.bindedMouseup)
|
||||
|
||||
}
|
||||
|
||||
disable() {
|
||||
|
||||
document.removeEventListener('mousedown', this.bindedMousedown)
|
||||
document.removeEventListener('mousemove', this.bindedMousemove, true)
|
||||
document.removeEventListener('mouseup', this.bindedMouseup)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = SelectArea
|
||||
Loading…
Add table
Add a link
Reference in a new issue