-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
369 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/app/src/media-view/media-info.ts → apps/app/src/info/media-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { fileURLToPath } from "url"; | ||
import type { Vault, TFile } from "obsidian"; | ||
import { FileSystemAdapter, normalizePath } from "obsidian"; | ||
import { addTempFrag, removeTempFrag } from "@/lib/hash/format"; | ||
import { parseTempFrag, type TempFragment } from "@/lib/hash/temporal-frag"; | ||
import path from "@/lib/path"; | ||
import { noHash } from "@/lib/url"; | ||
import { resolveUrlMatcher, type URLResolveResult } from "@/web/url-match"; | ||
|
||
import { checkMediaType, type MediaType } from "./media-type"; | ||
import type { MediaHost } from "./supported"; | ||
|
||
const allowedProtocols = new Set(["https:", "http:", "file:"]); | ||
|
||
export class MediaURL extends URL implements URLResolveResult { | ||
static create(url: string | URL, mx?: URL | string): MediaURL | null { | ||
if (url instanceof MediaURL) { | ||
return url.clone(); | ||
} | ||
try { | ||
return new MediaURL(url, mx); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
get inferredType(): MediaType | null { | ||
const ext = this.pathname.split(".").pop(); | ||
if (!ext) return null; | ||
return checkMediaType(ext); | ||
} | ||
|
||
get isFileUrl(): boolean { | ||
return this.protocol === "file:"; | ||
} | ||
get filePath(): string | null { | ||
if (this.isFileUrl) { | ||
try { | ||
return fileURLToPath(this); | ||
} catch (e) { | ||
console.error("Failed to convert file url to path", e, this.href); | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
getVaultFile(vault: Vault): TFile | null { | ||
if (!(vault.adapter instanceof FileSystemAdapter)) return null; | ||
const filePath = this.filePath; | ||
const vaultBasePath = vault.adapter.getBasePath(); | ||
if (!filePath) return null; | ||
const relative = path.relative(vaultBasePath, filePath); | ||
if (/^\.\.[/\\]/.test(relative) || path.isAbsolute(relative)) return null; | ||
const normalized = normalizePath(relative); | ||
return vault.getFileByPath(normalized); | ||
} | ||
|
||
compare(other: MediaURL | null | undefined): boolean { | ||
return !!other && this.jsonState.source === other.jsonState.source; | ||
} | ||
|
||
/** | ||
* Print the url with temporal fragment encoded (if supported) | ||
* @returns the url without hash | ||
*/ | ||
print(frag?: TempFragment): string { | ||
if (this.mxUrl) return noHash(this.mxUrl.href); | ||
if (!frag) return this.jsonState.source; | ||
if (this.#resolved.print) return this.#resolved.print(frag); | ||
return this.jsonState.source; | ||
} | ||
|
||
get tempFrag(): TempFragment | null { | ||
return parseTempFrag(this.hash); | ||
} | ||
// get isTimestamp(): boolean { | ||
// return !!this.tempFrag && isTimestamp(this.tempFrag); | ||
// } | ||
|
||
// setHash(hash: string | ((hash: string) => string)): MediaURL { | ||
// const prevHash = this.hash.replace(/^#+/, ""); | ||
// const newHash = | ||
// typeof hash === "string" ? hash.replace(/^#+/, "") : hash(prevHash); | ||
// if (newHash === prevHash) return this; | ||
// const newURL = this.clone(); | ||
// newURL.hash = newHash; | ||
// return newURL; | ||
// } | ||
setTempFrag(tempFrag: TempFragment | null): MediaURL { | ||
const newUrl = this.clone(); | ||
const notf = removeTempFrag(this.hash); | ||
if (!tempFrag) { | ||
newUrl.hash = notf; | ||
} else { | ||
newUrl.hash = addTempFrag(notf, tempFrag); | ||
} | ||
return newUrl; | ||
} | ||
|
||
clone() { | ||
return new MediaURL(this, this.mxUrl ?? undefined); | ||
} | ||
|
||
get readableHref() { | ||
return decodeURI(this.href); | ||
} | ||
|
||
#resolved: URLResolveResult; | ||
|
||
get source() { | ||
return this.#resolved.source; | ||
} | ||
get cleaned(): URL { | ||
return this.#resolved.cleaned; | ||
} | ||
get id(): string | undefined { | ||
return this.#resolved.id; | ||
} | ||
readonly type: MediaHost; | ||
|
||
get jsonState(): { source: string; hash: string } { | ||
return { | ||
source: noHash(this.mxUrl ?? this.cleaned), | ||
hash: addTempFrag(this.hash, this.#resolved.tempFrag), | ||
}; | ||
} | ||
|
||
mxUrl: URL | null; | ||
constructor(original: string | URL, mx?: URL | string) { | ||
super(original); | ||
this.mxUrl = mx ? new URL(mx) : null; | ||
if (!allowedProtocols.has(this.protocol)) | ||
throw new Error("Unsupported protocol: " + this.protocol); | ||
const { type, resolved } = resolveUrlMatcher(this); | ||
this.#resolved = resolved; | ||
this.type = type; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Editor } from "obsidian"; | ||
|
||
export function insertToCursor(str: string, editor: Editor) { | ||
const cursor = editor.getCursor("to"); | ||
console.debug("insert to cursor [to]", cursor.ch, cursor.line); | ||
editor.replaceRange(str, cursor, cursor); | ||
editor.setCursor(editor.offsetToPos(editor.posToOffset(cursor) + str.length)); | ||
} | ||
export function insertBeforeCursor(str: string, editor: Editor) { | ||
const cursor = editor.getCursor("from"); | ||
console.debug("insert before cursor [from]", cursor.ch, cursor.line); | ||
editor.replaceRange(str, cursor, cursor); | ||
// editor.setCursor(editor.offsetToPos(editor.posToOffset(cursor) + str.length)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TFolder, TFile } from "obsidian"; | ||
|
||
export function* iterateFiles(folder: TFolder): IterableIterator<TFile> { | ||
for (const child of folder.children) { | ||
if (child instanceof TFolder) { | ||
yield* iterateFiles(child); | ||
} else if (child instanceof TFile) { | ||
yield child; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.