Skip to content

Commit

Permalink
fix: avoid adding a new declaration when finish editing existing style (
Browse files Browse the repository at this point in the history
#21)

* chore: emit input-start event when style value starting editing

* fix: avoid adding a new declaration when finish editing existing style
  • Loading branch information
ktsn committed Apr 10, 2018
1 parent 98bd2fe commit 1ddcc1d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/view/components/StyleDeclaration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
class="style-declaration-prop-text"
:auto-focus="autoFocusProp"
:value="prop"
@input-start="$emit('input-start')"
@input="inputProp"
@input-end="finishInputProp"
/></span>
<span class="style-declaration-value"><StyleValue
:value="value"
@input-start="$emit('input-start')"
@input="inputValue"
@input-end="finishInputValue"
/></span>
Expand Down Expand Up @@ -48,6 +50,8 @@ export default Vue.extend({
},
finishInputProp(rawProp: string): void {
this.$emit('input-end')
const prop = rawProp.trim()
if (!prop) {
this.$emit('remove')
Expand All @@ -57,6 +61,8 @@ export default Vue.extend({
},
finishInputValue(rawValue: string): void {
this.$emit('input-end')
const value = rawValue.trim()
if (!value) {
this.$emit('remove')
Expand Down
36 changes: 35 additions & 1 deletion src/view/components/StyleInformation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@update:prop="updateDeclarationProp(d.path, arguments[0])"
@update:value="updateDeclarationValue(d.path, arguments[0])"
@remove="removeDeclaration(d.path)"
@input-start="onStartStyleInput"
@input-end="onEndStyleInput"
/>
</li>
</ul>
Expand Down Expand Up @@ -49,7 +51,8 @@ export default Vue.extend({
data() {
return {
autoFocusOnNextRender: false
autoFocusOnNextRender: false,
endingInput: false
}
},
Expand All @@ -73,10 +76,36 @@ export default Vue.extend({
},
onClickRule(rule: RuleForPrint): void {
if (this.endingInput) return
this.$emit('add-declaration', {
path: rule.path.concat(rule.declarations.length)
})
this.autoFocusOnNextRender = true
},
/*
* While it will add a new rule when clicking empty space,
* we don't want to do that if it is intended to cancel style editing.
* To resolve that situation, we limit to add a new declaration
* when the user is starting editing style value. Then release
* the limitation after several time passes from finished editing.
*/
onStartStyleInput(): void {
const vm: any = this
clearTimeout(vm.endingInputTimer)
this.endingInput = true
},
onEndStyleInput(): void {
const delayToEndEdit = 200
const vm: any = this
clearTimeout(vm.endingInputTimer)
vm.endingInputTimer = setTimeout(() => {
this.endingInput = false
}, delayToEndEdit)
}
},
Expand All @@ -86,6 +115,11 @@ export default Vue.extend({
this.autoFocusOnNextRender = false
})
}
},
created() {
const vm: any = this
vm.endingInputTimer = null
}
})
</script>
Expand Down
1 change: 1 addition & 0 deletions src/view/components/StyleValue.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default Vue.extend({
if (input) {
input.textContent = this.value
selectNodeContents(input)
this.$emit('input-start')
}
})
},
Expand Down
11 changes: 11 additions & 0 deletions test/view/StyleValue/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ describe('StyleValue basic', () => {
expect(wrapper.emitted('input')[0]).toEqual(['22px'])
})

it('should notify starting input', async () => {
const wrapper = mount(StyleValue, {
propsData: {
value: '20px'
}
})
wrapper.trigger('click')
await wrapper.vm.$nextTick()
expect(wrapper.emitted('input-start').length).toBe(1)
})

it('should end editing when blured', () => {
const wrapper = mount(StyleValue, {
propsData: {
Expand Down

0 comments on commit 1ddcc1d

Please sign in to comment.