mirror of
https://gitlab.com/ArkHost/HelixNotes.git
synced 2026-11-21 11:04:56 +01:00
462 lines
14 KiB
Svelte
462 lines
14 KiB
Svelte
<script lang="ts">
|
|
import { t as ui, tr as uiText } from '$lib/i18n';
|
|
import { onMount, onDestroy, untrack } from 'svelte';
|
|
import { listen } from '@tauri-apps/api/event';
|
|
import { getCurrentWebview } from '@tauri-apps/api/webview';
|
|
import { getCurrentWindow } from '@tauri-apps/api/window';
|
|
import Editor from './Editor.svelte';
|
|
import EditorModePicker from './EditorModePicker.svelte';
|
|
import {
|
|
appConfig,
|
|
activeNote,
|
|
activeNotePath,
|
|
editorDirty,
|
|
readOnly,
|
|
sourceMode,
|
|
tagStyles
|
|
} from '$lib/stores/app';
|
|
import { completeWindowClose, cancelWindowClose, getTagStyles, readNote } from '$lib/api';
|
|
import { keybindings, matchAction } from '$lib/keybindings';
|
|
import { isMobile } from '$lib/platform';
|
|
import type { FileEvent } from '$lib/types';
|
|
import { isCurrentVaultEvent } from '$lib/utils/note-list-updates';
|
|
import { beginNoteNavigation, isCurrentNoteNavigation, cancelNoteNavigation } from '$lib/utils/note-navigation';
|
|
import { mcpBusy } from '$lib/utils/mcp-editor';
|
|
|
|
let { notePath, vaultPath }: { notePath: string; vaultPath: string | null } = $props();
|
|
const ownerVault = untrack(() => vaultPath);
|
|
|
|
const appWindow = getCurrentWindow();
|
|
const appWebview = getCurrentWebview();
|
|
const isMac = navigator.platform.startsWith('Mac');
|
|
let editor = $state<Editor>(null!);
|
|
let unlistenFileChange: (() => void) | null = null;
|
|
let unlistenUiScale: (() => void) | null = null;
|
|
let unlistenClose: (() => void) | null = null;
|
|
let unlistenVaultRestored: (() => void) | null = null;
|
|
let removeMobileFlushListeners: (() => void) | null = null;
|
|
let maximized = $state(false);
|
|
let loadError = $state<string | null>(null);
|
|
let disposed = false;
|
|
let initialNavigationToken: number | null = null;
|
|
let initialLoadTimer: ReturnType<typeof setTimeout> | null = null;
|
|
let fileRefreshRequest = 0;
|
|
|
|
async function checkMaximized() {
|
|
const value = await appWindow.isMaximized();
|
|
if (!disposed) maximized = value;
|
|
}
|
|
|
|
$effect(() => {
|
|
checkMaximized();
|
|
const unlisten = appWindow.onResized(() => checkMaximized());
|
|
return () => { unlisten.then(fn => fn()); };
|
|
});
|
|
$effect(() => {
|
|
const title = $activeNote?.meta.title;
|
|
if (title && !disposed) void appWindow.setTitle(`${title} - HelixNotes`);
|
|
});
|
|
|
|
|
|
let lastMouseDown = 0;
|
|
const RESIZE_EDGE = 6;
|
|
|
|
function handleMouseDown(e: MouseEvent) {
|
|
if (e.button !== 0) return;
|
|
const target = e.target as HTMLElement;
|
|
if (target.closest('.titlebar-controls') || target.closest('.titlebar-actions')) return;
|
|
|
|
if (!maximized) {
|
|
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
|
if (e.clientY - rect.top < RESIZE_EDGE || e.clientX - rect.left < RESIZE_EDGE) return;
|
|
}
|
|
|
|
const now = Date.now();
|
|
if (now - lastMouseDown < 300) {
|
|
appWindow.toggleMaximize();
|
|
lastMouseDown = 0;
|
|
return;
|
|
}
|
|
lastMouseDown = now;
|
|
appWindow.startDragging();
|
|
}
|
|
|
|
function handleKeydown(e: KeyboardEvent) {
|
|
const action = matchAction(e, $keybindings);
|
|
if (action === 'save') {
|
|
e.preventDefault();
|
|
editor?.forceSave();
|
|
} else if (action === 'toggle-source') {
|
|
e.preventDefault();
|
|
$sourceMode = !$sourceMode;
|
|
}
|
|
}
|
|
|
|
async function applyUiScale(scale: number) {
|
|
try {
|
|
await appWebview.setZoom(scale);
|
|
} catch (e) {
|
|
console.error('Failed to apply interface scale to note window:', e);
|
|
}
|
|
}
|
|
|
|
onMount(() => {
|
|
const vault = ownerVault;
|
|
const path = notePath;
|
|
const token = beginNoteNavigation();
|
|
initialNavigationToken = token;
|
|
const isCurrentOwner = () => !disposed && (vault === null || vault === $appConfig?.active_vault);
|
|
if (isMobile) {
|
|
const flush = () => {
|
|
if (isCurrentOwner()) void editor?.flushSave();
|
|
};
|
|
const onVisibilityChange = () => {
|
|
if (document.visibilityState === 'hidden') flush();
|
|
};
|
|
document.addEventListener('visibilitychange', onVisibilityChange);
|
|
window.addEventListener('pagehide', flush);
|
|
window.addEventListener('app-lifecycle-flush', flush);
|
|
removeMobileFlushListeners = () => {
|
|
document.removeEventListener('visibilitychange', onVisibilityChange);
|
|
window.removeEventListener('pagehide', flush);
|
|
window.removeEventListener('app-lifecycle-flush', flush);
|
|
};
|
|
}
|
|
|
|
async function initialize() {
|
|
if (!isMobile) {
|
|
const closeListener = await appWindow.onCloseRequested(async (event) => {
|
|
event.preventDefault();
|
|
if (!isCurrentOwner()) { await cancelWindowClose(); return; }
|
|
try {
|
|
if (editor && !(await editor.flushSave())) { await cancelWindowClose(); return; }
|
|
if (!isCurrentOwner()) { await cancelWindowClose(); return; }
|
|
await completeWindowClose();
|
|
} catch (error) {
|
|
console.error('Failed to close window:', error);
|
|
try { await cancelWindowClose(); } catch (cancelError) { console.error('Failed to cancel window close:', cancelError); }
|
|
}
|
|
});
|
|
if (!isCurrentOwner()) {
|
|
closeListener();
|
|
return;
|
|
}
|
|
unlistenClose = closeListener;
|
|
}
|
|
if (!ownerVault) {
|
|
loadError = uiText("ui.this_note_window_has_no_valid_originating_vault");
|
|
return;
|
|
}
|
|
|
|
const scaleListener = await listen<number>('ui-scale-changed', (event) => {
|
|
if (isCurrentOwner()) void applyUiScale(event.payload);
|
|
});
|
|
if (!isCurrentOwner()) {
|
|
scaleListener();
|
|
return;
|
|
}
|
|
unlistenUiScale = scaleListener;
|
|
await applyUiScale($appConfig?.ui_scale ?? 1);
|
|
if (!isCurrentOwner()) return;
|
|
|
|
try {
|
|
const styles = await getTagStyles(ownerVault!);
|
|
if (!isCurrentOwner()) return;
|
|
$tagStyles = styles;
|
|
} catch (e) {
|
|
console.error('Failed to load tag styles:', e);
|
|
}
|
|
if (!isCurrentOwner()) return;
|
|
|
|
if (isCurrentNoteNavigation(token)) {
|
|
try {
|
|
const content = await readNote(ownerVault!, path);
|
|
if (!isCurrentOwner()) return;
|
|
if (isCurrentNoteNavigation(token)) {
|
|
$activeNote = content;
|
|
$activeNotePath = path;
|
|
$editorDirty = false;
|
|
initialLoadTimer = setTimeout(() => {
|
|
initialLoadTimer = null;
|
|
if (!isCurrentOwner() || !isCurrentNoteNavigation(token)
|
|
|| $activeNotePath !== path || $activeNote !== content || $editorDirty) return;
|
|
editor?.loadNote(path, content.content);
|
|
}, 50);
|
|
}
|
|
} catch (e) {
|
|
if (isCurrentOwner() && isCurrentNoteNavigation(token)) loadError = String(e);
|
|
}
|
|
}
|
|
if (!isCurrentOwner()) return;
|
|
|
|
async function refreshOpenNote(restored = false) {
|
|
const path = $activeNotePath;
|
|
const openingNote = $activeNote;
|
|
if ($mcpBusy || !path || !openingNote) return;
|
|
const request = ++fileRefreshRequest;
|
|
try {
|
|
const content = await readNote(ownerVault!, path);
|
|
if ($mcpBusy || !isCurrentOwner() || request !== fileRefreshRequest || $activeNotePath !== path) return;
|
|
if ($editorDirty) {
|
|
if (restored && content.raw !== editor?.getAcceptedRaw()) editor?.flagExternalConflict();
|
|
return;
|
|
}
|
|
if ($activeNote !== openingNote || openingNote.raw === content.raw) return;
|
|
$activeNote = content;
|
|
editor?.loadNote(path, content.content);
|
|
} catch (error) {
|
|
if ($mcpBusy) return;
|
|
if (restored && isCurrentOwner() && request === fileRefreshRequest && $activeNotePath === path) {
|
|
if ($editorDirty) editor?.flagExternalConflict();
|
|
else {
|
|
$activeNote = null;
|
|
$activeNotePath = null;
|
|
loadError = uiText("ui.this_note_was_removed_by_the_vault_restore");
|
|
}
|
|
} else if (!restored) console.error('Failed to refresh note window:', error);
|
|
}
|
|
}
|
|
const fileListener = await listen<FileEvent>('file-changed', (event) => {
|
|
if (!isCurrentOwner() || !isCurrentVaultEvent(event.payload, vault ?? null)) return;
|
|
if (event.payload.note?.path === $activeNotePath) void refreshOpenNote();
|
|
});
|
|
if (!isCurrentOwner()) {
|
|
fileListener();
|
|
return;
|
|
}
|
|
unlistenFileChange = fileListener;
|
|
unlistenVaultRestored = await listen<{ vault_path: string }>('vault-restored', ({ payload }) => {
|
|
if (isCurrentOwner() && payload.vault_path === vault) void refreshOpenNote(true);
|
|
});
|
|
if (!isCurrentOwner()) { unlistenVaultRestored(); return; }
|
|
}
|
|
|
|
void initialize().catch(error => {
|
|
if (isCurrentOwner()) console.error('Failed to initialize note window:', error);
|
|
});
|
|
});
|
|
|
|
onDestroy(() => {
|
|
disposed = true;
|
|
if (initialNavigationToken !== null) cancelNoteNavigation(initialNavigationToken);
|
|
if (initialLoadTimer !== null) clearTimeout(initialLoadTimer);
|
|
unlistenFileChange?.();
|
|
unlistenVaultRestored?.();
|
|
removeMobileFlushListeners?.();
|
|
unlistenUiScale?.();
|
|
unlistenClose?.();
|
|
});
|
|
</script>
|
|
|
|
<svelte:window onkeydown={handleKeydown} />
|
|
|
|
<div class="note-window">
|
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
<div class="nw-titlebar" class:macos={isMac} onmousedown={handleMouseDown}>
|
|
<div class="nw-titlebar-brand">
|
|
<svg width="14" height="14" viewBox="0 0 48 48" fill="none">
|
|
<rect width="48" height="48" rx="12" fill="var(--accent)" />
|
|
<circle cx="16" cy="16" r="3.5" fill="white" opacity="0.9" />
|
|
<circle cx="32" cy="16" r="3.5" fill="white" opacity="0.9" />
|
|
<circle cx="16" cy="32" r="3.5" fill="white" opacity="0.9" />
|
|
<circle cx="32" cy="32" r="3.5" fill="white" opacity="0.9" />
|
|
<line x1="19" y1="18" x2="29" y2="30" stroke="white" stroke-width="2" stroke-linecap="round" opacity="0.7" />
|
|
<line x1="29" y1="18" x2="19" y2="30" stroke="white" stroke-width="2" stroke-linecap="round" opacity="0.7" />
|
|
</svg>
|
|
<span class="nw-title">{$activeNote?.meta.title || 'Loading...'}</span>
|
|
{#if $editorDirty}
|
|
<span class="nw-dirty-dot" title={$ui("ui.unsaved_changes")}></span>
|
|
{/if}
|
|
</div>
|
|
<div class="titlebar-actions">
|
|
<EditorModePicker id="detached-editor-mode" />
|
|
<button class="nw-btn" class:active={$readOnly} onclick={() => ($readOnly = !$readOnly)} title={$readOnly ? $ui("ui.switch_to_edit_mode") : $ui("ui.switch_to_view_mode")}>
|
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
{#if $readOnly}
|
|
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
|
|
<circle cx="12" cy="12" r="3" />
|
|
{:else}
|
|
<path d="M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94" />
|
|
<path d="M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19" />
|
|
<line x1="1" y1="1" x2="23" y2="23" />
|
|
{/if}
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
{#if !isMac}
|
|
<div class="titlebar-controls">
|
|
<button class="titlebar-btn" onclick={() => appWindow.minimize()} title={$ui("ui.minimize")}>
|
|
<svg width="10" height="10" viewBox="0 0 10 10">
|
|
<line x1="1" y1="5" x2="9" y2="5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
|
|
</svg>
|
|
</button>
|
|
<button class="titlebar-btn" onclick={() => appWindow.toggleMaximize()} title={maximized ? $ui("ui.restore") : $ui("ui.maximize")}>
|
|
{#if maximized}
|
|
<svg width="10" height="10" viewBox="0 0 10 10">
|
|
<rect x="2.5" y="0.5" width="7" height="7" rx="1" fill="none" stroke="currentColor" stroke-width="1.2" />
|
|
<rect x="0.5" y="2.5" width="7" height="7" rx="1" fill="var(--bg-secondary)" stroke="currentColor" stroke-width="1.2" />
|
|
</svg>
|
|
{:else}
|
|
<svg width="10" height="10" viewBox="0 0 10 10">
|
|
<rect x="1" y="1" width="8" height="8" rx="1" fill="none" stroke="currentColor" stroke-width="1.2" />
|
|
</svg>
|
|
{/if}
|
|
</button>
|
|
<button class="titlebar-btn titlebar-close" onclick={() => appWindow.close()} title={$ui("ui.close")}>
|
|
<svg width="10" height="10" viewBox="0 0 10 10">
|
|
<line x1="1.5" y1="1.5" x2="8.5" y2="8.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
|
|
<line x1="8.5" y1="1.5" x2="1.5" y2="8.5" stroke="currentColor" stroke-width="1.2" stroke-linecap="round" />
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
|
|
{#if loadError}
|
|
<div class="nw-error">
|
|
<p>{$ui("ui.failed_to_load_note")}</p>
|
|
<p class="nw-error-detail">{loadError}</p>
|
|
</div>
|
|
{:else}
|
|
<div class="nw-editor">
|
|
<Editor bind:this={editor} />
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
|
|
<style>
|
|
.note-window {
|
|
display: flex;
|
|
flex-direction: column;
|
|
height: 100vh;
|
|
background: var(--bg-primary);
|
|
}
|
|
|
|
.nw-titlebar {
|
|
display: flex;
|
|
align-items: center;
|
|
height: 34px;
|
|
background: var(--bg-secondary);
|
|
border-bottom: 1px solid var(--border-color);
|
|
user-select: none;
|
|
flex-shrink: 0;
|
|
-webkit-app-region: drag;
|
|
}
|
|
|
|
.nw-titlebar.macos {
|
|
padding-left: 78px;
|
|
}
|
|
|
|
.nw-titlebar-brand {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
padding-left: 14px;
|
|
pointer-events: none;
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
|
|
.nw-title {
|
|
font-size: 12px;
|
|
font-weight: 500;
|
|
color: var(--text-tertiary);
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.nw-dirty-dot {
|
|
width: 6px;
|
|
height: 6px;
|
|
border-radius: 50%;
|
|
background: var(--accent);
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.titlebar-actions {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 4px;
|
|
margin-right: 8px;
|
|
-webkit-app-region: no-drag;
|
|
}
|
|
|
|
.nw-btn {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 28px;
|
|
height: 28px;
|
|
border: none;
|
|
border-radius: 7px;
|
|
background: var(--bg-hover);
|
|
color: var(--text-secondary);
|
|
cursor: pointer;
|
|
transition: background 0.15s, color 0.15s;
|
|
}
|
|
|
|
.nw-btn:hover {
|
|
background: var(--bg-tertiary, var(--bg-hover));
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.nw-btn.active {
|
|
background: color-mix(in srgb, var(--accent) 18%, transparent);
|
|
color: var(--accent);
|
|
}
|
|
|
|
.titlebar-controls {
|
|
display: flex;
|
|
height: 100%;
|
|
-webkit-app-region: no-drag;
|
|
}
|
|
|
|
.titlebar-btn {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 38px;
|
|
height: 100%;
|
|
border: none;
|
|
background: none;
|
|
color: var(--text-tertiary);
|
|
cursor: pointer;
|
|
transition: background 0.1s, color 0.1s;
|
|
}
|
|
|
|
.titlebar-btn:hover {
|
|
background: var(--bg-hover);
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.titlebar-close:hover {
|
|
background: #e81123;
|
|
color: white;
|
|
}
|
|
|
|
.nw-editor {
|
|
flex: 1;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.nw-error {
|
|
flex: 1;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
color: var(--text-secondary);
|
|
font-size: 14px;
|
|
}
|
|
|
|
.nw-error-detail {
|
|
font-size: 12px;
|
|
color: var(--text-tertiary);
|
|
max-width: 400px;
|
|
text-align: center;
|
|
word-break: break-word;
|
|
}
|
|
</style>
|