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