Skip to content

Commit

Permalink
refactor: update lib location
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Mar 19, 2024
1 parent 2e4dc59 commit 41e2942
Show file tree
Hide file tree
Showing 60 changed files with 369 additions and 418 deletions.
6 changes: 3 additions & 3 deletions apps/app/src/components/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { App, TFile, Vault } from "obsidian";
import { createContext, useContext } from "react";
// eslint-disable-next-line import/no-deprecated -- don't use equalityFn here
import { createStore, useStore } from "zustand";
import { type MediaURL } from "@/info/media-url";
import type { MediaHost } from "@/info/supported";
import { parseHashProps, type HashProps } from "@/lib/hash/hash-prop";
import { TimeoutError } from "@/lib/message";
import noop from "@/lib/no-op";
Expand All @@ -20,9 +22,7 @@ import {
} from "@/media-view/view-type";
import type MediaExtended from "@/mx-main";
import type { MxSettings } from "@/settings/def";
import { type MediaURL } from "@/web/url-match";
import type { MediaHost } from "@/web/url-match/supported";
import { type MediaInfo, mediaInfoFromFile } from "../media-view/media-info";
import { type MediaInfo, mediaInfoFromFile } from "../info/media-info";
import { applyTempFrag, handleTempFrag } from "./state/apply-tf";

export interface TransformConfig {
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/components/hook/use-playlist.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemoizedFn } from "ahooks";
import { useEffect, useMemo, useState } from "react";
import { compare } from "@/media-note/note-index";
import { compare } from "@/media-note/note-index/def";
import {
findWithMedia,
isWithMedia,
Expand Down
4 changes: 2 additions & 2 deletions apps/app/src/components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { useMemo, useState } from "react";
import { useTempFragHandler } from "@/components/hook/use-temporal-frag";
import { encodeWebpageUrl } from "@/lib/remote-player/encode";
import { cn } from "@/lib/utils";
import { toInfoKey } from "../media-note/note-index";
import { isFileMediaInfo } from "../media-view/media-info";
import { toInfoKey } from "@/media-note/note-index/def";
import { isFileMediaInfo } from "../info/media-info";
import { useApp, useIsEmbed, useMediaViewStore, useSettings } from "./context";
import { useViewTypeDetect } from "./hook/fix-webm-audio";
import { useControls, useDefaultVolume, useHashProps } from "./hook/use-hash";
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/components/player/menus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { Menu } from "obsidian";
import { useRef } from "react";
import { MoreIcon, PlaylistIcon, SubtitlesIcon } from "@/components/icon";
import { showAtButton } from "@/lib/menu";
import { compare } from "@/media-note/note-index/def";
import type { PlaylistItem } from "@/media-note/playlist/def";
import { isWithMedia } from "@/media-note/playlist/def";
import { compare } from "../../media-note/note-index";
import {
useApp,
useIsEmbed,
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/components/state/apply-tf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MediaPlayerInstance } from "@vidstack/react";
import { isTimestamp } from "@/lib/hash/temporal-frag";
import { compare } from "@/media-note/note-index";
import { compare } from "@/media-note/note-index/def";
import type { MediaViewState, MediaViewStoreApi } from "../context";

const tfNotInitial = new WeakSet<MediaPlayerInstance>();
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/icons.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addIcon } from "obsidian";
import { MediaHost } from "./web/url-match/supported";
import { MediaHost } from "./info/supported";

const icons: Record<Exclude<MediaHost, MediaHost.Generic>, string | null> = {
[MediaHost.Bilibili]: `<path fill-rule="evenodd" clip-rule="evenodd" d="M 20.736 14.88 C 18.513 12.735 18.513 9.173 20.736 7.028 C 22.849 4.99 26.197 4.99 28.311 7.028 L 40.096 18.397 C 40.43 18.72 40.715 19.075 40.949 19.453 L 58.772 19.453 C 59.006 19.075 59.291 18.72 59.625 18.397 L 71.41 7.028 C 73.523 4.99 76.871 4.99 78.984 7.028 C 81.208 9.173 81.208 12.735 78.984 14.88 L 74.244 19.453 L 77.778 19.453 C 90.051 19.453 100 29.402 100 41.675 L 100 72.262 C 100 84.534 90.051 94.484 77.778 94.484 L 22.222 94.484 C 9.949 94.484 0 84.534 0 72.262 L 0 41.675 C 0 29.402 9.949 19.453 22.222 19.453 L 25.477 19.453 L 20.736 14.88 Z M 22.222 30.172 C 16.086 30.172 11.111 35.146 11.111 41.283 L 11.111 72.654 C 11.111 78.79 16.086 83.765 22.222 83.765 L 77.778 83.765 C 83.914 83.765 88.889 78.79 88.889 72.654 L 88.889 41.283 C 88.889 35.146 83.914 30.172 77.778 30.172 L 22.222 30.172 Z M 27.778 51.805 C 27.778 48.737 30.265 46.25 33.333 46.25 C 36.402 46.25 38.889 48.737 38.889 51.805 L 38.889 56.772 C 38.889 59.84 36.402 62.328 33.333 62.328 C 30.265 62.328 27.778 59.84 27.778 56.772 L 27.778 51.805 Z M 66.667 46.25 C 63.598 46.25 61.111 48.737 61.111 51.805 L 61.111 56.772 C 61.111 59.84 63.598 62.328 66.667 62.328 C 69.735 62.328 72.222 59.84 72.222 56.772 L 72.222 51.805 C 72.222 48.737 69.735 46.25 66.667 46.25 Z" fill="currentColor"/>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Vault } from "obsidian";
import { TFile } from "obsidian";
import { checkMediaType, type MediaType } from "@/patch/media-type";
import type { MediaURL } from "@/web/url-match";
import type { MediaURL } from "@/info/media-url";
import { checkMediaType, type MediaType } from "./media-type";

export type MediaInfo = FileMediaInfo | MediaURL;

Expand Down
File renamed without changes.
139 changes: 139 additions & 0 deletions apps/app/src/info/media-url.ts
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { assertNever } from "assert-never";
import { Component, debounce } from "obsidian";
import { URLPattern } from "urlpattern-polyfill";
import type { FileMediaInfo, MediaInfo } from "@/media-view/media-info";
import type { FileMediaInfo, MediaInfo } from "@/info/media-info";
import type {
MediaFileViewType,
MediaViewType,
Expand All @@ -15,8 +15,8 @@ import {
MEDIA_FILE_VIEW_TYPE,
} from "@/media-view/view-type";
import type MxPlugin from "@/mx-main";
import { MediaURL } from "./media-url";
import { MediaHost } from "./supported";
import { MediaURL } from ".";

export type URLMatchPattern =
| {
Expand Down
14 changes: 14 additions & 0 deletions apps/app/src/lib/cursor.ts
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));
}
11 changes: 11 additions & 0 deletions apps/app/src/lib/iterate-files.ts
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;
}
}
}
2 changes: 1 addition & 1 deletion apps/app/src/lib/picker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mime from "mime";
import { Platform } from "obsidian";
import { MediaFileExtensions } from "@/patch/media-type";
import { MediaFileExtensions } from "@/info/media-type";
import { getDialog } from "@/web/session/utils";

/**
Expand Down
4 changes: 2 additions & 2 deletions apps/app/src/lib/remote-player/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import init from "inline:./scripts/initialize";
import { isString } from "maverick.js/std";
import { ButtonComponent, Notice } from "obsidian";
import type { WebviewElement } from "@/components/webview";
import { MediaURL } from "@/info/media-url";
import { MediaHost } from "@/info/supported";
import { GET_PORT_TIMEOUT, PORT_MESSAGE } from "@/lib/remote-player/const";
import { LoginModal } from "@/login/modal";
import { plugins } from "@/web/plugin";
import { titleParser } from "@/web/title";
import { MediaURL } from "@/web/url-match";
import { MediaHost } from "@/web/url-match/supported";
import { MessageController, TimeoutError } from "../message";
import { noHash } from "../url";
import { decodeWebpageUrl } from "./encode";
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/lib/subtitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readdir, readFile } from "fs/promises";
import type { TextTrackInit } from "@vidstack/react";
import type { Vault } from "obsidian";
import { Notice, TFile } from "obsidian";
import type { MediaURL } from "@/web/url-match";
import type { MediaURL } from "@/info/media-url";
import { groupBy } from "./group-by";
import { format, langCodeToLabel } from "./lang/lang";
import path from "./path";
Expand Down
10 changes: 5 additions & 5 deletions apps/app/src/login/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
DropdownComponent,
} from "obsidian";
import "./modal.global.less";
import { showAtButton } from "@/lib/menu";
import { getPartition } from "@/lib/remote-player/const";
import { getSession, getWebContents } from "@/web/session/utils";
import {
mediaHostDisplayName,
mediaHostUrl,
noGeneric,
} from "@/web/url-match/supported";
import type { SupportedMediaHost } from "@/web/url-match/supported";
} from "@/info/supported";
import type { SupportedMediaHost } from "@/info/supported";
import { showAtButton } from "@/lib/menu";
import { getPartition } from "@/lib/remote-player/const";
import { getSession, getWebContents } from "@/web/session/utils";

export class LoginModal extends Modal {
navEl = this.contentEl.insertAdjacentElement(
Expand Down
59 changes: 0 additions & 59 deletions apps/app/src/media-note/command/a.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/app/src/media-note/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
Modifier,
} from "obsidian";
import { Keymap, MarkdownView } from "obsidian";
import type { MediaInfo } from "@/media-view/media-info";
import type { MediaInfo } from "@/info/media-info";
import type { MediaView } from "@/media-view/view-type";
import type MxPlugin from "@/mx-main";
import { isMediaLeaf } from "../leaf-open";
Expand Down
4 changes: 2 additions & 2 deletions apps/app/src/media-note/leaf-open/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import type {
WorkspaceLeaf,
} from "obsidian";
import { Component, MarkdownView, debounce } from "obsidian";
import type { MediaInfo } from "@/info/media-info";
import { isFileMediaInfo } from "@/info/media-info";
import { normalizeFilename } from "@/lib/norm";
import type { MediaEmbedViewState } from "@/media-view/iframe-view";
import type { MediaInfo } from "@/media-view/media-info";
import { isFileMediaInfo } from "@/media-view/media-info";
import type { MediaUrlViewState } from "@/media-view/url-view";
import type {
MediaView,
Expand Down
Loading

0 comments on commit 41e2942

Please sign in to comment.