Skip to content

Commit 2bc709c

Browse files
authored
toolbar drag #541 (#544)
* #541: enable dragging li's and svg's within the toolbar too (WIP: still need to prevent click event) * #541: prevent tool change after drag. Still WIP: more testing required. * more reliable response to clicks and dragging * replace "?." optional chaining operator to pass tests * got tests to pass: trigger mouseup instead of .click()
1 parent 6f6c17e commit 2bc709c

8 files changed

Lines changed: 42 additions & 14 deletions

File tree

app/components/vis-bug/vis-bug.element.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,28 @@ export default class VisBug extends HTMLElement {
8484
? this.getAttribute('color-scheme')
8585
: this.setAttribute('color-scheme', 'auto')
8686

87-
$('li[data-tool]', this.$shadow).on('click', e =>
88-
this.toolSelected(e.currentTarget) && e.stopPropagation())
87+
const main_ol = this.$shadow.querySelector('ol:not([colors])')
88+
const buttonPieces = $('li[data-tool], li[data-tool] *', main_ol)
89+
90+
const clickEvent = (e) => {
91+
const target = e.currentTarget || e.target
92+
const toolButton = target.closest('[data-tool]')
93+
if (toolButton) this.toolSelected(toolButton) && e.stopPropagation();
94+
}
95+
96+
Array.from(buttonPieces)
97+
.forEach(toolButton => {
98+
draggable({
99+
el:this,
100+
surface: toolButton,
101+
cursor: 'pointer',
102+
clickEvent: clickEvent
103+
})
104+
})
89105

90106
draggable({
91107
el:this,
92-
surface: this.$shadow.querySelector('ol:not([colors])'),
108+
surface: main_ol,
93109
cursor: 'grab',
94110
})
95111

app/features/font.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test('Can Be Deactivated', async t => {
3030
const { page } = t.context
3131

3232
t.is(await getActiveTool(page), tool)
33-
await page.evaluate(`document.querySelector('vis-bug').$shadow.querySelector('li[data-tool="padding"]').click()`)
33+
await changeMode({ tool: 'padding', page })
3434
t.is(await getActiveTool(page), 'padding')
3535

3636
t.pass()

app/features/margin.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('Can Be Deactivated', async t => {
2929
const { page } = t.context
3030

3131
t.is(await getActiveTool(page), tool)
32-
await page.evaluate(`document.querySelector('vis-bug').$shadow.querySelector('li[data-tool="padding"]').click()`)
32+
await changeMode({ tool: 'padding', page })
3333
t.is(await getActiveTool(page), 'padding')
3434

3535
t.pass()

app/features/metatip.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('Can Be Deactivated', async t => {
2525
const { page } = t.context
2626

2727
t.is(await getActiveTool(page), tool)
28-
await page.evaluate(`document.querySelector('vis-bug').$shadow.querySelector('li[data-tool="padding"]').click()`)
28+
await changeMode({ tool: 'padding', page })
2929
t.is(await getActiveTool(page), 'padding')
3030

3131
t.pass()

app/features/move.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('Can Be Deactivated', async t => {
2929
const { page } = t.context
3030

3131
t.is(await getActiveTool(page), tool)
32-
await page.evaluate(`document.querySelector('vis-bug').$shadow.querySelector('li[data-tool="padding"]').click()`)
32+
await changeMode({ tool: 'padding', page })
3333
t.is(await getActiveTool(page), 'padding')
3434

3535
t.pass()

app/features/padding.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test('Can Be Deactivated', async t => {
2929
const { page } = t.context
3030

3131
t.is(await getActiveTool(page), tool)
32-
await page.evaluate(`document.querySelector('vis-bug').$shadow.querySelector('li[data-tool="margin"]').click()`)
32+
await changeMode({ tool: 'margin', page })
3333
t.is(await getActiveTool(page), 'margin')
3434

3535
t.pass()

app/features/position.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function Position() {
4343
}
4444
}
4545

46-
export function draggable({el, surface = el, cursor = 'move'}) {
46+
export function draggable({el, surface = el, cursor = 'move', clickEvent}) {
4747
const state = {
4848
target: el,
4949
surface,
@@ -55,7 +55,8 @@ export function draggable({el, surface = el, cursor = 'move'}) {
5555
element: {
5656
x: 0,
5757
y: 0,
58-
}
58+
},
59+
travelDistance: 0
5960
}
6061

6162
const setup = () => {
@@ -99,9 +100,10 @@ export function draggable({el, surface = el, cursor = 'move'}) {
99100
state.element.y = parseInt(getStyle(el, 'top'))
100101
}
101102

102-
state.mouse.x = e.clientX
103-
state.mouse.y = e.clientY
104-
state.mouse.down = true
103+
state.mouse.x = e.clientX
104+
state.mouse.y = e.clientY
105+
state.mouse.down = true
106+
state.travelDistance = 0
105107
}
106108

107109
const onMouseUp = e => {
@@ -127,6 +129,10 @@ export function draggable({el, surface = el, cursor = 'move'}) {
127129
state.element.x = parseInt(el.style.left) || 0
128130
state.element.y = parseInt(el.style.top) || 0
129131
}
132+
133+
const treatAsClick = !state.travelDistance || state.travelDistance < 5
134+
if (clickEvent && treatAsClick) clickEvent(e);
135+
state.travelDistance = 0 // reset after
130136
}
131137

132138
const onMouseMove = e => {
@@ -146,6 +152,8 @@ export function draggable({el, surface = el, cursor = 'move'}) {
146152
el.style.left = state.element.x + e.clientX - state.mouse.x + 'px'
147153
el.style.top = state.element.y + e.clientY - state.mouse.y + 'px'
148154
}
155+
156+
state.travelDistance += 1
149157
}
150158

151159
setup()

tests/helpers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export const teardownPptrTab = async ({context:{ page, browser }}) => {
1313
}
1414

1515
export const changeMode = async ({page, tool}) =>
16-
await page.evaluateHandle(`document.querySelector('vis-bug').$shadow.querySelector('li[data-tool=${tool}]').click()`)
16+
await page.evaluateHandle(`
17+
var mouseUpEvent = document.createEvent("MouseEvents");
18+
mouseUpEvent.initEvent("mouseup", true, true);
19+
document.querySelector('vis-bug').$shadow.querySelector('li[data-tool=${tool}]').dispatchEvent(mouseUpEvent);
20+
`)
1721

1822
export const getActiveTool = async page =>
1923
await page.$eval('vis-bug', el =>

0 commit comments

Comments
 (0)