generated from RyotaUshio/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef449e8
commit 3efa187
Showing
8 changed files
with
217 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,160 @@ | ||
import { Modifier, PluginSettingTab, Setting } from 'obsidian'; | ||
import MyPlugin from './main'; | ||
import { getModifierNameInPlatform } from 'utils'; | ||
|
||
|
||
// https://stackoverflow.com/a/50851710/13613783 | ||
export type BooleanKeys<T> = { [k in keyof T]: T[k] extends boolean ? k : never }[keyof T]; | ||
export type NumberKeys<T> = { [k in keyof T]: T[k] extends number ? k : never }[keyof T]; | ||
// Inspired by https://stackoverflow.com/a/50851710/13613783 | ||
export type KeysOfType<Obj, Type> = { [k in keyof Obj]: Obj[k] extends Type ? k : never }[keyof Obj]; | ||
|
||
|
||
export interface MyPluginSettings { | ||
math: boolean; | ||
// code: boolean; | ||
code: boolean; | ||
blockquote: boolean; | ||
heading: boolean; | ||
paragraph: boolean; | ||
callout: boolean; | ||
dev: boolean; | ||
math: boolean; | ||
listItem: boolean; | ||
footnoteDefinition: boolean; | ||
element: boolean; | ||
table: boolean; | ||
codeLines: number; | ||
blockquoteLines: number; | ||
paragraphLines: number; | ||
calloutLines: number; | ||
listItemLines: number; | ||
footnoteDefinitionLines: number; | ||
elementLines: number; | ||
tableLines: number; | ||
modifierToPreview: Modifier; | ||
compactPreview: boolean; | ||
dev: boolean; | ||
} | ||
|
||
export const DEFAULT_SETTINGS: MyPluginSettings = { | ||
math: true, | ||
// code: true, | ||
code: true, | ||
blockquote: true, | ||
heading: true, | ||
paragraph: true, | ||
callout: true, | ||
dev: false, | ||
math: true, | ||
listItem: true, | ||
footnoteDefinition: true, | ||
element: true, | ||
table: true, | ||
codeLines: 0, | ||
blockquoteLines: 0, | ||
paragraphLines: 0, | ||
calloutLines: 0, | ||
listItemLines: 0, | ||
footnoteDefinitionLines: 0, | ||
elementLines: 0, | ||
tableLines: 0, | ||
modifierToPreview: 'Alt', | ||
compactPreview: false, | ||
dev: false, | ||
} | ||
|
||
export class SampleSettingTab extends PluginSettingTab { | ||
constructor(public plugin: MyPlugin) { | ||
super(plugin.app, plugin); | ||
} | ||
|
||
addToggleSetting(name: string, settingName: BooleanKeys<MyPluginSettings>) { | ||
addToggleSetting(settingName: KeysOfType<MyPluginSettings, boolean>) { | ||
return new Setting(this.containerEl) | ||
.setName(name) | ||
.addToggle((toggle) => { | ||
toggle.setValue(this.plugin.settings[settingName]) | ||
toggle.onChange(async (value) => { | ||
this.plugin.settings[settingName] = value; | ||
await this.plugin.saveSettings(); | ||
}); | ||
.onChange(async (value) => { | ||
this.plugin.settings[settingName] = value; | ||
await this.plugin.saveSettings(); | ||
}); | ||
}); | ||
} | ||
|
||
addEnableSetting(name: string, settingName: BooleanKeys<MyPluginSettings>) { | ||
return this.addToggleSetting(name, settingName).setHeading(); | ||
addDropdowenSetting(settingName: KeysOfType<MyPluginSettings, string>, options: string[], display?: (option: string) => string) { | ||
return new Setting(this.containerEl) | ||
.addDropdown((dropdown) => { | ||
const displayNames = new Set<string>(); | ||
for (const option of options) { | ||
const displayName = display?.(option) ?? option; | ||
if (!displayNames.has(displayName)) { | ||
dropdown.addOption(option, displayName); | ||
displayNames.add(displayName); | ||
} | ||
}; | ||
dropdown.setValue(this.plugin.settings[settingName]) | ||
.onChange(async (value) => { | ||
// @ts-ignore | ||
this.plugin.settings[settingName] = value; | ||
await this.plugin.saveSettings(); | ||
}); | ||
}); | ||
} | ||
|
||
addSliderSetting(settingName: KeysOfType<MyPluginSettings, number>, min: number, max: number, step: number) { | ||
return new Setting(this.containerEl) | ||
.addSlider((slider) => { | ||
slider.setLimits(min, max, step) | ||
.setValue(this.plugin.settings[settingName]) | ||
.setDynamicTooltip() | ||
.onChange(async (value) => { | ||
// @ts-ignore | ||
this.plugin.settings[settingName] = value; | ||
await this.plugin.saveSettings(); | ||
}); | ||
}); | ||
} | ||
|
||
display(): void { | ||
const { containerEl } = this; | ||
containerEl.empty(); | ||
this.containerEl.empty(); | ||
|
||
this.addDropdowenSetting('modifierToPreview', ['Mod', 'Ctrl', 'Meta', 'Shift', 'Alt'], getModifierNameInPlatform) | ||
.setName('Modifier key for quick preview') | ||
.setDesc('Hold down this key to preview the link without clicking.'); | ||
this.addToggleSetting('compactPreview') | ||
.setName('Compact hover preview') | ||
.setDesc('Use compact font size for the hover preview.'); | ||
|
||
new Setting(this.containerEl).setName('Block markdown rendering').setHeading(); | ||
this.addToggleSetting('paragraph').setName('Render paragraphs'); | ||
this.addSliderSetting('paragraphLines', 0, 10, 1) | ||
.setName('Paragraph line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('heading').setName('Render headings'); | ||
this.addToggleSetting('callout').setName('Render callouts'); | ||
this.addSliderSetting('calloutLines', 0, 10, 1) | ||
.setName('Callout line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('blockquote').setName('Render blockquotes'); | ||
this.addSliderSetting('blockquoteLines', 0, 10, 1) | ||
.setName('Blockquote line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('code').setName('Render code blocks'); | ||
this.addSliderSetting('codeLines', 0, 10, 1) | ||
.setName('Code block line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('math').setName('Render math blocks'); | ||
this.addToggleSetting('listItem').setName('Render list items'); | ||
this.addSliderSetting('listItemLines', 0, 10, 1) | ||
.setName('List item line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('table').setName('Render tables'); | ||
this.addSliderSetting('tableLines', 0, 10, 1) | ||
.setName('Table line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('footnoteDefinition').setName('Render footnote definitions'); | ||
this.addSliderSetting('footnoteDefinitionLines', 0, 10, 1) | ||
.setName('Footnote definition line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
this.addToggleSetting('element').setName('Render elements'); | ||
this.addSliderSetting('elementLines', 0, 10, 1) | ||
.setName('Element line limit') | ||
.setDesc('Maximum number of lines to render. Set to 0 to disable line limit.'); | ||
|
||
new Setting(this.containerEl).setName('Advanced').setHeading(); | ||
|
||
this.addEnableSetting('Math blocks', 'math'); | ||
this.addEnableSetting('Callouts', 'callout'); | ||
this.addToggleSetting('Compact hover preview', 'compactPreview'); | ||
this.addToggleSetting('Dev mode', 'dev'); | ||
this.addToggleSetting('dev') | ||
.setName('Dev mode') | ||
.setDesc('Show metadata about suggestion items in the dev console.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Modifier, Platform } from "obsidian"; | ||
|
||
|
||
export function extractFirstNLines(text: string, n: number) { | ||
const lines = text.split('\n'); | ||
return lines.slice(0, n).join('\n'); | ||
} | ||
|
||
export function render(el: HTMLElement, cb: (containerEl: HTMLElement) => void) { | ||
const titleEl = el.querySelector<HTMLElement>('.suggestion-title'); | ||
if (titleEl) { | ||
const containerEl = createDiv({cls: ['markdown-rendered']}); | ||
titleEl.replaceChildren(containerEl); | ||
cb(containerEl); | ||
}; | ||
} | ||
|
||
export function getModifierNameInPlatform(mod: Modifier): string { | ||
if (mod == "Mod") { | ||
return Platform.isMacOS || Platform.isIosApp ? "command" : "Ctrl"; | ||
} | ||
if (mod == "Shift") { | ||
return Platform.isMacOS || Platform.isIosApp ? "shift" : "Shift"; | ||
} | ||
if (mod == "Alt") { | ||
return Platform.isMacOS || Platform.isIosApp ? "option" : "Alt"; | ||
} | ||
if (mod == "Meta") { | ||
return Platform.isMacOS || Platform.isIosApp ? "command" : Platform.isWin ? "Win" : "Meta"; | ||
} | ||
return "ctrl"; | ||
} |
Oops, something went wrong.