-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContinuousSavingPlugin.js
More file actions
109 lines (95 loc) · 3.67 KB
/
ContinuousSavingPlugin.js
File metadata and controls
109 lines (95 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/***
|Name |ContinuousSavingPlugin|
|Description |Makes loading and saving work via just one file picking per session|
|Source |https://github.com/YakovL/TiddlyWiki_ContinuousSavingPlugin/blob/main/ContinuousSavingPlugin.js|
|Author |Yakov Litvin|
|Version |0.4.0|
|Browsers |Up to date support can be checked [[here|https://caniuse.com/?search=showOpenFilePicker]], as of 02.2024 it's Chromium-based desktop browsers and Edge|
|~CoreVersion|2.10.0|
|Contact |Create an [[issue|https://github.com/YakovL/TiddlyWiki_ContinuousSavingPlugin/issues]] or start a new thread in the [[Google Group|https://groups.google.com/g/tiddlywikiclassic/]]|
|License |[[MIT|https://github.com/YakovL/TiddlyWiki_ContinuousSavingPlugin/blob/master/LICENSE]]|
***/
//{{{
config.extensions.fileIO = !window.showOpenFilePicker ? null : {
pickerOptions: { types: [
// specifying types here, like { accept: { "text/plain": [".txt"] }, description: "Text file" },
// forces user to select the type of picking; empty list makes *.* default, so no extra clicks are required
] },
// map of { id: { load, save } }
fileHandles: {},
load: async function(id) {
const result = { id, ok: false }
let existingHandle = this.fileHandles[id]
if(!existingHandle || !existingHandle.load) {
const handles = await window.showOpenFilePicker(this.pickerOptions)
if(handles[0]) {
if(!existingHandle) this.fileHandles[id] = {}
this.fileHandles[id].load = handles[0]
} else {
result.reason = 'cancelled by user'
return result
}
}
existingHandle = this.fileHandles[id]
if(!existingHandle || !existingHandle.load) {
result.reason = `after picking, fileHandles[id]${existingHandle ? '.load' : ''} is still ${
existingHandle ? existingHandle.load : existingHandle} (id is ${id})`
return result
}
const file = await existingHandle.load.getFile()
result.content = await file.text()
result.ok = true
return result
},
save: async function(id, content) {
const result = { id, ok: false }
if(!this.fileHandles[id]) {
// load handler is preferrable, but won't work for creating a file
const handle = await window.showSaveFilePicker(this.pickerOptions)
if(handle) {
this.fileHandles[id] = { save: handle }
} else {
result.reason = 'cancelled by user'
return result
}
}
if(!this.fileHandles[id]) {
result.reason = `after picking, fileHandles[id] is still ${
this.fileHandles[id]} (id is ${id})`
return result
}
const handle = this.fileHandles[id].load || this.fileHandles[id].save
const writable = await handle.createWritable()
if(!writable) {
result.reason = `writable (for id ${id}) is ${writable}`
return result
}
await writable.write(content)
await writable.close()
result.ok = true
result.content = content
return result
}
}
config.options.chkPreventAsyncSaving = false
if(window.tw && tw.io && tw.io.loadFile && !tw.io.orig_nonContinuous_loadFile) {
tw.io.orig_nonContinuous_loadFile = tw.io.loadFile
tw.io.loadFile = function(fileUrl, callback) {
const newCallback = function(result, details) {
if(result) callback(result, details)
else config.extensions.fileIO.load(fileUrl)
.then(({ content }) => callback(content))
}
return tw.io.orig_nonContinuous_loadFile(fileUrl, newCallback)
}
tw.io.orig_nonContinuous_asyncSaveFile = tw.io.asyncSaveFile
tw.io.asyncSaveFile = function(fileUrl, content, callback) {
const newCallback = function(result, details) {
if(result) callback(result, details)
else config.extensions.fileIO.save(fileUrl, content)
.then(result => callback(result.ok, result))
}
return tw.io.orig_nonContinuous_asyncSaveFile(fileUrl, content, newCallback)
}
}
//}}}