Skip to content

Commit dc312ad

Browse files
committed
rework rename hook, fixes #129
1 parent 515c858 commit dc312ad

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

src/model/store-vault-sync.ts

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
setDraftOnFrontmatterObject,
1717
} from "src/model/draft-utils";
1818
import { fileNameFromPath } from "./note-utils";
19-
import { findScene } from "./scene-navigation";
19+
import { findScene, sceneFolderPath } from "./scene-navigation";
2020

2121
type FileWithMetadata = {
2222
file: TFile;
@@ -233,39 +233,64 @@ export class StoreVaultSync {
233233
} else {
234234
// scene renamed
235235
const newTitle = fileNameFromPath(file.path);
236-
const found = findScene(oldPath, drafts);
237-
if (found) {
236+
const foundOld = findScene(oldPath, drafts);
237+
238+
// possibilities here:
239+
// 1. note was renamed in-place: rename the scene in the associated draft
240+
// 2. note was moved out of a draft: remove it from the old draft
241+
// 3. note was moved into a draft: add it to the new draft
242+
// (2) and (3) can occur for the same note.
243+
244+
// in-place
245+
const oldParent = oldPath.split("/").slice(0, -1).join("/");
246+
if (foundOld && oldParent === file.parent.path) {
238247
draftsStore.update((_drafts) => {
239248
return _drafts.map((d) => {
240249
if (
241-
d.vaultPath === found.draft.vaultPath &&
250+
d.vaultPath === foundOld.draft.vaultPath &&
242251
d.format === "scenes"
243252
) {
244-
d.scenes[found.index].title = newTitle;
253+
d.scenes[foundOld.index].title = newTitle;
245254
}
246255
return d;
247256
});
248257
});
249258
} else {
250-
// check if a new scene has been moved into this folder
251-
const scenePath = file.parent.path;
252-
const memberOfDraft = drafts.find((d) => {
253-
if (d.format !== "scenes") {
254-
return false;
255-
}
256-
const parentPath = this.vault.getAbstractFileByPath(d.vaultPath)
257-
.parent.path;
258-
const targetPath = normalizePath(`${parentPath}/${d.sceneFolder}`);
259-
return targetPath === scenePath;
259+
//in and/or out
260+
261+
// moved out of a draft
262+
const oldDraft = drafts.find((d) => {
263+
return (
264+
d.format === "scenes" &&
265+
sceneFolderPath(d, this.vault) === oldParent
266+
);
260267
});
261-
if (memberOfDraft) {
262-
draftsStore.update((allDrafts) => {
263-
return allDrafts.map((d) => {
264-
if (
265-
d.vaultPath === memberOfDraft.vaultPath &&
266-
d.format === "scenes"
267-
) {
268-
d.unknownFiles.push(newTitle);
268+
if (oldDraft) {
269+
draftsStore.update((_drafts) => {
270+
return _drafts.map((d) => {
271+
if (d.vaultPath === oldDraft.vaultPath && d.format === "scenes") {
272+
d.scenes = d.scenes.filter((s) => s.title !== file.basename);
273+
d.unknownFiles = d.unknownFiles.filter(
274+
(f) => f !== file.basename
275+
);
276+
}
277+
return d;
278+
});
279+
});
280+
}
281+
282+
// moved into a draft
283+
const newDraft = drafts.find((d) => {
284+
return (
285+
d.format === "scenes" &&
286+
sceneFolderPath(d, this.vault) === file.parent.path
287+
);
288+
});
289+
if (newDraft) {
290+
draftsStore.update((_drafts) => {
291+
return _drafts.map((d) => {
292+
if (d.vaultPath === newDraft.vaultPath && d.format === "scenes") {
293+
d.unknownFiles.push(file.basename);
269294
}
270295
return d;
271296
});

0 commit comments

Comments
 (0)