π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
Better organisation for helpers functions
Smaug673950b503f04816
diff --git a/frontend/app/components/MarkdownEditor/ColorPickerModal.vue b/frontend/app/components/MarkdownEditor/ColorPickerModal.vueindex 28056074..68f2fd94 100644--- a/frontend/app/components/MarkdownEditor/ColorPickerModal.vue+++ b/frontend/app/components/MarkdownEditor/ColorPickerModal.vue@@ -86,7 +86,7 @@<script setup lang="ts">import EditorAppHeader from './EditorAppHeader.vue';-import { hsvToHex, drawColorWheel } from '~/helpers/colors';+import { hsvToHex } from '~/helpers/colors';const props = defineProps<{ onColorSelect: (color: string) => void }>();const emit = defineEmits<{ (e: 'close'): void }>();@@ -115,6 +115,35 @@ const stopColorSelection = () => {isSelecting.value = false;};+function drawColorWheel(colorWheel: Ref<HTMLCanvasElement | undefined>) {+ if (!colorWheel.value) return;+ const canvas = colorWheel.value,+ ctx = canvas.getContext('2d');+ if (!ctx) return;+ const w = canvas.width,+ h = canvas.height,+ cx = w / 2,+ cy = h / 2,+ r = Math.min(cx, cy) - 10;+ ctx.clearRect(0, 0, w, h);+ for (let y = 0; y < h; y++)+ for (let x = 0; x < w; x++) {+ const dx = x - cx,+ dy = y - cy,+ dist = Math.sqrt(dx * dx + dy * dy);+ if (dist <= r) {+ const hue = ((Math.atan2(dy, dx) * 180) / Math.PI + 360) % 360;+ ctx.fillStyle = hsvToHex(hue, (dist / r) * 100, 100);+ ctx.fillRect(x, y, 1, 1);+ }+ }+ ctx.strokeStyle = 'var(--border-color)';+ ctx.lineWidth = 2;+ ctx.beginPath();+ ctx.arc(cx, cy, r, 0, 2 * Math.PI);+ ctx.stroke();+}+const updateColorSelection = (e: MouseEvent) => {if (!isSelecting.value || !colorWheel.value) return;const rect = colorWheel.value.getBoundingClientRect();diff --git a/frontend/app/helpers/colors.ts b/frontend/app/helpers/colors.tsindex 8fee7d20..7ff21f8b 100644--- a/frontend/app/helpers/colors.ts+++ b/frontend/app/helpers/colors.ts@@ -1,33 +1,4 @@-function drawColorWheel(colorWheel: Ref<HTMLCanvasElement | undefined>) {- if (!colorWheel.value) return;- const canvas = colorWheel.value,- ctx = canvas.getContext('2d');- if (!ctx) return;- const w = canvas.width,- h = canvas.height,- cx = w / 2,- cy = h / 2,- r = Math.min(cx, cy) - 10;- ctx.clearRect(0, 0, w, h);- for (let y = 0; y < h; y++)- for (let x = 0; x < w; x++) {- const dx = x - cx,- dy = y - cy,- dist = Math.sqrt(dx * dx + dy * dy);- if (dist <= r) {- const hue = ((Math.atan2(dy, dx) * 180) / Math.PI + 360) % 360;- ctx.fillStyle = hsvToHex(hue, (dist / r) * 100, 100);- ctx.fillRect(x, y, 1, 1);- }- }- ctx.strokeStyle = 'var(--border-color)';- ctx.lineWidth = 2;- ctx.beginPath();- ctx.arc(cx, cy, r, 0, 2 * Math.PI);- ctx.stroke();-}--function hsvToHex(h: number, s: number, v: number): string {+export function hsvToHex(h: number, s: number, v: number): string {s /= 100;v /= 100;const c = v * s,@@ -46,5 +17,3 @@ function hsvToHex(h: number, s: number, v: number): string {.toUpperCase());}--export { drawColorWheel, hsvToHex };diff --git a/frontend/app/helpers/importations.ts b/frontend/app/helpers/importations.tsdeleted file mode 100644index 994443ec..00000000--- a/frontend/app/helpers/importations.ts+++ /dev/null@@ -1,71 +0,0 @@-import type { DB_Node } from '~/stores';--interface ImportationStructure {- nodes: DB_Node[];-}--function analyseFile(file: File): Promise<ImportationStructure> {- return new Promise((resolve, reject) => {- const reader = new FileReader();- reader.onload = event => {- const content = event.target?.result;- if (typeof content === 'string') {- try {- const data = JSON.parse(content);- resolve(data);- } catch {- reject(new Error('Invalid JSON format'));- }- } else {- reject(new Error('File content is not a string'));- }- };- reader.onerror = () => {- reject(new Error('Error reading file'));- };- reader.readAsText(file);- });-}--function validateFileStructure(data: ImportationStructure): data is ImportationStructure {- if (typeof data === 'object' && data !== null && Array.isArray(data.nodes)) {- return true;- }- return false;-}--function compareDocumentsAndLocal(data: ImportationStructure) {- const diff: DB_Node[] = [];- const local = useSidebarTree().structure.value;- for (const doc of data.nodes) {- const localDoc = local.getItem(doc.id);- if (!localDoc) {- diff.push(doc);- }- }- return diff;-}--function prepareNewDocuments(new_docs: Partial<DB_Node>[]) {- // For each document, check if parent_id and category are valid- // If not, set them to null- const local = useSidebarTree().structure.value;- for (const doc of new_docs) {- if (doc.parent_id && !local.getItem(doc.parent_id)) {- doc.parent_id = undefined;- }- }-}--async function uploadDocument(document: DB_Node) {- const documentsStore = useNodesStore();- await documentsStore.post(document);-}-async function uploadDocuments(documents: DB_Node[]) {- const documentsStore = useNodesStore();- for (const doc of documents) {- await documentsStore.post(doc);- }-}--export { analyseFile, validateFileStructure, compareDocumentsAndLocal, prepareNewDocuments, uploadDocument, uploadDocuments };diff --git a/frontend/app/helpers/node.ts b/frontend/app/helpers/node.tsindex db500467..864a9492 100644--- a/frontend/app/helpers/node.ts+++ b/frontend/app/helpers/node.ts@@ -2,7 +2,7 @@* Node utilities - centralized helpers for working with nodes (documents, categories, workspaces, resources)* Roles: 1 = Workspace, 2 = Category, 3 = Document, 4 = Resource*/-import type { Node } from '~/stores';+import type { DB_Node, Node } from '~/stores';/** Resolve the appropriate icon for a node based on its role and properties */export const resolveIcon = (item: Node): string => {@@ -27,3 +27,62 @@ export const resolveNodeType = (node: Node): string => {const types: Record<number, string> = { 1: 'Workspace', 2: 'Category', 3: 'Document', 4: 'Resource' };return types[node.role] || '';};++// Node importation structure+interface ImportationStructure {+ nodes: DB_Node[];+}++// Analyze the uploaded file and parse its content as JSON+export function analyseFile(file: File): Promise<ImportationStructure> {+ return new Promise((resolve, reject) => {+ const reader = new FileReader();+ reader.onload = event => {+ const content = event.target?.result;+ if (typeof content === 'string') {+ try {+ const data = JSON.parse(content);+ resolve(data);+ } catch {+ reject(new Error('Invalid JSON format'));+ }+ } else {+ reject(new Error('File content is not a string'));+ }+ };+ reader.onerror = () => {+ reject(new Error('Error reading file'));+ };+ reader.readAsText(file);+ });+}++export function validateFileStructure(data: ImportationStructure): data is ImportationStructure {+ if (typeof data === 'object' && data !== null && Array.isArray(data.nodes)) {+ return true;+ }+ return false;+}++export function compareDocumentsAndLocal(data: ImportationStructure) {+ const diff: DB_Node[] = [];+ const local = useSidebarTree().structure.value;+ for (const doc of data.nodes) {+ const localDoc = local.getItem(doc.id);+ if (!localDoc) {+ diff.push(doc);+ }+ }+ return diff;+}++export function prepareNewDocuments(new_docs: Partial<DB_Node>[]) {+ // For each document, check if parent_id and category are valid+ // If not, set them to null+ const local = useSidebarTree().structure.value;+ for (const doc of new_docs) {+ if (doc.parent_id && !local.getItem(doc.parent_id)) {+ doc.parent_id = undefined;+ }+ }+}diff --git a/frontend/app/helpers/utils.ts b/frontend/app/helpers/utils.tsindex 25b7945a..45cbd8c3 100644--- a/frontend/app/helpers/utils.ts+++ b/frontend/app/helpers/utils.ts@@ -3,17 +3,6 @@* Contains hash-based color generation and user agent parsing*/-const colors = ['var(--blue)', 'var(--teal)', 'var(--yellow)', 'var(--red)', 'var(--red)'];--/** Generate a consistent color from a string (useful for avatars, labels) */-export function useColorHash(str: string, list = colors): string {- let hash = 0;- for (let i = 0; i < str.length; i++) {- hash = str.charCodeAt(i) + ((hash << 5) - hash);- }- return list[Math.abs(hash % list.length)] as string;-}-/** Parse a user agent string to extract OS and browser info */export function parseUserAgent(uaString?: string) {if (!uaString) return { os: 'Unknown OS', browser: 'Unknown Browser' };diff --git a/frontend/app/pages/dashboard/import.vue b/frontend/app/pages/dashboard/import.vueindex 5b659835..a7abaf2e 100644--- a/frontend/app/pages/dashboard/import.vue+++ b/frontend/app/pages/dashboard/import.vue@@ -33,7 +33,7 @@</template><script setup lang="ts">-import { analyseFile, validateFileStructure, compareDocumentsAndLocal, prepareNewDocuments, uploadDocument, uploadDocuments } from '~/helpers/importations';+import { analyseFile, validateFileStructure, compareDocumentsAndLocal, prepareNewDocuments } from '~/helpers/node';import type { DB_Node } from '~/stores';definePageMeta({ breadcrumb: 'Importations' });@@ -58,6 +58,18 @@ async function submit() {selectedFile.value = undefined; // Reset selected file after processing}}++async function uploadDocument(document: DB_Node) {+ const documentsStore = useNodesStore();+ await documentsStore.post(document);+}+async function uploadDocuments(documents: DB_Node[]) {+ const documentsStore = useNodesStore();+ for (const doc of documents) {+ await documentsStore.post(doc);+ }+}+function importDoc(file: DB_Node) {const fileContent = files_to_import.value.find(i => i.id === file.id);if (!fileContent) return useNotifications().add({ type: 'error', title: 'File not found' });