Skip to content

Commit

Permalink
Refine folder location options for export
Browse files Browse the repository at this point in the history
  • Loading branch information
luxmargos committed Feb 27, 2024
1 parent 2bea476 commit b107e39
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["esbenp.prettier-vscode"]
}
18 changes: 17 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
{
// "editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.tabSize": 2,
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.tabSize": 2,
},
"editor.formatOnSave": true,
"prettier.tabWidth": 2,
"prettier.printWidth": 180,
"files.exclude": {
"**/.git": false
},
"files.associations": {
"*.json":"jsonc"
}
}
}

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.7",
"version": "0.1.8",
"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.7",
"version": "0.1.8",
"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
35 changes: 22 additions & 13 deletions src/export_pack/export_utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TFile, normalizePath } from "obsidian";
import * as bp from "path-browserify";
import { ExportFormat, ExportPathData, exportFormatList } from "./export_types";
import { ImgkExportSettings } from "../settings/setting_types";
import {
ImgkExportSettings,
ImgkFolderDeterminer,
} from "../settings/setting_types";
import { ImgkRuntimeExportSettings } from "../settings/settings_as_func";
import { resolveExportDstInfo } from "../engines/imgEngine";
import { MainPluginContext } from "../context";
Expand Down Expand Up @@ -29,11 +32,11 @@ export const buildFileNameFormat = (prefix: string, suffix: string) => {
};

/**
*
* @param settings
* @param srcFile
* @param specificDst Optional. Specifies the destination file path. If provided, 'settings' will be ignored. Used for instant export.
* @returns
*
* @param settings
* @param srcFile
* @param specificDst Optional. Specifies the destination file path. If provided, 'settings' will be ignored. Used for instant export.
* @returns
*/
export const genExportPath = (
settings: ImgkExportSettings,
Expand All @@ -43,7 +46,7 @@ export const genExportPath = (
const srcFilePath: string =
srcFile instanceof TFile ? srcFile.path : srcFile;

const dir = bp.dirname(srcFilePath);
const srcFileDir = bp.dirname(srcFilePath);

const srcFileName = bp.basename(srcFilePath);
let srcFileNameWithoutExt = bp.basename(srcFilePath);
Expand Down Expand Up @@ -98,16 +101,22 @@ export const genExportPath = (
return undefined;
}

let dirText = dir;
if (!settings.pathOpts.asRelativePath) {
if (dir.length > 0 && dir !== ".") {
dirText = settings.pathOpts.exportDirAbs + "/" + dir;
let dirText = srcFileDir;
const fd = settings.pathOpts.folderDeterminer;

if (fd === ImgkFolderDeterminer.Absolute) {
dirText = settings.pathOpts.exportDirAbs;
} else if (
fd === ImgkFolderDeterminer.AbsoluteAndReflectFolderStructure
) {
if (srcFileDir.length > 0 && srcFileDir !== ".") {
dirText = settings.pathOpts.exportDirAbs + "/" + srcFileDir;
} else {
dirText = settings.pathOpts.exportDirAbs;
}
} else {
if (dir.length > 0 && dir !== ".") {
dirText = dir + "/" + settings.pathOpts.exportDirRel;
if (srcFileDir.length > 0 && srcFileDir !== ".") {
dirText = srcFileDir + "/" + settings.pathOpts.exportDirRel;
} else {
dirText = settings.pathOpts.exportDirRel;
}
Expand Down
11 changes: 8 additions & 3 deletions src/i18n/trans/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ export default {
DOUBLE_EXTS_BLOCKER_DESC:
"Avoid export if source file has at least two extensions. The filter determine file is already exported from another source. e.g, 'MyImage.psd.exported.png'",

AS_RELATIVE_FOLDER: "As relative folder",
AS_RELATIVE_FOLDER_DESC:
"If turned on, all exported images will be generated relative to their source file. Otherwise, they will be generated into the absolute folder.",
FOLDER_LOCATION: "Folder location",
FOLDER_LOCATION_DESC:
"Specify the folder location where the generated file will be placed.",

FOLDER_LOCATION_TYPE_RELATIVE: "Relative",
FOLDER_LOCATION_TYPE_ABSOLUTE_WITH_REFLECT_STRUCTURE:
"Absolute, reflect source's folder structure",
FOLDER_LOCATION_TYPE_ABSOLUTE: "Absolute",

FOLDER_ABSOLUTE: "Folder (Absolute)",
FOLDER_RELATIVE: "Folder (Relative)",
Expand Down
54 changes: 42 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
DEFAULT_INSTANT_EXPORT_SETTINGS,
DEFAULT_SETTINGS,
SettingsUtil,
migrageFolderDeterminer,
} from "./settings/settings";
import { MainPlugin, MainPluginContext } from "./context";
import { ImgkPluginSettingTab } from "./settings/settings_tab";
Expand All @@ -35,9 +36,13 @@ import { asTFile, isTFile } from "./vault_util";
import { t } from "./i18n/t";
import { clearCaches } from "./img_cache";
import { cloneDeep } from "lodash-es";
import { ImgkPluginSettings } from "./settings/setting_types";
import {
ImgkFolderDeterminer,
ImgkPluginSettings,
} from "./settings/setting_types";
import fileUrl from "file-url";
import { logLevelMobilePatcher } from "./utils/log_utils";
import packageJson from "../package.json";

export default class ImgMagicianPlugin extends MainPlugin {
settings: ImgkPluginSettings;
Expand Down Expand Up @@ -429,31 +434,34 @@ export default class ImgMagicianPlugin extends MainPlugin {
this.app.vault.adapter.getBasePath();

debug("Reveal BasePath with Desktop");
} else{
} else {
try {
if(
if (
//@ts-ignore
this.app.vault.adapter["getBasePath"] &&
//@ts-ignore
typeof this.app.vault.adapter["getBasePath"] === "function"
){
typeof this.app.vault.adapter["getBasePath"] ===
"function"
) {
debug("Reveal BasePath with Func");
//@ts-ignore
const getBasePathFunc = this.app.vault.adapter.getBasePath;
const getBasePathFunc =
//@ts-ignore
this.app.vault.adapter.getBasePath;
this.baseResourcePath = getBasePathFunc();
}
} catch (err) {}
}

if(this.baseResourcePath){
try{
if (this.baseResourcePath) {
try {
const fileUrlString = fileUrl(this.baseResourcePath);
const url = new URL(fileUrlString);
this.baseResourcePath = url.pathname;
}catch(e){}
} catch (e) {}

if(this.baseResourcePath){
this.baseResourcePathIdx = this.baseResourcePath?.length ?? -1;
if (this.baseResourcePath) {
this.baseResourcePathIdx =
this.baseResourcePath?.length ?? -1;
}
}

Expand Down Expand Up @@ -534,6 +542,28 @@ export default class ImgMagicianPlugin extends MainPlugin {
const savedData = await this.loadData();

this.settings = Object.assign({}, DEFAULT_SETTINGS, savedData);
//MINGRATION
//Legacy
if (this.settings.version === undefined) {
} else {
}
//

migrageFolderDeterminer(
this.settings.instantExport.pathOpts,
ImgkFolderDeterminer.Relative
);

for (const autoExportSettings of this.settings.autoExportList) {
migrageFolderDeterminer(
autoExportSettings.pathOpts,
ImgkFolderDeterminer.AbsoluteAndReflectFolderStructure
);
}

this.settings.version = packageJson.version;
//END OF MIGRATION

this.settingsUtil = new SettingsUtil(this.settings);
}

Expand Down
10 changes: 9 additions & 1 deletion src/settings/setting_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export enum ImgkFileFilterType {
DoubleExtsBlocker = 4,
}

export enum ImgkFolderDeterminer {
Relative = 0,
AbsoluteAndReflectFolderStructure = 1,
Absolute = 2,
}

export interface ImgkFileFilter {
active: boolean;
type: ImgkFileFilterType;
Expand All @@ -35,7 +41,9 @@ export interface ImgkExportPath {
sourceFilters: ImgkTextFilter[];
useBuiltInSourceFilters: boolean;
builtInSourceFilters: ImgkFileFilter[];
asRelativePath: boolean;
/** @deprecated */
asRelativePath?: boolean;
folderDeterminer: ImgkFolderDeterminer;
exportDirAbs: string;
exportDirRel: string;
useCustomFileNameFormat: boolean;
Expand Down
36 changes: 32 additions & 4 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import {
import { asTFile, isTFile } from "../vault_util";
import packageJson from "../../package.json";
import {
ImgkExportPath,
ImgkExportSettings,
ImgkFileFilterType,
ImgkFolderDeterminer,
ImgkPluginSettings,
} from "./setting_types";
import { ExportFormatPng } from "../export_pack/export_types";
import { UnsupportedChannelKindOffset } from "@webtoon/psd/dist/utils";

export const DEFAULT_EXPORT_SUPPORTED_FORMATS = [
"psd",
Expand Down Expand Up @@ -107,7 +110,9 @@ export const DEFAULT_EXPORT_SETTINGS: ImgkExportSettings = {
],
sourceExts: [],
sourceFilters: [],
asRelativePath: false,
// asRelativePath: false,
folderDeterminer:
ImgkFolderDeterminer.AbsoluteAndReflectFolderStructure,
exportDirAbs: "Exported Images",
exportDirRel: "",
useCustomFileNameFormat: false,
Expand All @@ -117,14 +122,37 @@ export const DEFAULT_EXPORT_SETTINGS: ImgkExportSettings = {
},
};

const cloneDefaultExportSettings = () => {
export const migrageFolderDeterminer = (
pathOpts: ImgkExportPath,
defaultFolderDeterminer: ImgkFolderDeterminer
) => {
if (pathOpts["asRelativePath"] !== undefined) {
console.log("migrate ", JSON.stringify(pathOpts, null, 2));

const value: boolean = pathOpts["asRelativePath"];
if (value === true) {
pathOpts.folderDeterminer = ImgkFolderDeterminer.Relative;
} else {
pathOpts.folderDeterminer =
ImgkFolderDeterminer.AbsoluteAndReflectFolderStructure;
}

delete pathOpts["asRelativePath"];
}

if (pathOpts["folderDeterminer"] === undefined) {
pathOpts.folderDeterminer = defaultFolderDeterminer;
}
};

const cloneDefaultExportSettingsForInstant = () => {
const defaultSettings = cloneDeep(DEFAULT_EXPORT_SETTINGS);
defaultSettings.pathOpts.asRelativePath = true;
defaultSettings.pathOpts.folderDeterminer = ImgkFolderDeterminer.Relative;
return defaultSettings;
};

export const DEFAULT_INSTANT_EXPORT_SETTINGS: ImgkExportSettings =
cloneDefaultExportSettings();
cloneDefaultExportSettingsForInstant();

export const getWarnList = () => {
if (satisfies(apiVersion, ">=1.5.3")) {
Expand Down
Loading

0 comments on commit b107e39

Please sign in to comment.