adrema/resources/js/components/form/Editor.vue

258 lines
6.8 KiB
Vue
Raw Normal View History

2024-02-02 01:05:45 +01:00
<template>
<div>
2024-06-28 14:04:20 +02:00
<div class="flex flex-col group" :for="id" :class="sizeClass(size)">
<f-label v-if="label" :required="required" :value="label"></f-label>
2024-04-19 13:49:47 +02:00
<div class="relative w-full h-full">
2024-06-28 14:04:20 +02:00
<div :id="id" :class="[fieldAppearance, paddingX, paddingY]"></div>
<f-hint v-if="hint" :value="hint"></f-hint>
2024-02-02 01:05:45 +01:00
</div>
</div>
2024-04-19 13:49:47 +02:00
2024-07-06 15:14:53 +02:00
<ui-popup
v-if="condition !== null"
heading="Bedingungen"
@close="
condition.resolve(condition.data);
condition = null;
"
>
2024-04-19 13:49:47 +02:00
<slot name="conditions" :data="condition.data" :resolve="condition.resolve" :reject="condition.reject"></slot>
</ui-popup>
2024-02-02 01:05:45 +01:00
</div>
</template>
<script setup>
2024-07-06 15:14:53 +02:00
import {debounce} from 'lodash';
import {onMounted, ref} from 'vue';
2024-02-02 01:05:45 +01:00
import EditorJS from '@editorjs/editorjs';
import Header from '@editorjs/header';
import Paragraph from '@editorjs/paragraph';
2024-02-02 02:03:39 +01:00
import NestedList from '@editorjs/nested-list';
2024-04-17 23:05:55 +02:00
import Alert from 'editorjs-alert';
2024-02-02 01:05:45 +01:00
import useFieldSize from '../../composables/useFieldSize.js';
const emit = defineEmits(['update:modelValue']);
2024-07-06 15:14:53 +02:00
const {fieldAppearance, paddingX, paddingY, sizeClass} = useFieldSize();
2024-02-02 01:05:45 +01:00
const props = defineProps({
required: {
type: Boolean,
default: false,
},
size: {
2024-06-28 14:04:20 +02:00
type: String,
default: () => 'base',
2024-02-02 01:05:45 +01:00
},
rows: {
2024-06-28 14:04:20 +02:00
type: Number,
default: () => 4,
2024-02-02 01:05:45 +01:00
},
id: {
2024-06-28 14:04:20 +02:00
type: String,
2024-02-02 01:05:45 +01:00
required: true,
},
2024-04-19 13:49:47 +02:00
conditions: {
required: false,
type: Boolean,
default: () => false,
},
2024-02-02 01:05:45 +01:00
hint: {
2024-06-28 14:04:20 +02:00
type: String,
default: () => '',
2024-02-02 01:05:45 +01:00
},
modelValue: {
default: undefined,
},
label: {
2024-06-28 14:04:20 +02:00
type: String,
default: () => '',
2024-02-02 01:05:45 +01:00
},
});
const editor = ref(null);
2024-04-19 13:49:47 +02:00
const condition = ref(null);
2024-04-19 22:06:09 +02:00
async function openPopup(data) {
2024-04-19 13:49:47 +02:00
return new Promise((resolve, reject) => {
new Promise((innerResolve, innerReject) => {
condition.value = {
resolve: innerResolve,
reject: innerReject,
2024-04-19 22:06:09 +02:00
data: data,
2024-04-19 13:49:47 +02:00
};
}).then((data) => {
resolve(data);
condition.value = null;
});
});
}
class ConditionTune {
2024-07-06 15:14:53 +02:00
constructor({api, data, config, block}) {
2024-04-19 13:49:47 +02:00
this.api = api;
this.data = data || {
2024-04-19 22:06:09 +02:00
mode: 'all',
ifs: [],
2024-04-19 13:49:47 +02:00
};
this.config = config;
this.block = block;
this.wrapper = null;
}
static get isTune() {
return true;
}
wrap(blockContent) {
this.wrapper = document.createElement('div');
var tooltip = document.createElement('div');
tooltip.setAttribute('data-tooltip', '');
var content = document.createElement('div');
content.setAttribute('data-content', '');
content.appendChild(blockContent);
this.wrapper.appendChild(tooltip);
this.wrapper.appendChild(content);
2024-04-19 13:49:47 +02:00
this.styleWrapper();
2024-04-19 13:49:47 +02:00
return this.wrapper;
}
2024-04-19 22:06:09 +02:00
hasData() {
return this.data.ifs.length > 0;
}
2024-04-19 13:49:47 +02:00
styleWrapper() {
2024-04-19 22:06:09 +02:00
if (this.hasData()) {
2024-04-25 22:49:07 +02:00
this.wrapper.querySelector('[data-content]').className = 'p-1 border border-blue-100 rounded';
this.wrapper.querySelector('[data-tooltip]').className =
2024-04-25 23:17:05 +02:00
'mt-1 inline-block tracking-wider font-semibold ml-2 mr-2 px-2 py-1 items-center text-xs leading-none bg-blue-100 text-blue-900 rounded-t-lg';
2024-04-25 22:49:07 +02:00
this.wrapper.querySelector('[data-tooltip]').innerHTML = this.descriptionName();
2024-04-19 13:49:47 +02:00
} else {
this.wrapper.querySelector('[data-content]').className = '';
this.wrapper.querySelector('[data-tooltip]').className = '';
this.wrapper.querySelector('[data-tooltip]').innerHTML = '';
2024-04-19 13:49:47 +02:00
}
}
2024-04-25 22:49:07 +02:00
descriptionName() {
return (
'Bedingung ' +
this.data.ifs
.map((i) => {
var parts = [i.field];
if (i.comparator === 'isEqual' || i.comparator === 'isIn') {
parts.push('=');
}
if (i.comparator === 'isNotEqual' || i.comparator === 'isNotIn') {
parts.push('&ne;');
}
if (typeof i.value === 'string') {
parts.push(i.value);
}
if (Array.isArray(i.value)) {
parts.push(i.value.join(', '));
}
2024-04-25 23:17:05 +02:00
if (typeof i.value === 'boolean') {
parts.push(i.value ? 'An' : 'Aus');
}
2024-04-25 22:49:07 +02:00
return parts.join(' ');
})
.join(', ')
);
}
2024-04-19 13:49:47 +02:00
render() {
return {
label: 'Bedingungen',
closeOnActivate: true,
toggle: true,
onActivate: async () => {
2024-04-19 22:06:09 +02:00
this.data = await openPopup(this.data);
2024-04-19 13:49:47 +02:00
this.styleWrapper();
this.block.dispatchChange();
},
};
}
save() {
return this.data;
}
}
2024-02-02 01:05:45 +01:00
onMounted(async () => {
2024-04-19 13:49:47 +02:00
var tools = {
paragraph: {
class: Paragraph,
shortcut: 'CTRL+P',
inlineToolbar: true,
config: {
preserveBlank: true,
placeholder: 'Absatz',
},
},
alert: {
class: Alert,
inlineToolbar: true,
config: {
defaultType: 'primary',
},
},
heading: {
class: Header,
shortcut: 'CTRL+H',
inlineToolbar: true,
config: {
placeholder: 'Überschrift',
levels: [2, 3, 4],
defaultLevel: 2,
},
},
list: {
class: NestedList,
shortcut: 'CTRL+L',
inlineToolbar: true,
},
};
var tunes = [];
if (props.conditions) {
tools.condition = {
class: ConditionTune,
};
tunes.push('condition');
}
2024-02-02 01:05:45 +01:00
editor.value = new EditorJS({
placeholder: props.placeholder,
holder: props.id,
2024-02-02 02:14:44 +01:00
minHeight: 0,
2024-02-02 01:05:45 +01:00
defaultBlock: 'paragraph',
data: JSON.parse(JSON.stringify(props.modelValue)),
2024-04-19 13:49:47 +02:00
tunes: tunes,
tools: tools,
2024-02-02 01:05:45 +01:00
onChange: debounce(async (api, event) => {
const data = await editor.value.save();
2024-04-19 13:49:47 +02:00
console.log(data);
2024-02-02 01:05:45 +01:00
emit('update:modelValue', data);
2024-04-20 00:18:10 +02:00
}, 200),
2024-04-19 13:49:47 +02:00
onPopup: () => {
console.log('opened');
},
2024-02-02 01:05:45 +01:00
});
await editor.value.isReady;
console.log('Editor is ready');
});
</script>