@@ -16,7 +16,7 @@ import {
16
16
setDraftOnFrontmatterObject ,
17
17
} from "src/model/draft-utils" ;
18
18
import { fileNameFromPath } from "./note-utils" ;
19
- import { findScene } from "./scene-navigation" ;
19
+ import { findScene , sceneFolderPath } from "./scene-navigation" ;
20
20
21
21
type FileWithMetadata = {
22
22
file : TFile ;
@@ -233,39 +233,64 @@ export class StoreVaultSync {
233
233
} else {
234
234
// scene renamed
235
235
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 ) {
238
247
draftsStore . update ( ( _drafts ) => {
239
248
return _drafts . map ( ( d ) => {
240
249
if (
241
- d . vaultPath === found . draft . vaultPath &&
250
+ d . vaultPath === foundOld . draft . vaultPath &&
242
251
d . format === "scenes"
243
252
) {
244
- d . scenes [ found . index ] . title = newTitle ;
253
+ d . scenes [ foundOld . index ] . title = newTitle ;
245
254
}
246
255
return d ;
247
256
} ) ;
248
257
} ) ;
249
258
} 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
+ ) ;
260
267
} ) ;
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 ) ;
269
294
}
270
295
return d ;
271
296
} ) ;
0 commit comments