-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support updating style declaration in preview (#6)
* feat: add style value component with basic features * fix: end style value editing when pressed enter * chore: add helper function to find declaration * chore: add style declaration modifier * feat: support updating style declaration in preview * chore: more proper contenteditable value handling * chore: naive client side declaration updating * chore: select entire text content when starting declaration editing * feat: allow to edit declaration prop * refactor: move parsing logic of !important to store module * refactor: remove unused type * test: add more test case for StyleValue
- Loading branch information
Showing
20 changed files
with
600 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<template> | ||
<button | ||
v-if="!editing" | ||
class="style-value" | ||
@click="startEdit" | ||
@focus="startEdit" | ||
>{{ value }}</button> | ||
<div | ||
v-else | ||
class="style-value editing" | ||
contenteditable="true" | ||
ref="input" | ||
@input="input" | ||
@keypress.enter="endEdit" | ||
@blur="endEdit" | ||
></div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import Vue from 'vue' | ||
import { selectNodeContents } from '@/view/editing' | ||
export default Vue.extend({ | ||
name: 'StyleValue', | ||
props: { | ||
value: { | ||
type: String, | ||
required: true | ||
} | ||
}, | ||
data() { | ||
return { | ||
editing: false | ||
} | ||
}, | ||
watch: { | ||
value(newValue: string): void { | ||
const input = this.$refs.input as HTMLDivElement | undefined | ||
if (input && newValue !== input.textContent) { | ||
input.textContent = newValue | ||
} | ||
} | ||
}, | ||
methods: { | ||
startEdit(): void { | ||
this.editing = true | ||
this.$nextTick(() => { | ||
const input = this.$refs.input as HTMLDivElement | undefined | ||
if (input) { | ||
input.textContent = this.value | ||
selectNodeContents(input) | ||
} | ||
}) | ||
}, | ||
endEdit(): void { | ||
this.editing = false | ||
}, | ||
input(event: Event): void { | ||
const el = event.target as HTMLDivElement | ||
this.$emit('input', el.textContent) | ||
} | ||
} | ||
}) | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
.style-value { | ||
display: inline; | ||
padding: 0; | ||
border-width: 0; | ||
background: none; | ||
font-family: inherit; | ||
font-size: inherit; | ||
} | ||
.style-value.editing { | ||
margin: -1px; | ||
border: 1px solid #aaa; | ||
background-color: #fff; | ||
outline: none; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function selectNodeContents(el: Node): void { | ||
// Skip if selection does not exist (in case of testing environment) | ||
if (!('getSelection' in window)) return | ||
|
||
const selection = window.getSelection() | ||
const range = new Range() | ||
range.selectNodeContents(el) | ||
selection.removeAllRanges() | ||
selection.addRange(range) | ||
} |
Oops, something went wrong.