Add manual setting to control if palettes are enabled

This commit is contained in:
Benji Grant 2025-10-24 19:01:57 +11:00
parent 8b446c94f0
commit 9c6aac875f
No known key found for this signature in database
GPG key ID: D41929A51D291D4D
3 changed files with 19 additions and 43 deletions

View file

@ -1,5 +1,4 @@
import { Plugin } from 'obsidian'
import { checkIfSnippetEnabled } from 'src/palettes'
import { inlayPostProcessor } from 'src/preview'
import { inlayExtension } from './live'
import {
@ -29,12 +28,6 @@ export default class CssColorsPlugin extends Plugin {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())
}
async loadData(): Promise<typeof this.settings> {
const data = await super.loadData()
const { enabled: palettesEnabled } = await checkIfSnippetEnabled(this.app)
return { ...data, palettesEnabled }
}
async saveSettings() {
await this.saveData(this.settings)
}

View file

@ -1,6 +1,6 @@
import { type App, Modal, Notice, normalizePath, Setting } from 'obsidian'
export const SNIPPET_NAME = 'css-inlay-palettes'
const SNIPPET_NAME = 'css-inlay-palettes'
/** Normalize a palette name into a class */
export const getPaletteClass = (text: string) =>
@ -57,24 +57,23 @@ const createPaletteFilePath = (app: App) =>
normalizePath(`${app.vault.configDir}/snippets/${SNIPPET_NAME}.css`)
/** Snippet exists and is enabled in Obsidian Appearance settings */
export const checkIfSnippetEnabled = async (app: App) => {
const checkIfSnippetEnabled = async (app: App) => {
if (!(await app.vault.adapter.exists(createPaletteFilePath(app))))
return { exists: false, enabled: false }
return false
const config = await app.vault.adapter.read(
normalizePath(`${app.vault.configDir}/appearance.json`),
)
try {
const enabledSnippets: string[] = JSON.parse(config).enabledCssSnippets
return { exists: true, enabled: enabledSnippets.includes(SNIPPET_NAME) }
return enabledSnippets.includes(SNIPPET_NAME)
} catch {
return { exists: true, enabled: false }
return false
}
}
const confirmIfOverwrite = async (app: App) => {
const { exists } = await checkIfSnippetEnabled(app)
if (exists)
if (await app.vault.adapter.exists(createPaletteFilePath(app)))
return new Promise<boolean>((resolve) => {
new ConfirmDownloadModal(app, (confirmed) => resolve(confirmed)).open()
})

View file

@ -1,10 +1,6 @@
import { type App, PluginSettingTab, Setting } from 'obsidian'
import type CssColorsPlugin from './main'
import {
checkIfSnippetEnabled,
downloadPredefinedPalettes,
SNIPPET_NAME,
} from './palettes'
import { downloadPredefinedPalettes } from './palettes'
export interface CssColorsPluginSettings {
showInLiveEditor: boolean
@ -91,42 +87,30 @@ export class CssColorsSettingsTab extends PluginSettingTab {
}),
)
let palettesEnabledStatus: HTMLSpanElement | undefined
new Setting(containerEl).setHeading().setName('Custom Palettes')
new Setting(containerEl)
.setHeading()
.setName('Custom Palettes')
.setName('Enable palette support')
.setDesc(
createFragment((desc) => {
desc.appendText(
`Create and enable a CSS snippet called ${SNIPPET_NAME}.css. This will generate classes for every inline code block surrounded in parentheses so they can be targeted.`,
'Enable custom color palette support. This will generate classes for every inline code block surrounded in parentheses so they can be targeted with a CSS snippet file.',
)
desc.createEl('br')
desc.appendText('This feature is currently: ')
palettesEnabledStatus = desc.createSpan({ text: 'loading...' })
desc.createEl('br')
desc.createEl('a', {
text: 'Learn more',
href: 'https://github.com/GRA0007/obsidian-css-inlay-colors?tab=readme-ov-file#custom-palettes',
})
}),
)
// Display palette
checkIfSnippetEnabled(this.app).then(({ exists, enabled }) => {
if (!palettesEnabledStatus) return
palettesEnabledStatus.empty()
palettesEnabledStatus.createEl('strong', {
cls: enabled ? 'mod-success' : 'mod-warning',
text: enabled ? 'enabled' : 'disabled',
})
if (!enabled) {
palettesEnabledStatus.appendText(
exists
? ' (snippet exists but is currently disabled)'
: ' (no snippet file found)',
)
}
})
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.palettesEnabled)
.onChange(async (value) => {
this.plugin.settings.palettesEnabled = value
await this.plugin.saveSettings()
}),
)
new Setting(containerEl)
.setName('Download predefined palettes')