Skip to content

Commit 3f998ca

Browse files
authored
feat(api): add writeTextFile and (path, contents, options) overload (#4228)
1 parent f685df3 commit 3f998ca

12 files changed

Lines changed: 192 additions & 106 deletions

File tree

.changes/write-file-rename.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Renamed `writeFile` to `writeTextFile` but kept the original function for backwards compatibility.

.changes/write-file-variants.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Added `(path, contents[, options])` overload to the `writeTextFile` and `writeBinaryFile` APIs.

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/dist/assets/index.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/dist/assets/vendor.js

Lines changed: 4 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/Cargo.lock

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
11
<script>
2-
import { readBinaryFile, readDir, Dir } from "@tauri-apps/api/fs";
3-
import { convertFileSrc } from "@tauri-apps/api/tauri";
2+
import {
3+
readBinaryFile,
4+
writeTextFile,
5+
readDir,
6+
Dir
7+
} from '@tauri-apps/api/fs'
8+
import { convertFileSrc } from '@tauri-apps/api/tauri'
49
5-
export let onMessage;
6-
export let insecureRenderHtml;
10+
export let onMessage
11+
export let insecureRenderHtml
712
8-
let pathToRead = "";
9-
let img;
13+
let pathToRead = ''
14+
let img
1015
1116
function getDir() {
12-
const dirSelect = document.getElementById("dir");
13-
return dirSelect.value ? parseInt(dir.value) : null;
17+
const dirSelect = document.getElementById('dir')
18+
return dirSelect.value ? parseInt(dir.value) : null
1419
}
1520
1621
function arrayBufferToBase64(buffer, callback) {
1722
const blob = new Blob([buffer], {
18-
type: "application/octet-binary",
19-
});
20-
const reader = new FileReader();
23+
type: 'application/octet-binary'
24+
})
25+
const reader = new FileReader()
2126
reader.onload = function (evt) {
22-
const dataurl = evt.target.result;
23-
callback(dataurl.substr(dataurl.indexOf(",") + 1));
24-
};
25-
reader.readAsDataURL(blob);
27+
const dataurl = evt.target.result
28+
callback(dataurl.substr(dataurl.indexOf(',') + 1))
29+
}
30+
reader.readAsDataURL(blob)
2631
}
2732
2833
const DirOptions = Object.keys(Dir)
2934
.filter((key) => isNaN(parseInt(key)))
30-
.map((dir) => [dir, Dir[dir]]);
35+
.map((dir) => [dir, Dir[dir]])
3136
3237
function read() {
33-
const isFile = pathToRead.match(/\S+\.\S+$/g);
38+
const isFile = pathToRead.match(/\S+\.\S+$/g)
3439
const opts = {
35-
dir: getDir(),
36-
};
40+
dir: getDir()
41+
}
3742
const promise = isFile
3843
? readBinaryFile(pathToRead, opts)
39-
: readDir(pathToRead, opts);
44+
: readDir(pathToRead, opts)
4045
promise
4146
.then(function (response) {
4247
if (isFile) {
43-
if (pathToRead.includes(".png") || pathToRead.includes(".jpg")) {
48+
if (pathToRead.includes('.png') || pathToRead.includes('.jpg')) {
4449
arrayBufferToBase64(new Uint8Array(response), function (base64) {
45-
const src = "data:image/png;base64," + base64;
46-
insecureRenderHtml('<img src="' + src + '"></img>');
47-
});
50+
const src = 'data:image/png;base64,' + base64
51+
insecureRenderHtml('<img src="' + src + '"></img>')
52+
})
4853
} else {
49-
const value = String.fromCharCode.apply(null, response);
54+
const value = String.fromCharCode.apply(null, response)
5055
insecureRenderHtml(
5156
'<textarea id="file-response"></textarea><button id="file-save">Save</button>'
52-
);
57+
)
5358
setTimeout(() => {
54-
const fileInput = document.getElementById("file-response");
55-
fileInput.value = value;
59+
const fileInput = document.getElementById('file-response')
60+
fileInput.value = value
5661
document
57-
.getElementById("file-save")
58-
.addEventListener("click", function () {
59-
writeFile(
60-
{
61-
file: pathToRead,
62-
contents: fileInput.value,
63-
},
64-
{
65-
dir: getDir(),
66-
}
67-
).catch(onMessage);
68-
});
69-
});
62+
.getElementById('file-save')
63+
.addEventListener('click', function () {
64+
writeTextFile(pathToRead, fileInput.value, {
65+
dir: getDir()
66+
}).catch(onMessage)
67+
})
68+
})
7069
}
7170
} else {
72-
onMessage(response);
71+
onMessage(response)
7372
}
7473
})
75-
.catch(onMessage);
74+
.catch(onMessage)
7675
}
7776
7877
function setSrc() {
@@ -95,5 +94,5 @@
9594
<button class="button" id="read">Read</button>
9695
<button class="button" type="button" on:click={setSrc}>Use as img src</button>
9796

98-
<img alt="file" bind:this={img}>
97+
<img alt="file" bind:this={img} />
9998
</form>

examples/api/yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
svelte-hmr "^0.14.7"
2424

2525
"@tauri-apps/api@../../tooling/api/dist":
26-
version "1.0.0-rc.3"
26+
version "1.0.0-rc.6"
2727
dependencies:
28-
type-fest "2.12.2"
28+
type-fest "2.13.0"
2929

3030
"@zerodevx/svelte-json-view@0.2.0":
3131
version "0.2.0"
@@ -267,10 +267,10 @@ svelte@3.35.0:
267267
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.35.0.tgz#e0d0ba60c4852181c2b4fd851194be6fda493e65"
268268
integrity sha512-gknlZkR2sXheu/X+B7dDImwANVvK1R0QGQLd8CNIfxxGPeXBmePnxfzb6fWwTQRsYQG7lYkZXvpXJvxvpsoB7g==
269269

270-
type-fest@2.12.2:
271-
version "2.12.2"
272-
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.12.2.tgz#80a53614e6b9b475eb9077472fb7498dc7aa51d0"
273-
integrity sha512-qt6ylCGpLjZ7AaODxbpyBZSs9fCI9SkL3Z9q2oxMBQhs/uyY+VD8jHA8ULCGmWQJlBgqvO3EJeAngOHD8zQCrQ==
270+
type-fest@2.13.0:
271+
version "2.13.0"
272+
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.13.0.tgz#d1ecee38af29eb2e863b22299a3d68ef30d2abfb"
273+
integrity sha512-lPfAm42MxE4/456+QyIaaVBAwgpJb6xZ8PRu09utnhPdWwcyj9vgy6Sq0Z5yNbJ21EdxB5dRU/Qg8bsyAMtlcw==
274274

275275
vite@^2.6.4:
276276
version "2.6.14"

tooling/api/src/dialog.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ async function confirm(
207207
})
208208
}
209209

210-
export type { DialogFilter, OpenDialogOptions, SaveDialogOptions }
210+
export type {
211+
DialogFilter,
212+
OpenDialogOptions,
213+
SaveDialogOptions,
214+
MessageDialogOptions
215+
}
211216

212217
export { open, save, message, ask, confirm }

0 commit comments

Comments
 (0)