2021-01-06 11:13:30 +01:00
|
|
|
import { get } from "svelte/store"
|
|
|
|
import { builderStore } from "../store"
|
|
|
|
|
|
|
|
const selectedComponentWidth = 2
|
|
|
|
const selectedComponentColor = "#4285f4"
|
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
/**
|
2021-01-06 11:13:30 +01:00
|
|
|
* Helper to build a CSS string from a style object.
|
2020-11-24 10:31:54 +01:00
|
|
|
*/
|
2021-01-06 11:13:30 +01:00
|
|
|
const buildStyleString = (styleObject, customStyles) => {
|
2020-11-17 13:08:24 +01:00
|
|
|
let str = ""
|
2021-01-04 19:39:17 +01:00
|
|
|
Object.entries(styleObject).forEach(([style, value]) => {
|
2020-11-24 10:31:54 +01:00
|
|
|
if (style && value != null) {
|
2020-11-17 13:08:24 +01:00
|
|
|
str += `${style}: ${value}; `
|
|
|
|
}
|
|
|
|
})
|
2021-01-06 11:13:30 +01:00
|
|
|
return str + (customStyles || "")
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies styles to enrich the builder preview.
|
|
|
|
* Applies styles to highlight the selected component, and allows pointer
|
|
|
|
* events for any selectable components (overriding the blanket ban on pointer
|
|
|
|
* events in the iframe HTML).
|
|
|
|
*/
|
|
|
|
const addBuilderPreviewStyles = (styleString, componentId, selectable) => {
|
|
|
|
let str = styleString
|
|
|
|
|
|
|
|
// Apply extra styles if we're in the builder preview
|
|
|
|
const state = get(builderStore)
|
|
|
|
if (state.inBuilder) {
|
|
|
|
// Allow pointer events and always enable cursor
|
|
|
|
if (selectable) {
|
|
|
|
str += ";pointer-events: all !important; cursor: pointer !important;"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Highlighted selected element
|
|
|
|
if (componentId === state.selectedComponentId) {
|
2021-01-06 17:03:45 +01:00
|
|
|
str += `;border: ${selectedComponentWidth}px solid ${selectedComponentColor} !important;`
|
2021-01-06 11:13:30 +01:00
|
|
|
}
|
2021-01-04 19:39:17 +01:00
|
|
|
}
|
2021-01-06 11:13:30 +01:00
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Svelte action to apply correct component styles.
|
2021-01-06 11:13:30 +01:00
|
|
|
* This also applies handlers for selecting components from the builder preview.
|
2020-11-17 13:08:24 +01:00
|
|
|
*/
|
|
|
|
export const styleable = (node, styles = {}) => {
|
2020-11-24 10:31:54 +01:00
|
|
|
let applyNormalStyles
|
|
|
|
let applyHoverStyles
|
2021-01-06 11:13:30 +01:00
|
|
|
let selectComponent
|
|
|
|
|
|
|
|
// Kill JS even bubbling
|
|
|
|
const blockEvent = event => {
|
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
return false
|
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
// Creates event listeners and applies initial styles
|
|
|
|
const setupStyles = newStyles => {
|
2021-01-06 11:13:30 +01:00
|
|
|
const componentId = newStyles.id
|
|
|
|
const selectable = newStyles.allowSelection
|
2021-01-04 19:39:17 +01:00
|
|
|
const customStyles = newStyles.custom
|
2021-01-06 11:13:30 +01:00
|
|
|
const normalStyles = newStyles.normal
|
2020-11-24 10:31:54 +01:00
|
|
|
const hoverStyles = {
|
|
|
|
...normalStyles,
|
|
|
|
...newStyles.hover,
|
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2021-01-06 11:13:30 +01:00
|
|
|
// Applies a style string to a DOM node, enriching it for the builder
|
|
|
|
// preview
|
|
|
|
const applyStyles = styleString => {
|
|
|
|
node.style = addBuilderPreviewStyles(styleString, componentId, selectable)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applies the "normal" style definition
|
2020-11-24 10:31:54 +01:00
|
|
|
applyNormalStyles = () => {
|
2021-01-06 11:13:30 +01:00
|
|
|
applyStyles(buildStyleString(normalStyles, customStyles))
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2021-01-06 11:13:30 +01:00
|
|
|
// Applies any "hover" styles as well as the base "normal" styles
|
2020-11-24 10:31:54 +01:00
|
|
|
applyHoverStyles = () => {
|
2021-01-06 11:13:30 +01:00
|
|
|
applyStyles(buildStyleString(hoverStyles, customStyles))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler to select a component in the builder when clicking it in the
|
|
|
|
// builder preview
|
|
|
|
selectComponent = event => {
|
|
|
|
builderStore.actions.selectComponent(newStyles.id)
|
|
|
|
return blockEvent(event)
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
// Add listeners to toggle hover styles
|
|
|
|
node.addEventListener("mouseover", applyHoverStyles)
|
|
|
|
node.addEventListener("mouseout", applyNormalStyles)
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2021-01-06 11:13:30 +01:00
|
|
|
// Add builder preview click listener
|
|
|
|
if (get(builderStore).inBuilder) {
|
|
|
|
node.addEventListener("click", selectComponent, false)
|
|
|
|
|
|
|
|
// Kill other interaction events
|
|
|
|
node.addEventListener("mousedown", blockEvent)
|
|
|
|
node.addEventListener("mouseup", blockEvent)
|
|
|
|
}
|
|
|
|
|
2020-11-24 10:31:54 +01:00
|
|
|
// Apply initial normal styles
|
|
|
|
applyNormalStyles()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the current event listeners
|
|
|
|
const removeListeners = () => {
|
|
|
|
node.removeEventListener("mouseover", applyHoverStyles)
|
|
|
|
node.removeEventListener("mouseout", applyNormalStyles)
|
2021-01-06 11:13:30 +01:00
|
|
|
|
|
|
|
// Remove builder preview click listener
|
|
|
|
if (get(builderStore).inBuilder) {
|
|
|
|
node.removeEventListener("click", selectComponent)
|
|
|
|
node.removeEventListener("mousedown", blockEvent)
|
|
|
|
node.removeEventListener("mouseup", blockEvent)
|
|
|
|
}
|
2020-11-24 10:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply initial styles
|
|
|
|
setupStyles(styles)
|
2020-11-17 13:08:24 +01:00
|
|
|
|
|
|
|
return {
|
2020-11-24 10:31:54 +01:00
|
|
|
// Clean up old listeners and apply new ones on update
|
|
|
|
update: newStyles => {
|
|
|
|
removeListeners()
|
|
|
|
setupStyles(newStyles)
|
|
|
|
},
|
|
|
|
// Clean up listeners when component is destroyed
|
2020-11-17 13:08:24 +01:00
|
|
|
destroy: () => {
|
2020-11-24 10:31:54 +01:00
|
|
|
removeListeners()
|
2020-11-17 13:08:24 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|