Merge pull request #406 from Budibase/colorpicker/keyboard-events-and-bugs

Key Events, Folder Tidyup, Bugs
This commit is contained in:
Conor_Mack 2020-07-01 17:39:43 +01:00 committed by GitHub
commit a1a1fce3ca
16 changed files with 135 additions and 35 deletions

View File

@ -0,0 +1,2 @@
export { default as drag } from "./drag.js"
export { default as keyevents } from "./key-events.js"

View File

@ -0,0 +1,41 @@
//events: Array<{trigger: fn}>
export default function(node, events = []) {
const ev = Object.entries(events)
let fns = []
for (let [trigger, fn] of ev) {
let f = addEvent(trigger, fn)
fns = [...fns, f]
}
function _scaffold(trigger, fn) {
return () => {
let trig = parseInt(trigger)
if (trig) {
if (event.keyCode === trig) {
fn(event)
}
} else {
if (event.key === trigger) {
fn(event)
}
}
}
}
function addEvent(trigger, fn) {
let f = _scaffold(trigger, fn)
node.addEventListener("keydown", f)
return f
}
function removeEvents() {
fns.forEach(f => node.removeEventListener("keypress", f))
}
return {
destroy() {
removeEvents()
},
}
}

View File

@ -1,5 +1,5 @@
<script> <script>
import { buildStyle } from "./helpers.js" import {buildStyle} from "../helpers.js"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
export let backgroundSize = "10px" export let backgroundSize = "10px"

View File

@ -3,17 +3,18 @@
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import Swatch from "./Swatch.svelte" import Swatch from "./Swatch.svelte"
import CheckedBackground from "./CheckedBackground.svelte" import CheckedBackground from "./CheckedBackground.svelte"
import { buildStyle } from "./helpers.js" import {buildStyle} from "../helpers.js"
import { import {
getColorFormat, getColorFormat,
convertToHSVA, convertToHSVA,
convertHsvaToFormat, convertHsvaToFormat,
} from "./utils.js" } from "../utils.js"
import Slider from "./Slider.svelte" import Slider from "./Slider.svelte"
import Palette from "./Palette.svelte" import Palette from "./Palette.svelte"
import ButtonGroup from "./ButtonGroup.svelte" import ButtonGroup from "./ButtonGroup.svelte"
import Input from "./Input.svelte" import Input from "./Input.svelte"
import Portal from "./Portal.svelte" import Portal from "./Portal.svelte"
import {keyevents} from "../actions"
export let value = "#3ec1d3ff" export let value = "#3ec1d3ff"
export let open = false; export let open = false;
@ -27,10 +28,10 @@
let colorPicker = null let colorPicker = null
let adder = null let adder = null
let h = null let h = 0
let s = null let s = 0
let v = null let v = 0
let a = null let a = 0
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
@ -56,9 +57,9 @@
} }
} }
function handleEscape(e) { function handleEscape() {
if(open && e.key === "Escape") { if(open) {
open = false open = false;
} }
} }
@ -162,15 +163,14 @@
</script> </script>
<Portal> <Portal>
<div <div
class="colorpicker-container" class="colorpicker-container"
use:keyevents={{"Escape": handleEscape}}
transition:fade transition:fade
bind:this={colorPicker} bind:this={colorPicker}
{style} {style}
tabindex="0" tabindex="0"
on:keydown={handleEscape}
bind:clientHeight={pickerHeight} bind:clientHeight={pickerHeight}
bind:clientWidth={pickerWidth}> bind:clientWidth={pickerWidth}>
@ -215,11 +215,13 @@
{/if} {/if}
{#if swatches.length !== 12} {#if swatches.length !== 12}
<div <div
tabindex="0"
use:keyevents={{"Enter": addSwatch}}
bind:this={adder} bind:this={adder}
transition:fade transition:fade
class="adder" class="adder"
on:click={addSwatch} class:shrink
class:shrink> on:click={addSwatch}>
<span>&plus;</span> <span>&plus;</span>
</div> </div>
{/if} {/if}
@ -246,6 +248,7 @@
display: flex; display: flex;
font-size: 11px; font-size: 11px;
font-weight: 400; font-weight: 400;
transition: top 0.1s, left 0.1s;
flex-direction: column; flex-direction: column;
margin: 5px 0px; margin: 5px 0px;
height: auto; height: auto;
@ -297,7 +300,7 @@
flex: 1; flex: 1;
height: 20px; height: 20px;
display: flex; display: flex;
transition: flex 0.5s; transition: flex 0.3s;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
background: #f1f3f4; background: #f1f3f4;
@ -307,6 +310,8 @@
margin-left: 5px; margin-left: 5px;
margin-top: 3px; margin-top: 3px;
font-weight: 500; font-weight: 500;
outline-color: #003cb0;
outline-width: thin;
} }
.shrink { .shrink {

View File

@ -1,11 +1,11 @@
<script> <script>
import Colorpicker from "./Colorpicker.svelte" import Colorpicker from "./Colorpicker.svelte"
import CheckedBackground from "./CheckedBackground.svelte" import CheckedBackground from "./CheckedBackground.svelte"
import { createEventDispatcher, beforeUpdate } from "svelte" import { createEventDispatcher, beforeUpdate, onMount } from "svelte"
import { buildStyle } from "./helpers.js" import {buildStyle, debounce} from "../helpers.js"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import { getColorFormat } from "./utils.js" import { getColorFormat } from "../utils.js"
export let value = "#3ec1d3ff" export let value = "#3ec1d3ff"
export let swatches = [] export let swatches = []
@ -50,8 +50,8 @@
dispatch("change", color.detail) dispatch("change", color.detail)
} }
$: if(open && colorPreview) { function calculateDimensions() {
const { const {
top: spaceAbove, top: spaceAbove,
width, width,
bottom, bottom,
@ -72,11 +72,15 @@
y = bottom y = bottom
} }
x = (left + previewCenter) - (pickerWidth / 2) x = (left + previewCenter) - (220 / 2)
dimensions = { [positionSide]: y.toFixed(1), left: x.toFixed(1) } dimensions = { [positionSide]: y.toFixed(1), left: x.toFixed(1) }
} }
$: if(open && colorPreview) {
calculateDimensions()
}
$: previewStyle = buildStyle({ width, height, background: value }) $: previewStyle = buildStyle({ width, height, background: value })
$: errorPreviewStyle = buildStyle({ width, height }) $: errorPreviewStyle = buildStyle({ width, height })
$: pickerStyle = buildStyle({ $: pickerStyle = buildStyle({
@ -86,6 +90,8 @@
</script> </script>
<svelte:window on:resize={debounce(calculateDimensions, 200)} />
<div class="color-preview-container"> <div class="color-preview-container">
{#if !errorMsg} {#if !errorMsg}
<CheckedBackground borderRadius="3px" backgroundSize="8px"> <CheckedBackground borderRadius="3px" backgroundSize="8px">

View File

@ -1,9 +1,14 @@
<script> <script>
import {createEventDispatcher} from "svelte"
import {keyevents} from "../actions"
export let text = "" export let text = ""
export let selected = false export let selected = false
const dispatch = createEventDispatcher()
</script> </script>
<div class="flatbutton" class:selected on:click>{text}</div> <div class="flatbutton" tabindex="0" use:keyevents={{"Enter": () => dispatch("click")}} class:selected on:click>{text}</div>
<style> <style>
.flatbutton { .flatbutton {
@ -19,11 +24,14 @@
justify-content: center; justify-content: center;
align-items: center; align-items: center;
background: #f1f3f4; background: #f1f3f4;
outline-color: #003cb0;
outline-width: thin;
} }
.selected { .selected {
color: #ffffff; color: #ffffff;
background-color: #003cb0; background-color: #003cb0;
border: none; border: none;
outline: none;
} }
</style> </style>

View File

@ -1,6 +1,6 @@
<script> <script>
import { onMount, createEventDispatcher } from "svelte" import { onMount, createEventDispatcher } from "svelte"
import dragable from "./drag.js" import {drag, keyevents} from "../actions"
export let value = 1 export let value = 1
export let type = "hue" export let type = "hue"
@ -9,6 +9,10 @@
let slider let slider
let sliderWidth = 0 let sliderWidth = 0
let upperLimit = type === "hue" ? 360 : 1
let incrementFactor = type === "hue" ? 1 : 0.01
const isWithinLimit = value => value >= 0 && value <= upperLimit
function onSliderChange(mouseX, isDrag = false) { function onSliderChange(mouseX, isDrag = false) {
const { left, width } = slider.getBoundingClientRect() const { left, width } = slider.getBoundingClientRect()
@ -17,12 +21,29 @@
let percentageClick = (clickPosition / sliderWidth).toFixed(2) let percentageClick = (clickPosition / sliderWidth).toFixed(2)
if (percentageClick >= 0 && percentageClick <= 1) { if (percentageClick >= 0 && percentageClick <= 1) {
let value = type === "hue" ? 360 * percentageClick : percentageClick
let value = type === "hue" ? 360 * percentageClick : percentageClick
dispatch("change", { color: value, isDrag }) dispatch("change", { color: value, isDrag })
} }
} }
function handleLeftKey() {
let v = value - incrementFactor
if(isWithinLimit(v)) {
value = v
dispatch("change", { color: value })
}
}
function handleRightKey() {
let v = value + incrementFactor
if(isWithinLimit(v)) {
value = v
dispatch("change", { color: value })
}
}
$: thumbPosition = $: thumbPosition =
type === "hue" ? sliderWidth * (value / 360) : sliderWidth * value type === "hue" ? sliderWidth * (value / 360) : sliderWidth * value
@ -30,14 +51,16 @@
</script> </script>
<div <div
tabindex="0"
bind:this={slider} bind:this={slider}
use:keyevents={{37: handleLeftKey, 39: handleRightKey}}
bind:clientWidth={sliderWidth} bind:clientWidth={sliderWidth}
on:click={event => onSliderChange(event.clientX)} on:click={event => onSliderChange(event.clientX)}
class="color-format-slider" class="color-format-slider"
class:hue={type === 'hue'} class:hue={type === 'hue'}
class:alpha={type === 'alpha'}> class:alpha={type === 'alpha'}>
<div <div
use:dragable use:drag
on:drag={e => onSliderChange(e.detail, true)} on:drag={e => onSliderChange(e.detail, true)}
on:dragend on:dragend
class="slider-thumb" class="slider-thumb"
@ -54,6 +77,8 @@
margin: 10px 0px; margin: 10px 0px;
border: 1px solid #e8e8ef; border: 1px solid #e8e8ef;
cursor: pointer; cursor: pointer;
outline-color: #003cb0;
outline-width: thin;
} }
.hue { .hue {

View File

@ -2,6 +2,7 @@
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import CheckedBackground from "./CheckedBackground.svelte" import CheckedBackground from "./CheckedBackground.svelte"
import {keyevents} from "../actions"
export let hovered = false export let hovered = false
export let color = "#fff" export let color = "#fff"
@ -12,6 +13,8 @@
<div class="space"> <div class="space">
<CheckedBackground borderRadius="6px"> <CheckedBackground borderRadius="6px">
<div <div
tabindex="0"
use:keyevents={{"Enter": () => dispatch("click")}}
in:fade in:fade
class="swatch" class="swatch"
style={`background: ${color};`} style={`background: ${color};`}
@ -38,6 +41,8 @@
border: 1px solid #dedada; border: 1px solid #dedada;
height: 20px; height: 20px;
width: 20px; width: 20px;
outline-color: #003cb0;
outline-width: thin;
} }
.space { .space {

View File

@ -1,4 +1,4 @@
export const buildStyle = styles => { export function buildStyle(styles) {
let str = "" let str = ""
for (let s in styles) { for (let s in styles) {
if (styles[s]) { if (styles[s]) {
@ -12,3 +12,9 @@ export const buildStyle = styles => {
export const convertCamel = str => { export const convertCamel = str => {
return str.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`) return str.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`)
} }
export const debounce = (fn, milliseconds) => {
return () => {
setTimeout(fn, milliseconds)
}
}

View File

@ -1,2 +1,2 @@
import Colorpreview from "./Colorpreview.svelte" import Colorpreview from "./components/Colorpreview.svelte"
export default Colorpreview export default Colorpreview

View File

@ -144,13 +144,15 @@ export const hslaToHSVA = ([h, s, l, a = 1]) => {
export const hsvaToHexa = (hsva, asString = false) => { export const hsvaToHexa = (hsva, asString = false) => {
const [r, g, b, a] = hsvaToRgba(hsva) const [r, g, b, a] = hsvaToRgba(hsva)
const padSingle = hex => (hex.length === 1 ? `0${hex}` : hex)
const hexa = [r, g, b] let hexa = [r, g, b].map(v => {
.map(v => { let hex = Math.round(v).toString(16)
let hex = Math.round(v).toString(16) return padSingle(hex)
return hex.length === 1 ? `0${hex}` : hex })
})
.concat(Math.round(a * 255).toString(16)) let alpha = padSingle(Math.round(a * 255).toString(16))
hexa = [...hexa, alpha]
return asString ? `#${hexa.join("")}` : hexa return asString ? `#${hexa.join("")}` : hexa
} }