(options: UseFileSystemAccessOptions = {})
| 99 | export function useFileSystemAccess(options: UseFileSystemAccessOptions & { dataType: 'Blob' }): UseFileSystemAccessReturn<Blob> |
| 100 | export function useFileSystemAccess(options: UseFileSystemAccessOptions): UseFileSystemAccessReturn<string | ArrayBuffer | Blob> |
| 101 | export function useFileSystemAccess(options: UseFileSystemAccessOptions = {}): UseFileSystemAccessReturn<string | ArrayBuffer | Blob> { |
| 102 | const { |
| 103 | window: _window = defaultWindow, |
| 104 | dataType = 'Text', |
| 105 | } = options |
| 106 | |
| 107 | const window = _window as FileSystemAccessWindow |
| 108 | const isSupported = useSupported(() => window && 'showSaveFilePicker' in window && 'showOpenFilePicker' in window) |
| 109 | |
| 110 | const fileHandle = shallowRef<FileSystemFileHandle>() |
| 111 | const data = shallowRef<string | ArrayBuffer | Blob>() |
| 112 | |
| 113 | const file = shallowRef<File>() |
| 114 | const fileName = computed(() => file.value?.name ?? '') |
| 115 | const fileMIME = computed(() => file.value?.type ?? '') |
| 116 | const fileSize = computed(() => file.value?.size ?? 0) |
| 117 | const fileLastModified = computed(() => file.value?.lastModified ?? 0) |
| 118 | |
| 119 | async function open(_options: UseFileSystemAccessCommonOptions = {}) { |
| 120 | if (!isSupported.value) |
| 121 | return |
| 122 | const [handle] = await window.showOpenFilePicker({ ...toValue(options), ..._options }) |
| 123 | fileHandle.value = handle |
| 124 | await updateData() |
| 125 | } |
| 126 | |
| 127 | async function create(_options: UseFileSystemAccessShowSaveFileOptions = {}) { |
| 128 | if (!isSupported.value) |
| 129 | return |
| 130 | fileHandle.value = await (window as FileSystemAccessWindow).showSaveFilePicker({ ...options, ..._options }) |
| 131 | data.value = undefined |
| 132 | await updateData() |
| 133 | } |
| 134 | |
| 135 | async function save(_options: UseFileSystemAccessShowSaveFileOptions = {}) { |
| 136 | if (!isSupported.value) |
| 137 | return |
| 138 | |
| 139 | if (!fileHandle.value) |
| 140 | // save as |
| 141 | return saveAs(_options) |
| 142 | |
| 143 | if (data.value) { |
| 144 | const writableStream = await fileHandle.value.createWritable() |
| 145 | await writableStream.write(data.value) |
| 146 | await writableStream.close() |
| 147 | } |
| 148 | await updateFile() |
| 149 | } |
| 150 | |
| 151 | async function saveAs(_options: UseFileSystemAccessShowSaveFileOptions = {}) { |
| 152 | if (!isSupported.value) |
| 153 | return |
| 154 | |
| 155 | fileHandle.value = await (window as FileSystemAccessWindow).showSaveFilePicker({ ...options, ..._options }) |
| 156 | |
| 157 | if (data.value) { |
| 158 | const writableStream = await fileHandle.value.createWritable() |
nothing calls this directly
no test coverage detected