Skip to content

Commit

Permalink
fix: remove single-vault note references
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinslin committed Dec 3, 2020
1 parent 48a9d74 commit fd5a381
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 38 deletions.
17 changes: 11 additions & 6 deletions packages/plugin-core/src/WorkspaceWatcher.ts
@@ -1,4 +1,4 @@
import { NotePropsV2, NoteUtilsV2 } from "@dendronhq/common-all";
import { DNodeUtilsV2, NotePropsV2, NoteUtilsV2 } from "@dendronhq/common-all";
import { HistoryService } from "@dendronhq/engine-server";
import _ from "lodash";
import moment from "moment";
Expand Down Expand Up @@ -45,12 +45,20 @@ export class WorkspaceWatcher {
const eclient = DendronWorkspace.instance().getEngine();
const fname = path.basename(uri.fsPath, ".md");
const now = moment.now();
const vault = DNodeUtilsV2.getVaultByDir({
dirPath: path.dirname(uri.fsPath),
vaults: eclient.vaultsv3,
});
const note = NoteUtilsV2.getNoteByFnameV4({
fname,
vault,
notes: eclient.notes,
}) as NotePropsV2;

// if recently changed, ignore
const recentEvents = HistoryService.instance().lookBack();
if (recentEvents[0].uri?.fsPath === uri.fsPath) {
let lastUpdated: string | number =
NoteUtilsV2.getNoteByFname(fname, eclient.notes)?.updated || now;
let lastUpdated: string | number = note?.updated || now;
if (_.isString(lastUpdated)) {
lastUpdated = _.parseInt(lastUpdated);
}
Expand All @@ -71,9 +79,6 @@ export class WorkspaceWatcher {
const startPos = ev.document.positionAt(match.index);
const endPos = ev.document.positionAt(match.index + match[0].length);
const p = new Promise(async (resolve) => {
const note = NoteUtilsV2.getNoteByFname(fname, eclient.notes, {
throwIfEmpty: true,
}) as NotePropsV2;
note.updated = now.toString();
await eclient.updateNote(note);
resolve([
Expand Down
12 changes: 8 additions & 4 deletions packages/plugin-core/src/commands/CopyNoteLink.ts
Expand Up @@ -2,10 +2,11 @@ import clipboardy from "@dendronhq/clipboardy";
import { NotePropsV2, NoteUtilsV2 } from "@dendronhq/common-all";
import _ from "lodash";
import { TextEditor, window } from "vscode";
import { PickerUtilsV2 } from "../components/lookup/utils";
import { DENDRON_COMMANDS } from "../constants";
import { VSCodeUtils } from "../utils";
import { getHeaderFromSelection } from "../utils/editor";
import { DendronWorkspace } from "../workspace";
import { getEngine } from "../workspace";
import { BasicCommand } from "./base";

type CommandOpts = {};
Expand All @@ -31,10 +32,13 @@ export class CopyNoteLinkCommand extends BasicCommand<
const editor = VSCodeUtils.getActiveTextEditor() as TextEditor;
const fname = NoteUtilsV2.uri2Fname(editor.document.uri);
let note: NotePropsV2;
note = NoteUtilsV2.getNoteByFname(

const vault = PickerUtilsV2.getOrPromptVaultForOpenEditor();
note = NoteUtilsV2.getNoteByFnameV4({
fname,
DendronWorkspace.instance().getEngine().notes
) as NotePropsV2;
vault,
notes: getEngine().notes,
}) as NotePropsV2;
if (!note) {
throw Error(`${fname} not found in engine`);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-core/src/commands/DeleteNodeCommand.ts
Expand Up @@ -5,7 +5,7 @@ import { window } from "vscode";
import { PickerUtilsV2 } from "../components/lookup/utils";
import { DENDRON_COMMANDS } from "../constants";
import { DendronClientUtilsV2, VSCodeUtils } from "../utils";
import { DendronWorkspace } from "../workspace";
import { DendronWorkspace, getEngine } from "../workspace";
import { BasicCommand } from "./base";

type CommandOpts = {};
Expand All @@ -32,11 +32,12 @@ export class DeleteNodeCommand extends BasicCommand<
const trimEnd = mode === "note" ? ".md" : ".schema.yml";
const fname = path.basename(fsPath, trimEnd);
const client = DendronWorkspace.instance().getEngine();
const vault = PickerUtilsV2.getVaultForOpenEditor();
if (mode === "note") {
const note = NoteUtilsV2.getNoteByFname(fname, client.notes, {
throwIfEmpty: true,
const vault = PickerUtilsV2.getOrPromptVaultForOpenEditor();
const note = NoteUtilsV2.getNoteByFnameV4({
fname,
vault,
notes: getEngine().notes,
}) as NotePropsV2;
return (await client.deleteNote(note.id)) as EngineDeletePayload;
} else {
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-core/src/commands/GoToSiblingCommand.ts
Expand Up @@ -47,9 +47,11 @@ export class GoToSiblingCommand extends BasicCommand<
vaults: client.vaultsv3 as DVault[],
dirPath: path.dirname(maybeTextEditor.document.uri.fsPath),
});
const rootNode = NoteUtilsV2.getNoteByFname(value, client.notes, {
const rootNode = NoteUtilsV2.getNoteByFnameV4({
fname: value,
vault,
});
notes: client.notes,
}) as NotePropsV2;
if (_.isUndefined(rootNode)) {
throw new DendronError({ msg: "no root node found" });
}
Expand Down
31 changes: 19 additions & 12 deletions packages/plugin-core/src/components/lookup/LookupProviderV2.ts
Expand Up @@ -17,7 +17,7 @@ import { CancellationToken, Uri, window } from "vscode";
import { Logger } from "../../logger";
import { EngineFlavor, EngineOpts } from "../../types";
import { getDurationMilliseconds, profile } from "../../utils/system";
import { DendronWorkspace, getWS } from "../../workspace";
import { DendronWorkspace, getEngine, getWS } from "../../workspace";
import { MORE_RESULTS_LABEL } from "./constants";
import { LookupControllerV2 } from "./LookupControllerV2";
import { DendronQuickPickerV2 } from "./types";
Expand Down Expand Up @@ -123,7 +123,12 @@ export class LookupProviderV2 {

Logger.info({ ctx, msg: "pre:checkNoteExist", uri });
// TODO: check for overwriting schema
let noteExists = NoteUtilsV2.getNoteByFname(nodeNew.fname, engine.notes);
const vault = PickerUtilsV2.getOrPromptVaultForOpenEditor();
const noteExists = NoteUtilsV2.getNoteByFnameV4({
fname: nodeNew.fname,
vault,
notes: engine.notes,
}) as NotePropsV2;
if (
noteExists &&
!foundStub &&
Expand Down Expand Up @@ -222,7 +227,6 @@ export class LookupProviderV2 {
activeItems: picker.activeItems.map((ent) => NoteUtilsV2.toLogObj(ent)),
});
const resp = this.validate(picker.value, opts.flavor);
const ws = DendronWorkspace.instance();
let uri: Uri;
let newNode: NotePropsV2 | SchemaModulePropsV2 | undefined;
if (resp) {
Expand Down Expand Up @@ -259,10 +263,12 @@ export class LookupProviderV2 {
// item from pressing enter
if (opts.flavor === "note") {
try {
const maybeNote = NoteUtilsV2.getNoteByFname(
value,
ws.getEngine().notes
);
const vault = PickerUtilsV2.getOrPromptVaultForOpenEditor();
const maybeNote = NoteUtilsV2.getNoteByFnameV4({
fname: value,
vault,
notes: getEngine().notes,
});
if (maybeNote) {
uri = node2Uri(maybeNote);
await showDocAndHidePicker([uri], picker);
Expand All @@ -286,7 +292,6 @@ export class LookupProviderV2 {
Logger.info({ ctx, msg: "enter", value, opts });
let selectedItems = PickerUtilsV2.getSelection(picker);
const resp = this.validate(picker.value, opts.flavor);
const ws = DendronWorkspace.instance();
let uris: Uri[];
if (resp) {
window.showErrorMessage(resp);
Expand All @@ -296,10 +301,12 @@ export class LookupProviderV2 {
// check if we get note by quickpick value instead of selection
const activeItem = picker.activeItems[0];
const maybeNoteFname = activeItem ? activeItem.fname : value;
const maybeNote = NoteUtilsV2.getNoteByFname(
maybeNoteFname,
ws.getEngine().notes
);
const vault = PickerUtilsV2.getOrPromptVaultForOpenEditor();
const maybeNote = NoteUtilsV2.getNoteByFnameV4({
fname: maybeNoteFname,
vault,
notes: getEngine().notes,
}) as NotePropsV2;
if (_.isEmpty(selectedItems) && opts.flavor === "note") {
if (maybeNote) {
if (maybeNote.stub) {
Expand Down
20 changes: 14 additions & 6 deletions packages/plugin-core/src/fileWatcher.ts
Expand Up @@ -66,10 +66,11 @@ export class VaultWatcher {
dirPath: path.dirname(uri.fsPath),
});
let note = string2Note({ content, fname, vault });
const noteHydrated = NoteUtilsV2.getNoteByFname(
const noteHydrated = NoteUtilsV2.getNoteByFnameV4({
fname,
eclient.notes
) as NotePropsV2;
vault,
notes: eclient.notes,
}) as NotePropsV2;
note = NoteUtilsV2.hydrate({ noteRaw: note, noteHydrated });
const links = ParserUtilsV2.findLinks({ note });
note.links = links;
Expand Down Expand Up @@ -106,9 +107,16 @@ export class VaultWatcher {

try {
this.L.debug({ ctx, uri, msg: "pre-add-to-engine" });
// TODO: MULTI_VAULT
note = file2Note(uri.fsPath, { fsPath: path.dirname(uri.fsPath) });
const maybeNote = NoteUtilsV2.getNoteByFname(fname, this.engine.notes);
const vault = DNodeUtilsV2.getVaultByDir({
dirPath: path.dirname(uri.fsPath),
vaults: this.engine.vaultsv3,
});
note = file2Note(uri.fsPath, vault);
const maybeNote = NoteUtilsV2.getNoteByFnameV4({
fname,
vault,
notes: this.engine.notes,
}) as NotePropsV2;
if (maybeNote) {
note = {
...note,
Expand Down
Expand Up @@ -117,7 +117,7 @@ suite("Scratch Notes", function () {
});
});

test.only("domainAsNamespace", function (done) {
test("domainAsNamespace", function (done) {
runLegacyMultiWorkspaceTest({
ctx,
configOverride: {
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-core/src/views/DendronTreeViewV2.ts
Expand Up @@ -19,7 +19,7 @@ import { VaultUtils } from "@dendronhq/common-server";
import { GotoNoteCommandOpts } from "../commands/GotoNote";
import { DENDRON_COMMANDS, ICONS } from "../constants";
import { Logger } from "../logger";
import { DendronWorkspace } from "../workspace";
import { DendronWorkspace, getEngine } from "../workspace";
import { HistoryEvent, HistoryService } from "@dendronhq/engine-server";

function createTreeNote(note: NotePropsV2) {
Expand Down Expand Up @@ -193,8 +193,11 @@ export class DendronTreeViewV2 {
vaults: DendronWorkspace.instance().vaults,
});
const fname = NoteUtilsV2.uri2Fname(uri);
const notes = DendronWorkspace.instance().getEngine().notes;
const note = NoteUtilsV2.getNoteByFname(fname, notes, { vault });
const note = NoteUtilsV2.getNoteByFnameV4({
fname,
vault,
notes: getEngine().notes,
}) as NotePropsV2;
if (note) {
this.treeView.reveal(note.id);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-core/src/workspace.ts
Expand Up @@ -86,6 +86,10 @@ export function getWS() {
return DendronWorkspace.instance();
}

export function getEngine() {
return DendronWorkspace.instance().getEngine();
}

export function resolveRelToWSRoot(fpath: string): string {
return resolvePath(fpath, DendronWorkspace.wsRoot() as string);
}
Expand Down

0 comments on commit fd5a381

Please sign in to comment.