nebula-cms 0.1.0
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/.claude/settings.local.json +42 -0
- package/.github/workflows/ci.yml +31 -0
- package/.mcp.json +12 -0
- package/.prettierignore +5 -0
- package/.prettierrc.cjs +22 -0
- package/AGENTS.md +183 -0
- package/LICENSE +201 -0
- package/README.md +128 -0
- package/package.json +74 -0
- package/playground/.claude/settings.local.json +5 -0
- package/playground/astro.config.mjs +7 -0
- package/playground/node_modules/.bin/astro +21 -0
- package/playground/node_modules/.bin/rollup +21 -0
- package/playground/node_modules/.bin/tsc +21 -0
- package/playground/node_modules/.bin/tsserver +21 -0
- package/playground/node_modules/.bin/vite +21 -0
- package/playground/node_modules/.vite/_svelte_metadata.json +1 -0
- package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js +80 -0
- package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js.map +7 -0
- package/playground/node_modules/.vite/deps/_metadata.json +184 -0
- package/playground/node_modules/.vite/deps/astro___aria-query.js +6776 -0
- package/playground/node_modules/.vite/deps/astro___aria-query.js.map +7 -0
- package/playground/node_modules/.vite/deps/astro___axobject-query.js +3754 -0
- package/playground/node_modules/.vite/deps/astro___axobject-query.js.map +7 -0
- package/playground/node_modules/.vite/deps/astro___html-escaper.js +34 -0
- package/playground/node_modules/.vite/deps/astro___html-escaper.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js +0 -0
- package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js +1005 -0
- package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js +8 -0
- package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js +21 -0
- package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js +223 -0
- package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js +204 -0
- package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js +27 -0
- package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js +4376 -0
- package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js +688 -0
- package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js +5099 -0
- package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js +23 -0
- package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js +148 -0
- package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js.map +7 -0
- package/playground/node_modules/.vite/deps/package.json +3 -0
- package/playground/node_modules/.vite/deps/smol-toml.js +843 -0
- package/playground/node_modules/.vite/deps/smol-toml.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte.js +55 -0
- package/playground/node_modules/.vite/deps/svelte.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte___clsx.js +9 -0
- package/playground/node_modules/.vite/deps/svelte___clsx.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_animate.js +57 -0
- package/playground/node_modules/.vite/deps/svelte_animate.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_attachments.js +15 -0
- package/playground/node_modules/.vite/deps/svelte_attachments.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_easing.js +67 -0
- package/playground/node_modules/.vite/deps/svelte_easing.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_events.js +11 -0
- package/playground/node_modules/.vite/deps/svelte_events.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal.js +5 -0
- package/playground/node_modules/.vite/deps/svelte_internal.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_client.js +402 -0
- package/playground/node_modules/.vite/deps/svelte_internal_client.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js +10 -0
- package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_legacy.js +35 -0
- package/playground/node_modules/.vite/deps/svelte_legacy.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_motion.js +545 -0
- package/playground/node_modules/.vite/deps/svelte_motion.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity.js +29 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity_window.js +127 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity_window.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_store.js +103 -0
- package/playground/node_modules/.vite/deps/svelte_store.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_transition.js +208 -0
- package/playground/node_modules/.vite/deps/svelte_transition.js.map +7 -0
- package/playground/package.json +16 -0
- package/playground/pnpm-lock.yaml +3167 -0
- package/playground/src/content/authors/jane-doe.json +8 -0
- package/playground/src/content/config/build.toml +2 -0
- package/playground/src/content/courses/web-fundamentals.json +29 -0
- package/playground/src/content/docs/advanced.mdx +6 -0
- package/playground/src/content/docs/intro.md +6 -0
- package/playground/src/content/guides/getting-started.mdx +6 -0
- package/playground/src/content/posts/hello-world.md +7 -0
- package/playground/src/content/products/t-shirt.json +16 -0
- package/playground/src/content/recipes/pancakes.mdoc +8 -0
- package/playground/src/content/settings/site.yml +2 -0
- package/playground/src/content.config.ts +198 -0
- package/playground/src/env.d.ts +1 -0
- package/playground/src/pages/index.astro +11 -0
- package/playground/src/pages/nebula.astro +14 -0
- package/pnpm-workspace.yaml +2 -0
- package/scripts/subset-icons.mjs +178 -0
- package/src/astro/index.ts +295 -0
- package/src/client/Admin.svelte +283 -0
- package/src/client/components/BackendPicker.svelte +291 -0
- package/src/client/components/DraftChip.svelte +46 -0
- package/src/client/components/MetadataForm.svelte +56 -0
- package/src/client/components/ThemeToggle.svelte +18 -0
- package/src/client/components/dialogs/DeleteDraftDialog.svelte +51 -0
- package/src/client/components/dialogs/FilenameDialog.svelte +129 -0
- package/src/client/components/editor/EditorPane.svelte +227 -0
- package/src/client/components/editor/EditorTabs.svelte +81 -0
- package/src/client/components/editor/EditorToolbar.svelte +131 -0
- package/src/client/components/editor/FormatSelector.svelte +66 -0
- package/src/client/components/editor/Toolbar.svelte +17 -0
- package/src/client/components/fields/ArrayField.svelte +339 -0
- package/src/client/components/fields/ArrayItem.svelte +325 -0
- package/src/client/components/fields/BooleanField.svelte +114 -0
- package/src/client/components/fields/DateField.svelte +82 -0
- package/src/client/components/fields/EnumField.svelte +74 -0
- package/src/client/components/fields/FieldWrapper.svelte +96 -0
- package/src/client/components/fields/NumberField.svelte +99 -0
- package/src/client/components/fields/ObjectField.svelte +121 -0
- package/src/client/components/fields/SchemaField.svelte +107 -0
- package/src/client/components/fields/StringField.svelte +104 -0
- package/src/client/components/sidebar/AdminSidebar.svelte +339 -0
- package/src/client/components/sidebar/AdminSidebarSort.svelte +123 -0
- package/src/client/css/a11y.css +14 -0
- package/src/client/css/btn.css +113 -0
- package/src/client/css/dialog.css +29 -0
- package/src/client/css/field-input.css +39 -0
- package/src/client/css/reset.css +59 -0
- package/src/client/css/theme.css +77 -0
- package/src/client/index.ts +1 -0
- package/src/client/js/drafts/merge.svelte.ts +121 -0
- package/src/client/js/drafts/ops.svelte.ts +227 -0
- package/src/client/js/drafts/storage.ts +108 -0
- package/src/client/js/drafts/workers/diff.ts +40 -0
- package/src/client/js/editor/editor.svelte.ts +343 -0
- package/src/client/js/editor/languages.ts +98 -0
- package/src/client/js/editor/link-wrap.ts +45 -0
- package/src/client/js/editor/markdown-shortcuts.ts +261 -0
- package/src/client/js/handlers/admin.ts +246 -0
- package/src/client/js/state/dialogs.svelte.ts +35 -0
- package/src/client/js/state/router.svelte.ts +156 -0
- package/src/client/js/state/schema.svelte.ts +140 -0
- package/src/client/js/state/state.svelte.ts +334 -0
- package/src/client/js/state/theme.svelte.ts +173 -0
- package/src/client/js/storage/adapter.ts +102 -0
- package/src/client/js/storage/client.ts +150 -0
- package/src/client/js/storage/db.ts +36 -0
- package/src/client/js/storage/fsa.ts +110 -0
- package/src/client/js/storage/github.ts +297 -0
- package/src/client/js/storage/storage.ts +83 -0
- package/src/client/js/storage/workers/frontmatter.ts +320 -0
- package/src/client/js/storage/workers/storage.ts +177 -0
- package/src/client/js/storage/workers/toml-parser.ts +106 -0
- package/src/client/js/storage/workers/yaml-parser.ts +132 -0
- package/src/client/js/utils/file-types.ts +192 -0
- package/src/client/js/utils/format.ts +16 -0
- package/src/client/js/utils/frontmatter.ts +38 -0
- package/src/client/js/utils/schema-utils.ts +295 -0
- package/src/client/js/utils/slug.ts +18 -0
- package/src/client/js/utils/sort.ts +84 -0
- package/src/client/js/utils/stable-stringify.ts +27 -0
- package/src/client/js/utils/url-utils.ts +38 -0
- package/src/types.ts +25 -0
- package/src/virtual.d.ts +22 -0
- package/svelte.config.js +4 -0
- package/tests/astro/build.test.ts +63 -0
- package/tests/astro/index.test.ts +689 -0
- package/tests/client/components/Admin.test.ts +446 -0
- package/tests/client/components/BackendPicker.test.ts +239 -0
- package/tests/client/components/DraftChip.test.ts +53 -0
- package/tests/client/components/MetadataForm.test.ts +164 -0
- package/tests/client/components/dialogs/DeleteDraftDialog.test.ts +91 -0
- package/tests/client/components/dialogs/FilenameDialog.test.ts +209 -0
- package/tests/client/components/dialogs/dialog-stubs.ts +19 -0
- package/tests/client/components/editor/EditorPane.test.ts +100 -0
- package/tests/client/components/editor/EditorTabs.test.ts +253 -0
- package/tests/client/components/editor/EditorToolbar.test.ts +252 -0
- package/tests/client/components/editor/fixtures.ts +31 -0
- package/tests/client/components/fields/ArrayField.test.ts +197 -0
- package/tests/client/components/fields/BooleanField.test.ts +206 -0
- package/tests/client/components/fields/DateField.test.ts +210 -0
- package/tests/client/components/fields/EnumField.test.ts +246 -0
- package/tests/client/components/fields/NumberField.test.ts +240 -0
- package/tests/client/components/fields/ObjectField.test.ts +157 -0
- package/tests/client/components/fields/SchemaField.test.ts +190 -0
- package/tests/client/components/fields/StringField.test.ts +223 -0
- package/tests/client/components/sidebar/AdminSidebar.test.ts +285 -0
- package/tests/client/components/sidebar/AdminSidebarSort.test.ts +135 -0
- package/tests/client/components/sidebar/sort-mock.ts +23 -0
- package/tests/client/js/drafts/fixtures.ts +22 -0
- package/tests/client/js/drafts/merge.test.ts +282 -0
- package/tests/client/js/drafts/ops.test.ts +658 -0
- package/tests/client/js/drafts/storage.test.ts +200 -0
- package/tests/client/js/drafts/workers/diff.test.ts +165 -0
- package/tests/client/js/editor/editor.test.ts +616 -0
- package/tests/client/js/editor/link-wrap.test.ts +225 -0
- package/tests/client/js/editor/markdown-shortcuts.test.ts +370 -0
- package/tests/client/js/handlers/admin.test.ts +467 -0
- package/tests/client/js/state/router.test.ts +619 -0
- package/tests/client/js/state/schema.test.ts +266 -0
- package/tests/client/js/state/state.test.ts +328 -0
- package/tests/client/js/storage/adapter.test.ts +115 -0
- package/tests/client/js/storage/client.test.ts +250 -0
- package/tests/client/js/storage/db.test.ts +59 -0
- package/tests/client/js/storage/fsa.test.ts +284 -0
- package/tests/client/js/storage/github.test.ts +349 -0
- package/tests/client/js/storage/mock-port.ts +95 -0
- package/tests/client/js/storage/storage.test.ts +77 -0
- package/tests/client/js/storage/workers/frontmatter.test.ts +479 -0
- package/tests/client/js/storage/workers/storage.test.ts +299 -0
- package/tests/client/js/storage/workers/toml-parser.test.ts +169 -0
- package/tests/client/js/storage/workers/yaml-parser.test.ts +168 -0
- package/tests/client/js/utils/file-types.test.ts +268 -0
- package/tests/client/js/utils/frontmatter.test.ts +87 -0
- package/tests/client/js/utils/schema-utils.test.ts +318 -0
- package/tests/client/js/utils/slug.test.ts +58 -0
- package/tests/client/js/utils/sort.test.ts +276 -0
- package/tests/client/js/utils/stable-stringify.test.ts +68 -0
- package/tests/client/js/utils/url-utils.test.ts +70 -0
- package/tests/e2e/backend-connection.test.ts +301 -0
- package/tests/e2e/draft-lifecycle.test.ts +388 -0
- package/tests/e2e/editing.test.ts +355 -0
- package/tests/e2e/github-adapter.test.ts +330 -0
- package/tests/e2e/helpers/mock-adapter.ts +166 -0
- package/tests/e2e/helpers/test-app.ts +155 -0
- package/tests/e2e/navigation.test.ts +358 -0
- package/tests/e2e/publishing.test.ts +345 -0
- package/tests/e2e/unsaved-changes.test.ts +317 -0
- package/tests/setup.ts +2 -0
- package/tests/stubs/codemirror.ts +197 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +178 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { syntaxTree } from '@codemirror/language';
|
|
2
|
+
import { EditorSelection, type EditorState } from '@codemirror/state';
|
|
3
|
+
import { EditorView, type KeyBinding } from '@codemirror/view';
|
|
4
|
+
import { isURL } from '../utils/url-utils';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
//////////////////////////////
|
|
8
|
+
// Bracket/Quote Pair Map
|
|
9
|
+
//////////////////////////////
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Maps opening characters to their closing counterparts for selection wrapping
|
|
13
|
+
const wrapPairs: Record<string, string> = {
|
|
14
|
+
'[': ']',
|
|
15
|
+
'{': '}',
|
|
16
|
+
'(': ')',
|
|
17
|
+
"'": "'",
|
|
18
|
+
'"': '"',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
//////////////////////////////
|
|
23
|
+
// Formatting Toggle Helpers
|
|
24
|
+
//////////////////////////////
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Finds the innermost syntax tree node of the given type that contains the
|
|
29
|
+
* given position. Returns the node's from/to range, or null if not found.
|
|
30
|
+
* @param {EditorState} state - The current editor state
|
|
31
|
+
* @param {number} pos - The document position to check
|
|
32
|
+
* @param {string} nodeType - The syntax tree node name to search for
|
|
33
|
+
* @return {{ from: number, to: number } | null} The node range, or null
|
|
34
|
+
*/
|
|
35
|
+
function findWrappingNode(
|
|
36
|
+
state: EditorState,
|
|
37
|
+
pos: number,
|
|
38
|
+
nodeType: string,
|
|
39
|
+
): { from: number; to: number } | null {
|
|
40
|
+
let result: { from: number; to: number } | null = null;
|
|
41
|
+
syntaxTree(state).iterate({
|
|
42
|
+
from: pos,
|
|
43
|
+
to: pos,
|
|
44
|
+
enter(node) {
|
|
45
|
+
if (node.name === nodeType) {
|
|
46
|
+
result = { from: node.from, to: node.to };
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a CodeMirror command that toggles a markdown formatting marker
|
|
55
|
+
* (e.g., ** for bold, _ for italic) using syntax tree detection.
|
|
56
|
+
* When inside the target node type, it unwraps. Otherwise, it wraps
|
|
57
|
+
* the selection or inserts empty markers with the cursor between them.
|
|
58
|
+
* @param {string} marker - The markdown marker string (e.g., "**" or "_")
|
|
59
|
+
* @param {string} nodeType - The syntax tree node name (e.g., "StrongEmphasis" or "Emphasis")
|
|
60
|
+
* @return {(view: EditorView) => boolean} A CodeMirror command function
|
|
61
|
+
*/
|
|
62
|
+
function toggleMarker(
|
|
63
|
+
marker: string,
|
|
64
|
+
nodeType: string,
|
|
65
|
+
): (view: EditorView) => boolean {
|
|
66
|
+
return (view: EditorView): boolean => {
|
|
67
|
+
const { state } = view;
|
|
68
|
+
const changes = state.changeByRange((range) => {
|
|
69
|
+
// Check if cursor/selection is inside the target node
|
|
70
|
+
const node = findWrappingNode(state, range.from, nodeType);
|
|
71
|
+
|
|
72
|
+
if (node) {
|
|
73
|
+
/*
|
|
74
|
+
* Unwrap — remove markers from both ends of the node.
|
|
75
|
+
* Post-change coordinates: removing the opening marker shifts everything
|
|
76
|
+
* left by len, and removing the closing marker doesn't affect earlier positions.
|
|
77
|
+
* So the inner text spans from node.from to node.to - len * 2.
|
|
78
|
+
*/
|
|
79
|
+
const len = marker.length;
|
|
80
|
+
/*
|
|
81
|
+
* Preserve cursor vs selection: if the user had a cursor (no selection),
|
|
82
|
+
* keep it as a cursor inside the unwrapped text rather than selecting all of it.
|
|
83
|
+
* Clamp the post-change cursor: if the cursor was at or before the opening marker,
|
|
84
|
+
* it lands at the start of the unwrapped text (node.from).
|
|
85
|
+
*/
|
|
86
|
+
const innerEnd = node.to - len * 2;
|
|
87
|
+
const postRange = range.empty
|
|
88
|
+
? EditorSelection.cursor(
|
|
89
|
+
Math.max(node.from, Math.min(range.from - len, innerEnd)),
|
|
90
|
+
)
|
|
91
|
+
: EditorSelection.range(node.from, innerEnd);
|
|
92
|
+
return {
|
|
93
|
+
range: postRange,
|
|
94
|
+
changes: [
|
|
95
|
+
{ from: node.from, to: node.from + len, insert: '' },
|
|
96
|
+
{ from: node.to - len, to: node.to, insert: '' },
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (range.empty) {
|
|
102
|
+
// No selection — insert empty markers and place cursor between them
|
|
103
|
+
const insert = marker + marker;
|
|
104
|
+
return {
|
|
105
|
+
range: EditorSelection.cursor(range.from + marker.length),
|
|
106
|
+
changes: { from: range.from, insert },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Wrap selection in markers, keep text selected
|
|
111
|
+
return {
|
|
112
|
+
range: EditorSelection.range(
|
|
113
|
+
range.from + marker.length,
|
|
114
|
+
range.to + marker.length,
|
|
115
|
+
),
|
|
116
|
+
changes: [
|
|
117
|
+
{ from: range.from, insert: marker },
|
|
118
|
+
{ from: range.to, insert: marker },
|
|
119
|
+
],
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
view.dispatch(changes);
|
|
124
|
+
return true;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/*
|
|
129
|
+
//////////////////////////////
|
|
130
|
+
// Link Insertion Command
|
|
131
|
+
//////////////////////////////
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Inserts a markdown link. With a selection, wraps as [text]() and places the
|
|
136
|
+
* cursor in the URL parentheses. Without a selection, inserts []() and places
|
|
137
|
+
* the cursor in the link text brackets. Does nothing inside existing Link nodes.
|
|
138
|
+
* @param {EditorView} view - The CodeMirror editor view
|
|
139
|
+
* @return {boolean} True if the command was handled
|
|
140
|
+
*/
|
|
141
|
+
function insertLink(view: EditorView): boolean {
|
|
142
|
+
const { state } = view;
|
|
143
|
+
|
|
144
|
+
// Don't re-wrap existing links — check the primary selection
|
|
145
|
+
if (findWrappingNode(state, state.selection.main.from, 'Link')) return false;
|
|
146
|
+
|
|
147
|
+
const changes = state.changeByRange((range) => {
|
|
148
|
+
if (range.empty) {
|
|
149
|
+
// No selection — insert []() with cursor inside []
|
|
150
|
+
return {
|
|
151
|
+
range: EditorSelection.cursor(range.from + 1),
|
|
152
|
+
changes: { from: range.from, insert: '[]()' },
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Wrap selection as [text]() with cursor inside ()
|
|
157
|
+
const selectedText = state.sliceDoc(range.from, range.to);
|
|
158
|
+
const replacement = `[${selectedText}]()`;
|
|
159
|
+
return {
|
|
160
|
+
// Cursor between the parentheses: [text](|)
|
|
161
|
+
range: EditorSelection.cursor(range.from + selectedText.length + 3),
|
|
162
|
+
changes: { from: range.from, to: range.to, insert: replacement },
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
view.dispatch(changes);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
//////////////////////////////
|
|
172
|
+
// Keymap Export
|
|
173
|
+
//////////////////////////////
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
// Keymap bindings for markdown formatting shortcuts (bold, italic, link)
|
|
177
|
+
export const markdownShortcutsKeymap: KeyBinding[] = [
|
|
178
|
+
{ key: 'Mod-b', run: toggleMarker('**', 'StrongEmphasis') },
|
|
179
|
+
{ key: 'Mod-i', run: toggleMarker('_', 'Emphasis') },
|
|
180
|
+
{ key: 'Mod-k', run: insertLink },
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
//////////////////////////////
|
|
185
|
+
// Smart Paste Handler
|
|
186
|
+
//////////////////////////////
|
|
187
|
+
*/
|
|
188
|
+
|
|
189
|
+
/*
|
|
190
|
+
* Intercepts paste events to wrap selected text as a markdown link when
|
|
191
|
+
* the clipboard contains a URL. Only activates when text is selected.
|
|
192
|
+
*/
|
|
193
|
+
const smartPasteHandler = EditorView.domEventHandlers({
|
|
194
|
+
paste(event: ClipboardEvent, view: EditorView) {
|
|
195
|
+
const { from, to, empty } = view.state.selection.main;
|
|
196
|
+
if (empty) return false;
|
|
197
|
+
|
|
198
|
+
const clipboardText = event.clipboardData?.getData('text/plain') ?? '';
|
|
199
|
+
if (!isURL(clipboardText)) return false;
|
|
200
|
+
|
|
201
|
+
const url = clipboardText.trim();
|
|
202
|
+
const selectedText = view.state.sliceDoc(from, to);
|
|
203
|
+
const replacement = `[${selectedText}](${url})`;
|
|
204
|
+
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
view.dispatch({
|
|
207
|
+
changes: { from, to, insert: replacement },
|
|
208
|
+
selection: EditorSelection.cursor(from + replacement.length),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return true;
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
/*
|
|
216
|
+
//////////////////////////////
|
|
217
|
+
// Bracket/Quote Wrap Handler
|
|
218
|
+
//////////////////////////////
|
|
219
|
+
*/
|
|
220
|
+
|
|
221
|
+
/*
|
|
222
|
+
* Intercepts single-character input to wrap selected text in matching
|
|
223
|
+
* bracket or quote pairs. Only activates when text is selected and
|
|
224
|
+
* the typed character is a recognized opening bracket or quote.
|
|
225
|
+
*/
|
|
226
|
+
const bracketWrapHandler = EditorView.inputHandler.of(
|
|
227
|
+
(view: EditorView, from: number, to: number, text: string) => {
|
|
228
|
+
const closing = wrapPairs[text];
|
|
229
|
+
if (!closing) return false;
|
|
230
|
+
|
|
231
|
+
const { main } = view.state.selection;
|
|
232
|
+
if (main.empty) return false;
|
|
233
|
+
|
|
234
|
+
const selectedText = view.state.sliceDoc(main.from, main.to);
|
|
235
|
+
view.dispatch({
|
|
236
|
+
changes: {
|
|
237
|
+
from: main.from,
|
|
238
|
+
to: main.to,
|
|
239
|
+
insert: text + selectedText + closing,
|
|
240
|
+
},
|
|
241
|
+
selection: EditorSelection.range(
|
|
242
|
+
main.from + 1,
|
|
243
|
+
main.from + 1 + selectedText.length,
|
|
244
|
+
),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
return true;
|
|
248
|
+
},
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
/*
|
|
252
|
+
//////////////////////////////
|
|
253
|
+
// Extensions Export
|
|
254
|
+
//////////////////////////////
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
// Non-keymap extensions: smart paste URL handler and bracket/quote wrapping
|
|
258
|
+
export const markdownShortcutsExtensions = [
|
|
259
|
+
smartPasteHandler,
|
|
260
|
+
bracketWrapHandler,
|
|
261
|
+
];
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Admin action handlers for save, publish, delete, and sidebar data construction.
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
clearEditor,
|
|
6
|
+
getEditorFile,
|
|
7
|
+
setFilename,
|
|
8
|
+
editor,
|
|
9
|
+
} from '../editor/editor.svelte';
|
|
10
|
+
import {
|
|
11
|
+
saveDraftToIDB,
|
|
12
|
+
publishFile,
|
|
13
|
+
deleteCurrentDraft,
|
|
14
|
+
} from '../drafts/ops.svelte';
|
|
15
|
+
import {
|
|
16
|
+
collections,
|
|
17
|
+
reloadCollection,
|
|
18
|
+
refreshDrafts,
|
|
19
|
+
updateContentItem,
|
|
20
|
+
type ContentItem,
|
|
21
|
+
} from '../state/state.svelte';
|
|
22
|
+
import { navigate, adminPath, type AdminRoute } from '../state/router.svelte';
|
|
23
|
+
import {
|
|
24
|
+
getCollectionTitle,
|
|
25
|
+
getCollectionDescription,
|
|
26
|
+
} from '../state/schema.svelte';
|
|
27
|
+
import type { Draft } from '../drafts/storage';
|
|
28
|
+
import { toSortDate } from '../utils/sort';
|
|
29
|
+
import { stripExtension } from '../utils/file-types';
|
|
30
|
+
import type { SidebarItem } from '../utils/sort';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds the content sidebar item list by merging live content with draft data.
|
|
34
|
+
* @param {ContentItem[]} contentList - Live content items from the storage worker
|
|
35
|
+
* @param {Draft[]} drafts - All drafts for the active collection
|
|
36
|
+
* @param {Record<string, boolean>} outdatedMap - Map of draft ID to outdated status
|
|
37
|
+
* @param {string | null} activeCollection - The currently active collection name
|
|
38
|
+
* @return {SidebarItem[]} Merged list of live and draft sidebar items
|
|
39
|
+
*/
|
|
40
|
+
export function buildContentItems(
|
|
41
|
+
contentList: ContentItem[],
|
|
42
|
+
drafts: Draft[],
|
|
43
|
+
outdatedMap: Record<string, boolean>,
|
|
44
|
+
activeCollection: string | null,
|
|
45
|
+
): SidebarItem[] {
|
|
46
|
+
// Build a filename → draft lookup map for O(1) access per content item
|
|
47
|
+
const draftByFile = new Map(
|
|
48
|
+
drafts.filter((d) => !d.isNew && d.filename).map((d) => [d.filename, d]),
|
|
49
|
+
);
|
|
50
|
+
const liveItems = contentList.map((item) => {
|
|
51
|
+
const title =
|
|
52
|
+
typeof item.data.title === 'string' ? item.data.title : item.filename;
|
|
53
|
+
const slug = stripExtension(item.filename);
|
|
54
|
+
const draft = draftByFile.get(item.filename);
|
|
55
|
+
const date = toSortDate(item.data.published);
|
|
56
|
+
return {
|
|
57
|
+
label: title,
|
|
58
|
+
href: adminPath(activeCollection!, slug),
|
|
59
|
+
subtitle: item.filename,
|
|
60
|
+
...(date ? { date } : {}),
|
|
61
|
+
...(draft
|
|
62
|
+
? {
|
|
63
|
+
draftId: draft.id,
|
|
64
|
+
isDraft: true,
|
|
65
|
+
isOutdated: outdatedMap[draft.id] ?? false,
|
|
66
|
+
}
|
|
67
|
+
: {}),
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
const newDraftItems = drafts
|
|
71
|
+
.filter((d) => d.isNew)
|
|
72
|
+
.map((d) => {
|
|
73
|
+
const date = toSortDate(d.formData.published);
|
|
74
|
+
return {
|
|
75
|
+
label:
|
|
76
|
+
typeof d.formData.title === 'string'
|
|
77
|
+
? d.formData.title
|
|
78
|
+
: 'Untitled Draft',
|
|
79
|
+
href: adminPath(activeCollection!, `draft-${d.id}`),
|
|
80
|
+
draftId: d.id,
|
|
81
|
+
isDraft: true as const,
|
|
82
|
+
isOutdated: false,
|
|
83
|
+
...(date ? { date } : {}),
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
return [...liveItems, ...newDraftItems];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Builds the collection sidebar items from collection names, using schema title/description when available.
|
|
91
|
+
* @return {SidebarItem[]} Collection sidebar items with hrefs under the configured basePath
|
|
92
|
+
*/
|
|
93
|
+
export function buildCollectionItems(): SidebarItem[] {
|
|
94
|
+
return collections.map((name) => ({
|
|
95
|
+
label:
|
|
96
|
+
getCollectionTitle(name) ?? name.charAt(0).toUpperCase() + name.slice(1),
|
|
97
|
+
href: adminPath(name),
|
|
98
|
+
subtitle: getCollectionDescription(name) ?? undefined,
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Derives the active file/draft href for sidebar highlighting from the current route.
|
|
104
|
+
* @param {AdminRoute} route - The current admin route
|
|
105
|
+
* @return {string | undefined} The href of the active file or draft, or undefined if not in a file/draft view
|
|
106
|
+
*/
|
|
107
|
+
export function buildActiveFileHref(route: AdminRoute): string | undefined {
|
|
108
|
+
if (route.view === 'file') {
|
|
109
|
+
return adminPath(route.collection, route.slug);
|
|
110
|
+
}
|
|
111
|
+
if (route.view === 'draft') {
|
|
112
|
+
return adminPath(route.collection, `draft-${route.draftId}`);
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Saves the current editor content as a draft to IndexedDB and refreshes the sidebar's draft list so changes appear immediately.
|
|
119
|
+
* @param {string | null} activeCollection - The active collection for refreshing the draft list
|
|
120
|
+
* @return {Promise<void>}
|
|
121
|
+
*/
|
|
122
|
+
export async function handleSave(
|
|
123
|
+
activeCollection: string | null,
|
|
124
|
+
): Promise<void> {
|
|
125
|
+
await saveDraftToIDB();
|
|
126
|
+
if (activeCollection) {
|
|
127
|
+
await refreshDrafts(activeCollection);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Result of attempting a publish operation.
|
|
132
|
+
export type PublishResult =
|
|
133
|
+
| { status: 'ok' }
|
|
134
|
+
| { status: 'no-file' }
|
|
135
|
+
| { status: 'needs-filename' };
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Publishes the current editor content via StorageClient. If the file has no filename, returns a status indicating the filename dialog should be shown.
|
|
139
|
+
* @param {string | null} activeCollection - The currently active collection name
|
|
140
|
+
* @return {Promise<PublishResult>} The result of the publish attempt
|
|
141
|
+
*/
|
|
142
|
+
export async function handlePublish(
|
|
143
|
+
activeCollection: string | null,
|
|
144
|
+
): Promise<PublishResult> {
|
|
145
|
+
const file = getEditorFile();
|
|
146
|
+
if (!file) return { status: 'no-file' };
|
|
147
|
+
if (!file.filename) return { status: 'needs-filename' };
|
|
148
|
+
if (!activeCollection) return { status: 'no-file' };
|
|
149
|
+
|
|
150
|
+
// Pass originalFilename so publishFile can delete the old file when format changes
|
|
151
|
+
const originalFn = editor.originalFilename;
|
|
152
|
+
await publishFile(
|
|
153
|
+
activeCollection,
|
|
154
|
+
file.filename,
|
|
155
|
+
originalFn !== file.filename ? originalFn : undefined,
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
// Determine if the filename changed (format conversion or new file)
|
|
159
|
+
const filenameChanged = originalFn && originalFn !== file.filename;
|
|
160
|
+
|
|
161
|
+
if (file.isNewDraft || filenameChanged) {
|
|
162
|
+
// New file or renamed file — not in contentList under this name, need a full refresh
|
|
163
|
+
reloadCollection(activeCollection);
|
|
164
|
+
} else {
|
|
165
|
+
/*
|
|
166
|
+
* Existing file, same name — optimistically update sidebar with current formData
|
|
167
|
+
* so the title reflects edits instantly without re-fetching all files.
|
|
168
|
+
*/
|
|
169
|
+
updateContentItem(file.filename, file.formData);
|
|
170
|
+
}
|
|
171
|
+
await refreshDrafts(activeCollection);
|
|
172
|
+
return { status: 'ok' };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Deletes the current draft. For drafts of live content, navigates to the live file's URL so the live version reloads in-place. For new drafts, navigates back to the collection list.
|
|
177
|
+
* @param {string | null} activeCollection - The collection to navigate within
|
|
178
|
+
* @return {Promise<void>}
|
|
179
|
+
*/
|
|
180
|
+
export async function handleDeleteDraft(
|
|
181
|
+
activeCollection: string | null,
|
|
182
|
+
): Promise<void> {
|
|
183
|
+
const file = getEditorFile();
|
|
184
|
+
const wasNewDraft = file?.isNewDraft ?? true;
|
|
185
|
+
const liveFilename = file?.filename;
|
|
186
|
+
|
|
187
|
+
await deleteCurrentDraft();
|
|
188
|
+
|
|
189
|
+
if (!activeCollection) return;
|
|
190
|
+
// Capture narrowed value before awaits (TS can't narrow across async boundaries)
|
|
191
|
+
const collection = activeCollection;
|
|
192
|
+
|
|
193
|
+
/*
|
|
194
|
+
* Refresh drafts list only — live content hasn't changed, so no need to
|
|
195
|
+
* reload the full collection (which re-reads all files and causes a flash).
|
|
196
|
+
*/
|
|
197
|
+
await refreshDrafts(collection);
|
|
198
|
+
|
|
199
|
+
/*
|
|
200
|
+
* Clear editor so the route change triggers a fresh load (preloadFile has
|
|
201
|
+
* an early return if the same filename is already open).
|
|
202
|
+
*/
|
|
203
|
+
clearEditor();
|
|
204
|
+
|
|
205
|
+
if (!wasNewDraft && liveFilename) {
|
|
206
|
+
// Draft of live content — navigate to the live file so it reloads from disk
|
|
207
|
+
const slug = stripExtension(liveFilename);
|
|
208
|
+
navigate(adminPath(collection, slug));
|
|
209
|
+
} else {
|
|
210
|
+
// New draft — no live file to return to, go to collection list
|
|
211
|
+
navigate(adminPath(collection));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Handles the filename dialog confirmation: sets the filename on the editor and triggers publish.
|
|
217
|
+
* @param {string} filename - The chosen filename (with extension)
|
|
218
|
+
* @param {string | null} activeCollection - The active collection name
|
|
219
|
+
* @return {Promise<void>}
|
|
220
|
+
*/
|
|
221
|
+
export async function handleFilenameConfirm(
|
|
222
|
+
filename: string,
|
|
223
|
+
activeCollection: string | null,
|
|
224
|
+
): Promise<void> {
|
|
225
|
+
setFilename(filename);
|
|
226
|
+
await handlePublish(activeCollection);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Checks whether the publish button should be disabled by verifying required schema fields are populated.
|
|
231
|
+
* @param {Record<string, unknown> | null} schema - The active JSON schema
|
|
232
|
+
* @param {Record<string, unknown>} formData - The current form data
|
|
233
|
+
* @return {boolean} True if publish should be disabled
|
|
234
|
+
*/
|
|
235
|
+
export function computePublishDisabled(
|
|
236
|
+
schema: Record<string, unknown> | null,
|
|
237
|
+
formData: Record<string, unknown>,
|
|
238
|
+
): boolean {
|
|
239
|
+
if (!schema) return true;
|
|
240
|
+
const required = schema['required'] as string[] | undefined;
|
|
241
|
+
if (!required) return false;
|
|
242
|
+
return required.some((key) => {
|
|
243
|
+
const val = formData[key];
|
|
244
|
+
return val === undefined || val === null || val === '';
|
|
245
|
+
});
|
|
246
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Reactive state for dialog visibility. Lives here rather than in Admin.svelte
|
|
3
|
+
* so that EditorToolbar (and other components) can trigger dialogs without
|
|
4
|
+
* callback drilling through intermediate components.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Which dialog is currently open, or null if none
|
|
8
|
+
let active = $state<'filename' | 'delete' | null>(null);
|
|
9
|
+
|
|
10
|
+
// The dialog type union, exported for type-safe consumers
|
|
11
|
+
export type DialogType = 'filename' | 'delete';
|
|
12
|
+
|
|
13
|
+
export const dialog = {
|
|
14
|
+
// The currently open dialog, or null if none.
|
|
15
|
+
get active(): DialogType | null {
|
|
16
|
+
return active;
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Opens a dialog by type. Only one dialog can be open at a time.
|
|
21
|
+
* @param {DialogType} type - The dialog to open
|
|
22
|
+
* @return {void}
|
|
23
|
+
*/
|
|
24
|
+
open(type: DialogType): void {
|
|
25
|
+
active = type;
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Closes whichever dialog is currently open.
|
|
30
|
+
* @return {void}
|
|
31
|
+
*/
|
|
32
|
+
close(): void {
|
|
33
|
+
active = null;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Parsed route state for the admin SPA.
|
|
3
|
+
* The base path is set at build time via virtual:nebula/config so both
|
|
4
|
+
* client-side code and the Vite dev server share the same value.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import config from 'virtual:nebula/config';
|
|
8
|
+
|
|
9
|
+
export type AdminRoute =
|
|
10
|
+
| { view: 'home' }
|
|
11
|
+
| { view: 'collection'; collection: string }
|
|
12
|
+
| { view: 'file'; collection: string; slug: string }
|
|
13
|
+
| { view: 'draft'; collection: string; draftId: string };
|
|
14
|
+
|
|
15
|
+
// Build-time constant — configured via the Astro integration
|
|
16
|
+
const basePath = config.basePath;
|
|
17
|
+
|
|
18
|
+
// Current route, reactive via Svelte 5 runes
|
|
19
|
+
let route = $state<AdminRoute>(parsePathname(location.pathname));
|
|
20
|
+
|
|
21
|
+
export const nav = {
|
|
22
|
+
// Current parsed admin route.
|
|
23
|
+
get route(): AdminRoute {
|
|
24
|
+
return route;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Builds an absolute path under the configured basePath by joining segments.
|
|
30
|
+
* Handles the root basePath case ('/') without producing double slashes.
|
|
31
|
+
* @param {...string} segments - Path segments to append (e.g. 'authors', 'my-post')
|
|
32
|
+
* @return {string} The joined path (e.g. '/admin/authors/my-post' or '/authors/my-post')
|
|
33
|
+
*/
|
|
34
|
+
export function adminPath(...segments: string[]): string {
|
|
35
|
+
if (segments.length === 0) return basePath;
|
|
36
|
+
const prefix = basePath === '/' ? '' : basePath;
|
|
37
|
+
return prefix + '/' + segments.join('/');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Checks whether a pathname falls under the configured basePath.
|
|
42
|
+
* Matches the basePath exactly or as a prefix followed by '/'.
|
|
43
|
+
* @param {string} pathname - The URL pathname to test
|
|
44
|
+
* @return {boolean} True if the pathname is within the basePath
|
|
45
|
+
*/
|
|
46
|
+
function isUnderBasePath(pathname: string): boolean {
|
|
47
|
+
// When basePath is '/', every absolute path is under it
|
|
48
|
+
if (basePath === '/') return pathname.startsWith('/');
|
|
49
|
+
return pathname === basePath || pathname.startsWith(basePath + '/');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Parses a pathname into an AdminRoute by stripping the basePath prefix.
|
|
54
|
+
* @param {string} pathname - The URL pathname to parse
|
|
55
|
+
* @return {AdminRoute} The route corresponding to the given pathname
|
|
56
|
+
*/
|
|
57
|
+
function parsePathname(pathname: string): AdminRoute {
|
|
58
|
+
/*
|
|
59
|
+
* Strip the basePath prefix at a segment boundary, then split the remainder.
|
|
60
|
+
* Must mirror isUnderBasePath's boundary check to avoid false matches
|
|
61
|
+
* (e.g. basePath '/cms' should not match pathname '/cmsextra/posts').
|
|
62
|
+
*/
|
|
63
|
+
const underBase = isUnderBasePath(pathname);
|
|
64
|
+
const rest = underBase ? pathname.slice(basePath.length) : pathname;
|
|
65
|
+
const segments = rest.split('/').filter(Boolean);
|
|
66
|
+
/*
|
|
67
|
+
* Draft URLs use a 2-segment pattern: {basePath}/{collection}/draft-{draftId}
|
|
68
|
+
* This keeps the same URL depth as regular files so Astro static paths work.
|
|
69
|
+
*/
|
|
70
|
+
if (segments.length >= 2 && segments[1].startsWith('draft-')) {
|
|
71
|
+
return {
|
|
72
|
+
view: 'draft',
|
|
73
|
+
collection: segments[0],
|
|
74
|
+
draftId: segments[1].slice('draft-'.length),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (segments.length >= 2) {
|
|
78
|
+
return { view: 'file', collection: segments[0], slug: segments[1] };
|
|
79
|
+
}
|
|
80
|
+
if (segments.length === 1) {
|
|
81
|
+
return { view: 'collection', collection: segments[0] };
|
|
82
|
+
}
|
|
83
|
+
return { view: 'home' };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Navigates to a path within the admin SPA using the Navigation API.
|
|
88
|
+
* @param {string} path - The path to navigate to (e.g., '/admin/posts')
|
|
89
|
+
* @return {void}
|
|
90
|
+
*/
|
|
91
|
+
export function navigate(path: string): void {
|
|
92
|
+
navigation.navigate(path);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Callback to check if the editor has unsaved changes
|
|
96
|
+
let dirtyChecker: (() => boolean) | null = null;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Registers a function that returns whether the editor has unsaved changes.
|
|
100
|
+
* @param {() => boolean} checker - Function returning true if there are unsaved changes
|
|
101
|
+
* @return {void}
|
|
102
|
+
*/
|
|
103
|
+
export function registerDirtyChecker(checker: () => boolean): void {
|
|
104
|
+
dirtyChecker = checker;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Guard against duplicate listener registration (e.g., HMR remount)
|
|
108
|
+
let initialized = false;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Initializes the Navigation API listener, intercepting navigations under the
|
|
112
|
+
* configured basePath and updating reactive route state.
|
|
113
|
+
* Safe to call multiple times — registers the listener only once.
|
|
114
|
+
* @return {void}
|
|
115
|
+
*/
|
|
116
|
+
export function initRouter(): void {
|
|
117
|
+
if (initialized) return;
|
|
118
|
+
initialized = true;
|
|
119
|
+
|
|
120
|
+
navigation.addEventListener('navigate', (event) => {
|
|
121
|
+
const url = new URL(event.destination.url);
|
|
122
|
+
|
|
123
|
+
// Only intercept navigations within the configured basePath
|
|
124
|
+
if (!isUnderBasePath(url.pathname)) return;
|
|
125
|
+
|
|
126
|
+
// Don't intercept downloads or hash-only changes
|
|
127
|
+
if (event.hashChange || event.downloadRequest) return;
|
|
128
|
+
|
|
129
|
+
if (!event.canIntercept) return;
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* Block navigation if editor has unsaved changes and user cancels.
|
|
133
|
+
* This check must happen before event.intercept() — if it were inside the
|
|
134
|
+
* handler, the URL would already be updated by the time the user cancels.
|
|
135
|
+
*/
|
|
136
|
+
if (
|
|
137
|
+
dirtyChecker?.() &&
|
|
138
|
+
!confirm('You have unsaved changes. Leave without saving?')
|
|
139
|
+
) {
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
event.intercept({
|
|
145
|
+
handler() {
|
|
146
|
+
route = parsePathname(url.pathname);
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
window.addEventListener('beforeunload', (event) => {
|
|
152
|
+
if (dirtyChecker?.()) {
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|