mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 16:28:31 +00:00
489 lines
14 KiB
Vue
489 lines
14 KiB
Vue
<template>
|
|
<div
|
|
ref="elementPreviewRef"
|
|
:key="element.id"
|
|
class="element-preview"
|
|
:class="{
|
|
'element-preview--active': isSelected,
|
|
'element-preview--parent-of-selected': isParentOfSelectedElement,
|
|
'element-preview--in-error': !!errorMessage,
|
|
'element-preview--on-top': isSelected && isAboveThreshold,
|
|
'element-preview--not-visible':
|
|
!isVisible && !isSelected && !isParentOfSelectedElement,
|
|
'element-preview--dragged': isDragged,
|
|
'element-preview--drop-before': isDropTarget && dropPosition === 'before',
|
|
'element-preview--drop-after': isDropTarget && dropPosition === 'after',
|
|
}"
|
|
:draggable="isDraggable"
|
|
@click="onSelect"
|
|
@mousedown="canUpdate && isSelected && onDragHandleMouseDown($event)"
|
|
@dragstart.stop="onDragStart"
|
|
@dragend="onDragEnd"
|
|
@dragenter="onDragEnter"
|
|
@dragover="onDragOver"
|
|
@dragleave="onDragLeave"
|
|
@drop="onDrop"
|
|
>
|
|
<div v-if="isSelected" class="element-preview__tags">
|
|
<div class="element-preview__name-tag">
|
|
{{ elementType.name }}
|
|
<i v-if="!isVisible" class="iconoir-eye-off" />
|
|
</div>
|
|
<div v-if="errorMessage" class="element-preview__error-tag">
|
|
{{ errorMessage }}
|
|
</div>
|
|
</div>
|
|
<InsertElementButton
|
|
v-show="isSelected"
|
|
v-if="canCreate"
|
|
class="element-preview__insert element-preview__insert--top"
|
|
@click="showAddElementModal(DIRECTIONS.BEFORE)"
|
|
/>
|
|
<ElementMenu
|
|
v-if="isSelected && canUpdate"
|
|
:directions="directions"
|
|
:allowed-directions="allowedMoveDirections"
|
|
:is-duplicating="isDuplicating"
|
|
:has-parent="!!parentElement"
|
|
@delete="deleteElement"
|
|
@move="onMove"
|
|
@duplicate="duplicateElement"
|
|
@select-parent="selectParentElement()"
|
|
@drag-handle-mousedown="onDragHandleMouseDown"
|
|
@mousedown.stop
|
|
/>
|
|
<PageElement
|
|
ref="elementRef"
|
|
:element="element"
|
|
:mode="mode"
|
|
class="element--read-only"
|
|
:show-element-id="showElementId"
|
|
@move="$emit('move', $event)"
|
|
/>
|
|
|
|
<InsertElementButton
|
|
v-show="isSelected"
|
|
v-if="canCreate"
|
|
class="element-preview__insert element-preview__insert--bottom"
|
|
@click="showAddElementModal(DIRECTIONS.AFTER)"
|
|
/>
|
|
<AddElementModal
|
|
v-if="canCreate"
|
|
ref="addElementModal"
|
|
:page="elementPage"
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import { computed, inject, ref } from 'vue'
|
|
import { useStore, mapActions, mapGetters } from 'vuex'
|
|
import ElementMenu from '@baserow/modules/builder/components/elements/ElementMenu'
|
|
import InsertElementButton from '@baserow/modules/builder/components/elements/InsertElementButton'
|
|
import PageElement from '@baserow/modules/builder/components/page/PageElement'
|
|
import { DIRECTIONS } from '@baserow/modules/builder/enums'
|
|
import AddElementModal from '@baserow/modules/builder/components/elements/AddElementModal'
|
|
import { notifyIf } from '@baserow/modules/core/utils/error'
|
|
import { checkIntermediateElements } from '@baserow/modules/core/utils/dom'
|
|
import applicationContextMixin from '@baserow/modules/builder/mixins/applicationContext'
|
|
import { useElementDraggable } from '@baserow/modules/builder/composables/useElementDraggable'
|
|
import { useDropElementTarget } from '@baserow/modules/builder/composables/useDropElementTarget'
|
|
|
|
export default {
|
|
name: 'ElementPreview',
|
|
components: {
|
|
AddElementModal,
|
|
ElementMenu,
|
|
InsertElementButton,
|
|
PageElement,
|
|
},
|
|
mixins: [applicationContextMixin],
|
|
inject: ['workspace', 'builder', 'mode', 'currentPage', 'pageTopData'],
|
|
props: {
|
|
element: {
|
|
type: Object,
|
|
required: true,
|
|
},
|
|
isFirstElement: {
|
|
type: Boolean,
|
|
required: false,
|
|
default: false,
|
|
},
|
|
showElementId: {
|
|
type: Boolean,
|
|
required: false,
|
|
default: false,
|
|
},
|
|
},
|
|
emits: ['move'],
|
|
setup(props) {
|
|
const elementPreviewRef = ref(null)
|
|
const elementRef = ref(null)
|
|
const store = useStore()
|
|
const builder = inject('builder')
|
|
|
|
const elementPage = computed(() =>
|
|
store.getters['page/getById'](builder, props.element.page_id)
|
|
)
|
|
const parentElement = computed(() =>
|
|
store.getters['element/getParent'](elementPage.value, props.element)
|
|
)
|
|
|
|
function getDragImageScale(rect) {
|
|
const defaultDragImageScale = 0.5
|
|
const maxHeightRatioBeforeScalingDown = 0.3
|
|
const viewportHeight =
|
|
window.innerHeight || document.documentElement.clientHeight
|
|
const maxHeightBeforeScalingDown =
|
|
viewportHeight * maxHeightRatioBeforeScalingDown
|
|
const defaultScaledHeight = rect.height * defaultDragImageScale
|
|
|
|
if (
|
|
!viewportHeight ||
|
|
defaultScaledHeight <= maxHeightBeforeScalingDown
|
|
) {
|
|
return defaultDragImageScale
|
|
}
|
|
|
|
return maxHeightBeforeScalingDown / rect.height
|
|
}
|
|
|
|
function createDragImage() {
|
|
const source = elementRef.value.$el
|
|
const rect = source.getBoundingClientRect()
|
|
const dragImageScale = getDragImageScale(rect)
|
|
|
|
const clone = source.cloneNode(true)
|
|
|
|
Object.assign(clone.style, {
|
|
boxSizing: 'border-box',
|
|
width: `${rect.width}px`,
|
|
minWidth: `${rect.width}px`,
|
|
maxWidth: `${rect.width}px`,
|
|
transform: `scale(${dragImageScale})`,
|
|
transformOrigin: 'top left',
|
|
})
|
|
|
|
const container = document.createElement('div')
|
|
Object.assign(container.style, {
|
|
position: 'fixed',
|
|
top: '0',
|
|
left: '0',
|
|
pointerEvents: 'none',
|
|
})
|
|
container.appendChild(clone)
|
|
elementPreviewRef.value.appendChild(container)
|
|
requestAnimationFrame(() => {
|
|
// immediately remove the cloned element
|
|
elementPreviewRef.value.removeChild(container)
|
|
})
|
|
|
|
return container
|
|
}
|
|
|
|
return {
|
|
...useElementDraggable({
|
|
element: props.element,
|
|
getDragImage: createDragImage,
|
|
}),
|
|
...useDropElementTarget({
|
|
parentElement,
|
|
referenceElement: props.element,
|
|
placeInContainer: props.element.place_in_container,
|
|
}),
|
|
elementPreviewRef,
|
|
elementRef,
|
|
parentElement,
|
|
elementPage,
|
|
}
|
|
},
|
|
data() {
|
|
return {
|
|
isDuplicating: false,
|
|
isAboveThreshold: false,
|
|
observer: null,
|
|
}
|
|
},
|
|
computed: {
|
|
...mapGetters({
|
|
getElementSelected: 'element/getSelected',
|
|
elementAncestors: 'element/getAncestors',
|
|
getClosestSiblingElement: 'element/getClosestSiblingElement',
|
|
loggedUser: 'userSourceUser/getUser',
|
|
}),
|
|
applicationContext() {
|
|
return {
|
|
...this.injectedApplicationContext,
|
|
...this.applicationContextAdditions,
|
|
element: this.element,
|
|
}
|
|
},
|
|
pageTop() {
|
|
return this.pageTopData?.value ?? 0
|
|
},
|
|
elementSelected() {
|
|
return this.getElementSelected(this.builder)
|
|
},
|
|
isVisible() {
|
|
return this.elementType.isVisible({
|
|
element: this.element,
|
|
applicationContext: this.applicationContext,
|
|
})
|
|
},
|
|
DIRECTIONS: () => DIRECTIONS,
|
|
directions() {
|
|
return [
|
|
DIRECTIONS.BEFORE,
|
|
DIRECTIONS.AFTER,
|
|
DIRECTIONS.LEFT,
|
|
DIRECTIONS.RIGHT,
|
|
]
|
|
},
|
|
parentOfElementSelected() {
|
|
return this.$store.getters['element/getParent'](
|
|
this.elementPage,
|
|
this.elementSelected
|
|
)
|
|
},
|
|
elementsAround() {
|
|
return this.elementType.getElementsAround({
|
|
builder: this.builder,
|
|
page: this.currentPage,
|
|
withSharedPage: true,
|
|
element: this.element,
|
|
})
|
|
},
|
|
nextPlaces() {
|
|
return this.elementType.getNextPlaces({
|
|
builder: this.builder,
|
|
page: this.elementPage,
|
|
element: this.element,
|
|
})
|
|
},
|
|
allowedMoveDirections() {
|
|
return Object.entries(this.nextPlaces)
|
|
.filter(([, nextPlace]) => !!nextPlace)
|
|
.map(([direction]) => direction)
|
|
},
|
|
canCreate() {
|
|
return this.$hasPermission(
|
|
'builder.page.create_element',
|
|
this.currentPage,
|
|
this.workspace.id
|
|
)
|
|
},
|
|
canUpdate() {
|
|
return this.$hasPermission(
|
|
'builder.page.element.update',
|
|
this.element,
|
|
this.workspace.id
|
|
)
|
|
},
|
|
isSelected() {
|
|
return this.element.id === this.elementSelected?.id
|
|
},
|
|
selectedElementAncestorIds() {
|
|
if (!this.elementSelected) {
|
|
return []
|
|
}
|
|
return this.elementAncestors(this.elementPage, this.elementSelected).map(
|
|
({ id }) => id
|
|
)
|
|
},
|
|
isParentOfSelectedElement() {
|
|
return this.selectedElementAncestorIds.includes(this.element.id)
|
|
},
|
|
elementType() {
|
|
return this.$registry.get('element', this.element.type)
|
|
},
|
|
errorMessage() {
|
|
return this.elementType.getErrorMessage(
|
|
this.element,
|
|
this.applicationContext
|
|
)
|
|
},
|
|
},
|
|
watch: {
|
|
/**
|
|
* If the element is currently selected, i.e. in the Elements Context menu,
|
|
* ensure the element is scrolled into the viewport.
|
|
*/
|
|
isSelected(newValue, old) {
|
|
if (newValue && !old) {
|
|
const rect = this.$el.getBoundingClientRect()
|
|
const isTopVisible =
|
|
rect.top >= 0 &&
|
|
rect.top <=
|
|
(window.innerHeight || document.documentElement.clientHeight)
|
|
|
|
if (!isTopVisible) {
|
|
this.$el.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
|
}
|
|
}
|
|
},
|
|
pageTop() {
|
|
this.setupIntersectionObserver()
|
|
},
|
|
/**
|
|
* If the currently selected element in the Page Preview has moved, ensure
|
|
* the element is scrolled into the viewport.
|
|
*/
|
|
element: {
|
|
handler(newValue, old) {
|
|
if (
|
|
newValue.place_in_container !== old.place_in_container &&
|
|
this.isSelected
|
|
) {
|
|
this.$el.scrollIntoView({ behavior: 'smooth', block: 'center' })
|
|
}
|
|
},
|
|
deep: true,
|
|
},
|
|
},
|
|
mounted() {
|
|
if (this.isFirstElement) {
|
|
this.actionSelectElement({ builder: this.builder, element: this.element })
|
|
}
|
|
this.setupIntersectionObserver()
|
|
},
|
|
beforeUnmount() {
|
|
if (this.observer) {
|
|
this.observer.disconnect()
|
|
}
|
|
},
|
|
methods: {
|
|
...mapActions({
|
|
actionDuplicateElement: 'element/duplicate',
|
|
actionDeleteElement: 'element/delete',
|
|
actionSelectElement: 'element/select',
|
|
}),
|
|
setupIntersectionObserver() {
|
|
if (this.observer) {
|
|
this.observer.disconnect()
|
|
}
|
|
|
|
// Ensure pageTop is a finite, non-negative number: the page top is negative
|
|
// once it's scrolled above the viewport, and `--10px` would make the
|
|
// IntersectionObserver constructor throw.
|
|
const topMargin = Number.isFinite(this.pageTop)
|
|
? Math.max(this.pageTop, 0)
|
|
: 0
|
|
|
|
const options = {
|
|
root: null,
|
|
rootMargin: `-${topMargin}px 0px 0px 0px`,
|
|
threshold: [0, 1],
|
|
}
|
|
|
|
this.observer = new IntersectionObserver((entries) => {
|
|
const rect = entries[0].boundingClientRect
|
|
this.isAboveThreshold = rect.top < topMargin
|
|
}, options)
|
|
|
|
this.$nextTick(() => {
|
|
this.observer.observe(this.$el)
|
|
})
|
|
},
|
|
onMove(direction) {
|
|
this.$emit('move', { element: this.element, direction })
|
|
},
|
|
onSelect($event) {
|
|
// Here we check that the event has been emitted for this particular element
|
|
// If we found an intermediate DOM element with the class `element-preview`,
|
|
// or `element-preview__menu`, then we don't select the element.
|
|
// It means it hasn't been originated by this element, so we don't select it.
|
|
if (
|
|
!checkIntermediateElements(this.$el, $event.target, (el) => {
|
|
return (
|
|
el.classList.contains('element-preview') ||
|
|
el.classList.contains('element-preview__menu')
|
|
)
|
|
})
|
|
) {
|
|
this.actionSelectElement({
|
|
builder: this.builder,
|
|
element: this.element,
|
|
})
|
|
}
|
|
},
|
|
showAddElementModal(direction) {
|
|
const rootElement = this.$store.getters['element/getAncestors'](
|
|
this.elementPage,
|
|
this.element,
|
|
{ includeSelf: true }
|
|
)[0]
|
|
const rootElementType = this.$registry.get('element', rootElement.type)
|
|
const pagePlace = rootElementType.getPagePlace()
|
|
|
|
// elementsAround uses withSharedPage:true, so nextEl may be a footer element
|
|
// from the shared page. Restrict to same-page elements so AddElementModal's
|
|
// cross-page filter doesn't end up with both beforeId and afterId null.
|
|
const nextEl = this.elementsAround[DIRECTIONS.AFTER]
|
|
const samePageNextEl =
|
|
nextEl?.page_id === this.elementPage.id ? nextEl : null
|
|
const beforeId =
|
|
direction === DIRECTIONS.BEFORE
|
|
? this.element.id
|
|
: samePageNextEl?.id || null
|
|
const afterId =
|
|
direction === DIRECTIONS.AFTER && !samePageNextEl
|
|
? this.element.id
|
|
: null
|
|
|
|
this.$refs.addElementModal.show({
|
|
placeInContainer: this.element.place_in_container,
|
|
parentElementId: this.parentElement?.id ?? null,
|
|
beforeId,
|
|
afterId,
|
|
pagePlace,
|
|
})
|
|
},
|
|
async duplicateElement() {
|
|
this.isDuplicating = true
|
|
try {
|
|
await this.actionDuplicateElement({
|
|
builder: this.builder,
|
|
page: this.elementPage,
|
|
elementId: this.element.id,
|
|
})
|
|
} catch (error) {
|
|
notifyIf(error)
|
|
}
|
|
this.isDuplicating = false
|
|
},
|
|
async deleteElement() {
|
|
try {
|
|
const siblingElementToSelect =
|
|
this.elementsAround[DIRECTIONS.AFTER] ||
|
|
this.elementsAround[DIRECTIONS.BEFORE] ||
|
|
this.elementsAround[DIRECTIONS.LEFT] ||
|
|
this.elementsAround[DIRECTIONS.RIGHT] ||
|
|
this.parentOfElementSelected
|
|
|
|
await this.actionDeleteElement({
|
|
builder: this.builder,
|
|
page: this.elementPage,
|
|
elementId: this.element.id,
|
|
})
|
|
if (siblingElementToSelect?.id) {
|
|
await this.actionSelectElement({
|
|
builder: this.builder,
|
|
element: siblingElementToSelect,
|
|
})
|
|
}
|
|
} catch (error) {
|
|
notifyIf(error)
|
|
}
|
|
},
|
|
selectParentElement() {
|
|
if (this.parentOfElementSelected) {
|
|
this.actionSelectElement({
|
|
builder: this.builder,
|
|
element: this.parentOfElementSelected,
|
|
})
|
|
}
|
|
},
|
|
},
|
|
}
|
|
</script>
|