From 09952c18946d467159487d76ed922e89d3bb4054 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9C=D0=B0?=
 =?UTF-8?q?=D0=BB=D1=8E=D0=B3=D0=B8=D0=BD?= <d.malygin@iqdev.digital>
Date: Thu, 8 Aug 2024 15:53:29 +0500
Subject: [PATCH] refactor entityText

---
 components.d.ts                      |  5 +-
 src/components/entities/TextItem.vue | 68 ++++++++++++++++++----------
 src/helpers/index.ts                 | 11 +++++
 src/modules/EntityItem.vue           |  2 +-
 4 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/components.d.ts b/components.d.ts
index b5319bf..65ca4e7 100644
--- a/components.d.ts
+++ b/components.d.ts
@@ -2,7 +2,7 @@
 // @ts-nocheck
 // Generated by unplugin-vue-components
 // Read more: https://github.com/vuejs/core/pull/3399
-export {};
+export {}
 
 /* prettier-ignore */
 declare module 'vue' {
@@ -13,14 +13,11 @@ declare module 'vue' {
     Divider: typeof import('primevue/divider')['default']
     Drawer: typeof import('primevue/drawer')['default']
     LogoAndLabel: typeof import('./src/components/LogoAndLabel.vue')['default']
-    Popover: typeof import('primevue/popover')['default']
     RouterLink: typeof import('vue-router')['RouterLink']
     RouterView: typeof import('vue-router')['RouterView']
-    ScrollPanel: typeof import('primevue/scrollpanel')['default']
     SpeedDial: typeof import('primevue/speeddial')['default']
     Splitter: typeof import('primevue/splitter')['default']
     SplitterPanel: typeof import('primevue/splitterpanel')['default']
-    Textarea: typeof import('primevue/textarea')['default']
     TextItem: typeof import('./src/components/entities/TextItem.vue')['default']
     Tree: typeof import('primevue/tree')['default']
     UserInfoHeaderWithSettings: typeof import('./src/components/UserInfoHeaderWithSettings.vue')['default']
diff --git a/src/components/entities/TextItem.vue b/src/components/entities/TextItem.vue
index dd3b61e..a0d923a 100644
--- a/src/components/entities/TextItem.vue
+++ b/src/components/entities/TextItem.vue
@@ -2,7 +2,8 @@
 import type { IText } from '@/interfaces/entities';
 import type { IEntity } from '@/interfaces/environment';
 import { useDataStore } from '@/stores/data';
-import {useTextareaAutosize} from "@vueuse/core";
+import { useTextareaAutosize } from '@vueuse/core';
+import { editEntity } from '@/helpers';
 
 interface Props {
   entityData: IText;
@@ -12,53 +13,74 @@ const entityData = ref(props.entityData);
 
 const dataStore = useDataStore();
 
-const editEntity = (newState: IEntity) => {
-  let prevState = dataStore.homeEntities;
-  prevState = prevState.map((entity: IEntity) => {
-    if (entity.uuid !== entityData.value.uuid) return entity;
-    return newState;
-  });
-  dataStore.editHomeEntities(prevState);
-};
-
 const addTitle = () => {
-  editEntity({ ...entityData.value, title: 'Title' });
+  editEntity({ ...entityData.value, title: 'Title' }, entityData.value.uuid);
   entityData.value = { ...entityData.value, title: 'Title' };
 };
 
 const editTitle = () => {
-  editEntity({ ...entityData.value, title: entityData.value.title });
+  editEntity({ ...entityData.value, title: entityData.value.title }, entityData.value.uuid);
 };
 
 const editText = () => {
-  editEntity({ ...entityData.value, text: entityData.value.text });
+  editEntity({ ...entityData.value, text: entityData.value.text }, entityData.value.uuid);
 };
 
 const deleteEntity = () => {
   let prevState = dataStore.homeEntities;
   prevState = prevState.filter((entity: IEntity) => entity.uuid !== entityData.value.uuid);
   dataStore.editHomeEntities(prevState);
-}
-const { textarea, triggerResize  } = useTextareaAutosize();
+};
+
+const deleteTitle = () => {
+  const newState = { ...entityData.value };
+  delete newState.title;
+  editEntity({ ...newState }, entityData.value.uuid);
+  entityData.value = newState;
+};
+
+const { textarea, triggerResize } = useTextareaAutosize();
 </script>
 
 <template>
   <div class="textContainer relative">
-    <input ref="input" v-if="entityData?.title" type="text" v-model="entityData.title" @change="editTitle" @input="triggerResize" class="w-full mb-2 font-bold pl-2" />
-    <textarea ref="textarea" v-model="entityData.text" class="w-full indent-5 resize-none pl-4 -ml-4 border-0 border-l-4 border-solid border-l-blue-400" @change="editText" @input="triggerResize" placeholder="Enter text..."/>
+    <input
+      ref="input"
+      v-if="entityData.title"
+      type="text"
+      v-model="entityData.title"
+      @change="editTitle"
+      @input="triggerResize"
+      class="w-full mb-2 font-bold pl-2"
+    />
+    <textarea
+      ref="textarea"
+      v-model="entityData.text"
+      class="w-full indent-5 resize-none pl-4 -ml-4 border-0 border-l-4 border-solid border-l-blue-400"
+      @change="editText"
+      @input="triggerResize"
+      placeholder="Enter text..."
+    />
     <button
-        v-if="!entityData.title"
-        @click.prevent="addTitle"
-        class="addTitleButton absolute top-0 left-4 bg-blue-500 py-0.5 px-2 rounded-md -translate-y-full border-2 border-solid border-black transition-all select-none"
+      v-if="!entityData?.title"
+      @click.prevent="addTitle"
+      class="addTitleButton absolute top-0 left-4 bg-blue-500 py-0.5 px-2 rounded-md -translate-y-full border-2 border-solid border-black transition-all select-none"
     >
       Add title
     </button>
     <button
-        @click.prevent="deleteEntity"
-        class="deleteEntityButton absolute top-0 right-4 bg-blue-500 py-0.5 px-2 rounded-md -translate-y-full border-2 border-solid border-black transition-all select-none"
+      @click.prevent="deleteEntity"
+      class="deleteEntityButton absolute top-0 right-4 bg-blue-500 py-0.5 px-2 rounded-md -translate-y-full border-2 border-solid border-black transition-all select-none"
     >
       <i class="pi pi-trash pr-2"></i>Delete
     </button>
+    <button
+      v-if="entityData?.title"
+      @click.prevent="deleteTitle"
+      class="deleteEntityButton absolute top-0 left-4 bg-blue-500 py-0.5 px-2 rounded-md -translate-y-full border-2 border-solid border-black transition-all select-none"
+    >
+      <i class="pi pi-trash pr-2"></i>Delete title
+    </button>
   </div>
 </template>
 
@@ -76,7 +98,7 @@ textarea::-webkit-scrollbar {
   opacity: 0;
 }
 .textContainer:hover > .addTitleButton,
-.textContainer:hover > .deleteEntityButton{
+.textContainer:hover > .deleteEntityButton {
   opacity: 100;
 }
 </style>
diff --git a/src/helpers/index.ts b/src/helpers/index.ts
index eb5badf..b53eb64 100644
--- a/src/helpers/index.ts
+++ b/src/helpers/index.ts
@@ -1,5 +1,6 @@
 import { useInterfaceStore } from '@/stores/interface';
 import type { IEntity } from '@/interfaces/environment';
+import { useDataStore } from '@/stores/data';
 
 export async function uploadFile($event: Event) {
   const target = $event.target as HTMLInputElement;
@@ -23,3 +24,13 @@ export function setDefaultHomeBackground() {
   );
   localStorage.removeItem('homeBackgroundUrl');
 }
+
+export const editEntity = (newState: IEntity, entityUuid: string) => {
+  const dataStore = useDataStore();
+  let prevState = dataStore.homeEntities;
+  prevState = prevState.map((entity: IEntity) => {
+    if (entity.uuid !== entityUuid) return entity;
+    return newState;
+  });
+  dataStore.editHomeEntities(prevState);
+};
diff --git a/src/modules/EntityItem.vue b/src/modules/EntityItem.vue
index b273298..f485f61 100644
--- a/src/modules/EntityItem.vue
+++ b/src/modules/EntityItem.vue
@@ -10,7 +10,7 @@ defineProps<Props>();
 
 <template>
   <div>
-    <TextItem v-if="entity.type === 'text'" :entityData="entity"/>
+    <TextItem v-if="entity.type === 'text'" :entityData="entity" />
   </div>
 </template>
 
-- 
GitLab