mirror of
https://github.com/dragonish/obsidian-heading-decorator.git
synced 2026-07-22 05:42:05 +00:00
feat(source): allow to hide number signs (#) in source mode
Resolve: #11
This commit is contained in:
parent
da51e18613
commit
cc7a05af32
4 changed files with 86 additions and 2 deletions
|
|
@ -60,6 +60,10 @@ interface HeadingDecoratorSettings {
|
|||
unorderedLevelHeadings: string;
|
||||
}
|
||||
|
||||
interface SourceHeadingDecoratorSettngs extends HeadingDecoratorSettings {
|
||||
hideNumberSigns?: boolean;
|
||||
}
|
||||
|
||||
export type HeadingPluginSettings = {
|
||||
metadataKeyword: string;
|
||||
folderBlacklist: string[];
|
||||
|
|
@ -69,7 +73,11 @@ export type HeadingPluginSettings = {
|
|||
enabledInSource: boolean;
|
||||
enabledInOutline: boolean;
|
||||
enabledInFileExplorer: boolean;
|
||||
} & Record<PluginDecoratorSettingsType, HeadingDecoratorSettings>;
|
||||
} & Record<
|
||||
Exclude<PluginDecoratorSettingsType, "sourceSettings">,
|
||||
HeadingDecoratorSettings
|
||||
> &
|
||||
Record<"sourceSettings", SourceHeadingDecoratorSettngs>;
|
||||
|
||||
export type HeadingPluginData = Omit<
|
||||
HeadingPluginSettings,
|
||||
|
|
@ -99,6 +107,7 @@ export const beforeDecoratorClassName = "before-heading-decorator";
|
|||
export const beforeInsideDecoratorClassName = "before-inside-heading-decorator";
|
||||
export const afterDecoratorClassName = "after-heading-decorator";
|
||||
export const afterInsideDecoratorClassName = "after-inside-heading-decorator";
|
||||
export const hideSourceNumberSignsClassName = "hide-source-number-signs";
|
||||
export const headingsSelector =
|
||||
".el-h1 h1, .el-h2 h2, .el-h3 h3, .el-h4 h4, .el-h5 h5, .el-h6 h6";
|
||||
export const defaultHeadingTuple: HeadingTuple = [
|
||||
|
|
@ -184,6 +193,19 @@ export function defaultHeadingDecoratorSettings(): HeadingDecoratorSettings {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Default source settings for source heading decorator.
|
||||
*
|
||||
* @returns default source settings for heading decorator.
|
||||
*/
|
||||
export function defaultSourceHeadingDecoratorSettings(): SourceHeadingDecoratorSettngs {
|
||||
const settings = defaultHeadingDecoratorSettings();
|
||||
return {
|
||||
...settings,
|
||||
hideNumberSigns: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unordered level headings from settings.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import {
|
|||
getUnorderedLevelHeadings,
|
||||
getOrderedCustomIdents,
|
||||
findFirstCharacterIndex,
|
||||
hideSourceNumberSignsClassName,
|
||||
} from "../common/data";
|
||||
import { Counter, Querier } from "../common/counter";
|
||||
import { Heading } from "../common/heading";
|
||||
|
|
@ -180,14 +181,45 @@ export class HeadingViewPlugin implements PluginValue {
|
|||
block: false,
|
||||
});
|
||||
|
||||
let hideDeco: Decoration | null = null;
|
||||
if (!isLivePreviwMode) {
|
||||
const hideNumberSigns = pluginData.sourceSettings.hideNumberSigns;
|
||||
if (hideNumberSigns) {
|
||||
hideDeco = Decoration.mark({
|
||||
class: hideSourceNumberSignsClassName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (position === "before-inside") {
|
||||
const charIndex = isLivePreviwMode
|
||||
? findFirstCharacterIndex(lineText)
|
||||
: 0;
|
||||
builder.add(line.from + charIndex, line.from + charIndex, deco);
|
||||
|
||||
if (hideDeco) {
|
||||
const cIndex = findFirstCharacterIndex(lineText);
|
||||
if (cIndex > 0) {
|
||||
builder.add(line.from, line.from + cIndex, hideDeco);
|
||||
}
|
||||
}
|
||||
} else if (position === "before") {
|
||||
builder.add(line.from, line.from, deco);
|
||||
|
||||
if (hideDeco) {
|
||||
const cIndex = findFirstCharacterIndex(lineText);
|
||||
if (cIndex > 0) {
|
||||
builder.add(line.from, line.from + cIndex, hideDeco);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (hideDeco) {
|
||||
const cIndex = findFirstCharacterIndex(lineText);
|
||||
if (cIndex > 0) {
|
||||
builder.add(line.from, line.from + cIndex, hideDeco);
|
||||
}
|
||||
}
|
||||
|
||||
builder.add(line.to, line.to, deco);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
24
main.ts
24
main.ts
|
|
@ -20,6 +20,7 @@ import {
|
|||
headingsSelector,
|
||||
orderedStyleTypeOptions,
|
||||
defaultHeadingDecoratorSettings,
|
||||
defaultSourceHeadingDecoratorSettings,
|
||||
getUnorderedLevelHeadings,
|
||||
getOrderedCustomIdents,
|
||||
diffLevel,
|
||||
|
|
@ -72,7 +73,7 @@ const DEFAULT_SETTINGS: HeadingPluginSettings = {
|
|||
enabledInPreview: true,
|
||||
previewSettings: defaultHeadingDecoratorSettings(),
|
||||
enabledInSource: false,
|
||||
sourceSettings: defaultHeadingDecoratorSettings(),
|
||||
sourceSettings: defaultSourceHeadingDecoratorSettings(),
|
||||
enabledInOutline: false,
|
||||
outlineSettings: defaultHeadingDecoratorSettings(),
|
||||
enabledInFileExplorer: false,
|
||||
|
|
@ -1348,6 +1349,27 @@ class HeadingSettingTab extends PluginSettingTab {
|
|||
})
|
||||
);
|
||||
|
||||
if (settingsType === "sourceSettings") {
|
||||
new Setting(containerEl).setName("Other").setHeading();
|
||||
|
||||
//* hideNumberSigns
|
||||
new Setting(containerEl)
|
||||
.setName("Hide number signs on inactive lines")
|
||||
.setDesc(
|
||||
"Hide number signs (#) on inactive lines similar to live preview."
|
||||
)
|
||||
.addToggle((toggle) => {
|
||||
toggle
|
||||
.setValue(
|
||||
this.plugin.settings[settingsType].hideNumberSigns ?? false
|
||||
)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings[settingsType].hideNumberSigns = value;
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//* Scroll back to the top
|
||||
containerEl.scrollTo({ top: 0, behavior: "smooth" });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -262,6 +262,14 @@ body {
|
|||
.custom-heading-decorator.source-custom-heading-decorator[data-decorator-opacity="100%"] {
|
||||
opacity: 100%;
|
||||
}
|
||||
|
||||
.hide-source-number-signs {
|
||||
display: none;
|
||||
}
|
||||
.cm-focused .cm-active .hide-source-number-signs,
|
||||
.cm-focused .cm-active .hide-source-number-signs {
|
||||
display: unset;
|
||||
}
|
||||
/* source end */
|
||||
|
||||
/* outline start */
|
||||
|
|
|
|||
Loading…
Reference in a new issue