Skip to content

Commit

Permalink
Improved iOS stability
Browse files Browse the repository at this point in the history
  • Loading branch information
luxmargos committed Jan 6, 2024
1 parent f713c55 commit f0d8f3d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 14 deletions.
4 changes: 2 additions & 2 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ const context = await esbuild.context({
"@lezer/lr",
...builtins,
],
// minify: prod ? true : false,
minify: false,
minify: prod ? true : false,
// minify: false,
plugins: [imageMagickWasmPlugin],
format: "cjs",
// format: "esm",
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "image-magician",
"name": "Image Magician",
"version": "0.1.3",
"version": "0.1.4",
"minAppVersion": "0.15.0",
"description": "Supports viewing and exporting various image formats powerd by ImageMagick.",
"author": "luxmargos",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-image-magician-plugin",
"version": "0.1.3",
"version": "0.1.4",
"description": "This is a plugin for Obsidian (https://obsidian.md). Supports viewing and exporting various image formats using ImageMagick.",
"main": "main.js",
"scripts": {
Expand Down
12 changes: 9 additions & 3 deletions src/editor_ext/img_post_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export const processVaultBasedPath = (
if (srcPath) {
const srcFile = findValutFile(context, srcPath, false);
if (srcFile) {
imgEl.src = context.plugin.app.vault.getResourcePath(srcFile);
const srcResPath =
context.plugin.app.vault.getResourcePath(srcFile);
debug(`processVaultBasedPath: ${srcUrl} => ${srcResPath}`);
imgEl.src = srcResPath;
}
}
};
Expand All @@ -100,12 +103,16 @@ export const handleImg = (
if (!(imgEl instanceof HTMLImageElement)) {
return;
}

const src = imgEl.getAttribute("src");
if (!src) {
return;
}

if (!src.startsWith("app://")) {
// desktop = "app://"
// ios = "capacitor://"
// android = ?
if (src.indexOf("://") < 0) {
return;
}

Expand Down Expand Up @@ -160,7 +167,6 @@ export const handleImg = (
let draw = !isLatestImgDrawnElement(imgEl, srcFile);

if (!draw) {
debug("pass redrawing");
return;
}

Expand Down
43 changes: 38 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
FileSystemAdapter,
Modal,
Platform,
Plugin,
TAbstractFile,
TFile,
ViewCreator,
Expand All @@ -25,17 +26,18 @@ import { getMarkdownPostProcessor } from "./editor_ext/post_processor";
import { Magick } from "@imagemagick/magick-wasm";

import { PluginFullName, PluginName } from "./consts/main";
import { debug, info, setLevel } from "loglevel";
import log, { debug, info, setLevel } from "loglevel";

import { ImgkMutationObserver } from "./editor_ext/mutation_ob";

import { ImgkPluginExportDialog } from "./dialogs/export_opt_dialog";
import { asTFile, isTFile } from "./vault_util";
import { asTFile, findValutFile, isTFile } from "./vault_util";
import { t } from "./i18n/t";
import { clearCaches } from "./img_cache";
import { ImgkPluginSettingsDialog } from "./dialogs/plugin_settings_dialog";
import { cloneDeep } from "lodash-es";
import { ImgkPluginSettings } from "./settings/setting_types";
import { logLevelMobilePatcher } from "./utils/log_utils";

export default class ImgMagicianPlugin extends MainPlugin {
settings: ImgkPluginSettings;
Expand Down Expand Up @@ -121,8 +123,13 @@ export default class ImgMagicianPlugin extends MainPlugin {

async onload() {
this.baseResourcePathIdx = -1;
setLevel("INFO");
// setLevel("DEBUG");
if (process.env.NODE_ENV === "development") {
setLevel("DEBUG");
logLevelMobilePatcher(this);
debug("DEV MODE");
} else {
setLevel("INFO");
}

if (!PIE._magick) {
// initialize magick engine
Expand Down Expand Up @@ -418,11 +425,37 @@ export default class ImgMagicianPlugin extends MainPlugin {
this.app.vault.adapter.getBasePath();
this.baseResourcePathIdx =
this.baseResourcePath?.length ?? -1;
} else {

debug("Reveal BasePath with Desktop");
} else if (
//@ts-ignore
this.app.vault.adapter["getBasePath"] &&
//@ts-ignore
typeof this.app.vault.adapter["getBasePath"] === "function"
) {
try {
//@ts-ignore
const func = this.app.vault.adapter.getBasePath;
this.baseResourcePath = func();
this.baseResourcePathIdx =
this.baseResourcePath?.length ?? -1;

debug("Reveal BasePath with Func");
} catch (err) {}
}

if (this.baseResourcePathIdx < 0) {
debug("Reveal BasePath manually");
this.baseResourcePath = await this.findBaseResourcePath();
this.baseResourcePathIdx =
this.baseResourcePath?.length ?? -1;
}

debug(
"FINAL BASE PATH : ",
this.baseResourcePath,
this.baseResourcePathIdx
);
} catch (err) {}

try {
Expand Down
49 changes: 49 additions & 0 deletions src/utils/log_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import log from "loglevel";
import { Platform, Plugin, TFile } from "obsidian";

export const logLevelMobilePatcher = (plugin: Plugin) => {
if (!Platform.isMobile) {
return;
}

//const logFile = `${plugin.manifest.dir}/logs.txt`;
const logFile = `${plugin.manifest.id}-logs.md`;
const logs: string[] = [];

let logTargetFile: TFile | undefined;
const mobileLogger = async () => {
if (!logTargetFile) {
if ((await plugin.app.vault.adapter.exists(logFile)) === false) {
await plugin.app.vault.create(logFile, "");
}

const abFile = await plugin.app.vault.getAbstractFileByPath(
logFile
);
if (abFile && abFile instanceof TFile) {
logTargetFile = abFile;
}
}

if (logTargetFile) {
await plugin.app.vault.modify(logTargetFile, logs.join(" "));
}
};

var originalFactory = log.methodFactory;
log.methodFactory = function (methodName, logLevel, loggerName) {
var rawMethod = originalFactory(methodName, logLevel, loggerName);

return function (...messages: any[]) {
rawMethod(messages);
logs.push(...messages);
logs.push("\n");

mobileLogger()
.then(() => {})
.catch((err) => {});
};
};

log.setLevel(log.getLevel()); // Be sure to call setLevel method in order to apply plugin
};
5 changes: 3 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"0.1.0": "0.15.0",
"0.1.1": "0.15.0",
"0.1.2": "0.15.0",
"0.1.3": "0.15.0"
}
"0.1.3": "0.15.0",
"0.1.4": "0.15.0"
}

0 comments on commit f0d8f3d

Please sign in to comment.