Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(structure): support xvault template in note traits #3329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/plugin-core/src/commands/CreateNoteWithTraitCommand.ts
Expand Up @@ -7,6 +7,8 @@ import {
EngagementEvents,
NoteUtils,
SetNameModifierResp,
parseDendronURI,
VaultUtils,
} from "@dendronhq/common-all";
import { HistoryEvent } from "@dendronhq/engine-server";
import path from "path";
Expand Down Expand Up @@ -198,10 +200,27 @@ export class CreateNoteWithTraitCommand extends BaseCommand<
} catch (Error: any) {
this.L.error({ ctx: "trait.OnCreate.setTemplate", msg: Error });
}
let maybeVault: DVault | undefined;
// for cross vault template
const { link: fname, vaultName } = parseDendronURI(templateNoteName);
if (!_.isUndefined(vaultName)) {
maybeVault = VaultUtils.getVaultByName({
vname: vaultName,
vaults: ExtensionProvider.getEngine().vaults,
});
// If vault is not found, skip lookup through rest of notes and return error
if (_.isUndefined(maybeVault)) {
this.L.error({
ctx: "trait.OnCreate.setTemplate",
msg: `No vault found for ${vaultName}`,
});
return;
}
}

const notes = await ExtensionProvider.getEngine().findNotes({
fname: templateNoteName,
vault,
fname,
vault: maybeVault,
});

const dummy = NoteUtils.createForFake({
Expand Down
Expand Up @@ -15,6 +15,7 @@ suite("CreateNoteWithTraitCommand tests", () => {
"GIVEN a Note Trait",
{
preSetupHook: ENGINE_HOOKS.setupBasic,
timeout: 1e4,
},
(ctx) => {
describe(`WHEN creating a note with that trait applied`, () => {
Expand All @@ -28,7 +29,7 @@ suite("CreateNoteWithTraitCommand tests", () => {

test(`THEN expect the title to have been modified AND have the foo template applied`, async () => {
const { engine, wsRoot, vaults } = ExtensionProvider.getDWorkspace();
const testTrait = new TestTrait();
const testTrait = new TestTrait("foo");

const mockExtension = new MockDendronExtension({
engine,
Expand Down Expand Up @@ -65,6 +66,44 @@ suite("CreateNoteWithTraitCommand tests", () => {
expect(props?.title).toEqual(testTrait.TEST_TITLE_MODIFIER);
expect(props?.body).toEqual("foo body");
});
test(`WHEN cross vault template is given, THEN correct template should be applied`, async () => {
const { engine, wsRoot, vaults } = ExtensionProvider.getDWorkspace();
const testTrait = new TestTrait("dendron://vault1/bar");
const mockExtension = new MockDendronExtension({
engine,
wsRoot,
context: ctx,
vaults,
});

const cmd = new CreateNoteWithTraitCommand(
mockExtension,
"test-create-note-with-trait",
testTrait
);

await cmd.execute({ fname: "xvault", vaultOverride: vaults[1] });

const expectedFName = path.join(
wsRoot,
VaultUtils.getRelPath(vaults[1]),
"xvault.md"
);

expect(
VSCodeUtils.getActiveTextEditor()?.document.uri.fsPath
).toEqual(expectedFName);

const props = (
await engine.findNotes({
fname: "xvault",
vault: vaults[1],
})
)[0];

expect(props?.title).toEqual(testTrait.TEST_TITLE_MODIFIER);
expect(props?.body).toEqual("bar body");
});
});
}
);
Expand Down
Expand Up @@ -10,6 +10,10 @@ import {
export class TestTrait implements NoteTrait {
TEST_NAME_MODIFIER = "Test Name Modifier";
TEST_TITLE_MODIFIER = "Test Title Modifier";
public template: string;
constructor(template: string) {
this.template = template;
}

id: string = "test-trait";
OnWillCreate: onWillCreateProps = {
Expand All @@ -25,7 +29,7 @@ export class TestTrait implements NoteTrait {
return this.TEST_TITLE_MODIFIER;
},
setTemplate: () => {
return "foo";
return this.template;
},
};
}