[+] Allow initial position

This commit is contained in:
Hykilpikonna
2021-12-11 18:33:42 -05:00
parent 8bc642036c
commit 7551ee8c05
+20 -6
View File
@@ -1,5 +1,5 @@
<template> <template>
<div id="ColorPicker" ref="window" @keydown.esc="close"> <div id="ColorPicker" ref="el" @keydown.esc="close">
<div id="title-colors" class="fbox-h"> <div id="title-colors" class="fbox-h">
<div class="text" @mousedown="windowDrag" @click.right.alt="close">Colors</div> <div class="text" @mousedown="windowDrag" @click.right.alt="close">Colors</div>
<div class="close fbox-vcenter" @click="close" v-if="showClose"><i class="fas fa-times"></i></div> <div class="close fbox-vcenter" @click="close" v-if="showClose"><i class="fas fa-times"></i></div>
@@ -24,7 +24,7 @@
import "vue3-colorpicker/style.css"; import "vue3-colorpicker/style.css";
import {ColorPicker} from "vue3-colorpicker"; import {ColorPicker} from "vue3-colorpicker";
import {range} from "@/utils"; import {range} from "@/utils";
import {Emit, Model} from "vue-property-decorator"; import {Emit, Model, Prop, Ref} from "vue-property-decorator";
import {Vue, Options} from "vue-class-component"; import {Vue, Options} from "vue-class-component";
/** /**
@@ -44,6 +44,9 @@ export default class MyColorPicker extends Vue
palette: string[][] = [] palette: string[][] = []
showClose = false showClose = false
@Ref() el!: HTMLElement
@Prop({default: null}) initialPos?: {x: number, y: number}
/** /**
* Init * Init
*/ */
@@ -57,6 +60,11 @@ export default class MyColorPicker extends Vue
this.storePalette() this.storePalette()
} }
mounted(): void
{
if (this.initialPos) this.setPos(this.initialPos.x, this.initialPos.y)
}
/** /**
* Color change * Color change
*/ */
@@ -67,22 +75,28 @@ export default class MyColorPicker extends Vue
return this.colorModel return this.colorModel
} }
/**
* Set window position
*/
setPos(x: number, y: number): void
{
this.el.style.left = x + 'px'
this.el.style.top = y + 'px'
}
/** /**
* Window dragging * Window dragging
*/ */
windowDrag(e: MouseEvent): void windowDrag(e: MouseEvent): void
{ {
e.preventDefault() e.preventDefault()
const el = this.$refs.window as HTMLElement
let lastX = e.clientX, lastY = e.clientY let lastX = e.clientX, lastY = e.clientY
const mousemove = (e: MouseEvent) => const mousemove = (e: MouseEvent) =>
{ {
const dx = lastX - e.clientX, dy = lastY - e.clientY const dx = lastX - e.clientX, dy = lastY - e.clientY
lastX = e.clientX; lastY = e.clientY lastX = e.clientX; lastY = e.clientY
el.style.left = (el.offsetLeft - dx) + 'px' this.setPos(this.el.offsetLeft - dx, this.el.offsetTop - dy)
el.style.top = (el.offsetTop - dy) + 'px'
} }
const mouseup = () => {document.removeEventListener('mouseup', mouseup); document.removeEventListener('mousemove', mousemove)} const mouseup = () => {document.removeEventListener('mouseup', mouseup); document.removeEventListener('mousemove', mousemove)}
document.addEventListener('mouseup', mouseup) document.addEventListener('mouseup', mouseup)