dom: batch dom operations that trigger layout calculation
panel: store raw scroll when scrolling and use that value when the editor needs it (avoids many layout trashing)
This commit is contained in:
parent
2225a3f760
commit
876c3277b2
8 changed files with 137 additions and 98 deletions
|
|
@ -77,7 +77,7 @@ function updateWidget(widget, options={}) {
|
|||
|
||||
// save state
|
||||
if (stateManager.queueCounter === 0) {
|
||||
scrollState = {}
|
||||
scrollState = {} // scrollbale modals don't store their scroll state in their value
|
||||
}
|
||||
stateManager.incrementQueue()
|
||||
for (let w of removedWidgets) {
|
||||
|
|
@ -89,8 +89,8 @@ function updateWidget(widget, options={}) {
|
|||
stateManager.pushValueState(id, value)
|
||||
if (valueProp !== '' && valueProp !== undefined) stateManager.pushValueOldProp(id, valueProp)
|
||||
}
|
||||
if (widgetManager.scrollingWidgets.indexOf(w.hash) > -1 && w.scroll) {
|
||||
var s = w.scroll()
|
||||
if (w.getProp('scroll') && w.getProp('type') === 'modal') {
|
||||
var s = w.getScroll()
|
||||
if (s[0] !== 0 || s[1] !== 0) {
|
||||
scrollState[w.getProp('id')] = s
|
||||
}
|
||||
|
|
@ -142,7 +142,7 @@ function updateWidget(widget, options={}) {
|
|||
if (stateManager.queueCounter === 0) {
|
||||
for (let id in scrollState) {
|
||||
for (let w of widgetManager.getWidgetById(id)) {
|
||||
if (w.scroll) w.scroll(scrollState[id])
|
||||
if (w.getProp('scroll')) w.setScroll(scrollState[id])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,37 +1,42 @@
|
|||
var fastdom = require('fastdom')
|
||||
var elements = []
|
||||
|
||||
function checkResizes(context, force){
|
||||
// Iterate over all elements to which the 'resize' event is bound.
|
||||
var resizedElems = [],
|
||||
cachedStyles = []
|
||||
for (let i = elements.length - 1; i >= 0; i--) {
|
||||
var elem = elements[i]
|
||||
fastdom.measure(()=>{
|
||||
// Iterate over all elements to which the 'resize' event is bound.
|
||||
let resizedElems = [],
|
||||
cachedStyles = []
|
||||
for (let i = elements.length - 1; i >= 0; i--) {
|
||||
var elem = elements[i]
|
||||
|
||||
if (!context.contains(elem)) continue
|
||||
if (!context.contains(elem)) continue
|
||||
|
||||
var style = window.getComputedStyle(elem),
|
||||
width = parseInt(style['width']) - parseInt(style['padding-left']) - parseInt(style['padding-right']),
|
||||
height = parseInt(style['height']) - parseInt(style['padding-top']) - parseInt(style['padding-bottom'])
|
||||
var style = window.getComputedStyle(elem),
|
||||
width = parseInt(style['width']) - parseInt(style['padding-left']) - parseInt(style['padding-right']),
|
||||
height = parseInt(style['height']) - parseInt(style['padding-top']) - parseInt(style['padding-bottom'])
|
||||
|
||||
if (!width || !height) continue
|
||||
if (!width || !height) continue
|
||||
|
||||
// If element size has changed since the last time, update the element
|
||||
// data store and trigger the 'resize' event.
|
||||
if (force || width != elem.resizedataw || height != elem.resizedatah) {
|
||||
elem.resizedataw = width
|
||||
elem.resizedatah = height
|
||||
resizedElems.push(elem)
|
||||
cachedStyles.push(style)
|
||||
// If element size has changed since the last time, update the element
|
||||
// data store and trigger the 'resize' event.
|
||||
if (force || width != elem.resizedataw || height != elem.resizedatah) {
|
||||
elem.resizedataw = width
|
||||
elem.resizedatah = height
|
||||
resizedElems.push(elem)
|
||||
cachedStyles.push(style)
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = resizedElems.length - 1; i >= 0; i--) {
|
||||
resizedElems[i]._resize_widget.trigger('resize', {
|
||||
width: resizedElems[i].resizedataw,
|
||||
height: resizedElems[i].resizedatah,
|
||||
style:cachedStyles[i],
|
||||
stopPropagation: true
|
||||
fastdom.mutate(()=>{
|
||||
for (let i = resizedElems.length - 1; i >= 0; i--) {
|
||||
resizedElems[i]._resize_widget.trigger('resize', {
|
||||
width: resizedElems[i].resizedataw,
|
||||
height: resizedElems[i].resizedatah,
|
||||
style:cachedStyles[i],
|
||||
stopPropagation: true
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
var UiWidget = require('./ui-widget'),
|
||||
keyboardJS = require('keyboardjs/dist/keyboard.min.js'),
|
||||
html = require('nanohtml'),
|
||||
fastdom = require('fastdom'),
|
||||
Tab, Root
|
||||
|
||||
class UiDragResize extends UiWidget {
|
||||
|
|
@ -154,9 +155,10 @@ class UiDragResize extends UiWidget {
|
|||
this.initWidth = this.width
|
||||
this.initHeight = this.height
|
||||
|
||||
this.widgets[0].parentNode.appendChild(this.container)
|
||||
this.mounted = true
|
||||
|
||||
fastdom.mutate(()=>{
|
||||
this.widgets[0].parentNode.appendChild(this.container)
|
||||
this.mounted = true
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -172,24 +174,33 @@ class UiDragResize extends UiWidget {
|
|||
]
|
||||
handlesVisibility.push(handlesVisibility[1] && handlesVisibility[2]) // se
|
||||
|
||||
for (var i in handlesVisibility) {
|
||||
this.handles[i].style.display = handlesVisibility[i] ? '' : 'none'
|
||||
}
|
||||
fastdom.measure(()=>{
|
||||
|
||||
var lefts = widgets.map(w => w.container.offsetLeft),
|
||||
tops = widgets.map(w => w.container.offsetTop),
|
||||
rights = widgets.map(w => w.container.offsetLeft + w.container.offsetWidth),
|
||||
bottoms = widgets.map(w => w.container.offsetTop + w.container.offsetHeight)
|
||||
var lefts = widgets.map(w => w.container.offsetLeft),
|
||||
tops = widgets.map(w => w.container.offsetTop),
|
||||
rights = widgets.map(w => w.container.offsetLeft + w.container.offsetWidth),
|
||||
bottoms = widgets.map(w => w.container.offsetTop + w.container.offsetHeight)
|
||||
|
||||
this.left = Math.min(...lefts) / PXSCALE
|
||||
this.top = Math.min(...tops) / PXSCALE
|
||||
this.width = (Math.max(...rights) - Math.min(...lefts)) / PXSCALE
|
||||
this.height = (Math.max(...bottoms) - Math.min(...tops)) / PXSCALE
|
||||
this.left = Math.min(...lefts) / PXSCALE
|
||||
this.top = Math.min(...tops) / PXSCALE
|
||||
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
|
||||
fastdom.mutate(()=>{
|
||||
|
||||
for (var i in handlesVisibility) {
|
||||
this.handles[i].style.display = handlesVisibility[i] ? '' : 'none'
|
||||
}
|
||||
|
||||
this.updateCss()
|
||||
|
||||
// this.cssTransform = widget.cssTransform || 'none'
|
||||
// this.cssTransformOrigin = widget.cssTransformOrigin
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
this.updateCss()
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ var UiWidget = require('./ui-widget'),
|
|||
scriptGlobals = ScriptVm.globals,
|
||||
editors = {}, editorModes = {
|
||||
javascript: require('brace/mode/javascript')
|
||||
}
|
||||
},
|
||||
fastdom = require('fastdom')
|
||||
|
||||
function createEditor(name, language, syntaxChecker) {
|
||||
var el = html`<div id="#editor${name}"></div>`
|
||||
|
|
@ -156,23 +157,34 @@ class UiInspectorField extends UiWidget {
|
|||
colorData = this.widget.constructor.cssVariables.find(x=>x.js === this.name),
|
||||
val = 'transparent'
|
||||
|
||||
try {
|
||||
val = chroma(style.getPropertyValue(colorData.css).trim()).hex('rgba')
|
||||
} catch(e) {}
|
||||
fastdom.measure(()=>{
|
||||
try {
|
||||
val = chroma(style.getPropertyValue(colorData.css).trim()).hex('rgba')
|
||||
} catch(e) {}
|
||||
fastdom.mutate(()=>{
|
||||
this.container.appendChild(html`
|
||||
<osc-inspector-color type="text" name="${this.name}" value="${val}" style="--color-picker-value: ${val}"></osc-inspector-color>
|
||||
`)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
} else if (stringValue === 'auto' && this.widget.constructor.cssVariables.some(x=>x.js === this.name)) {
|
||||
|
||||
let style = window.getComputedStyle(this.widget.container),
|
||||
data = this.widget.constructor.cssVariables.find(x=>x.js === this.name),
|
||||
val = style.getPropertyValue(data.css).trim()
|
||||
fastdom.measure(()=>{
|
||||
let style = window.getComputedStyle(this.widget.container),
|
||||
data = this.widget.constructor.cssVariables.find(x=>x.js === this.name),
|
||||
val = style.getPropertyValue(data.css).trim()
|
||||
|
||||
if (data.toJs) val = data.toJs(val)
|
||||
if (data.toJs) val = data.toJs(val)
|
||||
fastdom.mutate(()=>{
|
||||
this.container.appendChild(html`
|
||||
<div class="computed-value">${val}</div>
|
||||
`)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
this.container.appendChild(html`<div class="computed-value">${val}</div>`)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ var UiWidget = require('./ui-widget'),
|
|||
morph = require('nanomorph'),
|
||||
{defaults} = require('../widgets/'),
|
||||
html = require('nanohtml'),
|
||||
raw = require('nanohtml/raw')
|
||||
raw = require('nanohtml/raw'),
|
||||
fastdom = require('fastdom')
|
||||
|
||||
|
||||
|
||||
|
|
@ -56,10 +57,16 @@ class UiInspector extends UiWidget {
|
|||
|
||||
var name = node.getAttribute('name')
|
||||
|
||||
|
||||
var textarea = DOM.get(this.container, `textarea[name="${name}"]`)
|
||||
if (textarea) {
|
||||
textarea[0].value = !node.classList.contains('on')
|
||||
DOM.dispatchEvent(textarea[0], 'change')
|
||||
fastdom.measure(()=>{
|
||||
var bool = !node.classList.contains('on')
|
||||
fastdom.mutate(()=>{
|
||||
textarea[0].value = bool
|
||||
DOM.dispatchEvent(textarea[0], 'change')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
} else if (node.tagName === 'OSC-INSPECTOR-COLOR') {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
var Widget = require('./widget'),
|
||||
resize = require('../../events/resize'),
|
||||
canvasQueue = require('./queue'),
|
||||
html = require('nanohtml')
|
||||
html = require('nanohtml'),
|
||||
fastdom = require('fastdom')
|
||||
|
||||
|
||||
class Canvas extends Widget {
|
||||
|
|
@ -69,11 +70,12 @@ class Canvas extends Widget {
|
|||
this.height = height
|
||||
this.width = width
|
||||
|
||||
this.canvas.setAttribute('width', width * ratio)
|
||||
this.canvas.setAttribute('height', height * ratio)
|
||||
|
||||
this.container.style.setProperty('--widget-height', this.height + 'rem')
|
||||
this.container.style.setProperty('--widget-width', this.width + 'rem')
|
||||
// fastdom.mutate(()=>{
|
||||
this.canvas.setAttribute('width', width * ratio)
|
||||
this.canvas.setAttribute('height', height * ratio)
|
||||
this.container.style.setProperty('--widget-height', this.height + 'rem')
|
||||
this.container.style.setProperty('--widget-width', this.width + 'rem')
|
||||
// })
|
||||
this.parsersLocalScope.height = this.height
|
||||
this.parsersLocalScope.width = this.width
|
||||
|
||||
|
|
|
|||
|
|
@ -134,28 +134,30 @@ class Panel extends Container() {
|
|||
}
|
||||
|
||||
if (this.props.type !== 'modal') {
|
||||
|
||||
this.value = [0, 0]
|
||||
this.scrollWidth = 1
|
||||
this.scrollHeight = 1
|
||||
setTimeout(()=>{
|
||||
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
|
||||
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
|
||||
})
|
||||
this.scrollTimeout = null
|
||||
this.widget.addEventListener('scroll', ()=>{
|
||||
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
|
||||
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
|
||||
var x = this.widget.scrollLeft / this.scrollWidth || 0,
|
||||
y = this.widget.scrollTop / this.scrollHeight || 0
|
||||
if (iOS13) {
|
||||
if (this.iosScrollbars.horizontal._scrollable) this.iosScrollbars.horizontal.setValue(x)
|
||||
if (this.iosScrollbars.vertical._scrollable) this.iosScrollbars.vertical.setValue(y)
|
||||
}
|
||||
this.setValue([x, y], {sync: true, send:true})
|
||||
})
|
||||
|
||||
}
|
||||
this.scroll = [0, 0]
|
||||
this.scrollWidth = 1
|
||||
this.scrollHeight = 1
|
||||
setTimeout(()=>{
|
||||
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
|
||||
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
|
||||
})
|
||||
this.widget.addEventListener('scroll', ()=>{
|
||||
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
|
||||
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
|
||||
this.scroll = [this.widget.scrollLeft, this.widget.scrollTop]
|
||||
var x = this.widget.scrollLeft / this.scrollWidth || 0,
|
||||
y = this.widget.scrollTop / this.scrollHeight || 0
|
||||
if (iOS13) {
|
||||
if (this.iosScrollbars.horizontal._scrollable) this.iosScrollbars.horizontal.setValue(x)
|
||||
if (this.iosScrollbars.vertical._scrollable) this.iosScrollbars.vertical.setValue(y)
|
||||
}
|
||||
if (this.props.type !== 'modal') {
|
||||
this.setValue([x, y], {sync: true, send:true})
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -293,8 +295,7 @@ class Panel extends Container() {
|
|||
this.value = v
|
||||
clearTimeout(this.scrollTimeout)
|
||||
this.scrollTimeout = setTimeout(()=>{
|
||||
this.widget.scrollLeft = v[0] * this.scrollWidth
|
||||
this.widget.scrollTop = v[1] * this.scrollHeight
|
||||
this.setScroll(v[0] * this.scrollWidth, v[1] * this.scrollHeight)
|
||||
})
|
||||
|
||||
if (options.send) this.sendValue()
|
||||
|
|
@ -302,14 +303,16 @@ class Panel extends Container() {
|
|||
}
|
||||
}
|
||||
|
||||
scroll(s) {
|
||||
getScroll() {
|
||||
|
||||
if (!s) {
|
||||
return [this.widget.scrollLeft, this.widget.scrollTop]
|
||||
} else {
|
||||
if (s[0] !== undefined) this.widget.scrollLeft = s[0]
|
||||
if (s[1] !== undefined) this.widget.scrollTop = s[1]
|
||||
}
|
||||
return this.scroll
|
||||
|
||||
}
|
||||
|
||||
setScroll(x, y) {
|
||||
|
||||
if (x !== undefined) this.widget.scrollLeft = this.scroll[0] = x
|
||||
if (y !== undefined) this.widget.scrollTop = this.scroll[1] = y
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue