Skip to content

Commit

Permalink
feat(api): add writeTextFile and (path, contents, options) overlo…
Browse files Browse the repository at this point in the history
…ad (#4228)
  • Loading branch information
lucasfernog committed May 29, 2022
1 parent f685df3 commit 3f998ca
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 106 deletions.
5 changes: 5 additions & 0 deletions .changes/write-file-rename.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Renamed `writeFile` to `writeTextFile` but kept the original function for backwards compatibility.
5 changes: 5 additions & 0 deletions .changes/write-file-variants.md
@@ -0,0 +1,5 @@
---
"api": patch
---

Added `(path, contents[, options])` overload to the `writeTextFile` and `writeBinaryFile` APIs.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

21 changes: 4 additions & 17 deletions examples/api/dist/assets/vendor.js

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 43 additions & 44 deletions examples/api/src/components/FileSystem.svelte
@@ -1,78 +1,77 @@
<script>
import { readBinaryFile, readDir, Dir } from "@tauri-apps/api/fs";
import { convertFileSrc } from "@tauri-apps/api/tauri";
import {
readBinaryFile,
writeTextFile,
readDir,
Dir
} from '@tauri-apps/api/fs'
import { convertFileSrc } from '@tauri-apps/api/tauri'
export let onMessage;
export let insecureRenderHtml;
export let onMessage
export let insecureRenderHtml
let pathToRead = "";
let img;
let pathToRead = ''
let img
function getDir() {
const dirSelect = document.getElementById("dir");
return dirSelect.value ? parseInt(dir.value) : null;
const dirSelect = document.getElementById('dir')
return dirSelect.value ? parseInt(dir.value) : null
}
function arrayBufferToBase64(buffer, callback) {
const blob = new Blob([buffer], {
type: "application/octet-binary",
});
const reader = new FileReader();
type: 'application/octet-binary'
})
const reader = new FileReader()
reader.onload = function (evt) {
const dataurl = evt.target.result;
callback(dataurl.substr(dataurl.indexOf(",") + 1));
};
reader.readAsDataURL(blob);
const dataurl = evt.target.result
callback(dataurl.substr(dataurl.indexOf(',') + 1))
}
reader.readAsDataURL(blob)
}
const DirOptions = Object.keys(Dir)
.filter((key) => isNaN(parseInt(key)))
.map((dir) => [dir, Dir[dir]]);
.map((dir) => [dir, Dir[dir]])
function read() {
const isFile = pathToRead.match(/\S+\.\S+$/g);
const isFile = pathToRead.match(/\S+\.\S+$/g)
const opts = {
dir: getDir(),
};
dir: getDir()
}
const promise = isFile
? readBinaryFile(pathToRead, opts)
: readDir(pathToRead, opts);
: readDir(pathToRead, opts)
promise
.then(function (response) {
if (isFile) {
if (pathToRead.includes(".png") || pathToRead.includes(".jpg")) {
if (pathToRead.includes('.png') || pathToRead.includes('.jpg')) {
arrayBufferToBase64(new Uint8Array(response), function (base64) {
const src = "data:image/png;base64," + base64;
insecureRenderHtml('<img src="' + src + '"></img>');
});
const src = 'data:image/png;base64,' + base64
insecureRenderHtml('<img src="' + src + '"></img>')
})
} else {
const value = String.fromCharCode.apply(null, response);
const value = String.fromCharCode.apply(null, response)
insecureRenderHtml(
'<textarea id="file-response"></textarea><button id="file-save">Save</button>'
);
)
setTimeout(() => {
const fileInput = document.getElementById("file-response");
fileInput.value = value;
const fileInput = document.getElementById('file-response')
fileInput.value = value
document
.getElementById("file-save")
.addEventListener("click", function () {
writeFile(
{
file: pathToRead,
contents: fileInput.value,
},
{
dir: getDir(),
}
).catch(onMessage);
});
});
.getElementById('file-save')
.addEventListener('click', function () {
writeTextFile(pathToRead, fileInput.value, {
dir: getDir()
}).catch(onMessage)
})
})
}
} else {
onMessage(response);
onMessage(response)
}
})
.catch(onMessage);
.catch(onMessage)
}
function setSrc() {
Expand All @@ -95,5 +94,5 @@
<button class="button" id="read">Read</button>
<button class="button" type="button" on:click={setSrc}>Use as img src</button>

<img alt="file" bind:this={img}>
<img alt="file" bind:this={img} />
</form>
12 changes: 6 additions & 6 deletions examples/api/yarn.lock
Expand Up @@ -23,9 +23,9 @@
svelte-hmr "^0.14.7"

"@tauri-apps/api@../../tooling/api/dist":
version "1.0.0-rc.3"
version "1.0.0-rc.6"
dependencies:
type-fest "2.12.2"
type-fest "2.13.0"

"@zerodevx/svelte-json-view@0.2.0":
version "0.2.0"
Expand Down Expand Up @@ -267,10 +267,10 @@ svelte@3.35.0:
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.35.0.tgz#e0d0ba60c4852181c2b4fd851194be6fda493e65"
integrity sha512-gknlZkR2sXheu/X+B7dDImwANVvK1R0QGQLd8CNIfxxGPeXBmePnxfzb6fWwTQRsYQG7lYkZXvpXJvxvpsoB7g==

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

vite@^2.6.4:
version "2.6.14"
Expand Down
7 changes: 6 additions & 1 deletion tooling/api/src/dialog.ts
Expand Up @@ -207,6 +207,11 @@ async function confirm(
})
}

export type { DialogFilter, OpenDialogOptions, SaveDialogOptions }
export type {
DialogFilter,
OpenDialogOptions,
SaveDialogOptions,
MessageDialogOptions
}

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

0 comments on commit 3f998ca

Please sign in to comment.