Files
windmill/frontend/src/lib/autosize.ts
T
Faton RamadaniandRuben Fiszel 2344077b34 feat(frontend): Generated UI editor + Schema Form complete refactor (#3835)
* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): add type editor

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): dnd

* feat(frontend): add missing error

* feat(frontend): fix build

* feat(frontend): fix build

* feat(frontend): clean up

* feat(frontend): fix layout

* feat(frontend): fix dark mode

* feat(frontend): Fix Dnd + resource s3 and objects

* feat(frontend): handle inner objets

* feat(frontend): wip

* feat(frontend): wip

* feat(frontend): wip

* fix(frontend): UI nits

* fix(frontend): fix type selection

* fix(frontend): Correctly handle subproperties

* fix(frontend): fix autosize + height

* fix(frontend): replace everywhere

* fix(frontend): fix dnd

* fix(frontend): fix list refresh + height issues

* fix(frontend): fix order

* fix(frontend): restore default behavior

* fix(frontend): Move the rename input

* fix(frontend): Fix rename wrt order array

* fix(frontend): Fix hub

* fix(frontend): Fix renam

* fix(frontend): Fix UI Nits + fix SchemaForm order

* fix(frontend): Handle nested order

* fix(frontend): Fix saving properties

* fix(frontend): Improve approval form

* feat(frontend): done

* feat(frontend): merge main

* feat(frontend): done

* feat(frontend): wip

* feat(frontend): SchemaFormDND

* feat(frontend): SchemaFormDND wip

* feat(frontend): improve displayWebhookWarning

* feat(frontend): DND done

* feat(frontend): DND done

* feat(frontend): improve name

* feat(frontend): improve dnd

* feat(frontend): done

* first batch

* new try

* nits

* all

* drag handle in more places

* push everything

* migrate all

* all

* fix

* fix

* fix henri bug

---------

Co-authored-by: Ruben Fiszel <ruben@rubenfiszel.com>
2024-06-12 18:37:18 +02:00

94 lines
1.9 KiB
TypeScript

export const action = (node) => {
/* Constants */
const update = new Event('update')
/* Functions */
const init = () => {
addStyles()
observeElement()
addEventListeners()
setInitialHeight()
}
const dispatchUpdateEvent = () => {
node.dispatchEvent(update)
}
const setInitialHeight = () => {
let height = 0
const style = window.getComputedStyle(node)
const visible = style?.getPropertyValue('visibility') === 'hidden'
if (visible === false) {
return
}
if (node.value) {
height = node.scrollHeight
} else {
if (node.placeholder) {
node.value = node.placeholder
height = node.scrollHeight
node.value = ''
} else {
node.value = '|'
node.style.height = '0px'
height = node.scrollHeight
node.value = ''
}
}
node.style.height = height + 'px'
}
const setHeight = () => {
node.style.height = 'auto'
node.style.height = Math.max(node.scrollHeight ?? 0, 30) + 2 + 'px'
}
const addStyles = () => {
node.style.boxSizing = 'border-box'
}
const observeElement = () => {
let elementPrototype = Object.getPrototypeOf(node)
let descriptor = Object.getOwnPropertyDescriptor(elementPrototype, 'value')
Object.defineProperty(node, 'value', {
get: function () {
return descriptor?.get?.apply(this, arguments as any)
},
set: function () {
descriptor?.set?.apply(this, arguments as any)
dispatchUpdateEvent()
}
})
}
const addEventListeners = () => {
node.addEventListener('input', (e) => {
dispatchUpdateEvent()
})
node.addEventListener('update', setHeight)
}
const removeEventListeners = () => {
node.removeEventListener('input', dispatchUpdateEvent)
node.removeEventListener('update', setHeight)
}
if (node.tagName.toLowerCase() !== 'textarea') {
throw new Error('svelte-textarea-auto-height can only be used on textarea elements.')
} else {
init()
return {
destroy() {
removeEventListeners()
}
}
}
}
export default action