Skip to content

Commit

Permalink
make builder usable on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Oct 5, 2023
1 parent bfece57 commit a341d0d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 33 deletions.
1 change: 0 additions & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ document.addEventListener('turbo:submit-end', async (event) => {
window.customElements.define('template-builder', class extends HTMLElement {
connectedCallback () {
this.appElem = document.createElement('div')
this.appElem.classList.add('max-h-screen')

this.app = createApp(TemplateBuilder, {
template: reactive(JSON.parse(this.dataset.template)),
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/template_builder/area.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div
v-if="field?.type"
class="absolute bg-white rounded-t border overflow-visible whitespace-nowrap group-hover:flex group-hover:z-10"
:class="{ 'flex z-10': isNameFocus || isSelected, hidden: !isNameFocus && !isSelected }"
:class="{ 'flex z-10': isNameFocus || isSelected, invisible: !isNameFocus && !isSelected }"
style="top: -25px; height: 25px"
@mousedown.stop
@pointerdown.stop
Expand Down
92 changes: 76 additions & 16 deletions app/javascript/template_builder/builder.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<div
style="max-width: 1600px"
class="mx-auto pl-4 h-full"
class="mx-auto pl-3 md:pl-4 h-full"
>
<div class="flex justify-between py-1.5 items-center pr-4">
<div class="flex justify-between py-1.5 items-center pr-4 sticky top-0 z-10 bg-base-100">
<div class="flex space-x-3">
<a
v-if="withLogo"
Expand Down Expand Up @@ -59,10 +59,7 @@
</template>
</div>
</div>
<div
class="flex"
style="max-height: calc(100% - 60px)"
>
<div class="flex md:max-h-[calc(100vh-60px)]">
<div
ref="previews"
:style="{ 'display': isBreakpointLg ? 'none' : 'initial' }"
Expand Down Expand Up @@ -152,17 +149,54 @@
</div>
</template>
</div>
<div
v-if="sortedDocuments.length"
class="sticky md:hidden"
style="bottom: 100px"
<span
v-if="drawField"
class="fixed text-center w-full left-1/2 bottom-0 transform -translate-x-1/2"
>
<div class="px-4 py-3 rounded-2xl bg-base-200 flex items-center justify-between ml-4 mr-6">
<span class="w-full text-center text-lg">
You need a larger screen to use builder tools.
<span
class="rounded bg-base-200 px-4 py-2 rounded-full inline-flex space-x-2 mx-auto items-center mb-4 z-20"
>
<component
:is="fieldIcons[drawField.type]"
:width="20"
:height="20"
class="inline"
:stroke-width="1.6"
/>
<span>
Draw {{ fieldNames[drawField.type] }} Field
</span>
</div>
</div>
<a
href="#"
class="link block text-center"
@click.prevent="drawField = null"
>
Cancel
</a>
</span>
</span>
<FieldType
v-if="sortedDocuments.length && !drawField"
class="dropdown-top dropdown-end fixed bottom-4 right-4 z-10 md:hixdden"
:model-value="''"
@update:model-value="startFieldDraw($event)"
>
<label
class="btn btn-neutral text-white btn-circle btn-lg group"
tabindex="0"
>
<IconPlus
class="group-focus:hidden"
width="28"
height="28"
/>
<IconX
class="hidden group-focus:inline"
width="28"
height="28"
/>
</label>
</FieldType>
</div>
<div
class="relative w-80 flex-none mt-1 pr-4 pl-0.5 hidden md:block"
Expand Down Expand Up @@ -214,7 +248,8 @@ import Logo from './logo'
import Contenteditable from './contenteditable'
import DocumentPreview from './preview'
import DocumentControls from './controls'
import { IconUsersPlus, IconDeviceFloppy, IconInnerShadowTop } from '@tabler/icons-vue'
import FieldType from './field_type'
import { IconUsersPlus, IconDeviceFloppy, IconInnerShadowTop, IconPlus, IconX } from '@tabler/icons-vue'
import { v4 } from 'uuid'
import { ref, computed } from 'vue'
Expand All @@ -224,6 +259,9 @@ export default {
Upload,
Document,
Fields,
IconPlus,
FieldType,
IconX,
Logo,
Dropzone,
DocumentPreview,
Expand Down Expand Up @@ -300,6 +338,8 @@ export default {
}
},
computed: {
fieldIcons: FieldType.computed.fieldIcons,
fieldNames: FieldType.computed.fieldNames,
selectedAreaRef: () => ref(),
fieldAreasIndex () {
const areas = {}
Expand Down Expand Up @@ -351,6 +391,22 @@ export default {
this.documentRefs = []
},
methods: {
startFieldDraw (type) {
const field = {
name: '',
uuid: v4(),
required: type !== 'checkbox',
areas: [],
submitter_uuid: this.selectedSubmitter.uuid,
type
}
if (['select', 'multiple', 'radio'].includes(type)) {
field.options = ['']
}
this.drawField = field
},
undo () {
if (this.undoStack.length > 1) {
this.undoStack.pop()
Expand Down Expand Up @@ -448,6 +504,10 @@ export default {
this.drawField.areas ||= []
this.drawField.areas.push(area)
if (this.template.fields.indexOf(this.drawField) === -1) {
this.template.fields.push(this.drawField)
}
this.drawField = null
this.selectedAreaRef.value = area
Expand Down
28 changes: 15 additions & 13 deletions app/javascript/template_builder/field_type.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<template>
<span class="dropdown">
<label
tabindex="0"
:title="fieldNames[modelValue]"
class="cursor-pointer"
>
<component
:is="fieldIcons[modelValue]"
:width="buttonWidth"
:class="buttonClasses"
:stroke-width="1.6"
/>
</label>
<slot>
<label
tabindex="0"
:title="fieldNames[modelValue]"
class="cursor-pointer"
>
<component
:is="fieldIcons[modelValue]"
:width="buttonWidth"
:class="buttonClasses"
:stroke-width="1.6"
/>
</label>
</slot>
<ul
tabindex="0"
class="dropdown-content menu menu-xs p-2 shadow rounded-box w-52 z-10"
class="dropdown-content menu menu-xs p-2 shadow rounded-box w-52 z-10 mb-3"
:class="menuClasses"
@click="closeDropdown"
>
Expand Down
16 changes: 14 additions & 2 deletions app/javascript/template_builder/page.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<div class="relative cursor-crosshair select-none">
<div
class="relative cursor-crosshair select-none"
:style="drawField || isTouchMove ? 'touch-action: none' : ''"
>
<img
ref="image"
:src="image.url"
Expand Down Expand Up @@ -32,12 +35,13 @@
/>
</div>
<div
v-show="resizeDirection || isMove || isDrag || showMask"
v-show="resizeDirection || isMove || isDrag || showMask || (drawField && isMobile)"
id="mask"
ref="mask"
class="top-0 bottom-0 left-0 right-0 absolute z-10"
:class="{ 'cursor-grab': isDrag || isMove, 'cursor-nwse-resize': drawField, [resizeDirectionClasses[resizeDirection]]: !!resizeDirectionClasses }"
@pointermove="onPointermove"
@pointerdown="onStartDraw"
@dragover.prevent
@drop="onDrop"
@pointerup="onPointerup"
Expand Down Expand Up @@ -88,11 +92,15 @@ export default {
areaRefs: [],
showMask: false,
isMove: false,
isTouchMove: false,
resizeDirection: null,
newArea: null
}
},
computed: {
isMobile () {
return /android|iphone|ipad/i.test(navigator.userAgent)
},
resizeDirectionClasses () {
return {
nwse: 'cursor-nwse-resize',
Expand Down Expand Up @@ -125,6 +133,10 @@ export default {
})
},
onStartDraw (e) {
if (this.isMobile && !this.drawField) {
return
}
this.showMask = true
this.$nextTick(() => {
Expand Down

0 comments on commit a341d0d

Please sign in to comment.