feat: 添加最近使用颜色

This commit is contained in:
pipipi-pikachu 2021-02-24 16:18:43 +08:00
parent 7735f7767d
commit 9e4945d3ea

View File

@ -56,12 +56,24 @@
@click="selectPresetColor(c)" @click="selectPresetColor(c)"
></div> ></div>
</div> </div>
<div class="recent-colors-title">最近使用</div>
<div class="recent-colors">
<div
v-for="c in recentColors"
:key="c"
class="picker-presets-color"
:style="{ background: c }"
@click="selectPresetColor(c)"
></div>
</div>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, ref } from 'vue' import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import tinycolor, { ColorFormats } from 'tinycolor2' import tinycolor, { ColorFormats } from 'tinycolor2'
import debounce from 'lodash/debounce'
import Alpha from './Alpha.vue' import Alpha from './Alpha.vue'
import Checkboard from './Checkboard.vue' import Checkboard from './Checkboard.vue'
@ -69,6 +81,8 @@ import Hue from './Hue.vue'
import Saturation from './Saturation.vue' import Saturation from './Saturation.vue'
import EditableInput from './EditableInput.vue' import EditableInput from './EditableInput.vue'
const RECENT_COLORS = 'RECENT_COLORS'
const presetColorConfig = [ const presetColorConfig = [
['#7f7f7f', '#f2f2f2'], ['#7f7f7f', '#f2f2f2'],
['#0d0d0d', '#808080'], ['#0d0d0d', '#808080'],
@ -127,6 +141,7 @@ export default defineComponent({
}, },
setup(props, { emit }) { setup(props, { emit }) {
const hue = ref(0) const hue = ref(0)
const recentColors = ref<string[]>([])
const color = computed({ const color = computed({
get() { get() {
@ -150,12 +165,37 @@ export default defineComponent({
emit('update:modelValue', colorString) emit('update:modelValue', colorString)
} }
// 使
const updateRecentColorsCache = debounce(function() {
const _color = tinycolor(color.value).toRgbString()
if (!recentColors.value.includes(_color)) {
recentColors.value = [_color, ...recentColors.value]
const maxLength = 10
if (recentColors.value.length > maxLength) {
recentColors.value = recentColors.value.slice(-maxLength)
}
}
}, 300, { trailing: true })
onMounted(() => {
const recentColorsCache = localStorage.getItem(RECENT_COLORS)
if (recentColorsCache) recentColors.value = JSON.parse(recentColorsCache)
})
watch(recentColors, () => {
const recentColorsCache = JSON.stringify(recentColors.value)
localStorage.setItem(RECENT_COLORS, recentColorsCache)
})
const changeColor = (value: ColorFormats.RGBA | ColorFormats.HSLA | ColorFormats.HSVA) => { const changeColor = (value: ColorFormats.RGBA | ColorFormats.HSLA | ColorFormats.HSVA) => {
if ('h' in value) { if ('h' in value) {
hue.value = value.h hue.value = value.h
color.value = tinycolor(value).toRgb() color.value = tinycolor(value).toRgb()
} }
else color.value = value else color.value = value
updateRecentColorsCache()
} }
return { return {
@ -167,6 +207,7 @@ export default defineComponent({
currentColor, currentColor,
changeColor, changeColor,
selectPresetColor, selectPresetColor,
recentColors,
} }
}, },
}) })
@ -256,4 +297,12 @@ export default defineComponent({
position: relative; position: relative;
cursor: pointer; cursor: pointer;
} }
.recent-colors-title {
font-size: 12px;
margin-bottom: 4px;
}
.recent-colors {
@include flex-grid-layout();
}
</style> </style>