From 6a6b55b79b9733913010454ed4beda0694e6cacf Mon Sep 17 00:00:00 2001 From: William So Date: Thu, 6 Jun 2024 14:10:47 +0800 Subject: [PATCH] Change `.open` to use feature detection instead Signed-off-by: William So --- .changeset/yellow-islands-laugh.md | 5 +++++ sources/@types/obsidian.ts | 8 +++----- sources/obsidian.ts | 17 ++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 .changeset/yellow-islands-laugh.md diff --git a/.changeset/yellow-islands-laugh.md b/.changeset/yellow-islands-laugh.md new file mode 100644 index 0000000..48a2b04 --- /dev/null +++ b/.changeset/yellow-islands-laugh.md @@ -0,0 +1,5 @@ +--- +"@polyipseity/obsidian-plugin-library": minor +--- + +Change `$FileSystem.open` to use feature detection instead. diff --git a/sources/@types/obsidian.ts b/sources/@types/obsidian.ts index 40bd6eb..9a41511 100644 --- a/sources/@types/obsidian.ts +++ b/sources/@types/obsidian.ts @@ -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 @@ -67,9 +65,9 @@ interface $DataAdapter { } interface $FileSystem { - readonly open: ( - path: Deopaque extends Platform.Mobile ? string : never, - ) => Deopaque extends Platform.Mobile ? PromiseLike : never + readonly open?: ( + path: Length extends 1 ? string : never, + ) => Length extends 1 ? PromiseLike : never } interface $Plugins { diff --git a/sources/obsidian.ts b/sources/obsidian.ts index a3b9dd4..6c71f89 100644 --- a/sources/obsidian.ts +++ b/sources/obsidian.ts @@ -30,7 +30,6 @@ import { cloneAsWritable, createChildElement, deepFreeze, - inSet, instanceOf, multireplace, onVisible, @@ -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" @@ -486,10 +484,10 @@ export async function saveFileAs( adapter: DataAdapter, data: File, ): Promise { - const { CURRENT, MOBILE } = Platform - if (inSet(MOBILE, CURRENT)) { - await revealPrivateAsync(context, [adapter], async ({ fs }) => { - await fs.open( + if (await revealPrivateAsync(context, [adapter], async ({ fs }) => { + if ("open" in fs && fs.open.length === 1) { + const { length } = fs.open + await fs.open( (await Filesystem.writeFile({ data: await data.text(), directory: Directory.Cache, @@ -497,9 +495,10 @@ export async function saveFileAs( path: data.name, })).uri, ) - }, noop) - return - } + return true + } + return false + }, constant(false))) { return } saveAs(data) }