mirror of
https://github.com/ytliu74/obsidian-pseudocode.git
synced 2026-07-22 07:40:25 +00:00
faet: Add a toggle for theme following
This commit is contained in:
parent
3f28ab42b2
commit
81bc30282f
4 changed files with 59 additions and 25 deletions
7
main.ts
7
main.ts
|
|
@ -14,7 +14,7 @@ import {
|
||||||
} from "src/latex_translator";
|
} from "src/latex_translator";
|
||||||
import { createExportButton } from "src/export_button";
|
import { createExportButton } from "src/export_button";
|
||||||
import { extractInlineMacros } from "src/inline_macro";
|
import { extractInlineMacros } from "src/inline_macro";
|
||||||
import { setObserver, detachObserver } from "src/theme";
|
import { setObserver, detachObserver, setPseudocodeTheme } from "src/theme";
|
||||||
|
|
||||||
import * as pseudocode from "pseudocode";
|
import * as pseudocode from "pseudocode";
|
||||||
|
|
||||||
|
|
@ -59,6 +59,11 @@ export default class PseudocodePlugin extends Plugin {
|
||||||
blockDiv.empty();
|
blockDiv.empty();
|
||||||
blockDiv.appendChild(errorSpan);
|
blockDiv.appendChild(errorSpan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the pseudocode theme
|
||||||
|
if (this.settings.followSystemTheme) {
|
||||||
|
setPseudocodeTheme(blockDiv);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ export interface PseudocodeSettings {
|
||||||
preambleEnabled: boolean;
|
preambleEnabled: boolean;
|
||||||
preamblePath: string;
|
preamblePath: string;
|
||||||
preambleLoadedNotice: boolean;
|
preambleLoadedNotice: boolean;
|
||||||
|
followSystemTheme: boolean;
|
||||||
jsSettings: PseudocodeJsSettings;
|
jsSettings: PseudocodeJsSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,6 +24,7 @@ export const DEFAULT_SETTINGS: PseudocodeSettings = {
|
||||||
preambleEnabled: false,
|
preambleEnabled: false,
|
||||||
preamblePath: "preamble.sty",
|
preamblePath: "preamble.sty",
|
||||||
preambleLoadedNotice: false,
|
preambleLoadedNotice: false,
|
||||||
|
followSystemTheme: false,
|
||||||
jsSettings: {
|
jsSettings: {
|
||||||
indentSize: "1.2em",
|
indentSize: "1.2em",
|
||||||
commentDelimiter: "//",
|
commentDelimiter: "//",
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { App, PluginSettingTab, Setting } from "obsidian";
|
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||||
|
import { setObserver, detachObserver } from "./theme";
|
||||||
|
|
||||||
import PseudocodePlugin from "main";
|
import PseudocodePlugin from "main";
|
||||||
|
|
||||||
|
|
@ -36,6 +37,24 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Instantiate Follow System Theme setting
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName("Follow System Theme")
|
||||||
|
.setDesc("Whether to follow the system theme.")
|
||||||
|
.addToggle((toggle) =>
|
||||||
|
toggle
|
||||||
|
.setValue(this.plugin.settings.followSystemTheme)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.followSystemTheme = value;
|
||||||
|
if (value) {
|
||||||
|
setObserver();
|
||||||
|
} else {
|
||||||
|
detachObserver();
|
||||||
|
}
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
// Instantiate Indent Size setting
|
// Instantiate Indent Size setting
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName("Indent Size")
|
.setName("Indent Size")
|
||||||
|
|
|
||||||
56
src/theme.ts
56
src/theme.ts
|
|
@ -5,51 +5,59 @@ export const themeObserver = new MutationObserver(function (mutations) {
|
||||||
const target = mutation.target as HTMLElement;
|
const target = mutation.target as HTMLElement;
|
||||||
if (
|
if (
|
||||||
// dark -> dark & light -> light
|
// dark -> dark & light -> light
|
||||||
mutation.oldValue?.contains('theme-dark') &&
|
mutation.oldValue?.contains("theme-dark") &&
|
||||||
!mutation.oldValue?.contains('theme-light') && // key line, avoid calling twice
|
!mutation.oldValue?.contains("theme-light") && // key line, avoid calling twice
|
||||||
target.classList.value.contains('theme-light')
|
target.classList.value.contains("theme-light")
|
||||||
) {
|
) {
|
||||||
console.log('light theme detected');
|
console.log("light theme detected");
|
||||||
setTheme();
|
setPseudocodeTheme();
|
||||||
} else if (
|
} else if (
|
||||||
// light -> empty -> dark
|
// light -> empty -> dark
|
||||||
mutation.oldValue?.contains('theme-light') && // key line, avoid calling twice
|
mutation.oldValue?.contains("theme-light") && // key line, avoid calling twice
|
||||||
!mutation.oldValue?.contains('theme-dark') &&
|
!mutation.oldValue?.contains("theme-dark") &&
|
||||||
target.classList.value.contains('theme-dark')
|
target.classList.value.contains("theme-dark")
|
||||||
) {
|
) {
|
||||||
console.log('dark theme detected');
|
console.log("dark theme detected");
|
||||||
setTheme();
|
setPseudocodeTheme();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function setTheme() {
|
export function setPseudocodeTheme(psBlock?: Element) {
|
||||||
|
|
||||||
const bodyElement = document.body;
|
const bodyElement = document.body;
|
||||||
const backgroundValue = getComputedStyle(bodyElement).getPropertyValue('--background-primary').trim();
|
const backgroundValue = getComputedStyle(bodyElement)
|
||||||
const fontValue = getComputedStyle(bodyElement).getPropertyValue('--text-normal').trim();
|
.getPropertyValue("--background-primary")
|
||||||
console.log(getComputedStyle(document.documentElement));
|
.trim();
|
||||||
|
const fontValue = getComputedStyle(bodyElement)
|
||||||
|
.getPropertyValue("--text-normal")
|
||||||
|
.trim();
|
||||||
console.log(backgroundValue, fontValue);
|
console.log(backgroundValue, fontValue);
|
||||||
|
|
||||||
// Select all elements with the class 'ps-root'
|
const psRootElements = psBlock
|
||||||
const psRootElements = document.querySelectorAll('.ps-root');
|
? psBlock.querySelectorAll(".ps-root")
|
||||||
|
: document.querySelectorAll(".ps-root");
|
||||||
|
// console.log(psRootElements);
|
||||||
|
|
||||||
// Loop through each element and modify the CSS properties
|
// Loop through each element and modify the CSS properties
|
||||||
psRootElements.forEach(element => {
|
psRootElements.forEach((element) => {
|
||||||
const htmlElement = element as HTMLElement;
|
const htmlElement = element as HTMLElement;
|
||||||
htmlElement.style.backgroundColor = backgroundValue;
|
htmlElement.style.backgroundColor = backgroundValue;
|
||||||
htmlElement.style.opacity = '1';
|
htmlElement.style.opacity = "1";
|
||||||
htmlElement.style.color = fontValue;
|
htmlElement.style.color = fontValue;
|
||||||
|
|
||||||
// Change border colors for .ps-algorithm and .ps-algorithm.with-caption > .ps-line:first-child
|
// Change border colors for .ps-algorithm and .ps-algorithm.with-caption > .ps-line:first-child
|
||||||
const algorithmElements = htmlElement.querySelectorAll('.ps-algorithm');
|
const algorithmElements = htmlElement.querySelectorAll(".ps-algorithm");
|
||||||
algorithmElements.forEach(algElement => {
|
algorithmElements.forEach((algElement) => {
|
||||||
const algHtmlElement = algElement as HTMLElement;
|
const algHtmlElement = algElement as HTMLElement;
|
||||||
algHtmlElement.style.borderTopColor = fontValue;
|
algHtmlElement.style.borderTopColor = fontValue;
|
||||||
algHtmlElement.style.borderBottomColor = fontValue;
|
algHtmlElement.style.borderBottomColor = fontValue;
|
||||||
});
|
});
|
||||||
|
|
||||||
const lineElements = htmlElement.querySelectorAll('.ps-algorithm.with-caption > .ps-line:first-child');
|
const lineElements = htmlElement.querySelectorAll(
|
||||||
lineElements.forEach(lineElement => {
|
".ps-algorithm.with-caption > .ps-line:first-child"
|
||||||
|
);
|
||||||
|
lineElements.forEach((lineElement) => {
|
||||||
const lineHtmlElement = lineElement as HTMLElement;
|
const lineHtmlElement = lineElement as HTMLElement;
|
||||||
lineHtmlElement.style.borderBottomColor = fontValue;
|
lineHtmlElement.style.borderBottomColor = fontValue;
|
||||||
});
|
});
|
||||||
|
|
@ -60,10 +68,10 @@ export const setObserver = () => {
|
||||||
themeObserver.observe(document.body, {
|
themeObserver.observe(document.body, {
|
||||||
attributes: true,
|
attributes: true,
|
||||||
attributeOldValue: true,
|
attributeOldValue: true,
|
||||||
attributeFilter: ['class'],
|
attributeFilter: ["class"],
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const detachObserver = () => {
|
export const detachObserver = () => {
|
||||||
themeObserver.disconnect();
|
themeObserver.disconnect();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue