custom color picker

This commit is contained in:
bordun 2020-02-27 14:35:08 +01:00
parent c67c2b4c50
commit 7b8af3eeea
53 changed files with 988 additions and 519 deletions

View file

@ -0,0 +1,114 @@
var UiWidget = require('./ui-widget'),
Rgb = require('../widgets/pads/rgb'),
UiModal = require('./ui-modal'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
chroma = require('chroma-js'),
resize = require('../events/resize'),
locales = require('../locales')
class ColorPicker extends UiWidget {
constructor() {
super({})
this.name = ''
this.value = ''
this.rgb = new Rgb({props: {
...Rgb.defaults()._props(),
type: 'rgb',
label: false,
width: 250,
height: 200,
alphaStroke: 0,
snap: true
// height: '100%',
}, parent: this})
this.rgb.container.classList.add('not-editable')
this.modal = new UiModal({
closable: true,
title: '',
hide: true,
content: html`
<div class="color-picker-modal">
${this.rgb.container}
<div class="actions">
<div class="btn color-apply">${locales('inspector_color_apply')}</div>
<div class="btn color-confirm">${locales('inspector_color_confirm')}</div>
</div>
</div>
`,
enterKey: this.confirm.bind(this)
})
DOM.get(this.modal.container, '.color-confirm')[0].addEventListener('click', this.confirm.bind(this))
DOM.get(this.modal.container, '.color-apply')[0].addEventListener('click', this.apply.bind(this))
this.rgb.on('change', (e)=>{
e.stopPropagation = true
})
// this.rgb.on('dragend', (e)=>{
// this.value = chroma(this.rgb.value).hex()
// this.trigger('change')
// })
this.opened = 0
}
open() {
this.modal.open()
resize.check(this.rgb.container)
this.opened = 1
}
close() {
this.modal.close()
this.setName()
this.opened = 0
}
apply() {
this.value = chroma(this.rgb.value).hex()
this.trigger('change')
}
confirm() {
this.value = chroma(this.rgb.value).hex()
this.trigger('change')
this.close()
}
isVisible() {
return true
}
setName(n = '') {
this.name = n
}
setValue(v) {
this.rgb.setValue(chroma(v).rgb())
}
}
module.exports = ColorPicker

View file

@ -14,7 +14,6 @@ class UiDragResize extends UiWidget {
this.container = html`
<div id="drag-resize">
<div class="position-handle"></div>
<div class="helper"></div>
<div class="no-widget-select handle nw" data-action="left,top"></div>
<div class="no-widget-select handle e" data-action="width"></div>
@ -42,12 +41,13 @@ class UiDragResize extends UiWidget {
this.on('draginit', (event)=>{
var node = event.firstTarget
if (!this.handles.includes(node)) return
if (event.shiftKey || !this.handles.includes(node)) return
this.initLeft = this.left
this.initTop = this.top
this.initWidth = this.width
this.initHeight = this.height
this.matrix = ''
this.container.classList.add('dragging')
this.dragging = node.getAttribute('data-action').split(',')
@ -94,15 +94,7 @@ class UiDragResize extends UiWidget {
}, {element: this.container})
// handles appear on top of modal widgets
// very special case fix here...
this.watchedModalWidget = null
this.checkModalState = (e)=>{
if (e.widget !== this.watchedModalWidget) return
console.log(e.widget.value)
this.container.style.display = e.widget.value ? 'none' : ''
}
// this.handles[0]._ignore_css_transforms = true
}
@ -110,13 +102,6 @@ class UiDragResize extends UiWidget {
if (!this.mounted) return
if (this.watchedModalWidget) {
// reset modal widget fix
this.watchedModalWidget.off('change', this.checkModalState)
this.watchedModalWidget = null
this.container.style.display = ''
}
this.container.parentNode.removeChild(this.container)
this.mounted = false
@ -129,13 +114,6 @@ class UiDragResize extends UiWidget {
var widget = widgets[0]
if (widget instanceof Modal) {
// modal widget fix
this.watchedModalWidget = widget
widget.on('change', this.checkModalState)
if (widget.getValue()) this.container.style.display = 'none'
}
if (
widget instanceof Tab ||
widget instanceof Root ||
@ -153,8 +131,6 @@ class UiDragResize extends UiWidget {
this.handles[i].style.display = handlesVisibility[i] ? '' : 'none'
}
var parent = widget.parentNode,
lefts = widgets.map(w => w.container.offsetLeft),
tops = widgets.map(w => w.container.offsetTop),
@ -166,6 +142,9 @@ class UiDragResize extends UiWidget {
this.width = (Math.max(...rights) - Math.min(...lefts)) / PXSCALE
this.height = (Math.max(...bottoms) - Math.min(...tops)) / PXSCALE
// this.cssTransform = widget.cssTransform || 'none'
// this.cssTransformOrigin = widget.cssTransformOrigin
this.updateCss()
parent.appendChild(this.container)
@ -184,6 +163,9 @@ class UiDragResize extends UiWidget {
this.container.style.setProperty('--' + k, val + 'rem')
// this.container.style.transform = this.cssTransform
// this.container.style.transformOrigin = this.cssTransformOrigin
}
}

View file

@ -4,13 +4,15 @@ var locales = require('../locales'),
ipc = require('../ipc'),
doubleClick = require('../events/double-click'),
{icon} = require('./utils'),
UiModal = require('./ui-modal')
UiModal = require('./ui-modal'),
keyboardJS = require('keyboardjs')
module.exports = function UiFilebrowser(options, callback) {
var save = options.save,
saveInputFocus = undefined
saveInputFocus = undefined,
previousKbContext = keyboardJS.getContext()
var popup = new UiModal({
closable: true,
@ -192,6 +194,7 @@ module.exports = function UiFilebrowser(options, callback) {
ipc.off('listDir', null, popup)
document.removeEventListener('keydown', keyDownHandler)
document.removeEventListener('keyup', keyUpHandler)
keyboardJS.setContext(previousKbContext)
})
browser.appendChild(ariane)
@ -202,5 +205,6 @@ module.exports = function UiFilebrowser(options, callback) {
ipc.send('listDir', {extension: options.extension, path: options.directory ? [options.directory] : null})
popup.open()
keyboardJS.setContext('filebrowser')
}

View file

@ -101,7 +101,7 @@ class UiInspectorField extends UiWidget {
val = chroma(style.getPropertyValue('--' + this.name.replace(/([A-Z])/g, (a,s)=>'-' + s.toLowerCase())).trim()).hex('rgb')
} catch(e) {console.log(e)}
let picker = html`<input class="osc-inspector-color" type="color" name="${this.name}" value="${val}"/>`
let picker = html`<osc-inspector-color type="text" name="${this.name}" value="${val}" style="--color-picker-value: ${val}"></osc-inspector-color>`
this.container.appendChild(picker)

View file

@ -1,10 +1,14 @@
var UiWidget = require('./ui-widget'),
UiInspectorField = require('./ui-inspector-field'),
UiColorPicker = require('./ui-colorpicker'),
morph = require('nanomorph'),
{widgets, defaults} = require('../widgets/'),
{deepCopy} = require('../utils'),
html = require('nanohtml')
class UiInspector extends UiWidget {
constructor(options) {
@ -31,6 +35,15 @@ class UiInspector extends UiWidget {
})
this.colorPicker = new UiColorPicker()
this.colorPicker.on('change', ()=>{
var textarea = DOM.get(this.container, `textarea[name="${this.colorPicker.name}"]`)
if (textarea) {
textarea[0].value = this.colorPicker.value
DOM.dispatchEvent(textarea[0], 'change')
}
})
this.container.addEventListener('click', (e)=>{
var node = e.target
@ -45,6 +58,12 @@ class UiInspector extends UiWidget {
DOM.dispatchEvent(textarea[0], 'change')
}
} else if (node.tagName === 'OSC-INSPECTOR-COLOR') {
this.colorPicker.setName(node.getAttribute('name'))
this.colorPicker.setValue(node.getAttribute('value'))
this.colorPicker.open()
}
})
@ -67,6 +86,7 @@ class UiInspector extends UiWidget {
this.container.innerHTML = ''
this.mounted = false
this.clearTimeout = null
this.colorPicker.close()
})
}
@ -124,11 +144,6 @@ class UiInspector extends UiWidget {
} else {
// if (widget.parent !== widgetManager && widget.parent.getProp('type') === 'strip') {
// // special case for widgets in strips
// if (propName === 'top' || propName === 'left' || propName === (widget.parent.getProp('horizontal') ? 'height' : 'width')) continue
// }
field = new UiInspectorField({
parent: this,
widget: widget,
@ -137,6 +152,7 @@ class UiInspector extends UiWidget {
default: defaults[w.props.type][propName],
tabIndex: tabIndex++
}).container
if (!field) continue
}
@ -146,6 +162,7 @@ class UiInspector extends UiWidget {
content.appendChild(field)
}
}
if (category) content.appendChild(category)
@ -159,6 +176,17 @@ class UiInspector extends UiWidget {
this.mounted = true
if (this.colorPicker.opened) {
var picker = DOM.get(this.container, `osc-inspector-color[name="${this.colorPicker.name}"]`)
if (picker) {
this.colorPicker.setValue(picker[0].getAttribute('value'))
} else {
this.colorPicker.close()
}
}
}
onChange(event) {

View file

@ -2,8 +2,9 @@ var UiWidget = require('./ui-widget'),
{icon} = require('./utils'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
SINGLETON = null,
CONTAINER = null
MODAL_SINGLETON = null,
MODAL_CONTAINER = null,
OPENED_MODALS = 0
class UiModal extends UiWidget {
@ -11,12 +12,12 @@ class UiModal extends UiWidget {
super(options)
if (!CONTAINER) CONTAINER = DOM.get('osc-modal-container')[0]
if (!MODAL_CONTAINER) MODAL_CONTAINER = DOM.get('osc-modal-container')[0]
if (options.closable) {
if (SINGLETON) SINGLETON.close()
SINGLETON = this
if (MODAL_SINGLETON) MODAL_SINGLETON.close()
MODAL_SINGLETON = this
}
@ -77,7 +78,11 @@ class UiModal extends UiWidget {
if (this.escKey) document.removeEventListener('keydown', this.escKeyHandler)
if (this.enterKey) document.removeEventListener('keydown', this.enterKeyHandler)
CONTAINER.removeChild(this.container)
if (--OPENED_MODALS === 0) MODAL_CONTAINER.classList.remove('backdrop')
MODAL_CONTAINER.removeChild(this.container)
this.trigger('close')
@ -90,7 +95,10 @@ class UiModal extends UiWidget {
if (this.escKey) document.addEventListener('keydown', this.escKeyHandler)
if (this.enterKey) document.addEventListener('keydown', this.enterKeyHandler)
CONTAINER.appendChild(this.container)
if (++OPENED_MODALS === 1) MODAL_CONTAINER.classList.add('backdrop')
MODAL_CONTAINER.appendChild(this.container)
this.trigger('open')
}

View file

@ -62,6 +62,10 @@ class UiTree extends UiWidget {
})
}
widgetManager.on('prop-changed', (e)=>{
console.log(e)
})
}
clear() {