Skip to content

Commit

Permalink
Change .open to use feature detection instead
Browse files Browse the repository at this point in the history
Signed-off-by: William So <[email protected]>
  • Loading branch information
polyipseity committed Jun 6, 2024
1 parent ab8b364 commit 6a6b55b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-islands-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@polyipseity/obsidian-plugin-library": minor
---

Change `$FileSystem.open` to use feature detection instead.
8 changes: 3 additions & 5 deletions sources/@types/obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import type {
SettingTab,
UnknownSettingTab,
} from "obsidian"
import type { Deopaque } from "../types.js"
import type { Platform } from "../platform.js"
import type { Private } from "../private.js"

declare const PRIVATE_KEY: unique symbol
Expand Down Expand Up @@ -67,9 +65,9 @@ interface $DataAdapter {
}

interface $FileSystem {
readonly open: <T extends Platform.Current>(
path: Deopaque<T> extends Platform.Mobile ? string : never,
) => Deopaque<T> extends Platform.Mobile ? PromiseLike<void> : never
readonly open?: <Length extends number>(
path: Length extends 1 ? string : never,
) => Length extends 1 ? PromiseLike<void> : never
}

interface $Plugins {
Expand Down
17 changes: 8 additions & 9 deletions sources/obsidian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
cloneAsWritable,
createChildElement,
deepFreeze,
inSet,
instanceOf,
multireplace,
onVisible,
Expand All @@ -40,7 +39,6 @@ import { cloneDeep, constant, noop } from "lodash-es"
import { revealPrivate, revealPrivateAsync } from "./private.js"
import type { AsyncOrSync } from "ts-essentials"
import { InternalDOMClasses } from "./internals/magic.js"
import { Platform } from "./platform.js"
import type { PluginContext } from "./plugin.js"
import { around } from "monkey-around"
import { saveAs } from "file-saver"
Expand Down Expand Up @@ -486,20 +484,21 @@ export async function saveFileAs(
adapter: DataAdapter,
data: File,
): Promise<void> {
const { CURRENT, MOBILE } = Platform
if (inSet(MOBILE, CURRENT)) {
await revealPrivateAsync(context, [adapter], async ({ fs }) => {
await fs.open<typeof CURRENT>(
if (await revealPrivateAsync(context, [adapter], async ({ fs }) => {
if ("open" in fs && fs.open.length === 1) {
const { length } = fs.open
await fs.open<typeof length>(
(await Filesystem.writeFile({
data: await data.text(),
directory: Directory.Cache,
encoding: Encoding.UTF8,
path: data.name,
})).uri,
)
}, noop)
return
}
return true
}
return false
}, constant(false))) { return }
saveAs(data)
}

Expand Down

0 comments on commit 6a6b55b

Please sign in to comment.