Skip to content

Commit fe8e200

Browse files
fix(viola): Fix file path handling with entryContext & rename draft to drafts
- Use `file.filename` directly (relative to entryContext) in sandbox files map - Compute `prevFileDir` relative to `entryContext` when creating new files - Remove `defaultDraftDir` export from `sandbox.ts` (now inlined) - Rename template draft directories from `draft` to `drafts` - Add `entryContext: "drafts"` to minimal template config
1 parent 7884d5e commit fe8e200

17 files changed

Lines changed: 14 additions & 16 deletions

packages/viola/src/libs/editor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ const saveContent = debounce(
103103
.find((s) => s.trim())
104104
?.trim() || '';
105105
const markdown = editor.getMarkdown();
106-
$$sandbox.files[
107-
join($$sandbox.vivliostyleConfig.entryContext || '', file.filename)
108-
] = ref(new Blob([markdown], { type: 'text/markdown' }));
106+
$$sandbox.files[file.filename] = ref(
107+
new Blob([markdown], { type: 'text/markdown' }),
108+
);
109109
},
110110
1000,
111111
{ trailing: true },

packages/viola/src/stores/actions/content-file.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { EntryConfig } from '@vivliostyle/cli/schema';
22
import { invariant } from 'outvariant';
3-
import { dirname, join, sep } from 'pathe';
3+
import { dirname, join, relative, sep } from 'pathe';
44
import { ref } from 'valtio';
55

66
import { setupEditor } from '../../libs/editor';
77
import { generateId, generateRandomName } from '../../libs/generate-id';
88
import { $content, $sandbox, $ui } from '../accessors';
99
import type { ContentId } from '../proxies/content';
10-
import { defaultDraftDir } from '../proxies/sandbox';
1110

1211
export async function createContentFile({
1312
format,
@@ -19,15 +18,17 @@ export async function createContentFile({
1918
const $$content = $content.valueOrThrow();
2019
const $$sandbox = $sandbox.valueOrThrow();
2120
const prevFile = insertAfter && $$content.files.get(insertAfter);
22-
const prevFileDir = prevFile && dirname(prevFile.filename);
21+
const entryCotnext = $$sandbox.vivliostyleConfig.entryContext || '';
22+
const prevFileDir =
23+
prevFile && relative(entryCotnext, dirname(prevFile.filename));
2324
const contentId = generateId<ContentId>();
2425
const extname = '.md';
2526
const basename = `${generateRandomName()}${extname}`;
26-
const entryPath = join(prevFileDir || defaultDraftDir, basename);
27-
const filename = join(
28-
$$sandbox.vivliostyleConfig.entryContext || '',
29-
entryPath,
27+
const entryPath = join(
28+
prevFileDir && !prevFileDir.startsWith('.') ? prevFileDir : '',
29+
basename,
3030
);
31+
const filename = join(entryCotnext, entryPath);
3132
const index =
3233
((insertAfter && $$content.readingOrder.indexOf(insertAfter)) ?? -1) + 1;
3334

@@ -74,9 +75,7 @@ export async function deleteContentFile({
7475
$$sandbox.updateVivliostyleConfig((config) => {
7576
config.entry = [config.entry].flat().toSpliced(index, 1);
7677
});
77-
delete $$sandbox.files[
78-
join($$sandbox.vivliostyleConfig.entryContext || '', file.filename)
79-
];
78+
delete $$sandbox.files[file.filename];
8079
return contentId;
8180
}
8281

packages/viola/src/stores/proxies/sandbox.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ interface Tree<T> {
1313
[key: string]: Tree<T>;
1414
}
1515

16-
export const defaultDraftDir = 'drafts';
17-
1816
const origin = `https://${import.meta.env.VITE_APP_HOSTNAME}${location.port ? `:${location.port}` : ''}`;
1917

2018
const defaultCss = /* css */ `:root {
File renamed without changes.

packages/viola/templates/basic/draft/02_introduction.md renamed to packages/viola/templates/basic/drafts/02_introduction.md

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/viola/templates/basic/draft/06_styling-guide.md renamed to packages/viola/templates/basic/drafts/06_styling-guide.md

File renamed without changes.

packages/viola/templates/basic/draft/07_advanced-features.md renamed to packages/viola/templates/basic/drafts/07_advanced-features.md

File renamed without changes.

0 commit comments

Comments
 (0)