@knotpad/app 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/brief.js +165 -78
- package/package.json +6 -86
- package/app/(app)/calendar/page.tsx +0 -57
- package/app/(app)/error.tsx +0 -35
- package/app/(app)/graph/page.tsx +0 -32
- package/app/(app)/guide/page.tsx +0 -21
- package/app/(app)/kanban/loading.tsx +0 -24
- package/app/(app)/kanban/page.tsx +0 -59
- package/app/(app)/layout.tsx +0 -122
- package/app/(app)/list/loading.tsx +0 -21
- package/app/(app)/list/page.tsx +0 -137
- package/app/(app)/loading.tsx +0 -18
- package/app/(app)/notes/[noteId]/page.tsx +0 -84
- package/app/(app)/notes/layout.tsx +0 -30
- package/app/(app)/notes/page.tsx +0 -39
- package/app/(app)/page.tsx +0 -5
- package/app/(app)/settings/agent-token/page.tsx +0 -59
- package/app/(app)/settings/backup/page.tsx +0 -49
- package/app/(app)/settings/billing/page.tsx +0 -53
- package/app/(app)/settings/calendar/page.tsx +0 -41
- package/app/(app)/settings/layout.test.tsx +0 -39
- package/app/(app)/settings/layout.tsx +0 -71
- package/app/(app)/settings/page.tsx +0 -4
- package/app/(app)/settings/security/page.tsx +0 -43
- package/app/(app)/settings/team/page.tsx +0 -74
- package/app/(app)/settings/workspace/page.tsx +0 -27
- package/app/(app)/tasks/[taskId]/page.tsx +0 -79
- package/app/(auth)/forgot-password/page.tsx +0 -106
- package/app/(auth)/guest/page.tsx +0 -56
- package/app/(auth)/layout.tsx +0 -13
- package/app/(auth)/login/page.tsx +0 -14
- package/app/(auth)/register/page.tsx +0 -193
- package/app/(auth)/reset-password/page.tsx +0 -138
- package/app/api/account/claim/route.tsx +0 -135
- package/app/api/admin/backfill-encryption/route.tsx +0 -43
- package/app/api/admin/license/route.tsx +0 -42
- package/app/api/auth/2fa/route.tsx +0 -148
- package/app/api/auth/[...nextauth]/route.tsx +0 -3
- package/app/api/auth/change-password/route.tsx +0 -61
- package/app/api/auth/check-2fa/route.tsx +0 -19
- package/app/api/auth/forgot-password/route.tsx +0 -65
- package/app/api/auth/reset-password/route.tsx +0 -52
- package/app/api/auth/verify-2fa/route.tsx +0 -88
- package/app/api/backup/download/db/route.ts +0 -29
- package/app/api/backup/download/notes/route.ts +0 -25
- package/app/api/backup/settings/route.ts +0 -92
- package/app/api/billing/checkout/route.tsx +0 -81
- package/app/api/billing/migrate/route.tsx +0 -163
- package/app/api/billing/portal/route.tsx +0 -24
- package/app/api/billing/setup-intent/route.tsx +0 -55
- package/app/api/billing/status/route.tsx +0 -36
- package/app/api/billing/subscribe/route.tsx +0 -85
- package/app/api/billing/webhook/route.tsx +0 -199
- package/app/api/calendar-feeds/[feedId]/route.tsx +0 -67
- package/app/api/calendar-feeds/[feedId]/sync/route.tsx +0 -37
- package/app/api/calendar-feeds/events/route.tsx +0 -82
- package/app/api/calendar-feeds/route.tsx +0 -52
- package/app/api/calendar-feeds/sync-all/route.tsx +0 -34
- package/app/api/cron/calendar-feeds/route.tsx +0 -31
- package/app/api/cron/stale-tasks/route.tsx +0 -51
- package/app/api/cron/sync/route.tsx +0 -34
- package/app/api/devices/[deviceId]/route.tsx +0 -25
- package/app/api/devices/route.tsx +0 -41
- package/app/api/export/route.tsx +0 -40
- package/app/api/feedback/route.tsx +0 -54
- package/app/api/folders/[folderId]/route.tsx +0 -51
- package/app/api/folders/route.tsx +0 -37
- package/app/api/graph/route.tsx +0 -242
- package/app/api/guest/route.tsx +0 -58
- package/app/api/health/route.tsx +0 -10
- package/app/api/holidays/countries/route.tsx +0 -14
- package/app/api/holidays/route.tsx +0 -49
- package/app/api/holidays/states/route.tsx +0 -21
- package/app/api/invites/[token]/route.tsx +0 -131
- package/app/api/invites/route.tsx +0 -74
- package/app/api/mcp/generate-token/route.tsx +0 -55
- package/app/api/mcp/revoke-token/[tokenId]/route.tsx +0 -30
- package/app/api/mcp/update-alias/[tokenId]/route.tsx +0 -22
- package/app/api/notes/[noteId]/export/route.tsx +0 -45
- package/app/api/notes/[noteId]/route.tsx +0 -360
- package/app/api/notes/route.tsx +0 -112
- package/app/api/notifications/route.tsx +0 -44
- package/app/api/register/route.tsx +0 -67
- package/app/api/restore/route.tsx +0 -148
- package/app/api/sync/conflicts/[conflictId]/route.tsx +0 -134
- package/app/api/sync/conflicts/route.tsx +0 -48
- package/app/api/sync/status/route.tsx +0 -49
- package/app/api/sync/trigger/route.tsx +0 -15
- package/app/api/tasks/[taskId]/detail/route.tsx +0 -68
- package/app/api/tasks/[taskId]/route.tsx +0 -259
- package/app/api/tasks/bulk/route.tsx +0 -133
- package/app/api/tasks/route.tsx +0 -36
- package/app/api/workspace/active/route.tsx +0 -39
- package/app/api/workspace/create-team/route.tsx +0 -42
- package/app/api/workspace/kanban-statuses/route.tsx +0 -71
- package/app/api/workspace/members/[memberId]/route.tsx +0 -69
- package/app/api/workspace/route.tsx +0 -24
- package/app/download/page.tsx +0 -170
- package/app/favicon.ico +0 -0
- package/app/generated/prisma/client.d.ts +0 -1
- package/app/generated/prisma/client.js +0 -5
- package/app/generated/prisma/default.d.ts +0 -1
- package/app/generated/prisma/default.js +0 -5
- package/app/generated/prisma/edge.d.ts +0 -1
- package/app/generated/prisma/edge.js +0 -497
- package/app/generated/prisma/index-browser.js +0 -523
- package/app/generated/prisma/index.d.ts +0 -46376
- package/app/generated/prisma/index.js +0 -497
- package/app/generated/prisma/package.json +0 -144
- package/app/generated/prisma/query_compiler_fast_bg.js +0 -2
- package/app/generated/prisma/query_compiler_fast_bg.wasm +0 -0
- package/app/generated/prisma/query_compiler_fast_bg.wasm-base64.js +0 -2
- package/app/generated/prisma/runtime/client.d.ts +0 -3386
- package/app/generated/prisma/runtime/client.js +0 -86
- package/app/generated/prisma/runtime/index-browser.d.ts +0 -90
- package/app/generated/prisma/runtime/index-browser.js +0 -6
- package/app/generated/prisma/runtime/wasm-compiler-edge.js +0 -76
- package/app/generated/prisma/schema.prisma +0 -456
- package/app/generated/prisma/wasm-edge-light-loader.mjs +0 -5
- package/app/generated/prisma/wasm-worker-loader.mjs +0 -5
- package/app/globals.css +0 -54
- package/app/invite/[token]/page.tsx +0 -52
- package/app/layout.tsx +0 -90
- package/app/mcp/route.tsx +0 -430
- package/app/opengraph-image.tsx +0 -120
- package/app/page.tsx +0 -398
- package/app/privacy/page.tsx +0 -69
- package/app/robots.tsx +0 -25
- package/app/sitemap.tsx +0 -36
- package/app/terms/page.tsx +0 -69
- package/app/upgrade/page.tsx +0 -75
- package/auth.config.ts +0 -33
- package/auth.ts +0 -79
- package/components/auth/login-form.tsx +0 -302
- package/components/auth/password-checklist.tsx +0 -31
- package/components/auth/password-input.tsx +0 -36
- package/components/auth/switch-account-button.test.tsx +0 -22
- package/components/auth/switch-account-button.tsx +0 -19
- package/components/auth/two-factor-input.tsx +0 -116
- package/components/billing/billing-dashboard.tsx +0 -265
- package/components/billing/card-form.tsx +0 -210
- package/components/billing/claim-account-form.tsx +0 -99
- package/components/branding/app-logo.test.tsx +0 -20
- package/components/branding/app-logo.tsx +0 -25
- package/components/calendar/calendar-agenda.tsx +0 -150
- package/components/calendar/calendar-drag.test.tsx +0 -177
- package/components/calendar/calendar-grid.tsx +0 -357
- package/components/calendar/calendar-hooks.test.tsx +0 -27
- package/components/calendar/calendar-hooks.ts +0 -351
- package/components/calendar/calendar-toolbar.test.tsx +0 -68
- package/components/calendar/calendar-toolbar.tsx +0 -291
- package/components/calendar/calendar-types.ts +0 -148
- package/components/calendar/calendar-view.test.tsx +0 -295
- package/components/calendar/calendar-view.tsx +0 -307
- package/components/calendar/day-detail-popover.tsx +0 -174
- package/components/calendar/task-chip.tsx +0 -86
- package/components/command/command-palette.test.tsx +0 -33
- package/components/command/command-palette.tsx +0 -310
- package/components/download-cta.tsx +0 -87
- package/components/feedback/feedback-popup.tsx +0 -207
- package/components/graph/graph-draw.ts +0 -337
- package/components/graph/graph-overlays.tsx +0 -160
- package/components/graph/graph-page.test.tsx +0 -131
- package/components/graph/graph-page.tsx +0 -263
- package/components/graph/graph-types.ts +0 -47
- package/components/graph/graph-view.tsx +0 -322
- package/components/guide/guide-view.tsx +0 -522
- package/components/kanban/kanban-board.test.tsx +0 -128
- package/components/kanban/kanban-board.tsx +0 -361
- package/components/kanban/kanban-card-menu.tsx +0 -102
- package/components/kanban/kanban-card.tsx +0 -227
- package/components/kanban/kanban-column.tsx +0 -49
- package/components/kanban/kanban-status-context.tsx +0 -28
- package/components/landing/calendar-sandbox.test.tsx +0 -15
- package/components/landing/calendar-sandbox.tsx +0 -107
- package/components/landing/graph-sandbox.test.tsx +0 -27
- package/components/landing/graph-sandbox.tsx +0 -80
- package/components/landing/kanban-sandbox.test.tsx +0 -24
- package/components/landing/kanban-sandbox.tsx +0 -101
- package/components/landing/landing-showcase.test.tsx +0 -21
- package/components/landing/landing-showcase.tsx +0 -54
- package/components/landing/list-sandbox.tsx +0 -86
- package/components/landing/mock-workspace.ts +0 -168
- package/components/landing/notes-sandbox.test.tsx +0 -14
- package/components/landing/notes-sandbox.tsx +0 -88
- package/components/layout/app-shell.tsx +0 -83
- package/components/layout/backup-scheduler.tsx +0 -122
- package/components/layout/bottom-nav.tsx +0 -43
- package/components/layout/icon-bar.test.tsx +0 -29
- package/components/layout/icon-bar.tsx +0 -118
- package/components/layout/mobile-top-bar.tsx +0 -68
- package/components/layout/notes-panel-folder.tsx +0 -127
- package/components/layout/notes-panel-note-item.tsx +0 -140
- package/components/layout/notes-panel-task-tab.tsx +0 -63
- package/components/layout/notes-panel-types.ts +0 -44
- package/components/layout/notes-panel.tsx +0 -476
- package/components/layout/notification-bell.tsx +0 -251
- package/components/layout/paywall-screen.tsx +0 -41
- package/components/layout/pro-banner.tsx +0 -76
- package/components/layout/sw-register.tsx +0 -27
- package/components/layout/workspace-switcher.tsx +0 -90
- package/components/notes/mobile-bottom-sheet.tsx +0 -99
- package/components/notes/note-editor-context-menu.tsx +0 -47
- package/components/notes/note-editor-dom.ts +0 -33
- package/components/notes/note-editor-dropdowns.tsx +0 -484
- package/components/notes/note-editor-hooks.ts +0 -692
- package/components/notes/note-editor-keyboard.ts +0 -305
- package/components/notes/note-editor-overlay.tsx +0 -90
- package/components/notes/note-editor.test.tsx +0 -372
- package/components/notes/note-editor.tsx +0 -662
- package/components/notes/note-preview-pane.tsx +0 -156
- package/components/notes/note-tabs.tsx +0 -120
- package/components/notes/note-types.tsx +0 -157
- package/components/settings/accept-invite.tsx +0 -108
- package/components/settings/agent-token-settings.tsx +0 -369
- package/components/settings/backup-restore-settings.test.tsx +0 -25
- package/components/settings/backup-restore-settings.tsx +0 -327
- package/components/settings/calendar-feeds-settings.tsx +0 -489
- package/components/settings/calendar-general-settings.tsx +0 -174
- package/components/settings/confirm-danger-action.test.tsx +0 -215
- package/components/settings/confirm-danger-action.tsx +0 -65
- package/components/settings/security-settings.tsx +0 -252
- package/components/settings/settings-guidance.test.tsx +0 -98
- package/components/settings/team-settings.tsx +0 -319
- package/components/settings/two-factor-auth.tsx +0 -296
- package/components/settings/workspace-settings-client.tsx +0 -363
- package/components/settings/workspace-settings-form.tsx +0 -73
- package/components/sync/conflict-viewer.tsx +0 -247
- package/components/sync/sync-indicator.tsx +0 -171
- package/components/tasks/snippet-thread.tsx +0 -119
- package/components/tasks/status-dot.tsx +0 -47
- package/components/tasks/task-badge.tsx +0 -43
- package/components/tasks/task-detail.test.tsx +0 -187
- package/components/tasks/task-detail.tsx +0 -458
- package/components/tasks/task-list-filters.test.tsx +0 -75
- package/components/tasks/task-list-filters.tsx +0 -163
- package/components/tasks/task-list-types.ts +0 -20
- package/components/tasks/task-list.test.tsx +0 -175
- package/components/tasks/task-list.tsx +0 -481
- package/components/tasks/task-row.tsx +0 -85
- package/components/tasks/task-table-row.tsx +0 -259
- package/components/ui/skeleton.tsx +0 -3
- package/components/ui/toast.test.tsx +0 -42
- package/components/ui/toast.tsx +0 -70
- package/electron/main.ts +0 -251
- package/electron/preload.ts +0 -56
- package/instrumentation.tsx +0 -23
- package/lib/api-error.ts +0 -50
- package/lib/backup/backup-runner.test.ts +0 -32
- package/lib/backup/backup-runner.ts +0 -19
- package/lib/backup/backup-schedule.test.ts +0 -23
- package/lib/backup/backup-schedule.ts +0 -55
- package/lib/backup/backup-settings.test.ts +0 -30
- package/lib/backup/backup-settings.ts +0 -27
- package/lib/backup/export-notes-zip.test.ts +0 -26
- package/lib/backup/export-notes-zip.ts +0 -82
- package/lib/backup/export-workspace-backup.test.ts +0 -17
- package/lib/backup/export-workspace-backup.ts +0 -77
- package/lib/backup/restore-workspace-from-export.test.ts +0 -18
- package/lib/backup/restore-workspace-from-export.ts +0 -183
- package/lib/backup/types.ts +0 -14
- package/lib/brand-icons.ts +0 -1
- package/lib/calendar-feed-crypto.ts +0 -38
- package/lib/calendar-feed.ts +0 -239
- package/lib/client/online-status.ts +0 -47
- package/lib/conflict-resolver.test.ts +0 -57
- package/lib/conflict-resolver.ts +0 -240
- package/lib/db-init.ts +0 -79
- package/lib/email.ts +0 -159
- package/lib/encryption.test.ts +0 -41
- package/lib/encryption.ts +0 -98
- package/lib/extract-snippet.test.ts +0 -123
- package/lib/extract-snippet.ts +0 -69
- package/lib/kanban-status.ts +0 -55
- package/lib/license.ts +0 -21
- package/lib/limits.ts +0 -31
- package/lib/mcp-auth.test.ts +0 -58
- package/lib/mcp-auth.ts +0 -65
- package/lib/mcp-contract.test.ts +0 -25
- package/lib/mcp-contract.ts +0 -210
- package/lib/mcp-handler.ts +0 -31
- package/lib/mcp-url.test.ts +0 -12
- package/lib/mcp-url.ts +0 -7
- package/lib/mentions.test.ts +0 -45
- package/lib/mentions.ts +0 -73
- package/lib/note-crypto.ts +0 -108
- package/lib/note-sync.ts +0 -201
- package/lib/note-title.ts +0 -93
- package/lib/prisma.ts +0 -193
- package/lib/pro-flush.ts +0 -292
- package/lib/rate-limit.ts +0 -57
- package/lib/stripe.ts +0 -38
- package/lib/sync-worker.ts +0 -388
- package/lib/task-parser.test.ts +0 -91
- package/lib/task-parser.ts +0 -81
- package/lib/task-utils.ts +0 -52
- package/lib/use-is-electron.ts +0 -19
- package/lib/use-is-mobile.ts +0 -22
- package/lib/validation/calendar-feed.ts +0 -31
- package/lib/validation/note.ts +0 -27
- package/lib/validation/task.ts +0 -26
- package/lib/view-preferences.test.ts +0 -54
- package/lib/view-preferences.ts +0 -28
- package/lib/workspace.ts +0 -66
- package/next.config.ts +0 -21
- package/postcss.config.mjs +0 -7
- package/prisma/migrations/20260519021916_init/migration.sql +0 -388
- package/prisma/migrations/20260519061113_drop_sync_password/migration.sql +0 -8
- package/prisma/migrations/20260520065016_add_task_start_date/migration.sql +0 -2
- package/prisma/migrations/20260529010600_remove_encryption_fields/migration.sql +0 -12
- package/prisma/migrations/20260529020000_restore_encryption_salt/migration.sql +0 -3
- package/prisma/migrations/20260529030000_add_folders/migration.sql +0 -17
- package/prisma/migrations/20260605000000_deferred_fixes/migration.sql +0 -31
- package/prisma/migrations/20260605020806_add_pending_sync_to_note_and_task/migration.sql +0 -5
- package/prisma/migrations/20260605063634_add_stripe_webhook_event_sync_lock/migration.sql +0 -14
- package/prisma/migrations/20260605100000_add_prod_indexes/migration.sql +0 -26
- package/prisma/migrations/20260608081404_add_kanban_statuses/migration.sql +0 -23
- package/prisma/migrations/20260611032723_add_calendar_feeds/migration.sql +0 -43
- package/prisma/migrations/20260611040000_add_calendar_feed_color/migration.sql +0 -2
- package/prisma/migrations/20260611050000_add_task_priority/migration.sql +0 -14
- package/prisma/migrations/20260612060000_add_critical_priority/migration.sql +0 -2
- package/prisma/migrations/20260613090000_add_backup_settings/migration.sql +0 -25
- package/prisma/migrations/20260614160000_add_feedback/migration.sql +0 -20
- package/prisma/migrations/20260614210000_add_2fa/migration.sql +0 -4
- package/prisma/migrations/migration_lock.toml +0 -3
- package/prisma/schema.prisma +0 -457
- package/public/Logo_icon.svg +0 -1
- package/public/file.svg +0 -1
- package/public/globe.svg +0 -1
- package/public/icon-192.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/icon.svg +0 -4
- package/public/icon_dark.svg +0 -1
- package/public/knotpad_icon.svg +0 -1
- package/public/knotpad_logo_full.svg +0 -1
- package/public/manifest.json +0 -14
- package/public/next.svg +0 -1
- package/public/sw.js +0 -137
- package/public/vercel.svg +0 -1
- package/public/window.svg +0 -1
- package/tsconfig.json +0 -35
|
@@ -1,692 +0,0 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { useRouter } from "next/navigation";
|
|
3
|
-
import type { Task, Member, NoteRef, TaskRef, SlashCommandId, PriorityLevel } from "./note-types";
|
|
4
|
-
import { SAVE_DEBOUNCE_MS, AGENT_HANDLES, SLASH_COMMANDS, PRIORITY_LEVELS, stripBadgeComments } from "./note-types";
|
|
5
|
-
import { getCaretCoords } from "./note-editor-dom";
|
|
6
|
-
|
|
7
|
-
// ── useNoteSave ─────────────────────────────────────────────────────────────
|
|
8
|
-
// Manages debounced saving, version conflicts, abort, and unmount flush.
|
|
9
|
-
export function useNoteSave(
|
|
10
|
-
noteId: string,
|
|
11
|
-
initialVersion: number,
|
|
12
|
-
setTasks: React.Dispatch<React.SetStateAction<Task[]>>,
|
|
13
|
-
setContent: React.Dispatch<React.SetStateAction<string>>,
|
|
14
|
-
setTitle: React.Dispatch<React.SetStateAction<string>>,
|
|
15
|
-
) {
|
|
16
|
-
const router = useRouter();
|
|
17
|
-
const [saving, setSaving] = useState(false);
|
|
18
|
-
const [saveError, setSaveError] = useState<string | null>(null);
|
|
19
|
-
const [staleReloaded, setStaleReloaded] = useState(false);
|
|
20
|
-
const saveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
21
|
-
const saveAbort = useRef<AbortController | null>(null);
|
|
22
|
-
const versionRef = useRef(initialVersion);
|
|
23
|
-
const latestPayload = useRef<{ title: string; content: string } | null>(null);
|
|
24
|
-
const savePending = useRef(false);
|
|
25
|
-
|
|
26
|
-
const scheduleSave = useCallback(
|
|
27
|
-
(newTitle: string, newContent: string) => {
|
|
28
|
-
latestPayload.current = { title: newTitle, content: newContent };
|
|
29
|
-
savePending.current = true;
|
|
30
|
-
if (saveTimer.current) clearTimeout(saveTimer.current);
|
|
31
|
-
saveTimer.current = setTimeout(async () => {
|
|
32
|
-
if (saveAbort.current) saveAbort.current.abort();
|
|
33
|
-
const controller = new AbortController();
|
|
34
|
-
saveAbort.current = controller;
|
|
35
|
-
setSaving(true);
|
|
36
|
-
try {
|
|
37
|
-
const res = await fetch(`/api/notes/${noteId}`, {
|
|
38
|
-
method: "PATCH",
|
|
39
|
-
headers: { "Content-Type": "application/json" },
|
|
40
|
-
body: JSON.stringify({ title: newTitle, content: newContent, baseVersion: versionRef.current }),
|
|
41
|
-
signal: controller.signal,
|
|
42
|
-
});
|
|
43
|
-
if (res.status === 409) {
|
|
44
|
-
const data = await res.json();
|
|
45
|
-
if (typeof data.currentVersion === "number") versionRef.current = data.currentVersion;
|
|
46
|
-
if (typeof data.content === "string") setContent(stripBadgeComments(data.content));
|
|
47
|
-
if (typeof data.title === "string") setTitle(data.title);
|
|
48
|
-
setStaleReloaded(true);
|
|
49
|
-
router.refresh();
|
|
50
|
-
} else if (res.ok) {
|
|
51
|
-
const updated = await res.json();
|
|
52
|
-
if (typeof updated.version === "number") versionRef.current = updated.version;
|
|
53
|
-
setTasks((prev) => updated.tasks ?? prev);
|
|
54
|
-
router.refresh();
|
|
55
|
-
}
|
|
56
|
-
} catch (err) {
|
|
57
|
-
if ((err as Error).name === "AbortError") return;
|
|
58
|
-
setSaveError("Couldn't save — check your connection and try again.");
|
|
59
|
-
} finally {
|
|
60
|
-
if (saveAbort.current === controller) {
|
|
61
|
-
setSaving(false);
|
|
62
|
-
savePending.current = false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}, SAVE_DEBOUNCE_MS);
|
|
66
|
-
},
|
|
67
|
-
[noteId, router, setTasks, setContent, setTitle]
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
// Warn when closing the tab while an edit is in the debounce window.
|
|
71
|
-
useEffect(() => {
|
|
72
|
-
const handleBeforeUnload = (e: BeforeUnloadEvent) => {
|
|
73
|
-
if (savePending.current) {
|
|
74
|
-
e.preventDefault();
|
|
75
|
-
e.returnValue = "";
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
79
|
-
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
80
|
-
}, []);
|
|
81
|
-
|
|
82
|
-
// Flush on unmount.
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
return () => {
|
|
85
|
-
if (saveTimer.current) clearTimeout(saveTimer.current);
|
|
86
|
-
if (saveAbort.current) saveAbort.current.abort();
|
|
87
|
-
if (savePending.current && latestPayload.current) {
|
|
88
|
-
savePending.current = false;
|
|
89
|
-
fetch(`/api/notes/${noteId}`, {
|
|
90
|
-
method: "PATCH",
|
|
91
|
-
headers: { "Content-Type": "application/json" },
|
|
92
|
-
body: JSON.stringify({ ...latestPayload.current, baseVersion: versionRef.current }),
|
|
93
|
-
keepalive: true,
|
|
94
|
-
}).catch(() => {});
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
saving, setSaving,
|
|
101
|
-
saveError, setSaveError,
|
|
102
|
-
staleReloaded, setStaleReloaded,
|
|
103
|
-
savePending,
|
|
104
|
-
versionRef,
|
|
105
|
-
scheduleSave,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// ── useUndoHistory ──────────────────────────────────────────────────────────
|
|
110
|
-
// Custom undo/redo ring buffer (React's controlled textarea destroys the
|
|
111
|
-
// browser's native undo stack on every setState).
|
|
112
|
-
export function useUndoHistory(initialContent: string) {
|
|
113
|
-
const historyStack = useRef<string[]>([initialContent]);
|
|
114
|
-
const historyPos = useRef(0);
|
|
115
|
-
const historyTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
116
|
-
|
|
117
|
-
// Initialise once on mount
|
|
118
|
-
useEffect(() => {
|
|
119
|
-
historyStack.current = [initialContent];
|
|
120
|
-
historyPos.current = 0;
|
|
121
|
-
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
122
|
-
|
|
123
|
-
function pushHistory(value: string) {
|
|
124
|
-
if (historyTimer.current) clearTimeout(historyTimer.current);
|
|
125
|
-
historyTimer.current = setTimeout(() => {
|
|
126
|
-
const stack = historyStack.current;
|
|
127
|
-
const trimmed = stack.slice(0, historyPos.current + 1);
|
|
128
|
-
if (trimmed[trimmed.length - 1] === value) return;
|
|
129
|
-
trimmed.push(value);
|
|
130
|
-
if (trimmed.length > 300) trimmed.shift();
|
|
131
|
-
historyStack.current = trimmed;
|
|
132
|
-
historyPos.current = trimmed.length - 1;
|
|
133
|
-
}, 400);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function pushHistoryImmediate(value: string) {
|
|
137
|
-
if (historyTimer.current) clearTimeout(historyTimer.current);
|
|
138
|
-
const stack = historyStack.current.slice(0, historyPos.current + 1);
|
|
139
|
-
if (stack[stack.length - 1] === value) return;
|
|
140
|
-
stack.push(value);
|
|
141
|
-
if (stack.length > 300) stack.shift();
|
|
142
|
-
historyStack.current = stack;
|
|
143
|
-
historyPos.current = stack.length - 1;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return { historyStack, historyPos, historyTimer, pushHistory, pushHistoryImmediate };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// ── usePickerState ──────────────────────────────────────────────────────────
|
|
150
|
-
// Manages all 7 picker states, trigger detection, suggestion lists, and
|
|
151
|
-
// insert functions.
|
|
152
|
-
export function usePickerState(
|
|
153
|
-
content: string,
|
|
154
|
-
title: string,
|
|
155
|
-
members: Member[],
|
|
156
|
-
initialNotesList: NoteRef[],
|
|
157
|
-
initialTasksList: TaskRef[],
|
|
158
|
-
noteId: string,
|
|
159
|
-
textareaRef: React.RefObject<HTMLTextAreaElement | null>,
|
|
160
|
-
scheduleSave: (t: string, c: string) => void,
|
|
161
|
-
setContent: React.Dispatch<React.SetStateAction<string>>,
|
|
162
|
-
) {
|
|
163
|
-
// @mention autocomplete
|
|
164
|
-
const [mentionQuery, setMentionQuery] = useState<string | null>(null);
|
|
165
|
-
const [mentionStart, setMentionStart] = useState(0);
|
|
166
|
-
const [mentionIndex, setMentionIndex] = useState(0);
|
|
167
|
-
|
|
168
|
-
// [[note]] link picker
|
|
169
|
-
const [noteQuery, setNoteQuery] = useState<string | null>(null);
|
|
170
|
-
const [noteStart, setNoteStart] = useState(0);
|
|
171
|
-
const [noteIndex, setNoteIndex] = useState(0);
|
|
172
|
-
|
|
173
|
-
// ((task)) reference picker
|
|
174
|
-
const [taskQuery, setTaskQuery] = useState<string | null>(null);
|
|
175
|
-
const [taskStart, setTaskStart] = useState(0);
|
|
176
|
-
const [taskIndex, setTaskIndex] = useState(0);
|
|
177
|
-
|
|
178
|
-
// !priority picker
|
|
179
|
-
const [priorityQuery, setPriorityQuery] = useState<string | null>(null);
|
|
180
|
-
const [priorityStart, setPriorityStart] = useState(0);
|
|
181
|
-
const [priorityIndex, setPriorityIndex] = useState(0);
|
|
182
|
-
|
|
183
|
-
// Live lists for pickers
|
|
184
|
-
const [liveNotesList, setLiveNotesList] = useState<NoteRef[]>(initialNotesList);
|
|
185
|
-
const [liveTasksList, setLiveTasksList] = useState<TaskRef[]>(initialTasksList);
|
|
186
|
-
|
|
187
|
-
// + quick-pick
|
|
188
|
-
const [plusOpen, setPlusOpen] = useState(false);
|
|
189
|
-
const [plusStart, setPlusStart] = useState(0);
|
|
190
|
-
|
|
191
|
-
// Template sub-picker
|
|
192
|
-
const [templateOpen, setTemplateOpen] = useState(false);
|
|
193
|
-
const templateInsertPos = useRef(0);
|
|
194
|
-
|
|
195
|
-
// Right-click context menu
|
|
196
|
-
const [contextMenu, setContextMenu] = useState<{ top: number; left: number; selStart: number; selEnd: number } | null>(null);
|
|
197
|
-
const contextMenuRef = useRef<HTMLDivElement>(null);
|
|
198
|
-
|
|
199
|
-
// Inline date picker
|
|
200
|
-
const [datePickerPos, setDatePickerPos] = useState<{ top: number; left: number } | null>(null);
|
|
201
|
-
const [datePickerStart, setDatePickerStart] = useState("");
|
|
202
|
-
const [datePickerEnd, setDatePickerEnd] = useState("");
|
|
203
|
-
const datePickerCtx = useRef<{ before: string; after: string }>({ before: "", after: "" });
|
|
204
|
-
|
|
205
|
-
// / command palette
|
|
206
|
-
const [slashQuery, setSlashQuery] = useState<string | null>(null);
|
|
207
|
-
const [slashStart, setSlashStart] = useState(0);
|
|
208
|
-
const [slashIndex, setSlashIndex] = useState(0);
|
|
209
|
-
|
|
210
|
-
// Shared dropdown anchor
|
|
211
|
-
const [dropdownPos, setDropdownPos] = useState<{ top: number; left: number } | null>(null);
|
|
212
|
-
|
|
213
|
-
// "Did you mean @name?" soft hint
|
|
214
|
-
const [softHint, setSoftHint] = useState<{
|
|
215
|
-
word: string; handle: string; start: number; end: number;
|
|
216
|
-
} | null>(null);
|
|
217
|
-
|
|
218
|
-
// Saved cursor for post-render restoration
|
|
219
|
-
const savedCursor = useRef<{ start: number; end: number } | null>(null);
|
|
220
|
-
|
|
221
|
-
// Refresh note picker list when [[ is first typed.
|
|
222
|
-
useEffect(() => {
|
|
223
|
-
if (noteQuery !== "") return;
|
|
224
|
-
fetch("/api/notes")
|
|
225
|
-
.then((r) => r.ok ? r.json() : null)
|
|
226
|
-
.then((data: { id: string; title: string }[] | null) => {
|
|
227
|
-
if (data) setLiveNotesList(data.filter((n) => n.id !== noteId));
|
|
228
|
-
})
|
|
229
|
-
.catch(() => {});
|
|
230
|
-
}, [noteQuery]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
231
|
-
|
|
232
|
-
// Refresh task picker list when (( is first typed.
|
|
233
|
-
useEffect(() => {
|
|
234
|
-
if (taskQuery !== "") return;
|
|
235
|
-
fetch("/api/tasks?limit=200&picker=1")
|
|
236
|
-
.then((r) => r.ok ? r.json() : null)
|
|
237
|
-
.then((data: { id: string; title: string; note?: { title: string } }[] | null) => {
|
|
238
|
-
if (data) setLiveTasksList(data.map((t) => ({ id: t.id, title: t.title, noteTitle: t.note?.title ?? "" })));
|
|
239
|
-
})
|
|
240
|
-
.catch(() => {});
|
|
241
|
-
}, [taskQuery]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
242
|
-
|
|
243
|
-
// Dismiss context menu on outside click.
|
|
244
|
-
useEffect(() => {
|
|
245
|
-
if (!contextMenu) return;
|
|
246
|
-
function onDown(e: MouseEvent) {
|
|
247
|
-
if (contextMenuRef.current && !contextMenuRef.current.contains(e.target as Node)) {
|
|
248
|
-
setContextMenu(null);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
document.addEventListener("mousedown", onDown);
|
|
252
|
-
return () => document.removeEventListener("mousedown", onDown);
|
|
253
|
-
}, [contextMenu]);
|
|
254
|
-
|
|
255
|
-
// ── Derived data ────────────────────────────────────────────────────────
|
|
256
|
-
const allHandles: Member[] = [
|
|
257
|
-
...AGENT_HANDLES,
|
|
258
|
-
...members.filter((m) => !m.isAgent),
|
|
259
|
-
];
|
|
260
|
-
|
|
261
|
-
const mentionSuggestions =
|
|
262
|
-
mentionQuery !== null
|
|
263
|
-
? allHandles.filter(
|
|
264
|
-
(m) =>
|
|
265
|
-
m.handle.toLowerCase().includes(mentionQuery.toLowerCase()) ||
|
|
266
|
-
m.label.toLowerCase().includes(mentionQuery.toLowerCase())
|
|
267
|
-
)
|
|
268
|
-
: [];
|
|
269
|
-
|
|
270
|
-
const slashSuggestions =
|
|
271
|
-
slashQuery !== null
|
|
272
|
-
? SLASH_COMMANDS.filter(
|
|
273
|
-
(c) =>
|
|
274
|
-
c.id.startsWith(slashQuery.toLowerCase()) ||
|
|
275
|
-
c.label.toLowerCase().includes(slashQuery.toLowerCase())
|
|
276
|
-
)
|
|
277
|
-
: [];
|
|
278
|
-
|
|
279
|
-
const noteSuggestions =
|
|
280
|
-
noteQuery !== null
|
|
281
|
-
? liveNotesList.filter((n) =>
|
|
282
|
-
n.title.toLowerCase().includes(noteQuery.toLowerCase())
|
|
283
|
-
)
|
|
284
|
-
: [];
|
|
285
|
-
|
|
286
|
-
const taskSuggestions =
|
|
287
|
-
taskQuery !== null
|
|
288
|
-
? liveTasksList.filter((t) =>
|
|
289
|
-
t.title.toLowerCase().includes(taskQuery.toLowerCase()) ||
|
|
290
|
-
t.noteTitle.toLowerCase().includes(taskQuery.toLowerCase())
|
|
291
|
-
)
|
|
292
|
-
: [];
|
|
293
|
-
|
|
294
|
-
const prioritySuggestions =
|
|
295
|
-
priorityQuery !== null
|
|
296
|
-
? PRIORITY_LEVELS.filter(
|
|
297
|
-
(p) =>
|
|
298
|
-
p.id.startsWith(priorityQuery.toLowerCase()) ||
|
|
299
|
-
p.label.toLowerCase().startsWith(priorityQuery.toLowerCase())
|
|
300
|
-
)
|
|
301
|
-
: [];
|
|
302
|
-
|
|
303
|
-
// ── Trigger detection ───────────────────────────────────────────────────
|
|
304
|
-
function detectTriggers(value: string, cursorPos: number) {
|
|
305
|
-
const textBefore = value.slice(0, cursorPos);
|
|
306
|
-
const ta = textareaRef.current;
|
|
307
|
-
|
|
308
|
-
// @mention
|
|
309
|
-
const atMatch = /@([\w-]*)$/.exec(textBefore);
|
|
310
|
-
if (atMatch) {
|
|
311
|
-
setMentionQuery(atMatch[1]);
|
|
312
|
-
setMentionStart(atMatch.index);
|
|
313
|
-
setMentionIndex(0);
|
|
314
|
-
setSlashQuery(null);
|
|
315
|
-
setSoftHint(null);
|
|
316
|
-
if (ta) setDropdownPos(getCaretCoords(ta, atMatch.index));
|
|
317
|
-
return;
|
|
318
|
-
}
|
|
319
|
-
setMentionQuery(null);
|
|
320
|
-
|
|
321
|
-
// !priority picker
|
|
322
|
-
const bangMatch = /(?:^|\s)!([\w-]*)$/.exec(textBefore);
|
|
323
|
-
if (bangMatch) {
|
|
324
|
-
const start = textBefore.lastIndexOf("!");
|
|
325
|
-
setPriorityQuery(bangMatch[1]);
|
|
326
|
-
setPriorityStart(start);
|
|
327
|
-
setPriorityIndex(0);
|
|
328
|
-
setSlashQuery(null);
|
|
329
|
-
setSoftHint(null);
|
|
330
|
-
if (ta) setDropdownPos(getCaretCoords(ta, start));
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
setPriorityQuery(null);
|
|
334
|
-
|
|
335
|
-
// / command
|
|
336
|
-
const lastSlash = textBefore.lastIndexOf("/");
|
|
337
|
-
if (lastSlash !== -1) {
|
|
338
|
-
const charBefore = lastSlash > 0 ? textBefore[lastSlash - 1] : null;
|
|
339
|
-
const isLineStart = charBefore === null || charBefore === "\n";
|
|
340
|
-
const isAfterSpace = charBefore === " ";
|
|
341
|
-
if (isLineStart || isAfterSpace) {
|
|
342
|
-
const query = textBefore.slice(lastSlash + 1);
|
|
343
|
-
if (/^\w*$/.test(query)) {
|
|
344
|
-
setSlashQuery(query);
|
|
345
|
-
setSlashStart(lastSlash);
|
|
346
|
-
setSlashIndex(0);
|
|
347
|
-
setSoftHint(null);
|
|
348
|
-
if (ta) setDropdownPos(getCaretCoords(ta, lastSlash));
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
setSlashQuery(null);
|
|
354
|
-
|
|
355
|
-
// [[note]] link picker
|
|
356
|
-
const bracketMatch = /\[\[([^\]]*)$/.exec(textBefore);
|
|
357
|
-
if (bracketMatch) {
|
|
358
|
-
setNoteQuery(bracketMatch[1]);
|
|
359
|
-
setNoteStart(bracketMatch.index);
|
|
360
|
-
setNoteIndex(0);
|
|
361
|
-
setTaskQuery(null);
|
|
362
|
-
setPlusOpen(false);
|
|
363
|
-
setSlashQuery(null);
|
|
364
|
-
setSoftHint(null);
|
|
365
|
-
if (ta) setDropdownPos(getCaretCoords(ta, bracketMatch.index));
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
setNoteQuery(null);
|
|
369
|
-
|
|
370
|
-
// ((task)) reference picker
|
|
371
|
-
const parenMatch = /\(\(([^)]*)$/.exec(textBefore);
|
|
372
|
-
if (parenMatch) {
|
|
373
|
-
setTaskQuery(parenMatch[1]);
|
|
374
|
-
setTaskStart(parenMatch.index);
|
|
375
|
-
setTaskIndex(0);
|
|
376
|
-
setPlusOpen(false);
|
|
377
|
-
setSlashQuery(null);
|
|
378
|
-
setSoftHint(null);
|
|
379
|
-
if (ta) setDropdownPos(getCaretCoords(ta, parenMatch.index));
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
setTaskQuery(null);
|
|
383
|
-
|
|
384
|
-
// + quick-pick
|
|
385
|
-
const plusMatch = /(?:^|\s)\+$/.exec(textBefore);
|
|
386
|
-
if (plusMatch) {
|
|
387
|
-
setPlusStart(textBefore.lastIndexOf("+"));
|
|
388
|
-
setPlusOpen(true);
|
|
389
|
-
setSlashQuery(null);
|
|
390
|
-
setSoftHint(null);
|
|
391
|
-
if (ta) setDropdownPos(getCaretCoords(ta, textBefore.lastIndexOf("+")));
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
setPlusOpen(false);
|
|
395
|
-
|
|
396
|
-
// "Did you mean?" soft hint
|
|
397
|
-
const bareMatch = /\b([\w-]{2,})$/.exec(textBefore);
|
|
398
|
-
if (bareMatch) {
|
|
399
|
-
const word = bareMatch[1].toLowerCase();
|
|
400
|
-
const humanMatch = members.find(
|
|
401
|
-
(m) => !m.isAgent && m.label.toLowerCase().startsWith(word)
|
|
402
|
-
);
|
|
403
|
-
if (humanMatch) {
|
|
404
|
-
setSoftHint({
|
|
405
|
-
word: bareMatch[1],
|
|
406
|
-
handle: humanMatch.handle,
|
|
407
|
-
start: bareMatch.index,
|
|
408
|
-
end: bareMatch.index + bareMatch[1].length,
|
|
409
|
-
});
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
setSoftHint(null);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// ── Insert functions ────────────────────────────────────────────────────
|
|
417
|
-
function applySoftHint() {
|
|
418
|
-
const ta = textareaRef.current;
|
|
419
|
-
if (!ta || !softHint) return;
|
|
420
|
-
const before = content.slice(0, softHint.start);
|
|
421
|
-
const after = content.slice(softHint.end);
|
|
422
|
-
const newContent = before + softHint.handle + after;
|
|
423
|
-
setContent(newContent);
|
|
424
|
-
setSoftHint(null);
|
|
425
|
-
scheduleSave(title, newContent);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
function insertMention(handle: string) {
|
|
429
|
-
const ta = textareaRef.current;
|
|
430
|
-
if (!ta) return;
|
|
431
|
-
const before = content.slice(0, mentionStart);
|
|
432
|
-
const after = content.slice(ta.selectionStart);
|
|
433
|
-
const newContent = before + handle + " " + after;
|
|
434
|
-
setContent(newContent);
|
|
435
|
-
setMentionQuery(null);
|
|
436
|
-
setDropdownPos(null);
|
|
437
|
-
scheduleSave(title, newContent);
|
|
438
|
-
requestAnimationFrame(() => {
|
|
439
|
-
const pos = mentionStart + handle.length + 1;
|
|
440
|
-
ta.setSelectionRange(pos, pos);
|
|
441
|
-
ta.focus();
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
function insertNoteLink(noteRef: NoteRef) {
|
|
446
|
-
const ta = textareaRef.current;
|
|
447
|
-
if (!ta) return;
|
|
448
|
-
const link = `[[${noteRef.title}]]`;
|
|
449
|
-
const before = content.slice(0, noteStart);
|
|
450
|
-
const after = content.slice(ta.selectionStart);
|
|
451
|
-
const newContent = before + link + " " + after;
|
|
452
|
-
setContent(newContent);
|
|
453
|
-
setNoteQuery(null);
|
|
454
|
-
setDropdownPos(null);
|
|
455
|
-
scheduleSave(title, newContent);
|
|
456
|
-
requestAnimationFrame(() => {
|
|
457
|
-
const pos = noteStart + link.length + 1;
|
|
458
|
-
ta.setSelectionRange(pos, pos);
|
|
459
|
-
ta.focus();
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function insertTaskRef(taskRef: TaskRef) {
|
|
464
|
-
const ta = textareaRef.current;
|
|
465
|
-
if (!ta) return;
|
|
466
|
-
const link = `((${taskRef.title}))`;
|
|
467
|
-
const before = content.slice(0, taskStart);
|
|
468
|
-
const after = content.slice(ta.selectionStart);
|
|
469
|
-
const newContent = before + link + " " + after;
|
|
470
|
-
setContent(newContent);
|
|
471
|
-
setTaskQuery(null);
|
|
472
|
-
setDropdownPos(null);
|
|
473
|
-
scheduleSave(title, newContent);
|
|
474
|
-
requestAnimationFrame(() => {
|
|
475
|
-
const pos = taskStart + link.length + 1;
|
|
476
|
-
ta.setSelectionRange(pos, pos);
|
|
477
|
-
ta.focus();
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
function insertPriority(level: PriorityLevel) {
|
|
482
|
-
const ta = textareaRef.current;
|
|
483
|
-
if (!ta) return;
|
|
484
|
-
const tag = `!${level}`;
|
|
485
|
-
const before = content.slice(0, priorityStart);
|
|
486
|
-
const after = content.slice(ta.selectionStart);
|
|
487
|
-
const newContent = before + tag + " " + after;
|
|
488
|
-
setContent(newContent);
|
|
489
|
-
setPriorityQuery(null);
|
|
490
|
-
setDropdownPos(null);
|
|
491
|
-
scheduleSave(title, newContent);
|
|
492
|
-
requestAnimationFrame(() => {
|
|
493
|
-
const pos = priorityStart + tag.length + 1;
|
|
494
|
-
ta.setSelectionRange(pos, pos);
|
|
495
|
-
ta.focus();
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
function triggerPlusOption(option: "note" | "task" | "person" | "date" | "priority") {
|
|
500
|
-
const ta = textareaRef.current;
|
|
501
|
-
if (!ta) return;
|
|
502
|
-
const before = content.slice(0, plusStart);
|
|
503
|
-
const after = content.slice(ta.selectionStart);
|
|
504
|
-
setPlusOpen(false);
|
|
505
|
-
setDropdownPos(null);
|
|
506
|
-
if (option === "note") {
|
|
507
|
-
const trigger = "[[";
|
|
508
|
-
const nc = before + trigger + after;
|
|
509
|
-
setContent(nc);
|
|
510
|
-
savedCursor.current = { start: plusStart + trigger.length, end: plusStart + trigger.length };
|
|
511
|
-
setNoteQuery("");
|
|
512
|
-
setNoteStart(plusStart);
|
|
513
|
-
setNoteIndex(0);
|
|
514
|
-
if (ta) setDropdownPos(getCaretCoords(ta, plusStart));
|
|
515
|
-
} else if (option === "task") {
|
|
516
|
-
const trigger = "((";
|
|
517
|
-
const nc = before + trigger + after;
|
|
518
|
-
setContent(nc);
|
|
519
|
-
savedCursor.current = { start: plusStart + trigger.length, end: plusStart + trigger.length };
|
|
520
|
-
setTaskQuery("");
|
|
521
|
-
setTaskStart(plusStart);
|
|
522
|
-
setTaskIndex(0);
|
|
523
|
-
if (ta) setDropdownPos(getCaretCoords(ta, plusStart));
|
|
524
|
-
} else if (option === "person") {
|
|
525
|
-
const trigger = "@";
|
|
526
|
-
const nc = before + trigger + after;
|
|
527
|
-
setContent(nc);
|
|
528
|
-
savedCursor.current = { start: plusStart + trigger.length, end: plusStart + trigger.length };
|
|
529
|
-
setMentionQuery("");
|
|
530
|
-
setMentionStart(plusStart);
|
|
531
|
-
setMentionIndex(0);
|
|
532
|
-
if (ta) setDropdownPos(getCaretCoords(ta, plusStart));
|
|
533
|
-
} else if (option === "priority") {
|
|
534
|
-
const trigger = "!";
|
|
535
|
-
const nc = before + trigger + after;
|
|
536
|
-
setContent(nc);
|
|
537
|
-
savedCursor.current = { start: plusStart + trigger.length, end: plusStart + trigger.length };
|
|
538
|
-
setPriorityQuery("");
|
|
539
|
-
setPriorityStart(plusStart);
|
|
540
|
-
setPriorityIndex(0);
|
|
541
|
-
if (ta) setDropdownPos(getCaretCoords(ta, plusStart));
|
|
542
|
-
} else {
|
|
543
|
-
datePickerCtx.current = { before, after };
|
|
544
|
-
setDatePickerStart(new Date().toISOString().slice(0, 10));
|
|
545
|
-
setDatePickerEnd("");
|
|
546
|
-
if (ta) setDatePickerPos(getCaretCoords(ta, plusStart));
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
function executeSlashCommand(commandId: SlashCommandId) {
|
|
551
|
-
const ta = textareaRef.current;
|
|
552
|
-
if (!ta) return;
|
|
553
|
-
|
|
554
|
-
if (commandId === "template") {
|
|
555
|
-
templateInsertPos.current = slashStart;
|
|
556
|
-
setSlashQuery(null);
|
|
557
|
-
setTemplateOpen(true);
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
const before = content.slice(0, slashStart);
|
|
562
|
-
const after = content.slice(ta.selectionStart);
|
|
563
|
-
|
|
564
|
-
const insertions: Record<Exclude<SlashCommandId, "template">, { text: string; cursor: number }> = {
|
|
565
|
-
todo: { text: "- [ ] ", cursor: 6 },
|
|
566
|
-
bullet: { text: "- ", cursor: 2 },
|
|
567
|
-
numbered: { text: "1. ", cursor: 3 },
|
|
568
|
-
h1: { text: "# ", cursor: 2 },
|
|
569
|
-
h2: { text: "## ", cursor: 3 },
|
|
570
|
-
h3: { text: "### ", cursor: 4 },
|
|
571
|
-
bold: { text: "****", cursor: 2 },
|
|
572
|
-
italic: { text: "*", cursor: 1 },
|
|
573
|
-
strike: { text: "~~~~", cursor: 2 },
|
|
574
|
-
quote: { text: "> ", cursor: 2 },
|
|
575
|
-
code: { text: "```\n\n```", cursor: 4 },
|
|
576
|
-
table: { text: "| Col 1 | Col 2 |\n|---|---|\n| | |\n", cursor: 2 },
|
|
577
|
-
callout: { text: "> [!NOTE]\n> ", cursor: 12 },
|
|
578
|
-
warning: { text: "> [!WARNING]\n> ", cursor: 14 },
|
|
579
|
-
hr: { text: "\n---\n", cursor: 5 },
|
|
580
|
-
};
|
|
581
|
-
|
|
582
|
-
const { text, cursor } = insertions[commandId as Exclude<SlashCommandId, "template">];
|
|
583
|
-
const newContent = before + text + after;
|
|
584
|
-
setContent(newContent);
|
|
585
|
-
setSlashQuery(null);
|
|
586
|
-
setDropdownPos(null);
|
|
587
|
-
scheduleSave(title, newContent);
|
|
588
|
-
requestAnimationFrame(() => {
|
|
589
|
-
const pos = slashStart + cursor;
|
|
590
|
-
ta.setSelectionRange(pos, pos);
|
|
591
|
-
ta.focus();
|
|
592
|
-
});
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
function insertTemplate(templateContent: string) {
|
|
596
|
-
const ta = textareaRef.current;
|
|
597
|
-
if (!ta) return;
|
|
598
|
-
const insertPos = templateInsertPos.current;
|
|
599
|
-
const before = content.slice(0, insertPos);
|
|
600
|
-
const after = content.slice(ta.selectionStart);
|
|
601
|
-
const newContent = before + templateContent + after;
|
|
602
|
-
const cursorPos = before.length + templateContent.length;
|
|
603
|
-
savedCursor.current = { start: cursorPos, end: cursorPos };
|
|
604
|
-
setContent(newContent);
|
|
605
|
-
setTemplateOpen(false);
|
|
606
|
-
setDropdownPos(null);
|
|
607
|
-
scheduleSave(title, newContent);
|
|
608
|
-
requestAnimationFrame(() => ta.focus());
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
function confirmDatePicker() {
|
|
612
|
-
const ta = textareaRef.current;
|
|
613
|
-
if (!ta || !datePickerStart) return;
|
|
614
|
-
const dateStr =
|
|
615
|
-
datePickerEnd && datePickerEnd !== datePickerStart
|
|
616
|
-
? `${datePickerStart}..${datePickerEnd}`
|
|
617
|
-
: datePickerStart;
|
|
618
|
-
const { before, after } = datePickerCtx.current;
|
|
619
|
-
const newContent = before + dateStr + after;
|
|
620
|
-
const caret = before.length + dateStr.length;
|
|
621
|
-
savedCursor.current = { start: caret, end: caret };
|
|
622
|
-
setContent(newContent);
|
|
623
|
-
setDatePickerPos(null);
|
|
624
|
-
scheduleSave(title, newContent);
|
|
625
|
-
requestAnimationFrame(() => ta.focus());
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
// ── Context menu actions ────────────────────────────────────────────────
|
|
629
|
-
function applyContextFormat(fmt: "bold" | "italic" | "strike") {
|
|
630
|
-
if (!contextMenu) return;
|
|
631
|
-
const { selStart, selEnd } = contextMenu;
|
|
632
|
-
const sel = content.slice(selStart, selEnd);
|
|
633
|
-
const wrap = fmt === "bold" ? "**" : fmt === "italic" ? "_" : "~~";
|
|
634
|
-
const newContent = content.slice(0, selStart) + wrap + sel + wrap + content.slice(selEnd);
|
|
635
|
-
savedCursor.current = { start: selStart + wrap.length, end: selEnd + wrap.length };
|
|
636
|
-
setContent(newContent);
|
|
637
|
-
setContextMenu(null);
|
|
638
|
-
scheduleSave(title, newContent);
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
function convertSelectionToTask() {
|
|
642
|
-
if (!contextMenu) return;
|
|
643
|
-
const { selStart, selEnd } = contextMenu;
|
|
644
|
-
const sel = content.slice(selStart, selEnd).trim();
|
|
645
|
-
const lineStart = content.lastIndexOf("\n", selStart - 1) + 1;
|
|
646
|
-
const lineEnd = content.indexOf("\n", selEnd);
|
|
647
|
-
const actualLineEnd = lineEnd === -1 ? content.length : lineEnd;
|
|
648
|
-
const prefix = "- [ ] ";
|
|
649
|
-
const newContent = content.slice(0, lineStart) + prefix + sel + content.slice(actualLineEnd);
|
|
650
|
-
savedCursor.current = { start: lineStart + prefix.length + sel.length, end: lineStart + prefix.length + sel.length };
|
|
651
|
-
setContent(newContent);
|
|
652
|
-
setContextMenu(null);
|
|
653
|
-
scheduleSave(title, newContent);
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
return {
|
|
657
|
-
// States
|
|
658
|
-
mentionQuery, setMentionQuery, mentionStart, mentionIndex, setMentionIndex,
|
|
659
|
-
noteQuery, setNoteQuery, noteStart, noteIndex, setNoteIndex,
|
|
660
|
-
taskQuery, setTaskQuery, taskStart, taskIndex, setTaskIndex,
|
|
661
|
-
priorityQuery, setPriorityQuery, priorityStart, priorityIndex, setPriorityIndex,
|
|
662
|
-
plusOpen, setPlusOpen, plusStart,
|
|
663
|
-
templateOpen, setTemplateOpen,
|
|
664
|
-
contextMenu, setContextMenu, contextMenuRef,
|
|
665
|
-
datePickerPos, setDatePickerPos,
|
|
666
|
-
datePickerStart, setDatePickerStart,
|
|
667
|
-
datePickerEnd, setDatePickerEnd,
|
|
668
|
-
slashQuery, setSlashQuery, slashStart, slashIndex, setSlashIndex,
|
|
669
|
-
dropdownPos, setDropdownPos,
|
|
670
|
-
softHint, setSoftHint,
|
|
671
|
-
savedCursor,
|
|
672
|
-
// Suggestions
|
|
673
|
-
mentionSuggestions,
|
|
674
|
-
slashSuggestions,
|
|
675
|
-
noteSuggestions,
|
|
676
|
-
taskSuggestions,
|
|
677
|
-
prioritySuggestions,
|
|
678
|
-
// Actions
|
|
679
|
-
detectTriggers,
|
|
680
|
-
applySoftHint,
|
|
681
|
-
insertMention,
|
|
682
|
-
insertNoteLink,
|
|
683
|
-
insertTaskRef,
|
|
684
|
-
insertPriority,
|
|
685
|
-
triggerPlusOption,
|
|
686
|
-
executeSlashCommand,
|
|
687
|
-
insertTemplate,
|
|
688
|
-
confirmDatePicker,
|
|
689
|
-
applyContextFormat,
|
|
690
|
-
convertSelectionToTask,
|
|
691
|
-
};
|
|
692
|
-
}
|