Skip to content

Commit

Permalink
refactor: move log into main for dev options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Dec 4, 2023
1 parent dc7f184 commit 3be59e8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 59 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"monkey-around": "^2.3.0",
"obsidian": "^1.4.0",
"obsidian-plugin-cli": "^0.9.0",
"ts-deepmerge": "^6.2.0",
"typescript": "^5.1.6"
},
"license": "GPL-3.0-only",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"remove": "Remove markup for all links",
"title": "Links format"
},
"debug": {
"desc": "Show debug log in console",
"title": "Debug information"
},
"edit": {
"desc": "Edit view settings",
"title": "Edit"
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"remove": "Supprimer les balises pour tous les liens",
"title": "Format des liens"
},
"debug": {
"desc": "Affiche le journal de débogage dans la console",
"title": "Informations de débogage"
},
"edit": {
"desc": "Paramètre pour la vue d'édition",
"title": "Édition"
Expand Down
2 changes: 2 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface EnhancedCopySettings {
editing: GlobalSettings;
reading: GlobalSettings;
overrideCopy: boolean;
devMode: boolean;
}

/**
Expand Down Expand Up @@ -111,6 +112,7 @@ export const DEFAULT_SETTINGS: EnhancedCopySettings = {
replaceText: []
},
overrideCopy: false,
devMode: false
};

export interface GlobalSettings {
Expand Down
53 changes: 34 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {EditorView} from "@codemirror/view";
import i18next from "i18next";
import {around} from "monkey-around";
import { ItemView, MarkdownView, Plugin, WorkspaceLeaf} from "obsidian";
import { ItemView, MarkdownView, Platform, Plugin, WorkspaceLeaf} from "obsidian";
import merge from "ts-deepmerge";

import {resources, translationLanguage} from "./i18n/i18next";
import {ApplyingToView, DEFAULT_SETTINGS,EnhancedCopySettings} from "./interface";
import {EnhancedCopySettingTab} from "./settings";
import {convertEditMarkdown, convertMarkdown,} from "./utils/conversion";
import {devLog} from "./utils/log";
import {removeDataBasePluginRelationShip} from "./utils/pluginFix";
import {canvasSelectionText, copySelectionRange, getSelectionAsHTML} from "./utils/selection";

Expand All @@ -21,18 +21,18 @@ export default class EnhancedCopy extends Plugin {
let viewIn: ApplyingToView;
let selectedText: string;
if (activeView && activeView.getMode() !== "source") {
devLog(i18next.t("log.readingMode"));
this.devLog(i18next.t("log.readingMode"));
selectedText = getSelectionAsHTML(this.settings);
viewIn = ApplyingToView.reading;
} else if (activeView) {
devLog(i18next.t("log.editMode"));
this.devLog(i18next.t("log.editMode"));
const editor = activeView.editor;
selectedText = copySelectionRange(editor);
selectedText = copySelectionRange(editor, this);
viewIn = ApplyingToView.edit;
} else {
const leafType = this.app.workspace.getActiveViewOfType(ItemView)?.getViewType();
if (leafType === "canvas") {
selectedText = canvasSelectionText(this.app, this.settings);
selectedText = canvasSelectionText(this.app, this);
viewIn = this.app.workspace.activeEditor ? ApplyingToView.edit : ApplyingToView.reading;
} else {
selectedText = leafType === "database-plugin" ? removeDataBasePluginRelationShip() : activeWindow.getSelection()?.toString() ?? "";
Expand All @@ -46,8 +46,8 @@ export default class EnhancedCopy extends Plugin {
) {
selectedText = viewIn === ApplyingToView.edit
?
convertEditMarkdown(selectedText, this.settings.editing, this.settings) :
convertMarkdown(selectedText, this.settings, this.settings.reading);
convertEditMarkdown(selectedText, this.settings.editing, this) :
convertMarkdown(selectedText, this.settings.reading, this);
}
return selectedText;
} else if (viewIn === ApplyingToView.edit) {
Expand All @@ -64,7 +64,6 @@ export default class EnhancedCopy extends Plugin {
return (event: ClipboardEvent) => {
try {
const selectedText = this.enhancedCopy();
console.log(selectedText);
if (selectedText) {
event.preventDefault();
event.clipboardData?.setData("text/plain", selectedText);
Expand Down Expand Up @@ -135,9 +134,9 @@ export default class EnhancedCopy extends Plugin {
name: i18next.t("commands.editor"),
hotkeys: [],
editorCallback: (editor) => {
let selectedText = copySelectionRange(editor);
let selectedText = copySelectionRange(editor, this);
if (selectedText && selectedText.trim().length > 0) {
selectedText = convertEditMarkdown(selectedText, this.settings.editing, this.settings);
selectedText = convertEditMarkdown(selectedText, this.settings.editing, this);
navigator.clipboard.writeText(selectedText);
}
}
Expand All @@ -154,7 +153,7 @@ export default class EnhancedCopy extends Plugin {
if (!checking) {
let selectedText = getSelectionAsHTML(this.settings);
if (!this.settings.exportAsHTML) {
selectedText = convertMarkdown(selectedText, this.settings, this.settings.reading);
selectedText = convertMarkdown(selectedText, this.settings.reading, this);
}
navigator.clipboard.writeText(selectedText);
}
Expand All @@ -171,14 +170,14 @@ export default class EnhancedCopy extends Plugin {
checkCallback: (checking: boolean) => {
//everything not markdown view
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
devLog(!markdownView, checking);
this.devLog(!markdownView, checking);
if (!markdownView) {
if (!checking) {
const leafType = this.app.workspace.getActiveViewOfType(ItemView)?.getViewType();
let selectedText: string;
let viewIn: ApplyingToView;
if (leafType === "canvas") {
selectedText = canvasSelectionText(this.app, this.settings);
selectedText = canvasSelectionText(this.app, this);
viewIn = this.app.workspace.activeEditor ? ApplyingToView.edit : ApplyingToView.reading;
} else {
selectedText = leafType === "database-plugin" ? removeDataBasePluginRelationShip() : activeWindow.getSelection()?.toString() ?? "";
Expand All @@ -190,8 +189,8 @@ export default class EnhancedCopy extends Plugin {
) {
selectedText = viewIn === ApplyingToView.edit
?
convertEditMarkdown(selectedText, this.settings.editing, this.settings) :
convertMarkdown(selectedText, this.settings, this.settings.reading);
convertEditMarkdown(selectedText, this.settings.editing, this) :
convertMarkdown(selectedText, this.settings.reading, this);
}
navigator.clipboard.writeText(selectedText);
}
Expand All @@ -206,7 +205,6 @@ export default class EnhancedCopy extends Plugin {
if (this.settings.overrideCopy) {
this.registerEvent(this.app.workspace.on("active-leaf-change", async (leaf) => {
if (!leaf) {
console.log("No active leaf");
for (const monkey of Object.values(this.activeMonkeys)) {
monkey();
}
Expand All @@ -219,7 +217,7 @@ export default class EnhancedCopy extends Plugin {
if (leaf.view instanceof ItemView && leaf.view.getViewType() === "canvas") {
//no event listener in dom
leaf.view.containerEl.addEventListener("copy", (event) => {
const selectedText = canvasSelectionText(this.app, this.settings);
const selectedText = canvasSelectionText(this.app, this);
event.preventDefault();
event.clipboardData?.setData("text/plain", selectedText);
});
Expand All @@ -245,9 +243,26 @@ export default class EnhancedCopy extends Plugin {
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
const loadedData = await this.loadData();
try {
this.settings = merge(DEFAULT_SETTINGS, loadedData) as unknown as EnhancedCopySettings;
} catch (e) {
console.warn("[Enhanced copy] Error while deep merging settings, using default loading method");
this.settings = Object.assign({}, DEFAULT_SETTINGS, loadedData);
}
}
async saveSettings() {
await this.saveData(this.settings);
}
devLog(...args: unknown[]) {
if (!(Platform.isDesktop && this.settings.devMode)) {
return;
}
let callFunction = new Error().stack?.split("\n")[2].trim();
callFunction = callFunction?.substring(callFunction.indexOf("at ") + 3, callFunction.lastIndexOf(" ("));
callFunction = callFunction!.replace("Object.callback", "");
callFunction = callFunction.length > 0 ? callFunction : "main";
const date = new Date().toISOString().slice(11, 23);
console.log(`[${date}](${callFunction}):\n`, ...args);
}
}
11 changes: 11 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ export class EnhancedCopySettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});
new Setting(this.settingsPage)
.setName(i18next.t("debug.title"))
.setDesc(i18next.t("debug.desc"))
.addToggle((toggle) => {
toggle
.setValue(this.settings.devMode)
.onChange(async (value) => {
this.settings.devMode = value;
await this.plugin.saveSettings();
});
});
}

renderReading() {
Expand Down
1 change: 0 additions & 1 deletion src/utils/NodesEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export function replaceAllDivCalloutToBlockquote(
settings: GlobalSettings
): HTMLDivElement {
const allDivCallout = div.querySelectorAll("div[class*='callout']");
console.log(allDivCallout);
let calloutTitle = "";
for (const divCallout of allDivCallout) {
if (
Expand Down
30 changes: 15 additions & 15 deletions src/utils/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ConversionOfLinks,
EnhancedCopySettings, GlobalSettings
} from "../interface";
import {devLog} from "./log";
import EnhancedCopy from "../main";

/**
* If a list is preceded by an empty line, remove the empty line
Expand Down Expand Up @@ -154,25 +154,24 @@ function removeHighlightMark(markdown: string, settings: GlobalSettings): string
* @param settings {EnhancedCopySettings} Settings of the plugin
* @returns {string} Markdown with hard breaks added if needed
*/
function hardBreak(markdown: string, settings: GlobalSettings): string {
function hardBreak(markdown: string, settings: GlobalSettings, plugin: EnhancedCopy): string {
if (settings.hardBreak) {
markdown = markdown.replace(/ *\n/g, " \n");
markdown += " ";
} else {
devLog(i18next.t("log.noHardBreaks"));
plugin.devLog(i18next.t("log.noHardBreaks"));
markdown = markdown.replace(/ *\n/g, "\n");
}
return markdown;
}

function convertCallout(markdown: string, overrides: GlobalSettings): string {
devLog(i18next.t("log.callout.title"), overrides.callout);
function convertCallout(markdown: string, overrides: GlobalSettings, plugin: EnhancedCopy): string {
plugin.devLog(i18next.t("log.callout.title"), overrides.callout);
const calloutRegex = /^>* *\[!(\w+)\|?(.*)\] *(.*)$/gm;
if (overrides.callout === CalloutKeepType.removeKeepTitle || overrides.callout === CalloutKeepType.remove) {
devLog(i18next.t("log.callout.remove"));
plugin.devLog(i18next.t("log.callout.remove"));
//delete the type of the callout
markdown = markdown.replace(calloutRegex, (match, p1, p2, p3) => {
console.log("COUCOU");
if (p3 === "") {
//remove the line without adding a new line / space
return "undefined";
Expand Down Expand Up @@ -238,7 +237,8 @@ function textReplacement(markdown: string, settings: GlobalSettings) {
* @param overrides
* @returns {string} converted markdown
*/
export function convertMarkdown(markdown: string, settings: EnhancedCopySettings, overrides: GlobalSettings):string {
export function convertMarkdown(markdown: string, overrides: GlobalSettings, plugin: EnhancedCopy): string {
const settings = plugin.settings;
return removeEmptyLineBeforeList(
convertSpaceSize(convertCallout(
hardBreak(
Expand All @@ -251,23 +251,23 @@ export function convertMarkdown(markdown: string, settings: EnhancedCopySettings
),
overrides
),
overrides
overrides,plugin
),
overrides),
overrides, plugin),
settings)
);
}

export function convertEditMarkdown(markdown: string, overrides: GlobalSettings, settings: EnhancedCopySettings) {
export function convertEditMarkdown(markdown: string, overrides: GlobalSettings, plugin: EnhancedCopy) {
const settings = plugin.settings;
if (settings.wikiToMarkdown) {
markdown = convertWikiToMarkdown(markdown);
markdown = removeLinksBracketsInMarkdown(markdown, overrides);
}
markdown = convertTabToSpace(markdown, settings);
markdown = removeMarkdownFootNotes(markdown, overrides);
markdown = convertCallout(markdown, overrides);
markdown = convertCallout(markdown, overrides, plugin);
markdown = removeHighlightMark(markdown, overrides);
markdown = hardBreak(markdown, overrides);
markdown = textReplacement(markdown, overrides);
return markdown;
markdown = hardBreak(markdown, overrides, plugin);
return textReplacement(markdown, overrides);
}
19 changes: 0 additions & 19 deletions src/utils/log.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import i18next from "i18next";
import {App, Editor, EditorPosition, htmlToMarkdown} from "obsidian";

import {EnhancedCopySettings} from "../interface";
import { devLog } from "./log";
import EnhancedCopy from "../main";
import {reNumerateList, replaceAllDivCalloutToBlockquote} from "./NodesEdit";

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ function getAnchor(head: EditorPosition, anchor: EditorPosition) {
* @param editor {Editor} Editor of the activeView
* @returns {string} The selected text (copying behavior of Obsidian)
*/
export function copySelectionRange(editor: Editor):string {
export function copySelectionRange(editor: Editor, plugin: EnhancedCopy):string {
let selectedText = "";
const selection = editor.listSelections();
for (const selected of selection) {
Expand All @@ -93,7 +93,7 @@ export function copySelectionRange(editor: Editor):string {
selectedText = selectedText.substring(0, selectedText.length - 1);
if (selectedText === "") {
const getSelection = activeWindow.getSelection();
devLog( i18next.t("log.empty"));
plugin.devLog( i18next.t("log.empty"));
return getSelection === null ? "" : getSelection.toString();
}
return selectedText;
Expand All @@ -106,11 +106,12 @@ export function copySelectionRange(editor: Editor):string {
* @param settings {EnhancedCopySettings}
* @returns {string}
*/
export function canvasSelectionText(app: App, settings: EnhancedCopySettings): string {
export function canvasSelectionText(app: App, plugin: EnhancedCopy): string {
const {settings} = plugin;
const editor = app.workspace.activeEditor;
if (editor) {
const editorMode = editor.editor as Editor;
return copySelectionRange(editorMode);
return copySelectionRange(editorMode, plugin);
} else {
return getSelectionAsHTML(settings);
}
Expand Down

0 comments on commit 3be59e8

Please sign in to comment.