Fix folder editing and add links updates on file rename

This commit is contained in:
Peaceultime 2024-12-17 20:36:51 +01:00
parent 36909c5d66
commit 0abf0b11e6
4 changed files with 11 additions and 9 deletions

View File

@ -10,7 +10,7 @@ const baseItem = z.object({
navigable: z.boolean(), navigable: z.boolean(),
private: z.boolean(), private: z.boolean(),
order: z.number().finite(), order: z.number().finite(),
content: z.string().optional(), content: z.string().optional().or(z.null()),
}); });
export const item: z.ZodType<ProjectItem> = baseItem.extend({ export const item: z.ZodType<ProjectItem> = baseItem.extend({
children: z.lazy(() => item.array().optional()), children: z.lazy(() => item.array().optional()),

View File

@ -31,7 +31,7 @@ export function convertToStorableLinks(content: string, path: string[]): string
return content.replaceAll(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/g, (e: string, a1?: string, a2?: string , a3?: string) => { return content.replaceAll(/!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/g, (e: string, a1?: string, a2?: string , a3?: string) => {
const parsed = parsePath(a1 ?? '%%%%----%%%%----%%%%'); const parsed = parsePath(a1 ?? '%%%%----%%%%----%%%%');
const replacer = path.find(e => e.endsWith(parsed)); const replacer = path.find(e => e.endsWith(parsed));
const value = `[[${replacer ?? ''}${a2 ?? ''}${(!a3 && replacer !== parsed ? '|' + a1 : a3) ?? ''}]]`; const value = `[[${a1 ? (replacer ?? '') : ''}${a2 ?? ''}${(!a3 && a1 && replacer !== parsed ? '|' + a1 : a3) ?? ''}]]`;
return value; return value;
}); });
} }

View File

@ -50,11 +50,6 @@ export default defineEventHandler(async (e) => {
content.content = convertFromStorableLinks(content.content); content.content = convertFromStorableLinks(content.content);
} }
if(session && session.user && session.user.id !== content.owner)
{
content.content = content.content.replace(/%%(.+)%%/g, "");
}
return { content: content.content }; return { content: content.content };
} }

View File

@ -3,7 +3,7 @@ import useDatabase from '~/composables/useDatabase';
import { explorerContentTable } from '~/db/schema'; import { explorerContentTable } from '~/db/schema';
import { project, type ProjectItem } from '~/schemas/project'; import { project, type ProjectItem } from '~/schemas/project';
import { parsePath } from "#shared/general.utils"; import { parsePath } from "#shared/general.utils";
import { eq, getTableColumns } from "drizzle-orm"; import { eq, getTableColumns, sql } from "drizzle-orm";
export default defineEventHandler(async (e) => { export default defineEventHandler(async (e) => {
const { user } = await getUserSession(e); const { user } = await getUserSession(e);
@ -42,6 +42,8 @@ export default defineEventHandler(async (e) => {
const item = items[i]; const item = items[i];
const old = full[item.match!]; const old = full[item.match!];
const path = [item.parent, parsePath(item.name === '' ? item.title : item.name)].filter(e => !!e).join('/');
tx.insert(explorerContentTable).values({ tx.insert(explorerContentTable).values({
path: item.path, path: item.path,
owner: user.id, owner: user.id,
@ -53,7 +55,7 @@ export default defineEventHandler(async (e) => {
content: item.content ?? old.content, content: item.content ?? old.content,
}).onConflictDoUpdate({ }).onConflictDoUpdate({
set: { set: {
path: [item.parent, parsePath(item.name === '' ? item.title : item.name)].filter(e => !!e).join('/'), path: path,
title: item.title, title: item.title,
type: item.type, type: item.type,
navigable: item.navigable, navigable: item.navigable,
@ -64,6 +66,11 @@ export default defineEventHandler(async (e) => {
}, },
target: explorerContentTable.path, target: explorerContentTable.path,
}).run(); }).run();
if(item.path !== path)
{
tx.update(explorerContentTable).set({ content: sql`replace(${explorerContentTable.content}, ${sql.placeholder('old')}, ${sql.placeholder('new')})` }).prepare().run({ 'old': item.path, 'new': path });
}
} }
for(let i = 0; i < full.length; i++) for(let i = 0; i < full.length; i++)
{ {