Small UI improvements and fixes to project edition

This commit is contained in:
Peaceultime 2024-11-28 00:01:57 +01:00
parent 20ab51a66c
commit d71e8b7910
10 changed files with 98 additions and 35 deletions

View File

@ -5,7 +5,7 @@
class="mx-4 caret-light-50 dark:caret-dark-50 text-light-100 dark:text-dark-100 placeholder:text-light-50 dark:placeholder:text-dark-50 class="mx-4 caret-light-50 dark:caret-dark-50 text-light-100 dark:text-dark-100 placeholder:text-light-50 dark:placeholder:text-dark-50
bg-light-20 dark:bg-dark-20 appearance-none outline-none px-3 py-1 focus:shadow-raw transition-[box-shadow] focus:shadow-light-40 dark:focus:shadow-dark-40 bg-light-20 dark:bg-dark-20 appearance-none outline-none px-3 py-1 focus:shadow-raw transition-[box-shadow] focus:shadow-light-40 dark:focus:shadow-dark-40
border border-light-35 dark:border-dark-35 hover:border-light-50 dark:hover:border-dark-50 data-[disabled]:bg-light-20 dark:data-[disabled]:bg-dark-20 data-[disabled]:border-light-20 dark:data-[disabled]:border-dark-20" border border-light-35 dark:border-dark-35 hover:border-light-50 dark:hover:border-dark-50 data-[disabled]:bg-light-20 dark:data-[disabled]:bg-dark-20 data-[disabled]:border-light-20 dark:data-[disabled]:border-dark-20"
:type="type" v-model="model" :data-disabled="disabled || undefined" v-bind="$attrs"> :type="type" v-model="model" :data-disabled="disabled || undefined" v-bind="$attrs" @change="(e) => emits('change', e)" @input="(e) => emits('input', e)">
</Label> </Label>
</template> </template>
@ -16,5 +16,10 @@ const { type = 'text', label, disabled = false, placeholder } = defineProps<{
disabled?: boolean disabled?: boolean
placeholder?: string placeholder?: string
}>(); }>();
const emits = defineEmits<{
change: [Event]
input: [Event]
}>();
const model = defineModel<string>(); const model = defineModel<string>();
</script> </script>

BIN
db.sqlite

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,16 +5,26 @@
<div class="flex flex-1 flex-row gap-4 p-6 items-start" v-if="navigation"> <div class="flex flex-1 flex-row gap-4 p-6 items-start" v-if="navigation">
<div class="flex flex-1 flex-col w-[450px] max-w-[450px] max-h-full"> <div class="flex flex-1 flex-col w-[450px] max-w-[450px] max-h-full">
<DraggableTree class="list-none select-none border border-light-35 dark:border-dark-35 text-light-100 dark:text-dark-100 p-2 xl:text-base text-sm overflow-auto" <DraggableTree class="list-none select-none border border-light-35 dark:border-dark-35 text-light-100 dark:text-dark-100 p-2 xl:text-base text-sm overflow-auto"
:items="navigation ?? undefined" :get-key="(item: Partial<ProjectItem>) => item.path !== undefined ? getPath(item as ProjectItem) : ''" @updateTree="drop"> :items="navigation ?? undefined" :get-key="(item: Partial<ProjectExtendedItem>) => item.path !== undefined ? getPath(item as ProjectExtendedItem) : ''" @updateTree="drop">
<template #default="{ handleToggle, handleSelect, isExpanded, isSelected, isDragging, item }"> <template #default="{ handleToggle, handleSelect, isExpanded, isSelected, isDragging, item }">
<div class="flex flex-1 items-center px-2" :class="{ 'opacity-50': isDragging }" :style="{ 'padding-left': `${item.level - 0.5}em` }"> <div class="flex flex-1 items-center px-2" :class="{ 'opacity-50': isDragging }" :style="{ 'padding-left': `${item.level - 0.5}em` }">
<span class="py-2 px-2" @click="handleToggle" v-if="item.hasChildren" > <span class="py-2 px-2" @click="handleToggle" v-if="item.hasChildren" >
<Icon :icon="isExpanded ? 'lucide:folder-open' : 'lucide:folder'"/> <Icon :icon="isExpanded ? 'lucide:folder-open' : 'lucide:folder'"/>
</span> </span>
<Icon v-else-if="iconByType[item.value.type]" :icon="iconByType[item.value.type]" class="group-[:hover]:text-accent-purple mx-2" @click="() => { handleSelect(); selected = isSelected ? undefined : item.value; }" /> <Icon v-else-if="iconByType[item.value.type]" :icon="iconByType[item.value.type]" class="group-[:hover]:text-accent-purple mx-2" @click="() => { handleSelect(); selected = isSelected ? undefined : item.value; }" />
<div class="pl-3 py-1 flex-1 truncate" :title="item.value.title" @click="() => { handleSelect(); selected = isSelected ? undefined : item.value; }"> <div class="pl-3 py-1 flex-1 truncate" :title="item.value.title" @click="() => { handleSelect(); selected = isSelected ? undefined : item.value; }">
{{ item.value.title }} {{ item.value.title }}
</div> </div>
<div class="flex gap-2">
<span @click="item.value.private = !item.value.private">
<Icon v-if="item.value.private" icon="radix-icons:lock-closed" />
<Icon v-else class="text-light-50 dark:text-dark-50" icon="radix-icons:lock-open-2" />
</span>
<span @click="item.value.navigable = !item.value.navigable">
<Icon v-if="item.value.navigable" icon="radix-icons:eye-open" />
<Icon v-else class="text-light-50 dark:text-dark-50" icon="radix-icons:eye-none" />
</span>
</div>
</div> </div>
</template> </template>
<template #hint="{ instruction }"> <template #hint="{ instruction }">
@ -61,11 +71,35 @@
</div> </div>
<div v-if="selected" class="flex-1 flex justify-start items-start"> <div v-if="selected" class="flex-1 flex justify-start items-start">
<div class="flex flex-col flex-1 justify-start items-start"> <div class="flex flex-col flex-1 justify-start items-start">
<input type="text" v-model="selected.title" placeholder="Titre" class="flex-1 mx-4 h-16 w-full caret-light-50 dark:caret-dark-50 text-light-100 dark:text-dark-100 placeholder:text-light-50 dark:placeholder:text-dark-50 appearance-none outline-none px-3 py-1 text-5xl font-thin bg-transparent" /> <input type="text" v-model="selected.title" @change="(e) => {
<span><pre class="ps-6 inline">/{{ selected.path === '' ? getPath(selected) : selected.path }}</pre></span> if(selected && !selected.customPath)
<div class="flex ms-6 flex-col justify-start items-start"> {
<Tooltip message="Consultable uniquement par le propriétaire" side="right"><Switch label="Privé" v-model="selected.private" /></Tooltip> selected.name = parsePath(selected.title);
<Tooltip message="Afficher dans le menu de navigation" side="right"><Switch label="Navigable" v-model="selected.navigable" /></Tooltip> rebuildPath(selected.children, getPath(selected));
}
}" placeholder="Titre" class="flex-1 mx-4 h-16 w-full caret-light-50 dark:caret-dark-50 text-light-100 dark:text-dark-100 placeholder:text-light-50 dark:placeholder:text-dark-50 appearance-none outline-none px-3 py-1 text-5xl font-thin bg-transparent" />
<div class="flex ms-6 flex-col justify-start items-start gap-2">
<div class="flex flex-col justify-start items-start">
<Switch label="Chemin personnalisé" v-model="selected.customPath" />
<span>
<pre v-if="selected.customPath" class="flex items-center">/{{ selected.parent !== '' ? selected.parent + '/' : '' }}<TextInput v-model="selected.name" @input="(e) => {
if(selected && selected.customPath)
{
selected.name = parsePath(selected.name);
rebuildPath(selected.children, getPath(selected));
}
}" class="mx-0"/></pre>
<pre v-else>/{{ getPath(selected) }}</pre>
</span>
</div>
<div class="flex items-center gap-2">
<HoverCard class="!py-2 !px-4"><Icon icon="radix-icons:question-mark-circled" /><template #content><span class="text-sm italic text-light-60 dark:text-dark-60">Un fichier privé n'est consultable que par le propriétaire du projet. Rendre un dossier privé cache automatiquement son contenu sans avoir à chaque fichier un par un.</span></template></HoverCard>
<Switch label="Privé" v-model="selected.private" />
</div>
<div class="flex items-center gap-2">
<HoverCard class="!py-2 !px-4"><Icon icon="radix-icons:question-mark-circled" /><template #content><span class="text-sm italic text-light-60 dark:text-dark-60">Un fichier navigable est disponible dans le menu de navigation à gauche. Les fichiers non navigable peuvent toujours être utilisés dans des liens.</span></template></HoverCard>
<Switch label="Navigable" v-model="selected.navigable" />
</div>
</div> </div>
</div> </div>
</div> </div>
@ -73,7 +107,6 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { Icon } from '@iconify/vue/dist/iconify.js'; import { Icon } from '@iconify/vue/dist/iconify.js';
import type { Instruction } from '@atlaskit/pragmatic-drag-and-drop-hitbox/dist/types/tree-item'; import type { Instruction } from '@atlaskit/pragmatic-drag-and-drop-hitbox/dist/types/tree-item';
@ -82,19 +115,34 @@ import type { ProjectItem } from '~/schemas/project';
import type { FileType } from '~/schemas/file'; import type { FileType } from '~/schemas/file';
import { iconByType } from '#shared/general.utils'; import { iconByType } from '#shared/general.utils';
interface ProjectExtendedItem extends ProjectItem
{
customPath: boolean
children?: ProjectExtendedItem[]
}
interface ProjectExtended
{
items: ProjectExtendedItem[]
}
definePageMeta({ definePageMeta({
rights: ['admin', 'editor'], rights: ['admin', 'editor'],
}); });
const router = useRouter(); const router = useRouter();
const route = router.currentRoute;
const path = computed(() => Array.isArray(route.value.params.path) ? route.value.params.path[0] : route.value.params.path);
const toaster = useToast(); const toaster = useToast();
const saveStatus = ref<'idle' | 'pending' | 'success' | 'error'>('idle'); const saveStatus = ref<'idle' | 'pending' | 'success' | 'error'>('idle');
const { data: project } = await useFetch(`/api/project`); const { data: project } = await useFetch(`/api/project`, {
const navigation = computed<ProjectItem[] | undefined>({ transform: (project) =>{
if(project)
(project as ProjectExtended).items = transform(project.items)!;
return project as ProjectExtended;
}
});
const navigation = computed<ProjectExtendedItem[] | undefined>({
get: () => project.value?.items, get: () => project.value?.items,
set: (value) => { set: (value) => {
const proj = project.value; const proj = project.value;
@ -105,16 +153,17 @@ const navigation = computed<ProjectItem[] | undefined>({
project.value = proj; project.value = proj;
} }
}); });
const selected = ref<ProjectItem>(); const selected = ref<ProjectExtendedItem>();
useShortcuts({ useShortcuts({
meta_s: { usingInput: true, handler: () => save(false) }, meta_s: { usingInput: true, handler: () => save(false) },
meta_n: { usingInput: true, handler: () => add('markdown') }, meta_n: { usingInput: true, handler: () => add('markdown') },
meta_shift_z: { usingInput: true, handler: () => router.push({ name: 'explore-path', params: { path: path.value }}) } meta_shift_n: { usingInput: true, handler: () => add('folder') },
meta_shift_z: { usingInput: true, handler: () => router.push({ name: 'explore' }) }
}) })
const tree = { const tree = {
remove(data: ProjectItem[], id: string): ProjectItem[] { remove(data: ProjectExtendedItem[], id: string): ProjectExtendedItem[] {
return data return data
.filter(item => getPath(item) !== id) .filter(item => getPath(item) !== id)
.map((item) => { .map((item) => {
@ -127,7 +176,7 @@ const tree = {
return item; return item;
}); });
}, },
insertBefore(data: ProjectItem[], targetId: string, newItem: ProjectItem): ProjectItem[] { insertBefore(data: ProjectExtendedItem[], targetId: string, newItem: ProjectExtendedItem): ProjectExtendedItem[] {
return data.flatMap((item) => { return data.flatMap((item) => {
if (getPath(item) === targetId) if (getPath(item) === targetId)
return [newItem, item]; return [newItem, item];
@ -141,7 +190,7 @@ const tree = {
return item; return item;
}); });
}, },
insertAfter(data: ProjectItem[], targetId: string, newItem: ProjectItem): ProjectItem[] { insertAfter(data: ProjectExtendedItem[], targetId: string, newItem: ProjectExtendedItem): ProjectExtendedItem[] {
return data.flatMap((item) => { return data.flatMap((item) => {
if (getPath(item) === targetId) if (getPath(item) === targetId)
return [item, newItem]; return [item, newItem];
@ -156,7 +205,7 @@ const tree = {
return item; return item;
}); });
}, },
insertChild(data: ProjectItem[], targetId: string, newItem: ProjectItem): ProjectItem[] { insertChild(data: ProjectExtendedItem[], targetId: string, newItem: ProjectExtendedItem): ProjectExtendedItem[] {
return data.flatMap((item) => { return data.flatMap((item) => {
if (getPath(item) === targetId) { if (getPath(item) === targetId) {
// already a parent: add as first child // already a parent: add as first child
@ -177,7 +226,7 @@ const tree = {
}; };
}); });
}, },
find(data: ProjectItem[], itemId: string): ProjectItem | undefined { find(data: ProjectExtendedItem[], itemId: string): ProjectExtendedItem | undefined {
for (const item of data) { for (const item of data) {
if (getPath(item) === itemId) if (getPath(item) === itemId)
return item; return item;
@ -189,7 +238,7 @@ const tree = {
} }
} }
}, },
search(data: ProjectItem[], prop: keyof ProjectItem, value: string): ProjectItem[] { search(data: ProjectExtendedItem[], prop: keyof ProjectExtendedItem, value: string): ProjectExtendedItem[] {
const arr = []; const arr = [];
for (const item of data) for (const item of data)
@ -209,7 +258,7 @@ const tree = {
targetId, targetId,
parentIds = [], parentIds = [],
}: { }: {
current: ProjectItem[] current: ProjectExtendedItem[]
targetId: string targetId: string
parentIds?: string[] parentIds?: string[]
}): string[] | undefined { }): string[] | undefined {
@ -226,7 +275,7 @@ const tree = {
return nested; return nested;
} }
}, },
hasChildren(item: ProjectItem): boolean { hasChildren(item: ProjectExtendedItem): boolean {
return (item.children ?? []).length > 0; return (item.children ?? []).length > 0;
}, },
} }
@ -239,7 +288,8 @@ function add(type: FileType): void
} }
const news = [...tree.search(navigation.value, 'title', 'Nouveau')].filter((e, i, a) => a.indexOf(e) === i); const news = [...tree.search(navigation.value, 'title', 'Nouveau')].filter((e, i, a) => a.indexOf(e) === i);
const item: ProjectItem = { navigable: true, private: false, parent: '', path: '', title: `Nouveau${news.length > 0 ? ' (' + news.length +')' : ''}`, type: type, order: 0, children: type === 'folder' ? [] : undefined }; const title = `Nouveau${news.length > 0 ? ' (' + news.length +')' : ''}`;
const item: ProjectExtendedItem = { navigable: true, private: false, parent: '', path: '', title: title, name: parsePath(title), type: type, order: 0, children: type === 'folder' ? [] : undefined, customPath: false };
if(!selected.value) if(!selected.value)
{ {
@ -255,7 +305,7 @@ function add(type: FileType): void
navigation.value = tree.insertAfter(navigation.value, getPath(selected.value), item); navigation.value = tree.insertAfter(navigation.value, getPath(selected.value), item);
} }
} }
function updateTree(instruction: Instruction, itemId: string, targetId: string) : ProjectItem[] | undefined { function updateTree(instruction: Instruction, itemId: string, targetId: string) : ProjectExtendedItem[] | undefined {
if(!navigation.value) if(!navigation.value)
return; return;
@ -309,12 +359,15 @@ function updateTree(instruction: Instruction, itemId: string, targetId: string)
return navigation.value; return navigation.value;
} }
function transform(items: ProjectItem[] | undefined): ProjectExtendedItem[] | undefined
{
return items?.map(e => ({...e, customPath: e.name !== parsePath(e.title), children: transform(e.children)}));
}
function drop(instruction: Instruction, itemId: string, targetId: string) function drop(instruction: Instruction, itemId: string, targetId: string)
{ {
navigation.value = updateTree(instruction, itemId, targetId) ?? navigation.value ?? []; navigation.value = updateTree(instruction, itemId, targetId) ?? navigation.value ?? [];
} }
function rebuildPath(tree: ProjectItem[] | null | undefined, parentPath: string) function rebuildPath(tree: ProjectExtendedItem[] | null | undefined, parentPath: string)
{ {
if(!tree) if(!tree)
return; return;
@ -339,7 +392,7 @@ async function save(redirect: boolean): Promise<void>
type: 'success', content: 'Contenu enregistré', timer: true, duration: 10000 type: 'success', content: 'Contenu enregistré', timer: true, duration: 10000
}); });
if(redirect) router.push({ name: 'explore-path', params: { path: path.value } }); if(redirect) router.push({ name: 'explore' });
} catch(e: any) { } catch(e: any) {
toaster.add({ toaster.add({
type: 'error', content: e.message, timer: true, duration: 10000 type: 'error', content: e.message, timer: true, duration: 10000
@ -347,8 +400,8 @@ async function save(redirect: boolean): Promise<void>
saveStatus.value = 'error'; saveStatus.value = 'error';
} }
} }
function getPath(item: ProjectItem): string function getPath(item: ProjectExtendedItem): string
{ {
return [item.parent, parsePath(item.title)].filter(e => !!e).join('/'); return [item.parent, parsePath(item.customPath ? item.name : item.title)].filter(e => !!e).join('/');
} }
</script> </script>

View File

@ -4,6 +4,7 @@ import { fileType } from "./file";
const baseItem = z.object({ const baseItem = z.object({
path: z.string(), path: z.string(),
parent: z.string(), parent: z.string(),
name: z.string(),
title: z.string(), title: z.string(),
type: fileType, type: fileType,
navigable: z.boolean(), navigable: z.boolean(),

View File

@ -26,6 +26,10 @@ export default defineEventHandler(async (e) => {
order: explorerContentTable.order, order: explorerContentTable.order,
}).from(explorerContentTable).prepare().all(); }).from(explorerContentTable).prepare().all();
content.sort((a, b) => {
return a.path.split('/').length - b.path.split('/').length;
});
if(content.length > 0) if(content.length > 0)
{ {
const project: Project = { const project: Project = {

View File

@ -28,7 +28,7 @@ export default defineEventHandler(async (e) => {
for(let i = full.length - 1; i >= 0; i--) for(let i = full.length - 1; i >= 0; i--)
{ {
if(items.find(e => (e.path === '' ? [e.parent, parsePath(e.title)].filter(e => !!e).join('/') : e.path) === full[i].path)) if(items.find(e => (e.path === '' ? [e.parent, parsePath(e.name === '' ? e.title : e.name)].filter(e => !!e).join('/') : e.path) === full[i].path))
full.splice(i, 1); full.splice(i, 1);
} }
@ -48,7 +48,7 @@ export default defineEventHandler(async (e) => {
content: null, content: null,
}).onConflictDoUpdate({ }).onConflictDoUpdate({
set: { set: {
path: [item.parent, parsePath(item.title)].filter(e => !!e).join('/'), path: [item.parent, parsePath(item.name === '' ? item.title : item.name)].filter(e => !!e).join('/'),
title: item.title, title: item.title,
type: item.type, type: item.type,
navigable: item.navigable, navigable: item.navigable,

View File

@ -35,7 +35,7 @@ export default defineTask({
const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/'); const path = (e.path as string).split('/').map(f => { const check = /(\d+)\. ?(.+)/gsmi.exec(f); return check && check[2] ? check[2] : f }).join('/');
return { return {
path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""), path: path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
//order: order && order[1] ? order[1] : 50, order: order && order[1] ? order[1] : 0,
title: order && order[2] ? order[2] : title, title: order && order[2] ? order[2] : title,
type: 'folder', type: 'folder',
content: null, content: null,
@ -53,7 +53,7 @@ export default defineTask({
return { return {
path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""), path: (extension === '.md' ? path.replace(extension, '') : path).toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""),
//order: order && order[1] ? order[1] : 50, order: order && order[1] ? order[1] : 0,
title: order && order[2] ? order[2] : title, title: order && order[2] ? order[2] : title,
type: (typeMapping[extension] ?? 'file'), type: (typeMapping[extension] ?? 'file'),
content: reshapeContent(content as string, typeMapping[extension] ?? 'File'), content: reshapeContent(content as string, typeMapping[extension] ?? 'File'),

View File

@ -6,7 +6,7 @@ export function unifySlug(slug: string | string[]): string
} }
export function parsePath(path: string): string export function parsePath(path: string): string
{ {
return path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replace(/[\u0300-\u036f]/g, ""); return path.toLowerCase().replaceAll(" ", "-").normalize("NFD").replaceAll(/[\u0300-\u036f]/g, "").replaceAll('(', '').replaceAll(')', '');
} }
export function parseId(id: string | undefined): string |undefined export function parseId(id: string | undefined): string |undefined
{ {