Skip to content

Commit 67d50ef

Browse files
committed
feat: callback of th-deleting-tiddler
1 parent 5ac7699 commit 67d50ef

8 files changed

Lines changed: 78 additions & 12 deletions

File tree

dprint.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
],
1616
"excludes": ["**/node_modules", "**/*-lock.json"],
1717
"plugins": [
18-
"https://plugins.dprint.dev/typescript-0.93.3.wasm",
19-
"https://plugins.dprint.dev/json-0.19.4.wasm",
20-
"https://plugins.dprint.dev/markdown-0.17.8.wasm"
18+
"https://plugins.dprint.dev/typescript-0.95.0.wasm",
19+
"https://plugins.dprint.dev/json-0.20.0.wasm",
20+
"https://plugins.dprint.dev/markdown-0.18.0.wasm"
2121
]
2222
}

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@
3333
]
3434
},
3535
"devDependencies": {
36-
"@modern-js/tsconfig": "^2.64.0",
37-
"@types/node": "^22.12.0",
38-
"dprint": "^0.48.0",
39-
"eslint-config-tidgi": "1.2.3",
36+
"@modern-js/tsconfig": "^2.67.3",
37+
"@types/node": "^22.15.3",
38+
"dprint": "^0.49.1",
39+
"eslint-config-tidgi": "2.1.0",
4040
"husky": "^9.1.7",
41-
"lint-staged": "^15.4.3",
42-
"postcss": "^8.5.1",
41+
"lint-staged": "^15.5.1",
42+
"postcss": "^8.5.3",
4343
"rimraf": "^6.0.1",
4444
"ts-node": "^10.9.2",
45-
"tw5-typed": "^0.5.14",
46-
"typescript": "^5.7.3"
45+
"tw5-typed": "^0.6.2",
46+
"typescript": "^5.8.3"
4747
},
4848
"dependencies": {
49-
"npm-check-updates": "^17.1.14",
49+
"npm-check-updates": "^18.0.1",
5050
"tiddlywiki": "^5.3.6",
5151
"tiddlywiki-plugin-dev": "^0.3.2"
5252
}

src/tidgi-external-attachments/config.multids

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ MoveToWikiFolder: yes
66
WikiFolderToMove: files
77
UseAbsoluteForDescendents: no
88
UseAbsoluteForNonDescendents: yes
9+
MoveToTrash: yes
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
2+
import { Tiddler } from 'tiddlywiki';
3+
import { joinPaths } from './makePathRelative';
4+
5+
// Configuration tiddler title
6+
const MOVE_TO_TRASH_TITLE = '$:/config/ExternalAttachments/MoveToTrash';
7+
8+
/**
9+
* Handles the 'th-deleting-tiddler' hook which is triggered when a tiddler is being deleted.
10+
* If the tiddler references an external file via _canonical_uri, this function can move that file to trash.
11+
*
12+
* @param tiddler - The tiddler being deleted
13+
* @param wikiFolderLocation - The location of the current wiki folder
14+
* @returns The tiddler (unmodified)
15+
*/
16+
export function handleDeletingTiddler(tiddler: Tiddler, wikiFolderLocation: string): Tiddler {
17+
// Check if we should move external files to trash on tiddler deletion
18+
const moveToTrash = $tw.wiki.getTiddlerText(MOVE_TO_TRASH_TITLE, '') === 'yes';
19+
if (!moveToTrash) return tiddler;
20+
21+
// Check if the tiddler has a canonical URI (reference to external file)
22+
const canonicalUri = tiddler.fields._canonical_uri;
23+
if (!canonicalUri || typeof canonicalUri !== 'string') return tiddler;
24+
let filePath: string;
25+
// Handle relative vs absolute paths
26+
if (
27+
canonicalUri.startsWith('/') ||
28+
(canonicalUri.length > 1 && canonicalUri[1] === ':')
29+
) {
30+
// Absolute path
31+
filePath = canonicalUri;
32+
} else {
33+
// Relative path - resolve against wiki folder location
34+
filePath = joinPaths(wikiFolderLocation, canonicalUri);
35+
}
36+
37+
// Move the file to trash using TidGi's API
38+
try {
39+
void window.service?.native?.moveToTrash?.(filePath);
40+
41+
// Optional: Log operation to console
42+
console.log(`Moved file to trash: ${filePath}`);
43+
} catch (error) {
44+
console.error(`Failed to move file to trash: ${filePath}`, error);
45+
}
46+
47+
return tiddler;
48+
}

src/tidgi-external-attachments/settings-en-GB.tid

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ caption: ExtAttach
99

1010
<$checkbox tiddler="$:/config/ExternalAttachments/MoveToWikiFolder" field="text" checked="yes" unchecked="no" default="no"> <$link to="$:/config/ExternalAttachments/MoveToWikiFolder">Automatically move imported files to the Wiki folder</$link> </$checkbox>
1111

12+
<$checkbox tiddler="$:/config/ExternalAttachments/MoveToTrash" field="text" checked="yes" unchecked="no" default="no"> <$link to="$:/config/ExternalAttachments/MoveToTrash">Move associated external files to trash when deleting tiddlers</$link> </$checkbox>
13+
1214
<%if [{$:/config/ExternalAttachments/MoveToWikiFolder}match[yes]] %>
1315

1416
<$edit-text tiddler="$:/config/ExternalAttachments/WikiFolderToMove" tabindex=-1 focus=false cancelPopups="yes" fileDrop=no tag="input" /> Automatically move files to this folder inside the Wiki folder (suggested to keep using `files`).
@@ -30,5 +32,6 @@ caption: ExtAttach
3032
* Keep "Automatically move files to Wiki folder" enabled (default)
3133
* Retain the default storage directory as `files`
3234
* Always disable "Use absolute paths for files within Wiki"
35+
* Default to moving external files to trash when deleting tiddlers to simplify external file management, as TidGi's automatic git backups can restore files at any time.
3336

3437
Read more in the [[plugin readme|$:/plugins/linonetwo/tidgi-external-attachments/readme]].

src/tidgi-external-attachments/settings-zh-Hans.tid

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ caption: 外部附件
99

1010
<$checkbox tiddler="$:/config/ExternalAttachments/MoveToWikiFolder" field="text" checked="yes" unchecked="no" default="no"> <$link to="$:/config/ExternalAttachments/MoveToWikiFolder">自动将拖入的文件移动到 Wiki 文件夹内</$link> </$checkbox>
1111

12+
<$checkbox tiddler="$:/config/ExternalAttachments/MoveToTrash" field="text" checked="yes" unchecked="no" default="yes"> <$link to="$:/config/ExternalAttachments/MoveToTrash">删除条目时将关联的外部文件移动到回收站</$link> </$checkbox>
13+
1214
<%if [{$:/config/ExternalAttachments/MoveToWikiFolder}match[yes]] %>
1315

1416
<$edit-text tiddler="$:/config/ExternalAttachments/WikiFolderToMove" tabindex=-1 focus=false cancelPopups="yes" fileDrop=no tag="input" /> 自动移动文件到 Wiki 文件夹里的这个文件夹里(建议保持用 `files`)。
@@ -30,5 +32,6 @@ caption: 外部附件
3032
* 保持「自动移动文件到 Wiki 文件夹内」开启(默认启用)
3133
* 保留默认存储目录为 `files`
3234
* 始终关闭「为 Wiki 内文件使用绝对路径」
35+
* 默认开启「删除条目时移动关联外部文件到回收站」功能,以简化外部应用管理,反正太记有自动git备份可以随时回档
3336

3437
阅读[[插件说明|$:/plugins/linonetwo/tidgi-external-attachments/readme]]了解更多信息。

src/tidgi-external-attachments/startup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
44

55
import { handleBeforeImporting } from './handleBeforeImporting';
6+
import { handleDeletingTiddler } from './handleDeletingTiddler';
67
import { handleImportingFile } from './handleImportingFile';
78
import { makePathRelative } from './makePathRelative';
89

@@ -51,5 +52,9 @@ exports.startup = function() {
5152
$tw.hooks.addHook('th-before-importing', function(info) {
5253
return handleBeforeImporting(info);
5354
});
55+
56+
$tw.hooks.addHook("th-deleting-tiddler", function(tiddler) {
57+
return handleDeletingTiddler(tiddler, wikiFolderLocation);
58+
});
5459
});
5560
};

src/tidgi-external-attachments/type.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ declare global {
1818
toFilePath: string,
1919
options?: { fileToDir?: boolean },
2020
): Promise<false | string>;
21+
/**
22+
* Move a file or directory to the trash bin.
23+
* @param filePath The absolute path of the file or directory to move to the trash.
24+
* @returns A promise that resolves to true if the operation was successful, false otherwise.
25+
*/
26+
moveToTrash(filePath: string): Promise<boolean>;
2127
path(
2228
method: 'basename' | 'dirname' | 'join',
2329
pathString: string | undefined,

0 commit comments

Comments
 (0)